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

difftreelog

test(nft) check for name/symbol availability

Yaroslav Bolyukin2022-10-13parent: #9695a43.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/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  });71});7273describe('Check ERC721 token URI for NFT', () => {74  let donor: IKeyringPair;7576  before(async function() {77    await usingEthPlaygrounds(async (_helper, privateKey) => {78      donor = privateKey('//Alice');79    });80  });8182  async function setup(helper: EthUniqueHelper, baseUri: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {83    const owner = await helper.eth.createAccountWithBalance(donor);84    const receiver = helper.eth.createAccount();8586    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', 'a', 'b', baseUri);87    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);88    89    const nextTokenId = await contract.methods.nextTokenId().call();90    expect(nextTokenId).to.be.equal('1');91    const result = await contract.methods.mint(92      receiver,93      nextTokenId,94    ).send();9596    if (propertyKey && propertyValue) {97      // Set URL or suffix98      await contract.methods.setProperty(nextTokenId, propertyKey, Buffer.from(propertyValue)).send();99    }100101    const event = result.events.Transfer;102    expect(event.address).to.be.equal(collectionAddress);103    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');104    expect(event.returnValues.to).to.be.equal(receiver);105    expect(event.returnValues.tokenId).to.be.equal(nextTokenId);106107    return {contract, nextTokenId};108  }109110  itEth('Empty tokenURI', async ({helper}) => {111    const {contract, nextTokenId} = await setup(helper, '');112    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');113  });114115  itEth('TokenURI from url', async ({helper}) => {116    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URI', 'Token URI');117    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');118  });119120  itEth('TokenURI from baseURI', async ({helper}) => {121    const {contract, nextTokenId} = await setup(helper, 'BaseURI_');122    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_');123  });124125  itEth('TokenURI from baseURI + suffix', async ({helper}) => {126    const suffix = '/some/suffix';127    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URISuffix', suffix);128    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);129  });130});131132describe('NFT: Plain calls', () => {133  let donor: IKeyringPair;134  let alice: IKeyringPair;135136  before(async function() {137    await usingEthPlaygrounds(async (helper, privateKey) => {138      donor = privateKey('//Alice');139      [alice] = await helper.arrange.createAccounts([10n], donor);140    });141  });142143  itEth('Can perform mint()', async ({helper}) => {144    const owner = await helper.eth.createAccountWithBalance(donor);145    const receiver = helper.eth.createAccount();146147    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', '');148    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);149    const nextTokenId = await contract.methods.nextTokenId().call();150151    expect(nextTokenId).to.be.equal('1');152    const result = await contract.methods.mintWithTokenURI(153      receiver,154      nextTokenId,155      'Test URI',156    ).send();157158    const event = result.events.Transfer;159    expect(event.address).to.be.equal(collectionAddress);160    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');161    expect(event.returnValues.to).to.be.equal(receiver);162    expect(event.returnValues.tokenId).to.be.equal(nextTokenId);163164    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');165166    // TODO: this wont work right now, need release 919000 first167    // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();168    // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();169    // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);170  });171172  //TODO: CORE-302 add eth methods173  itEth.skip('Can perform mintBulk()', async ({helper}) => {174    const caller = await helper.eth.createAccountWithBalance(donor);175    const receiver = helper.eth.createAccount();176177    const collection = await helper.nft.mintCollection(alice);178    await collection.addAdmin(alice, {Ethereum: caller});179180    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);181    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);182    {183      const bulkSize = 3;184      const nextTokenId = await contract.methods.nextTokenId().call();185      expect(nextTokenId).to.be.equal('1');186      const result = await contract.methods.mintBulkWithTokenURI(187        receiver,188        Array.from({length: bulkSize}, (_, i) => (189          [+nextTokenId + i, `Test URI ${i}`]190        )),191      ).send({from: caller});192193      const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.tokenId - b.returnValues.tokenId);194      for (let i = 0; i < bulkSize; i++) {195        const event = events[i];196        expect(event.address).to.equal(collectionAddress);197        expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');198        expect(event.returnValues.to).to.equal(receiver);199        expect(event.returnValues.tokenId).to.equal(`${+nextTokenId + i}`);200201        expect(await contract.methods.tokenURI(+nextTokenId + i).call()).to.be.equal(`Test URI ${i}`);202      }203    }204  });205206  itEth('Can perform burn()', async ({helper}) => {207    const caller = await helper.eth.createAccountWithBalance(donor);208209    const collection = await helper.nft.mintCollection(alice, {});210    const {tokenId} = await collection.mintToken(alice, {Ethereum: caller});211212    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);213    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);214215    {216      const result = await contract.methods.burn(tokenId).send({from: caller});217      218      const event = result.events.Transfer;219      expect(event.address).to.be.equal(collectionAddress);220      expect(event.returnValues.from).to.be.equal(caller);221      expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');222      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);223    }224  });225226  itEth('Can perform approve()', async ({helper}) => {227    const owner = await helper.eth.createAccountWithBalance(donor);228    const spender = helper.eth.createAccount();229230    const collection = await helper.nft.mintCollection(alice, {});231    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});232233    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);234    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);235236    {237      const result = await contract.methods.approve(spender, tokenId).send({from: owner});238239      const event = result.events.Approval;240      expect(event.address).to.be.equal(collectionAddress);241      expect(event.returnValues.owner).to.be.equal(owner);242      expect(event.returnValues.approved).to.be.equal(spender);243      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);244    }245  });246247  itEth('Can perform transferFrom()', async ({helper}) => {248    const owner = await helper.eth.createAccountWithBalance(donor);249    const spender = await helper.eth.createAccountWithBalance(donor);250    const receiver = helper.eth.createAccount();251252    const collection = await helper.nft.mintCollection(alice, {});253    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});254255    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);256    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);257258    await contract.methods.approve(spender, tokenId).send({from: owner});259260    {261      const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});262263      const event = result.events.Transfer;264      expect(event.address).to.be.equal(collectionAddress);265      expect(event.returnValues.from).to.be.equal(owner);266      expect(event.returnValues.to).to.be.equal(receiver);267      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);268    }269270    {271      const balance = await contract.methods.balanceOf(receiver).call();272      expect(+balance).to.equal(1);273    }274275    {276      const balance = await contract.methods.balanceOf(owner).call();277      expect(+balance).to.equal(0);278    }279  });280281  itEth('Can perform transfer()', async ({helper}) => {282    const collection = await helper.nft.mintCollection(alice, {});283    const owner = await helper.eth.createAccountWithBalance(donor);284    const receiver = helper.eth.createAccount();285286    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});287288    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);289    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);290291    {292      const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});293294      const event = result.events.Transfer;295      expect(event.address).to.be.equal(collectionAddress);296      expect(event.returnValues.from).to.be.equal(owner);297      expect(event.returnValues.to).to.be.equal(receiver);298      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);299    }300301    {302      const balance = await contract.methods.balanceOf(owner).call();303      expect(+balance).to.equal(0);304    }305306    {307      const balance = await contract.methods.balanceOf(receiver).call();308      expect(+balance).to.equal(1);309    }310  });311});312313describe('NFT: Fees', () => {314  let donor: IKeyringPair;315  let alice: IKeyringPair;316317  before(async function() {318    await usingEthPlaygrounds(async (helper, privateKey) => {319      donor = privateKey('//Alice');320      [alice] = await helper.arrange.createAccounts([10n], donor);321    });322  });323  324  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {325    const owner = await helper.eth.createAccountWithBalance(donor);326    const spender = helper.eth.createAccount();327328    const collection = await helper.nft.mintCollection(alice, {});329    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});330331    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);332333    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));334    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));335  });336337  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {338    const owner = await helper.eth.createAccountWithBalance(donor);339    const spender = await helper.eth.createAccountWithBalance(donor);340341    const collection = await helper.nft.mintCollection(alice, {});342    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});343344    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);345346    await contract.methods.approve(spender, tokenId).send({from: owner});347348    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));349    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));350  });351352  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {353    const owner = await helper.eth.createAccountWithBalance(donor);354    const receiver = helper.eth.createAccount();355356    const collection = await helper.nft.mintCollection(alice, {});357    const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});358359    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);360361    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));362    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));363  });364});365366describe('NFT: Substrate calls', () => {367  let donor: IKeyringPair;368  let alice: IKeyringPair;369370  before(async function() {371    await usingEthPlaygrounds(async (helper, privateKey) => {372      donor = privateKey('//Alice');373      [alice] = await helper.arrange.createAccounts([20n], donor);374    });375  });376377  itEth('Events emitted for mint()', async ({helper}) => {378    const collection = await helper.nft.mintCollection(alice, {});379    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);380    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');381382    const events: any = [];383    contract.events.allEvents((_: any, event: any) => {384      events.push(event);385    });386    const {tokenId} = await collection.mintToken(alice);387388    const event = events[0];389    expect(event.event).to.be.equal('Transfer');390    expect(event.address).to.be.equal(collectionAddress);391    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');392    expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));393    expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());394  });395396  itEth('Events emitted for burn()', async ({helper}) => {397    const collection = await helper.nft.mintCollection(alice, {});398    const token = await collection.mintToken(alice);399400    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);401    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');402    403    const events: any = [];404    contract.events.allEvents((_: any, event: any) => {405      events.push(event);406    });407408    await token.burn(alice);409410    const event = events[0];411    expect(event.event).to.be.equal('Transfer');412    expect(event.address).to.be.equal(collectionAddress);413    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));414    expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');415    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());416  });417418  itEth('Events emitted for approve()', async ({helper}) => {419    const receiver = helper.eth.createAccount();420421    const collection = await helper.nft.mintCollection(alice, {});422    const token = await collection.mintToken(alice);423424    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);425    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');426    427    const events: any = [];428    contract.events.allEvents((_: any, event: any) => {429      events.push(event);430    });431432    await token.approve(alice, {Ethereum: receiver});433434    const event = events[0];435    expect(event.event).to.be.equal('Approval');436    expect(event.address).to.be.equal(collectionAddress);437    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));438    expect(event.returnValues.approved).to.be.equal(receiver);439    expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());440  });441442  itEth('Events emitted for transferFrom()', async ({helper}) => {443    const [bob] = await helper.arrange.createAccounts([10n], donor);444    const receiver = helper.eth.createAccount();445446    const collection = await helper.nft.mintCollection(alice, {});447    const token = await collection.mintToken(alice);448    await token.approve(alice, {Substrate: bob.address});449450    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);451    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');452    453    const events: any = [];454    contract.events.allEvents((_: any, event: any) => {455      events.push(event);456    });457458    await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});459    460    const event = events[0];461    expect(event.address).to.be.equal(collectionAddress);462    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));463    expect(event.returnValues.to).to.be.equal(receiver);464    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);465  });466467  itEth('Events emitted for transfer()', async ({helper}) => {468    const receiver = helper.eth.createAccount();469470    const collection = await helper.nft.mintCollection(alice, {});471    const token = await collection.mintToken(alice);472473    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);474    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');475    476    const events: any = [];477    contract.events.allEvents((_: any, event: any) => {478      events.push(event);479    });480481    await token.transfer(alice, {Ethereum: receiver});482    483    const event = events[0];484    expect(event.address).to.be.equal(collectionAddress);485    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));486    expect(event.returnValues.to).to.be.equal(receiver);487    expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);488  });489});490491describe('Common metadata', () => {492  let donor: IKeyringPair;493  let alice: IKeyringPair;494495  before(async function() {496    await usingEthPlaygrounds(async (helper, privateKey) => {497      donor = privateKey('//Alice');498      [alice] = await helper.arrange.createAccounts([20n], donor);499    });500  });501502  itEth('Returns collection name', async ({helper}) => {503    const caller = await helper.eth.createAccountWithBalance(donor);504    const tokenPropertyPermissions = [{505      key: 'URI',506      permission: {507        mutable: true,508        collectionAdmin: true,509        tokenOwner: false,510      },511    }];512    const collection = await helper.nft.mintCollection(513      alice,514      {515        name: 'oh River',516        tokenPrefix: 'CHANGE',517        properties: [{key: 'ERC721Metadata', value: '1'}],518        tokenPropertyPermissions,519      },520    );521522    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);523    const name = await contract.methods.name().call();524    expect(name).to.equal('oh River');525  });526527  itEth('Returns symbol name', async ({helper}) => {528    const caller = await helper.eth.createAccountWithBalance(donor);529    const tokenPropertyPermissions = [{530      key: 'URI',531      permission: {532        mutable: true,533        collectionAdmin: true,534        tokenOwner: false,535      },536    }];537    const collection = await helper.nft.mintCollection(538      alice,539      {540        name: 'oh River',541        tokenPrefix: 'CHANGE',542        properties: [{key: 'ERC721Metadata', value: '1'}],543        tokenPropertyPermissions,544      },545    );546547    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);548    const symbol = await contract.methods.symbol().call();549    expect(symbol).to.equal('CHANGE');550  });551});