git.delta.rocks / unique-network / refs/commits / 70da175fb9fe

difftreelog

source

examples/testnft/create_tokens.js2.3 KiBsourcehistory
1const { ApiPromise, WsProvider, Keyring } = require('@polkadot/api');2const config = require('./config');3const fs = require('fs');45const collectionId = 1;67function checkOwner(owner) {8  for (let i=0; i<32; i++) {9    if (owner[i] != 0) return true;10  }11  return false;12}1314function mintAsync(api, admin) {15  return new Promise(async function(resolve, reject) {16    const unsub = await api.tx.nft17      .createItem(collectionId, "0x", admin.address)18      .signAndSend(admin, (result) => {19        console.log(`Current tx status is ${result.status}`);20    21        if (result.status.isInBlock) {22          console.log(`Transaction included at blockHash ${result.status.asInBlock}`);23          resolve();24          unsub();25        } else if (result.status.isFinalized) {26          console.log(`Transaction finalized at blockHash ${result.status.asFinalized}`);27          resolve();28          unsub();29        }30      });31  });32}3334async function main() {35  // Initialise the provider to connect to the node36  const wsProvider = new WsProvider(config.wsEndpoint);37  const rtt = require("./runtime_types.json");3839  // Create the API and wait until ready40  const api = await ApiPromise.create({ 41    provider: wsProvider,42    types: rtt43  });4445  // Retrieve the chain & node information information via rpc calls46  const [chain, nodeName, nodeVersion, collection] = await Promise.all([47    api.rpc.system.chain(),48    api.rpc.system.name(),49    api.rpc.system.version(),50    api.query.nft.collection(collectionId)51  ]);5253  console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);54  console.log(`Collection: ${collection}`);5556  if (checkOwner(collection.Owner.toString())) {57    // Import account from mnemonic phrase in config file58    const keyring = new Keyring({ type: 'sr25519' });59    const owner = keyring.addFromUri(config.ownerSeed);60    console.log("Owner address: ", owner.address)6162    // Create Tokens63    for (let i=0; i<config.totalTokens; i++) {64      console.log(`=== Importing Token ${i+1} of ${config.totalTokens} ===`);6566      // Mint67      await mintAsync(api, owner);68    }6970  }71  else {72    console.log("\nERROR: Collection not found.\nCheck the ID and make sure you have created collection and set the admin");73  }7475}7677main().catch(console.error).finally(() => process.exit());