difftreelog
Confirm sponsorship tests in progress
in: master
3 files changed
tests/src/confirmSponsorship.test.tsdiffbeforeafterboth--- a/tests/src/confirmSponsorship.test.ts
+++ b/tests/src/confirmSponsorship.test.ts
@@ -13,10 +13,14 @@
setCollectionSponsorExpectFailure,
confirmSponsorshipExpectSuccess,
confirmSponsorshipExpectFailure,
+ createItemExpectSuccess,
+ findUnusedAddress,
+ getGenericResult,
} from "./util/helpers";
import { Keyring } from "@polkadot/api";
import { IKeyringPair } from "@polkadot/types/types";
import type { AccountId } from '@polkadot/types/interfaces';
+import { BigNumber } from 'bignumber.js';
chai.use(chaiAsPromised);
const expect = chai.expect;
@@ -30,6 +34,7 @@
before(async () => {
await usingApi(async (api) => {
const keyring = new Keyring({ type: 'sr25519' });
+ alice = keyring.addFromUri(`//Alice`);
bob = keyring.addFromUri(`//Bob`);
charlie = keyring.addFromUri(`//Charlie`);
});
@@ -53,11 +58,31 @@
await setCollectionSponsorExpectSuccess(collectionId, charlie.address);
});
- it.skip('Transfer fees are paid by the sponsor after confirmation', async () => {
- // const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
- // await setCollectionSponsorExpectSuccess(collectionId, bob.address);
- // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
- expect(false).to.be.true;
+ it.only('Transfer fees are paid by the sponsor after confirmation', async () => {
+ const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');
+ await setCollectionSponsorExpectSuccess(collectionId, bob.address);
+ await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
+ const itemId = await createItemExpectSuccess(collectionId, 'NFT', '//Alice');
+
+ await usingApi(async (api) => {
+ const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).toString());
+
+ // Find unused address
+ const zeroBalance = await findUnusedAddress(api);
+
+ const aliceToZero = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);
+ await submitTransactionAsync(alice, aliceToZero);
+
+ const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);
+ const events = await submitTransactionAsync(zeroBalance, zeroToAlice);
+ const result = getGenericResult(events);
+
+ const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).toString());
+
+ expect(result.success).to.be.true;
+ expect(BsponsorBalance.toNumber()).to.be.lessThan(AsponsorBalance.toNumber());
+ });
+
});
it.skip('CreateItem fees are paid by the sponsor after confirmation', async () => {
tests/src/createItem.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/createItem.test.ts
@@ -0,0 +1,27 @@
+import { assert } from 'chai';
+import { alicesPublicKey } from './accounts';
+import privateKey from './substrate/privateKey';
+import { default as usingApi } from './substrate/substrate-api';
+import waitNewBlocks from './substrate/wait-new-blocks';
+import {
+ createCollectionExpectSuccess,
+ createItemExpectSuccess
+} from './util/helpers';
+
+describe('integration test: ext. createItem():', () => {
+ it('Create new item in NFT collection', async () => {
+ const createMode = 'NFT';
+ const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
+ await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
+ });
+ it('Create new item in Fungible collection', async () => {
+ const createMode = 'Fungible';
+ const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
+ await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
+ });
+ it('Create new item in ReFungible collection', async () => {
+ const createMode = 'ReFungible';
+ const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
+ await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
+ });
+});
tests/src/util/helpers.tsdiffbeforeafterboth1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import 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';1617chai.use(chaiAsPromised);18const expect = chai.expect;1920type GenericResult = {21 success: boolean,22};2324type CreateCollectionResult = {25 success: boolean,26 collectionId: number27};2829type CreateItemResult = {30 success: boolean,31 collectionId: number,32 itemId: number33};3435export function getGenericResult(events: EventRecord[]): GenericResult {36 let result: GenericResult = {37 success: false38 }39 events.forEach(({ phase, event: { data, method, section } }) => {40 // console.log(` ${phase}: ${section}.${method}:: ${data}`);41 if (method == 'ExtrinsicSuccess') {42 result.success = true;43 }44 });45 return result;46}4748function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {49 let success = false;50 let collectionId: number = 0;51 events.forEach(({ phase, event: { data, method, section } }) => {52 // console.log(` ${phase}: ${section}.${method}:: ${data}`);53 if (method == 'ExtrinsicSuccess') {54 success = true;55 } else if ((section == 'nft') && (method == 'Created')) {56 collectionId = parseInt(data[0].toString());57 }58 });59 let result: CreateCollectionResult = {60 success,61 collectionId62 }63 return result;64}6566function getCreateItemResult(events: EventRecord[]): CreateItemResult {67 let success = false;68 let collectionId: number = 0;69 let itemId: number = 0;70 events.forEach(({ phase, event: { data, method, section } }) => {71 // console.log(` ${phase}: ${section}.${method}:: ${data}`);72 if (method == 'ExtrinsicSuccess') {73 success = true;74 } else if ((section == 'nft') && (method == 'ItemCreated')) {75 collectionId = parseInt(data[0].toString());76 itemId = parseInt(data[1].toString());77 }78 });79 let result: CreateItemResult = {80 success,81 collectionId,82 itemId83 }84 return result;85}8687export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string): Promise<number> {88 let collectionId: number = 0;89 await usingApi(async (api) => {90 // Get number of collections before the transaction91 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());9293 // Run the CreateCollection transaction94 const alicePrivateKey = privateKey('//Alice');95 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);96 const events = await submitTransactionAsync(alicePrivateKey, tx);97 const result = getCreateCollectionResult(events);9899 // Get number of collections after the transaction100 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());101102 // Get the collection 103 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();104105 // What to expect106 expect(result.success).to.be.true;107 expect(result.collectionId).to.be.equal(BcollectionCount);108 expect(collection).to.be.not.null;109 expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');110 expect(collection.Owner).to.be.equal(alicesPublicKey);111 expect(utf16ToStr(collection.Name)).to.be.equal(name);112 expect(utf16ToStr(collection.Description)).to.be.equal(description);113 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);114115 collectionId = result.collectionId;116 });117118 return collectionId;119}120 121export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {122 await usingApi(async (api) => {123 // Get number of collections before the transaction124 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());125126 // Run the CreateCollection transaction127 const alicePrivateKey = privateKey('//Alice');128 const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);129 const events = await submitTransactionAsync(alicePrivateKey, tx);130 const result = getCreateCollectionResult(events);131132 // Get number of collections after the transaction133 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());134135 // What to expect136 expect(result.success).to.be.false;137 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');138 });139}140 141export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {142 let bal = new BigNumber(0);143 let unused;144 do {145 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000));146 const keyring = new Keyring({ type: 'sr25519' });147 unused = keyring.addFromUri(`//${randomSeed}`);148 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());149 } while (bal.toFixed() != '0');150 return unused; 151}152153function getDestroyResult(events: EventRecord[]): boolean {154 let success: boolean = false;155 events.forEach(({ phase, event: { data, method, section } }) => {156 // console.log(` ${phase}: ${section}.${method}:: ${data}`);157 if (method == 'ExtrinsicSuccess') {158 success = true;159 }160 });161 return success;162}163164export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {165 await usingApi(async (api) => {166 // Run the DestroyCollection transaction167 const alicePrivateKey = privateKey(senderSeed);168 const tx = api.tx.nft.destroyCollection(collectionId);169 const events = await submitTransactionAsync(alicePrivateKey, tx);170 const result = getDestroyResult(events);171172 // What to expect173 expect(result).to.be.false;174 });175}176177export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {178 await usingApi(async (api) => {179 // Run the DestroyCollection transaction180 const alicePrivateKey = privateKey(senderSeed);181 const tx = api.tx.nft.destroyCollection(collectionId);182 const events = await submitTransactionAsync(alicePrivateKey, tx);183 const result = getDestroyResult(events);184185 // Get the collection 186 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();187188 // What to expect189 expect(result).to.be.true;190 expect(collection).to.be.not.null;191 expect(collection.Owner).to.be.equal(nullPublicKey);192 });193}194195export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {196 await usingApi(async (api) => {197198 // Run the transaction199 const alicePrivateKey = privateKey('//Alice');200 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);201 const events = await submitTransactionAsync(alicePrivateKey, tx);202 const result = getGenericResult(events);203204 // Get the collection 205 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();206207 // What to expect208 expect(result.success).to.be.true;209 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());210 expect(collection.SponsorConfirmed).to.be.false;211 });212}213214export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {215 await usingApi(async (api) => {216217 // Run the transaction218 const alicePrivateKey = privateKey(senderSeed);219 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);220 const events = await submitTransactionAsync(alicePrivateKey, tx);221 const result = getGenericResult(events);222223 // What to expect224 expect(result.success).to.be.false;225 });226}227228export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {229 await usingApi(async (api) => {230231 // Run the transaction232 const sender = privateKey(senderSeed);233 const tx = api.tx.nft.confirmSponsorship(collectionId);234 const events = await submitTransactionAsync(sender, tx);235 const result = getGenericResult(events);236237 // Get the collection 238 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();239240 // What to expect241 expect(result.success).to.be.true;242 expect(collection.Sponsor).to.be.equal(sender.address);243 expect(collection.SponsorConfirmed).to.be.true;244 });245}246247export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {248 await usingApi(async (api) => {249250 // Run the transaction251 const sender = privateKey(senderSeed);252 const tx = api.tx.nft.confirmSponsorship(collectionId);253 const events = await submitTransactionAsync(sender, tx);254 const result = getGenericResult(events);255256 // What to expect257 expect(result.success).to.be.false;258 });259}260261export async function createItemExpectSuccess(collectionId: number, createMode: string, senderSeed: string = '//Alice') {262 let newItemId: number = 0;263 await usingApi(async (api) => {264 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());265266 const sender = privateKey(senderSeed);267 const tx = api.tx.nft.createItem(collectionId, sender.address, createMode);268 const events = await submitTransactionAsync(sender, tx);269 const result = getCreateItemResult(events);270 271 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());272273 // What to expect274 expect(result.success).to.be.true;275 expect(BItemCount).to.be.equal(AItemCount+1);276 expect(collectionId).to.be.equal(result.collectionId);277 expect(BItemCount).to.be.equal(result.itemId);278 newItemId = result.itemId;279 });280 return newItemId;281}