difftreelog
Add test for public minti sponsoring
in: master
3 files changed
tests/src/confirmSponsorship.test.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 { 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 enableWhiteListExpectSuccess,20} from "./util/helpers";21import { Keyring } from "@polkadot/api";22import { IKeyringPair } from "@polkadot/types/types";23import type { AccountId } from '@polkadot/types/interfaces';24import { BigNumber } from 'bignumber.js';2526chai.use(chaiAsPromised);27const expect = chai.expect;2829let alice: IKeyringPair;30let bob: IKeyringPair;31let charlie: IKeyringPair;3233describe('integration test: ext. confirmSponsorship():', () => {3435 before(async () => {36 await usingApi(async (api) => {37 const keyring = new Keyring({ type: 'sr25519' });38 alice = keyring.addFromUri(`//Alice`);39 bob = keyring.addFromUri(`//Bob`);40 charlie = keyring.addFromUri(`//Charlie`);41 });42 });4344 it('Confirm collection sponsorship', async () => {45 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');46 await setCollectionSponsorExpectSuccess(collectionId, bob.address);47 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');48 });49 it('Add sponsor to a collection after the same sponsor was already added and confirmed', async () => {50 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');51 await setCollectionSponsorExpectSuccess(collectionId, bob.address);52 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');53 await setCollectionSponsorExpectSuccess(collectionId, bob.address);54 });55 it('Add new sponsor to a collection after another sponsor was already added and confirmed', async () => {56 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');57 await setCollectionSponsorExpectSuccess(collectionId, bob.address);58 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');59 await setCollectionSponsorExpectSuccess(collectionId, charlie.address);60 });6162 it('NFT: Transfer fees are paid by the sponsor after confirmation', async () => {63 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');64 await setCollectionSponsorExpectSuccess(collectionId, bob.address);65 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');6667 await usingApi(async (api) => {68 const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());6970 // Find unused address71 const zeroBalance = await findUnusedAddress(api);7273 // Mint token for unused address74 const itemId = await createItemExpectSuccess(collectionId, 'NFT', zeroBalance.address, '//Alice');7576 // Transfer this tokens from unused address to Alice77 const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);78 const events = await submitTransactionAsync(zeroBalance, zeroToAlice);79 const result = getGenericResult(events);8081 const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());8283 expect(result.success).to.be.true;84 expect(BsponsorBalance.lt(AsponsorBalance)).to.be.true;85 });8687 });8889 it('Fungible: Transfer fees are paid by the sponsor after confirmation', async () => {90 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'Fungible');91 await setCollectionSponsorExpectSuccess(collectionId, bob.address);92 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');9394 await usingApi(async (api) => {95 const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());9697 // Find unused address98 const zeroBalance = await findUnusedAddress(api);99100 // Mint token for unused address101 const itemId = await createItemExpectSuccess(collectionId, 'Fungible', zeroBalance.address, '//Alice');102103 // Transfer this tokens from unused address to Alice104 const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 1);105 const events1 = await submitTransactionAsync(zeroBalance, zeroToAlice);106 const result1 = getGenericResult(events1);107108 const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());109110 expect(result1.success).to.be.true;111 expect(BsponsorBalance.lt(AsponsorBalance)).to.be.true;112 });113 });114115 it('ReFungible: Transfer fees are paid by the sponsor after confirmation', async () => {116 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'ReFungible');117 await setCollectionSponsorExpectSuccess(collectionId, bob.address);118 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');119120 await usingApi(async (api) => {121 const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());122123 // Find unused address124 const zeroBalance = await findUnusedAddress(api);125126 // Mint token for unused address127 const itemId = await createItemExpectSuccess(collectionId, 'ReFungible', zeroBalance.address, '//Alice');128129 // Transfer this tokens from unused address to Alice130 const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 1);131 const events1 = await submitTransactionAsync(zeroBalance, zeroToAlice);132 const result1 = getGenericResult(events1);133134 const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());135136 expect(result1.success).to.be.true;137 expect(BsponsorBalance.lt(AsponsorBalance)).to.be.true;138 });139 });140141 it.only('CreateItem fees are paid by the sponsor after confirmation', async () => {142 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');143 await setCollectionSponsorExpectSuccess(collectionId, bob.address);144 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');145146 // Enable collection white list 147 await enableWhiteListExpectSuccess(collectionId);148149 // Enable public minting150151 // Create Item152153154155 });156157 it('NFT: Sponsoring is rate limited', async () => {158 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');159 await setCollectionSponsorExpectSuccess(collectionId, bob.address);160 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');161162 await usingApi(async (api) => {163 // Find unused address164 const zeroBalance = await findUnusedAddress(api);165166 // Mint token for alice167 const itemId = await createItemExpectSuccess(collectionId, 'NFT', alice.address, '//Alice');168169 // Transfer this token from Alice to unused address and back170 // Alice to Zero gets sponsored171 const aliceToZero = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);172 const events1 = await submitTransactionAsync(alice, aliceToZero);173 const result1 = getGenericResult(events1);174175 // Second transfer should fail176 const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());177 const zeroToAlice = api.tx.nft.transfer(alice.address, collectionId, itemId, 0);178 const badTransaction = async function () { 179 console.log = function () {};180 console.error = function () {};181 await submitTransactionAsync(zeroBalance, zeroToAlice);182 delete console.log;183 delete console.error;184 };185 await expect(badTransaction()).to.be.rejectedWith("Inability to pay some fees");186 const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());187188 // Try again after Zero gets some balance - now it should succeed189 const balancetx = api.tx.balances.transfer(zeroBalance.address, 1e15);190 await submitTransactionAsync(alice, balancetx);191 const events2 = await submitTransactionAsync(zeroBalance, zeroToAlice);192 const result2 = getGenericResult(events2);193194 expect(result1.success).to.be.true;195 expect(result2.success).to.be.true;196 expect(BsponsorBalance.isEqualTo(AsponsorBalance)).to.be.true;197 });198 });199200 it('Fungible: Sponsoring is rate limited', async () => {201 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'Fungible');202 await setCollectionSponsorExpectSuccess(collectionId, bob.address);203 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');204205 await usingApi(async (api) => {206 // Find unused address207 const zeroBalance = await findUnusedAddress(api);208209 // Mint token for unused address210 const itemId = await createItemExpectSuccess(collectionId, 'Fungible', zeroBalance.address, '//Alice');211212 // Transfer this tokens in parts from unused address to Alice213 const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 1);214 const events1 = await submitTransactionAsync(zeroBalance, zeroToAlice);215 const result1 = getGenericResult(events1);216217 const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());218219 const badTransaction = async function () { 220 console.log = function () {};221 console.error = function () {};222 await submitTransactionAsync(zeroBalance, zeroToAlice);223 delete console.log;224 delete console.error;225 };226227 const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());228229 // Try again after Zero gets some balance - now it should succeed230 const balancetx = api.tx.balances.transfer(zeroBalance.address, 1e15);231 await submitTransactionAsync(alice, balancetx);232 const events2 = await submitTransactionAsync(zeroBalance, zeroToAlice);233 const result2 = getGenericResult(events2);234235 expect(result1.success).to.be.true;236 expect(result2.success).to.be.true;237 expect(BsponsorBalance.isEqualTo(AsponsorBalance)).to.be.true;238 });239 });240241 it('ReFungible: Sponsoring is rate limited', async () => {242 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'ReFungible');243 await setCollectionSponsorExpectSuccess(collectionId, bob.address);244 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');245246 await usingApi(async (api) => {247 // Find unused address248 const zeroBalance = await findUnusedAddress(api);249250 // Mint token for alice251 const itemId = await createItemExpectSuccess(collectionId, 'ReFungible', alice.address, '//Alice');252253 // Transfer this token from Alice to unused address and back254 // Alice to Zero gets sponsored255 const aliceToZero = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 1);256 const events1 = await submitTransactionAsync(alice, aliceToZero);257 const result1 = getGenericResult(events1);258259 // Second transfer should fail260 const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());261 const zeroToAlice = api.tx.nft.transfer(alice.address, collectionId, itemId, 1);262 const badTransaction = async function () { 263 console.log = function () {};264 console.error = function () {};265 await submitTransactionAsync(zeroBalance, zeroToAlice);266 delete console.log;267 delete console.error;268 };269 await expect(badTransaction()).to.be.rejectedWith("Inability to pay some fees");270 const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());271272 // Try again after Zero gets some balance - now it should succeed273 const balancetx = api.tx.balances.transfer(zeroBalance.address, 1e15);274 await submitTransactionAsync(alice, balancetx);275 const events2 = await submitTransactionAsync(zeroBalance, zeroToAlice);276 const result2 = getGenericResult(events2);277278 expect(result1.success).to.be.true;279 expect(result2.success).to.be.true;280 expect(BsponsorBalance.isEqualTo(AsponsorBalance)).to.be.true;281 });282 });283284});285286describe('(!negative test!) integration test: ext. setCollectionSponsor():', () => {287 before(async () => {288 await usingApi(async (api) => {289 const keyring = new Keyring({ type: 'sr25519' });290 alice = keyring.addFromUri(`//Alice`);291 bob = keyring.addFromUri(`//Bob`);292 charlie = keyring.addFromUri(`//Charlie`);293 });294 });295296 it('(!negative test!) Confirm sponsorship for a collection that never existed', async () => {297 // Find the collection that never existed298 const collectionId = 0;299 await usingApi(async (api) => {300 const collectionId = parseInt((await api.query.nft.createdCollectionCount()).toString()) + 1;301 });302303 await confirmSponsorshipExpectFailure(collectionId, '//Bob');304 });305306 it('(!negative test!) Confirm sponsorship using a non-sponsor address', async () => {307 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');308 await setCollectionSponsorExpectSuccess(collectionId, bob.address);309310 await usingApi(async (api) => {311 const transfer = api.tx.balances.transfer(charlie.address, 1e15);312 await submitTransactionAsync(alice, transfer);313 });314315 await confirmSponsorshipExpectFailure(collectionId, '//Charlie');316 });317318 it('(!negative test!) Confirm sponsorship using owner address', async () => {319 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');320 await setCollectionSponsorExpectSuccess(collectionId, bob.address);321 await confirmSponsorshipExpectFailure(collectionId, '//Alice');322 });323324 it('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async () => {325 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');326 await confirmSponsorshipExpectFailure(collectionId, '//Bob');327 });328 329 it('(!negative test!) Confirm sponsorship in a collection that was destroyed', async () => {330 const collectionId = await createCollectionExpectSuccess('A', 'B', 'C', 'NFT');331 await destroyCollectionExpectSuccess(collectionId);332 await confirmSponsorshipExpectFailure(collectionId, '//Bob');333 });334});tests/src/createItem.test.tsdiffbeforeafterboth--- a/tests/src/createItem.test.ts
+++ b/tests/src/createItem.test.ts
@@ -1,27 +1,34 @@
-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 { Keyring } from "@polkadot/api";
+import { IKeyringPair } from "@polkadot/types/types";
import {
createCollectionExpectSuccess,
createItemExpectSuccess
} from './util/helpers';
+let alice: IKeyringPair;
+
describe('integration test: ext. createItem():', () => {
+ before(async () => {
+ await usingApi(async (api) => {
+ const keyring = new Keyring({ type: 'sr25519' });
+ alice = keyring.addFromUri(`//Alice`);
+ });
+ });
+
it('Create new item in NFT collection', async () => {
const createMode = 'NFT';
const newCollectionID = await createCollectionExpectSuccess('0', '0', '0', createMode);
- await createItemExpectSuccess(newCollectionID, createMode);
+ await createItemExpectSuccess(alice, 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);
+ await createItemExpectSuccess(alice, 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);
+ await createItemExpectSuccess(alice, newCollectionID, createMode);
});
});
tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -273,14 +273,13 @@
ReFungible: CreateReFungibleData
};
-export async function createItemExpectSuccess(collectionId: number, createMode: string, owner: string = '', senderSeed: string = '//Alice') {
+export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {
let newItemId: number = 0;
await usingApi(async (api) => {
const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());
const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();
const AItemBalance = new BigNumber(Aitem.Value);
- const sender = privateKey(senderSeed);
if (owner === '') owner = sender.address;
let tx;
@@ -313,11 +312,10 @@
return newItemId;
}
-export async function enableWhiteListExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {
+export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {
await usingApi(async (api) => {
// Run the transaction
- const sender = privateKey(senderSeed);
const tx = api.tx.nft.setPublicAccessMode(collectionId, 'WhiteList');
const events = await submitTransactionAsync(sender, tx);
const result = getGenericResult(events);
@@ -330,3 +328,38 @@
expect(collection.Access).to.be.equal('WhiteList');
});
}
+
+export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {
+ await usingApi(async (api) => {
+
+ // Run the transaction
+ const tx = api.tx.nft.setMintPermission(collectionId, true);
+ const events = await submitTransactionAsync(sender, tx);
+ const result = getGenericResult(events);
+
+ // Get the collection
+ const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
+
+ // What to expect
+ expect(result.success).to.be.true;
+ expect(collection.MintMode).to.be.equal(true);
+ });
+}
+
+export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {
+ await usingApi(async (api) => {
+
+ // Run the transaction
+ const tx = api.tx.nft.addToWhiteList(collectionId, address);
+ const events = await submitTransactionAsync(sender, tx);
+ const result = getGenericResult(events);
+
+ // Get the collection
+ const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
+
+ // What to expect
+ expect(result.success).to.be.true;
+ expect(collection.MintMode).to.be.equal(true);
+ });
+}
+