git.delta.rocks / unique-network / refs/commits / 8c823a622616

difftreelog

Add reaffirm approved address test

Max Andreev2022-12-01parent: #79fd5e9.patch.diff
in: master

1 file changed

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';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'});254    const ownerSub = bob;255    const ownerCross = helper.ethCrossAccount.fromKeyringPair(ownerSub);256    const ownerEth = await helper.eth.createAccountWithBalance(donor, 100n);257258    const burnerEth = await helper.eth.createAccountWithBalance(donor, 100n);259    const burnerCrossEth = helper.ethCrossAccount.fromAddress(burnerEth);260261    const token1 = await collection.mintToken(minter, {Substrate: ownerSub.address});262    const token2 = await collection.mintToken(minter, {Ethereum: ownerEth});263264    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);265    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft');266267    // Approve tokens from substrate and ethereum:268    await token1.approve(ownerSub, {Ethereum: burnerEth});269    await collectionEvm.methods.approveCross(burnerCrossEth, token2.tokenId).send({from: ownerEth});270271    // can burnFromCross:272    const result1 = await collectionEvm.methods.burnFromCross(ownerCross, token1.tokenId).send({from: burnerEth});273    // FIXME Error No Permission?:274    const result2 = await collectionEvm.methods.burnFromCross(ownerCross, token2.tokenId).send({from: burnerEth});275    const events1 = result1.events.Transfer;276    const events2 = result2.events.Transfer;277278    [[events1, token1], [events2, token2]].map(burnEvents => {279      expect(burnEvents[0]).to.be.like({280        address: collectionAddress,281        event: 'Transfer',282        returnValues: {283          from: helper.address.substrateToEth(ownerSub.address),284          to: '0x0000000000000000000000000000000000000000',285          tokenId: burnEvents[1].tokenId.toString(),286        },287      });288    });289290    expect(await token1.doesExist()).to.be.false;291    expect(await token2.doesExist()).to.be.false;292  });293294  itEth('Can perform approveCross()', async ({helper}) => {295    // arrange: create accounts296    const owner = await helper.eth.createAccountWithBalance(donor, 100n);297    const ownerCross = helper.ethCrossAccount.fromAddress(owner);298    const receiverSub = charlie;299    const recieverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);300    const receiverEth = await helper.eth.createAccountWithBalance(donor, 100n);301    const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);302303    // arrange: create collection and tokens:304    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});305    const token1 = await collection.mintToken(minter, {Ethereum: owner});306    const token2 = await collection.mintToken(minter, {Ethereum: owner});307308    const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');309310    // Can approveCross substrate and ethereum address:311    const resultSub = await collectionEvm.methods.approveCross(recieverCrossSub, token1.tokenId).send({from: owner});312    const resultEth = await collectionEvm.methods.approveCross(receiverCrossEth, token2.tokenId).send({from: owner});313    const eventSub = resultSub.events.Approval;314    const eventEth = resultEth.events.Approval;315    expect(eventSub).to.be.like({316      address: helper.ethAddress.fromCollectionId(collection.collectionId),317      event: 'Approval',318      returnValues: {319        owner,320        approved: helper.address.substrateToEth(receiverSub.address),321        tokenId: token1.tokenId.toString(),322      },323    });324    expect(eventEth).to.be.like({325      address: helper.ethAddress.fromCollectionId(collection.collectionId),326      event: 'Approval',327      returnValues: {328        owner,329        approved: receiverEth,330        tokenId: token2.tokenId.toString(),331      },332    });333334    // Substrate address can transferFrom approved tokens:335    await helper.nft.transferTokenFrom(receiverSub, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiverSub.address});336    expect(await helper.nft.getTokenOwner(collection.collectionId, token1.tokenId)).to.deep.eq({Substrate: receiverSub.address});337    // Ethereum address can transferFromCross approved tokens:338    await collectionEvm.methods.transferFromCross(ownerCross, receiverCrossEth, token2.tokenId).send({from: receiverEth});339    expect(await helper.nft.getTokenOwner(collection.collectionId, token2.tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});340  });341342  itEth('Can perform transferFrom()', async ({helper}) => {343    const owner = await helper.eth.createAccountWithBalance(donor);344    const spender = await helper.eth.createAccountWithBalance(donor);345    const receiver = helper.eth.createAccount();346347    const collection = await helper.nft.mintCollection(minter, {});348    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});349350    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);351    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);352353    await contract.methods.approve(spender, tokenId).send({from: owner});354355    {356      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});357358      const event = result.events.Transfer;359      expect(event.address).to.be.equal(collectionAddress);360      expect(event.returnValues.from).to.be.equal(owner);361      expect(event.returnValues.to).to.be.equal(receiver);362      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);363    }364365    {366      const balance = await contract.methods.balanceOf(receiver).call();367      expect(+balance).to.equal(1);368    }369370    {371      const balance = await contract.methods.balanceOf(owner).call();372      expect(+balance).to.equal(0);373    }374  });375376  itEth('Can perform transferFromCross()', async ({helper}) => {377    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});378379    const [owner, receiver] = await helper.arrange.createAccounts([100n, 100n], donor);380    const spender = await helper.eth.createAccountWithBalance(donor);381382    const token = await collection.mintToken(minter, {Substrate: owner.address});383384    const address = helper.ethAddress.fromCollectionId(collection.collectionId);385    const contract = helper.ethNativeContract.collection(address, 'nft');386387    await token.approve(owner, {Ethereum: spender});388389    {390      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);391      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);392      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});393      const event = result.events.Transfer;394      expect(event).to.be.like({395        address: helper.ethAddress.fromCollectionId(collection.collectionId),396        event: 'Transfer',397        returnValues: {398          from: helper.address.substrateToEth(owner.address),399          to: helper.address.substrateToEth(receiver.address),400          tokenId: token.tokenId.toString(),401        },402      });403    }404405    expect(await token.getOwner()).to.be.like({Substrate: receiver.address});406  });407408  itEth('Can perform transfer()', async ({helper}) => {409    const collection = await helper.nft.mintCollection(minter, {});410    const owner = await helper.eth.createAccountWithBalance(donor);411    const receiver = helper.eth.createAccount();412413    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});414415    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);416    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);417418    {419      const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});420421      const event = result.events.Transfer;422      expect(event.address).to.be.equal(collectionAddress);423      expect(event.returnValues.from).to.be.equal(owner);424      expect(event.returnValues.to).to.be.equal(receiver);425      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);426    }427428    {429      const balance = await contract.methods.balanceOf(owner).call();430      expect(+balance).to.equal(0);431    }432433    {434      const balance = await contract.methods.balanceOf(receiver).call();435      expect(+balance).to.equal(1);436    }437  });438  439  itEth('Can perform transferCross()', async ({helper}) => {440    const collection = await helper.nft.mintCollection(minter, {});441    const owner = await helper.eth.createAccountWithBalance(donor);442    const receiver = await helper.eth.createAccountWithBalance(donor);443    const to = helper.ethCrossAccount.fromAddress(receiver);444    const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter);445    446    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});447448    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);449    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);450451    {452      const result = await contract.methods.transferCross(to, tokenId).send({from: owner});453454      const event = result.events.Transfer;455      expect(event.address).to.be.equal(collectionAddress);456      expect(event.returnValues.from).to.be.equal(owner);457      expect(event.returnValues.to).to.be.equal(receiver);458      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);459    }460461    {462      const balance = await contract.methods.balanceOf(owner).call();463      expect(+balance).to.equal(0);464    }465466    {467      const balance = await contract.methods.balanceOf(receiver).call();468      expect(+balance).to.equal(1);469    }470    471    {472      const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver});473      474475      const event = substrateResult.events.Transfer;476      expect(event.address).to.be.equal(collectionAddress);477      expect(event.returnValues.from).to.be.equal(receiver);478      expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));479      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);480    }481482    {483      const balance = await contract.methods.balanceOf(receiver).call();484      expect(+balance).to.equal(0);485    }486487    {488      const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});489      expect(balance).to.be.contain(tokenId);490    }491  });492});493494describe('NFT: Fees', () => {495  let donor: IKeyringPair;496  let alice: IKeyringPair;497  let bob: IKeyringPair;498  let charlie: IKeyringPair;499500  before(async function() {501    await usingEthPlaygrounds(async (helper, privateKey) => {502      donor = await privateKey({filename: __filename});503      [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);504    });505  });506507  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {508    const owner = await helper.eth.createAccountWithBalance(donor);509    const spender = helper.eth.createAccount();510511    const collection = await helper.nft.mintCollection(alice, {});512    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});513514    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);515516    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));517    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));518  });519520  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {521    const owner = await helper.eth.createAccountWithBalance(donor);522    const spender = await helper.eth.createAccountWithBalance(donor);523524    const collection = await helper.nft.mintCollection(alice, {});525    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});526527    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);528529    await contract.methods.approve(spender, tokenId).send({from: owner});530531    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));532    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));533  });534535  itEth('Can perform transferFromCross()', async ({helper}) => {536    const collectionMinter = alice;537    const owner = bob;538    const receiver = charlie;539    const collection = await helper.nft.mintCollection(collectionMinter, {name: 'A', description: 'B', tokenPrefix: 'C'});540541    const spender = await helper.eth.createAccountWithBalance(donor, 100n);542543    const token = await collection.mintToken(collectionMinter, {Substrate: owner.address});544545    const address = helper.ethAddress.fromCollectionId(collection.collectionId);546    const contract = helper.ethNativeContract.collection(address, 'nft');547548    await token.approve(owner, {Ethereum: spender});549550    {551      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);552      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);553      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});554      const event = result.events.Transfer;555      expect(event).to.be.like({556        address: helper.ethAddress.fromCollectionId(collection.collectionId),557        event: 'Transfer',558        returnValues: {559          from: helper.address.substrateToEth(owner.address),560          to: helper.address.substrateToEth(receiver.address),561          tokenId: token.tokenId.toString(),562        },563      });564    }565566    expect(await token.getOwner()).to.be.like({Substrate: receiver.address});567  });568569  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {570    const owner = await helper.eth.createAccountWithBalance(donor);571    const receiver = helper.eth.createAccount();572573    const collection = await helper.nft.mintCollection(alice, {});574    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});575576    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);577578    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));579    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));580  });581});582583describe('NFT: Substrate calls', () => {584  let donor: IKeyringPair;585  let alice: IKeyringPair;586587  before(async function() {588    await usingEthPlaygrounds(async (helper, privateKey) => {589      donor = await privateKey({filename: __filename});590      [alice] = await helper.arrange.createAccounts([20n], donor);591    });592  });593594  itEth('Events emitted for mint()', async ({helper}) => {595    const collection = await helper.nft.mintCollection(alice, {});596    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);597    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');598599    const events: any = [];600    contract.events.allEvents((_: any, event: any) => {601      events.push(event);602    });603604    const {tokenId} = await collection.mintToken(alice);605    if (events.length == 0) await helper.wait.newBlocks(1);606    const event = events[0];607608    expect(event.event).to.be.equal('Transfer');609    expect(event.address).to.be.equal(collectionAddress);610    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');611    expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));612    expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());613  });614615  itEth('Events emitted for burn()', async ({helper}) => {616    const collection = await helper.nft.mintCollection(alice, {});617    const token = await collection.mintToken(alice);618619    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);620    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');621622    const events: any = [];623    contract.events.allEvents((_: any, event: any) => {624      events.push(event);625    });626627    await token.burn(alice);628    if (events.length == 0) await helper.wait.newBlocks(1);629    const event = events[0];630631    expect(event.event).to.be.equal('Transfer');632    expect(event.address).to.be.equal(collectionAddress);633    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));634    expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');635    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());636  });637638  itEth('Events emitted for approve()', async ({helper}) => {639    const receiver = helper.eth.createAccount();640641    const collection = await helper.nft.mintCollection(alice, {});642    const token = await collection.mintToken(alice);643644    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);645    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');646647    const events: any = [];648    contract.events.allEvents((_: any, event: any) => {649      events.push(event);650    });651652    await token.approve(alice, {Ethereum: receiver});653    if (events.length == 0) await helper.wait.newBlocks(1);654    const event = events[0];655656    expect(event.event).to.be.equal('Approval');657    expect(event.address).to.be.equal(collectionAddress);658    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));659    expect(event.returnValues.approved).to.be.equal(receiver);660    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());661  });662663  itEth('Events emitted for transferFrom()', async ({helper}) => {664    const [bob] = await helper.arrange.createAccounts([10n], donor);665    const receiver = helper.eth.createAccount();666667    const collection = await helper.nft.mintCollection(alice, {});668    const token = await collection.mintToken(alice);669    await token.approve(alice, {Substrate: bob.address});670671    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);672    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');673674    const events: any = [];675    contract.events.allEvents((_: any, event: any) => {676      events.push(event);677    });678679    await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});680681    if (events.length == 0) await helper.wait.newBlocks(1);682    const event = events[0];683684    expect(event.address).to.be.equal(collectionAddress);685    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));686    expect(event.returnValues.to).to.be.equal(receiver);687    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);688  });689690  itEth('Events emitted for transfer()', async ({helper}) => {691    const receiver = helper.eth.createAccount();692693    const collection = await helper.nft.mintCollection(alice, {});694    const token = await collection.mintToken(alice);695696    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);697    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');698699    const events: any = [];700    contract.events.allEvents((_: any, event: any) => {701      events.push(event);702    });703704    await token.transfer(alice, {Ethereum: receiver});705706    if (events.length == 0) await helper.wait.newBlocks(1);707    const event = events[0];708709    expect(event.address).to.be.equal(collectionAddress);710    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));711    expect(event.returnValues.to).to.be.equal(receiver);712    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);713  });714});715716describe('Common metadata', () => {717  let donor: IKeyringPair;718  let alice: IKeyringPair;719720  before(async function() {721    await usingEthPlaygrounds(async (helper, privateKey) => {722      donor = await privateKey({filename: __filename});723      [alice] = await helper.arrange.createAccounts([20n], donor);724    });725  });726727  itEth('Returns collection name', async ({helper}) => {728    const caller = await helper.eth.createAccountWithBalance(donor);729    const tokenPropertyPermissions = [{730      key: 'URI',731      permission: {732        mutable: true,733        collectionAdmin: true,734        tokenOwner: false,735      },736    }];737    const collection = await helper.nft.mintCollection(738      alice,739      {740        name: 'oh River',741        tokenPrefix: 'CHANGE',742        properties: [{key: 'ERC721Metadata', value: '1'}],743        tokenPropertyPermissions,744      },745    );746747    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);748    const name = await contract.methods.name().call();749    expect(name).to.equal('oh River');750  });751752  itEth('Returns symbol name', async ({helper}) => {753    const caller = await helper.eth.createAccountWithBalance(donor);754    const tokenPropertyPermissions = [{755      key: 'URI',756      permission: {757        mutable: true,758        collectionAdmin: true,759        tokenOwner: false,760      },761    }];762    const collection = await helper.nft.mintCollection(763      alice,764      {765        name: 'oh River',766        tokenPrefix: 'CHANGE',767        properties: [{key: 'ERC721Metadata', value: '1'}],768        tokenPropertyPermissions,769      },770    );771772    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);773    const symbol = await contract.methods.symbol().call();774    expect(symbol).to.equal('CHANGE');775  });776});
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'});254    const ownerSub = bob;255    const ownerCross = helper.ethCrossAccount.fromKeyringPair(ownerSub);256    const ownerEth = await helper.eth.createAccountWithBalance(donor, 100n);257258    const burnerEth = await helper.eth.createAccountWithBalance(donor, 100n);259    const burnerCrossEth = helper.ethCrossAccount.fromAddress(burnerEth);260261    const token1 = await collection.mintToken(minter, {Substrate: ownerSub.address});262    const token2 = await collection.mintToken(minter, {Ethereum: ownerEth});263264    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);265    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft');266267    // Approve tokens from substrate and ethereum:268    await token1.approve(ownerSub, {Ethereum: burnerEth});269    await collectionEvm.methods.approveCross(burnerCrossEth, token2.tokenId).send({from: ownerEth});270271    // can burnFromCross:272    const result1 = await collectionEvm.methods.burnFromCross(ownerCross, token1.tokenId).send({from: burnerEth});273    // FIXME Error No Permission?:274    const result2 = await collectionEvm.methods.burnFromCross(ownerCross, token2.tokenId).send({from: burnerEth});275    const events1 = result1.events.Transfer;276    const events2 = result2.events.Transfer;277278    [[events1, token1], [events2, token2]].map(burnEvents => {279      expect(burnEvents[0]).to.be.like({280        address: collectionAddress,281        event: 'Transfer',282        returnValues: {283          from: helper.address.substrateToEth(ownerSub.address),284          to: '0x0000000000000000000000000000000000000000',285          tokenId: burnEvents[1].tokenId.toString(),286        },287      });288    });289290    expect(await token1.doesExist()).to.be.false;291    expect(await token2.doesExist()).to.be.false;292  });293294  itEth('Can perform approveCross()', async ({helper}) => {295    // arrange: create accounts296    const owner = await helper.eth.createAccountWithBalance(donor, 100n);297    const ownerCross = helper.ethCrossAccount.fromAddress(owner);298    const receiverSub = charlie;299    const recieverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);300    const receiverEth = await helper.eth.createAccountWithBalance(donor, 100n);301    const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);302303    // arrange: create collection and tokens:304    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});305    const token1 = await collection.mintToken(minter, {Ethereum: owner});306    const token2 = await collection.mintToken(minter, {Ethereum: owner});307308    const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');309310    // Can approveCross substrate and ethereum address:311    const resultSub = await collectionEvm.methods.approveCross(recieverCrossSub, token1.tokenId).send({from: owner});312    const resultEth = await collectionEvm.methods.approveCross(receiverCrossEth, token2.tokenId).send({from: owner});313    const eventSub = resultSub.events.Approval;314    const eventEth = resultEth.events.Approval;315    expect(eventSub).to.be.like({316      address: helper.ethAddress.fromCollectionId(collection.collectionId),317      event: 'Approval',318      returnValues: {319        owner,320        approved: helper.address.substrateToEth(receiverSub.address),321        tokenId: token1.tokenId.toString(),322      },323    });324    expect(eventEth).to.be.like({325      address: helper.ethAddress.fromCollectionId(collection.collectionId),326      event: 'Approval',327      returnValues: {328        owner,329        approved: receiverEth,330        tokenId: token2.tokenId.toString(),331      },332    });333334    // Substrate address can transferFrom approved tokens:335    await helper.nft.transferTokenFrom(receiverSub, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiverSub.address});336    expect(await helper.nft.getTokenOwner(collection.collectionId, token1.tokenId)).to.deep.eq({Substrate: receiverSub.address});337    // Ethereum address can transferFromCross approved tokens:338    await collectionEvm.methods.transferFromCross(ownerCross, receiverCrossEth, token2.tokenId).send({from: receiverEth});339    expect(await helper.nft.getTokenOwner(collection.collectionId, token2.tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});340  });341342  itEth('Can reaffirm approved address', async ({helper}) => {343    const owner = await helper.eth.createAccountWithBalance(donor, 100n);344    const [receiver1, receiver2] = await helper.arrange.createAccounts([100n, 100n], donor);345    const receiver1Cross = helper.ethCrossAccount.fromKeyringPair(receiver1);346    const receiver2Cross = helper.ethCrossAccount.fromKeyringPair(receiver2);347    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});348    const token1 = await collection.mintToken(minter, {Ethereum: owner});349    const token2 = await collection.mintToken(minter, {Ethereum: owner});350    const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');351352    // Can approve and reaffirm approved address:353    await collectionEvm.methods.approveCross(receiver1Cross, token1.tokenId).send({from: owner});354    await collectionEvm.methods.approveCross(receiver2Cross, token1.tokenId).send({from: owner});355356    // receiver1 cannot transferFrom:357    await expect(helper.nft.transferTokenFrom(receiver1, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiver1.address})).to.be.rejected;358    // receiver2 can transferFrom:359    await helper.nft.transferTokenFrom(receiver2, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiver2.address});360361    // can set approved address to zero address:362    await collectionEvm.methods.approveCross(receiver1Cross, token2.tokenId).send({from: owner});363364    // FIXME how to remove approval?:365    await collectionEvm.methods.approveCross({eth: '0x0000000000000000000000000000000000000000', sub: '0'}, token2.tokenId).call({from: owner});366    await collectionEvm.methods.approve('0x0000000000000000000000000000000000000000', token2.tokenId).call({from: owner});367368    // receiver1 cannot transfer token anymore:369    await expect(helper.nft.transferTokenFrom(receiver1, collection.collectionId, token2.tokenId, {Ethereum: owner}, {Substrate: receiver1.address})).to.be.rejected;370  });371372  itEth('Can perform transferFrom()', async ({helper}) => {373    const owner = await helper.eth.createAccountWithBalance(donor);374    const spender = await helper.eth.createAccountWithBalance(donor);375    const receiver = helper.eth.createAccount();376377    const collection = await helper.nft.mintCollection(minter, {});378    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});379380    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);381    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);382383    await contract.methods.approve(spender, tokenId).send({from: owner});384385    {386      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});387388      const event = result.events.Transfer;389      expect(event.address).to.be.equal(collectionAddress);390      expect(event.returnValues.from).to.be.equal(owner);391      expect(event.returnValues.to).to.be.equal(receiver);392      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);393    }394395    {396      const balance = await contract.methods.balanceOf(receiver).call();397      expect(+balance).to.equal(1);398    }399400    {401      const balance = await contract.methods.balanceOf(owner).call();402      expect(+balance).to.equal(0);403    }404  });405406  itEth('Can perform transferFromCross()', async ({helper}) => {407    const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});408409    const [owner, receiver] = await helper.arrange.createAccounts([100n, 100n], donor);410    const spender = await helper.eth.createAccountWithBalance(donor);411412    const token = await collection.mintToken(minter, {Substrate: owner.address});413414    const address = helper.ethAddress.fromCollectionId(collection.collectionId);415    const contract = helper.ethNativeContract.collection(address, 'nft');416417    await token.approve(owner, {Ethereum: spender});418419    {420      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);421      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);422      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});423      const event = result.events.Transfer;424      expect(event).to.be.like({425        address: helper.ethAddress.fromCollectionId(collection.collectionId),426        event: 'Transfer',427        returnValues: {428          from: helper.address.substrateToEth(owner.address),429          to: helper.address.substrateToEth(receiver.address),430          tokenId: token.tokenId.toString(),431        },432      });433    }434435    expect(await token.getOwner()).to.be.like({Substrate: receiver.address});436  });437438  itEth('Can perform transfer()', async ({helper}) => {439    const collection = await helper.nft.mintCollection(minter, {});440    const owner = await helper.eth.createAccountWithBalance(donor);441    const receiver = helper.eth.createAccount();442443    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});444445    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);446    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);447448    {449      const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});450451      const event = result.events.Transfer;452      expect(event.address).to.be.equal(collectionAddress);453      expect(event.returnValues.from).to.be.equal(owner);454      expect(event.returnValues.to).to.be.equal(receiver);455      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);456    }457458    {459      const balance = await contract.methods.balanceOf(owner).call();460      expect(+balance).to.equal(0);461    }462463    {464      const balance = await contract.methods.balanceOf(receiver).call();465      expect(+balance).to.equal(1);466    }467  });468  469  itEth('Can perform transferCross()', async ({helper}) => {470    const collection = await helper.nft.mintCollection(minter, {});471    const owner = await helper.eth.createAccountWithBalance(donor);472    const receiver = await helper.eth.createAccountWithBalance(donor);473    const to = helper.ethCrossAccount.fromAddress(receiver);474    const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter);475    476    const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});477478    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);479    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);480481    {482      const result = await contract.methods.transferCross(to, tokenId).send({from: owner});483484      const event = result.events.Transfer;485      expect(event.address).to.be.equal(collectionAddress);486      expect(event.returnValues.from).to.be.equal(owner);487      expect(event.returnValues.to).to.be.equal(receiver);488      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);489    }490491    {492      const balance = await contract.methods.balanceOf(owner).call();493      expect(+balance).to.equal(0);494    }495496    {497      const balance = await contract.methods.balanceOf(receiver).call();498      expect(+balance).to.equal(1);499    }500    501    {502      const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver});503      504505      const event = substrateResult.events.Transfer;506      expect(event.address).to.be.equal(collectionAddress);507      expect(event.returnValues.from).to.be.equal(receiver);508      expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));509      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);510    }511512    {513      const balance = await contract.methods.balanceOf(receiver).call();514      expect(+balance).to.equal(0);515    }516517    {518      const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});519      expect(balance).to.be.contain(tokenId);520    }521  });522});523524describe('NFT: Fees', () => {525  let donor: IKeyringPair;526  let alice: IKeyringPair;527  let bob: IKeyringPair;528  let charlie: IKeyringPair;529530  before(async function() {531    await usingEthPlaygrounds(async (helper, privateKey) => {532      donor = await privateKey({filename: __filename});533      [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);534    });535  });536537  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {538    const owner = await helper.eth.createAccountWithBalance(donor);539    const spender = helper.eth.createAccount();540541    const collection = await helper.nft.mintCollection(alice, {});542    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});543544    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);545546    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));547    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));548  });549550  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {551    const owner = await helper.eth.createAccountWithBalance(donor);552    const spender = await helper.eth.createAccountWithBalance(donor);553554    const collection = await helper.nft.mintCollection(alice, {});555    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});556557    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);558559    await contract.methods.approve(spender, tokenId).send({from: owner});560561    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));562    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));563  });564565  itEth('Can perform transferFromCross()', async ({helper}) => {566    const collectionMinter = alice;567    const owner = bob;568    const receiver = charlie;569    const collection = await helper.nft.mintCollection(collectionMinter, {name: 'A', description: 'B', tokenPrefix: 'C'});570571    const spender = await helper.eth.createAccountWithBalance(donor, 100n);572573    const token = await collection.mintToken(collectionMinter, {Substrate: owner.address});574575    const address = helper.ethAddress.fromCollectionId(collection.collectionId);576    const contract = helper.ethNativeContract.collection(address, 'nft');577578    await token.approve(owner, {Ethereum: spender});579580    {581      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);582      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);583      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});584      const event = result.events.Transfer;585      expect(event).to.be.like({586        address: helper.ethAddress.fromCollectionId(collection.collectionId),587        event: 'Transfer',588        returnValues: {589          from: helper.address.substrateToEth(owner.address),590          to: helper.address.substrateToEth(receiver.address),591          tokenId: token.tokenId.toString(),592        },593      });594    }595596    expect(await token.getOwner()).to.be.like({Substrate: receiver.address});597  });598599  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {600    const owner = await helper.eth.createAccountWithBalance(donor);601    const receiver = helper.eth.createAccount();602603    const collection = await helper.nft.mintCollection(alice, {});604    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});605606    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);607608    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));609    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));610  });611});612613describe('NFT: Substrate calls', () => {614  let donor: IKeyringPair;615  let alice: IKeyringPair;616617  before(async function() {618    await usingEthPlaygrounds(async (helper, privateKey) => {619      donor = await privateKey({filename: __filename});620      [alice] = await helper.arrange.createAccounts([20n], donor);621    });622  });623624  itEth('Events emitted for mint()', async ({helper}) => {625    const collection = await helper.nft.mintCollection(alice, {});626    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);627    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');628629    const events: any = [];630    contract.events.allEvents((_: any, event: any) => {631      events.push(event);632    });633634    const {tokenId} = await collection.mintToken(alice);635    if (events.length == 0) await helper.wait.newBlocks(1);636    const event = events[0];637638    expect(event.event).to.be.equal('Transfer');639    expect(event.address).to.be.equal(collectionAddress);640    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');641    expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));642    expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());643  });644645  itEth('Events emitted for burn()', async ({helper}) => {646    const collection = await helper.nft.mintCollection(alice, {});647    const token = await collection.mintToken(alice);648649    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);650    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');651652    const events: any = [];653    contract.events.allEvents((_: any, event: any) => {654      events.push(event);655    });656657    await token.burn(alice);658    if (events.length == 0) await helper.wait.newBlocks(1);659    const event = events[0];660661    expect(event.event).to.be.equal('Transfer');662    expect(event.address).to.be.equal(collectionAddress);663    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));664    expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');665    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());666  });667668  itEth('Events emitted for approve()', async ({helper}) => {669    const receiver = helper.eth.createAccount();670671    const collection = await helper.nft.mintCollection(alice, {});672    const token = await collection.mintToken(alice);673674    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);675    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');676677    const events: any = [];678    contract.events.allEvents((_: any, event: any) => {679      events.push(event);680    });681682    await token.approve(alice, {Ethereum: receiver});683    if (events.length == 0) await helper.wait.newBlocks(1);684    const event = events[0];685686    expect(event.event).to.be.equal('Approval');687    expect(event.address).to.be.equal(collectionAddress);688    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));689    expect(event.returnValues.approved).to.be.equal(receiver);690    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());691  });692693  itEth('Events emitted for transferFrom()', async ({helper}) => {694    const [bob] = await helper.arrange.createAccounts([10n], donor);695    const receiver = helper.eth.createAccount();696697    const collection = await helper.nft.mintCollection(alice, {});698    const token = await collection.mintToken(alice);699    await token.approve(alice, {Substrate: bob.address});700701    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);702    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');703704    const events: any = [];705    contract.events.allEvents((_: any, event: any) => {706      events.push(event);707    });708709    await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});710711    if (events.length == 0) await helper.wait.newBlocks(1);712    const event = events[0];713714    expect(event.address).to.be.equal(collectionAddress);715    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));716    expect(event.returnValues.to).to.be.equal(receiver);717    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);718  });719720  itEth('Events emitted for transfer()', async ({helper}) => {721    const receiver = helper.eth.createAccount();722723    const collection = await helper.nft.mintCollection(alice, {});724    const token = await collection.mintToken(alice);725726    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);727    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');728729    const events: any = [];730    contract.events.allEvents((_: any, event: any) => {731      events.push(event);732    });733734    await token.transfer(alice, {Ethereum: receiver});735736    if (events.length == 0) await helper.wait.newBlocks(1);737    const event = events[0];738739    expect(event.address).to.be.equal(collectionAddress);740    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));741    expect(event.returnValues.to).to.be.equal(receiver);742    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);743  });744});745746describe('Common metadata', () => {747  let donor: IKeyringPair;748  let alice: IKeyringPair;749750  before(async function() {751    await usingEthPlaygrounds(async (helper, privateKey) => {752      donor = await privateKey({filename: __filename});753      [alice] = await helper.arrange.createAccounts([20n], donor);754    });755  });756757  itEth('Returns collection name', async ({helper}) => {758    const caller = await helper.eth.createAccountWithBalance(donor);759    const tokenPropertyPermissions = [{760      key: 'URI',761      permission: {762        mutable: true,763        collectionAdmin: true,764        tokenOwner: false,765      },766    }];767    const collection = await helper.nft.mintCollection(768      alice,769      {770        name: 'oh River',771        tokenPrefix: 'CHANGE',772        properties: [{key: 'ERC721Metadata', value: '1'}],773        tokenPropertyPermissions,774      },775    );776777    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);778    const name = await contract.methods.name().call();779    expect(name).to.equal('oh River');780  });781782  itEth('Returns symbol name', async ({helper}) => {783    const caller = await helper.eth.createAccountWithBalance(donor);784    const tokenPropertyPermissions = [{785      key: 'URI',786      permission: {787        mutable: true,788        collectionAdmin: true,789        tokenOwner: false,790      },791    }];792    const collection = await helper.nft.mintCollection(793      alice,794      {795        name: 'oh River',796        tokenPrefix: 'CHANGE',797        properties: [{key: 'ERC721Metadata', value: '1'}],798        tokenPropertyPermissions,799      },800    );801802    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);803    const symbol = await contract.methods.symbol().call();804    expect(symbol).to.equal('CHANGE');805  });806});