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

difftreelog

source

tests/src/eth/nonFungible.test.ts11.7 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, createItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess, UNIQUE } from '../util/helpers';8import { collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, transferBalanceToEth } from './util/helpers';9import nonFungibleAbi from './nonFungibleAbi.json';10import { expect } from 'chai';11import waitNewBlocks from '../substrate/wait-new-blocks';1213describe('NFT: Information getting', () => {14  itWeb3('totalSupply', async ({ api, web3 }) => {15    const collection = await createCollectionExpectSuccess({16      mode: { type: 'NFT' },17    });18    const alice = privateKey('//Alice');19    const caller = await createEthAccountWithBalance(api, web3);2021    await createItemExpectSuccess(alice, collection, 'NFT', { substrate: alice.address });2223    const address = collectionIdToAddress(collection);24    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});25    const totalSupply = await contract.methods.totalSupply().call();2627    // FIXME: always equals to 0, because this method is not implemented28    expect(totalSupply).to.equal('0');29  });3031  itWeb3('balanceOf', async ({ api, web3 }) => {32    const collection = await createCollectionExpectSuccess({33      mode: { type: 'NFT' },34    });35    const alice = privateKey('//Alice');3637    const caller = await createEthAccountWithBalance(api, web3);38    await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });39    await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });40    await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });4142    const address = collectionIdToAddress(collection);43    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});44    const balance = await contract.methods.balanceOf(caller).call();4546    expect(balance).to.equal('3');47  });4849  itWeb3('ownerOf', async ({ api, web3 }) => {50    const collection = await createCollectionExpectSuccess({51      mode: { type: 'NFT' },52    });53    const alice = privateKey('//Alice');5455    const caller = await createEthAccountWithBalance(api, web3);56    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });5758    const address = collectionIdToAddress(collection);59    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});60    const owner = await contract.methods.ownerOf(tokenId).call();6162    expect(owner).to.equal(caller);63  });64});6566describe('NFT: Plain calls', () => {67  itWeb3('Can perform approve()', async ({ web3, api }) => {68    const collection = await createCollectionExpectSuccess({69      mode: { type: 'NFT' },70    });71    const alice = privateKey('//Alice');7273    const owner = createEthAccount(web3);74    await transferBalanceToEth(api, alice, owner, 999999999999999);7576    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });7778    const spender = createEthAccount(web3);7980    const address = collectionIdToAddress(collection);81    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);8283    {84      const result = await contract.methods.approve(spender, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });85      const events = normalizeEvents(result.events);8687      expect(events).to.be.deep.equal([88        {89          address,90          event: 'Approval',91          args: {92            owner,93            approved: spender,94            tokenId: tokenId.toString(),95          },96        },97      ]);98    }99  });100101  itWeb3('Can perform transferFrom()', async ({ web3, api }) => {102    const collection = await createCollectionExpectSuccess({103      mode: { type: 'NFT' },104    });105    const alice = privateKey('//Alice');106107    const owner = createEthAccount(web3);108    await transferBalanceToEth(api, alice, owner, 999999999999999);109110    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });111112    const spender = createEthAccount(web3);113    await transferBalanceToEth(api, alice, spender, 999999999999999);114115    const receiver = createEthAccount(web3);116117    const address = collectionIdToAddress(collection);118    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});119120    await contract.methods.approve(spender, tokenId).send({ from: owner });121122    {123      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({ from: spender });124      const events = normalizeEvents(result.events);125      expect(events).to.be.deep.equal([126        {127          address,128          event: 'Transfer',129          args: {130            from: owner,131            to: receiver,132            tokenId: tokenId.toString(),133          },134        },135      ]);136    }137138    {139      const balance = await contract.methods.balanceOf(receiver).call();140      expect(+balance).to.equal(1);141    }142143    {144      const balance = await contract.methods.balanceOf(owner).call();145      expect(+balance).to.equal(0);146    }147  });148149  itWeb3('Can perform transfer()', async ({ web3, api }) => {150    const collection = await createCollectionExpectSuccess({151      mode: { type: 'NFT' },152    });153    const alice = privateKey('//Alice');154155    const owner = createEthAccount(web3);156    await transferBalanceToEth(api, alice, owner, 999999999999999);157158    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { 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(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});165166    {167      const result = await contract.methods.transfer(receiver, tokenId).send({ from: owner });168      await waitNewBlocks(api, 1);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            tokenId: tokenId.toString(),178          },179        },180      ]);181    }182183    {184      const balance = await contract.methods.balanceOf(owner).call();185      expect(+balance).to.equal(0);186    }187188    {189      const balance = await contract.methods.balanceOf(receiver).call();190      expect(+balance).to.equal(1);191    }192  });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});252253describe('NFT: Substrate calls', () => {254  itWeb3('Events emitted for approve()', async ({ web3 }) => {255    const collection = await createCollectionExpectSuccess({256      mode: { type: 'NFT' },257    });258    const alice = privateKey('//Alice');259260    const receiver = createEthAccount(web3);261262    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');263264    const address = collectionIdToAddress(collection);265    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);266267    const events = await recordEvents(contract, async () => {268      await approveExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1);269    });270271    expect(events).to.be.deep.equal([272      {273        address,274        event: 'Approval',275        args: {276          owner: subToEth(alice.address),277          approved: receiver,278          tokenId: tokenId.toString(),279        },280      },281    ]);282  });283284  itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {285    const collection = await createCollectionExpectSuccess({286      mode: { type: 'NFT' },287    });288    const alice = privateKey('//Alice');289    const bob = privateKey('//Bob');290291    const receiver = createEthAccount(web3);292293    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');294    await approveExpectSuccess(collection, tokenId, alice, bob, 1);295296    const address = collectionIdToAddress(collection);297    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);298299    const events = await recordEvents(contract, async () => {300      await transferFromExpectSuccess(collection, tokenId, bob, alice, { ethereum: receiver }, 1, 'NFT');301    });302303    expect(events).to.be.deep.equal([304      {305        address,306        event: 'Transfer',307        args: {308          from: subToEth(alice.address),309          to: receiver,310          tokenId: tokenId.toString(),311        },312      },313    ]);314  });315316  itWeb3('Events emitted for transfer()', async ({ web3 }) => {317    const collection = await createCollectionExpectSuccess({318      mode: { type: 'NFT' },319    });320    const alice = privateKey('//Alice');321322    const receiver = createEthAccount(web3);323324    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');325326    const address = collectionIdToAddress(collection);327    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);328329    const events = await recordEvents(contract, async () => {330      await transferExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1, 'NFT');331    });332333    expect(events).to.be.deep.equal([334      {335        address,336        event: 'Transfer',337        args: {338          from: subToEth(alice.address),339          to: receiver,340          tokenId: tokenId.toString(),341        },342      },343    ]);344  });345});