git.delta.rocks / unique-network / refs/commits / b526a8b308f2

difftreelog

Add test for public minti sponsoring

Greg Zaitsev2020-12-25parent: #f4dfc50.patch.diff
in: master

3 files changed

modifiedtests/src/confirmSponsorship.test.tsdiffbeforeafterboth
--- a/tests/src/confirmSponsorship.test.ts
+++ b/tests/src/confirmSponsorship.test.ts
@@ -17,6 +17,8 @@
   findUnusedAddress,
   getGenericResult,
   enableWhiteListExpectSuccess,
+  enablePublicMintingExpectSuccess,
+  addToWhiteListExpectSuccess,
 } from "./util/helpers";
 import { Keyring } from "@polkadot/api";
 import { IKeyringPair } from "@polkadot/types/types";
@@ -30,7 +32,7 @@
 let bob: IKeyringPair;
 let charlie: IKeyringPair;
 
-describe('integration test: ext. confirmSponsorship():', () => {
+describe.only('integration test: ext. confirmSponsorship():', () => {
 
   before(async () => {
     await usingApi(async (api) => {
@@ -71,7 +73,7 @@
       const zeroBalance = await findUnusedAddress(api);
 
       // Mint token for unused address
-      const itemId = await createItemExpectSuccess(collectionId, 'NFT', zeroBalance.address, '//Alice');
+      const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', zeroBalance.address);
 
       // Transfer this tokens from unused address to Alice
       const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 0);
@@ -98,7 +100,7 @@
       const zeroBalance = await findUnusedAddress(api);
 
       // Mint token for unused address
-      const itemId = await createItemExpectSuccess(collectionId, 'Fungible', zeroBalance.address, '//Alice');
+      const itemId = await createItemExpectSuccess(alice, collectionId, 'Fungible', zeroBalance.address);
 
       // Transfer this tokens from unused address to Alice
       const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 1);
@@ -124,7 +126,7 @@
       const zeroBalance = await findUnusedAddress(api);
 
       // Mint token for unused address
-      const itemId = await createItemExpectSuccess(collectionId, 'ReFungible', zeroBalance.address, '//Alice');
+      const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', zeroBalance.address);
 
       // Transfer this tokens from unused address to Alice
       const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 1);
@@ -138,20 +140,34 @@
     });
   });
 
-  it.only('CreateItem fees are paid by the sponsor after confirmation', async () => {
+  it('CreateItem 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');
 
     // Enable collection white list 
-    await enableWhiteListExpectSuccess(collectionId);
+    await enableWhiteListExpectSuccess(alice, collectionId);
 
     // Enable public minting
+    await enablePublicMintingExpectSuccess(alice, collectionId);
+
+    // Create Item 
+    await usingApi(async (api) => {
+      const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());
 
-    // Create Item
+      // Find unused address
+      const zeroBalance = await findUnusedAddress(api);
+
+      // Add zeroBalance address to white list
+      await addToWhiteListExpectSuccess(alice, collectionId, zeroBalance.address);
 
+      // Mint token using unused address as signer
+      const tokenId = await createItemExpectSuccess(zeroBalance, collectionId, 'NFT', zeroBalance.address);
 
+      const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());
 
+      expect(BsponsorBalance.lt(AsponsorBalance)).to.be.true;
+    });
   });
 
   it('NFT: Sponsoring is rate limited', async () => {
@@ -164,7 +180,7 @@
       const zeroBalance = await findUnusedAddress(api);
 
       // Mint token for alice
-      const itemId = await createItemExpectSuccess(collectionId, 'NFT', alice.address, '//Alice');
+      const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);
 
       // Transfer this token from Alice to unused address and back
       // Alice to Zero gets sponsored
@@ -207,7 +223,7 @@
       const zeroBalance = await findUnusedAddress(api);
 
       // Mint token for unused address
-      const itemId = await createItemExpectSuccess(collectionId, 'Fungible', zeroBalance.address, '//Alice');
+      const itemId = await createItemExpectSuccess(alice, collectionId, 'Fungible', zeroBalance.address);
 
       // Transfer this tokens in parts from unused address to Alice
       const zeroToAlice = api.tx.nft.transfer(zeroBalance.address, collectionId, itemId, 1);
@@ -248,7 +264,7 @@
       const zeroBalance = await findUnusedAddress(api);
 
       // Mint token for alice
-      const itemId = await createItemExpectSuccess(collectionId, 'ReFungible', alice.address, '//Alice');
+      const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', alice.address);
 
       // Transfer this token from Alice to unused address and back
       // Alice to Zero gets sponsored
@@ -283,7 +299,7 @@
 
 });
 
-describe('(!negative test!) integration test: ext. setCollectionSponsor():', () => {
+describe.only('(!negative test!) integration test: ext. setCollectionSponsor():', () => {
   before(async () => {
     await usingApi(async (api) => {
       const keyring = new Keyring({ type: 'sr25519' });
modifiedtests/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);
   });
 });
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
273 ReFungible: CreateReFungibleData273 ReFungible: CreateReFungibleData
274};274};
275275
276export async function createItemExpectSuccess(collectionId: number, createMode: string, owner: string = '', senderSeed: string = '//Alice') {276export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {
277 let newItemId: number = 0;277 let newItemId: number = 0;
278 await usingApi(async (api) => {278 await usingApi(async (api) => {
279 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());279 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());
280 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON(); 280 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();
281 const AItemBalance = new BigNumber(Aitem.Value);281 const AItemBalance = new BigNumber(Aitem.Value);
282282
283 const sender = privateKey(senderSeed);
284 if (owner === '') owner = sender.address;283 if (owner === '') owner = sender.address;
285284
286 let tx;285 let tx;
313 return newItemId;312 return newItemId;
314}313}
315314
316export async function enableWhiteListExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {315export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {
317 await usingApi(async (api) => {316 await usingApi(async (api) => {
318317
319 // Run the transaction318 // Run the transaction
320 const sender = privateKey(senderSeed);
321 const tx = api.tx.nft.setPublicAccessMode(collectionId, 'WhiteList');319 const tx = api.tx.nft.setPublicAccessMode(collectionId, 'WhiteList');
322 const events = await submitTransactionAsync(sender, tx);320 const events = await submitTransactionAsync(sender, tx);
323 const result = getGenericResult(events);321 const result = getGenericResult(events);
331 });329 });
332}330}
331
332export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {
333 await usingApi(async (api) => {
334
335 // Run the transaction
336 const tx = api.tx.nft.setMintPermission(collectionId, true);
337 const events = await submitTransactionAsync(sender, tx);
338 const result = getGenericResult(events);
339
340 // Get the collection
341 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
342
343 // What to expect
344 expect(result.success).to.be.true;
345 expect(collection.MintMode).to.be.equal(true);
346 });
347}
348
349export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {
350 await usingApi(async (api) => {
351
352 // Run the transaction
353 const tx = api.tx.nft.addToWhiteList(collectionId, address);
354 const events = await submitTransactionAsync(sender, tx);
355 const result = getGenericResult(events);
356
357 // Get the collection
358 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
359
360 // What to expect
361 expect(result.success).to.be.true;
362 expect(collection.MintMode).to.be.equal(true);
363 });
364}
333365
366