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

difftreelog

test fix tests for updated frontier

Yaroslav Bolyukin2021-07-27parent: #7168c55.patch.diff
in: master

3 files changed

modifiedtests/src/eth/fungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/fungible.test.ts
+++ b/tests/src/eth/fungible.test.ts
@@ -5,41 +5,44 @@
 
 import privateKey from '../substrate/privateKey';
 import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from '../util/helpers';
-import { collectionIdToAddress, createEthAccount, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';
+import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';
 import fungibleAbi from './fungibleAbi.json';
 import { expect } from 'chai';
 
 describe('Information getting', () => {
-  itWeb3('totalSupply', async ({ web3 }) => {
+  itWeb3('totalSupply', async ({ api, web3 }) => {
     const collection = await createCollectionExpectSuccess({
       name: 'token name',
       mode: { type: 'Fungible', decimalPoints: 0 },
     });
     const alice = privateKey('//Alice');
 
+    const caller = await createEthAccountWithBalance(api, web3);
+  
     await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { substrate: alice.address });
 
     const address = collectionIdToAddress(collection);
     const contract = new web3.eth.Contract(fungibleAbi as any, address);
-    const totalSupply = await contract.methods.totalSupply().call();
+    const totalSupply = await contract.methods.totalSupply().call({ from: caller, ...GAS_ARGS });
 
     // FIXME: always equals to 0, because this method is not implemented
     expect(totalSupply).to.equal('0');
   });
 
-  itWeb3('balanceOf', async ({ web3 }) => {
+  itWeb3('balanceOf', async ({ api, web3 }) => {
     const collection = await createCollectionExpectSuccess({
       name: 'token name',
       mode: { type: 'Fungible', decimalPoints: 0 },
     });
     const alice = privateKey('//Alice');
 
-    const caller = createEthAccount(web3);
+    const caller = await createEthAccountWithBalance(api, web3);
+
     await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: caller });
 
     const address = collectionIdToAddress(collection);
     const contract = new web3.eth.Contract(fungibleAbi as any, address);
-    const balance = await contract.methods.balanceOf(caller).call();
+    const balance = await contract.methods.balanceOf(caller).call({ from: caller, ...GAS_ARGS });
 
     expect(balance).to.equal('200');
   });
@@ -53,18 +56,17 @@
     });
     const alice = privateKey('//Alice');
 
-    const owner = createEthAccount(web3);
-    await transferBalanceToEth(api, alice, owner, 999999999999999);
+    const owner = await createEthAccountWithBalance(api, web3);
 
     await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });
 
     const spender = createEthAccount(web3);
 
     const address = collectionIdToAddress(collection);
-    const contract = new web3.eth.Contract(fungibleAbi as any, address);
+    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});
 
     {
-      const result = await contract.methods.approve(spender, 100).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });
+      const result = await contract.methods.approve(spender, 100).send({from: owner});
       const events = normalizeEvents(result.events);
 
       expect(events).to.be.deep.equal([
@@ -104,12 +106,12 @@
     const receiver = createEthAccount(web3);
 
     const address = collectionIdToAddress(collection);
-    const contract = new web3.eth.Contract(fungibleAbi as any, address);
+    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});
 
-    await contract.methods.approve(spender, 100).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });
+    await contract.methods.approve(spender, 100).send();
 
     {
-      const result = await contract.methods.transferFrom(owner, receiver, 49).send({ from: spender, gas: '0x1000000', gasPrice: '0x01' });
+      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});
       const events = normalizeEvents(result.events);
       expect(events).to.be.deep.equal([
         {
@@ -160,10 +162,10 @@
     await transferBalanceToEth(api, alice, receiver, 999999999999999);
 
     const address = collectionIdToAddress(collection);
-    const contract = new web3.eth.Contract(fungibleAbi as any, address);
+    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});
 
     {
-      const result = await contract.methods.transfer(receiver, 50).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });
+      const result = await contract.methods.transfer(receiver, 50).send({ from: owner});
       const events = normalizeEvents(result.events);
       expect(events).to.be.deep.equal([
         {
modifiedtests/src/eth/nonFungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/nonFungible.test.ts
+++ b/tests/src/eth/nonFungible.test.ts
@@ -61,7 +61,7 @@
   });
 });
 
