git.delta.rocks / unique-network / refs/commits / 427c943f97f4

difftreelog

Add tests for sponsorship

Greg Zaitsev2020-12-23parent: #298277a.patch.diff
in: master

4 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -2356,6 +2356,7 @@
                 
                 let mut sponsor_transfer = false;
                 if <Collection<T>>::get(collection_id).sponsor_confirmed {
+
                     let collection_limits = <Collection<T>>::get(collection_id).limits;
                     let collection_mode = <Collection<T>>::get(collection_id).mode;
     
modifiedtests/src/confirmSponsorship.test.tsdiffbeforeafterboth
before · tests/src/confirmSponsorship.test.ts
1//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 { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";9import { 10  createCollectionExpectSuccess, 11  setCollectionSponsorExpectSuccess, 12  destroyCollectionExpectSuccess, 13  setCollectionSponsorExpectFailure,14  confirmSponsorshipExpectSuccess,15  confirmSponsorshipExpectFailure,16  createItemExpectSuccess,17  findUnusedAddress,18  getGenericResult,19} from "./util/helpers";20import { Keyring } from "@polkadot/api";21import { IKeyringPair } from "@polkadot/types/types";22import type { AccountId } from '@polkadot/types/interfaces';23import { BigNumber } from 'bignumber.js';2425chai.use(chaiAsPromised);26const expect = chai.expect;2728let alice: IKeyringPair;29let bob: IKeyringPair;30let charlie: IKeyringPair;3132describe('integration test: ext. confirmSponsorship():', () => {3334  before(async () => {35    await usingApi(async (api) => {36      const keyring = new Keyring({ type: 'sr25519' });37      alice = keyring.addFromUri(`//Alice`);38      bob = keyring.addFromUri(`//Bob`);39      charlie = keyring.addFromUri(`//Charlie`);40    });41  });4243  it('Confirm collection sponsorship', async () => {44    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');45    await setCollectionSponsorExpectSuccess(collectionId, bob.address);46    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');47  });48  it('Add sponsor to a collection after the same sponsor was already added and confirmed', async () => {49    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');50    await setCollectionSponsorExpectSuccess(collectionId, bob.address);51    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');52    await setCollectionSponsorExpectSuccess(collectionId, bob.address);53  });54  it('Add new sponsor to a collection after another sponsor was already added and confirmed', async () => {55    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');56    await setCollectionSponsorExpectSuccess(collectionId, bob.address);57    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');58    await setCollectionSponsorExpectSuccess(collectionId, charlie.address);59  });6061  it.only('Transfer fees are paid by the sponsor after confirmation', async () => {62    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');63    await setCollectionSponsorExpectSuccess(collectionId, bob.address);64    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');65    const itemId = await createItemExpectSuccess(collectionId, 'NFT', '//Alice');6667    await usingApi(async (api) => {68      const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).toString());6970      // Find unused address71      const zeroBalance = await findUnusedAddress(api);72      73      const aliceToZero = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);74      await submitTransactionAsync(alice, aliceToZero);7576      const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);77      const events = await submitTransactionAsync(zeroBalance, zeroToAlice);78      const result = getGenericResult(events);7980      const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).toString());8182      expect(result.success).to.be.true;83      expect(BsponsorBalance.toNumber()).to.be.lessThan(AsponsorBalance.toNumber());84    });8586  });8788  it.skip('CreateItem fees are paid by the sponsor after confirmation', async () => {89    // const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');90    // await setCollectionSponsorExpectSuccess(collectionId, bob.address);91    // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');92    expect(false).to.be.true;93  });9495});9697describe('(!negative test!) integration test: ext. setCollectionSponsor():', () => {98  before(async () => {99    await usingApi(async (api) => {100      const keyring = new Keyring({ type: 'sr25519' });101      alice = keyring.addFromUri(`//Alice`);102      bob = keyring.addFromUri(`//Bob`);103      charlie = keyring.addFromUri(`//Charlie`);104    });105  });106107  it('(!negative test!) Confirm sponsorship for a collection that never existed', async () => {108    // Find the collection that never existed109    const collectionId = 0;110    await usingApi(async (api) => {111      const collectionId = parseInt((await api.query.nft.createdCollectionCount()).toString()) + 1;112    });113114    await confirmSponsorshipExpectFailure(collectionId, '//Bob');115  });116117  it('(!negative test!) Confirm sponsorship using a non-sponsor address', async () => {118    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');119    await setCollectionSponsorExpectSuccess(collectionId, bob.address);120121    await usingApi(async (api) => {122      const transfer = api.tx.balances.transfer(charlie.address, 1e15);123      await submitTransactionAsync(alice, transfer);124    });125126    await confirmSponsorshipExpectFailure(collectionId, '//Charlie');127  });128129  it('(!negative test!) Confirm sponsorship using owner address', async () => {130    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');131    await setCollectionSponsorExpectSuccess(collectionId, bob.address);132    await confirmSponsorshipExpectFailure(collectionId, '//Alice');133  });134135  it('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async () => {136    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');137    await confirmSponsorshipExpectFailure(collectionId, '//Bob');138  });139    140  it('(!negative test!) Confirm sponsorship in a collection that was destroyed', async () => {141    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');142    await destroyCollectionExpectSuccess(collectionId);143    await confirmSponsorshipExpectFailure(collectionId, '//Bob');144  });145});
after · tests/src/confirmSponsorship.test.ts
1//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 { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";9import { 10  createCollectionExpectSuccess, 11  setCollectionSponsorExpectSuccess, 12  destroyCollectionExpectSuccess, 13  setCollectionSponsorExpectFailure,14  confirmSponsorshipExpectSuccess,15  confirmSponsorshipExpectFailure,16  createItemExpectSuccess,17  findUnusedAddress,18  getGenericResult,19} from "./util/helpers";20import { Keyring } from "@polkadot/api";21import { IKeyringPair } from "@polkadot/types/types";22import type { AccountId } from '@polkadot/types/interfaces';23import { BigNumber } from 'bignumber.js';2425chai.use(chaiAsPromised);26const expect = chai.expect;2728let alice: IKeyringPair;29let bob: IKeyringPair;30let charlie: IKeyringPair;3132describe('integration test: ext. confirmSponsorship():', () => {3334  before(async () => {35    await usingApi(async (api) => {36      const keyring = new Keyring({ type: 'sr25519' });37      alice = keyring.addFromUri(`//Alice`);38      bob = keyring.addFromUri(`//Bob`);39      charlie = keyring.addFromUri(`//Charlie`);40    });41  });4243  it('Confirm collection sponsorship', async () => {44    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');45    await setCollectionSponsorExpectSuccess(collectionId, bob.address);46    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');47  });48  it('Add sponsor to a collection after the same sponsor was already added and confirmed', async () => {49    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');50    await setCollectionSponsorExpectSuccess(collectionId, bob.address);51    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');52    await setCollectionSponsorExpectSuccess(collectionId, bob.address);53  });54  it('Add new sponsor to a collection after another sponsor was already added and confirmed', async () => {55    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');56    await setCollectionSponsorExpectSuccess(collectionId, bob.address);57    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');58    await setCollectionSponsorExpectSuccess(collectionId, charlie.address);59  });6061  it('NFT: Transfer fees are paid by the sponsor after confirmation', async () => {62    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');63    await setCollectionSponsorExpectSuccess(collectionId, bob.address);64    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');6566    await usingApi(async (api) => {67      const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());6869      // Find unused address70      const zeroBalance = await findUnusedAddress(api);7172      // Mint token for unused address73      const itemId = await createItemExpectSuccess(collectionId, 'NFT', zeroBalance.address, '//Alice');7475      // Transfer this token from unused address to Alice76      const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);77      const events = await submitTransactionAsync(zeroBalance, zeroToAlice);78      const result = getGenericResult(events);7980      const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());8182      expect(result.success).to.be.true;83      expect(BsponsorBalance.lt(AsponsorBalance)).to.be.true;84    });8586  });8788  it('Fungible: Transfer fees are paid by the sponsor after confirmation', async () => {89    expect(false).to.be.true;90  });9192  it('ReFungible: Transfer fees are paid by the sponsor after confirmation', async () => {93    expect(false).to.be.true;94  });9596  it.skip('CreateItem fees are paid by the sponsor after confirmation', async () => {97    // const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');98    // await setCollectionSponsorExpectSuccess(collectionId, bob.address);99    // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');100    expect(false).to.be.true;101  });102103  it('NFT: Sponsoring is rate limited', async () => {104    expect(false).to.be.true;105  });106107  it('Fungible: Sponsoring is rate limited', async () => {108    expect(false).to.be.true;109  });110111  it('ReFungible: Sponsoring is rate limited', async () => {112    expect(false).to.be.true;113  });114115});116117describe('(!negative test!) integration test: ext. setCollectionSponsor():', () => {118  before(async () => {119    await usingApi(async (api) => {120      const keyring = new Keyring({ type: 'sr25519' });121      alice = keyring.addFromUri(`//Alice`);122      bob = keyring.addFromUri(`//Bob`);123      charlie = keyring.addFromUri(`//Charlie`);124    });125  });126127  it('(!negative test!) Confirm sponsorship for a collection that never existed', async () => {128    // Find the collection that never existed129    const collectionId = 0;130    await usingApi(async (api) => {131      const collectionId = parseInt((await api.query.nft.createdCollectionCount()).toString()) + 1;132    });133134    await confirmSponsorshipExpectFailure(collectionId, '//Bob');135  });136137  it('(!negative test!) Confirm sponsorship using a non-sponsor address', async () => {138    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');139    await setCollectionSponsorExpectSuccess(collectionId, bob.address);140141    await usingApi(async (api) => {142      const transfer = api.tx.balances.transfer(charlie.address, 1e15);143      await submitTransactionAsync(alice, transfer);144    });145146    await confirmSponsorshipExpectFailure(collectionId, '//Charlie');147  });148149  it('(!negative test!) Confirm sponsorship using owner address', async () => {150    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');151    await setCollectionSponsorExpectSuccess(collectionId, bob.address);152    await confirmSponsorshipExpectFailure(collectionId, '//Alice');153  });154155  it('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async () => {156    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');157    await confirmSponsorshipExpectFailure(collectionId, '//Bob');158  });159    160  it('(!negative test!) Confirm sponsorship in a collection that was destroyed', async () => {161    const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');162    await destroyCollectionExpectSuccess(collectionId);163    await confirmSponsorshipExpectFailure(collectionId, '//Bob');164  });165});
modifiedtests/src/createItem.test.tsdiffbeforeafterboth
--- a/tests/src/createItem.test.ts
+++ b/tests/src/createItem.test.ts
@@ -12,16 +12,16 @@
   it('Create new item in NFT collection', async () => {
     const createMode = 'NFT';
     const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
-    await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
+    await createItemExpectSuccess(newCollectionID, createMode);
   });
   it('Create new item in Fungible collection', async () => {
     const createMode = 'Fungible';
     const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
-    await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
+    await createItemExpectSuccess(newCollectionID, createMode);
   });
   it('Create new item in ReFungible collection', async () => {
     const createMode = 'ReFungible';
     const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
-    await createItemExpectSuccess(newCollectionID, createMode, '//Alice');
+    await createItemExpectSuccess(newCollectionID, createMode);
   });
 });
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -258,13 +258,14 @@
   });
 }
 
-export async function createItemExpectSuccess(collectionId: number, createMode: string, senderSeed: string = '//Alice') {
+export async function createItemExpectSuccess(collectionId: number, createMode: string, owner: 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);
+    if (owner === '') owner = sender.address;
+    const tx = api.tx.nft.createItem(collectionId, owner, createMode);
     const events = await submitTransactionAsync(sender, tx);
     const result = getCreateItemResult(events);