git.delta.rocks / unique-network / refs/commits / 4e90c111d3d7

difftreelog

source

tests/src/eth/proxy/nonFungibleProxy.test.ts13.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 {createCollectionExpectSuccess, createItemExpectSuccess, setVariableMetaDataExpectSuccess, setMetadataUpdatePermissionFlagExpectSuccess} from '../../util/helpers';8import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents} from '../util/helpers';9import nonFungibleAbi from '../nonFungibleAbi.json';10import {expect} from 'chai';11import {submitTransactionAsync} from '../../substrate/substrate-api';12import Web3 from 'web3';13import {readFile} from 'fs/promises';14import {ApiPromise} from '@polkadot/api';1516async function proxyWrap(api: ApiPromise, web3: Web3, wrapped: any) {17  // Proxy owner has no special privilegies, we don't need to reuse them18  const owner = await createEthAccountWithBalance(api, web3);19  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueNFTProxy.abi`)).toString()), undefined, {20    from: owner,21    ...GAS_ARGS,22  });23  const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueNFTProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});24  return proxy;25}2627describe('NFT (Via EVM proxy): Information getting', () => {28  itWeb3('totalSupply', async ({api, web3}) => {29    const collection = await createCollectionExpectSuccess({30      mode: {type: 'NFT'},31    });32    const alice = privateKey('//Alice');33    const caller = await createEthAccountWithBalance(api, web3);3435    await createItemExpectSuccess(alice, collection, 'NFT', {Substrate: alice.address});3637    const address = collectionIdToAddress(collection);38    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}));39    const totalSupply = await contract.methods.totalSupply().call();4041    expect(totalSupply).to.equal('1');42  });4344  itWeb3('balanceOf', async ({api, web3}) => {45    const collection = await createCollectionExpectSuccess({46      mode: {type: 'NFT'},47    });48    const alice = privateKey('//Alice');4950    const caller = await createEthAccountWithBalance(api, web3);51    await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: caller});52    await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: caller});53    await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: caller});5455    const address = collectionIdToAddress(collection);56    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}));57    const balance = await contract.methods.balanceOf(caller).call();5859    expect(balance).to.equal('3');60  });6162  itWeb3('ownerOf', async ({api, web3}) => {63    const collection = await createCollectionExpectSuccess({64      mode: {type: 'NFT'},65    });66    const alice = privateKey('//Alice');6768    const caller = await createEthAccountWithBalance(api, web3);69    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: caller});7071    const address = collectionIdToAddress(collection);72    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}));73    const owner = await contract.methods.ownerOf(tokenId).call();7475    expect(owner).to.equal(caller);76  });77});7879describe('NFT (Via EVM proxy): Plain calls', () => {80  itWeb3('Can perform mint()', async ({web3, api}) => {81    const collection = await createCollectionExpectSuccess({82      mode: {type: 'NFT'},83    });84    const alice = privateKey('//Alice');85    const caller = await createEthAccountWithBalance(api, web3);86    const receiver = createEthAccount(web3);8788    const address = collectionIdToAddress(collection);89    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}));9091    const changeAdminTx = api.tx.nft.addCollectionAdmin(collection, {Ethereum: contract.options.address});92    await submitTransactionAsync(alice, changeAdminTx);9394    {95      const nextTokenId = await contract.methods.nextTokenId().call();96      expect(nextTokenId).to.be.equal('1');97      const result = await contract.methods.mintWithTokenURI(98        receiver,99        nextTokenId,100        'Test URI',101      ).send({from: caller});102      const events = normalizeEvents(result.events);103104      expect(events).to.be.deep.equal([105        {106          address,107          event: 'Transfer',108          args: {109            from: '0x0000000000000000000000000000000000000000',110            to: receiver,111            tokenId: nextTokenId,112          },113        },114      ]);115116      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');117    }118  });119  itWeb3('Can perform mintBulk()', async ({web3, api}) => {120    const collection = await createCollectionExpectSuccess({121      mode: {type: 'NFT'},122    });123    const alice = privateKey('//Alice');124125    const caller = await createEthAccountWithBalance(api, web3);126    const receiver = createEthAccount(web3);127128    const address = collectionIdToAddress(collection);129    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}));130    const changeAdminTx = api.tx.nft.addCollectionAdmin(collection, {Ethereum: contract.options.address});131    await submitTransactionAsync(alice, changeAdminTx);132133    {134      const nextTokenId = await contract.methods.nextTokenId().call();135      expect(nextTokenId).to.be.equal('1');136      const result = await contract.methods.mintBulkWithTokenURI(137        receiver,138        [139          [nextTokenId, 'Test URI 0'],140          [+nextTokenId + 1, 'Test URI 1'],141          [+nextTokenId + 2, 'Test URI 2'],142        ],143      ).send({from: caller});144      const events = normalizeEvents(result.events);145146      expect(events).to.be.deep.equal([147        {148          address,149          event: 'Transfer',150          args: {151            from: '0x0000000000000000000000000000000000000000',152            to: receiver,153            tokenId: nextTokenId,154          },155        },156        {157          address,158          event: 'Transfer',159          args: {160            from: '0x0000000000000000000000000000000000000000',161            to: receiver,162            tokenId: String(+nextTokenId + 1),163          },164        },165        {166          address,167          event: 'Transfer',168          args: {169            from: '0x0000000000000000000000000000000000000000',170            to: receiver,171            tokenId: String(+nextTokenId + 2),172          },173        },174      ]);175176      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');177      expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');178      expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');179    }180  });181182  itWeb3('Can perform burn()', async ({web3, api}) => {183    const collection = await createCollectionExpectSuccess({184      mode: {type: 'NFT'},185    });186    const alice = privateKey('//Alice');187    const caller = await createEthAccountWithBalance(api, web3);188189    const address = collectionIdToAddress(collection);190    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}));191    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: contract.options.address});192193    const changeAdminTx = api.tx.nft.addCollectionAdmin(collection, {Ethereum: contract.options.address});194    await submitTransactionAsync(alice, changeAdminTx);195196    {197      const result = await contract.methods.burn(tokenId).send({from: caller});198      const events = normalizeEvents(result.events);199200      expect(events).to.be.deep.equal([201        {202          address,203          event: 'Transfer',204          args: {205            from: contract.options.address,206            to: '0x0000000000000000000000000000000000000000',207            tokenId: tokenId.toString(),208          },209        },210      ]);211    }212  });213214  itWeb3('Can perform approve()', async ({web3, api}) => {215    const collection = await createCollectionExpectSuccess({216      mode: {type: 'NFT'},217    });218    const alice = privateKey('//Alice');219    const caller = await createEthAccountWithBalance(api, web3);220    const spender = createEthAccount(web3);221222    const address = collectionIdToAddress(collection);223    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address));224    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: contract.options.address});225226    {227      const result = await contract.methods.approve(spender, tokenId).send({from: caller, gas: '0x1000000', gasPrice: '0x01'});228      const events = normalizeEvents(result.events);229230      expect(events).to.be.deep.equal([231        {232          address,233          event: 'Approval',234          args: {235            owner: contract.options.address,236            approved: spender,237            tokenId: tokenId.toString(),238          },239        },240      ]);241    }242  });243244  itWeb3('Can perform transferFrom()', async ({web3, api}) => {245    const collection = await createCollectionExpectSuccess({246      mode: {type: 'NFT'},247    });248    const alice = privateKey('//Alice');249    const caller = await createEthAccountWithBalance(api, web3);250    const owner = await createEthAccountWithBalance(api, web3);251252    const receiver = createEthAccount(web3);253254    const address = collectionIdToAddress(collection);255    const evmCollection = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});256    const contract = await proxyWrap(api, web3, evmCollection);257    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: owner});258259    await evmCollection.methods.approve(contract.options.address, tokenId).send({from: owner});260261    {262      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: caller});263      const events = normalizeEvents(result.events);264      expect(events).to.be.deep.equal([265        {266          address,267          event: 'Transfer',268          args: {269            from: owner,270            to: receiver,271            tokenId: tokenId.toString(),272          },273        },274      ]);275    }276277    {278      const balance = await contract.methods.balanceOf(receiver).call();279      expect(+balance).to.equal(1);280    }281282    {283      const balance = await contract.methods.balanceOf(contract.options.address).call();284      expect(+balance).to.equal(0);285    }286  });287288  itWeb3('Can perform transfer()', async ({web3, api}) => {289    const collection = await createCollectionExpectSuccess({290      mode: {type: 'NFT'},291    });292    const alice = privateKey('//Alice');293    const caller = await createEthAccountWithBalance(api, web3);294    const receiver = createEthAccount(web3);295296    const address = collectionIdToAddress(collection);297    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}));298    const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: contract.options.address});299300    {301      const result = await contract.methods.transfer(receiver, tokenId).send({from: caller});302      const events = normalizeEvents(result.events);303      expect(events).to.be.deep.equal([304        {305          address,306          event: 'Transfer',307          args: {308            from: contract.options.address,309            to: receiver,310            tokenId: tokenId.toString(),311          },312        },313      ]);314    }315316    {317      const balance = await contract.methods.balanceOf(contract.options.address).call();318      expect(+balance).to.equal(0);319    }320321    {322      const balance = await contract.methods.balanceOf(receiver).call();323      expect(+balance).to.equal(1);324    }325  });326327  itWeb3('Can perform getVariableMetadata', async ({web3, api}) => {328    const collection = await createCollectionExpectSuccess({329      mode: {type: 'NFT'},330    });331    const alice = privateKey('//Alice');332    const caller = await createEthAccountWithBalance(api, web3);333334    const address = collectionIdToAddress(collection);335    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}));336    const item = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: contract.options.address});337    await setMetadataUpdatePermissionFlagExpectSuccess(alice, collection, 'Admin');338    await setVariableMetaDataExpectSuccess(alice, collection, item, [1, 2, 3]);339340    expect(await contract.methods.getVariableMetadata(item).call()).to.be.equal('0x010203');341  });342343  itWeb3('Can perform setVariableMetadata', async ({web3, api}) => {344    const collection = await createCollectionExpectSuccess({345      mode: {type: 'NFT'},346    });347    const alice = privateKey('//Alice');348    const caller = await createEthAccountWithBalance(api, web3);349350    const address = collectionIdToAddress(collection);351    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}));352    const item = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: contract.options.address});353354    expect(await contract.methods.setVariableMetadata(item, '0x010203').send({from: caller}));355    expect(await contract.methods.getVariableMetadata(item).call()).to.be.equal('0x010203');356  });357});