git.delta.rocks / unique-network / refs/commits / 3a01a5229f7a

difftreelog

source

tests/src/eth/proxy/nonFungibleProxy.test.ts12.8 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 {GAS_ARGS, itWeb3, normalizeEvents} from '../util/helpers';18import {expect} from 'chai';19import {readFile} from 'fs/promises';20import {IKeyringPair} from '@polkadot/types/types';21import {EthUniqueHelper, itEth, usingEthPlaygrounds} from '../util/playgrounds';2223async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {24  // Proxy owner has no special privilegies, we don't need to reuse them25  const owner = await helper.eth.createAccountWithBalance(donor);26  const web3 = helper.getWeb3();27  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${__dirname}/UniqueNFTProxy.abi`)).toString()), undefined, {28    from: owner,29    ...GAS_ARGS,30  });31  const proxy = await proxyContract.deploy({data: (await readFile(`${__dirname}/UniqueNFTProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});32  return proxy;33}3435describe('NFT (Via EVM proxy): Information getting', () => {36  let alice: IKeyringPair;37  let donor: IKeyringPair;3839  before(async function() {40    await usingEthPlaygrounds(async (helper, privateKey) => {41      donor = privateKey('//Alice');42      [alice] = await helper.arrange.createAccounts([10n], donor);43    });44  });4546  itEth('totalSupply', async ({helper}) => {47    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});48    const caller = await helper.eth.createAccountWithBalance(donor);49    await collection.mintToken(alice, {Substrate: alice.address});5051    const address = helper.ethAddress.fromCollectionId(collection.collectionId);52    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);53    const contract = await proxyWrap(helper, evmCollection, donor);54    const totalSupply = await contract.methods.totalSupply().call();5556    expect(totalSupply).to.equal('1');57  });5859  itEth('balanceOf', async ({helper}) => {60    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});6162    const caller = await helper.eth.createAccountWithBalance(donor);63    await collection.mintMultipleTokens(alice, [64      {owner: {Ethereum: caller}},65      {owner: {Ethereum: caller}},66      {owner: {Ethereum: caller}},67    ]);6869    const address = helper.ethAddress.fromCollectionId(collection.collectionId);70    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);71    const contract = await proxyWrap(helper, evmCollection, donor);72    const balance = await contract.methods.balanceOf(caller).call();7374    expect(balance).to.equal('3');75  });7677  itEth('ownerOf', async ({helper}) => {78    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});7980    const caller = await helper.eth.createAccountWithBalance(donor);81    const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});8283    const address = helper.ethAddress.fromCollectionId(collection.collectionId);84    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);85    const contract = await proxyWrap(helper, evmCollection, donor);86    const owner = await contract.methods.ownerOf(tokenId).call();8788    expect(owner).to.equal(caller);89  });90});9192describe('NFT (Via EVM proxy): Plain calls', () => {93  let alice: IKeyringPair;94  let donor: IKeyringPair;9596  before(async function() {97    await usingEthPlaygrounds(async (helper, privateKey) => {98      donor = privateKey('//Alice');99      [alice] = await helper.arrange.createAccounts([10n], donor);100    });101  });102103  itEth('Can perform mint()', async ({helper}) => {104    const owner = await helper.eth.createAccountWithBalance(donor);105    const {collectionAddress} = await helper.eth.createNonfungibleCollection(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);110    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);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      expect(nextTokenId).to.be.equal('1');117      const result = await contract.methods.mintWithTokenURI(118        receiver,119        nextTokenId,120        'Test URI',121      ).send({from: caller});122      const events = normalizeEvents(result.events);123      events[0].address = events[0].address.toLocaleLowerCase();124125      expect(events).to.be.deep.equal([126        {127          address: collectionAddress.toLocaleLowerCase(),128          event: 'Transfer',129          args: {130            from: '0x0000000000000000000000000000000000000000',131            to: receiver,132            tokenId: nextTokenId,133          },134        },135      ]);136137      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');138    }139  });140141  //TODO: CORE-302 add eth methods142  itWeb3.skip('Can perform mintBulk()', async ({web3, api, privateKeyWrapper}) => {143    /*144    const collection = await createCollectionExpectSuccess({145      mode: {type: 'NFT'},146    });147    const alice = privateKeyWrapper('//Alice');148149    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);150    const receiver = createEthAccount(web3);151152    const address = collectionIdToAddress(collection);153    const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);154    const changeAdminTx = api.tx.unique.addCollectionAdmin(collection, {Ethereum: contract.options.address});155    await submitTransactionAsync(alice, changeAdminTx);156157    {158      const nextTokenId = await contract.methods.nextTokenId().call();159      expect(nextTokenId).to.be.equal('1');160      const result = await contract.methods.mintBulkWithTokenURI(161        receiver,162        [163          [nextTokenId, 'Test URI 0'],164          [+nextTokenId + 1, 'Test URI 1'],165          [+nextTokenId + 2, 'Test URI 2'],166        ],167      ).send({from: caller});168      const events = normalizeEvents(result.events);169170      expect(events).to.be.deep.equal([171        {172          address,173          event: 'Transfer',174          args: {175            from: '0x0000000000000000000000000000000000000000',176            to: receiver,177            tokenId: nextTokenId,178          },179        },180        {181          address,182          event: 'Transfer',183          args: {184            from: '0x0000000000000000000000000000000000000000',185            to: receiver,186            tokenId: String(+nextTokenId + 1),187          },188        },189        {190          address,191          event: 'Transfer',192          args: {193            from: '0x0000000000000000000000000000000000000000',194            to: receiver,195            tokenId: String(+nextTokenId + 2),196          },197        },198      ]);199200      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');201      expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');202      expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');203    }204    */205  });206207  itEth('Can perform burn()', async ({helper}) => {208    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});209    const caller = await helper.eth.createAccountWithBalance(donor);210211    const address = helper.ethAddress.fromCollectionId(collection.collectionId);212    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);213    const contract = await proxyWrap(helper, evmCollection, donor);214    const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});215    await collection.addAdmin(alice, {Ethereum: contract.options.address});216217    {218      const result = await contract.methods.burn(tokenId).send({from: caller});219      const events = normalizeEvents(result.events);220221      expect(events).to.be.deep.equal([222        {223          address,224          event: 'Transfer',225          args: {226            from: contract.options.address,227            to: '0x0000000000000000000000000000000000000000',228            tokenId: tokenId.toString(),229          },230        },231      ]);232    }233  });234235  itEth('Can perform approve()', async ({helper}) => {236    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});237    const caller = await helper.eth.createAccountWithBalance(donor);238    const spender = helper.eth.createAccount();239240    const address = helper.ethAddress.fromCollectionId(collection.collectionId);241    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);242    const contract = await proxyWrap(helper, evmCollection, donor);243    const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});244245    {246      const result = await contract.methods.approve(spender, tokenId).send({from: caller, ...GAS_ARGS});247      const events = normalizeEvents(result.events);248249      expect(events).to.be.deep.equal([250        {251          address,252          event: 'Approval',253          args: {254            owner: contract.options.address,255            approved: spender,256            tokenId: tokenId.toString(),257          },258        },259      ]);260    }261  });262263  itEth('Can perform transferFrom()', async ({helper}) => {264    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});265    const caller = await helper.eth.createAccountWithBalance(donor);266    const owner = await helper.eth.createAccountWithBalance(donor);267268    const receiver = 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: owner});274275    await evmCollection.methods.approve(contract.options.address, tokenId).send({from: owner});276277    {278      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: caller});279      const events = normalizeEvents(result.events);280      expect(events).to.be.deep.equal([281        {282          address,283          event: 'Transfer',284          args: {285            from: owner,286            to: receiver,287            tokenId: tokenId.toString(),288          },289        },290      ]);291    }292293    {294      const balance = await contract.methods.balanceOf(receiver).call();295      expect(+balance).to.equal(1);296    }297298    {299      const balance = await contract.methods.balanceOf(contract.options.address).call();300      expect(+balance).to.equal(0);301    }302  });303304  itEth('Can perform transfer()', async ({helper}) => {305    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});306    const caller = await helper.eth.createAccountWithBalance(donor);307    const receiver = helper.eth.createAccount();308309    const address = helper.ethAddress.fromCollectionId(collection.collectionId);310    const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);311    const contract = await proxyWrap(helper, evmCollection, donor);312    const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});313314    {315      const result = await contract.methods.transfer(receiver, tokenId).send({from: caller});316      const events = normalizeEvents(result.events);317      expect(events).to.be.deep.equal([318        {319          address,320          event: 'Transfer',321          args: {322            from: contract.options.address,323            to: receiver,324            tokenId: tokenId.toString(),325          },326        },327      ]);328    }329330    {331      const balance = await contract.methods.balanceOf(contract.options.address).call();332      expect(+balance).to.equal(0);333    }334335    {336      const balance = await contract.methods.balanceOf(receiver).call();337      expect(+balance).to.equal(1);338    }339  });340});