-describe.only('Plain calls', () => {
+describe('Plain calls', () => {
   itWeb3('Can perform approve()', async ({ web3, api }) => {
     const collection = await createCollectionExpectSuccess({
       mode: { type: 'NFT' },
modifiedtests/src/eth/util/helpers.tsdiffbeforeafterboth
before · tests/src/eth/util/helpers.ts
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import { ApiPromise } from '@polkadot/api';7import { addressToEvm, evmToAddress } from '@polkadot/util-crypto';8import Web3 from 'web3';9import usingApi, { submitTransactionAsync } from '../../substrate/substrate-api';10import { IKeyringPair } from '@polkadot/types/types';11import { expect } from 'chai';12import { getGenericResult } from '../../util/helpers';1314let web3Connected = false;15export async function usingWeb3<T>(cb: (web3: Web3) => Promise<T> | T): Promise<T> {16  if (web3Connected) throw new Error('do not nest usingWeb3 calls');17  web3Connected = true;1819  const provider = new Web3.providers.WebsocketProvider('http://localhost:9944');20  const web3 = new Web3(provider);2122  try {23    return await cb(web3);24  } finally {25    // provider.disconnect(3000, 'normal disconnect');26    provider.connection.close();27    web3Connected = false;28  }29}3031export function collectionIdToAddress(address: number): string {32  if (address >= 0xffffffff || address < 0) throw new Error('id overflow');33  const buf = Buffer.from([0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,34    address >> 24,35    (address >> 16) & 0xff,36    (address >> 8) & 0xff,37    address & 0xff,38  ]);39  return Web3.utils.toChecksumAddress('0x' + buf.toString('hex'));40}4142export function createEthAccount(web3: Web3) {43  const account = web3.eth.accounts.create();44  web3.eth.accounts.wallet.add(account.privateKey);45  return account.address;46}4748export async function transferBalanceToEth(api: ApiPromise, source: IKeyringPair, target: string, amount: number) {49  const tx = api.tx.balances.transfer(evmToAddress(target), amount);50  const events = await submitTransactionAsync(source, tx);51  const result = getGenericResult(events);52  expect(result.success).to.be.true;53}5455export async function itWeb3(name: string, cb: (apis: { web3: Web3, api: ApiPromise }) => any, opts: { only?: boolean, skip?: boolean } = {}) {56  let i: any = it;57  if (opts.only) i = i.only;58  else if (opts.skip) i = i.skip;59  i(name, async () => {60    await usingApi(async api => {61      await usingWeb3(async web3 => {62        await cb({ api, web3 });63      });64    });65  });66}67itWeb3.only = (name: string, cb: (apis: { web3: Web3, api: ApiPromise }) => any) => itWeb3(name, cb, { only: true });68itWeb3.skip = (name: string, cb: (apis: { web3: Web3, api: ApiPromise }) => any) => itWeb3(name, cb, { skip: true });6970export async function generateSubstrateEthPair(web3: Web3) {71  const account = web3.eth.accounts.create();72  evmToAddress(account.address);73}7475type NormalizedEvent = {76    address: string,77    event: string,78    args: { [key: string]: string }79};8081export function normalizeEvents(events: any): NormalizedEvent[] {82  const output = [];83  for (const key of Object.keys(events)) {84    if (key.match(/^[0-9]+$/)) {85      output.push(events[key]);86    } else if (Array.isArray(events[key])) {87      output.push(...events[key]);88    } else {89      output.push(events[key]);90    }91  }92  output.sort((a, b) => a.logIndex - b.logIndex);93  return output.map(({ address, event, returnValues }) => {94    const args: { [key: string]: string } = {};95    for (const key of Object.keys(returnValues)) {96      if (!key.match(/^[0-9]+$/)) {97        args[key] = returnValues[key];98      }99    }100    return {101      address,102      event,103      args,104    };105  });106}107108export async function recordEvents(contract: any, action: () => Promise<void>): Promise<NormalizedEvent[]> {109  const out: any = [];110  contract.events.allEvents((_: any, event: any) => {111    out.push(event);112  });113  await action();114  return normalizeEvents(out);115}116117export function subToEth(eth: string): string {118  const bytes = addressToEvm(eth);119  const string = '0x' + Buffer.from(bytes).toString('hex');120  return Web3.utils.toChecksumAddress(string);121}
after · tests/src/eth/util/helpers.ts
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import { ApiPromise } from '@polkadot/api';7import { addressToEvm, evmToAddress } from '@polkadot/util-crypto';8import Web3 from 'web3';9import usingApi, { submitTransactionAsync } from '../../substrate/substrate-api';10import { IKeyringPair } from '@polkadot/types/types';11import { expect } from 'chai';12import { getGenericResult } from '../../util/helpers';1314let web3Connected = false;15export async function usingWeb3<T>(cb: (web3: Web3) => Promise<T> | T): Promise<T> {16  if (web3Connected) throw new Error('do not nest usingWeb3 calls');17  web3Connected = true;1819  const provider = new Web3.providers.WebsocketProvider(config.substrateUrl);20  const web3 = new Web3(provider);2122  try {23    return await cb(web3);24  } finally {25    // provider.disconnect(3000, 'normal disconnect');26    provider.connection.close();27    web3Connected = false;28  }29}3031export function collectionIdToAddress(address: number): string {32  if (address >= 0xffffffff || address < 0) throw new Error('id overflow');33  const buf = Buffer.from([0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,34    address >> 24,35    (address >> 16) & 0xff,36    (address >> 8) & 0xff,37    address & 0xff,38  ]);39  return Web3.utils.toChecksumAddress('0x' + buf.toString('hex'));40}4142export function createEthAccount(web3: Web3) {43  const account = web3.eth.accounts.create();44  web3.eth.accounts.wallet.add(account.privateKey);45  return account.address;46}4748export async function createEthAccountWithBalance(api: ApiPromise, web3: Web3) {49  const alice = privateKey('//Alice');50  const account = createEthAccount(web3);51  await transferBalanceToEth(api, alice, account);5253  return account;54}5556export async function transferBalanceToEth(api: ApiPromise, source: IKeyringPair, target: string, amount = 999999999999999) {57  const tx = api.tx.balances.transfer(evmToAddress(target), amount);58  const events = await submitTransactionAsync(source, tx);59  const result = getGenericResult(events);60  expect(result.success).to.be.true;61}6263export async function itWeb3(name: string, cb: (apis: { web3: Web3, api: ApiPromise }) => any, opts: { only?: boolean, skip?: boolean } = {}) {64  let i: any = it;65  if (opts.only) i = i.only;66  else if (opts.skip) i = i.skip;67  i(name, async () => {68    await usingApi(async api => {69      await usingWeb3(async web3 => {70        await cb({ api, web3 });71      });72    });73  });74}75itWeb3.only = (name: string, cb: (apis: { web3: Web3, api: ApiPromise }) => any) => itWeb3(name, cb, { only: true });76itWeb3.skip = (name: string, cb: (apis: { web3: Web3, api: ApiPromise }) => any) => itWeb3(name, cb, { skip: true });7778export async function generateSubstrateEthPair(web3: Web3) {79  const account = web3.eth.accounts.create();80  evmToAddress(account.address);81}8283type NormalizedEvent = {84    address: string,85    event: string,86    args: { [key: string]: string }87};8889export function normalizeEvents(events: any): NormalizedEvent[] {90  const output = [];91  for (const key of Object.keys(events)) {92    if (key.match(/^[0-9]+$/)) {93      output.push(events[key]);94    } else if (Array.isArray(events[key])) {95      output.push(...events[key]);96    } else {97      output.push(events[key]);98    }99  }100  output.sort((a, b) => a.logIndex - b.logIndex);101  return output.map(({ address, event, returnValues }) => {102    const args: { [key: string]: string } = {};103    for (const key of Object.keys(returnValues)) {104      if (!key.match(/^[0-9]+$/)) {105        args[key] = returnValues[key];106      }107    }108    return {109      address,110      event,111      args,112    };113  });114}115116export async function recordEvents(contract: any, action: () => Promise<void>): Promise<NormalizedEvent[]> {117  const out: any = [];118  contract.events.allEvents((_: any, event: any) => {119    out.push(event);120  });121  await action();122  return normalizeEvents(out);123}124125export function subToEth(eth: string): string {126  const bytes = addressToEvm(eth);127  const string = '0x' + Buffer.from(bytes).toString('hex');128  return Web3.utils.toChecksumAddress(string);129}