difftreelog
test call fees
in: master
6 files changed
tests/src/eth/base.test.tsdiffbeforeafterbothno changes
tests/src/eth/fungible.test.tsdiffbeforeafterboth4//4//556import 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';1111192 });192 });193});193});194195describe('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');201202 const owner = await createEthAccountWithBalance(api, web3);203 const spender = createEthAccount(web3);204205 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });206207 const address = collectionIdToAddress(collection);208 const contract = new web3.eth.Contract(fungibleAbi as any, address, { from: owner, ...GAS_ARGS });209210 const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, 100).send({ from: owner }));211 expect(cost < BigInt(0.2 * Number(UNIQUE)));212 });213214 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);222223 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });224225 const address = collectionIdToAddress(collection);226 const contract = new web3.eth.Contract(fungibleAbi as any, address, { from: owner, ...GAS_ARGS });227228 await contract.methods.approve(spender, 100).send({ from: owner });229230 const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, 100).send({ from: spender }));231 expect(cost < BigInt(0.2 * Number(UNIQUE)));232 });233234 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');239240 const owner = await createEthAccountWithBalance(api, web3);241 const receiver = createEthAccount(web3);242243 await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });244245 const address = collectionIdToAddress(collection);246 const contract = new web3.eth.Contract(fungibleAbi as any, address, { from: owner, ...GAS_ARGS });247248 const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, 100).send({ from: owner }));249 expect(cost < BigInt(0.2 * Number(UNIQUE)));250 });251});194252195describe('Fungible: Substrate calls', () => {253describe('Fungible: Substrate calls', () => {196 itWeb3('Events emitted for approve()', async ({ web3 }) => {254 itWeb3('Events emitted for approve()', async ({ web3 }) => {tests/src/eth/helpersSmoke.test.tsdiffbeforeafterboth1import { 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';445itWeb3('Contract owner is recorded', async ({ api, web3 }) => {5describe('Helpers sanity check', () => {6 await usingWeb3Http(async web3Http => {7 const owner = await createEthAccountWithBalance(api, web3Http);89 const flipper = await deployFlipper(web3Http, owner);10 await waitNewBlocks(api, 1);1112 expect(await contractHelpers(web3, owner).methods.contractOwner(flipper.options.address).call()).to.be.equal(owner);13 });14});1516itWeb3('Flipper is working', async({api}) => {6 itWeb3('Contract owner is recorded', async ({ api, web3 }) => {7 const owner = await createEthAccountWithBalance(api, web3);89 const flipper = await deployFlipper(web3, owner);10 await waitNewBlocks(api, 1);1112 expect(await contractHelpers(web3, owner).methods.contractOwner(flipper.options.address).call()).to.be.equal(owner);13 });1417 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);211922 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});tests/src/eth/nonFungible.test.tsdiffbeforeafterboth4//4//556import 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});194195describe('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');201202 const owner = await createEthAccountWithBalance(api, web3);203 const spender = createEthAccount(web3);204205 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });206207 const address = collectionIdToAddress(collection);208 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });209210 const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, tokenId).send({ from: owner }));211 expect(cost < BigInt(0.2 * Number(UNIQUE)));212 });213214 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);222223 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });224225 const address = collectionIdToAddress(collection);226 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });227228 await contract.methods.approve(spender, tokenId).send({ from: owner });229230 const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({ from: spender }));231 expect(cost < BigInt(0.2 * Number(UNIQUE)));232 });233234 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');239240 const owner = await createEthAccountWithBalance(api, web3);241 const receiver = createEthAccount(web3);242243 const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });244245 const address = collectionIdToAddress(collection);246 const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });247248 const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, tokenId).send({ from: owner }));249 expect(cost < BigInt(0.2 * Number(UNIQUE)));250 });251});194252195describe('NFT: Substrate calls', () => {253describe('NFT: Substrate calls', () => {196 itWeb3('Events emitted for approve()', async ({ web3 }) => {254 itWeb3('Events emitted for approve()', async ({ web3 }) => {tests/src/eth/util/helpers.tsdiffbeforeafterboth17import 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';202121export const GAS_ARGS = { gas: 0x1000000, gasPrice: '0x01' };22export const GAS_ARGS = { gas: 0x1000000, gasPrice: '0x01' };2223223 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}226227export async function ethBalanceViaSub(api: ApiPromise, address: string): Promise<bigint> {228 return (await getBalance(api, [evmToAddress(address)]))[0];229}230231export async function recordEthFee(api: ApiPromise, user: string, call: () => Promise<any>): Promise<bigint> {232 const before = await ethBalanceViaSub(api, user);233234 await call();235236 const after = await ethBalanceViaSub(api, user);237238 // Can't use .to.be.less, because chai doesn't supports bigint239 expect(after < before).to.be.true;240241 return before - after;242}tests/src/util/helpers.tsdiffbeforeafterboth535354export const U128_MAX = (1n << 128n) - 1n;54export const U128_MAX = (1n << 128n) - 1n;5556const MICROUNIQUE = 1_000_000_000n;57const MILLIUNIQUE = 1_000n * MICROUNIQUE;58const CENTIUNIQUE = 10n * MILLIUNIQUE;59export const UNIQUE = 100n * CENTIUNIQUE;556056type GenericResult = {61type GenericResult = {57 success: boolean,62 success: boolean,