difftreelog
test call fees
in: master
6 files changed
tests/src/eth/base.test.tsdiffbeforeafterboth--- /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
tests/src/eth/fungible.test.tsdiffbeforeafterboth--- 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({
tests/src/eth/helpersSmoke.test.tsdiffbeforeafterboth--- 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;
});
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.tsdiffbeforeafterboth--- 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<bigint> {
+ return (await getBalance(api, [evmToAddress(address)]))[0];
+}
+
+export async function recordEthFee(api: ApiPromise, user: string, call: () => Promise<any>): Promise<bigint> {
+ 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
tests/src/util/helpers.tsdiffbeforeafterboth--- 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,
};