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

difftreelog

fix eth tests

rkv2022-10-04parent: #104c91a.patch.diff
in: master

5 files changed

modifiedtests/src/eth/base.test.tsdiffbeforeafterboth
2323
24import {IKeyringPair} from '@polkadot/types/types';24import {IKeyringPair} from '@polkadot/types/types';
25import {EthUniqueHelper, itEth, usingEthPlaygrounds, expect} from './util/playgrounds';25import {EthUniqueHelper, itEth, usingEthPlaygrounds, expect} from './util/playgrounds';
26import {UNIQUE} from '../util/helpers';
2726
28describe('Contract calls', () => {27describe('Contract calls', () => {
29 let donor: IKeyringPair;28 let donor: IKeyringPair;
39 const flipper = await helper.eth.deployFlipper(deployer);38 const flipper = await helper.eth.deployFlipper(deployer);
4039
41 const cost = await recordEthFee(helper.api!, deployer, () => flipper.methods.flip().send({from: deployer}));40 const cost = await recordEthFee(helper.api!, deployer, () => flipper.methods.flip().send({from: deployer}));
42 expect(cost < BigInt(0.2 * Number(UNIQUE))).to.be.true;41 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal()))).to.be.true;
43 });42 });
4443
45 itEth('Balance transfer fee is less than 0.2 UNQ', async ({helper}) => {44 itEth('Balance transfer fee is less than 0.2 UNQ', async ({helper}) => {
46 const userA = await helper.eth.createAccountWithBalance(donor);45 const userA = await helper.eth.createAccountWithBalance(donor);
47 const userB = helper.eth.createAccount();46 const userB = helper.eth.createAccount();
48 const cost = await recordEthFee(helper.api!, userA, () => helper.web3!.eth.sendTransaction({from: userA, to: userB, value: '1000000', ...GAS_ARGS}));47 const cost = await recordEthFee(helper.api!, userA, () => helper.web3!.eth.sendTransaction({from: userA, to: userB, value: '1000000', ...GAS_ARGS}));
49 const balanceB = await ethBalanceViaSub(helper.api!, userB);48 const balanceB = await ethBalanceViaSub(helper.api!, userB);
50 expect(cost - balanceB < BigInt(0.2 * Number(UNIQUE))).to.be.true;49 expect(cost - balanceB < BigInt(0.2 * Number(helper.balance.getOneTokenNominal()))).to.be.true;
51 });50 });
5251
53 itEth('NFT transfer is close to 0.15 UNQ', async ({helper}) => {52 itEth('NFT transfer is close to 0.15 UNQ', async ({helper}) => {
6362
64 const cost = await recordEthFee(helper.api!, caller, () => contract.methods.transfer(receiver, tokenId).send(caller));63 const cost = await recordEthFee(helper.api!, caller, () => contract.methods.transfer(receiver, tokenId).send(caller));
6564
66 const fee = Number(cost) / Number(UNIQUE);65 const fee = Number(cost) / Number(helper.balance.getOneTokenNominal());
67 const expectedFee = 0.15;66 const expectedFee = 0.15;
68 const tolerance = 0.001;67 const tolerance = 0.001;
6968
modifiedtests/src/eth/collectionAdmin.test.tsdiffbeforeafterboth
--- a/tests/src/eth/collectionAdmin.test.ts
+++ b/tests/src/eth/collectionAdmin.test.ts
@@ -14,11 +14,32 @@
 // along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
 
 import {IKeyringPair} from '@polkadot/types/types';
-import {UNIQUE} from '../util/helpers';
-import {
-  recordEthFee,
-} from './util/helpers';
-import {usingEthPlaygrounds, itEth, expect} from './util/playgrounds';
+import {usingEthPlaygrounds, itEth, expect, EthUniqueHelper} from './util/playgrounds';
+
+async function waitNewBlocks(helper: EthUniqueHelper, count: number) {
+  // eslint-disable-next-line no-async-promise-executor
+  return new Promise<void>(async (resolve) => {
+    const unsubscribe = await helper.callRpc('api.rpc.chain.subscribeNewHeads', [() => {
+      if (count > 0) {
+        count--;
+      } else {
+        unsubscribe();
+        resolve();
+      }
+    }]);
+  });
+}
+
+async function recordEthFee(helper: EthUniqueHelper, userAddress: string, call: () => Promise<any>) {
+  const before = await helper.balance.getSubstrate(helper.address.ethToSubstrate(userAddress));
+  await call();
+  waitNewBlocks(helper, 1);
+  const after = await helper.balance.getSubstrate(helper.address.ethToSubstrate(userAddress));
+
+  expect(after < before).to.be.true;
+
+  return before - after;
+}
 
 describe('Add collection admins', () => {
   let donor: IKeyringPair;
@@ -37,7 +58,7 @@
     const newAdmin = helper.eth.createAccount();
 
     await collectionEvm.methods.addCollectionAdmin(newAdmin).send();
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList[0].asEthereum.toString().toLocaleLowerCase())
       .to.be.eq(newAdmin.toLocaleLowerCase());
   });
@@ -50,12 +71,11 @@
     const [newAdmin] = await helper.arrange.createAccounts([10n], donor);
     await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();
 
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())
       .to.be.eq(newAdmin.address.toLocaleLowerCase());
   });
 
