git.delta.rocks / unique-network / refs/commits / 3b7dec1d258f

difftreelog

source

tests/src/eth/reFungibleToken.test.ts21.2 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 {Pallets, requirePalletsOrSkip} from '../util/playgrounds';18import {EthUniqueHelper, expect, itEth, usingEthPlaygrounds} from './util/playgrounds';19import {IKeyringPair} from '@polkadot/types/types';20import {Contract} from 'web3-eth-contract';2122describe('Refungible token: Information getting', () => {23  let donor: IKeyringPair;24  let alice: IKeyringPair;2526  before(async function() {27    await usingEthPlaygrounds(async (helper, privateKey) => {28      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);2930      donor = privateKey('//Alice');31      [alice] = await helper.arrange.createAccounts([20n], donor);32    });33  });3435  itEth('totalSupply', async ({helper}) => {36    const caller = await helper.eth.createAccountWithBalance(donor);37    const collection = await helper.rft.mintCollection(alice, {tokenPrefix: 'MUON'});38    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: caller});3940    const contract = helper.ethNativeContract.rftTokenById(collection.collectionId, tokenId, caller);41    const totalSupply = await contract.methods.totalSupply().call();42    expect(totalSupply).to.equal('200');43  });4445  itEth('balanceOf', async ({helper}) => {46    const caller = await helper.eth.createAccountWithBalance(donor);47    const collection = await helper.rft.mintCollection(alice, {tokenPrefix: 'MUON'});48    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: caller});4950    const contract = helper.ethNativeContract.rftTokenById(collection.collectionId, tokenId, caller);51    const balance = await contract.methods.balanceOf(caller).call();52    expect(balance).to.equal('200');53  });5455  itEth('decimals', async ({helper}) => {56    const caller = await helper.eth.createAccountWithBalance(donor);57    const collection = await helper.rft.mintCollection(alice, {tokenPrefix: 'MUON'});58    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: caller});5960    const contract = helper.ethNativeContract.rftTokenById(collection.collectionId, tokenId, caller);61    const decimals = await contract.methods.decimals().call();62    expect(decimals).to.equal('0');63  });64});6566// FIXME: Need erc721 for ReFubgible.67describe('Check ERC721 token URI for ReFungible', () => {68  let donor: IKeyringPair;6970  before(async function() {71    await usingEthPlaygrounds(async (helper, privateKey) => {72      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);7374      donor = privateKey('//Alice');75    });76  });7778  async function setup(helper: EthUniqueHelper, tokenPrefix: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {79    const owner = await helper.eth.createAccountWithBalance(donor);80    const receiver = helper.eth.createAccount();8182    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);83    let result = await collectionHelper.methods.createERC721MetadataCompatibleCollection('Mint collection', 'a', 'b', tokenPrefix).send();84    const collectionAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);85    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);86    87    const nextTokenId = await contract.methods.nextTokenId().call();88    expect(nextTokenId).to.be.equal('1');89    result = await contract.methods.mint(90      receiver,91      nextTokenId,92    ).send();9394    if (propertyKey && propertyValue) {95      // Set URL or suffix96      await contract.methods.setProperty(nextTokenId, propertyKey, Buffer.from(propertyValue)).send();97    }9899    const event = result.events.Transfer;100    expect(event.address).to.be.equal(collectionAddress);101    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');102    expect(event.returnValues.to).to.be.equal(receiver);103    expect(event.returnValues.tokenId).to.be.equal(nextTokenId);104105    return {contract, nextTokenId};106  }107108  itEth('Empty tokenURI', async ({helper}) => {109    const {contract, nextTokenId} = await setup(helper, '');110    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');111  });112113  itEth('TokenURI from url', async ({helper}) => {114    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'url', 'Token URI');115    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');116  });117118  itEth('TokenURI from baseURI + tokenId', async ({helper}) => {119    const {contract, nextTokenId} = await setup(helper, 'BaseURI_');120    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + nextTokenId);121  });122123  itEth('TokenURI from baseURI + suffix', async ({helper}) => {124    const suffix = '/some/suffix';125    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'suffix', suffix);126    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);127  });128});129130describe('Refungible: Plain calls', () => {131  let donor: IKeyringPair;132  let alice: IKeyringPair;133134  before(async function() {135    await usingEthPlaygrounds(async (helper, privateKey) => {136      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);137138      donor = privateKey('//Alice');139      [alice] = await helper.arrange.createAccounts([50n], donor);140    });141  });142143  itEth('Can perform approve()', async ({helper}) => {144    const owner = await helper.eth.createAccountWithBalance(donor);145    const spender = helper.eth.createAccount();146    const collection = await helper.rft.mintCollection(alice);147    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});148149    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);150    const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);151152    {153      const result = await contract.methods.approve(spender, 100).send({from: owner});154      const event = result.events.Approval;155      expect(event.address).to.be.equal(tokenAddress);156      expect(event.returnValues.owner).to.be.equal(owner);157      expect(event.returnValues.spender).to.be.equal(spender);158      expect(event.returnValues.value).to.be.equal('100');159    }160161    {162      const allowance = await contract.methods.allowance(owner, spender).call();163      expect(+allowance).to.equal(100);164    }165  });166167  itEth('Can perform transferFrom()', async ({helper}) => {168    const owner = await helper.eth.createAccountWithBalance(donor);169    const spender = await helper.eth.createAccountWithBalance(donor);170    const receiver = helper.eth.createAccount();171    const collection = await helper.rft.mintCollection(alice);172    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});173174    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);175    const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);176177    await contract.methods.approve(spender, 100).send();178179    {180      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});181      let event = result.events.Transfer;182      expect(event.address).to.be.equal(tokenAddress);183      expect(event.returnValues.from).to.be.equal(owner);184      expect(event.returnValues.to).to.be.equal(receiver);185      expect(event.returnValues.value).to.be.equal('49');186187      event = result.events.Approval;188      expect(event.address).to.be.equal(tokenAddress);189      expect(event.returnValues.owner).to.be.equal(owner);190      expect(event.returnValues.spender).to.be.equal(spender);191      expect(event.returnValues.value).to.be.equal('51');192    }193194    {195      const balance = await contract.methods.balanceOf(receiver).call();196      expect(+balance).to.equal(49);197    }198199    {200      const balance = await contract.methods.balanceOf(owner).call();201      expect(+balance).to.equal(151);202    }203  });204205  itEth('Can perform transfer()', async ({helper}) => {206    const owner = await helper.eth.createAccountWithBalance(donor);207    const receiver = helper.eth.createAccount();208    const collection = await helper.rft.mintCollection(alice);209    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});210211    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);212    const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);213214    {215      const result = await contract.methods.transfer(receiver, 50).send({from: owner});216      const event = result.events.Transfer;217      expect(event.address).to.be.equal(tokenAddress);218      expect(event.returnValues.from).to.be.equal(owner);219      expect(event.returnValues.to).to.be.equal(receiver);220      expect(event.returnValues.value).to.be.equal('50');221    }222223    {224      const balance = await contract.methods.balanceOf(owner).call();225      expect(+balance).to.equal(150);226    }227228    {229      const balance = await contract.methods.balanceOf(receiver).call();230      expect(+balance).to.equal(50);231    }232  });233234  itEth('Can perform repartition()', async ({helper}) => {235    const owner = await helper.eth.createAccountWithBalance(donor);236    const receiver = await helper.eth.createAccountWithBalance(donor);237    const collection = await helper.rft.mintCollection(alice);238    const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});239240    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);241    const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);242243    await contract.methods.repartition(200).send({from: owner});244    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(200);245    await contract.methods.transfer(receiver, 110).send({from: owner});246    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(90);247    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(110);248249    await expect(contract.methods.repartition(80).send({from: owner})).to.eventually.be.rejected; // Transaction is reverted250251    await contract.methods.transfer(receiver, 90).send({from: owner});252    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);253    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);254255    await contract.methods.repartition(150).send({from: receiver});256    await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected; // Transaction is reverted257    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);258  });259260  itEth('Can repartition with increased amount', async ({helper}) => {261    const owner = await helper.eth.createAccountWithBalance(donor);262    const collection = await helper.rft.mintCollection(alice);263    const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});264265    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);266    const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);267268    const result = await contract.methods.repartition(200).send();269270    const event = result.events.Transfer;271    expect(event.address).to.be.equal(tokenAddress);272    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');273    expect(event.returnValues.to).to.be.equal(owner);274    expect(event.returnValues.value).to.be.equal('100');275  });276277  itEth('Can repartition with decreased amount', async ({helper}) => {278    const owner = await helper.eth.createAccountWithBalance(donor);279    const collection = await helper.rft.mintCollection(alice);280    const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});281282    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);283    const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);284285    const result = await contract.methods.repartition(50).send();286    const event = result.events.Transfer;287    expect(event.address).to.be.equal(tokenAddress);288    expect(event.returnValues.from).to.be.equal(owner);289    expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');290    expect(event.returnValues.value).to.be.equal('50');291  });292293  itEth('Receiving Transfer event on burning into full ownership', async ({helper}) => {294    const caller = await helper.eth.createAccountWithBalance(donor);295    const receiver = await helper.eth.createAccountWithBalance(donor);296    const {collectionId, collectionAddress} = await helper.eth.createRefungibleCollection(caller, 'Devastation', '6', '6');297    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);298299    const tokenId = await contract.methods.nextTokenId().call();300    await contract.methods.mint(caller, tokenId).send();301    const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);302    const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, caller);303304    await tokenContract.methods.repartition(2).send();305    await tokenContract.methods.transfer(receiver, 1).send();306307    const events: any = [];308    contract.events.allEvents((_: any, event: any) => {309      events.push(event);310    });311    await tokenContract.methods.burnFrom(caller, 1).send();312313    const event = events[0];314    expect(event.address).to.be.equal(collectionAddress);315    expect(event.returnValues.from).to.be.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');316    expect(event.returnValues.to).to.be.equal(receiver);317    expect(event.returnValues.tokenId).to.be.equal(tokenId);318  });319});320321describe('Refungible: Fees', () => {322  let donor: IKeyringPair;323  let alice: IKeyringPair;324325  before(async function() {326    await usingEthPlaygrounds(async (helper, privateKey) => {327      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);328329      donor = privateKey('//Alice');330      [alice] = await helper.arrange.createAccounts([50n], donor);331    });332  });333334  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();337    const collection = await helper.rft.mintCollection(alice);338    const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});339340    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);341    const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);342343    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).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);350    const collection = await helper.rft.mintCollection(alice);351    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});352353    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);354    const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);355356    await contract.methods.approve(spender, 100).send({from: owner});357358    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).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();365    const collection = await helper.rft.mintCollection(alice);366    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});367368    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);369    const contract = helper.ethNativeContract.rftToken(tokenAddress, owner);370371    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));372    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));373  });374});375376describe('Refungible: Substrate calls', () => {377  let donor: IKeyringPair;378  let alice: IKeyringPair;379380  before(async function() {381    await usingEthPlaygrounds(async (helper, privateKey) => {382      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);383384      donor = privateKey('//Alice');385      [alice] = await helper.arrange.createAccounts([50n], donor);386    });387  });388389  itEth('Events emitted for approve()', async ({helper}) => {390    const receiver = helper.eth.createAccount();391    const collection = await helper.rft.mintCollection(alice);392    const token = await collection.mintToken(alice, 200n);393394    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);395    const contract = helper.ethNativeContract.rftToken(tokenAddress);396397    const events: any = [];398    contract.events.allEvents((_: any, event: any) => {399      events.push(event);400    });401    expect(await token.approve(alice, {Ethereum: receiver}, 100n)).to.be.true;402403    const event = events[0];404    expect(event.event).to.be.equal('Approval');405    expect(event.address).to.be.equal(tokenAddress);406    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));407    expect(event.returnValues.spender).to.be.equal(receiver);408    expect(event.returnValues.value).to.be.equal('100');409  });410411  itEth('Events emitted for transferFrom()', async ({helper}) => {412    const [bob] = await helper.arrange.createAccounts([10n], donor);413    const receiver = helper.eth.createAccount();414    const collection = await helper.rft.mintCollection(alice);415    const token = await collection.mintToken(alice, 200n);416    await token.approve(alice, {Substrate: bob.address}, 100n);417418    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);419    const contract = helper.ethNativeContract.rftToken(tokenAddress);420421    const events: any = [];422    contract.events.allEvents((_: any, event: any) => {423      events.push(event);424    });425426    expect(await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver},  51n)).to.be.true;427428    let event = events[0];429    expect(event.event).to.be.equal('Transfer');430    expect(event.address).to.be.equal(tokenAddress);431    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));432    expect(event.returnValues.to).to.be.equal(receiver);433    expect(event.returnValues.value).to.be.equal('51');434435    event = events[1];436    expect(event.event).to.be.equal('Approval');437    expect(event.address).to.be.equal(tokenAddress);438    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));439    expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));440    expect(event.returnValues.value).to.be.equal('49');441  });442443  itEth('Events emitted for transfer()', async ({helper}) => {444    const receiver = helper.eth.createAccount();445    const collection = await helper.rft.mintCollection(alice);446    const token = await collection.mintToken(alice, 200n);447448    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);449    const contract = helper.ethNativeContract.rftToken(tokenAddress);450451    const events: any = [];452    contract.events.allEvents((_: any, event: any) => {453      events.push(event);454    });455456    expect(await token.transfer(alice, {Ethereum: receiver},  51n)).to.be.true;457458    const event = events[0];459    expect(event.event).to.be.equal('Transfer');460    expect(event.address).to.be.equal(tokenAddress);461    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));462    expect(event.returnValues.to).to.be.equal(receiver);463    expect(event.returnValues.value).to.be.equal('51');464  });465});466467describe('ERC 1633 implementation', () => {468  let donor: IKeyringPair;469470  before(async function() {471    await usingEthPlaygrounds(async (helper, privateKey) => {472      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);473474      donor = privateKey('//Alice');475    });476  });477478  itEth('Default parent token address and id', async ({helper}) => {479    const owner = await helper.eth.createAccountWithBalance(donor);480481    const {collectionId, collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Sands', '', 'GRAIN');482    const collectionContract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);483    484    const tokenId = await collectionContract.methods.nextTokenId().call();485    await collectionContract.methods.mint(owner, tokenId).send();486    const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);487    const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, owner);488489    expect(await tokenContract.methods.parentToken().call()).to.be.equal(collectionAddress);490    expect(await tokenContract.methods.parentTokenId().call()).to.be.equal(tokenId);491  });492});