git.delta.rocks / unique-network / refs/commits / 09a417ba5601

difftreelog

Add approveCross and trasferFromCross checks

Max Andreev2022-12-01parent: #71e7d44.patch.diff
in: master

2 files changed

modifiedtests/src/eth/allowlist.test.tsdiffbeforeafterboth
--- a/tests/src/eth/allowlist.test.ts
+++ b/tests/src/eth/allowlist.test.ts
@@ -91,7 +91,7 @@
     expect(await collectionEvm.methods.allowlistedCross(crossUser).call({from: owner})).to.be.false;
   });
 
-  itEth.only('Collection allowlist can be added and removed by [cross] address', async ({helper}) => {
+  itEth('Collection allowlist can be added and removed by [cross] address', async ({helper}) => {
     const owner = (await helper.eth.createAccountWithBalance(donor)).toLowerCase();
     const [userSub] = await helper.arrange.createAccounts([10n], donor);
     const userEth = await helper.eth.createAccountWithBalance(donor);
@@ -100,6 +100,7 @@
     const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
     const userCrossSub = helper.ethCrossAccount.fromKeyringPair(userSub);
     const userCrossEth = helper.ethCrossAccount.fromAddress(userEth);
+    const ownerCrossEth = helper.ethCrossAccount.fromAddress(owner);
     
     // Can addToCollectionAllowListCross:
     expect(await helper.collection.allowed(collectionId, {Substrate: userSub.address})).to.be.false;
@@ -114,6 +115,7 @@
     await collectionEvm.methods.mint(userEth).send();
     await collectionEvm.methods.setCollectionAccess(1).send();
     await collectionEvm.methods.transfer(owner, 1).send({from: userEth}); // FIXME: why not?
+    // await collectionEvm.methods.transferCross(ownerCrossEth, 1).send({from: userEth}); // FIXME: why not?
     expect(await helper.nft.getTokenOwner(collectionId, 1)).to.eq({Ethereum: owner});
     
     // can removeFromCollectionAllowListCross
modifiedtests/src/eth/nonFungible.test.tsdiffbeforeafterboth
before · tests/src/eth/nonFungible.test.ts
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 {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util';18import {IKeyringPair} from '@polkadot/types/types';19import {Contract} from 'web3-eth-contract';20import exp from 'constants';212223describe('NFT: Information getting', () => {24  let donor: IKeyringPair;25  let alice: IKeyringPair;2627  before(async function() {28    await usingEthPlaygrounds(async (helper, privateKey) => {29      donor = await privateKey({filename: __filename});30      [alice] = await helper.arrange.createAccounts([10n], donor);31    });32  });3334  itEth('totalSupply', async ({helper}) => {35    const collection = await helper.nft.mintCollection(alice, {});36    await collection.mintToken(alice);3738    const caller = await helper.eth.createAccountWithBalance(donor);3940    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);41    const totalSupply = await contract.methods.totalSupply().call();4243    expect(totalSupply).to.equal('1');44  });4546  itEth('balanceOf', async ({helper}) => {47    const collection = await helper.nft.mintCollection(alice, {});48    const caller = await helper.eth.createAccountWithBalance(donor);4950    await collection.mintToken(alice, {Ethereum: caller});51    await collection.mintToken(alice, {Ethereum: caller});52    await collection.mintToken(alice, {Ethereum: caller});5354    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);55    const balance = await contract.methods.balanceOf(caller).call();5657    expect(balance).to.equal('3');58  });5960  itEth('ownerOf', async ({helper}) => {61    const collection = await helper.nft.mintCollection(alice, {});62    const caller = await helper.eth.createAccountWithBalance(donor);6364    const token = await collection.mintToken(alice, {Ethereum: caller});6566    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);6768    const owner = await contract.methods.ownerOf(token.tokenId).call();6970    expect(owner).to.equal(caller);71  });7273  itEth('name/symbol is available regardless of ERC721Metadata support', async ({helper}) => {74    const collection = await helper.nft.mintCollection(alice, {name: 'test', tokenPrefix: 'TEST'});75    const caller = helper.eth.createAccount();7677    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);7879    expect(await contract.methods.name().call()).to.equal('test');80    expect(await contract.methods.symbol().call()).to.equal('TEST');81  });82});8384describe('Check ERC721 token URI for NFT', () => {85  let donor: IKeyringPair;8687  before(async function() {88    await usingEthPlaygrounds(async (_helper, privateKey) => {89      donor = await privateKey({filename: __filename});90    });91  });9293  async function setup(helper: EthUniqueHelper, baseUri: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {94    const owner = await helper.eth.createAccountWithBalance(donor);95    const receiver = helper.eth.createAccount();9697    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', 'a', 'b', baseUri);98    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);99100    const result = await contract.methods.mint(receiver).send();101    const tokenId = result.events.Transfer.returnValues.tokenId;102    expect(tokenId).to.be.equal('1');103104    if (propertyKey && propertyValue) {105      // Set URL or suffix106      await contract.methods.setProperties(tokenId, [{key: propertyKey, value: Buffer.from(propertyValue)}]).send();107    }108109    const event = result.events.Transfer;110    expect(event.address).to.be.equal(collectionAddress);111    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');112    expect(event.returnValues.to).to.be.equal(receiver);113    expect(event.returnValues.tokenId).to.be.equal(tokenId);114115    return {contract, nextTokenId: tokenId};116  }117118  itEth('Empty tokenURI', async ({helper}) => {119    const {contract, nextTokenId} = await setup(helper, '');120    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');121  });122123  itEth('TokenURI from url', async ({helper}) => {124    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URI', 'Token URI');125    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');126  });127128  itEth('TokenURI from baseURI', async ({helper}) => {129    const {contract, nextTokenId} = await setup(helper, 'BaseURI_');130    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_');131  });132133  itEth('TokenURI from baseURI + suffix', async ({helper}) => {134    const suffix = '/some/suffix';135    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URISuffix', suffix);136    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);137  });138});139140describe('NFT: Plain calls', () => {141  let donor: IKeyringPair;142  let minter: IKeyringPair;143  let bob: IKeyringPair;144  let charlie: IKeyringPair;145146  before(async function() {147    await usingEthPlaygrounds(async (helper, privateKey) => {148      donor = await privateKey({filename: __filename});149      [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);150    });151  });152153  itEth('Can perform mint() & get crossOwner()', async ({helper}) => {154    const owner = await helper.eth.createAccountWithBalance(donor);155    const receiver = helper.eth.createAccount();156157    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', '');158    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);159160    const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();161    const tokenId = result.events.Transfer.returnValues.tokenId;162    expect(tokenId).to.be.equal('1');163164    const event = result.events.Transfer;165    expect(event.address).to.be.equal(collectionAddress);166    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');167    expect(event.returnValues.to).to.be.equal(receiver);168169    expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');170    console.log(await contract.methods.crossOwnerOf(tokenId).call());171    expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']);172    // TODO: this wont work right now, need release 919000 first173    // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();174    // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();175    // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);176  });177178  //TODO: CORE-302 add eth methods179  itEth.skip('Can perform mintBulk()', async ({helper}) => {180    const caller = await helper.eth.createAccountWithBalance(donor);181    const receiver = helper.eth.createAccount();182183    const collection = await helper.nft.mintCollection(minter);184    await collection.addAdmin(minter, {Ethereum: caller});185186    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);187    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);188    {189      const bulkSize = 3;190      const nextTokenId = await contract.methods.nextTokenId().call();191      expect(nextTokenId).to.be.equal('1');192      const result = await contract.methods.mintBulkWithTokenURI(193        receiver,194        Array.from({length: bulkSize}, (_, i) => (195          [+nextTokenId + i, `Test URI ${i}`]196        )),197      ).send({from: caller});198199      const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.tokenId - b.returnValues.tokenId);200      for (let i = 0; i < bulkSize; i++) {201        const event = events[i];202        expect(event.address).to.equal(collectionAddress);203        expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');204        expect(event.returnValues.to).to.equal(receiver);205        expect(event.returnValues.tokenId).to.equal(`${+nextTokenId + i}`);206207        expect(await contract.methods.tokenURI(+nextTokenId + i).call()).to.be.equal(`Test URI ${i}`);208      }209    }210  });211212  itEth('Can perform burn()', async ({helper}) => {213    const caller = await helper.eth.createAccountWithBalance(donor);214215    const collection = await helper.nft.mintCollection(minter, {});216    const {tokenId} = await collection.mintToken(minter, {Ethereum: caller});217218    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);219    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);220221    {222      const result = await contract.methods.burn(tokenId).send({from: caller});223224      const event = result.events.Transfer;225      expect(event.address).to.be.equal(collectionAddress);226      expect(event.returnValues.from).to.be.equal(caller);227      expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');228      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);229    }230  });231232  itEth('Can perform approve()', async ({helper}) => {233    const owner = await helper.eth.createAccountWithBalance(donor);234    const spender = helper.eth.createAccount();235236    const collection = await helper.nft.mintCollection(minter, {});237    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});238239    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);240    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);241242    {243      const result = await contract.methods.approve(spender, tokenId).send({from: owner});244245      const event = result.events.Approval;246      expect(event.address).to.be.equal(collectionAddress);247      expect(event.returnValues.owner).to.be.equal(owner);248      expect(event.returnValues.approved).to.be.equal(spender);249      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);250    }251  });252253  itEth('Can perform burnFromCross()', async ({helper}) => {254    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});255256    const owner = bob;257    const spender = await helper.eth.createAccountWithBalance(donor, 100n);258259    const token = await collection.mintToken(minter, {Substrate: owner.address});260261    const address = helper.ethAddress.fromCollectionId(collection.collectionId);262    const contract = helper.ethNativeContract.collection(address, 'nft');263264    {265      await token.approve(owner, {Ethereum: spender});266      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);267      const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender});268      const events = result.events.Transfer;269270      expect(events).to.be.like({271        address,272        event: 'Transfer',273        returnValues: {274          from: helper.address.substrateToEth(owner.address),275          to: '0x0000000000000000000000000000000000000000',276          tokenId: token.tokenId.toString(),277        },278      });279    }280  });281282  itEth('Can perform approveCross()', async ({helper}) => {283    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});284285    const owner = await helper.eth.createAccountWithBalance(donor, 100n);286    const receiver = charlie;287288    const token = await collection.mintToken(minter, {Ethereum: owner});289290    const address = helper.ethAddress.fromCollectionId(collection.collectionId);291    const contract = helper.ethNativeContract.collection(address, 'nft');292293    {294      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);295      const result = await contract.methods.approveCross(recieverCross, token.tokenId).send({from: owner});296      const event = result.events.Approval;297      expect(event).to.be.like({298        address: helper.ethAddress.fromCollectionId(collection.collectionId),299        event: 'Approval',300        returnValues: {301          owner,302          approved: helper.address.substrateToEth(receiver.address),303          tokenId: token.tokenId.toString(),304        },305      });306    }307  });308309  itEth('Can perform transferFrom()', async ({helper}) => {310    const owner = await helper.eth.createAccountWithBalance(donor);311    const spender = await helper.eth.createAccountWithBalance(donor);312    const receiver = helper.eth.createAccount();313314    const collection = await helper.nft.mintCollection(minter, {});315    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});316317    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);318    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);319320    await contract.methods.approve(spender, tokenId).send({from: owner});321322    {323      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});324325      const event = result.events.Transfer;326      expect(event.address).to.be.equal(collectionAddress);327      expect(event.returnValues.from).to.be.equal(owner);328      expect(event.returnValues.to).to.be.equal(receiver);329      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);330    }331332    {333      const balance = await contract.methods.balanceOf(receiver).call();334      expect(+balance).to.equal(1);335    }336337    {338      const balance = await contract.methods.balanceOf(owner).call();339      expect(+balance).to.equal(0);340    }341  });342343  itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {344    const minter = await privateKey('//Alice');345    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});346347    const owner = await privateKey('//Bob');348    const spender = await helper.eth.createAccountWithBalance(donor);349    const receiver = await privateKey('//Charlie');350351    const token = await collection.mintToken(minter, {Substrate: owner.address});352353    const address = helper.ethAddress.fromCollectionId(collection.collectionId);354    const contract = helper.ethNativeContract.collection(address, 'nft');355356    await token.approve(owner, {Ethereum: spender});357358    {359      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);360      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);361      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});362      const event = result.events.Transfer;363      expect(event).to.be.like({364        address: helper.ethAddress.fromCollectionId(collection.collectionId),365        event: 'Transfer',366        returnValues: {367          from: helper.address.substrateToEth(owner.address),368          to: helper.address.substrateToEth(receiver.address),369          tokenId: token.tokenId.toString(),370        },371      });372    }373374    expect(await token.getOwner()).to.be.like({Substrate: receiver.address});375  });376377  itEth('Can perform transfer()', async ({helper}) => {378    const collection = await helper.nft.mintCollection(minter, {});379    const owner = await helper.eth.createAccountWithBalance(donor);380    const receiver = helper.eth.createAccount();381382    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});383384    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);385    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);386387    {388      const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});389390      const event = result.events.Transfer;391      expect(event.address).to.be.equal(collectionAddress);392      expect(event.returnValues.from).to.be.equal(owner);393      expect(event.returnValues.to).to.be.equal(receiver);394      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);395    }396397    {398      const balance = await contract.methods.balanceOf(owner).call();399      expect(+balance).to.equal(0);400    }401402    {403      const balance = await contract.methods.balanceOf(receiver).call();404      expect(+balance).to.equal(1);405    }406  });407  408  itEth('Can perform transferCross()', async ({helper}) => {409    const collection = await helper.nft.mintCollection(minter, {});410    const owner = await helper.eth.createAccountWithBalance(donor);411    const receiver = await helper.eth.createAccountWithBalance(donor);412    const to = helper.ethCrossAccount.fromAddress(receiver);413    const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter);414    415    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});416417    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);418    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);419420    {421      const result = await contract.methods.transferCross(to, tokenId).send({from: owner});422423      const event = result.events.Transfer;424      expect(event.address).to.be.equal(collectionAddress);425      expect(event.returnValues.from).to.be.equal(owner);426      expect(event.returnValues.to).to.be.equal(receiver);427      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);428    }429430    {431      const balance = await contract.methods.balanceOf(owner).call();432      expect(+balance).to.equal(0);433    }434435    {436      const balance = await contract.methods.balanceOf(receiver).call();437      expect(+balance).to.equal(1);438    }439    440    {441      const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver});442      443444      const event = substrateResult.events.Transfer;445      expect(event.address).to.be.equal(collectionAddress);446      expect(event.returnValues.from).to.be.equal(receiver);447      expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));448      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);449    }450451    {452      const balance = await contract.methods.balanceOf(receiver).call();453      expect(+balance).to.equal(0);454    }455456    {457      const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});458      expect(balance).to.be.contain(tokenId);459    }460  });461});462463describe('NFT: Fees', () => {464  let donor: IKeyringPair;465  let alice: IKeyringPair;466  let bob: IKeyringPair;467  let charlie: IKeyringPair;468469  before(async function() {470    await usingEthPlaygrounds(async (helper, privateKey) => {471      donor = await privateKey({filename: __filename});472      [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);473    });474  });475476  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {477    const owner = await helper.eth.createAccountWithBalance(donor);478    const spender = helper.eth.createAccount();479480    const collection = await helper.nft.mintCollection(alice, {});481    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});482483    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);484485    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));486    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));487  });488489  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {490    const owner = await helper.eth.createAccountWithBalance(donor);491    const spender = await helper.eth.createAccountWithBalance(donor);492493    const collection = await helper.nft.mintCollection(alice, {});494    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});495496    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);497498    await contract.methods.approve(spender, tokenId).send({from: owner});499500    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));501    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));502  });503504  itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {505    const collectionMinter = alice;506    const owner = bob;507    const receiver = charlie;508    const collection = await helper.nft.mintCollection(collectionMinter, {name: 'A', description: 'B', tokenPrefix: 'C'});509510    const spender = await helper.eth.createAccountWithBalance(donor, 100n);511512    const token = await collection.mintToken(collectionMinter, {Substrate: owner.address});513514    const address = helper.ethAddress.fromCollectionId(collection.collectionId);515    const contract = helper.ethNativeContract.collection(address, 'nft');516517    await token.approve(owner, {Ethereum: spender});518519    {520      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);521      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);522      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});523      const event = result.events.Transfer;524      expect(event).to.be.like({525        address: helper.ethAddress.fromCollectionId(collection.collectionId),526        event: 'Transfer',527        returnValues: {528          from: helper.address.substrateToEth(owner.address),529          to: helper.address.substrateToEth(receiver.address),530          tokenId: token.tokenId.toString(),531        },532      });533    }534535    expect(await token.getOwner()).to.be.like({Substrate: receiver.address});536  });537538  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {539    const owner = await helper.eth.createAccountWithBalance(donor);540    const receiver = helper.eth.createAccount();541542    const collection = await helper.nft.mintCollection(alice, {});543    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});544545    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);546547    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));548    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));549  });550});551552describe('NFT: Substrate calls', () => {553  let donor: IKeyringPair;554  let alice: IKeyringPair;555556  before(async function() {557    await usingEthPlaygrounds(async (helper, privateKey) => {558      donor = await privateKey({filename: __filename});559      [alice] = await helper.arrange.createAccounts([20n], donor);560    });561  });562563  itEth('Events emitted for mint()', async ({helper}) => {564    const collection = await helper.nft.mintCollection(alice, {});565    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);566    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');567568    const events: any = [];569    contract.events.allEvents((_: any, event: any) => {570      events.push(event);571    });572573    const {tokenId} = await collection.mintToken(alice);574    if (events.length == 0) await helper.wait.newBlocks(1);575    const event = events[0];576577    expect(event.event).to.be.equal('Transfer');578    expect(event.address).to.be.equal(collectionAddress);579    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');580    expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));581    expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());582  });583584  itEth('Events emitted for burn()', async ({helper}) => {585    const collection = await helper.nft.mintCollection(alice, {});586    const token = await collection.mintToken(alice);587588    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);589    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');590591    const events: any = [];592    contract.events.allEvents((_: any, event: any) => {593      events.push(event);594    });595596    await token.burn(alice);597    if (events.length == 0) await helper.wait.newBlocks(1);598    const event = events[0];599600    expect(event.event).to.be.equal('Transfer');601    expect(event.address).to.be.equal(collectionAddress);602    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));603    expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');604    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());605  });606607  itEth('Events emitted for approve()', async ({helper}) => {608    const receiver = helper.eth.createAccount();609610    const collection = await helper.nft.mintCollection(alice, {});611    const token = await collection.mintToken(alice);612613    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);614    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');615616    const events: any = [];617    contract.events.allEvents((_: any, event: any) => {618      events.push(event);619    });620621    await token.approve(alice, {Ethereum: receiver});622    if (events.length == 0) await helper.wait.newBlocks(1);623    const event = events[0];624625    expect(event.event).to.be.equal('Approval');626    expect(event.address).to.be.equal(collectionAddress);627    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));628    expect(event.returnValues.approved).to.be.equal(receiver);629    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());630  });631632  itEth('Events emitted for transferFrom()', async ({helper}) => {633    const [bob] = await helper.arrange.createAccounts([10n], donor);634    const receiver = helper.eth.createAccount();635636    const collection = await helper.nft.mintCollection(alice, {});637    const token = await collection.mintToken(alice);638    await token.approve(alice, {Substrate: bob.address});639640    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);641    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');642643    const events: any = [];644    contract.events.allEvents((_: any, event: any) => {645      events.push(event);646    });647648    await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});649650    if (events.length == 0) await helper.wait.newBlocks(1);651    const event = events[0];652653    expect(event.address).to.be.equal(collectionAddress);654    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));655    expect(event.returnValues.to).to.be.equal(receiver);656    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);657  });658659  itEth('Events emitted for transfer()', async ({helper}) => {660    const receiver = helper.eth.createAccount();661662    const collection = await helper.nft.mintCollection(alice, {});663    const token = await collection.mintToken(alice);664665    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);666    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');667668    const events: any = [];669    contract.events.allEvents((_: any, event: any) => {670      events.push(event);671    });672673    await token.transfer(alice, {Ethereum: receiver});674675    if (events.length == 0) await helper.wait.newBlocks(1);676    const event = events[0];677678    expect(event.address).to.be.equal(collectionAddress);679    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));680    expect(event.returnValues.to).to.be.equal(receiver);681    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);682  });683});684685describe('Common metadata', () => {686  let donor: IKeyringPair;687  let alice: IKeyringPair;688689  before(async function() {690    await usingEthPlaygrounds(async (helper, privateKey) => {691      donor = await privateKey({filename: __filename});692      [alice] = await helper.arrange.createAccounts([20n], donor);693    });694  });695696  itEth('Returns collection name', async ({helper}) => {697    const caller = await helper.eth.createAccountWithBalance(donor);698    const tokenPropertyPermissions = [{699      key: 'URI',700      permission: {701        mutable: true,702        collectionAdmin: true,703        tokenOwner: false,704      },705    }];706    const collection = await helper.nft.mintCollection(707      alice,708      {709        name: 'oh River',710        tokenPrefix: 'CHANGE',711        properties: [{key: 'ERC721Metadata', value: '1'}],712        tokenPropertyPermissions,713      },714    );715716    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);717    const name = await contract.methods.name().call();718    expect(name).to.equal('oh River');719  });720721  itEth('Returns symbol name', async ({helper}) => {722    const caller = await helper.eth.createAccountWithBalance(donor);723    const tokenPropertyPermissions = [{724      key: 'URI',725      permission: {726        mutable: true,727        collectionAdmin: true,728        tokenOwner: false,729      },730    }];731    const collection = await helper.nft.mintCollection(732      alice,733      {734        name: 'oh River',735        tokenPrefix: 'CHANGE',736        properties: [{key: 'ERC721Metadata', value: '1'}],737        tokenPropertyPermissions,738      },739    );740741    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);742    const symbol = await contract.methods.symbol().call();743    expect(symbol).to.equal('CHANGE');744  });745});
after · tests/src/eth/nonFungible.test.ts
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 {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util';18import {IKeyringPair} from '@polkadot/types/types';19import {Contract} from 'web3-eth-contract';202122describe('NFT: Information getting', () => {23  let donor: IKeyringPair;24  let alice: IKeyringPair;2526  before(async function() {27    await usingEthPlaygrounds(async (helper, privateKey) => {28      donor = await privateKey({filename: __filename});29      [alice] = await helper.arrange.createAccounts([10n], donor);30    });31  });3233  itEth('totalSupply', async ({helper}) => {34    const collection = await helper.nft.mintCollection(alice, {});35    await collection.mintToken(alice);3637    const caller = await helper.eth.createAccountWithBalance(donor);3839    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);40    const totalSupply = await contract.methods.totalSupply().call();4142    expect(totalSupply).to.equal('1');43  });4445  itEth('balanceOf', async ({helper}) => {46    const collection = await helper.nft.mintCollection(alice, {});47    const caller = await helper.eth.createAccountWithBalance(donor);4849    await collection.mintToken(alice, {Ethereum: caller});50    await collection.mintToken(alice, {Ethereum: caller});51    await collection.mintToken(alice, {Ethereum: caller});5253    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);54    const balance = await contract.methods.balanceOf(caller).call();5556    expect(balance).to.equal('3');57  });5859  itEth('ownerOf', async ({helper}) => {60    const collection = await helper.nft.mintCollection(alice, {});61    const caller = await helper.eth.createAccountWithBalance(donor);6263    const token = await collection.mintToken(alice, {Ethereum: caller});6465    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);6667    const owner = await contract.methods.ownerOf(token.tokenId).call();6869    expect(owner).to.equal(caller);70  });7172  itEth('name/symbol is available regardless of ERC721Metadata support', async ({helper}) => {73    const collection = await helper.nft.mintCollection(alice, {name: 'test', tokenPrefix: 'TEST'});74    const caller = helper.eth.createAccount();7576    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);7778    expect(await contract.methods.name().call()).to.equal('test');79    expect(await contract.methods.symbol().call()).to.equal('TEST');80  });81});8283describe('Check ERC721 token URI for NFT', () => {84  let donor: IKeyringPair;8586  before(async function() {87    await usingEthPlaygrounds(async (_helper, privateKey) => {88      donor = await privateKey({filename: __filename});89    });90  });9192  async function setup(helper: EthUniqueHelper, baseUri: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {93    const owner = await helper.eth.createAccountWithBalance(donor);94    const receiver = helper.eth.createAccount();9596    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', 'a', 'b', baseUri);97    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);9899    const result = await contract.methods.mint(receiver).send();100    const tokenId = result.events.Transfer.returnValues.tokenId;101    expect(tokenId).to.be.equal('1');102103    if (propertyKey && propertyValue) {104      // Set URL or suffix105      await contract.methods.setProperties(tokenId, [{key: propertyKey, value: Buffer.from(propertyValue)}]).send();106    }107108    const event = result.events.Transfer;109    expect(event.address).to.be.equal(collectionAddress);110    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');111    expect(event.returnValues.to).to.be.equal(receiver);112    expect(event.returnValues.tokenId).to.be.equal(tokenId);113114    return {contract, nextTokenId: tokenId};115  }116117  itEth('Empty tokenURI', async ({helper}) => {118    const {contract, nextTokenId} = await setup(helper, '');119    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');120  });121122  itEth('TokenURI from url', async ({helper}) => {123    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URI', 'Token URI');124    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');125  });126127  itEth('TokenURI from baseURI', async ({helper}) => {128    const {contract, nextTokenId} = await setup(helper, 'BaseURI_');129    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_');130  });131132  itEth('TokenURI from baseURI + suffix', async ({helper}) => {133    const suffix = '/some/suffix';134    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URISuffix', suffix);135    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);136  });137});138139describe('NFT: Plain calls', () => {140  let donor: IKeyringPair;141  let minter: IKeyringPair;142  let bob: IKeyringPair;143  let charlie: IKeyringPair;144145  before(async function() {146    await usingEthPlaygrounds(async (helper, privateKey) => {147      donor = await privateKey({filename: __filename});148      [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);149    });150  });151152  itEth('Can perform mint() & get crossOwner()', async ({helper}) => {153    const owner = await helper.eth.createAccountWithBalance(donor);154    const receiver = helper.eth.createAccount();155156    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', '');157    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);158159    const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();160    const tokenId = result.events.Transfer.returnValues.tokenId;161    expect(tokenId).to.be.equal('1');162163    const event = result.events.Transfer;164    expect(event.address).to.be.equal(collectionAddress);165    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');166    expect(event.returnValues.to).to.be.equal(receiver);167168    expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');169    console.log(await contract.methods.crossOwnerOf(tokenId).call());170    expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']);171    // TODO: this wont work right now, need release 919000 first172    // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();173    // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();174    // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);175  });176177  //TODO: CORE-302 add eth methods178  itEth.skip('Can perform mintBulk()', async ({helper}) => {179    const caller = await helper.eth.createAccountWithBalance(donor);180    const receiver = helper.eth.createAccount();181182    const collection = await helper.nft.mintCollection(minter);183    await collection.addAdmin(minter, {Ethereum: caller});184185    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);186    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);187    {188      const bulkSize = 3;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        Array.from({length: bulkSize}, (_, i) => (194          [+nextTokenId + i, `Test URI ${i}`]195        )),196      ).send({from: caller});197198      const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.tokenId - b.returnValues.tokenId);199      for (let i = 0; i < bulkSize; i++) {200        const event = events[i];201        expect(event.address).to.equal(collectionAddress);202        expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');203        expect(event.returnValues.to).to.equal(receiver);204        expect(event.returnValues.tokenId).to.equal(`${+nextTokenId + i}`);205206        expect(await contract.methods.tokenURI(+nextTokenId + i).call()).to.be.equal(`Test URI ${i}`);207      }208    }209  });210211  itEth('Can perform burn()', async ({helper}) => {212    const caller = await helper.eth.createAccountWithBalance(donor);213214    const collection = await helper.nft.mintCollection(minter, {});215    const {tokenId} = await collection.mintToken(minter, {Ethereum: caller});216217    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);218    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);219220    {221      const result = await contract.methods.burn(tokenId).send({from: caller});222223      const event = result.events.Transfer;224      expect(event.address).to.be.equal(collectionAddress);225      expect(event.returnValues.from).to.be.equal(caller);226      expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');227      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);228    }229  });230231  itEth('Can perform approve()', async ({helper}) => {232    const owner = await helper.eth.createAccountWithBalance(donor);233    const spender = helper.eth.createAccount();234235    const collection = await helper.nft.mintCollection(minter, {});236    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});237238    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);239    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);240241    {242      const result = await contract.methods.approve(spender, tokenId).send({from: owner});243244      const event = result.events.Approval;245      expect(event.address).to.be.equal(collectionAddress);246      expect(event.returnValues.owner).to.be.equal(owner);247      expect(event.returnValues.approved).to.be.equal(spender);248      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);249    }250  });251252  itEth('Can perform burnFromCross()', async ({helper}) => {253    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});254255    const owner = bob;256    const spender = await helper.eth.createAccountWithBalance(donor, 100n);257258    const token = await collection.mintToken(minter, {Substrate: owner.address});259260    const address = helper.ethAddress.fromCollectionId(collection.collectionId);261    const contract = helper.ethNativeContract.collection(address, 'nft');262263    {264      await token.approve(owner, {Ethereum: spender});265      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);266      const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender});267      const events = result.events.Transfer;268269      expect(events).to.be.like({270        address,271        event: 'Transfer',272        returnValues: {273          from: helper.address.substrateToEth(owner.address),274          to: '0x0000000000000000000000000000000000000000',275          tokenId: token.tokenId.toString(),276        },277      });278    }279  });280281  itEth('Can perform approveCross()', async ({helper}) => {282    // arrange: create accounts283    const owner = await helper.eth.createAccountWithBalance(donor, 100n);284    const ownerCross = helper.ethCrossAccount.fromAddress(owner);285    const receiverSub = charlie;286    const recieverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);287    const receiverEth = await helper.eth.createAccountWithBalance(donor, 100n);288    const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);289290    // arrange: create collection and tokens:291    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});292    const token1 = await collection.mintToken(minter, {Ethereum: owner});293    const token2 = await collection.mintToken(minter, {Ethereum: owner});294295    const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');296297    // Can approveCross substrate and ethereum address:298    const resultSub = await collectionEvm.methods.approveCross(recieverCrossSub, token1.tokenId).send({from: owner});299    const resultEth = await collectionEvm.methods.approveCross(receiverCrossEth, token2.tokenId).send({from: owner});300    const eventSub = resultSub.events.Approval;301    const eventEth = resultEth.events.Approval;302    expect(eventSub).to.be.like({303      address: helper.ethAddress.fromCollectionId(collection.collectionId),304      event: 'Approval',305      returnValues: {306        owner,307        approved: helper.address.substrateToEth(receiverSub.address),308        tokenId: token1.tokenId.toString(),309      },310    });311    expect(eventEth).to.be.like({312      address: helper.ethAddress.fromCollectionId(collection.collectionId),313      event: 'Approval',314      returnValues: {315        owner,316        approved: receiverEth,317        tokenId: token2.tokenId.toString(),318      },319    });320321    // Substrate address can transferFrom approved tokens:322    await helper.nft.transferTokenFrom(receiverSub, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiverSub.address});323    expect(await helper.nft.getTokenOwner(collection.collectionId, token1.tokenId)).to.deep.eq({Substrate: receiverSub.address});324    // Ethereum address can transferFromCross approved tokens:325    await collectionEvm.methods.transferFromCross(ownerCross, receiverCrossEth, token2.tokenId).send({from: receiverEth});326    expect(await helper.nft.getTokenOwner(collection.collectionId, token2.tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});327  });328329  itEth('Can perform transferFrom()', async ({helper}) => {330    const owner = await helper.eth.createAccountWithBalance(donor);331    const spender = await helper.eth.createAccountWithBalance(donor);332    const receiver = helper.eth.createAccount();333334    const collection = await helper.nft.mintCollection(minter, {});335    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});336337    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);338    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);339340    await contract.methods.approve(spender, tokenId).send({from: owner});341342    {343      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});344345      const event = result.events.Transfer;346      expect(event.address).to.be.equal(collectionAddress);347      expect(event.returnValues.from).to.be.equal(owner);348      expect(event.returnValues.to).to.be.equal(receiver);349      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);350    }351352    {353      const balance = await contract.methods.balanceOf(receiver).call();354      expect(+balance).to.equal(1);355    }356357    {358      const balance = await contract.methods.balanceOf(owner).call();359      expect(+balance).to.equal(0);360    }361  });362363  itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {364    const minter = await privateKey('//Alice');365    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});366367    const owner = await privateKey('//Bob');368    const spender = await helper.eth.createAccountWithBalance(donor);369    const receiver = await privateKey('//Charlie');370371    const token = await collection.mintToken(minter, {Substrate: owner.address});372373    const address = helper.ethAddress.fromCollectionId(collection.collectionId);374    const contract = helper.ethNativeContract.collection(address, 'nft');375376    await token.approve(owner, {Ethereum: spender});377378    {379      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);380      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);381      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});382      const event = result.events.Transfer;383      expect(event).to.be.like({384        address: helper.ethAddress.fromCollectionId(collection.collectionId),385        event: 'Transfer',386        returnValues: {387          from: helper.address.substrateToEth(owner.address),388          to: helper.address.substrateToEth(receiver.address),389          tokenId: token.tokenId.toString(),390        },391      });392    }393394    expect(await token.getOwner()).to.be.like({Substrate: receiver.address});395  });396397  itEth('Can perform transfer()', async ({helper}) => {398    const collection = await helper.nft.mintCollection(minter, {});399    const owner = await helper.eth.createAccountWithBalance(donor);400    const receiver = helper.eth.createAccount();401402    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});403404    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);405    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);406407    {408      const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});409410      const event = result.events.Transfer;411      expect(event.address).to.be.equal(collectionAddress);412      expect(event.returnValues.from).to.be.equal(owner);413      expect(event.returnValues.to).to.be.equal(receiver);414      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);415    }416417    {418      const balance = await contract.methods.balanceOf(owner).call();419      expect(+balance).to.equal(0);420    }421422    {423      const balance = await contract.methods.balanceOf(receiver).call();424      expect(+balance).to.equal(1);425    }426  });427  428  itEth('Can perform transferCross()', async ({helper}) => {429    const collection = await helper.nft.mintCollection(minter, {});430    const owner = await helper.eth.createAccountWithBalance(donor);431    const receiver = await helper.eth.createAccountWithBalance(donor);432    const to = helper.ethCrossAccount.fromAddress(receiver);433    const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter);434    435    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});436437    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);438    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);439440    {441      const result = await contract.methods.transferCross(to, tokenId).send({from: owner});442443      const event = result.events.Transfer;444      expect(event.address).to.be.equal(collectionAddress);445      expect(event.returnValues.from).to.be.equal(owner);446      expect(event.returnValues.to).to.be.equal(receiver);447      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);448    }449450    {451      const balance = await contract.methods.balanceOf(owner).call();452      expect(+balance).to.equal(0);453    }454455    {456      const balance = await contract.methods.balanceOf(receiver).call();457      expect(+balance).to.equal(1);458    }459    460    {461      const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver});462      463464      const event = substrateResult.events.Transfer;465      expect(event.address).to.be.equal(collectionAddress);466      expect(event.returnValues.from).to.be.equal(receiver);467      expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));468      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);469    }470471    {472      const balance = await contract.methods.balanceOf(receiver).call();473      expect(+balance).to.equal(0);474    }475476    {477      const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});478      expect(balance).to.be.contain(tokenId);479    }480  });481});482483describe('NFT: Fees', () => {484  let donor: IKeyringPair;485  let alice: IKeyringPair;486  let bob: IKeyringPair;487  let charlie: IKeyringPair;488489  before(async function() {490    await usingEthPlaygrounds(async (helper, privateKey) => {491      donor = await privateKey({filename: __filename});492      [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);493    });494  });495496  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {497    const owner = await helper.eth.createAccountWithBalance(donor);498    const spender = helper.eth.createAccount();499500    const collection = await helper.nft.mintCollection(alice, {});501    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});502503    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);504505    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));506    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));507  });508509  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {510    const owner = await helper.eth.createAccountWithBalance(donor);511    const spender = await helper.eth.createAccountWithBalance(donor);512513    const collection = await helper.nft.mintCollection(alice, {});514    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});515516    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);517518    await contract.methods.approve(spender, tokenId).send({from: owner});519520    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));521    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));522  });523524  itEth('Can perform transferFromCross()', async ({helper}) => {525    const collectionMinter = alice;526    const owner = bob;527    const receiver = charlie;528    const collection = await helper.nft.mintCollection(collectionMinter, {name: 'A', description: 'B', tokenPrefix: 'C'});529530    const spender = await helper.eth.createAccountWithBalance(donor, 100n);531532    const token = await collection.mintToken(collectionMinter, {Substrate: owner.address});533534    const address = helper.ethAddress.fromCollectionId(collection.collectionId);535    const contract = helper.ethNativeContract.collection(address, 'nft');536537    await token.approve(owner, {Ethereum: spender});538539    {540      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);541      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);542      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});543      const event = result.events.Transfer;544      expect(event).to.be.like({545        address: helper.ethAddress.fromCollectionId(collection.collectionId),546        event: 'Transfer',547        returnValues: {548          from: helper.address.substrateToEth(owner.address),549          to: helper.address.substrateToEth(receiver.address),550          tokenId: token.tokenId.toString(),551        },552      });553    }554555    expect(await token.getOwner()).to.be.like({Substrate: receiver.address});556  });557558  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {559    const owner = await helper.eth.createAccountWithBalance(donor);560    const receiver = helper.eth.createAccount();561562    const collection = await helper.nft.mintCollection(alice, {});563    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});564565    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);566567    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));568    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));569  });570});571572describe('NFT: Substrate calls', () => {573  let donor: IKeyringPair;574  let alice: IKeyringPair;575576  before(async function() {577    await usingEthPlaygrounds(async (helper, privateKey) => {578      donor = await privateKey({filename: __filename});579      [alice] = await helper.arrange.createAccounts([20n], donor);580    });581  });582583  itEth('Events emitted for mint()', async ({helper}) => {584    const collection = await helper.nft.mintCollection(alice, {});585    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);586    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');587588    const events: any = [];589    contract.events.allEvents((_: any, event: any) => {590      events.push(event);591    });592593    const {tokenId} = await collection.mintToken(alice);594    if (events.length == 0) await helper.wait.newBlocks(1);595    const event = events[0];596597    expect(event.event).to.be.equal('Transfer');598    expect(event.address).to.be.equal(collectionAddress);599    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');600    expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));601    expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());602  });603604  itEth('Events emitted for burn()', async ({helper}) => {605    const collection = await helper.nft.mintCollection(alice, {});606    const token = await collection.mintToken(alice);607608    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);609    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');610611    const events: any = [];612    contract.events.allEvents((_: any, event: any) => {613      events.push(event);614    });615616    await token.burn(alice);617    if (events.length == 0) await helper.wait.newBlocks(1);618    const event = events[0];619620    expect(event.event).to.be.equal('Transfer');621    expect(event.address).to.be.equal(collectionAddress);622    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));623    expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');624    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());625  });626627  itEth('Events emitted for approve()', async ({helper}) => {628    const receiver = helper.eth.createAccount();629630    const collection = await helper.nft.mintCollection(alice, {});631    const token = await collection.mintToken(alice);632633    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);634    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');635636    const events: any = [];637    contract.events.allEvents((_: any, event: any) => {638      events.push(event);639    });640641    await token.approve(alice, {Ethereum: receiver});642    if (events.length == 0) await helper.wait.newBlocks(1);643    const event = events[0];644645    expect(event.event).to.be.equal('Approval');646    expect(event.address).to.be.equal(collectionAddress);647    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));648    expect(event.returnValues.approved).to.be.equal(receiver);649    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());650  });651652  itEth('Events emitted for transferFrom()', async ({helper}) => {653    const [bob] = await helper.arrange.createAccounts([10n], donor);654    const receiver = helper.eth.createAccount();655656    const collection = await helper.nft.mintCollection(alice, {});657    const token = await collection.mintToken(alice);658    await token.approve(alice, {Substrate: bob.address});659660    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);661    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');662663    const events: any = [];664    contract.events.allEvents((_: any, event: any) => {665      events.push(event);666    });667668    await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});669670    if (events.length == 0) await helper.wait.newBlocks(1);671    const event = events[0];672673    expect(event.address).to.be.equal(collectionAddress);674    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));675    expect(event.returnValues.to).to.be.equal(receiver);676    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);677  });678679  itEth('Events emitted for transfer()', async ({helper}) => {680    const receiver = helper.eth.createAccount();681682    const collection = await helper.nft.mintCollection(alice, {});683    const token = await collection.mintToken(alice);684685    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);686    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');687688    const events: any = [];689    contract.events.allEvents((_: any, event: any) => {690      events.push(event);691    });692693    await token.transfer(alice, {Ethereum: receiver});694695    if (events.length == 0) await helper.wait.newBlocks(1);696    const event = events[0];697698    expect(event.address).to.be.equal(collectionAddress);699    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));700    expect(event.returnValues.to).to.be.equal(receiver);701    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);702  });703});704705describe('Common metadata', () => {706  let donor: IKeyringPair;707  let alice: IKeyringPair;708709  before(async function() {710    await usingEthPlaygrounds(async (helper, privateKey) => {711      donor = await privateKey({filename: __filename});712      [alice] = await helper.arrange.createAccounts([20n], donor);713    });714  });715716  itEth('Returns collection name', async ({helper}) => {717    const caller = await helper.eth.createAccountWithBalance(donor);718    const tokenPropertyPermissions = [{719      key: 'URI',720      permission: {721        mutable: true,722        collectionAdmin: true,723        tokenOwner: false,724      },725    }];726    const collection = await helper.nft.mintCollection(727      alice,728      {729        name: 'oh River',730        tokenPrefix: 'CHANGE',731        properties: [{key: 'ERC721Metadata', value: '1'}],732        tokenPropertyPermissions,733      },734    );735736    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);737    const name = await contract.methods.name().call();738    expect(name).to.equal('oh River');739  });740741  itEth('Returns symbol name', async ({helper}) => {742    const caller = await helper.eth.createAccountWithBalance(donor);743    const tokenPropertyPermissions = [{744      key: 'URI',745      permission: {746        mutable: true,747        collectionAdmin: true,748        tokenOwner: false,749      },750    }];751    const collection = await helper.nft.mintCollection(752      alice,753      {754        name: 'oh River',755        tokenPrefix: 'CHANGE',756        properties: [{key: 'ERC721Metadata', value: '1'}],757        tokenPropertyPermissions,758      },759    );760761    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);762    const symbol = await contract.methods.symbol().call();763    expect(symbol).to.equal('CHANGE');764  });765});