git.delta.rocks / unique-network / refs/commits / d4f43b6a7259

difftreelog

source

tests/src/eth/nonFungible.test.ts21.8 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util/playgrounds';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 = privateKey('//Alice');29      [alice] = await helper.arrange.createAccounts([10n], donor);30    });31  });32  33  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 = privateKey('//Alice');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);98    99    const nextTokenId = await contract.methods.nextTokenId().call();100    expect(nextTokenId).to.be.equal('1');101    const result = await contract.methods.mint(102      receiver,103      nextTokenId,104    ).send();105106    if (propertyKey && propertyValue) {107      // Set URL or suffix108      await contract.methods.setProperty(nextTokenId, propertyKey, Buffer.from(propertyValue)).send();109    }110111    const event = result.events.Transfer;112    expect(event.address).to.be.equal(collectionAddress);113    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');114    expect(event.returnValues.to).to.be.equal(receiver);115    expect(event.returnValues.tokenId).to.be.equal(nextTokenId);116117    return {contract, nextTokenId};118  }119120  itEth('Empty tokenURI', async ({helper}) => {121    const {contract, nextTokenId} = await setup(helper, '');122    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');123  });124125  itEth('TokenURI from url', async ({helper}) => {126    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URI', 'Token URI');127    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');128  });129130  itEth('TokenURI from baseURI', async ({helper}) => {131    const {contract, nextTokenId} = await setup(helper, 'BaseURI_');132    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_');133  });134135  itEth('TokenURI from baseURI + suffix', async ({helper}) => {136    const suffix = '/some/suffix';137    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URISuffix', suffix);138    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);139  });140});141142describe('NFT: Plain calls', () => {143  let donor: IKeyringPair;144  let alice: IKeyringPair;145146  before(async function() {147    await usingEthPlaygrounds(async (helper, privateKey) => {148      donor = privateKey('//Alice');149      [alice] = await helper.arrange.createAccounts([10n], donor);150    });151  });152153  itEth('Can perform mint()', 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);159    const nextTokenId = await contract.methods.nextTokenId().call();160161    expect(nextTokenId).to.be.equal('1');162    const result = await contract.methods.mintWithTokenURI(163      receiver,164      nextTokenId,165      'Test URI',166    ).send();167168    const event = result.events.Transfer;169    expect(event.address).to.be.equal(collectionAddress);170    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');171    expect(event.returnValues.to).to.be.equal(receiver);172    expect(event.returnValues.tokenId).to.be.equal(nextTokenId);173174    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');175176    // TODO: this wont work right now, need release 919000 first177    // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();178    // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();179    // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);180  });181182  //TODO: CORE-302 add eth methods183  itEth.skip('Can perform mintBulk()', async ({helper}) => {184    const caller = await helper.eth.createAccountWithBalance(donor);185    const receiver = helper.eth.createAccount();186187    const collection = await helper.nft.mintCollection(alice);188    await collection.addAdmin(alice, {Ethereum: caller});189190    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);191    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);192    {193      const bulkSize = 3;194      const nextTokenId = await contract.methods.nextTokenId().call();195      expect(nextTokenId).to.be.equal('1');196      const result = await contract.methods.mintBulkWithTokenURI(197        receiver,198        Array.from({length: bulkSize}, (_, i) => (199          [+nextTokenId + i, `Test URI ${i}`]200        )),201      ).send({from: caller});202203      const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.tokenId - b.returnValues.tokenId);204      for (let i = 0; i < bulkSize; i++) {205        const event = events[i];206        expect(event.address).to.equal(collectionAddress);207        expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');208        expect(event.returnValues.to).to.equal(receiver);209        expect(event.returnValues.tokenId).to.equal(`${+nextTokenId + i}`);210211        expect(await contract.methods.tokenURI(+nextTokenId + i).call()).to.be.equal(`Test URI ${i}`);212      }213    }214  });215216  itEth('Can perform burn()', async ({helper}) => {217    const caller = await helper.eth.createAccountWithBalance(donor);218219    const collection = await helper.nft.mintCollection(alice, {});220    const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});221222    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);223    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);224225    {226      const result = await contract.methods.burn(tokenId).send({from: caller});227      228      const event = result.events.Transfer;229      expect(event.address).to.be.equal(collectionAddress);230      expect(event.returnValues.from).to.be.equal(caller);231      expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');232      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);233    }234  });235236  itEth('Can perform approve()', async ({helper}) => {237    const owner = await helper.eth.createAccountWithBalance(donor);238    const spender = helper.eth.createAccount();239240    const collection = await helper.nft.mintCollection(alice, {});241    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});242243    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);244    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);245246    {247      const result = await contract.methods.approve(spender, tokenId).send({from: owner});248249      const event = result.events.Approval;250      expect(event.address).to.be.equal(collectionAddress);251      expect(event.returnValues.owner).to.be.equal(owner);252      expect(event.returnValues.approved).to.be.equal(spender);253      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);254    }255  });256257  itEth('Can perform transferFrom()', async ({helper}) => {258    const owner = await helper.eth.createAccountWithBalance(donor);259    const spender = await helper.eth.createAccountWithBalance(donor);260    const receiver = helper.eth.createAccount();261262    const collection = await helper.nft.mintCollection(alice, {});263    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});264265    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);266    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);267268    await contract.methods.approve(spender, tokenId).send({from: owner});269270    {271      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});272273      const event = result.events.Transfer;274      expect(event.address).to.be.equal(collectionAddress);275      expect(event.returnValues.from).to.be.equal(owner);276      expect(event.returnValues.to).to.be.equal(receiver);277      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);278    }279280    {281      const balance = await contract.methods.balanceOf(receiver).call();282      expect(+balance).to.equal(1);283    }284285    {286      const balance = await contract.methods.balanceOf(owner).call();287      expect(+balance).to.equal(0);288    }289  });290291  itEth('Can perform transfer()', async ({helper}) => {292    const collection = await helper.nft.mintCollection(alice, {});293    const owner = await helper.eth.createAccountWithBalance(donor);294    const receiver = helper.eth.createAccount();295296    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});297298    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);299    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);300301    {302      const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});303304      const event = result.events.Transfer;305      expect(event.address).to.be.equal(collectionAddress);306      expect(event.returnValues.from).to.be.equal(owner);307      expect(event.returnValues.to).to.be.equal(receiver);308      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);309    }310311    {312      const balance = await contract.methods.balanceOf(owner).call();313      expect(+balance).to.equal(0);314    }315316    {317      const balance = await contract.methods.balanceOf(receiver).call();318      expect(+balance).to.equal(1);319    }320  });321});322323describe('NFT: Fees', () => {324  let donor: IKeyringPair;325  let alice: IKeyringPair;326327  before(async function() {328    await usingEthPlaygrounds(async (helper, privateKey) => {329      donor = privateKey('//Alice');330      [alice] = await helper.arrange.createAccounts([10n], donor);331    });332  });333  334  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {335    const owner = await helper.eth.createAccountWithBalance(donor);336    const spender = helper.eth.createAccount();337338    const collection = await helper.nft.mintCollection(alice, {});339    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});340341    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);342343    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));344    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));345  });346347  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {348    const owner = await helper.eth.createAccountWithBalance(donor);349    const spender = await helper.eth.createAccountWithBalance(donor);350351    const collection = await helper.nft.mintCollection(alice, {});352    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});353354    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);355356    await contract.methods.approve(spender, tokenId).send({from: owner});357358    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));359    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));360  });361362  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {363    const owner = await helper.eth.createAccountWithBalance(donor);364    const receiver = helper.eth.createAccount();365366    const collection = await helper.nft.mintCollection(alice, {});367    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});368369    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);370371    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));372    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));373  });374});375376describe('NFT: Substrate calls', () => {377  let donor: IKeyringPair;378  let alice: IKeyringPair;379380  before(async function() {381    await usingEthPlaygrounds(async (helper, privateKey) => {382      donor = privateKey('//Alice');383      [alice] = await helper.arrange.createAccounts([20n], donor);384    });385  });386387  itEth('Events emitted for mint()', async ({helper}) => {388    const collection = await helper.nft.mintCollection(alice, {});389    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);390    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');391392    const events: any = [];393    contract.events.allEvents((_: any, event: any) => {394      events.push(event);395    });396    const {tokenId} = await collection.mintToken(alice);397398    const event = events[0];399    expect(event.event).to.be.equal('Transfer');400    expect(event.address).to.be.equal(collectionAddress);401    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');402    expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));403    expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());404  });405406  itEth('Events emitted for burn()', async ({helper}) => {407    const collection = await helper.nft.mintCollection(alice, {});408    const token = await collection.mintToken(alice);409410    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);411    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');412    413    const events: any = [];414    contract.events.allEvents((_: any, event: any) => {415      events.push(event);416    });417418    await token.burn(alice);419420    const event = events[0];421    expect(event.event).to.be.equal('Transfer');422    expect(event.address).to.be.equal(collectionAddress);423    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));424    expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');425    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());426  });427428  itEth('Events emitted for approve()', async ({helper}) => {429    const receiver = helper.eth.createAccount();430431    const collection = await helper.nft.mintCollection(alice, {});432    const token = await collection.mintToken(alice);433434    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);435    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');436    437    const events: any = [];438    contract.events.allEvents((_: any, event: any) => {439      events.push(event);440    });441442    await token.approve(alice, {Ethereum: receiver});443444    const event = events[0];445    expect(event.event).to.be.equal('Approval');446    expect(event.address).to.be.equal(collectionAddress);447    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));448    expect(event.returnValues.approved).to.be.equal(receiver);449    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());450  });451452  itEth('Events emitted for transferFrom()', async ({helper}) => {453    const [bob] = await helper.arrange.createAccounts([10n], donor);454    const receiver = helper.eth.createAccount();455456    const collection = await helper.nft.mintCollection(alice, {});457    const token = await collection.mintToken(alice);458    await token.approve(alice, {Substrate: bob.address});459460    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);461    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');462    463    const events: any = [];464    contract.events.allEvents((_: any, event: any) => {465      events.push(event);466    });467468    await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});469    470    const event = events[0];471    expect(event.address).to.be.equal(collectionAddress);472    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));473    expect(event.returnValues.to).to.be.equal(receiver);474    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);475  });476477  itEth('Events emitted for transfer()', async ({helper}) => {478    const receiver = helper.eth.createAccount();479480    const collection = await helper.nft.mintCollection(alice, {});481    const token = await collection.mintToken(alice);482483    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);484    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');485    486    const events: any = [];487    contract.events.allEvents((_: any, event: any) => {488      events.push(event);489    });490491    await token.transfer(alice, {Ethereum: receiver});492    493    const event = events[0];494    expect(event.address).to.be.equal(collectionAddress);495    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));496    expect(event.returnValues.to).to.be.equal(receiver);497    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);498  });499});500501describe('Common metadata', () => {502  let donor: IKeyringPair;503  let alice: IKeyringPair;504505  before(async function() {506    await usingEthPlaygrounds(async (helper, privateKey) => {507      donor = privateKey('//Alice');508      [alice] = await helper.arrange.createAccounts([20n], donor);509    });510  });511512  itEth('Returns collection name', async ({helper}) => {513    const caller = await helper.eth.createAccountWithBalance(donor);514    const tokenPropertyPermissions = [{515      key: 'URI',516      permission: {517        mutable: true,518        collectionAdmin: true,519        tokenOwner: false,520      },521    }];522    const collection = await helper.nft.mintCollection(523      alice,524      {525        name: 'oh River',526        tokenPrefix: 'CHANGE',527        properties: [{key: 'ERC721Metadata', value: '1'}],528        tokenPropertyPermissions,529      },530    );531532    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);533    const name = await contract.methods.name().call();534    expect(name).to.equal('oh River');535  });536537  itEth('Returns symbol name', async ({helper}) => {538    const caller = await helper.eth.createAccountWithBalance(donor);539    const tokenPropertyPermissions = [{540      key: 'URI',541      permission: {542        mutable: true,543        collectionAdmin: true,544        tokenOwner: false,545      },546    }];547    const collection = await helper.nft.mintCollection(548      alice,549      {550        name: 'oh River',551        tokenPrefix: 'CHANGE',552        properties: [{key: 'ERC721Metadata', value: '1'}],553        tokenPropertyPermissions,554      },555    );556557    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);558    const symbol = await contract.methods.symbol().call();559    expect(symbol).to.equal('CHANGE');560  });561});