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

difftreelog

test call fees

Yaroslav Bolyukin2021-08-18parent: #0999c79.patch.diff
in: master

6 files changed

addedtests/src/eth/base.test.tsdiffbeforeafterboth

no changes

modifiedtests/src/eth/fungible.test.tsdiffbeforeafterboth
4//4//
55
6import privateKey from '../substrate/privateKey';6import privateKey from '../substrate/privateKey';
7import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from '../util/helpers';7import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess, UNIQUE } from '../util/helpers';
8import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';8import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';
9import fungibleAbi from './fungibleAbi.json';9import fungibleAbi from './fungibleAbi.json';
10import { expect } from 'chai';10import { expect } from 'chai';
1111
192 });192 });
193});193});
194
195describe('Fungible: Fees', () => {
196 itWeb3('approve() call fee is less than 0.2UNQ', async ({ web3, api }) => {
197 const collection = await createCollectionExpectSuccess({
198 mode: { type: 'Fungible', decimalPoints: 0 },
199 });
200 const alice = privateKey('//Alice');
201
202 const owner = await createEthAccountWithBalance(api, web3);
203 const spender = createEthAccount(web3);
204
205 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });
206
207 const address = collectionIdToAddress(collection);
208 const contract = new web3.eth.Contract(fungibleAbi as any, address, { from: owner, ...GAS_ARGS });
209
210 const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, 100).send({ from: owner }));
211 expect(cost < BigInt(0.2 * Number(UNIQUE)));
212 });
213
214 itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({ web3, api }) => {
215 const collection = await createCollectionExpectSuccess({
216 mode: {type: 'Fungible', decimalPoints: 0},
217 });
218 const alice = privateKey('//Alice');
219
220 const owner = await createEthAccountWithBalance(api, web3);
221 const spender = await createEthAccountWithBalance(api, web3);
222
223 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });
224
225 const address = collectionIdToAddress(collection);
226 const contract = new web3.eth.Contract(fungibleAbi as any, address, { from: owner, ...GAS_ARGS });
227
228 await contract.methods.approve(spender, 100).send({ from: owner });
229
230 const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, 100).send({ from: spender }));
231 expect(cost < BigInt(0.2 * Number(UNIQUE)));
232 });
233
234 itWeb3('transfer() call fee is less than 0.2UNQ', async ({ web3, api }) => {
235 const collection = await createCollectionExpectSuccess({
236 mode: { type: 'Fungible', decimalPoints: 0 },
237 });
238 const alice = privateKey('//Alice');
239
240 const owner = await createEthAccountWithBalance(api, web3);
241 const receiver = createEthAccount(web3);
242
243 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });
244
245 const address = collectionIdToAddress(collection);
246 const contract = new web3.eth.Contract(fungibleAbi as any, address, { from: owner, ...GAS_ARGS });
247
248 const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, 100).send({ from: owner }));
249 expect(cost < BigInt(0.2 * Number(UNIQUE)));
250 });
251});
194252
195describe('Fungible: Substrate calls', () => {253describe('Fungible: Substrate calls', () => {
196 itWeb3('Events emitted for approve()', async ({ web3 }) => {254 itWeb3('Events emitted for approve()', async ({ web3 }) => {
modifiedtests/src/eth/helpersSmoke.test.tsdiffbeforeafterboth
1import { expect } from 'chai';1import { expect } from 'chai';
2import waitNewBlocks from '../substrate/wait-new-blocks';2import waitNewBlocks from '../substrate/wait-new-blocks';
3import { createEthAccountWithBalance, deployFlipper, itWeb3, usingWeb3Http, contractHelpers } from './util/helpers';3import { createEthAccountWithBalance, deployFlipper, itWeb3, contractHelpers } from './util/helpers';
44
5itWeb3('Contract owner is recorded', async ({ api, web3 }) => {5describe('Helpers sanity check', () => {
6 await usingWeb3Http(async web3Http => {
7 const owner = await createEthAccountWithBalance(api, web3Http);
8
9 const flipper = await deployFlipper(web3Http, owner);
10 await waitNewBlocks(api, 1);
11
12 expect(await contractHelpers(web3, owner).methods.contractOwner(flipper.options.address).call()).to.be.equal(owner);
13 });
14});
15
16itWeb3('Flipper is working', async({api}) => {6 itWeb3('Contract owner is recorded', async ({ api, web3 }) => {
7 const owner = await createEthAccountWithBalance(api, web3);
8
9 const flipper = await deployFlipper(web3, owner);
10 await waitNewBlocks(api, 1);
11
12 expect(await contractHelpers(web3, owner).methods.contractOwner(flipper.options.address).call()).to.be.equal(owner);
13 });
14
17 await usingWeb3Http(async web3Http => {15 itWeb3('Flipper is working', async ({ api, web3 }) => {
18 const owner = await createEthAccountWithBalance(api, web3Http);16 const owner = await createEthAccountWithBalance(api, web3);
19 const flipper = await deployFlipper(web3Http, owner);17 const flipper = await deployFlipper(web3, owner);
20 await waitNewBlocks(api, 1);18 await waitNewBlocks(api, 1);
2119
22 expect(await flipper.methods.getValue().call()).to.be.false;20 expect(await flipper.methods.getValue().call()).to.be.false;
23 await flipper.methods.flip().send({from: owner});21 await flipper.methods.flip().send({ from: owner });
24 await waitNewBlocks(api, 1);22 await waitNewBlocks(api, 1);
25 expect(await flipper.methods.getValue().call()).to.be.true;23 expect(await flipper.methods.getValue().call()).to.be.true;
26 });24 });
27});25});
modifiedtests/src/eth/nonFungible.test.tsdiffbeforeafterboth
4//4//
55
6import privateKey from '../substrate/privateKey';6import privateKey from '../substrate/privateKey';
7import { approveExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from '../util/helpers';7import { approveExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess, UNIQUE } from '../util/helpers';
8import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';8import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';
9import nonFungibleAbi from './nonFungibleAbi.json';9import nonFungibleAbi from './nonFungibleAbi.json';
10import { expect } from 'chai';10import { expect } from 'chai';
11import waitNewBlocks from '../substrate/wait-new-blocks';11import waitNewBlocks from '../substrate/wait-new-blocks';
192 });192 });
193});193});
194
195describe('NFT: Fees', () => {
196 itWeb3('approve() call fee is less than 0.2UNQ', async ({ web3, api }) => {
197 const collection = await createCollectionExpectSuccess({
198 mode: { type: 'NFT' },
199 });
200 const alice = privateKey('//Alice');
201
202 const owner = await createEthAccountWithBalance(api, web3);
203 const spender = createEthAccount(web3);
204
205 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });
206
207 const address = collectionIdToAddress(collection);
208 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });
209
210 const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, tokenId).send({ from: owner }));
211 expect(cost < BigInt(0.2 * Number(UNIQUE)));
212 });
213
214 itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({ web3, api }) => {
215 const collection = await createCollectionExpectSuccess({
216 mode: { type: 'NFT' },
217 });
218 const alice = privateKey('//Alice');
219
220 const owner = await createEthAccountWithBalance(api, web3);
221 const spender = await createEthAccountWithBalance(api, web3);
222
223 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });
224
225 const address = collectionIdToAddress(collection);
226 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });
227
228 await contract.methods.approve(spender, tokenId).send({ from: owner });
229
230 const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({ from: spender }));
231 expect(cost < BigInt(0.2 * Number(UNIQUE)));
232 });
233
234 itWeb3('transfer() call fee is less than 0.2UNQ', async ({ web3, api }) => {
235 const collection = await createCollectionExpectSuccess({
236 mode: { type: 'NFT' },
237 });
238 const alice = privateKey('//Alice');
239
240 const owner = await createEthAccountWithBalance(api, web3);
241 const receiver = createEthAccount(web3);
242
243 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });
244
245 const address = collectionIdToAddress(collection);
246 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });
247
248 const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, tokenId).send({ from: owner }));
249 expect(cost < BigInt(0.2 * Number(UNIQUE)));
250 });
251});
194252
195describe('NFT: Substrate calls', () => {253describe('NFT: Substrate calls', () => {
196 itWeb3('Events emitted for approve()', async ({ web3 }) => {254 itWeb3('Events emitted for approve()', async ({ web3 }) => {
modifiedtests/src/eth/util/helpers.tsdiffbeforeafterboth
17import config from '../../config';17import config from '../../config';
18import privateKey from '../../substrate/privateKey';18import privateKey from '../../substrate/privateKey';
19import contractHelpersAbi from './contractHelpersAbi.json';19import contractHelpersAbi from './contractHelpersAbi.json';
20import getBalance from '../../substrate/get-balance';
2021
21export const GAS_ARGS = { gas: 0x1000000, gasPrice: '0x01' };22export const GAS_ARGS = { gas: 0x1000000, gasPrice: '0x01' };
2223
223 return new web3.eth.Contract(contractHelpersAbi as any, '0x842899ECF380553E8a4de75bF534cdf6fBF64049', {from: caller, ...GAS_ARGS});224 return new web3.eth.Contract(contractHelpersAbi as any, '0x842899ECF380553E8a4de75bF534cdf6fBF64049', {from: caller, ...GAS_ARGS});
224}225}
226
227export async function ethBalanceViaSub(api: ApiPromise, address: string): Promise<bigint> {
228 return (await getBalance(api, [evmToAddress(address)]))[0];
229}
230
231export async function recordEthFee(api: ApiPromise, user: string, call: () => Promise<any>): Promise<bigint> {
232 const before = await ethBalanceViaSub(api, user);
233
234 await call();
235
236 const after = await ethBalanceViaSub(api, user);
237
238 // Can't use .to.be.less, because chai doesn't supports bigint
239 expect(after < before).to.be.true;
240
241 return before - after;
242}
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
5353
54export const U128_MAX = (1n << 128n) - 1n;54export const U128_MAX = (1n << 128n) - 1n;
55
56const MICROUNIQUE = 1_000_000_000n;
57const MILLIUNIQUE = 1_000n * MICROUNIQUE;
58const CENTIUNIQUE = 10n * MILLIUNIQUE;
59export const UNIQUE = 100n * CENTIUNIQUE;
5560
56type GenericResult = {61type GenericResult = {
57 success: boolean,62 success: boolean,