--- /dev/null +++ b/tests/src/eth/base.test.ts @@ -0,0 +1,22 @@ + +import { createEthAccount, createEthAccountWithBalance, deployFlipper, ethBalanceViaSub, GAS_ARGS, itWeb3, recordEthFee } from './util/helpers'; +import { expect } from 'chai'; +import { UNIQUE } from '../util/helpers'; + +describe('Contract calls', () => { + itWeb3('Call of simple contract fee is less than 0.2 UNQ', async ({ web3, api }) => { + const deployer = await createEthAccountWithBalance(api, web3); + const flipper = await deployFlipper(web3 as any, deployer); + + const cost = await recordEthFee(api, deployer, () => flipper.methods.flip()); + expect(cost < BigInt(0.2 * Number(UNIQUE))).to.be.true; + }); + + itWeb3('Balance transfer fee is less than 0.2 UNQ', async ({ web3, api }) => { + const userA = await createEthAccountWithBalance(api, web3); + const userB = createEthAccount(web3); + + const cost = await recordEthFee(api, userA, () => web3.eth.sendTransaction({ from: userA, to: userB, value: '1000000', ...GAS_ARGS })); + expect(cost - await ethBalanceViaSub(api, userB) < BigInt(0.2 * Number(UNIQUE))).to.be.true; + }); +}); \ No newline at end of file --- a/tests/src/eth/fungible.test.ts +++ b/tests/src/eth/fungible.test.ts @@ -4,8 +4,8 @@ // import privateKey from '../substrate/privateKey'; -import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from '../util/helpers'; -import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers'; +import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess, UNIQUE } from '../util/helpers'; +import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, transferBalanceToEth } from './util/helpers'; import fungibleAbi from './fungibleAbi.json'; import { expect } from 'chai'; @@ -192,6 +192,64 @@ }); }); +describe('Fungible: Fees', () => { + itWeb3('approve() call fee is less than 0.2UNQ', async ({ web3, api }) => { + const collection = await createCollectionExpectSuccess({ + mode: { type: 'Fungible', decimalPoints: 0 }, + }); + const alice = privateKey('//Alice'); + + const owner = await createEthAccountWithBalance(api, web3); + const spender = createEthAccount(web3); + + await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner }); + + const address = collectionIdToAddress(collection); + const contract = new web3.eth.Contract(fungibleAbi as any, address, { from: owner, ...GAS_ARGS }); + + const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, 100).send({ from: owner })); + expect(cost < BigInt(0.2 * Number(UNIQUE))); + }); + + itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({ web3, api }) => { + const collection = await createCollectionExpectSuccess({ + mode: {type: 'Fungible', decimalPoints: 0}, + }); + const alice = privateKey('//Alice'); + + const owner = await createEthAccountWithBalance(api, web3); + const spender = await createEthAccountWithBalance(api, web3); + + await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner }); + + const address = collectionIdToAddress(collection); + const contract = new web3.eth.Contract(fungibleAbi as any, address, { from: owner, ...GAS_ARGS }); + + await contract.methods.approve(spender, 100).send({ from: owner }); + + const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, 100).send({ from: spender })); + expect(cost < BigInt(0.2 * Number(UNIQUE))); + }); + + itWeb3('transfer() call fee is less than 0.2UNQ', async ({ web3, api }) => { + const collection = await createCollectionExpectSuccess({ + mode: { type: 'Fungible', decimalPoints: 0 }, + }); + const alice = privateKey('//Alice'); + + const owner = await createEthAccountWithBalance(api, web3); + const receiver = createEthAccount(web3); + + await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner }); + + const address = collectionIdToAddress(collection); + const contract = new web3.eth.Contract(fungibleAbi as any, address, { from: owner, ...GAS_ARGS }); + + const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, 100).send({ from: owner })); + expect(cost < BigInt(0.2 * Number(UNIQUE))); + }); +}); + describe('Fungible: Substrate calls', () => { itWeb3('Events emitted for approve()', async ({ web3 }) => { const collection = await createCollectionExpectSuccess({ --- a/tests/src/eth/helpersSmoke.test.ts +++ b/tests/src/eth/helpersSmoke.test.ts @@ -1,26 +1,24 @@ import { expect } from 'chai'; import waitNewBlocks from '../substrate/wait-new-blocks'; -import { createEthAccountWithBalance, deployFlipper, itWeb3, usingWeb3Http, contractHelpers } from './util/helpers'; +import { createEthAccountWithBalance, deployFlipper, itWeb3, contractHelpers } from './util/helpers'; -itWeb3('Contract owner is recorded', async ({ api, web3 }) => { - await usingWeb3Http(async web3Http => { - const owner = await createEthAccountWithBalance(api, web3Http); +describe('Helpers sanity check', () => { + itWeb3('Contract owner is recorded', async ({ api, web3 }) => { + const owner = await createEthAccountWithBalance(api, web3); - const flipper = await deployFlipper(web3Http, owner); + const flipper = await deployFlipper(web3, owner); await waitNewBlocks(api, 1); expect(await contractHelpers(web3, owner).methods.contractOwner(flipper.options.address).call()).to.be.equal(owner); }); -}); -itWeb3('Flipper is working', async({api}) => { - await usingWeb3Http(async web3Http => { - const owner = await createEthAccountWithBalance(api, web3Http); - const flipper = await deployFlipper(web3Http, owner); + itWeb3('Flipper is working', async ({ api, web3 }) => { + const owner = await createEthAccountWithBalance(api, web3); + const flipper = await deployFlipper(web3, owner); await waitNewBlocks(api, 1); expect(await flipper.methods.getValue().call()).to.be.false; - await flipper.methods.flip().send({from: owner}); + await flipper.methods.flip().send({ from: owner }); await waitNewBlocks(api, 1); expect(await flipper.methods.getValue().call()).to.be.true; }); --- a/tests/src/eth/nonFungible.test.ts +++ b/tests/src/eth/nonFungible.test.ts @@ -4,8 +4,8 @@ // import privateKey from '../substrate/privateKey'; -import { approveExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from '../util/helpers'; -import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers'; +import { approveExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess, UNIQUE } from '../util/helpers'; +import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, transferBalanceToEth } from './util/helpers'; import nonFungibleAbi from './nonFungibleAbi.json'; import { expect } from 'chai'; import waitNewBlocks from '../substrate/wait-new-blocks'; @@ -192,6 +192,64 @@ }); }); +describe('NFT: Fees', () => { + itWeb3('approve() call fee is less than 0.2UNQ', async ({ web3, api }) => { + const collection = await createCollectionExpectSuccess({ + mode: { type: 'NFT' }, + }); + const alice = privateKey('//Alice'); + + const owner = await createEthAccountWithBalance(api, web3); + const spender = createEthAccount(web3); + + const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner }); + + const address = collectionIdToAddress(collection); + const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS }); + + const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, tokenId).send({ from: owner })); + expect(cost < BigInt(0.2 * Number(UNIQUE))); + }); + + itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({ web3, api }) => { + const collection = await createCollectionExpectSuccess({ + mode: { type: 'NFT' }, + }); + const alice = privateKey('//Alice'); + + const owner = await createEthAccountWithBalance(api, web3); + const spender = await createEthAccountWithBalance(api, web3); + + const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner }); + + const address = collectionIdToAddress(collection); + const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS }); + + await contract.methods.approve(spender, tokenId).send({ from: owner }); + + const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({ from: spender })); + expect(cost < BigInt(0.2 * Number(UNIQUE))); + }); + + itWeb3('transfer() call fee is less than 0.2UNQ', async ({ web3, api }) => { + const collection = await createCollectionExpectSuccess({ + mode: { type: 'NFT' }, + }); + const alice = privateKey('//Alice'); + + const owner = await createEthAccountWithBalance(api, web3); + const receiver = createEthAccount(web3); + + const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner }); + + const address = collectionIdToAddress(collection); + const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS }); + + const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, tokenId).send({ from: owner })); + expect(cost < BigInt(0.2 * Number(UNIQUE))); + }); +}); + describe('NFT: Substrate calls', () => { itWeb3('Events emitted for approve()', async ({ web3 }) => { const collection = await createCollectionExpectSuccess({ --- a/tests/src/eth/util/helpers.ts +++ b/tests/src/eth/util/helpers.ts @@ -17,6 +17,7 @@ import config from '../../config'; import privateKey from '../../substrate/privateKey'; import contractHelpersAbi from './contractHelpersAbi.json'; +import getBalance from '../../substrate/get-balance'; export const GAS_ARGS = { gas: 0x1000000, gasPrice: '0x01' }; @@ -221,4 +222,21 @@ export function contractHelpers(web3: Web3, caller: string) { return new web3.eth.Contract(contractHelpersAbi as any, '0x842899ECF380553E8a4de75bF534cdf6fBF64049', {from: caller, ...GAS_ARGS}); +} + +export async function ethBalanceViaSub(api: ApiPromise, address: string): Promise { + return (await getBalance(api, [evmToAddress(address)]))[0]; +} + +export async function recordEthFee(api: ApiPromise, user: string, call: () => Promise): Promise { + const before = await ethBalanceViaSub(api, user); + + await call(); + + const after = await ethBalanceViaSub(api, user); + + // Can't use .to.be.less, because chai doesn't supports bigint + expect(after < before).to.be.true; + + return before - after; } \ No newline at end of file --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -53,6 +53,11 @@ export const U128_MAX = (1n << 128n) - 1n; +const MICROUNIQUE = 1_000_000_000n; +const MILLIUNIQUE = 1_000n * MICROUNIQUE; +const CENTIUNIQUE = 10n * MILLIUNIQUE; +export const UNIQUE = 100n * CENTIUNIQUE; + type GenericResult = { success: boolean, };