123456import chai from 'chai';7import chaiAsPromised from 'chai-as-promised';8import type { AccountId, EventRecord } from '@polkadot/types/interfaces';9import { ApiPromise, Keyring } from "@polkadot/api";10import { default as usingApi, submitTransactionAsync } from "../substrate/substrate-api";11import privateKey from '../substrate/privateKey';12import { alicesPublicKey, nullPublicKey } from "../accounts";13import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';14import { IKeyringPair } from "@polkadot/types/types";15import { BigNumber } from 'bignumber.js';16import { Struct, Enum } from '@polkadot/types/codec';17import { u128 } from '@polkadot/types/primitive';1819chai.use(chaiAsPromised);20const expect = chai.expect;2122type GenericResult = {23 success: boolean,24};2526type CreateCollectionResult = {27 success: boolean,28 collectionId: number29};3031type CreateItemResult = {32 success: boolean,33 collectionId: number,34 itemId: number35};3637export function getGenericResult(events: EventRecord[]): GenericResult {38 let result: GenericResult = {39 success: false40 }41 events.forEach(({ phase, event: { data, method, section } }) => {42 43 if (method == 'ExtrinsicSuccess') {44 result.success = true;45 }46 });47 return result;48}4950function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {51 let success = false;52 let collectionId: number = 0;53 events.forEach(({ phase, event: { data, method, section } }) => {54 55 if (method == 'ExtrinsicSuccess') {56 success = true;57 } else if ((section == 'nft') && (method == 'Created')) {58 collectionId = parseInt(data[0].toString());59 }60 });61 let result: CreateCollectionResult = {62 success,63 collectionId64 }65 return result;66}6768function getCreateItemResult(events: EventRecord[]): CreateItemResult {69 let success = false;70 let collectionId: number = 0;71 let itemId: number = 0;72 events.forEach(({ phase, event: { data, method, section } }) => {73 74 if (method == 'ExtrinsicSuccess') {75 success = true;76 } else if ((section == 'nft') && (method == 'ItemCreated')) {77 collectionId = parseInt(data[0].toString());78 itemId = parseInt(data[1].toString());79 }80 });81 let result: CreateItemResult = {82 success,83 collectionId,84 itemId85 }86 return result;87}8889export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string): Promise<number> {90 let collectionId: number = 0;91 await usingApi(async (api) => {92 93 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());9495 96 const alicePrivateKey = privateKey('//Alice');97 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);98 const events = await submitTransactionAsync(alicePrivateKey, tx);99 const result = getCreateCollectionResult(events);100101 102 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());103104 105 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();106107 108 expect(result.success).to.be.true;109 expect(result.collectionId).to.be.equal(BcollectionCount);110 expect(collection).to.be.not.null;111 expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');112 expect(collection.Owner).to.be.equal(alicesPublicKey);113 expect(utf16ToStr(collection.Name)).to.be.equal(name);114 expect(utf16ToStr(collection.Description)).to.be.equal(description);115 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);116117 collectionId = result.collectionId;118 });119120 return collectionId;121}122 123export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {124 await usingApi(async (api) => {125 126 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());127128 129 const alicePrivateKey = privateKey('//Alice');130 const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);131 const events = await submitTransactionAsync(alicePrivateKey, tx);132 const result = getCreateCollectionResult(events);133134 135 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());136137 138 expect(result.success).to.be.false;139 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');140 });141}142 143export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {144 let bal = new BigNumber(0);145 let unused;146 do {147 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000));148 const keyring = new Keyring({ type: 'sr25519' });149 unused = keyring.addFromUri(`//${randomSeed}`);150 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());151 } while (bal.toFixed() != '0');152 return unused; 153}154155function getDestroyResult(events: EventRecord[]): boolean {156 let success: boolean = false;157 events.forEach(({ phase, event: { data, method, section } }) => {158 159 if (method == 'ExtrinsicSuccess') {160 success = true;161 }162 });163 return success;164}165166export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {167 await usingApi(async (api) => {168 169 const alicePrivateKey = privateKey(senderSeed);170 const tx = api.tx.nft.destroyCollection(collectionId);171 const events = await submitTransactionAsync(alicePrivateKey, tx);172 const result = getDestroyResult(events);173174 175 expect(result).to.be.false;176 });177}178179export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {180 await usingApi(async (api) => {181 182 const alicePrivateKey = privateKey(senderSeed);183 const tx = api.tx.nft.destroyCollection(collectionId);184 const events = await submitTransactionAsync(alicePrivateKey, tx);185 const result = getDestroyResult(events);186187 188 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();189190 191 expect(result).to.be.true;192 expect(collection).to.be.not.null;193 expect(collection.Owner).to.be.equal(nullPublicKey);194 });195}196197export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {198 await usingApi(async (api) => {199200 201 const alicePrivateKey = privateKey('//Alice');202 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);203 const events = await submitTransactionAsync(alicePrivateKey, tx);204 const result = getGenericResult(events);205206 207 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();208209 210 expect(result.success).to.be.true;211 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());212 expect(collection.SponsorConfirmed).to.be.false;213 });214}215216export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {217 await usingApi(async (api) => {218219 220 const alicePrivateKey = privateKey(senderSeed);221 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);222 const events = await submitTransactionAsync(alicePrivateKey, tx);223 const result = getGenericResult(events);224225 226 expect(result.success).to.be.false;227 });228}229230export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {231 await usingApi(async (api) => {232233 234 const sender = privateKey(senderSeed);235 const tx = api.tx.nft.confirmSponsorship(collectionId);236 const events = await submitTransactionAsync(sender, tx);237 const result = getGenericResult(events);238239 240 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();241242 243 expect(result.success).to.be.true;244 expect(collection.Sponsor).to.be.equal(sender.address);245 expect(collection.SponsorConfirmed).to.be.true;246 });247}248249export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {250 await usingApi(async (api) => {251252 253 const sender = privateKey(senderSeed);254 const tx = api.tx.nft.confirmSponsorship(collectionId);255 const events = await submitTransactionAsync(sender, tx);256 const result = getGenericResult(events);257258 259 expect(result.success).to.be.false;260 });261}262263export interface CreateFungibleData extends Struct {264 readonly value: u128;265};266267export interface CreateReFungibleData extends Struct {};268export interface CreateNftData extends Struct {};269270export interface CreateItemData extends Enum {271 NFT: CreateNftData,272 Fungible: CreateFungibleData,273 ReFungible: CreateReFungibleData274};275276export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {277 let newItemId: number = 0;278 await usingApi(async (api) => {279 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());280 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON(); 281 const AItemBalance = new BigNumber(Aitem.Value);282283 if (owner === '') owner = sender.address;284285 let tx;286 if (createMode == 'Fungible') {287 let createData = {fungible: {value: 10}};288 tx = api.tx.nft.createItem(collectionId, owner, createData);289 }290 else {291 tx = api.tx.nft.createItem(collectionId, owner, createMode);292 }293 const events = await submitTransactionAsync(sender, tx);294 const result = getCreateItemResult(events);295296 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());297 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON(); 298 const BItemBalance = new BigNumber(Bitem.Value);299300 301 expect(result.success).to.be.true;302 if (createMode == 'Fungible') {303 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);304 }305 else {306 expect(BItemCount).to.be.equal(AItemCount+1);307 }308 expect(collectionId).to.be.equal(result.collectionId);309 expect(BItemCount).to.be.equal(result.itemId);310 newItemId = result.itemId;311 });312 return newItemId;313}314315export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {316 await usingApi(async (api) => {317318 319 const tx = api.tx.nft.setPublicAccessMode(collectionId, 'WhiteList');320 const events = await submitTransactionAsync(sender, tx);321 const result = getGenericResult(events);322323 324 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();325326 327 expect(result.success).to.be.true;328 expect(collection.Access).to.be.equal('WhiteList');329 });330}331332export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {333 await usingApi(async (api) => {334335 336 const tx = api.tx.nft.setMintPermission(collectionId, true);337 const events = await submitTransactionAsync(sender, tx);338 const result = getGenericResult(events);339340 341 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();342343 344 expect(result.success).to.be.true;345 expect(collection.MintMode).to.be.equal(true);346 });347}348349export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {350 await usingApi(async (api) => {351352 353 const tx = api.tx.nft.addToWhiteList(collectionId, address);354 const events = await submitTransactionAsync(sender, tx);355 const result = getGenericResult(events);356357 358 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();359360 361 expect(result.success).to.be.true;362 expect(collection.MintMode).to.be.equal(true);363 });364}365