git.delta.rocks / unique-network / refs/commits / 770feb59678f

difftreelog

source

examples/testnft/create_tokens.js2.2 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}`);2021        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);3738  // Create the API and wait until ready39  const api = await ApiPromise.create({40    provider: wsProvider,41  });4243  // Retrieve the chain & node information information via rpc calls44  const [chain, nodeName, nodeVersion, collection] = await Promise.all([45    api.rpc.system.chain(),46    api.rpc.system.name(),47    api.rpc.system.version(),48    api.query.nft.collection(collectionId)49  ]);5051  console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);52  console.log(`Collection: ${collection}`);5354  if (checkOwner(collection.owner.toString())) {55    // Import account from mnemonic phrase in config file56    const keyring = new Keyring({ type: 'sr25519' });57    const owner = keyring.addFromUri(config.ownerSeed);58    console.log("Owner address: ", owner.address)5960    // Create Tokens61    for (let i=0; i<config.totalTokens; i++) {62      console.log(`=== Importing Token ${i+1} of ${config.totalTokens} ===`);6364      // Mint65      await mintAsync(api, owner);66    }6768  }69  else {70    console.log("\nERROR: Collection not found.\nCheck the ID and make sure you have created collection and set the admin");71  }7273}7475main().catch(console.error).finally(() => process.exit());