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
before · tests/src/eth/fungible.test.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 privateKey from '../substrate/privateKey';7import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from '../util/helpers';8import { collectionIdToAddress, createEthAccount, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';9import fungibleAbi from './fungibleAbi.json';10import { expect } from 'chai';1112describe('Information getting', () => {13  itWeb3('totalSupply', async ({ web3 }) => {14    const collection = await createCollectionExpectSuccess({15      name: 'token name',16      mode: { type: 'Fungible', decimalPoints: 0 },17    });18    const alice = privateKey('//Alice');1920    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { substrate: alice.address });2122    const address = collectionIdToAddress(collection);23    const contract = new web3.eth.Contract(fungibleAbi as any, address);24    const totalSupply = await contract.methods.totalSupply().call();2526    // FIXME: always equals to 0, because this method is not implemented27    expect(totalSupply).to.equal('0');28  });2930  itWeb3('balanceOf', async ({ web3 }) => {31    const collection = await createCollectionExpectSuccess({32      name: 'token name',33      mode: { type: 'Fungible', decimalPoints: 0 },34    });35    const alice = privateKey('//Alice');3637    const caller = createEthAccount(web3);38    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: caller });3940    const address = collectionIdToAddress(collection);41    const contract = new web3.eth.Contract(fungibleAbi as any, address);42    const balance = await contract.methods.balanceOf(caller).call();4344    expect(balance).to.equal('200');45  });46});4748describe('Plain calls', () => {49  itWeb3('Can perform approve()', async ({ web3, api }) => {50    const collection = await createCollectionExpectSuccess({51      name: 'token name',52      mode: { type: 'Fungible', decimalPoints: 0 },53    });54    const alice = privateKey('//Alice');5556    const owner = createEthAccount(web3);57    await transferBalanceToEth(api, alice, owner, 999999999999999);5859    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });6061    const spender = createEthAccount(web3);6263    const address = collectionIdToAddress(collection);64    const contract = new web3.eth.Contract(fungibleAbi as any, address);6566    {67      const result = await contract.methods.approve(spender, 100).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });68      const events = normalizeEvents(result.events);6970      expect(events).to.be.deep.equal([71        {72          address,73          event: 'Approval',74          args: {75            owner,76            spender,77            value: '100',78          },79        },80      ]);81    }8283    {84      const allowance = await contract.methods.allowance(owner, spender).call();85      expect(+allowance).to.equal(100);86    }87  });8889  itWeb3('Can perform transferFrom()', async ({ web3, api }) => {90    const collection = await createCollectionExpectSuccess({91      name: 'token name',92      mode: { type: 'Fungible', decimalPoints: 0 },93    });94    const alice = privateKey('//Alice');9596    const owner = createEthAccount(web3);97    await transferBalanceToEth(api, alice, owner, 999999999999999);9899    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });100101    const spender = createEthAccount(web3);102    await transferBalanceToEth(api, alice, spender, 999999999999999);103104    const receiver = createEthAccount(web3);105106    const address = collectionIdToAddress(collection);107    const contract = new web3.eth.Contract(fungibleAbi as any, address);108109    await contract.methods.approve(spender, 100).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });110111    {112      const result = await contract.methods.transferFrom(owner, receiver, 49).send({ from: spender, gas: '0x1000000', gasPrice: '0x01' });113      const events = normalizeEvents(result.events);114      expect(events).to.be.deep.equal([115        {116          address,117          event: 'Transfer',118          args: {119            from: owner,120            to: receiver,121            value: '49',122          },123        },124        {125          address,126          event: 'Approval',127          args: {128            owner,129            spender,130            value: '51',131          },132        },133      ]);134    }135136    {137      const balance = await contract.methods.balanceOf(receiver).call();138      expect(+balance).to.equal(49);139    }140141    {142      const balance = await contract.methods.balanceOf(owner).call();143      expect(+balance).to.equal(151);144    }145  });146147  itWeb3('Can perform transfer()', async ({ web3, api }) => {148    const collection = await createCollectionExpectSuccess({149      name: 'token name',150      mode: { type: 'Fungible', decimalPoints: 0 },151    });152    const alice = privateKey('//Alice');153154    const owner = createEthAccount(web3);155    await transferBalanceToEth(api, alice, owner, 999999999999999);156157    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });158159    const receiver = createEthAccount(web3);160    await transferBalanceToEth(api, alice, receiver, 999999999999999);161162    const address = collectionIdToAddress(collection);163    const contract = new web3.eth.Contract(fungibleAbi as any, address);164165    {166      const result = await contract.methods.transfer(receiver, 50).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });167      const events = normalizeEvents(result.events);168      expect(events).to.be.deep.equal([169        {170          address,171          event: 'Transfer',172          args: {173            from: owner,174            to: receiver,175            value: '50',176          },177        },178      ]);179    }180181    {182      const balance = await contract.methods.balanceOf(owner).call();183      expect(+balance).to.equal(150);184    }185186    {187      const balance = await contract.methods.balanceOf(receiver).call();188      expect(+balance).to.equal(50);189    }190  });191});192193describe('Substrate calls', () => {194  itWeb3('Events emitted for approve()', async ({ web3 }) => {195    const collection = await createCollectionExpectSuccess({196      mode: { type: 'Fungible', decimalPoints: 0 },197    });198    const alice = privateKey('//Alice');199200    const receiver = createEthAccount(web3);201202    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });203204    const address = collectionIdToAddress(collection);205    const contract = new web3.eth.Contract(fungibleAbi as any, address);206207    const events = await recordEvents(contract, async () => {208      await approveExpectSuccess(collection, 1, alice, { ethereum: receiver }, 100);209    });210211    expect(events).to.be.deep.equal([212      {213        address,214        event: 'Approval',215        args: {216          owner: subToEth(alice.address),217          spender: receiver,218          value: '100',219        },220      },221    ]);222  });223224  itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {225    const collection = await createCollectionExpectSuccess({226      mode: { type: 'Fungible', decimalPoints: 0 },227    });228    const alice = privateKey('//Alice');229    const bob = privateKey('//Bob');230231    const receiver = createEthAccount(web3);232233    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });234    await approveExpectSuccess(collection, 1, alice, bob, 100);235236    const address = collectionIdToAddress(collection);237    const contract = new web3.eth.Contract(fungibleAbi as any, address);238239    const events = await recordEvents(contract, async () => {240      await transferFromExpectSuccess(collection, 1, bob, alice, { ethereum: receiver }, 51, 'Fungible');241    });242243    expect(events).to.be.deep.equal([244      {245        address,246        event: 'Transfer',247        args: {248          from: subToEth(alice.address),249          to: receiver,250          value: '51',251        },252      },253      {254        address,255        event: 'Approval',256        args: {257          owner: subToEth(alice.address),258          spender: subToEth(bob.address),259          value: '49',260        },261      },262    ]);263  });264265  itWeb3('Events emitted for transfer()', async ({ web3 }) => {266    const collection = await createCollectionExpectSuccess({267      mode: { type: 'Fungible', decimalPoints: 0 },268    });269    const alice = privateKey('//Alice');270271    const receiver = createEthAccount(web3);272273    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });274275    const address = collectionIdToAddress(collection);276    const contract = new web3.eth.Contract(fungibleAbi as any, address);277278    const events = await recordEvents(contract, async () => {279      await transferExpectSuccess(collection, 1, alice, { ethereum: receiver }, 51, 'Fungible');280    });281282    expect(events).to.be.deep.equal([283      {284        address,285        event: 'Transfer',286        args: {287          from: subToEth(alice.address),288          to: receiver,289          value: '51',290        },291      },292    ]);293  });294});
after · tests/src/eth/fungible.test.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 privateKey from '../substrate/privateKey';7import { approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess } from '../util/helpers';8import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';9import fungibleAbi from './fungibleAbi.json';10import { expect } from 'chai';1112describe('Information getting', () => {13  itWeb3('totalSupply', async ({ api, web3 }) => {14    const collection = await createCollectionExpectSuccess({15      name: 'token name',16      mode: { type: 'Fungible', decimalPoints: 0 },17    });18    const alice = privateKey('//Alice');1920    const caller = await createEthAccountWithBalance(api, web3);21  22    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { substrate: alice.address });2324    const address = collectionIdToAddress(collection);25    const contract = new web3.eth.Contract(fungibleAbi as any, address);26    const totalSupply = await contract.methods.totalSupply().call({ from: caller, ...GAS_ARGS });2728    // FIXME: always equals to 0, because this method is not implemented29    expect(totalSupply).to.equal('0');30  });3132  itWeb3('balanceOf', async ({ api, web3 }) => {33    const collection = await createCollectionExpectSuccess({34      name: 'token name',35      mode: { type: 'Fungible', decimalPoints: 0 },36    });37    const alice = privateKey('//Alice');3839    const caller = await createEthAccountWithBalance(api, web3);4041    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: caller });4243    const address = collectionIdToAddress(collection);44    const contract = new web3.eth.Contract(fungibleAbi as any, address);45    const balance = await contract.methods.balanceOf(caller).call({ from: caller, ...GAS_ARGS });4647    expect(balance).to.equal('200');48  });49});5051describe('Plain calls', () => {52  itWeb3('Can perform approve()', async ({ web3, api }) => {53    const collection = await createCollectionExpectSuccess({54      name: 'token name',55      mode: { type: 'Fungible', decimalPoints: 0 },56    });57    const alice = privateKey('//Alice');5859    const owner = await createEthAccountWithBalance(api, web3);6061    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });6263    const spender = createEthAccount(web3);6465    const address = collectionIdToAddress(collection);66    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});6768    {69      const result = await contract.methods.approve(spender, 100).send({from: owner});70      const events = normalizeEvents(result.events);7172      expect(events).to.be.deep.equal([73        {74          address,75          event: 'Approval',76          args: {77            owner,78            spender,79            value: '100',80          },81        },82      ]);83    }8485    {86      const allowance = await contract.methods.allowance(owner, spender).call();87      expect(+allowance).to.equal(100);88    }89  });9091  itWeb3('Can perform transferFrom()', async ({ web3, api }) => {92    const collection = await createCollectionExpectSuccess({93      name: 'token name',94      mode: { type: 'Fungible', decimalPoints: 0 },95    });96    const alice = privateKey('//Alice');9798    const owner = createEthAccount(web3);99    await transferBalanceToEth(api, alice, owner, 999999999999999);100101    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });102103    const spender = createEthAccount(web3);104    await transferBalanceToEth(api, alice, spender, 999999999999999);105106    const receiver = createEthAccount(web3);107108    const address = collectionIdToAddress(collection);109    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});110111    await contract.methods.approve(spender, 100).send();112113    {114      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});115      const events = normalizeEvents(result.events);116      expect(events).to.be.deep.equal([117        {118          address,119          event: 'Transfer',120          args: {121            from: owner,122            to: receiver,123            value: '49',124          },125        },126        {127          address,128          event: 'Approval',129          args: {130            owner,131            spender,132            value: '51',133          },134        },135      ]);136    }137138    {139      const balance = await contract.methods.balanceOf(receiver).call();140      expect(+balance).to.equal(49);141    }142143    {144      const balance = await contract.methods.balanceOf(owner).call();145      expect(+balance).to.equal(151);146    }147  });148149  itWeb3('Can perform transfer()', async ({ web3, api }) => {150    const collection = await createCollectionExpectSuccess({151      name: 'token name',152      mode: { type: 'Fungible', decimalPoints: 0 },153    });154    const alice = privateKey('//Alice');155156    const owner = createEthAccount(web3);157    await transferBalanceToEth(api, alice, owner, 999999999999999);158159    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { ethereum: owner });160161    const receiver = createEthAccount(web3);162    await transferBalanceToEth(api, alice, receiver, 999999999999999);163164    const address = collectionIdToAddress(collection);165    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});166167    {168      const result = await contract.methods.transfer(receiver, 50).send({ from: owner});169      const events = normalizeEvents(result.events);170      expect(events).to.be.deep.equal([171        {172          address,173          event: 'Transfer',174          args: {175            from: owner,176            to: receiver,177            value: '50',178          },179        },180      ]);181    }182183    {184      const balance = await contract.methods.balanceOf(owner).call();185      expect(+balance).to.equal(150);186    }187188    {189      const balance = await contract.methods.balanceOf(receiver).call();190      expect(+balance).to.equal(50);191    }192  });193});194195describe('Substrate calls', () => {196  itWeb3('Events emitted for approve()', async ({ web3 }) => {197    const collection = await createCollectionExpectSuccess({198      mode: { type: 'Fungible', decimalPoints: 0 },199    });200    const alice = privateKey('//Alice');201202    const receiver = createEthAccount(web3);203204    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });205206    const address = collectionIdToAddress(collection);207    const contract = new web3.eth.Contract(fungibleAbi as any, address);208209    const events = await recordEvents(contract, async () => {210      await approveExpectSuccess(collection, 1, alice, { ethereum: receiver }, 100);211    });212213    expect(events).to.be.deep.equal([214      {215        address,216        event: 'Approval',217        args: {218          owner: subToEth(alice.address),219          spender: receiver,220          value: '100',221        },222      },223    ]);224  });225226  itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {227    const collection = await createCollectionExpectSuccess({228      mode: { type: 'Fungible', decimalPoints: 0 },229    });230    const alice = privateKey('//Alice');231    const bob = privateKey('//Bob');232233    const receiver = createEthAccount(web3);234235    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });236    await approveExpectSuccess(collection, 1, alice, bob, 100);237238    const address = collectionIdToAddress(collection);239    const contract = new web3.eth.Contract(fungibleAbi as any, address);240241    const events = await recordEvents(contract, async () => {242      await transferFromExpectSuccess(collection, 1, bob, alice, { ethereum: receiver }, 51, 'Fungible');243    });244245    expect(events).to.be.deep.equal([246      {247        address,248        event: 'Transfer',249        args: {250          from: subToEth(alice.address),251          to: receiver,252          value: '51',253        },254      },255      {256        address,257        event: 'Approval',258        args: {259          owner: subToEth(alice.address),260          spender: subToEth(bob.address),261          value: '49',262        },263      },264    ]);265  });266267  itWeb3('Events emitted for transfer()', async ({ web3 }) => {268    const collection = await createCollectionExpectSuccess({269      mode: { type: 'Fungible', decimalPoints: 0 },270    });271    const alice = privateKey('//Alice');272273    const receiver = createEthAccount(web3);274275    await createFungibleItemExpectSuccess(alice, collection, { Value: 200n });276277    const address = collectionIdToAddress(collection);278    const contract = new web3.eth.Contract(fungibleAbi as any, address);279280    const events = await recordEvents(contract, async () => {281      await transferExpectSuccess(collection, 1, alice, { ethereum: receiver }, 51, 'Fungible');282    });283284    expect(events).to.be.deep.equal([285      {286        address,287        event: 'Transfer',288        args: {289          from: subToEth(alice.address),290          to: receiver,291          value: '51',292        },293      },294    ]);295  });296});
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
--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -16,7 +16,7 @@
   if (web3Connected) throw new Error('do not nest usingWeb3 calls');
   web3Connected = true;
 
-  const provider = new Web3.providers.WebsocketProvider('http://localhost:9944');
+  const provider = new Web3.providers.WebsocketProvider(config.substrateUrl);
   const web3 = new Web3(provider);
 
   try {
@@ -45,7 +45,15 @@
   return account.address;
 }
 
-export async function transferBalanceToEth(api: ApiPromise, source: IKeyringPair, target: string, amount: number) {
+export async function createEthAccountWithBalance(api: ApiPromise, web3: Web3) {
+  const alice = privateKey('//Alice');
+  const account = createEthAccount(web3);
+  await transferBalanceToEth(api, alice, account);
+
+  return account;
+}
+
+export async function transferBalanceToEth(api: ApiPromise, source: IKeyringPair, target: string, amount = 999999999999999) {
   const tx = api.tx.balances.transfer(evmToAddress(target), amount);
   const events = await submitTransactionAsync(source, tx);
   const result = getGenericResult(events);