git.delta.rocks / unique-network / refs/commits / 549eb4c11dfd

difftreelog

source

tests/src/eth/nonFungible.test.ts19.3 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, burnItemExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess, setVariableMetaDataExpectSuccess, transferExpectSuccess, transferFromExpectSuccess, UNIQUE, setMetadataUpdatePermissionFlagExpectSuccess } 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';12import { submitTransactionAsync } from '../substrate/substrate-api';1314describe('NFT: Information getting', () => {15  itWeb3('totalSupply', async ({ api, web3 }) => {16    const collection = await createCollectionExpectSuccess({17      mode: { type: 'NFT' },18    });19    const alice = privateKey('//Alice');20    const caller = await createEthAccountWithBalance(api, web3);2122    await createItemExpectSuccess(alice, collection, 'NFT', { substrate: alice.address });2324    const address = collectionIdToAddress(collection);25    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});26    const totalSupply = await contract.methods.totalSupply().call();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      mode: { type: 'NFT' },35    });36    const alice = privateKey('//Alice');3738    const caller = await createEthAccountWithBalance(api, web3);39    await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });40    await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });41    await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });4243    const address = collectionIdToAddress(collection);44    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});45    const balance = await contract.methods.balanceOf(caller).call();4647    expect(balance).to.equal('3');48  });4950  itWeb3('ownerOf', async ({ api, web3 }) => {51    const collection = await createCollectionExpectSuccess({52      mode: { type: 'NFT' },53    });54    const alice = privateKey('//Alice');5556    const caller = await createEthAccountWithBalance(api, web3);57    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: caller });5859    const address = collectionIdToAddress(collection);60    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});61    const owner = await contract.methods.ownerOf(tokenId).call();6263    expect(owner).to.equal(caller);64  });65});6667describe('NFT: Plain calls', () => {68  itWeb3('Can perform mint()', async ({ web3, api }) => {69    const collection = await createCollectionExpectSuccess({70      mode: { type: 'NFT' },71    });72    const alice = privateKey('//Alice');7374    const caller = await createEthAccountWithBalance(api, web3);75    const changeAdminTx = api.tx.nft.addCollectionAdmin(collection, { ethereum: caller });76    await submitTransactionAsync(alice, changeAdminTx);77    const receiver = createEthAccount(web3);7879    const address = collectionIdToAddress(collection);80    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});8182    {83      const nextTokenId = await contract.methods.nextTokenId().call();84      expect(nextTokenId).to.be.equal('1');85      const result = await contract.methods.mintWithTokenURI(86        receiver,87        nextTokenId,88        'Test URI',89      ).send({from: caller});90      const events = normalizeEvents(result.events);9192      expect(events).to.be.deep.equal([93        {94          address,95          event: 'Transfer',96          args: {97            from: '0x0000000000000000000000000000000000000000',98            to: receiver,99            tokenId: nextTokenId,100          },101        },102      ]);103104      await waitNewBlocks(api, 1);105      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');106    }107  });108  itWeb3('Can perform mintBulk()', async ({ web3, api }) => {109    const collection = await createCollectionExpectSuccess({110      mode: { type: 'NFT' },111    });112    const alice = privateKey('//Alice');113114    const caller = await createEthAccountWithBalance(api, web3);115    const changeAdminTx = api.tx.nft.addCollectionAdmin(collection, { ethereum: caller });116    await submitTransactionAsync(alice, changeAdminTx);117    const receiver = createEthAccount(web3);118119    const address = collectionIdToAddress(collection);120    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});121122    {123      const nextTokenId = await contract.methods.nextTokenId().call();124      expect(nextTokenId).to.be.equal('1');125      const result = await contract.methods.mintBulkWithTokenURI(126        receiver,127        [128          [nextTokenId, 'Test URI 0'],129          [+nextTokenId + 1, 'Test URI 1'],130          [+nextTokenId + 2, 'Test URI 2'],131        ],132      ).send({ from: caller });133      const events = normalizeEvents(result.events);134135      expect(events).to.be.deep.equal([136        {137          address,138          event: 'Transfer',139          args: {140            from: '0x0000000000000000000000000000000000000000',141            to: receiver,142            tokenId: nextTokenId,143          },144        },145        {146          address,147          event: 'Transfer',148          args: {149            from: '0x0000000000000000000000000000000000000000',150            to: receiver,151            tokenId: String(+nextTokenId + 1),152          },153        },154        {155          address,156          event: 'Transfer',157          args: {158            from: '0x0000000000000000000000000000000000000000',159            to: receiver,160            tokenId: String(+nextTokenId + 2),161          },162        },163      ]);164165      await waitNewBlocks(api, 1);166      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');167      expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');168      expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');169    }170  });171172  itWeb3('Can perform burn()', async ({ web3, api }) => {173    const collection = await createCollectionExpectSuccess({174      mode: {type: 'NFT'},175    });176    const alice = privateKey('//Alice');177178    const owner = await createEthAccountWithBalance(api, web3);179180    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });181    182    const address = collectionIdToAddress(collection);183    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});184185    {186      const result = await contract.methods.burn(tokenId).send({ from: owner });187      const events = normalizeEvents(result.events);188      189      expect(events).to.be.deep.equal([190        {191          address,192          event: 'Transfer',193          args: {194            from: owner,195            to: '0x0000000000000000000000000000000000000000',196            tokenId: tokenId.toString(),197          },198        },199      ]);200    }201  });202203  itWeb3('Can perform approve()', async ({ web3, api }) => {204    const collection = await createCollectionExpectSuccess({205      mode: { type: 'NFT' },206    });207    const alice = privateKey('//Alice');208209    const owner = createEthAccount(web3);210    await transferBalanceToEth(api, alice, owner, 999999999999999);211212    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });213214    const spender = createEthAccount(web3);215216    const address = collectionIdToAddress(collection);217    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);218219    {220      const result = await contract.methods.approve(spender, tokenId).send({ from: owner, gas: '0x1000000', gasPrice: '0x01' });221      const events = normalizeEvents(result.events);222223      expect(events).to.be.deep.equal([224        {225          address,226          event: 'Approval',227          args: {228            owner,229            approved: spender,230            tokenId: tokenId.toString(),231          },232        },233      ]);234    }235  });236237  itWeb3('Can perform transferFrom()', async ({ web3, api }) => {238    const collection = await createCollectionExpectSuccess({239      mode: { type: 'NFT' },240    });241    const alice = privateKey('//Alice');242243    const owner = createEthAccount(web3);244    await transferBalanceToEth(api, alice, owner, 999999999999999);245246    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });247248    const spender = createEthAccount(web3);249    await transferBalanceToEth(api, alice, spender, 999999999999999);250251    const receiver = createEthAccount(web3);252253    const address = collectionIdToAddress(collection);254    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});255256    await contract.methods.approve(spender, tokenId).send({ from: owner });257258    {259      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({ from: spender });260      const events = normalizeEvents(result.events);261      expect(events).to.be.deep.equal([262        {263          address,264          event: 'Transfer',265          args: {266            from: owner,267            to: receiver,268            tokenId: tokenId.toString(),269          },270        },271      ]);272    }273274    {275      const balance = await contract.methods.balanceOf(receiver).call();276      expect(+balance).to.equal(1);277    }278279    {280      const balance = await contract.methods.balanceOf(owner).call();281      expect(+balance).to.equal(0);282    }283  });284285  itWeb3('Can perform transfer()', async ({ web3, api }) => {286    const collection = await createCollectionExpectSuccess({287      mode: { type: 'NFT' },288    });289    const alice = privateKey('//Alice');290291    const owner = createEthAccount(web3);292    await transferBalanceToEth(api, alice, owner, 999999999999999);293294    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });295296    const receiver = createEthAccount(web3);297    await transferBalanceToEth(api, alice, receiver, 999999999999999);298299    const address = collectionIdToAddress(collection);300    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: owner, ...GAS_ARGS});301302    {303      const result = await contract.methods.transfer(receiver, tokenId).send({ from: owner });304      await waitNewBlocks(api, 1);305      const events = normalizeEvents(result.events);306      expect(events).to.be.deep.equal([307        {308          address,309          event: 'Transfer',310          args: {311            from: owner,312            to: receiver,313            tokenId: tokenId.toString(),314          },315        },316      ]);317    }318319    {320      const balance = await contract.methods.balanceOf(owner).call();321      expect(+balance).to.equal(0);322    }323324    {325      const balance = await contract.methods.balanceOf(receiver).call();326      expect(+balance).to.equal(1);327    }328  });329330  itWeb3('Can perform getVariableMetadata', async ({ web3, api }) => {331    const collection = await createCollectionExpectSuccess({332      mode: { type: 'NFT' },333    });334    const alice = privateKey('//Alice');335336    const owner = await createEthAccountWithBalance(api, web3);337338    const item = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });339    await setMetadataUpdatePermissionFlagExpectSuccess(alice, collection, 'Admin');340    await setVariableMetaDataExpectSuccess(alice, collection, item, [1, 2, 3]);341342    const address = collectionIdToAddress(collection);343    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });344    345    expect(await contract.methods.getVariableMetadata(item).call()).to.be.equal('0x010203');346  });347348  itWeb3('Can perform setVariableMetadata', async ({ web3, api }) => {349    const collection = await createCollectionExpectSuccess({350      mode: { type: 'NFT' },351    });352    const alice = privateKey('//Alice');353354    const owner = await createEthAccountWithBalance(api, web3);355356    const item = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });357358    const address = collectionIdToAddress(collection);359    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });360    361    expect(await contract.methods.setVariableMetadata(item, '0x010203').send({ from: owner }));362    await waitNewBlocks(api, 1);363    expect(await contract.methods.getVariableMetadata(item).call()).to.be.equal('0x010203');364  });365});366367describe('NFT: Fees', () => {368  itWeb3('approve() call fee is less than 0.2UNQ', async ({ web3, api }) => {369    const collection = await createCollectionExpectSuccess({370      mode: { type: 'NFT' },371    });372    const alice = privateKey('//Alice');373374    const owner = await createEthAccountWithBalance(api, web3);375    const spender = createEthAccount(web3);376377    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });378379    const address = collectionIdToAddress(collection);380    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });381382    const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, tokenId).send({ from: owner }));383    expect(cost < BigInt(0.2 * Number(UNIQUE)));384  });385386  itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({ web3, api }) => {387    const collection = await createCollectionExpectSuccess({388      mode: { type: 'NFT' },389    });390    const alice = privateKey('//Alice');391  392    const owner = await createEthAccountWithBalance(api, web3);393    const spender = await createEthAccountWithBalance(api, web3);394395    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });396397    const address = collectionIdToAddress(collection);398    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });399400    await contract.methods.approve(spender, tokenId).send({ from: owner });401402    const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({ from: spender }));403    expect(cost < BigInt(0.2 * Number(UNIQUE)));404  });405406  itWeb3('transfer() call fee is less than 0.2UNQ', async ({ web3, api }) => {407    const collection = await createCollectionExpectSuccess({408      mode: { type: 'NFT' },409    });410    const alice = privateKey('//Alice');411412    const owner = await createEthAccountWithBalance(api, web3);413    const receiver = createEthAccount(web3);414415    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { ethereum: owner });416417    const address = collectionIdToAddress(collection);418    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, { from: owner, ...GAS_ARGS });419420    const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, tokenId).send({ from: owner }));421    expect(cost < BigInt(0.2 * Number(UNIQUE)));422  });423});424425describe('NFT: Substrate calls', () => {426  itWeb3('Events emitted for mint()', async ({ web3 }) => {427    const collection = await createCollectionExpectSuccess({428      mode: { type: 'NFT' },429    });430    const alice = privateKey('//Alice');431432    const address = collectionIdToAddress(collection);433    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);434435    let tokenId: number;436    const events = await recordEvents(contract, async () => {437      tokenId = await createItemExpectSuccess(alice, collection, 'NFT');438    });439440    expect(events).to.be.deep.equal([441      {442        address,443        event: 'Transfer',444        args: {445          from: '0x0000000000000000000000000000000000000000',446          to: subToEth(alice.address),447          tokenId: tokenId!.toString(),448        },449      },450    ]);451  });452453  itWeb3('Events emitted for burn()', async ({ web3 }) => {454    const collection = await createCollectionExpectSuccess({455      mode: { type: 'NFT' },456    });457    const alice = privateKey('//Alice');458459    const address = collectionIdToAddress(collection);460    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);461462    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');463    const events = await recordEvents(contract, async () => {464      await burnItemExpectSuccess(alice, collection, tokenId);465    });466467    expect(events).to.be.deep.equal([468      {469        address,470        event: 'Transfer',471        args: {472          from: subToEth(alice.address),473          to: '0x0000000000000000000000000000000000000000',474          tokenId: tokenId.toString(),475        },476      },477    ]);478  });479480  itWeb3('Events emitted for approve()', async ({ web3 }) => {481    const collection = await createCollectionExpectSuccess({482      mode: { type: 'NFT' },483    });484    const alice = privateKey('//Alice');485486    const receiver = createEthAccount(web3);487488    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');489490    const address = collectionIdToAddress(collection);491    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);492493    const events = await recordEvents(contract, async () => {494      await approveExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1);495    });496497    expect(events).to.be.deep.equal([498      {499        address,500        event: 'Approval',501        args: {502          owner: subToEth(alice.address),503          approved: receiver,504          tokenId: tokenId.toString(),505        },506      },507    ]);508  });509510  itWeb3('Events emitted for transferFrom()', async ({ web3 }) => {511    const collection = await createCollectionExpectSuccess({512      mode: { type: 'NFT' },513    });514    const alice = privateKey('//Alice');515    const bob = privateKey('//Bob');516517    const receiver = createEthAccount(web3);518519    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');520    await approveExpectSuccess(collection, tokenId, alice, bob, 1);521522    const address = collectionIdToAddress(collection);523    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);524525    const events = await recordEvents(contract, async () => {526      await transferFromExpectSuccess(collection, tokenId, bob, alice, { ethereum: receiver }, 1, 'NFT');527    });528529    expect(events).to.be.deep.equal([530      {531        address,532        event: 'Transfer',533        args: {534          from: subToEth(alice.address),535          to: receiver,536          tokenId: tokenId.toString(),537        },538      },539    ]);540  });541542  itWeb3('Events emitted for transfer()', async ({ web3 }) => {543    const collection = await createCollectionExpectSuccess({544      mode: { type: 'NFT' },545    });546    const alice = privateKey('//Alice');547548    const receiver = createEthAccount(web3);549550    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT');551552    const address = collectionIdToAddress(collection);553    const contract = new web3.eth.Contract(nonFungibleAbi as any, address);554555    const events = await recordEvents(contract, async () => {556      await transferExpectSuccess(collection, tokenId, alice, { ethereum: receiver }, 1, 'NFT');557    });558559    expect(events).to.be.deep.equal([560      {561        address,562        event: 'Transfer',563        args: {564          from: subToEth(alice.address),565          to: receiver,566          tokenId: tokenId.toString(),567        },568      },569    ]);570  });571});