git.delta.rocks / unique-network / refs/commits / 786ff7ed1061

difftreelog

source

tests/src/eth/reFungible.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';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()', 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.tokenURI(tokenId).call()).to.be.equal('Test URI');136  });137138  itEth.skip('Can perform mintBulk()', async ({helper}) => {139    const owner = await helper.eth.createAccountWithBalance(donor);140    const receiver = helper.eth.createAccount();141    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'MintBulky', '6', '6', '');142    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);143144    {145      const nextTokenId = await contract.methods.nextTokenId().call();146      expect(nextTokenId).to.be.equal('1');147      const result = await contract.methods.mintBulkWithTokenURI(148        receiver,149        [150          [nextTokenId, 'Test URI 0'],151          [+nextTokenId + 1, 'Test URI 1'],152          [+nextTokenId + 2, 'Test URI 2'],153        ],154      ).send();155156      const events = result.events.Transfer;157      for (let i = 0; i < 2; i++) {158        const event = events[i];159        expect(event.address).to.equal(collectionAddress);160        expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');161        expect(event.returnValues.to).to.equal(receiver);162        expect(event.returnValues.tokenId).to.equal(String(+nextTokenId + i));163      }164165      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');166      expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');167      expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');168    }169  });170171  itEth('Can perform burn()', async ({helper}) => {172    const caller = await helper.eth.createAccountWithBalance(donor);173    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Burny', '6', '6');174    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);175176    const result = await contract.methods.mint(caller).send();177    const tokenId = result.events.Transfer.returnValues.tokenId;178    {179      const result = await contract.methods.burn(tokenId).send();180      const event = result.events.Transfer;181      expect(event.address).to.equal(collectionAddress);182      expect(event.returnValues.from).to.equal(caller);183      expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');184      expect(event.returnValues.tokenId).to.equal(tokenId.toString());185    }186  });187188  itEth('Can perform transferFrom()', async ({helper}) => {189    const caller = await helper.eth.createAccountWithBalance(donor);190    const receiver = helper.eth.createAccount();191    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'TransferFromy', '6', '6');192    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);193194    const result = await contract.methods.mint(caller).send();195    const tokenId = result.events.Transfer.returnValues.tokenId;196197    const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);198199    const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, caller);200    await tokenContract.methods.repartition(15).send();201202    {203      const tokenEvents: any = [];204      tokenContract.events.allEvents((_: any, event: any) => {205        tokenEvents.push(event);206      });207      const result = await contract.methods.transferFrom(caller, receiver, tokenId).send();208      if (tokenEvents.length == 0) await helper.wait.newBlocks(1);209210      let event = result.events.Transfer;211      expect(event.address).to.equal(collectionAddress);212      expect(event.returnValues.from).to.equal(caller);213      expect(event.returnValues.to).to.equal(receiver);214      expect(event.returnValues.tokenId).to.equal(tokenId.toString());215216      event = tokenEvents[0];217      expect(event.address).to.equal(tokenAddress);218      expect(event.returnValues.from).to.equal(caller);219      expect(event.returnValues.to).to.equal(receiver);220      expect(event.returnValues.value).to.equal('15');221    }222223    {224      const balance = await contract.methods.balanceOf(receiver).call();225      expect(+balance).to.equal(1);226    }227228    {229      const balance = await contract.methods.balanceOf(caller).call();230      expect(+balance).to.equal(0);231    }232  });233234  itEth('Can perform burnFrom()', async ({helper}) => {235    const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});236237    const owner = await helper.eth.createAccountWithBalance(donor, 100n);238    const spender = await helper.eth.createAccountWithBalance(donor, 100n);239240    const token = await collection.mintToken(minter, 100n, {Ethereum: owner});241242    const address = helper.ethAddress.fromCollectionId(collection.collectionId);243    const contract = helper.ethNativeContract.collection(address, 'rft');244245    const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);246    const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, owner);247    await tokenContract.methods.repartition(15).send();248    await tokenContract.methods.approve(spender, 15).send();249250    {251      const result = await contract.methods.burnFrom(owner, token.tokenId).send({from: spender});252      const event = result.events.Transfer;253      expect(event).to.be.like({254        address: helper.ethAddress.fromCollectionId(collection.collectionId),255        event: 'Transfer',256        returnValues: {257          from: owner,258          to: '0x0000000000000000000000000000000000000000',259          tokenId: token.tokenId.toString(),260        },261      });262    }263264    expect(await collection.getTokenBalance(token.tokenId, {Ethereum: owner})).to.be.eq(0n);265  });266267  itEth('Can perform burnFromCross()', async ({helper}) => {268    const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});269    270    const owner = bob;271    const spender = await helper.eth.createAccountWithBalance(donor, 100n);272273    const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});274275    const address = helper.ethAddress.fromCollectionId(collection.collectionId);276    const contract = helper.ethNativeContract.collection(address, 'rft');277278    await token.repartition(owner, 15n);279    await token.approve(owner, {Ethereum: spender}, 15n);280281    {282      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);283      const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender});284      const event = result.events.Transfer;285      expect(event).to.be.like({286        address: helper.ethAddress.fromCollectionId(collection.collectionId),287        event: 'Transfer',288        returnValues: {289          from: helper.address.substrateToEth(owner.address),290          to: '0x0000000000000000000000000000000000000000',291          tokenId: token.tokenId.toString(),292        },293      });294    }295296    expect(await collection.getTokenBalance(token.tokenId, {Substrate: owner.address})).to.be.eq(0n);297  });298299  itEth('Can perform transferFromCross()', async ({helper}) => {300    const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});301302    const owner = bob;303    const spender = await helper.eth.createAccountWithBalance(donor, 100n);304    const receiver = charlie;305306    const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});307308    const address = helper.ethAddress.fromCollectionId(collection.collectionId);309    const contract = helper.ethNativeContract.collection(address, 'rft');310311    await token.repartition(owner, 15n);312    await token.approve(owner, {Ethereum: spender}, 15n);313314    {315      const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);316      const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);317      const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});318      const event = result.events.Transfer;319      expect(event).to.be.like({320        address: helper.ethAddress.fromCollectionId(collection.collectionId),321        event: 'Transfer',322        returnValues: {323          from: helper.address.substrateToEth(owner.address),324          to: helper.address.substrateToEth(receiver.address),325          tokenId: token.tokenId.toString(),326        },327      });328    }329330    expect(await token.getTop10Owners()).to.be.like([{Substrate: receiver.address}]);331  });332333  itEth('Can perform transfer()', async ({helper}) => {334    const caller = await helper.eth.createAccountWithBalance(donor);335    const receiver = helper.eth.createAccount();336    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry', '6', '6');337    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);338339    const result = await contract.methods.mint(caller).send();340    const tokenId = result.events.Transfer.returnValues.tokenId;341342    {343      const result = await contract.methods.transfer(receiver, tokenId).send();344345      const event = result.events.Transfer;346      expect(event.address).to.equal(collectionAddress);347      expect(event.returnValues.from).to.equal(caller);348      expect(event.returnValues.to).to.equal(receiver);349      expect(event.returnValues.tokenId).to.equal(tokenId.toString());350    }351352    {353      const balance = await contract.methods.balanceOf(caller).call();354      expect(+balance).to.equal(0);355    }356357    {358      const balance = await contract.methods.balanceOf(receiver).call();359      expect(+balance).to.equal(1);360    }361  });362363  itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => {364    const caller = await helper.eth.createAccountWithBalance(donor);365    const receiver = helper.eth.createAccount();366    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Partial-to-Full', '6', '6');367    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);368369    const result = await contract.methods.mint(caller).send();370    const tokenId = result.events.Transfer.returnValues.tokenId;371372    const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);373374    await tokenContract.methods.repartition(2).send();375    await tokenContract.methods.transfer(receiver, 1).send();376377    const events: any = [];378    contract.events.allEvents((_: any, event: any) => {379      events.push(event);380    });381382    await tokenContract.methods.transfer(receiver, 1).send();383    if (events.length == 0) await helper.wait.newBlocks(1);384    const event = events[0];385386    expect(event.address).to.equal(collectionAddress);387    expect(event.returnValues.from).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');388    expect(event.returnValues.to).to.equal(receiver);389    expect(event.returnValues.tokenId).to.equal(tokenId.toString());390  });391392  itEth('transfer event on transfer from full ownership to partial ownership', async ({helper}) => {393    const caller = await helper.eth.createAccountWithBalance(donor);394    const receiver = helper.eth.createAccount();395    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Full-to-Partial', '6', '6');396    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);397398    const result = await contract.methods.mint(caller).send();399    const tokenId = result.events.Transfer.returnValues.tokenId;400401    const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);402403    await tokenContract.methods.repartition(2).send();404405    const events: any = [];406    contract.events.allEvents((_: any, event: any) => {407      events.push(event);408    });409410    await tokenContract.methods.transfer(receiver, 1).send();411    if (events.length == 0) await helper.wait.newBlocks(1);412    const event = events[0];413414    expect(event.address).to.equal(collectionAddress);415    expect(event.returnValues.from).to.equal(caller);416    expect(event.returnValues.to).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');417    expect(event.returnValues.tokenId).to.equal(tokenId.toString());418  });419});420421describe('RFT: Fees', () => {422  let donor: IKeyringPair;423424  before(async function() {425    await usingEthPlaygrounds(async (helper, privateKey) => {426      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);427428      donor = await privateKey({filename: __filename});429    });430  });431432  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {433    const caller = await helper.eth.createAccountWithBalance(donor);434    const receiver = helper.eth.createAccount();435    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer-From', '6', '6');436    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);437438    const result = await contract.methods.mint(caller).send();439    const tokenId = result.events.Transfer.returnValues.tokenId;440441    const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transferFrom(caller, receiver, tokenId).send());442    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));443    expect(cost > 0n);444  });445446  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {447    const caller = await helper.eth.createAccountWithBalance(donor);448    const receiver = helper.eth.createAccount();449    const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer', '6', '6');450    const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);451452    const result = await contract.methods.mint(caller).send();453    const tokenId = result.events.Transfer.returnValues.tokenId;454455    const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transfer(receiver, tokenId).send());456    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));457    expect(cost > 0n);458  });459});460461describe('Common metadata', () => {462  let donor: IKeyringPair;463  let alice: IKeyringPair;464465  before(async function() {466    await usingEthPlaygrounds(async (helper, privateKey) => {467      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);468469      donor = await privateKey({filename: __filename});470      [alice] = await helper.arrange.createAccounts([20n], donor);471    });472  });473474  itEth('Returns collection name', async ({helper}) => {475    const caller = helper.eth.createAccount();476    const tokenPropertyPermissions = [{477      key: 'URI',478      permission: {479        mutable: true,480        collectionAdmin: true,481        tokenOwner: false,482      },483    }];484    const collection = await helper.rft.mintCollection(485      alice,486      {487        name: 'Leviathan',488        tokenPrefix: '11',489        properties: [{key: 'ERC721Metadata', value: '1'}],490        tokenPropertyPermissions,491      },492    );493494    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'rft', caller);495    const name = await contract.methods.name().call();496    expect(name).to.equal('Leviathan');497  });498499  itEth('Returns symbol name', async ({helper}) => {500    const caller = await helper.eth.createAccountWithBalance(donor);501    const tokenPropertyPermissions = [{502      key: 'URI',503      permission: {504        mutable: true,505        collectionAdmin: true,506        tokenOwner: false,507      },508    }];509    const {collectionId} = await helper.rft.mintCollection(510      alice,511      {512        name: 'Leviathan',513        tokenPrefix: '12',514        properties: [{key: 'ERC721Metadata', value: '1'}],515        tokenPropertyPermissions,516      },517    );518519    const contract = helper.ethNativeContract.collectionById(collectionId, 'rft', caller);520    const symbol = await contract.methods.symbol().call();521    expect(symbol).to.equal('12');522  });523});