From 9b1ad2d3650d41e6d64eb8d6e91134509dc44337 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Wed, 23 Dec 2020 16:28:46 +0000 Subject: [PATCH] Confirm sponsorship tests in progress --- --- 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 () => { --- /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'); + }); +}); --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -26,6 +26,12 @@ collectionId: number }; +type CreateItemResult = { + success: boolean, + collectionId: number, + itemId: number +}; + export function getGenericResult(events: EventRecord[]): GenericResult { let result: GenericResult = { success: false @@ -57,6 +63,27 @@ return result; } +function getCreateItemResult(events: EventRecord[]): CreateItemResult { + let success = false; + let collectionId: number = 0; + let itemId: number = 0; + events.forEach(({ phase, event: { data, method, section } }) => { + // console.log(` ${phase}: ${section}.${method}:: ${data}`); + if (method == 'ExtrinsicSuccess') { + success = true; + } else if ((section == 'nft') && (method == 'ItemCreated')) { + collectionId = parseInt(data[0].toString()); + itemId = parseInt(data[1].toString()); + } + }); + let result: CreateItemResult = { + success, + collectionId, + itemId + } + return result; +} + export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string): Promise { let collectionId: number = 0; await usingApi(async (api) => { @@ -231,3 +258,24 @@ }); } +export async function createItemExpectSuccess(collectionId: number, createMode: string, senderSeed: string = '//Alice') { + let newItemId: number = 0; + await usingApi(async (api) => { + const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString()); + + const sender = privateKey(senderSeed); + const tx = api.tx.nft.createItem(collectionId, sender.address, createMode); + const events = await submitTransactionAsync(sender, tx); + const result = getCreateItemResult(events); + + const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString()); + + // What to expect + expect(result.success).to.be.true; + expect(BItemCount).to.be.equal(AItemCount+1); + expect(collectionId).to.be.equal(result.collectionId); + expect(BItemCount).to.be.equal(result.itemId); + newItemId = result.itemId; + }); + return newItemId; +} -- gitstuff