-  //FIXME:
   itEth('Verify owner or admin', async ({helper}) => {
     const owner = await helper.eth.createAccountWithBalance(donor);
     const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');
@@ -79,7 +99,7 @@
     await expect(collectionEvm.methods.addCollectionAdmin(user).call({from: admin}))
       .to.be.rejectedWith('NoPermission');
 
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList.length).to.be.eq(1);
     expect(adminList[0].asEthereum.toString().toLocaleLowerCase())
       .to.be.eq(admin.toLocaleLowerCase());
@@ -96,7 +116,7 @@
     await expect(collectionEvm.methods.addCollectionAdmin(user).call({from: notAdmin}))
       .to.be.rejectedWith('NoPermission');
 
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList.length).to.be.eq(0);
   });
 
@@ -112,7 +132,7 @@
     await expect(collectionEvm.methods.addCollectionAdminSubstrate(notAdmin.addressRaw).call({from: admin}))
       .to.be.rejectedWith('NoPermission');
 
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList.length).to.be.eq(1);
     expect(adminList[0].asEthereum.toString().toLocaleLowerCase())
       .to.be.eq(admin.toLocaleLowerCase());
@@ -128,7 +148,7 @@
     await expect(collectionEvm.methods.addCollectionAdminSubstrate(notAdmin1.addressRaw).call({from: notAdmin0}))
       .to.be.rejectedWith('NoPermission');
 
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList.length).to.be.eq(0);
   });
 });
@@ -151,14 +171,14 @@
     await collectionEvm.methods.addCollectionAdmin(newAdmin).send();
 
     {
-      const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+      const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
       expect(adminList.length).to.be.eq(1);
       expect(adminList[0].asEthereum.toString().toLocaleLowerCase())
         .to.be.eq(newAdmin.toLocaleLowerCase());
     }
 
     await collectionEvm.methods.removeCollectionAdmin(newAdmin).send();
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList.length).to.be.eq(0);
   });
 
@@ -170,13 +190,13 @@
     const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
     await collectionEvm.methods.addCollectionAdminSubstrate(newAdmin.addressRaw).send();
     {
-      const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+      const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
       expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())
         .to.be.eq(newAdmin.address.toLocaleLowerCase());
     }
 
     await collectionEvm.methods.removeCollectionAdminSubstrate(newAdmin.addressRaw).send();
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList.length).to.be.eq(0);
   });
 
