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

difftreelog

source

tests/src/eth/fungible.test.ts11.4 KiBsourcehistory
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, UNIQUE} from '../util/helpers';8import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, transferBalanceToEth} from './util/helpers';9import fungibleAbi from './fungibleAbi.json';10import {expect} from 'chai';1112describe('Fungible: 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);2122    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, {from: caller, ...GAS_ARGS});26    const totalSupply = await contract.methods.totalSupply().call();2728    expect(totalSupply).to.equal('200');29  });3031  itWeb3('balanceOf', async ({api, web3}) => {32    const collection = await createCollectionExpectSuccess({33      name: 'token name',34      mode: {type: 'Fungible', decimalPoints: 0},35    });36    const alice = privateKey('//Alice');3738    const caller = await createEthAccountWithBalance(api, web3);3940    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: caller});4142    const address = collectionIdToAddress(collection);43    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: caller, ...GAS_ARGS});44    const balance = await contract.methods.balanceOf(caller).call();4546    expect(balance).to.equal('200');47  });48});4950describe('Fungible: Plain calls', () => {51  itWeb3('Can perform approve()', async ({web3, api}) => {52    const collection = await createCollectionExpectSuccess({53      name: 'token name',54      mode: {type: 'Fungible', decimalPoints: 0},55    });56    const alice = privateKey('//Alice');5758    const owner = await createEthAccountWithBalance(api, web3);5960    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});6162    const spender = createEthAccount(web3);6364    const address = collectionIdToAddress(collection);65    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});6667    {68      const result = await contract.methods.approve(spender, 100).send({from: owner});69      const events = normalizeEvents(result.events);7071      expect(events).to.be.deep.equal([72        {73          address,74          event: 'Approval',75          args: {76            owner,77            spender,78            value: '100',79          },80        },81      ]);82    }8384    {85      const allowance = await contract.methods.allowance(owner, spender).call();86      expect(+allowance).to.equal(100);87    }88  });8990  itWeb3('Can perform transferFrom()', async ({web3, api}) => {91    const collection = await createCollectionExpectSuccess({92      name: 'token name',93      mode: {type: 'Fungible', decimalPoints: 0},94    });95    const alice = privateKey('//Alice');9697    const owner = createEthAccount(web3);98    await transferBalanceToEth(api, alice, owner, 999999999999999);99100    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});101102    const spender = createEthAccount(web3);103    await transferBalanceToEth(api, alice, spender, 999999999999999);104105    const receiver = createEthAccount(web3);106107    const address = collectionIdToAddress(collection);108    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});109110    await contract.methods.approve(spender, 100).send();111112    {113      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});114      const events = normalizeEvents(result.events);115      expect(events).to.be.deep.equal([116        {117          address,118          event: 'Transfer',119          args: {120            from: owner,121            to: receiver,122            value: '49',123          },124        },125        {126          address,127          event: 'Approval',128          args: {129            owner,130            spender,131            value: '51',132          },133        },134      ]);135    }136137    {138      const balance = await contract.methods.balanceOf(receiver).call();139      expect(+balance).to.equal(49);140    }141142    {143      const balance = await contract.methods.balanceOf(owner).call();144      expect(+balance).to.equal(151);145    }146  });147148  itWeb3('Can perform transfer()', async ({web3, api}) => {149    const collection = await createCollectionExpectSuccess({150      name: 'token name',151      mode: {type: 'Fungible', decimalPoints: 0},152    });153    const alice = privateKey('//Alice');154155    const owner = createEthAccount(web3);156    await transferBalanceToEth(api, alice, owner, 999999999999999);157158    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});159160    const receiver = createEthAccount(web3);161    await transferBalanceToEth(api, alice, receiver, 999999999999999);162163    const address = collectionIdToAddress(collection);164    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});165166    {167      const result = await contract.methods.transfer(receiver, 50).send({from: owner});168      const events = normalizeEvents(result.events);169      expect(events).to.be.deep.equal([170        {171          address,172          event: 'Transfer',173          args: {174            from: owner,175            to: receiver,176            value: '50',177          },178        },179      ]);180    }181182    {183      const balance = await contract.methods.balanceOf(owner).call();184      expect(+balance).to.equal(150);185    }186187    {188      const balance = await contract.methods.balanceOf(receiver).call();189      expect(+balance).to.equal(50);190    }191  });192});193194describe('Fungible: Fees', () => {195  itWeb3('approve() call fee is less than 0.2UNQ', async ({web3, api}) => {196    const collection = await createCollectionExpectSuccess({197      mode: {type: 'Fungible', decimalPoints: 0},198    });199    const alice = privateKey('//Alice');200201    const owner = await createEthAccountWithBalance(api, web3);202    const spender = createEthAccount(web3);203204    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});205206    const address = collectionIdToAddress(collection);207    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});208209    const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, 100).send({from: owner}));210    expect(cost < BigInt(0.2 * Number(UNIQUE)));211  });212213  itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({web3, api}) => {214    const collection = await createCollectionExpectSuccess({215      mode: {type: 'Fungible', decimalPoints: 0},216    });217    const alice = privateKey('//Alice');218219    const owner = await createEthAccountWithBalance(api, web3);220    const spender = await createEthAccountWithBalance(api, web3);221222    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});223224    const address = collectionIdToAddress(collection);225    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});226227    await contract.methods.approve(spender, 100).send({from: owner});228229    const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));230    expect(cost < BigInt(0.2 * Number(UNIQUE)));231  });232233  itWeb3('transfer() call fee is less than 0.2UNQ', async ({web3, api}) => {234    const collection = await createCollectionExpectSuccess({235      mode: {type: 'Fungible', decimalPoints: 0},236    });237    const alice = privateKey('//Alice');238239    const owner = await createEthAccountWithBalance(api, web3);240    const receiver = createEthAccount(web3);241242    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n}, {Ethereum: owner});243244    const address = collectionIdToAddress(collection);245    const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: owner, ...GAS_ARGS});246247    const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));248    expect(cost < BigInt(0.2 * Number(UNIQUE)));249  });250});251252describe('Fungible: Substrate calls', () => {253  itWeb3('Events emitted for approve()', async ({web3}) => {254    const collection = await createCollectionExpectSuccess({255      mode: {type: 'Fungible', decimalPoints: 0},256    });257    const alice = privateKey('//Alice');258259    const receiver = createEthAccount(web3);260261    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n});262263    const address = collectionIdToAddress(collection);264    const contract = new web3.eth.Contract(fungibleAbi as any, address);265266    const events = await recordEvents(contract, async () => {267      await approveExpectSuccess(collection, 0, alice, {Ethereum: receiver}, 100);268    });269270    expect(events).to.be.deep.equal([271      {272        address,273        event: 'Approval',274        args: {275          owner: subToEth(alice.address),276          spender: receiver,277          value: '100',278        },279      },280    ]);281  });282283  itWeb3('Events emitted for transferFrom()', async ({web3}) => {284    const collection = await createCollectionExpectSuccess({285      mode: {type: 'Fungible', decimalPoints: 0},286    });287    const alice = privateKey('//Alice');288    const bob = privateKey('//Bob');289290    const receiver = createEthAccount(web3);291292    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n});293    await approveExpectSuccess(collection, 0, alice, bob.address, 100);294295    const address = collectionIdToAddress(collection);296    const contract = new web3.eth.Contract(fungibleAbi as any, address);297298    const events = await recordEvents(contract, async () => {299      await transferFromExpectSuccess(collection, 0, bob, alice, {Ethereum: receiver}, 51, 'Fungible');300    });301302    expect(events).to.be.deep.equal([303      {304        address,305        event: 'Transfer',306        args: {307          from: subToEth(alice.address),308          to: receiver,309          value: '51',310        },311      },312      {313        address,314        event: 'Approval',315        args: {316          owner: subToEth(alice.address),317          spender: subToEth(bob.address),318          value: '49',319        },320      },321    ]);322  });323324  itWeb3('Events emitted for transfer()', async ({web3}) => {325    const collection = await createCollectionExpectSuccess({326      mode: {type: 'Fungible', decimalPoints: 0},327    });328    const alice = privateKey('//Alice');329330    const receiver = createEthAccount(web3);331332    await createFungibleItemExpectSuccess(alice, collection, {Value: 200n});333334    const address = collectionIdToAddress(collection);335    const contract = new web3.eth.Contract(fungibleAbi as any, address);336337    const events = await recordEvents(contract, async () => {338      await transferExpectSuccess(collection, 0, alice, {Ethereum:receiver}, 51, 'Fungible');339    });340341    expect(events).to.be.deep.equal([342      {343        address,344        event: 'Transfer',345        args: {346          from: subToEth(alice.address),347          to: receiver,348          value: '51',349        },350      },351    ]);352  });353});