git.delta.rocks / unique-network / refs/commits / 2e0d1a668d33

difftreelog

source

tests/src/eth/proxy/nonFungibleProxy.test.ts14.4 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {readFile} from 'fs/promises';18import {IKeyringPair} from '@polkadot/types/types';19import {EthUniqueHelper, itEth, usingEthPlaygrounds, expect} from '../util';202122async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {23  // Proxy owner has no special privilegies, we don't need to reuse them24  const owner = await helper.eth.createAccountWithBalance(donor);25  const web3 = helper.getWeb3();26  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueNFTProxy.abi`)).toString()), undefined, {27    from: owner,28    gas: helper.eth.DEFAULT_GAS,29  });30  const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueNFTProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});31  return proxy;32}3334describe('NFT (Via EVM proxy): Information getting', () => {35  let alice: IKeyringPair;36  let donor: IKeyringPair;3738  before(async function() {39    await usingEthPlaygrounds(async (helper, privateKey) => {40      donor = await privateKey({filename: __filename});41      [alice] = await helper.arrange.createAccounts([10n], donor);42    });43  });4445  itEth('totalSupply', async ({helper}) => {46    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});47    const caller = await helper.eth.createAccountWithBalance(donor);48    await collection.mintToken(alice, {Substrate: alice.address});4950    const address = helper.ethAddress.fromCollectionId(collection.collectionId);51    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);52    const contract = await proxyWrap(helper, evmCollection, donor);53    const totalSupply = await contract.methods.totalSupply().call();5455    expect(totalSupply).to.equal('1');56  });5758  itEth('balanceOf', async ({helper}) => {59    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});6061    const caller = await helper.eth.createAccountWithBalance(donor);62    await collection.mintMultipleTokens(alice, [63      {owner: {Ethereum: caller}},64      {owner: {Ethereum: caller}},65      {owner: {Ethereum: caller}},66    ]);6768    const address = helper.ethAddress.fromCollectionId(collection.collectionId);69    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);70    const contract = await proxyWrap(helper, evmCollection, donor);71    const balance = await contract.methods.balanceOf(caller).call();7273    expect(balance).to.equal('3');74  });7576  itEth('ownerOf', async ({helper}) => {77    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});7879    const caller = await helper.eth.createAccountWithBalance(donor);80    const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});8182    const address = helper.ethAddress.fromCollectionId(collection.collectionId);83    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);84    const contract = await proxyWrap(helper, evmCollection, donor);85    const owner = await contract.methods.ownerOf(tokenId).call();8687    expect(owner).to.equal(caller);88  });89});9091describe('NFT (Via EVM proxy): Plain calls', () => {92  let alice: IKeyringPair;93  let donor: IKeyringPair;9495  before(async function() {96    await usingEthPlaygrounds(async (helper, privateKey) => {97      donor = await privateKey({filename: __filename});98      [alice] = await helper.arrange.createAccounts([10n], donor);99    });100  });101102  // Soft-deprecated103  itEth('[eth] Can perform mint()', async ({helper}) => {104    const owner = await helper.eth.createAccountWithBalance(donor);105    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'A', 'A', '');106    const caller = await helper.eth.createAccountWithBalance(donor);107    const receiver = helper.eth.createAccount();108109    const collectionEvmOwned = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);110    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', caller, true);111    const contract = await proxyWrap(helper, collectionEvm, donor);112    await collectionEvmOwned.methods.addCollectionAdmin(contract.options.address).send();113114    {115      const nextTokenId = await contract.methods.nextTokenId().call();116      const result = await contract.methods.mintWithTokenURI(receiver, nextTokenId, 'Test URI').send({from: caller});117      const tokenId = result.events.Transfer.returnValues.tokenId;118      expect(tokenId).to.be.equal('1');119120      const events = helper.eth.normalizeEvents(result.events);121      events[0].address = events[0].address.toLocaleLowerCase();122123      expect(events).to.be.deep.equal([124        {125          address: collectionAddress.toLocaleLowerCase(),126          event: 'Transfer',127          args: {128            from: '0x0000000000000000000000000000000000000000',129            to: receiver,130            tokenId,131          },132        },133      ]);134135      expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');136    }137  });138139  itEth('[cross] Can perform mint()', async ({helper}) => {140    const owner = await helper.eth.createAccountWithBalance(donor);141    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'A', 'A', '');142    const caller = await helper.eth.createAccountWithBalance(donor);143    const receiver = helper.eth.createAccount();144145    const collectionEvmOwned = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);146    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);147    const contract = await proxyWrap(helper, collectionEvm, donor);148    const contractAddressCross = helper.ethCrossAccount.fromAddress(contract.options.address);149    await collectionEvmOwned.methods.addCollectionAdminCross(contractAddressCross).send();150151    {152      const nextTokenId = await contract.methods.nextTokenId().call();153      const result = await contract.methods.mintWithTokenURI(receiver, nextTokenId, 'Test URI').send({from: caller});154      const tokenId = result.events.Transfer.returnValues.tokenId;155      expect(tokenId).to.be.equal('1');156157      const events = helper.eth.normalizeEvents(result.events);158      events[0].address = events[0].address.toLocaleLowerCase();159160      expect(events).to.be.deep.equal([161        {162          address: collectionAddress.toLocaleLowerCase(),163          event: 'Transfer',164          args: {165            from: '0x0000000000000000000000000000000000000000',166            to: receiver,167            tokenId,168          },169        },170      ]);171172      expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');173    }174  });175176  //TODO: CORE-302 add eth methods177  itEth.skip('Can perform mintBulk()', async ({helper}) => {178    const collection = await helper.nft.mintCollection(donor, {name: 'New', description: 'New collection', tokenPrefix: 'NEW'});179180    const caller = await helper.eth.createAccountWithBalance(donor, 30n);181    const receiver = helper.eth.createAccount();182183    const address = helper.ethAddress.fromCollectionId(collection.collectionId);184    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);185    const contract = await proxyWrap(helper, evmCollection, donor);186    await collection.addAdmin(donor, {Ethereum: contract.options.address});187188    {189      const nextTokenId = await contract.methods.nextTokenId().call();190      expect(nextTokenId).to.be.equal('1');191      const result = await contract.methods.mintBulkWithTokenURI(192        receiver,193        [194          [nextTokenId, 'Test URI 0'],195          [+nextTokenId + 1, 'Test URI 1'],196          [+nextTokenId + 2, 'Test URI 2'],197        ],198      ).send({from: caller});199      const events = helper.eth.normalizeEvents(result.events);200201      expect(events).to.be.deep.equal([202        {203          address,204          event: 'Transfer',205          args: {206            from: '0x0000000000000000000000000000000000000000',207            to: receiver,208            tokenId: nextTokenId,209          },210        },211        {212          address,213          event: 'Transfer',214          args: {215            from: '0x0000000000000000000000000000000000000000',216            to: receiver,217            tokenId: String(+nextTokenId + 1),218          },219        },220        {221          address,222          event: 'Transfer',223          args: {224            from: '0x0000000000000000000000000000000000000000',225            to: receiver,226            tokenId: String(+nextTokenId + 2),227          },228        },229      ]);230231      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');232      expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');233      expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');234    }235  });236237  itEth('Can perform burn()', async ({helper}) => {238    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});239    const caller = await helper.eth.createAccountWithBalance(donor);240241    const address = helper.ethAddress.fromCollectionId(collection.collectionId);242    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);243    const contract = await proxyWrap(helper, evmCollection, donor);244    const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});245    await collection.addAdmin(alice, {Ethereum: contract.options.address});246247    {248      const result = await contract.methods.burn(tokenId).send({from: caller});249      const events = helper.eth.normalizeEvents(result.events);250251      expect(events).to.be.deep.equal([252        {253          address,254          event: 'Transfer',255          args: {256            from: contract.options.address,257            to: '0x0000000000000000000000000000000000000000',258            tokenId: tokenId.toString(),259          },260        },261      ]);262    }263  });264265  itEth('Can perform approve()', async ({helper}) => {266    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});267    const caller = await helper.eth.createAccountWithBalance(donor);268    const spender = helper.eth.createAccount();269270    const address = helper.ethAddress.fromCollectionId(collection.collectionId);271    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);272    const contract = await proxyWrap(helper, evmCollection, donor);273    const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});274275    {276      const result = await contract.methods.approve(spender, tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS});277      const events = helper.eth.normalizeEvents(result.events);278279      expect(events).to.be.deep.equal([280        {281          address,282          event: 'Approval',283          args: {284            owner: contract.options.address,285            approved: spender,286            tokenId: tokenId.toString(),287          },288        },289      ]);290    }291  });292293  itEth('Can perform transferFrom()', async ({helper}) => {294    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});295    const caller = await helper.eth.createAccountWithBalance(donor);296    const owner = await helper.eth.createAccountWithBalance(donor);297298    const receiver = helper.eth.createAccount();299300    const address = helper.ethAddress.fromCollectionId(collection.collectionId);301    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);302    const contract = await proxyWrap(helper, evmCollection, donor);303    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});304305    await evmCollection.methods.approve(contract.options.address, tokenId).send({from: owner});306307    {308      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: caller});309      const events = helper.eth.normalizeEvents(result.events);310      expect(events).to.be.deep.equal([311        {312          address,313          event: 'Transfer',314          args: {315            from: owner,316            to: receiver,317            tokenId: tokenId.toString(),318          },319        },320      ]);321    }322323    {324      const balance = await contract.methods.balanceOf(receiver).call();325      expect(+balance).to.equal(1);326    }327328    {329      const balance = await contract.methods.balanceOf(contract.options.address).call();330      expect(+balance).to.equal(0);331    }332  });333334  itEth('Can perform transfer()', async ({helper}) => {335    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});336    const caller = await helper.eth.createAccountWithBalance(donor);337    const receiver = helper.eth.createAccount();338339    const address = helper.ethAddress.fromCollectionId(collection.collectionId);340    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);341    const contract = await proxyWrap(helper, evmCollection, donor);342    const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});343344    {345      const result = await contract.methods.transfer(receiver, tokenId).send({from: caller});346      const events = helper.eth.normalizeEvents(result.events);347      expect(events).to.be.deep.equal([348        {349          address,350          event: 'Transfer',351          args: {352            from: contract.options.address,353            to: receiver,354            tokenId: tokenId.toString(),355          },356        },357      ]);358    }359360    {361      const balance = await contract.methods.balanceOf(contract.options.address).call();362      expect(+balance).to.equal(0);363    }364365    {366      const balance = await contract.methods.balanceOf(receiver).call();367      expect(+balance).to.equal(1);368    }369  });370});