git.delta.rocks / unique-network / refs/commits / 538026a2f540

difftreelog

source

examples/testnft/create_collection.js2.2 KiBsourcehistory
1const { ApiPromise, WsProvider, Keyring } = require('@polkadot/api');2const config = require('./config');3var BigNumber = require('bn.js');4const fs = require('fs');56function submitTransaction(sender, transaction) {7  return new Promise(async function(resolve, reject) {8    try {9      const unsub = await transaction10      .signAndSend(sender, (result) => {11        console.log(`Current tx status is ${result.status}`);1213        if (result.status.isInBlock) {14          console.log(`Transaction included at blockHash ${result.status.asInBlock}`);15          resolve();16          unsub();17        } else if (result.status.isFinalized) {18          console.log(`Transaction finalized at blockHash ${result.status.asFinalized}`);19          resolve();20          unsub();21        }22      });23    }24    catch (e) {25      reject(e.toString());26    }27  });28}2930async function createCollectionAsync(api, alice) {31  // Test NFT32  const name = [0x54, 0x65, 0x73, 0x74, 0x20, 0x4e, 0x46, 0x54];33  // Test NFT Collection34  const description = [0x54, 0x65, 0x73, 0x74, 0x20, 0x4e, 0x46, 0x54, 0x20, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e];35  // TestNFT36  const tokenPrefix = [0x54, 0x65, 0x73, 0x74, 0x4e, 0x46, 0x54];3738  // Mode: NFT39  const tx = api.tx.nft.createCollection(name, description, tokenPrefix, {"NFT": config.collectionDataSize});40  await submitTransaction(alice, tx);4142  const collectionId = 1;4344  const tx2 = api.tx.nft.setOffchainSchema(collectionId, "https://ipfs-gateway.usetech.com/ipfs/QmVdFFZjnq6i3fNDjm6FQe2tfAtUDnqMNkBh8e4sYWUmbH/images/image{id}.png");45  await submitTransaction(alice, tx2);46}4748async function main() {49  // Initialise the provider to connect to the node50  const wsProvider = new WsProvider(config.wsEndpoint);5152  // Create the API and wait until ready53  const api = await ApiPromise.create({54    provider: wsProvider,55  });5657  // Owners's keypair58  const keyring = new Keyring({ type: 'sr25519' });59  const owner = keyring.addFromUri(config.ownerSeed);60  console.log("Collection owner address: ", owner.address);6162  // Create collection as owner63  console.log("=== Create collection ===");64  await createCollectionAsync(api, owner);65}6667main().catch(console.error).finally(() => process.exit());