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

difftreelog

test(ss58Format) fix format address, remove accounts constants

h3lpkey2022-06-08parent: #2074c93.patch.diff
in: master

6 files changed

deletedtests/src/accounts.tsdiffbeforeafterboth
--- a/tests/src/accounts.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// Unique Network is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-export const bobsPublicKey = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
-export const alicesPublicKey = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
-export const ferdiesPublicKey = '5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL';
-export const nullPublicKey = '5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM';
\ No newline at end of file
modifiedtests/src/creditFeesToTreasury.test.tsdiffbeforeafterboth
before · tests/src/creditFeesToTreasury.test.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import './interfaces/augment-api-consts';18import chai from 'chai';19import chaiAsPromised from 'chai-as-promised';20import {default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';21import {alicesPublicKey, bobsPublicKey} from './accounts';22import {IKeyringPair} from '@polkadot/types/types';23import {24  createCollectionExpectSuccess,25  createItemExpectSuccess,26  getGenericResult,27  transferExpectSuccess,28  UNIQUE,29} from './util/helpers';3031import {default as waitNewBlocks} from './substrate/wait-new-blocks';32import {ApiPromise} from '@polkadot/api';3334chai.use(chaiAsPromised);35const expect = chai.expect;3637const TREASURY = '5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z';38const saneMinimumFee = 0.05;39const saneMaximumFee = 0.5;40const createCollectionDeposit = 100;4142let alice: IKeyringPair;43let bob: IKeyringPair;4445// Skip the inflation block pauses if the block is close to inflation block46// until the inflation happens47/*eslint no-async-promise-executor: "off"*/48function skipInflationBlock(api: ApiPromise): Promise<void> {49  const promise = new Promise<void>(async (resolve) => {50    const blockInterval = (await api.consts.inflation.inflationBlockInterval).toNumber();51    const unsubscribe = await api.rpc.chain.subscribeNewHeads(head => {52      const currentBlock = head.number.toNumber();53      if (currentBlock % blockInterval < blockInterval - 10) {54        unsubscribe();55        resolve();56      } else {57        console.log(`Skipping inflation block, current block: ${currentBlock}`);58      }59    });60  });6162  return promise;63}6465describe('integration test: Fees must be credited to Treasury:', () => {66  before(async () => {67    await usingApi(async (api, privateKeyWrapper) => {68      alice = privateKeyWrapper('//Alice');69      bob = privateKeyWrapper('//Bob');70    });71  });7273  it('Total issuance does not change', async () => {74    await usingApi(async (api, privateKeyWrapper) => {75      await skipInflationBlock(api);76      await waitNewBlocks(api, 1);7778      const totalBefore = (await api.query.balances.totalIssuance()).toBigInt();7980      const alicePrivateKey = privateKeyWrapper('//Alice');81      const amount = 1n;82      const transfer = api.tx.balances.transfer(bobsPublicKey, amount);8384      const result = getGenericResult(await submitTransactionAsync(alicePrivateKey, transfer));8586      const totalAfter = (await api.query.balances.totalIssuance()).toBigInt();8788      expect(result.success).to.be.true;89      expect(totalAfter).to.be.equal(totalBefore);90    });91  });9293  it('Sender balance decreased by fee+sent amount, Treasury balance increased by fee', async () => {94    await usingApi(async (api, privateKeyWrapper) => {95      await skipInflationBlock(api);96      await waitNewBlocks(api, 1);9798      const alicePrivateKey = privateKeyWrapper('//Alice');99      const treasuryBalanceBefore: bigint = (await api.query.system.account(TREASURY)).data.free.toBigInt();100      const aliceBalanceBefore: bigint = (await api.query.system.account(alicesPublicKey)).data.free.toBigInt();101102      const amount = 1n;103      const transfer = api.tx.balances.transfer(bobsPublicKey, amount);104      const result = getGenericResult(await submitTransactionAsync(alicePrivateKey, transfer));105106      const treasuryBalanceAfter: bigint = (await api.query.system.account(TREASURY)).data.free.toBigInt();107      const aliceBalanceAfter: bigint = (await api.query.system.account(alicesPublicKey)).data.free.toBigInt();108      const fee = aliceBalanceBefore - aliceBalanceAfter - amount;109      const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;110111      expect(result.success).to.be.true;112      expect(treasuryIncrease).to.be.equal(fee);113    });114  });115116  it('Treasury balance increased by failed tx fee', async () => {117    await usingApi(async (api, privateKeyWrapper) => {118      //await skipInflationBlock(api);119      await waitNewBlocks(api, 1);120121      const bobPrivateKey = privateKeyWrapper('//Bob');122      const treasuryBalanceBefore = (await api.query.system.account(TREASURY)).data.free.toBigInt();123      const bobBalanceBefore = (await api.query.system.account(bobsPublicKey)).data.free.toBigInt();124125      const badTx = api.tx.balances.setBalance(alicesPublicKey, 0, 0);126      await expect(submitTransactionExpectFailAsync(bobPrivateKey, badTx)).to.be.rejected;127128      const treasuryBalanceAfter = (await api.query.system.account(TREASURY)).data.free.toBigInt();129      const bobBalanceAfter = (await api.query.system.account(bobsPublicKey)).data.free.toBigInt();130      const fee = bobBalanceBefore - bobBalanceAfter;131      const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;132133      expect(treasuryIncrease).to.be.equal(fee);134    });135  });136137  it('NFT Transactions also send fees to Treasury', async () => {138    await usingApi(async (api) => {139      await skipInflationBlock(api);140      await waitNewBlocks(api, 1);141142      const treasuryBalanceBefore = (await api.query.system.account(TREASURY)).data.free.toBigInt();143      const aliceBalanceBefore = (await api.query.system.account(alicesPublicKey)).data.free.toBigInt();144145      await createCollectionExpectSuccess();146147      const treasuryBalanceAfter = (await api.query.system.account(TREASURY)).data.free.toBigInt();148      const aliceBalanceAfter = (await api.query.system.account(alicesPublicKey)).data.free.toBigInt();149      const fee = aliceBalanceBefore - aliceBalanceAfter;150      const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;151152      expect(treasuryIncrease).to.be.equal(fee);153    });154  });155156  it('Fees are sane', async () => {157    await usingApi(async (api) => {158      await skipInflationBlock(api);159      await waitNewBlocks(api, 1);160161      const aliceBalanceBefore: bigint = (await api.query.system.account(alicesPublicKey)).data.free.toBigInt();162163      await createCollectionExpectSuccess();164165      const aliceBalanceAfter: bigint = (await api.query.system.account(alicesPublicKey)).data.free.toBigInt();166      const fee = aliceBalanceBefore - aliceBalanceAfter;167168      expect(fee / UNIQUE < BigInt(Math.ceil(saneMaximumFee + createCollectionDeposit))).to.be.true;169      expect(fee / UNIQUE < BigInt(Math.ceil(saneMinimumFee  + createCollectionDeposit))).to.be.true;170    });171  });172173  it('NFT Transfer fee is close to 0.1 Unique', async () => {174    await usingApi(async (api) => {175      await skipInflationBlock(api);176      await waitNewBlocks(api, 1);177178      const collectionId = await createCollectionExpectSuccess();179      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');180181      const aliceBalanceBefore: bigint = (await api.query.system.account(alicesPublicKey)).data.free.toBigInt();182      await transferExpectSuccess(collectionId, tokenId, alice, bob, 1, 'NFT');183      const aliceBalanceAfter: bigint = (await api.query.system.account(alicesPublicKey)).data.free.toBigInt();184185      const fee = Number(aliceBalanceBefore - aliceBalanceAfter) / Number(UNIQUE);186      const expectedTransferFee = 0.1;187      // fee drifts because of NextFeeMultiplier188      const tolerance = 0.001;189190      expect(Math.abs(fee - expectedTransferFee)).to.be.lessThan(tolerance);191    });192  });193194});
modifiedtests/src/substrate/substrate-api.tsdiffbeforeafterboth
--- a/tests/src/substrate/substrate-api.ts
+++ b/tests/src/substrate/substrate-api.ts
@@ -88,7 +88,7 @@
     await promisifySubstrate(api, async () => {
       if (api) {
         await api.isReadyOrError;
-        const ss58Format = (api.registry.getChainProperties())!.toHuman().ss58Format;
+        const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;
         const privateKeyWrapper = (account: string) => privateKey(account, Number(ss58Format));
         result = await action(api, privateKeyWrapper);
       }
modifiedtests/src/transfer.test.tsdiffbeforeafterboth
--- a/tests/src/transfer.test.ts
+++ b/tests/src/transfer.test.ts
@@ -17,7 +17,6 @@
 import {ApiPromise} from '@polkadot/api';
 import {IKeyringPair} from '@polkadot/types/types';
 import {expect} from 'chai';
-import {alicesPublicKey, bobsPublicKey} from './accounts';
 import getBalance from './substrate/get-balance';
 import {default as usingApi, submitTransactionAsync} from './substrate/substrate-api';
 import {
@@ -47,19 +46,24 @@
 let charlie: IKeyringPair;
 
 describe('Integration Test Transfer(recipient, collection_id, item_id, value)', () => {
+  before(async () => {
+    await usingApi(async (api, privateKeyWrapper) => {
+      alice = privateKeyWrapper('//Alice');
+      bob = privateKeyWrapper('//Bob');
+    });
+  });
+  
   it('Balance transfers and check balance', async () => {
     await usingApi(async (api, privateKeyWrapper) => {
-      const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);
-
-      const alicePrivateKey = privateKeyWrapper('//Alice');
+      const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alice.address, bob.address]);
 
-      const transfer = api.tx.balances.transfer(bobsPublicKey, 1n);
-      const events = await submitTransactionAsync(alicePrivateKey, transfer);
+      const transfer = api.tx.balances.transfer(bob.address, 1n);
+      const events = await submitTransactionAsync(alice, transfer);
       const result = getCreateItemResult(events);
       // tslint:disable-next-line:no-unused-expression
       expect(result.success).to.be.true;
 
-      const [alicesBalanceAfter, bobsBalanceAfter] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);
+      const [alicesBalanceAfter, bobsBalanceAfter] = await getBalance(api, [alice.address, bob.address]);
 
       // tslint:disable-next-line:no-unused-expression
       expect(alicesBalanceAfter < alicesBalanceBefore).to.be.true;
@@ -73,7 +77,7 @@
       // Find unused address
       const pk = await findUnusedAddress(api);
 
-      const badTransfer = api.tx.balances.transfer(bobsPublicKey, 1n);
+      const badTransfer = api.tx.balances.transfer(bob.address, 1n);
       // const events = await submitTransactionAsync(pk, badTransfer);
       const badTransaction = async () => {
         const events = await submitTransactionAsync(pk, badTransfer);
@@ -87,8 +91,6 @@
 
   it('User can transfer owned token', async () => {
     await usingApi(async (api, privateKeyWrapper) => {
-      const alice = privateKeyWrapper('//Alice');
-      const bob = privateKeyWrapper('//Bob');
       // nft
       const nftCollectionId = await createCollectionExpectSuccess();
       const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');
@@ -114,8 +116,6 @@
 
   it('Collection admin can transfer owned token', async () => {
     await usingApi(async (api, privateKeyWrapper) => {
-      const alice = privateKeyWrapper('//Alice');
-      const bob = privateKeyWrapper('//Bob');
       // nft
       const nftCollectionId = await createCollectionExpectSuccess();
       await addCollectionAdminExpectSuccess(alice, nftCollectionId, bob.address);
@@ -316,7 +316,6 @@
 describe('Transfers to self (potentially over substrate-evm boundary)', () => {
   itWeb3('Transfers to self. In case of same frontend', async ({api, privateKeyWrapper}) => {
     const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-    const alice = privateKeyWrapper('//Alice');
     const aliceProxy = subToEth(alice.address);
     const tokenId = await createItemExpectSuccess(alice, collectionId, 'Fungible', {Substrate: alice.address});
     await transferExpectSuccess(collectionId, tokenId, alice, {Ethereum: aliceProxy}, 10, 'Fungible');
@@ -328,7 +327,6 @@
 
   itWeb3('Transfers to self. In case of substrate-evm boundary', async ({api, privateKeyWrapper}) => {
     const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-    const alice = privateKeyWrapper('//Alice');
     const aliceProxy = subToEth(alice.address);
     const tokenId = await createItemExpectSuccess(alice, collectionId, 'Fungible', {Substrate: alice.address});
     const balanceAliceBefore = await getTokenBalance(api, collectionId, normalizeAccountId(alice), tokenId);
@@ -340,7 +338,6 @@
 
   itWeb3('Transfers to self. In case of inside substrate-evm', async ({api, privateKeyWrapper}) => {
     const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-    const alice = privateKeyWrapper('//Alice');
     const tokenId = await createItemExpectSuccess(alice, collectionId, 'Fungible', {Substrate: alice.address});
     const balanceAliceBefore = await getTokenBalance(api, collectionId, normalizeAccountId(alice), tokenId);
     await transferExpectSuccess(collectionId, tokenId, alice, alice , 10, 'Fungible');
@@ -351,7 +348,6 @@
 
   itWeb3('Transfers to self. In case of inside substrate-evm when not enought "Fungibles"', async ({api, privateKeyWrapper}) => {
     const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-    const alice = privateKeyWrapper('//Alice');
     const tokenId = await createItemExpectSuccess(alice, collectionId, 'Fungible', {Substrate: alice.address});
     const balanceAliceBefore = await getTokenBalance(api, collectionId, normalizeAccountId(alice), tokenId);
     await transferExpectFailure(collectionId, tokenId, alice, alice , 11);
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -23,7 +23,6 @@
 import BN from 'bn.js';
 import chai from 'chai';
 import chaiAsPromised from 'chai-as-promised';
-import {alicesPublicKey} from '../accounts';
 import {default as usingApi, executeTransaction, submitTransactionAsync, submitTransactionExpectFailAsync} from '../substrate/substrate-api';
 import {hexToStr, strToUTF16, utf16ToStr} from './util';
 import {UpDataStructsRpcCollection, UpDataStructsCreateItemData, UpDataStructsProperty} from '@polkadot/types/lookup';
@@ -40,7 +39,7 @@
 
 export function normalizeAccountId(input: string | AccountId | CrossAccountId | IKeyringPair): CrossAccountId {
   if (typeof input === 'string') {
-    if (input.length === 48 || input.length === 47) {
+    if (input.length >= 47) {
       return {Substrate: input};
     } else if (input.length === 42 && input.startsWith('0x')) {
       return {Ethereum: input.toLowerCase()};
@@ -363,7 +362,7 @@
     // tslint:disable-next-line:no-unused-expression
     expect(collection).to.be.not.null;
     expect(collectionCountAfter).to.be.equal(collectionCountBefore + 1, 'Error: NFT collection NOT created.');
-    expect(collection.owner.toString()).to.be.equal(toSubstrateAddress(alicesPublicKey));
+    expect(collection.owner.toString()).to.be.equal(toSubstrateAddress(alicePrivateKey));
     expect(utf16ToStr(collection.name.toJSON() as any)).to.be.equal(name);
     expect(utf16ToStr(collection.description.toJSON() as any)).to.be.equal(description);
     expect(hexToStr(collection.tokenPrefix.toJSON())).to.be.equal(tokenPrefix);
@@ -411,7 +410,7 @@
     // tslint:disable-next-line:no-unused-expression
     expect(collection).to.be.not.null;
     expect(collectionCountAfter).to.be.equal(collectionCountBefore + 1, 'Error: NFT collection NOT created.');
-    expect(collection.owner.toString()).to.be.equal(toSubstrateAddress(alicesPublicKey));
+    expect(collection.owner.toString()).to.be.equal(toSubstrateAddress(alicePrivateKey));
     expect(utf16ToStr(collection.name.toJSON() as any)).to.be.equal(name);
     expect(utf16ToStr(collection.description.toJSON() as any)).to.be.equal(description);
     expect(hexToStr(collection.tokenPrefix.toJSON())).to.be.equal(tokenPrefix);
modifiedtests/src/xcmTransfer.test.tsdiffbeforeafterboth
--- a/tests/src/xcmTransfer.test.ts
+++ b/tests/src/xcmTransfer.test.ts
@@ -24,7 +24,6 @@
 import {getGenericResult} from './util/helpers';
 import waitNewBlocks from './substrate/wait-new-blocks';
 import getBalance from './substrate/get-balance';
-import {alicesPublicKey} from './accounts';
 
 chai.use(chaiAsPromised);
 const expect = chai.expect;
@@ -144,7 +143,7 @@
     let balanceBefore: bigint;
     
     await usingApi(async (api) => {
-      [balanceBefore] = await getBalance(api, [alicesPublicKey]);
+      [balanceBefore] = await getBalance(api, [alice.address]);
     });
 
     await usingApi(async (api) => {
@@ -181,7 +180,7 @@
     await usingApi(async (api) => {
       // todo do something about instant sealing, where there might not be any new blocks
       await waitNewBlocks(api, 3);
-      const [balanceAfter] = await getBalance(api, [alicesPublicKey]);
+      const [balanceAfter] = await getBalance(api, [alice.address]);
       expect(balanceAfter > balanceBefore).to.be.true;
     });
   });