git.delta.rocks / unique-network / refs/commits / 7f04dc961d64

difftreelog

source

tests/src/eth/reFungibleToken.test.ts32.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';18import {EthUniqueHelper, expect, itEth, usingEthPlaygrounds} from './util';19import {IKeyringPair} from '@polkadot/types/types';20import {Contract} from 'web3-eth-contract';2122// FIXME: Need erc721 for ReFubgible.23describe('Check ERC721 token URI for ReFungible', () => {24  let donor: IKeyringPair;2526  before(async function() {27    await usingEthPlaygrounds(async (helper, privateKey) => {28      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);2930      donor = await privateKey({url: import.meta.url});31    });32  });3334  async function setup(helper: EthUniqueHelper, baseUri: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {35    const owner = await helper.eth.createAccountWithBalance(donor);36    const receiver = helper.eth.createAccount();3738    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'Mint collection', 'a', 'b', baseUri);39    const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', owner);4041    const result = await contract.methods.mint(receiver).send();4243    const event = result.events.Transfer;44    const tokenId = event.returnValues.tokenId;45    expect(tokenId).to.be.equal('1');46    expect(event.address).to.be.equal(collectionAddress);47    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');48    expect(event.returnValues.to).to.be.equal(receiver);4950    if(propertyKey && propertyValue) {51      // Set URL or suffix5253      await contract.methods.setProperties(tokenId, [{key: propertyKey, value: Buffer.from(propertyValue)}]).send();54    }5556    return {contract, nextTokenId: tokenId};57  }5859  itEth('Empty tokenURI', async ({helper}) => {60    const {contract, nextTokenId} = await setup(helper, '');61    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');62  });6364  itEth('TokenURI from url', async ({helper}) => {65    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URI', 'Token URI');66    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');67  });6869  itEth('TokenURI from baseURI', async ({helper}) => {70    const {contract, nextTokenId} = await setup(helper, 'BaseURI_');71    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_');72  });7374  itEth('TokenURI from baseURI + suffix', async ({helper}) => {75    const suffix = '/some/suffix';76    const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URISuffix', suffix);77    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);78  });79});8081describe('Refungible: Plain calls', () => {82  let donor: IKeyringPair;83  let alice: IKeyringPair;8485  before(async function() {86    await usingEthPlaygrounds(async (helper, privateKey) => {87      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);8889      donor = await privateKey({url: import.meta.url});90      [alice] = await helper.arrange.createAccounts([50n], donor);91    });92  });9394  itEth('Can perform approve()', async ({helper}) => {95    const owner = await helper.eth.createAccountWithBalance(donor);96    const spender = helper.eth.createAccount();97    const collection = await helper.rft.mintCollection(alice);98    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});99100    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);101    const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);102103    {104      const result = await contract.methods.approve(spender, 100).send({from: owner});105      const event = result.events.Approval;106      expect(event.address).to.be.equal(tokenAddress);107      expect(event.returnValues.owner).to.be.equal(owner);108      expect(event.returnValues.spender).to.be.equal(spender);109      expect(event.returnValues.value).to.be.equal('100');110    }111112    {113      const allowance = await contract.methods.allowance(owner, spender).call();114      expect(+allowance).to.equal(100);115    }116  });117118  itEth('Can perform approveCross()', async ({helper}) => {119    const owner = await helper.eth.createAccountWithBalance(donor);120    const spender = helper.eth.createAccount();121    const spenderSub = (await helper.arrange.createAccounts([1n], donor))[0];122    const spenderCrossEth = helper.ethCrossAccount.fromAddress(spender);123    const spenderCrossSub = helper.ethCrossAccount.fromKeyringPair(spenderSub);124125126    const collection = await helper.rft.mintCollection(alice);127    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});128129    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);130    const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);131132    {133      const result = await contract.methods.approveCross(spenderCrossEth, 100).send({from: owner});134      const event = result.events.Approval;135      expect(event.address).to.be.equal(tokenAddress);136      expect(event.returnValues.owner).to.be.equal(owner);137      expect(event.returnValues.spender).to.be.equal(spender);138      expect(event.returnValues.value).to.be.equal('100');139    }140141    {142      const allowance = await contract.methods.allowance(owner, spender).call();143      expect(+allowance).to.equal(100);144    }145146147    {148      const result = await contract.methods.approveCross(spenderCrossSub, 100).send({from: owner});149      const event = result.events.Approval;150      expect(event.address).to.be.equal(tokenAddress);151      expect(event.returnValues.owner).to.be.equal(owner);152      expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(spenderSub.address));153      expect(event.returnValues.value).to.be.equal('100');154    }155156    {157      const allowance = await collection.getTokenApprovedPieces(tokenId, {Ethereum: owner}, {Substrate: spenderSub.address});158      expect(allowance).to.equal(100n);159    }160161    {162      //TO-DO expect with future allowanceCross(owner, spenderCrossEth).call()163    }164  });165166  itEth('Non-owner and non admin cannot approveCross', async ({helper}) => {167    const nonOwner = await helper.eth.createAccountWithBalance(donor);168    const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner);169    const owner = await helper.eth.createAccountWithBalance(donor);170    const collection = await helper.rft.mintCollection(alice, {name: 'A', description: 'B', tokenPrefix: 'C'});171    const token = await collection.mintToken(alice, 100n, {Ethereum: owner});172173    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);174    const tokenEvm = await helper.ethNativeContract.rftToken(tokenAddress, owner);175176    await expect(tokenEvm.methods.approveCross(nonOwnerCross, 20).call({from: nonOwner})).to.be.rejectedWith('CantApproveMoreThanOwned');177  });178179  [180    'transferFrom',181    'transferFromCross',182  ].map(testCase =>183    itEth(`Can perform ${testCase}`, async ({helper}) => {184      const isCross = testCase === 'transferFromCross';185      const owner = await helper.eth.createAccountWithBalance(donor);186      const ownerCross = helper.ethCrossAccount.fromAddress(owner);187      const spender = await helper.eth.createAccountWithBalance(donor);188      const receiverEth = helper.eth.createAccount();189      const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);190      const [receiverSub] = await helper.arrange.createAccounts([1n], donor);191      const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);192193      const collection = await helper.rft.mintCollection(alice);194      const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});195196      const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);197      const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);198199      await contract.methods.approve(spender, 100).send({from: owner});200201      // 1. Can transfer from202      // 1.1 Plain ethereum or cross address:203      {204        const result = await contract.methods[testCase](205          isCross ? ownerCross : owner,206          isCross ? receiverCrossEth : receiverEth,207          49,208        ).send({from: spender});209210        // Check events:211        const transferEvent = result.events.Transfer;212        expect(transferEvent.address).to.be.equal(tokenAddress);213        expect(transferEvent.returnValues.from).to.be.equal(owner);214        expect(transferEvent.returnValues.to).to.be.equal(receiverEth);215        expect(transferEvent.returnValues.value).to.be.equal('49');216217        const approvalEvent = result.events.Approval;218        expect(approvalEvent.address).to.be.equal(tokenAddress);219        expect(approvalEvent.returnValues.owner).to.be.equal(owner);220        expect(approvalEvent.returnValues.spender).to.be.equal(spender);221        expect(approvalEvent.returnValues.value).to.be.equal('51');222223        // Check balances:224        const receiverBalance = await contract.methods.balanceOf(receiverEth).call();225        const ownerBalance = await contract.methods.balanceOf(owner).call();226227        expect(+receiverBalance).to.equal(49);228        expect(+ownerBalance).to.equal(151);229      }230231      // 1.2 Cross substrate address:232      if(testCase === 'transferFromCross') {233        const result = await contract.methods.transferFromCross(ownerCross, receiverCrossSub, 51).send({from: spender});234        // Check events:235        const transferEvent = result.events.Transfer;236        expect(transferEvent.address).to.be.equal(tokenAddress);237        expect(transferEvent.returnValues.from).to.be.equal(owner);238        expect(transferEvent.returnValues.to).to.be.equal(helper.address.substrateToEth(receiverSub.address));239        expect(transferEvent.returnValues.value).to.be.equal('51');240241        const approvalEvent = result.events.Approval;242        expect(approvalEvent.address).to.be.equal(tokenAddress);243        expect(approvalEvent.returnValues.owner).to.be.equal(owner);244        expect(approvalEvent.returnValues.spender).to.be.equal(spender);245        expect(approvalEvent.returnValues.value).to.be.equal('0');246247        // Check balances:248        const receiverBalance = await collection.getTokenBalance(tokenId, {Substrate: receiverSub.address});249        const ownerBalance = await contract.methods.balanceOf(owner).call();250        expect(receiverBalance).to.equal(51n);251        expect(+ownerBalance).to.equal(100);252      }253    }));254255  [256    'transfer',257    'transferCross',258  ].map(testCase =>259    itEth(`Can perform ${testCase}()`, async ({helper}) => {260      const isCross = testCase === 'transferCross';261      const owner = await helper.eth.createAccountWithBalance(donor);262      const receiverEth = helper.eth.createAccount();263      const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);264      const [receiverSub] = await helper.arrange.createAccounts([1n], donor);265      const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);266      const collection = await helper.rft.mintCollection(alice);267      const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});268269      const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);270      const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);271272      // 1. Can transfer to plain ethereum or cross-ethereum account:273      {274        const result = await contract.methods[testCase](isCross ? receiverCrossEth : receiverEth, 50).send({from: owner});275        // Check events276        const transferEvent = result.events.Transfer;277        expect(transferEvent.address).to.be.equal(tokenAddress);278        expect(transferEvent.returnValues.from).to.be.equal(owner);279        expect(transferEvent.returnValues.to).to.be.equal(receiverEth);280        expect(transferEvent.returnValues.value).to.be.equal('50');281        // Check balances:282        const ownerBalance = await contract.methods.balanceOf(owner).call();283        const receiverBalance = await contract.methods.balanceOf(receiverEth).call();284        expect(+ownerBalance).to.equal(150);285        expect(+receiverBalance).to.equal(50);286      }287288      // 2. Can transfer to cross-substrate account:289      if(isCross) {290        const result = await contract.methods.transferCross(receiverCrossSub, 50).send({from: owner});291        // Check events:292        const event = result.events.Transfer;293        expect(event.address).to.be.equal(tokenAddress);294        expect(event.returnValues.from).to.be.equal(owner);295        expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(receiverSub.address));296        expect(event.returnValues.value).to.be.equal('50');297        // Check balances:298        const ownerBalance = await contract.methods.balanceOf(owner).call();299        const receiverBalance = await collection.getTokenBalance(tokenId, {Substrate: receiverSub.address});300        expect(+ownerBalance).to.equal(100);301        expect(receiverBalance).to.equal(50n);302      }303    }));304305  [306    'transfer',307    'transferCross',308  ].map(testCase =>309    itEth(`Cannot ${testCase}() non-owned token`, async ({helper}) => {310      const isCross = testCase === 'transferCross';311      const owner = await helper.eth.createAccountWithBalance(donor);312      const ownerCross = helper.ethCrossAccount.fromAddress(owner);313      const receiverEth = await helper.eth.createAccountWithBalance(donor);314      const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);315      const collection = await helper.rft.mintCollection(alice);316      const rftOwner = await collection.mintToken(alice, 10n, {Ethereum: owner});317      const rftReceiver = await collection.mintToken(alice, 10n, {Ethereum: receiverEth});318      const tokenIdNonExist = 9999999;319320      const tokenAddress1 = helper.ethAddress.fromTokenId(collection.collectionId, rftOwner.tokenId);321      const tokenAddress2 = helper.ethAddress.fromTokenId(collection.collectionId, rftReceiver.tokenId);322      const tokenAddressNonExist = helper.ethAddress.fromTokenId(collection.collectionId, tokenIdNonExist);323      const tokenEvmOwner = await helper.ethNativeContract.rftToken(tokenAddress1, owner);324      const tokenEvmReceiver = await helper.ethNativeContract.rftToken(tokenAddress2, owner);325      const tokenEvmNonExist = await helper.ethNativeContract.rftToken(tokenAddressNonExist, owner);326327      // 1. Can transfer zero amount (EIP-20):328      await tokenEvmOwner.methods[testCase](isCross ? receiverCrossEth : receiverEth, 0).send({from: owner});329      // 2. Cannot transfer non-owned token:330      await expect(tokenEvmReceiver.methods[testCase](isCross ? ownerCross : owner, 0).send({from: owner})).to.be.rejected;331      await expect(tokenEvmReceiver.methods[testCase](isCross ? ownerCross : owner, 5).send({from: owner})).to.be.rejected;332      // 3. Cannot transfer non-existing token:333      await expect(tokenEvmNonExist.methods[testCase](isCross ? ownerCross : owner, 0).send({from: owner})).to.be.rejected;334      await expect(tokenEvmNonExist.methods[testCase](isCross ? ownerCross : owner, 5).send({from: owner})).to.be.rejected;335336      // 4. Storage is not corrupted:337      expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);338      expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: receiverEth.toLowerCase()}]);339      expect(await helper.rft.getTokenTop10Owners(collection.collectionId, tokenIdNonExist)).to.deep.eq([]);340341      // 4.1 Tokens can be transferred:342      await tokenEvmOwner.methods[testCase](isCross ? receiverCrossEth : receiverEth, 10).send({from: owner});343      await tokenEvmReceiver.methods[testCase](isCross ? ownerCross : owner, 10).send({from: receiverEth});344      expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: receiverEth.toLowerCase()}]);345      expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);346    }));347348  itEth('Can perform repartition()', async ({helper}) => {349    const owner = await helper.eth.createAccountWithBalance(donor);350    const receiver = await helper.eth.createAccountWithBalance(donor);351    const collection = await helper.rft.mintCollection(alice);352    const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});353354    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);355    const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);356357    await contract.methods.repartition(200).send({from: owner});358    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(200);359    await contract.methods.transfer(receiver, 110).send({from: owner});360    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(90);361    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(110);362363    await expect(contract.methods.repartition(80).send({from: owner})).to.eventually.be.rejected; // Transaction is reverted364365    await contract.methods.transfer(receiver, 90).send({from: owner});366    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);367    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);368369    await contract.methods.repartition(150).send({from: receiver});370    await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected; // Transaction is reverted371    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);372  });373374  itEth('Can repartition with increased amount', async ({helper}) => {375    const owner = await helper.eth.createAccountWithBalance(donor);376    const collection = await helper.rft.mintCollection(alice);377    const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});378379    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);380    const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);381382    const result = await contract.methods.repartition(200).send();383384    const event = result.events.Transfer;385    expect(event.address).to.be.equal(tokenAddress);386    expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');387    expect(event.returnValues.to).to.be.equal(owner);388    expect(event.returnValues.value).to.be.equal('100');389  });390391  itEth('Can repartition with decreased amount', async ({helper}) => {392    const owner = await helper.eth.createAccountWithBalance(donor);393    const collection = await helper.rft.mintCollection(alice);394    const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});395396    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);397    const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);398399    const result = await contract.methods.repartition(50).send();400    const event = result.events.Transfer;401    expect(event.address).to.be.equal(tokenAddress);402    expect(event.returnValues.from).to.be.equal(owner);403    expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');404    expect(event.returnValues.value).to.be.equal('50');405  });406407  itEth('Receiving Transfer event on burning into full ownership', async ({helper}) => {408    const caller = await helper.eth.createAccountWithBalance(donor);409    const receiver = await helper.eth.createAccountWithBalance(donor);410    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Devastation', '6', '6');411    const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', caller);412413    const result = await contract.methods.mint(caller).send();414    const tokenId = result.events.Transfer.returnValues.tokenId;415    const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);416    const tokenContract = await helper.ethNativeContract.rftToken(tokenAddress, caller, true);417418    await tokenContract.methods.repartition(2).send();419    await tokenContract.methods.transfer(receiver, 1).send();420421    const events: any = [];422    contract.events.allEvents((_: any, event: any) => {423      events.push(event);424    });425    await tokenContract.methods.burnFrom(caller, 1).send();426427    if(events.length == 0) await helper.wait.newBlocks(1);428    const event = events[0];429    expect(event.address).to.be.equal(collectionAddress);430    expect(event.returnValues.from).to.be.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');431    expect(event.returnValues.to).to.be.equal(receiver);432    expect(event.returnValues.tokenId).to.be.equal(tokenId);433  });434435  itEth('Can perform burnFromCross()', async ({helper}) => {436    const owner = await helper.eth.createAccountWithBalance(donor);437    const ownerSub = (await helper.arrange.createAccounts([10n], donor))[0];438    const ownerCross = helper.ethCrossAccount.fromAddress(owner);439    const spender = await helper.eth.createAccountWithBalance(donor);440441    const spenderCrossEth = helper.ethCrossAccount.fromAddress(spender);442    const ownerSubCross = helper.ethCrossAccount.fromKeyringPair(ownerSub);443444    const collection = await helper.rft.mintCollection(alice);445    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});446447448    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);449    const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);450451    {452      await contract.methods.approveCross(spenderCrossEth, 100).send({from: owner});453454      await expect(contract.methods.burnFromCross(ownerCross, 50).send({from: spender})).to.be.fulfilled;455      await expect(contract.methods.burnFromCross(ownerCross, 100).send({from: spender})).to.be.rejected;456      expect(await contract.methods.balanceOf(owner).call({from: owner})).to.be.equal('150');457    }458    {459      const {tokenId} = await collection.mintToken(alice, 200n, {Substrate: ownerSub.address});460      await collection.approveToken(ownerSub, tokenId, {Ethereum: spender}, 100n);461      const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);462      const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);463464      await expect(contract.methods.burnFromCross(ownerSubCross, 50).send({from: spender})).to.be.fulfilled;465      await expect(contract.methods.burnFromCross(ownerSubCross, 100).send({from: spender})).to.be.rejected;466      expect(await collection.getTokenBalance(tokenId, {Substrate: ownerSub.address})).to.be.equal(150n);467    }468  });469470  itEth('Check balanceOfCross()', async ({helper}) => {471    const collection = await helper.rft.mintCollection(alice, {});472    const owner = await helper.ethCrossAccount.createAccountWithBalance(donor);473    const other = await helper.ethCrossAccount.createAccountWithBalance(donor);474    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner.eth});475    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);476    const tokenContract = await helper.ethNativeContract.rftToken(tokenAddress, owner.eth);477478    expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('200');479    expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('0');480481    await tokenContract.methods.repartition(100n).send({from: owner.eth});482    expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('100');483    expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('0');484485    await tokenContract.methods.transferCross(other, 50n).send({from: owner.eth});486    expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('50');487    expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('50');488489    await tokenContract.methods.transferCross(other, 50n).send({from: owner.eth});490    expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('0');491    expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('100');492493    await tokenContract.methods.repartition(1000n).send({from: other.eth});494    await tokenContract.methods.transferCross(owner, 500n).send({from: other.eth});495    expect(await tokenContract.methods.balanceOfCross(owner).call({from: owner.eth})).to.be.eq('500');496    expect(await tokenContract.methods.balanceOfCross(other).call({from: owner.eth})).to.be.eq('500');497  });498});499500describe('Refungible: Fees', () => {501  let donor: IKeyringPair;502  let alice: IKeyringPair;503504  before(async function() {505    await usingEthPlaygrounds(async (helper, privateKey) => {506      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);507508      donor = await privateKey({url: import.meta.url});509      [alice] = await helper.arrange.createAccounts([50n], donor);510    });511  });512513  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {514    const owner = await helper.eth.createAccountWithBalance(donor);515    const spender = helper.eth.createAccount();516    const collection = await helper.rft.mintCollection(alice);517    const {tokenId} = await collection.mintToken(alice, 100n, {Ethereum: owner});518519    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);520    const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);521522    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));523    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));524  });525526  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {527    const owner = await helper.eth.createAccountWithBalance(donor);528    const spender = await helper.eth.createAccountWithBalance(donor);529    const collection = await helper.rft.mintCollection(alice);530    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});531532    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);533    const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);534535    await contract.methods.approve(spender, 100).send({from: owner});536537    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));538    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));539  });540541  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {542    const owner = await helper.eth.createAccountWithBalance(donor);543    const receiver = helper.eth.createAccount();544    const collection = await helper.rft.mintCollection(alice);545    const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: owner});546547    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, tokenId);548    const contract = await helper.ethNativeContract.rftToken(tokenAddress, owner);549550    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));551    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));552  });553});554555describe('Refungible: Substrate calls', () => {556  let donor: IKeyringPair;557  let alice: IKeyringPair;558559  before(async function() {560    await usingEthPlaygrounds(async (helper, privateKey) => {561      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);562563      donor = await privateKey({url: import.meta.url});564      [alice] = await helper.arrange.createAccounts([50n], donor);565    });566  });567568  itEth('Events emitted for approve()', async ({helper}) => {569    const receiver = helper.eth.createAccount();570    const collection = await helper.rft.mintCollection(alice);571    const token = await collection.mintToken(alice, 200n);572573    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);574    const contract = await helper.ethNativeContract.rftToken(tokenAddress);575576    const events: any = [];577    contract.events.allEvents((_: any, event: any) => {578      events.push(event);579    });580581    expect(await token.approve(alice, {Ethereum: receiver}, 100n)).to.be.true;582    if(events.length == 0) await helper.wait.newBlocks(1);583    const event = events[0];584585    expect(event.event).to.be.equal('Approval');586    expect(event.address).to.be.equal(tokenAddress);587    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));588    expect(event.returnValues.spender).to.be.equal(receiver);589    expect(event.returnValues.value).to.be.equal('100');590  });591592  itEth('Events emitted for transferFrom()', async ({helper}) => {593    const [bob] = await helper.arrange.createAccounts([10n], donor);594    const receiver = helper.eth.createAccount();595    const collection = await helper.rft.mintCollection(alice);596    const token = await collection.mintToken(alice, 200n);597    await token.approve(alice, {Substrate: bob.address}, 100n);598599    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);600    const contract = await helper.ethNativeContract.rftToken(tokenAddress);601602    const events: any = [];603    contract.events.allEvents((_: any, event: any) => {604      events.push(event);605    });606607    expect(await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver},  51n)).to.be.true;608    if(events.length == 0) await helper.wait.newBlocks(1);609610    let event = events[0];611    expect(event.event).to.be.equal('Transfer');612    expect(event.address).to.be.equal(tokenAddress);613    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));614    expect(event.returnValues.to).to.be.equal(receiver);615    expect(event.returnValues.value).to.be.equal('51');616617    event = events[1];618    expect(event.event).to.be.equal('Approval');619    expect(event.address).to.be.equal(tokenAddress);620    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));621    expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));622    expect(event.returnValues.value).to.be.equal('49');623  });624625  itEth('Events emitted for transfer()', async ({helper}) => {626    const receiver = helper.eth.createAccount();627    const collection = await helper.rft.mintCollection(alice);628    const token = await collection.mintToken(alice, 200n);629630    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);631    const contract = await helper.ethNativeContract.rftToken(tokenAddress);632633    const events: any = [];634    contract.events.allEvents((_: any, event: any) => {635      events.push(event);636    });637638    expect(await token.transfer(alice, {Ethereum: receiver},  51n)).to.be.true;639    if(events.length == 0) await helper.wait.newBlocks(1);640    const event = events[0];641642    expect(event.event).to.be.equal('Transfer');643    expect(event.address).to.be.equal(tokenAddress);644    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));645    expect(event.returnValues.to).to.be.equal(receiver);646    expect(event.returnValues.value).to.be.equal('51');647  });648});649650describe('ERC 1633 implementation', () => {651  let donor: IKeyringPair;652653  before(async function() {654    await usingEthPlaygrounds(async (helper, privateKey) => {655      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);656657      donor = await privateKey({url: import.meta.url});658    });659  });660661  itEth('Default parent token address and id', async ({helper}) => {662    const owner = await helper.eth.createAccountWithBalance(donor);663664    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Sands', '', 'GRAIN');665    const collectionContract = await helper.ethNativeContract.collection(collectionAddress, 'rft', owner);666667    const result = await collectionContract.methods.mint(owner).send();668    const tokenId = result.events.Transfer.returnValues.tokenId;669670    const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);671    const tokenContract = await helper.ethNativeContract.rftToken(tokenAddress, owner);672673    expect(await tokenContract.methods.parentToken().call()).to.be.equal(collectionAddress);674    expect(await tokenContract.methods.parentTokenId().call()).to.be.equal(tokenId);675  });676});