git.delta.rocks / unique-network / refs/commits / 465cfefa64e5

difftreelog

source

tests/src/eth/reFungible.test.ts23.3 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 {expect, itEth, usingEthPlaygrounds} from './util';19import {IKeyringPair} from '@polkadot/types/types';2021describe('Refungible: Information getting', () => {22  let donor: IKeyringPair;2324  before(async function() {25    await usingEthPlaygrounds(async (helper, privateKey) => {26      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);2728      donor = await privateKey({filename: __filename});29    });30  });3132  itEth('totalSupply', async ({helper}) => {33    const caller = await helper.eth.createAccountWithBalance(donor);34    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'TotalSupply', '6', '6');35    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);3637    await contract.methods.mint(caller).send();3839    const totalSupply = await contract.methods.totalSupply().call();40    expect(totalSupply).to.equal('1');41  });4243  itEth('balanceOf', async ({helper}) => {44    const caller = await helper.eth.createAccountWithBalance(donor);45    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'BalanceOf', '6', '6');46    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);4748    await contract.methods.mint(caller).send();49    await contract.methods.mint(caller).send();50    await contract.methods.mint(caller).send();5152    const balance = await contract.methods.balanceOf(caller).call();53    expect(balance).to.equal('3');54  });5556  itEth('ownerOf', async ({helper}) => {57    const caller = await helper.eth.createAccountWithBalance(donor);58    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf', '6', '6');59    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);6061    const result = await contract.methods.mint(caller).send();62    const tokenId = result.events.Transfer.returnValues.tokenId;6364    const owner = await contract.methods.ownerOf(tokenId).call();65    expect(owner).to.equal(caller);66  });6768  itEth('ownerOf after burn', async ({helper}) => {69    const caller = await helper.eth.createAccountWithBalance(donor);70    const receiver = helper.eth.createAccount();71    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf-AfterBurn', '6', '6');72    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);7374    const result = await contract.methods.mint(caller).send();75    const tokenId = result.events.Transfer.returnValues.tokenId;76    const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);7778    await tokenContract.methods.repartition(2).send();79    await tokenContract.methods.transfer(receiver, 1).send();8081    await tokenContract.methods.burnFrom(caller, 1).send();8283    const owner = await contract.methods.ownerOf(tokenId).call();84    expect(owner).to.equal(receiver);85  });8687  itEth('ownerOf for partial ownership', async ({helper}) => {88    const caller = await helper.eth.createAccountWithBalance(donor);89    const receiver = helper.eth.createAccount();90    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Partial-OwnerOf', '6', '6');91    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);9293    const result = await contract.methods.mint(caller).send();94    const tokenId = result.events.Transfer.returnValues.tokenId;95    const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);9697    await tokenContract.methods.repartition(2).send();98    await tokenContract.methods.transfer(receiver, 1).send();99100    const owner = await contract.methods.ownerOf(tokenId).call();101    expect(owner).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');102  });103});104105describe('Refungible: Plain calls', () => {106  let donor: IKeyringPair;107  let minter: IKeyringPair;108  let bob: IKeyringPair;109  let charlie: IKeyringPair;110111  before(async function() {112    await usingEthPlaygrounds(async (helper, privateKey) => {113      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);114115      donor = await privateKey({filename: __filename});116      [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);117    });118  });119120  itEth('Can perform mint() & crossOwnerOf()', async ({helper}) => {121    const owner = await helper.eth.createAccountWithBalance(donor);122    const receiver = helper.eth.createAccount();123    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'Minty', '6', '6', '');124    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);125126    const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();127128    const event = result.events.Transfer;129    expect(event.address).to.equal(collectionAddress);130    expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');131    expect(event.returnValues.to).to.equal(receiver);132    const tokenId = event.returnValues.tokenId;133    expect(tokenId).to.be.equal('1');134135    expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']);136    expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');137  });138139  itEth.skip('Can perform mintBulk()', async ({helper}) => {140    const owner = await helper.eth.createAccountWithBalance(donor);141    const receiver = helper.eth.createAccount();142    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'MintBulky', '6', '6', '');143    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);144145    {146      const nextTokenId = await contract.methods.nextTokenId().call();147      expect(nextTokenId).to.be.equal('1');148      const result = await contract.methods.mintBulkWithTokenURI(149        receiver,150        [151          [nextTokenId, 'Test URI 0'],152          [+nextTokenId + 1, 'Test URI 1'],153          [+nextTokenId + 2, 'Test URI 2'],154        ],155      ).send();156157      const events = result.events.Transfer;158      for (let i = 0; i < 2; i++) {159        const event = events[i];160        expect(event.address).to.equal(collectionAddress);161        expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');162        expect(event.returnValues.to).to.equal(receiver);163        expect(event.returnValues.tokenId).to.equal(String(+nextTokenId + i));164      }165166      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');167      expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');168      expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');169    }170  });171172  itEth('Can perform burn()', async ({helper}) => {173    const caller = await helper.eth.createAccountWithBalance(donor);174    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Burny', '6', '6');175    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);176177    const result = await contract.methods.mint(caller).send();178    const tokenId = result.events.Transfer.returnValues.tokenId;179    {180      const result = await contract.methods.burn(tokenId).send();181      const event = result.events.Transfer;182      expect(event.address).to.equal(collectionAddress);183      expect(event.returnValues.from).to.equal(caller);184      expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');185      expect(event.returnValues.tokenId).to.equal(tokenId.toString());186    }187  });188189  itEth('Can perform transferFrom()', async ({helper}) => {190    const caller = await helper.eth.createAccountWithBalance(donor);191    const receiver = helper.eth.createAccount();192    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'TransferFromy', '6', '6');193    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);194195    const result = await contract.methods.mint(caller).send();196    const tokenId = result.events.Transfer.returnValues.tokenId;197198    const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);199200    const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, caller);201    await tokenContract.methods.repartition(15).send();202203    {204      const tokenEvents: any = [];205      tokenContract.events.allEvents((_: any, event: any) => {206        tokenEvents.push(event);207      });208      const result = await contract.methods.transferFrom(caller, receiver, tokenId).send();209      if (tokenEvents.length == 0) await helper.wait.newBlocks(1);210211      let event = result.events.Transfer;212      expect(event.address).to.equal(collectionAddress);213      expect(event.returnValues.from).to.equal(caller);214      expect(event.returnValues.to).to.equal(receiver);215      expect(event.returnValues.tokenId).to.equal(tokenId.toString());216217      event = tokenEvents[0];218      expect(event.address).to.equal(tokenAddress);219      expect(event.returnValues.from).to.equal(caller);220      expect(event.returnValues.to).to.equal(receiver);221      expect(event.returnValues.value).to.equal('15');222    }223224    {225      const balance = await contract.methods.balanceOf(receiver).call();226      expect(+balance).to.equal(1);227    }228229    {230      const balance = await contract.methods.balanceOf(caller).call();231      expect(+balance).to.equal(0);232    }233  });234235  // Soft-deprecated236  itEth('Can perform burnFrom()', async ({helper}) => {237    const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});238239    const owner = await helper.eth.createAccountWithBalance(donor, 100n);240    const spender = await helper.eth.createAccountWithBalance(donor, 100n);241242    const token = await collection.mintToken(minter, 100n, {Ethereum: owner});243244    const address = helper.ethAddress.fromCollectionId(collection.collectionId);245    const contract = helper.ethNativeContract.collection(address, 'rft', spender, true);246247    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);248    const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, owner);249    await tokenContract.methods.repartition(15).send();250    await tokenContract.methods.approve(spender, 15).send();251252    {253      const result = await contract.methods.burnFrom(owner, token.tokenId).send();254      const event = result.events.Transfer;255      expect(event).to.be.like({256        address: helper.ethAddress.fromCollectionId(collection.collectionId),257        event: 'Transfer',258        returnValues: {259          from: owner,260          to: '0x0000000000000000000000000000000000000000',261          tokenId: token.tokenId.toString(),262        },263      });264    }265266    expect(await collection.getTokenBalance(token.tokenId, {Ethereum: owner})).to.be.eq(0n);267  });268269  itEth('Can perform burnFromCross()', async ({helper}) => {270    const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});271    272    const owner = bob;273    const spender = await helper.eth.createAccountWithBalance(donor, 100n);274275    const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});276277    const address = helper.ethAddress.fromCollectionId(collection.collectionId);278    const contract = helper.ethNativeContract.collection(address, 'rft');279280    await token.repartition(owner, 15n);281    await token.approve(owner, {Ethereum: spender}, 15n);282283    {284      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);285      const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender});286      const event = result.events.Transfer;287      expect(event).to.be.like({288        address: helper.ethAddress.fromCollectionId(collection.collectionId),289        event: 'Transfer',290        returnValues: {291          from: helper.address.substrateToEth(owner.address),292          to: '0x0000000000000000000000000000000000000000',293          tokenId: token.tokenId.toString(),294        },295      });296    }297298    expect(await collection.getTokenBalance(token.tokenId, {Substrate: owner.address})).to.be.eq(0n);299  });300301  itEth('Can perform transferFromCross()', async ({helper}) => {302    const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});303304    const owner = bob;305    const spender = await helper.eth.createAccountWithBalance(donor, 100n);306    const receiver = charlie;307308    const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});309310    const address = helper.ethAddress.fromCollectionId(collection.collectionId);311    const contract = helper.ethNativeContract.collection(address, 'rft');312313    await token.repartition(owner, 15n);314    await token.approve(owner, {Ethereum: spender}, 15n);315316    {317      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);318      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);319      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});320      const event = result.events.Transfer;321      expect(event).to.be.like({322        address: helper.ethAddress.fromCollectionId(collection.collectionId),323        event: 'Transfer',324        returnValues: {325          from: helper.address.substrateToEth(owner.address),326          to: helper.address.substrateToEth(receiver.address),327          tokenId: token.tokenId.toString(),328        },329      });330    }331332    expect(await token.getTop10Owners()).to.be.like([{Substrate: receiver.address}]);333  });334335  itEth('Can perform transfer()', async ({helper}) => {336    const caller = await helper.eth.createAccountWithBalance(donor);337    const receiver = helper.eth.createAccount();338    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry', '6', '6');339    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);340341    const result = await contract.methods.mint(caller).send();342    const tokenId = result.events.Transfer.returnValues.tokenId;343344    {345      const result = await contract.methods.transfer(receiver, tokenId).send();346347      const event = result.events.Transfer;348      expect(event.address).to.equal(collectionAddress);349      expect(event.returnValues.from).to.equal(caller);350      expect(event.returnValues.to).to.equal(receiver);351      expect(event.returnValues.tokenId).to.equal(tokenId.toString());352    }353354    {355      const balance = await contract.methods.balanceOf(caller).call();356      expect(+balance).to.equal(0);357    }358359    {360      const balance = await contract.methods.balanceOf(receiver).call();361      expect(+balance).to.equal(1);362    }363  });364  365  itEth('Can perform transferCross()', async ({helper}) => {366    const caller = await helper.eth.createAccountWithBalance(donor);367    const receiver = await helper.eth.createAccountWithBalance(donor);368    const to = helper.ethCrossAccount.fromAddress(receiver);369    const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter);370    const collection = await helper.rft.mintCollection(minter, {});371    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);372    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);373374    const {tokenId} = await collection.mintToken(minter, 1n, {Ethereum: caller});375376    {377      const result = await contract.methods.transferCross(to, tokenId).send({from: caller});378379      const event = result.events.Transfer;380      expect(event.address).to.equal(collectionAddress);381      expect(event.returnValues.from).to.equal(caller);382      expect(event.returnValues.to).to.equal(receiver);383      expect(event.returnValues.tokenId).to.equal(tokenId.toString());384    }385386    {387      const balance = await contract.methods.balanceOf(caller).call();388      expect(+balance).to.equal(0);389    }390391    {392      const balance = await contract.methods.balanceOf(receiver).call();393      expect(+balance).to.equal(1);394    }395    396    {397      const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver});398      399400      const event = substrateResult.events.Transfer;401      expect(event.address).to.be.equal(collectionAddress);402      expect(event.returnValues.from).to.be.equal(receiver);403      expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));404      expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);405    }406407    {408      const balance = await contract.methods.balanceOf(receiver).call();409      expect(+balance).to.equal(0);410    }411412    {413      const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});414      expect(balance).to.be.contain(tokenId);415    }416  });417418  itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => {419    const caller = await helper.eth.createAccountWithBalance(donor);420    const receiver = helper.eth.createAccount();421    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Partial-to-Full', '6', '6');422    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);423424    const result = await contract.methods.mint(caller).send();425    const tokenId = result.events.Transfer.returnValues.tokenId;426427    const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);428429    await tokenContract.methods.repartition(2).send();430    await tokenContract.methods.transfer(receiver, 1).send();431432    const events: any = [];433    contract.events.allEvents((_: any, event: any) => {434      events.push(event);435    });436437    await tokenContract.methods.transfer(receiver, 1).send();438    if (events.length == 0) await helper.wait.newBlocks(1);439    const event = events[0];440441    expect(event.address).to.equal(collectionAddress);442    expect(event.returnValues.from).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');443    expect(event.returnValues.to).to.equal(receiver);444    expect(event.returnValues.tokenId).to.equal(tokenId.toString());445  });446447  itEth('transfer event on transfer from full ownership to partial ownership', async ({helper}) => {448    const caller = await helper.eth.createAccountWithBalance(donor);449    const receiver = helper.eth.createAccount();450    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Full-to-Partial', '6', '6');451    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);452453    const result = await contract.methods.mint(caller).send();454    const tokenId = result.events.Transfer.returnValues.tokenId;455456    const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);457458    await tokenContract.methods.repartition(2).send();459460    const events: any = [];461    contract.events.allEvents((_: any, event: any) => {462      events.push(event);463    });464465    await tokenContract.methods.transfer(receiver, 1).send();466    if (events.length == 0) await helper.wait.newBlocks(1);467    const event = events[0];468469    expect(event.address).to.equal(collectionAddress);470    expect(event.returnValues.from).to.equal(caller);471    expect(event.returnValues.to).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');472    expect(event.returnValues.tokenId).to.equal(tokenId.toString());473  });474});475476describe('RFT: Fees', () => {477  let donor: IKeyringPair;478479  before(async function() {480    await usingEthPlaygrounds(async (helper, privateKey) => {481      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);482483      donor = await privateKey({filename: __filename});484    });485  });486487  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {488    const caller = await helper.eth.createAccountWithBalance(donor);489    const receiver = helper.eth.createAccount();490    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer-From', '6', '6');491    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);492493    const result = await contract.methods.mint(caller).send();494    const tokenId = result.events.Transfer.returnValues.tokenId;495496    const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transferFrom(caller, receiver, tokenId).send());497    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));498    expect(cost > 0n);499  });500501  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {502    const caller = await helper.eth.createAccountWithBalance(donor);503    const receiver = helper.eth.createAccount();504    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer', '6', '6');505    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);506507    const result = await contract.methods.mint(caller).send();508    const tokenId = result.events.Transfer.returnValues.tokenId;509510    const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transfer(receiver, tokenId).send());511    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));512    expect(cost > 0n);513  });514});515516describe('Common metadata', () => {517  let donor: IKeyringPair;518  let alice: IKeyringPair;519520  before(async function() {521    await usingEthPlaygrounds(async (helper, privateKey) => {522      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);523524      donor = await privateKey({filename: __filename});525      [alice] = await helper.arrange.createAccounts([20n], donor);526    });527  });528529  itEth('Returns collection name', async ({helper}) => {530    const caller = helper.eth.createAccount();531    const tokenPropertyPermissions = [{532      key: 'URI',533      permission: {534        mutable: true,535        collectionAdmin: true,536        tokenOwner: false,537      },538    }];539    const collection = await helper.rft.mintCollection(540      alice,541      {542        name: 'Leviathan',543        tokenPrefix: '11',544        properties: [{key: 'ERC721Metadata', value: '1'}],545        tokenPropertyPermissions,546      },547    );548549    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'rft', caller);550    const name = await contract.methods.name().call();551    expect(name).to.equal('Leviathan');552  });553554  itEth('Returns symbol name', async ({helper}) => {555    const caller = await helper.eth.createAccountWithBalance(donor);556    const tokenPropertyPermissions = [{557      key: 'URI',558      permission: {559        mutable: true,560        collectionAdmin: true,561        tokenOwner: false,562      },563    }];564    const {collectionId} = await helper.rft.mintCollection(565      alice,566      {567        name: 'Leviathan',568        tokenPrefix: '12',569        properties: [{key: 'ERC721Metadata', value: '1'}],570        tokenPropertyPermissions,571      },572    );573574    const contract = helper.ethNativeContract.collectionById(collectionId, 'rft', caller);575    const symbol = await contract.methods.symbol().call();576    expect(symbol).to.equal('12');577  });578});