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 type CollectionMode = 'NFT' | 'Fungible' | 'ReFungible';90export type CreateCollectionParams = {91 mode: CollectionMode,92 name: string,93 description: string,94 tokenPrefix: string95};9697const defaultCreateCollectionParams: CreateCollectionParams = {98 name: 'name',99 description: 'description',100 mode: 'NFT',101 tokenPrefix: 'prefix'102}103104export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {105 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};106107 let collectionId: number = 0;108 await usingApi(async (api) => {109 110 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());111112 113 const alicePrivateKey = privateKey('//Alice');114 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);115 const events = await submitTransactionAsync(alicePrivateKey, tx);116 const result = getCreateCollectionResult(events);117118 119 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());120121 122 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();123124 125 expect(result.success).to.be.true;126 expect(result.collectionId).to.be.equal(BcollectionCount);127 expect(collection).to.be.not.null;128 expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');129 expect(collection.Owner).to.be.equal(alicesPublicKey);130 expect(utf16ToStr(collection.Name)).to.be.equal(name);131 expect(utf16ToStr(collection.Description)).to.be.equal(description);132 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);133134 collectionId = result.collectionId;135 });136137 return collectionId;138}139 140export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {141 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};142143 await usingApi(async (api) => {144 145 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());146147 148 const alicePrivateKey = privateKey('//Alice');149 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);150 const events = await submitTransactionAsync(alicePrivateKey, tx);151 const result = getCreateCollectionResult(events);152153 154 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());155156 157 expect(result.success).to.be.false;158 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');159 });160}161 162export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {163 let bal = new BigNumber(0);164 let unused;165 do {166 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000));167 const keyring = new Keyring({ type: 'sr25519' });168 unused = keyring.addFromUri(`//${randomSeed}`);169 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());170 } while (bal.toFixed() != '0');171 return unused; 172}173174function getDestroyResult(events: EventRecord[]): boolean {175 let success: boolean = false;176 events.forEach(({ phase, event: { data, method, section } }) => {177 178 if (method == 'ExtrinsicSuccess') {179 success = true;180 }181 });182 return success;183}184185export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {186 await usingApi(async (api) => {187 188 const alicePrivateKey = privateKey(senderSeed);189 const tx = api.tx.nft.destroyCollection(collectionId);190 const events = await submitTransactionAsync(alicePrivateKey, tx);191 const result = getDestroyResult(events);192193 194 expect(result).to.be.false;195 });196}197198export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {199 await usingApi(async (api) => {200 201 const alicePrivateKey = privateKey(senderSeed);202 const tx = api.tx.nft.destroyCollection(collectionId);203 const events = await submitTransactionAsync(alicePrivateKey, tx);204 const result = getDestroyResult(events);205206 207 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();208209 210 expect(result).to.be.true;211 expect(collection).to.be.not.null;212 expect(collection.Owner).to.be.equal(nullPublicKey);213 });214}215216export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {217 await usingApi(async (api) => {218219 220 const alicePrivateKey = privateKey('//Alice');221 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);222 const events = await submitTransactionAsync(alicePrivateKey, tx);223 const result = getGenericResult(events);224225 226 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();227228 229 expect(result.success).to.be.true;230 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());231 expect(collection.SponsorConfirmed).to.be.false;232 });233}234235export async function removeCollectionSponsorExpectSuccess(collectionId: number) {236 await usingApi(async (api) => {237238 239 const alicePrivateKey = privateKey('//Alice');240 const tx = api.tx.nft.removeCollectionSponsor(collectionId);241 const events = await submitTransactionAsync(alicePrivateKey, tx);242 const result = getGenericResult(events);243244 245 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();246247 248 expect(result.success).to.be.true;249 expect(collection.Sponsor).to.be.equal(nullPublicKey);250 expect(collection.SponsorConfirmed).to.be.false;251 });252}253254export async function removeCollectionSponsorExpectFailure(collectionId: number) {255 await usingApi(async (api) => {256257 258 const alicePrivateKey = privateKey('//Alice');259 const tx = api.tx.nft.removeCollectionSponsor(collectionId);260 const events = await submitTransactionAsync(alicePrivateKey, tx);261 const result = getGenericResult(events);262263 264 expect(result.success).to.be.false;265 });266}267268export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {269 await usingApi(async (api) => {270271 272 const alicePrivateKey = privateKey(senderSeed);273 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);274 const events = await submitTransactionAsync(alicePrivateKey, tx);275 const result = getGenericResult(events);276277 278 expect(result.success).to.be.false;279 });280}281282export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {283 await usingApi(async (api) => {284285 286 const sender = privateKey(senderSeed);287 const tx = api.tx.nft.confirmSponsorship(collectionId);288 const events = await submitTransactionAsync(sender, tx);289 const result = getGenericResult(events);290291 292 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();293294 295 expect(result.success).to.be.true;296 expect(collection.Sponsor).to.be.equal(sender.address);297 expect(collection.SponsorConfirmed).to.be.true;298 });299}300301export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {302 await usingApi(async (api) => {303304 305 const sender = privateKey(senderSeed);306 const tx = api.tx.nft.confirmSponsorship(collectionId);307 const events = await submitTransactionAsync(sender, tx);308 const result = getGenericResult(events);309310 311 expect(result.success).to.be.false;312 });313}314315export interface CreateFungibleData extends Struct {316 readonly value: u128;317};318319export interface CreateReFungibleData extends Struct {};320export interface CreateNftData extends Struct {};321322export interface CreateItemData extends Enum {323 NFT: CreateNftData,324 Fungible: CreateFungibleData,325 ReFungible: CreateReFungibleData326};327328export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {329 let newItemId: number = 0;330 await usingApi(async (api) => {331 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());332 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON(); 333 const AItemBalance = new BigNumber(Aitem.Value);334335 if (owner === '') owner = sender.address;336337 let tx;338 if (createMode == 'Fungible') {339 let createData = {fungible: {value: 10}};340 tx = api.tx.nft.createItem(collectionId, owner, createData);341 }342 else {343 tx = api.tx.nft.createItem(collectionId, owner, createMode);344 }345 const events = await submitTransactionAsync(sender, tx);346 const result = getCreateItemResult(events);347348 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());349 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON(); 350 const BItemBalance = new BigNumber(Bitem.Value);351352 353 expect(result.success).to.be.true;354 if (createMode == 'Fungible') {355 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);356 }357 else {358 expect(BItemCount).to.be.equal(AItemCount+1);359 }360 expect(collectionId).to.be.equal(result.collectionId);361 expect(BItemCount).to.be.equal(result.itemId);362 newItemId = result.itemId;363 });364 return newItemId;365}366367export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {368 await usingApi(async (api) => {369370 371 const tx = api.tx.nft.setPublicAccessMode(collectionId, 'WhiteList');372 const events = await submitTransactionAsync(sender, tx);373 const result = getGenericResult(events);374375 376 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();377378 379 expect(result.success).to.be.true;380 expect(collection.Access).to.be.equal('WhiteList');381 });382}383384export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {385 await usingApi(async (api) => {386387 388 const tx = api.tx.nft.setMintPermission(collectionId, true);389 const events = await submitTransactionAsync(sender, tx);390 const result = getGenericResult(events);391392 393 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();394395 396 expect(result.success).to.be.true;397 expect(collection.MintMode).to.be.equal(true);398 });399}400401export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {402 await usingApi(async (api) => {403404 405 const tx = api.tx.nft.addToWhiteList(collectionId, address);406 const events = await submitTransactionAsync(sender, tx);407 const result = getGenericResult(events);408409 410 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();411412 413 expect(result.success).to.be.true;414 expect(collection.MintMode).to.be.equal(true);415 });416}417