@@ -194,7 +214,7 @@
     await expect(collectionEvm.methods.removeCollectionAdmin(admin1).call({from: admin0}))
       .to.be.rejectedWith('NoPermission');
     {
-      const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+      const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
       expect(adminList.length).to.be.eq(2);
       expect(adminList.toString().toLocaleLowerCase())
         .to.be.deep.contains(admin0.toLocaleLowerCase())
@@ -215,7 +235,7 @@
     await expect(collectionEvm.methods.removeCollectionAdmin(admin).call({from: notAdmin}))
       .to.be.rejectedWith('NoPermission');
     {
-      const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+      const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
       expect(adminList[0].asEthereum.toString().toLocaleLowerCase())
         .to.be.eq(admin.toLocaleLowerCase());
       expect(adminList.length).to.be.eq(1);
@@ -235,7 +255,7 @@
     await expect(collectionEvm.methods.removeCollectionAdminSubstrate(adminSub.addressRaw).call({from: adminEth}))
       .to.be.rejectedWith('NoPermission');
 
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList.length).to.be.eq(2);
     expect(adminList.toString().toLocaleLowerCase())
       .to.be.deep.contains(adminSub.address.toLocaleLowerCase())
@@ -254,7 +274,7 @@
     await expect(collectionEvm.methods.removeCollectionAdminSubstrate(adminSub.addressRaw).call({from: notAdminEth}))
       .to.be.rejectedWith('NoPermission');
 
-    const adminList = await helper.api!.rpc.unique.adminlist(collectionId);
+    const adminList = await helper.callRpc('api.rpc.unique.adminlist', [collectionId]);
     expect(adminList.length).to.be.eq(1);
     expect(adminList[0].asSubstrate.toString().toLocaleLowerCase())
       .to.be.eq(adminSub.address.toLocaleLowerCase());
@@ -287,13 +307,11 @@
     const newOwner = await helper.eth.createAccountWithBalance(donor);
     const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');
     const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
-
-    const cost = await recordEthFee(helper.api!, owner, () => collectionEvm.methods.setOwner(newOwner).send());
-    expect(cost < BigInt(0.2 * Number(UNIQUE)));
+    const cost = await recordEthFee(helper, owner, () => collectionEvm.methods.setOwner(newOwner).send());
+    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));
     expect(cost > 0);
   });
 
-  //FIXME
   itEth('(!negative tests!) call setOwner by non owner', async ({helper}) => {
     const owner = await helper.eth.createAccountWithBalance(donor);
     const newOwner = await helper.eth.createAccountWithBalance(donor);
@@ -314,7 +332,6 @@
     });
   });
 
-  //FIXME
   itEth.skip('Change owner', async ({helper}) => {
     const owner = await helper.eth.createAccountWithBalance(donor);
     const [newOwner] = await helper.arrange.createAccounts([10n], donor);
@@ -336,12 +353,11 @@
     const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');
     const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
 
-    const cost = await recordEthFee(helper.api!, owner, () => collectionEvm.methods.setOwnerSubstrate(newOwner.addressRaw).send());
-    expect(cost < BigInt(0.2 * Number(UNIQUE)));
+    const cost = await recordEthFee(helper, owner, () => collectionEvm.methods.setOwnerSubstrate(newOwner.addressRaw).send());
+    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));
     expect(cost > 0);
   });
 
-  //FIXME
   itEth.skip('(!negative tests!) call setOwner by non owner', async ({helper}) => {
     const owner = await helper.eth.createAccountWithBalance(donor);
     const otherReceiver = await helper.eth.createAccountWithBalance(donor);
modifiedtests/src/eth/collectionProperties.test.tsdiffbeforeafterboth
--- a/tests/src/eth/collectionProperties.test.ts
+++ b/tests/src/eth/collectionProperties.test.ts
@@ -51,6 +51,6 @@
     const contract = helper.ethNativeContract.collection(address, 'nft', caller);
 
     const value = await contract.methods.collectionProperty('testKey').call();
-    expect(value).to.equal(helper.web3?.utils.toHex('testValue'));
+    expect(value).to.equal(helper.getWeb3().utils.toHex('testValue'));
   });
 });
modifiedtests/src/eth/proxy/fungibleProxy.test.tsdiffbeforeafterboth
--- a/tests/src/eth/proxy/fungibleProxy.test.ts
+++ b/tests/src/eth/proxy/fungibleProxy.test.ts
@@ -23,7 +23,8 @@
 async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {
   // Proxy owner has no special privilegies, we don't need to reuse them
   const owner = await helper.eth.createAccountWithBalance(donor);
-  const proxyContract = new helper.web3!.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {
+  const web3 = helper.getWeb3();
+  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {
     from: owner,
     ...GAS_ARGS,
   });
modifiedtests/src/eth/proxy/nonFungibleProxy.test.tsdiffbeforeafterboth
--- a/tests/src/eth/proxy/nonFungibleProxy.test.ts
+++ b/tests/src/eth/proxy/nonFungibleProxy.test.ts
@@ -23,7 +23,8 @@
 async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {
   // Proxy owner has no special privilegies, we don't need to reuse them
   const owner = await helper.eth.createAccountWithBalance(donor);
-  const proxyContract = new helper.web3!.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueNFTProxy.abi`)).toString()), undefined, {
+  const web3 = helper.getWeb3();
+  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueNFTProxy.abi`)).toString()), undefined, {
     from: owner,
     ...GAS_ARGS,
   });