difftreelog
fix eth tests
in: master
5 files changed
tests/src/eth/base.test.tsdiffbeforeafterboth--- a/tests/src/eth/base.test.ts
+++ b/tests/src/eth/base.test.ts
@@ -23,7 +23,6 @@
import {IKeyringPair} from '@polkadot/types/types';
import {EthUniqueHelper, itEth, usingEthPlaygrounds, expect} from './util/playgrounds';
-import {UNIQUE} from '../util/helpers';
describe('Contract calls', () => {
let donor: IKeyringPair;
@@ -39,7 +38,7 @@
const flipper = await helper.eth.deployFlipper(deployer);
const cost = await recordEthFee(helper.api!, deployer, () => flipper.methods.flip().send({from: deployer}));
- expect(cost < BigInt(0.2 * Number(UNIQUE))).to.be.true;
+ expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal()))).to.be.true;
});
itEth('Balance transfer fee is less than 0.2 UNQ', async ({helper}) => {
@@ -47,7 +46,7 @@
const userB = helper.eth.createAccount();
const cost = await recordEthFee(helper.api!, userA, () => helper.web3!.eth.sendTransaction({from: userA, to: userB, value: '1000000', ...GAS_ARGS}));
const balanceB = await ethBalanceViaSub(helper.api!, userB);
- expect(cost - balanceB < BigInt(0.2 * Number(UNIQUE))).to.be.true;
+ expect(cost - balanceB < BigInt(0.2 * Number(helper.balance.getOneTokenNominal()))).to.be.true;
});
itEth('NFT transfer is close to 0.15 UNQ', async ({helper}) => {
@@ -63,7 +62,7 @@
const cost = await recordEthFee(helper.api!, caller, () => contract.methods.transfer(receiver, tokenId).send(caller));
- const fee = Number(cost) / Number(UNIQUE);
+ const fee = Number(cost) / Number(helper.balance.getOneTokenNominal());
const expectedFee = 0.15;
const tolerance = 0.001;
tests/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);
tests/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'));
});
});
tests/src/eth/proxy/fungibleProxy.test.tsdiffbeforeafterboth1// 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 {GAS_ARGS, normalizeEvents} from '../util/helpers';18import {expect} from 'chai';19import {readFile} from 'fs/promises';20import {IKeyringPair} from '@polkadot/types/types';21import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util/playgrounds';2223async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {24 // Proxy owner has no special privilegies, we don't need to reuse them25 const owner = await helper.eth.createAccountWithBalance(donor);26 const proxyContract = new helper.web3!.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueFungibleProxy.abi`)).toString()), undefined, {27 from: owner,28 ...GAS_ARGS,29 });30 const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueFungibleProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});31 return proxy;32}3334describe('Fungible (Via EVM proxy): Information getting', () => {35 let alice: IKeyringPair;36 let donor: IKeyringPair;3738 before(async function() {39 await usingEthPlaygrounds(async (helper, privateKey) => {40 donor = privateKey('//Alice');41 [alice] = await helper.arrange.createAccounts([10n], donor);42 });43 });4445 itEth('totalSupply', async ({helper}) => {46 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);47 const caller = await helper.eth.createAccountWithBalance(donor);48 await collection.mint(alice, 200n, {Substrate: alice.address});4950 const address = helper.ethAddress.fromCollectionId(collection.collectionId);51 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);52 const contract = await proxyWrap(helper, evmCollection, donor);53 const totalSupply = await contract.methods.totalSupply().call();5455 expect(totalSupply).to.equal('200');56 });5758 itEth('balanceOf', async ({helper}) => {59 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);60 const caller = await helper.eth.createAccountWithBalance(donor);6162 await collection.mint(alice, 200n, {Ethereum: caller});6364 const address = helper.ethAddress.fromCollectionId(collection.collectionId);65 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);66 const contract = await proxyWrap(helper, evmCollection, donor);67 const balance = await contract.methods.balanceOf(caller).call();6869 expect(balance).to.equal('200');70 });71});7273describe('Fungible (Via EVM proxy): Plain calls', () => {74 let alice: IKeyringPair;75 let donor: IKeyringPair;7677 before(async function() {78 await usingEthPlaygrounds(async (helper, privateKey) => {79 donor = privateKey('//Alice');80 [alice] = await helper.arrange.createAccounts([10n], donor);81 });82 });8384 itEth('Can perform approve()', async ({helper}) => {85 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);86 const caller = await helper.eth.createAccountWithBalance(donor);87 const spender = helper.eth.createAccount();8889 const address = helper.ethAddress.fromCollectionId(collection.collectionId);90 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);91 const contract = await proxyWrap(helper, evmCollection, donor);92 await collection.mint(alice, 200n, {Ethereum: contract.options.address});9394 {95 const result = await contract.methods.approve(spender, 100).send({from: caller});96 const events = normalizeEvents(result.events);9798 expect(events).to.be.deep.equal([99 {100 address,101 event: 'Approval',102 args: {103 owner: contract.options.address,104 spender,105 value: '100',106 },107 },108 ]);109 }110111 {112 const allowance = await contract.methods.allowance(contract.options.address, spender).call();113 expect(+allowance).to.equal(100);114 }115 });116117 itEth('Can perform transferFrom()', async ({helper}) => {118 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);119 const caller = await helper.eth.createAccountWithBalance(donor);120 const owner = await helper.eth.createAccountWithBalance(donor);121122 await collection.mint(alice, 200n, {Ethereum: owner});123 const receiver = helper.eth.createAccount();124125 const address = helper.ethAddress.fromCollectionId(collection.collectionId);126 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);127 const contract = await proxyWrap(helper, evmCollection, donor);128129 await evmCollection.methods.approve(contract.options.address, 100).send({from: owner});130131 {132 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: caller});133 const events = normalizeEvents(result.events);134 expect(events).to.be.deep.equal([135 {136 address,137 event: 'Transfer',138 args: {139 from: owner,140 to: receiver,141 value: '49',142 },143 },144 {145 address,146 event: 'Approval',147 args: {148 owner,149 spender: contract.options.address,150 value: '51',151 },152 },153 ]);154 }155156 {157 const balance = await contract.methods.balanceOf(receiver).call();158 expect(+balance).to.equal(49);159 }160161 {162 const balance = await contract.methods.balanceOf(owner).call();163 expect(+balance).to.equal(151);164 }165 });166167 itEth('Can perform transfer()', async ({helper}) => {168 const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);169 const caller = await helper.eth.createAccountWithBalance(donor);170 const receiver = await helper.eth.createAccountWithBalance(donor);171172 const address = helper.ethAddress.fromCollectionId(collection.collectionId);173 const evmCollection = helper.ethNativeContract.collection(address, 'ft', caller);174 const contract = await proxyWrap(helper, evmCollection, donor);175 await collection.mint(alice, 200n, {Ethereum: contract.options.address});176177 {178 const result = await contract.methods.transfer(receiver, 50).send({from: caller});179 const events = normalizeEvents(result.events);180 expect(events).to.be.deep.equal([181 {182 address,183 event: 'Transfer',184 args: {185 from: contract.options.address,186 to: receiver,187 value: '50',188 },189 },190 ]);191 }192193 {194 const balance = await contract.methods.balanceOf(contract.options.address).call();195 expect(+balance).to.equal(150);196 }197198 {199 const balance = await contract.methods.balanceOf(receiver).call();200 expect(+balance).to.equal(50);201 }202 });203});tests/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,
});