git.delta.rocks / unique-network / refs/commits / 27ba026da63d

difftreelog

source

js-packages/tests/eth/proxy/nonFungibleProxy.test.ts14.6 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 type {IKeyringPair} from '@polkadot/types/types';19import {itEth, usingEthPlaygrounds, expect} from '../util/index.js';20import {EthUniqueHelper} from '../util/playgrounds/unique.dev.js';21import {makeNames} from '../../util/index.js';2223const {dirname} = makeNames(import.meta.url);242526async function proxyWrap(helper: EthUniqueHelper, wrapped: any, donor: IKeyringPair) {27  // Proxy owner has no special privilegies, we don't need to reuse them28  const owner = await helper.eth.createAccountWithBalance(donor);29  const web3 = helper.getWeb3();30  const proxyContract = new web3.eth.Contract(JSON.parse((await readFile(`${dirname}/UniqueNFTProxy.abi`)).toString()), undefined, {31    from: owner,32    gas: helper.eth.DEFAULT_GAS,33  });34  const proxy = await proxyContract.deploy({data: (await readFile(`${dirname}/UniqueNFTProxy.bin`)).toString(), arguments: [wrapped.options.address]}).send({from: owner});35  return proxy;36}3738describe('NFT (Via EVM proxy): Information getting', () => {39  let alice: IKeyringPair;40  let donor: IKeyringPair;4142  before(async function() {43    await usingEthPlaygrounds(async (helper, privateKey) => {44      donor = await privateKey({url: import.meta.url});45      [alice] = await helper.arrange.createAccounts([10n], donor);46    });47  });4849  itEth('totalSupply', async ({helper}) => {50    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});51    const caller = await helper.eth.createAccountWithBalance(donor);52    await collection.mintToken(alice, {Substrate: alice.address});5354    const address = helper.ethAddress.fromCollectionId(collection.collectionId);55    const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);56    const contract = await proxyWrap(helper, evmCollection, donor);57    const totalSupply = await contract.methods.totalSupply().call();5859    expect(totalSupply).to.equal('1');60  });6162  itEth('balanceOf', async ({helper}) => {63    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});6465    const caller = await helper.eth.createAccountWithBalance(donor);66    await collection.mintMultipleTokens(alice, [67      {owner: {Ethereum: caller}},68      {owner: {Ethereum: caller}},69      {owner: {Ethereum: caller}},70    ]);7172    const address = helper.ethAddress.fromCollectionId(collection.collectionId);73    const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);74    const contract = await proxyWrap(helper, evmCollection, donor);75    const balance = await contract.methods.balanceOf(caller).call();7677    expect(balance).to.equal('3');78  });7980  itEth('ownerOf', async ({helper}) => {81    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});8283    const caller = await helper.eth.createAccountWithBalance(donor);84    const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});8586    const address = helper.ethAddress.fromCollectionId(collection.collectionId);87    const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);88    const contract = await proxyWrap(helper, evmCollection, donor);89    const owner = await contract.methods.ownerOf(tokenId).call();9091    expect(owner).to.equal(caller);92  });93});9495describe('NFT (Via EVM proxy): Plain calls', () => {96  let alice: IKeyringPair;97  let donor: IKeyringPair;9899  before(async function() {100    await usingEthPlaygrounds(async (helper, privateKey) => {101      donor = await privateKey({url: import.meta.url});102      [alice] = await helper.arrange.createAccounts([10n], donor);103    });104  });105106  // Soft-deprecated107  itEth('[eth] Can perform mint()', async ({helper}) => {108    const owner = await helper.eth.createAccountWithBalance(donor);109    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'A', 'A', '');110    const caller = await helper.eth.createAccountWithBalance(donor);111    const receiver = helper.eth.createAccount();112113    const collectionEvmOwned = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);114    const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', caller, true);115    const contract = await proxyWrap(helper, collectionEvm, donor);116    await collectionEvmOwned.methods.addCollectionAdmin(contract.options.address).send();117118    {119      const nextTokenId = await contract.methods.nextTokenId().call();120      const result = await contract.methods.mintWithTokenURI(receiver, nextTokenId, 'Test URI').send({from: caller});121      const tokenId = result.events.Transfer.returnValues.tokenId;122      expect(tokenId).to.be.equal('1');123124      const event = helper.eth.normalizeEvents(result.events)125        .find(event => event.event === 'Transfer')!;126      event.address = event.address.toLocaleLowerCase();127128      expect(event).to.be.deep.equal({129        address: collectionAddress.toLocaleLowerCase(),130        event: 'Transfer',131        args: {132          from: '0x0000000000000000000000000000000000000000',133          to: receiver,134          tokenId,135        },136      });137138      expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');139    }140  });141142  itEth('[cross] Can perform mint()', async ({helper}) => {143    const owner = await helper.eth.createAccountWithBalance(donor);144    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'A', 'A', '');145    const caller = await helper.eth.createAccountWithBalance(donor);146    const receiver = helper.eth.createAccount();147148    const collectionEvmOwned = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);149    const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', caller);150    const contract = await proxyWrap(helper, collectionEvm, donor);151    const contractAddressCross = helper.ethCrossAccount.fromAddress(contract.options.address);152    await collectionEvmOwned.methods.addCollectionAdminCross(contractAddressCross).send();153154    {155      const nextTokenId = await contract.methods.nextTokenId().call();156      const result = await contract.methods.mintWithTokenURI(receiver, nextTokenId, 'Test URI').send({from: caller});157      const tokenId = result.events.Transfer.returnValues.tokenId;158      expect(tokenId).to.be.equal('1');159160      const event = helper.eth.normalizeEvents(result.events)161        .find(event => event.event === 'Transfer')!;162      event.address = event.address.toLocaleLowerCase();163164      expect(event).to.be.deep.equal({165        address: collectionAddress.toLocaleLowerCase(),166        event: 'Transfer',167        args: {168          from: '0x0000000000000000000000000000000000000000',169          to: receiver,170          tokenId,171        },172      });173174      expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');175    }176  });177178  //TODO: CORE-302 add eth methods179  itEth.skip('Can perform mintBulk()', async ({helper}) => {180    const collection = await helper.nft.mintCollection(donor, {name: 'New', description: 'New collection', tokenPrefix: 'NEW'});181182    const caller = await helper.eth.createAccountWithBalance(donor);183    const receiver = helper.eth.createAccount();184185    const address = helper.ethAddress.fromCollectionId(collection.collectionId);186    const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);187    const contract = await proxyWrap(helper, evmCollection, donor);188    await collection.addAdmin(donor, {Ethereum: contract.options.address});189190    {191      const nextTokenId = await contract.methods.nextTokenId().call();192      expect(nextTokenId).to.be.equal('1');193      const result = await contract.methods.mintBulkWithTokenURI(194        receiver,195        [196          [nextTokenId, 'Test URI 0'],197          [+nextTokenId + 1, 'Test URI 1'],198          [+nextTokenId + 2, 'Test URI 2'],199        ],200      ).send({from: caller});201      const events = helper.eth.normalizeEvents(result.events);202203      expect(events).to.be.deep.equal([204        {205          address,206          event: 'Transfer',207          args: {208            from: '0x0000000000000000000000000000000000000000',209            to: receiver,210            tokenId: nextTokenId,211          },212        },213        {214          address,215          event: 'Transfer',216          args: {217            from: '0x0000000000000000000000000000000000000000',218            to: receiver,219            tokenId: String(+nextTokenId + 1),220          },221        },222        {223          address,224          event: 'Transfer',225          args: {226            from: '0x0000000000000000000000000000000000000000',227            to: receiver,228            tokenId: String(+nextTokenId + 2),229          },230        },231      ]);232233      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');234      expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');235      expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');236    }237  });238239  itEth('Can perform burn()', async ({helper}) => {240    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});241    const caller = await helper.eth.createAccountWithBalance(donor);242243    const address = helper.ethAddress.fromCollectionId(collection.collectionId);244    const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);245    const contract = await proxyWrap(helper, evmCollection, donor);246    const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});247    await collection.addAdmin(alice, {Ethereum: contract.options.address});248249    {250      const result = await contract.methods.burn(tokenId).send({from: caller});251      const events = helper.eth.normalizeEvents(result.events);252253      expect(events).to.be.deep.equal([254        {255          address,256          event: 'Transfer',257          args: {258            from: contract.options.address,259            to: '0x0000000000000000000000000000000000000000',260            tokenId: tokenId.toString(),261          },262        },263      ]);264    }265  });266267  itEth('Can perform approve()', async ({helper}) => {268    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});269    const caller = await helper.eth.createAccountWithBalance(donor);270    const spender = helper.eth.createAccount();271272    const address = helper.ethAddress.fromCollectionId(collection.collectionId);273    const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);274    const contract = await proxyWrap(helper, evmCollection, donor);275    const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});276277    {278      const result = await contract.methods.approve(spender, tokenId).send({from: caller, gas: helper.eth.DEFAULT_GAS});279      const events = helper.eth.normalizeEvents(result.events);280281      expect(events).to.be.deep.equal([282        {283          address,284          event: 'Approval',285          args: {286            owner: contract.options.address,287            approved: spender,288            tokenId: tokenId.toString(),289          },290        },291      ]);292    }293  });294295  itEth('Can perform transferFrom()', async ({helper}) => {296    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});297    const caller = await helper.eth.createAccountWithBalance(donor);298    const owner = await helper.eth.createAccountWithBalance(donor);299300    const receiver = helper.eth.createAccount();301302    const address = helper.ethAddress.fromCollectionId(collection.collectionId);303    const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);304    const contract = await proxyWrap(helper, evmCollection, donor);305    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});306307    await evmCollection.methods.approve(contract.options.address, tokenId).send({from: owner});308309    {310      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: caller});311      const events = helper.eth.normalizeEvents(result.events);312      expect(events).to.be.deep.equal([313        {314          address,315          event: 'Transfer',316          args: {317            from: owner,318            to: receiver,319            tokenId: tokenId.toString(),320          },321        },322      ]);323    }324325    {326      const balance = await contract.methods.balanceOf(receiver).call();327      expect(+balance).to.equal(1);328    }329330    {331      const balance = await contract.methods.balanceOf(contract.options.address).call();332      expect(+balance).to.equal(0);333    }334  });335336  itEth('Can perform transfer()', async ({helper}) => {337    const collection = await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});338    const caller = await helper.eth.createAccountWithBalance(donor);339    const receiver = helper.eth.createAccount();340341    const address = helper.ethAddress.fromCollectionId(collection.collectionId);342    const evmCollection = await helper.ethNativeContract.collection(address, 'nft', caller);343    const contract = await proxyWrap(helper, evmCollection, donor);344    const {tokenId} = await collection.mintToken(alice, {Ethereum: contract.options.address});345346    {347      const result = await contract.methods.transfer(receiver, tokenId).send({from: caller});348      const events = helper.eth.normalizeEvents(result.events);349      expect(events).to.be.deep.equal([350        {351          address,352          event: 'Transfer',353          args: {354            from: contract.options.address,355            to: receiver,356            tokenId: tokenId.toString(),357          },358        },359      ]);360    }361362    {363      const balance = await contract.methods.balanceOf(contract.options.address).call();364      expect(+balance).to.equal(0);365    }366367    {368      const balance = await contract.methods.balanceOf(receiver).call();369      expect(+balance).to.equal(1);370    }371  });372});