git.delta.rocks / unique-network / refs/commits / 6a580826c34a

difftreelog

source

js-packages/tests/eth/tokens/minting.test.ts7.9 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 type {IKeyringPair} from '@polkadot/types/types';18import {Pallets} from '../../util/index.js';19import {expect, itEth, usingEthPlaygrounds} from '../util/index.js';20import {CreateCollectionData} from '../util/playgrounds/types.js';212223describe('Minting tokens', () => {24  let donor: IKeyringPair;25  let alice: IKeyringPair;2627  before(async function() {28    await usingEthPlaygrounds(async (helper, privateKey) => {29      donor = await privateKey({url: import.meta.url});30      [alice] = await helper.arrange.createAccounts([30n, 20n], donor);31    });32  });3334  [35    {mode: 'nft' as const, requiredPallets: []},36    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},37    {mode: 'ft' as const, requiredPallets: []},38  ].map(testCase => {39    itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mint() for Substrate collection`, testCase.requiredPallets, async ({helper}) => {40      const owner = await helper.eth.createAccountWithBalance(donor);41      const receiver = helper.eth.createAccount();42      const mintingParams = testCase.mode === 'ft' ? [receiver, 100] : [receiver];4344      const collection = await helper[testCase.mode].mintCollection(alice);45      await collection.addAdmin(alice, {Ethereum: owner});4647      const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);48      const contract = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);4950      const result = await contract.methods.mint(...mintingParams).send({from: owner});5152      // Check events:53      const event = result.events.Transfer;54      expect(event.address).to.equal(collectionAddress);55      expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');56      expect(event.returnValues.to).to.equal(receiver);57      if(testCase.mode === 'ft')58        expect(event.returnValues.value).to.equal('100');5960      // Check token exist:61      if(testCase.mode === 'ft') {62        expect(await helper.ft.getBalance(collection.collectionId, {Ethereum: receiver})).to.eq(100n);63      } else {64        const tokenId = event.returnValues.tokenId;65        expect(tokenId).to.be.equal('1');66        expect(await helper.collection.getLastTokenId(collection.collectionId)).to.eq(1);67        expect(await contract.methods.ownerOfCross(tokenId).call()).to.be.like([receiver, '0']);68      }69    });70  });7172  [73    {mode: 'nft' as const, requiredPallets: []},74    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},75    {mode: 'ft' as const, requiredPallets: []},76  ].map(testCase => {77    itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mint() for Ethereum collection`, testCase.requiredPallets, async ({helper}) => {78      const owner = await helper.eth.createAccountWithBalance(donor);79      const receiver = helper.eth.createAccount();80      const mintingParams = testCase.mode === 'ft' ? [receiver, 100] : [receiver];8182      const {collection, collectionAddress, collectionId} = await helper.eth.createCollection(owner, new CreateCollectionData('Name', 'Desc', 'Prefix', testCase.mode)).send();8384      const result = await collection.methods.mint(...mintingParams).send({from: owner});8586      // Check events:87      const event = result.events.Transfer;88      expect(event.address).to.equal(collectionAddress);89      expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');90      expect(event.returnValues.to).to.equal(receiver);91      if(testCase.mode === 'ft')92        expect(event.returnValues.value).to.equal('100');9394      // Check token exist:95      if(testCase.mode === 'ft') {96        expect(await helper.ft.getBalance(collectionId, {Ethereum: receiver})).to.eq(100n);97      } else {98        const tokenId = event.returnValues.tokenId;99        expect(tokenId).to.be.equal('1');100        expect(await helper.collection.getLastTokenId(collectionId)).to.eq(1);101        expect(await collection.methods.ownerOfCross(tokenId).call()).to.be.like([receiver, '0']);102      }103    });104  });105106  [107    {mode: 'nft' as const, requiredPallets: []},108    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},109    {mode: 'ft' as const, requiredPallets: []},110  ].map(testCase => {111    itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mint() for Ethereum collection`, testCase.requiredPallets, async ({helper}) => {112      const owner = await helper.eth.createAccountWithBalance(donor);113      const receiver = helper.eth.createAccount();114      const mintingParams = testCase.mode === 'ft' ? [receiver, 100] : [receiver];115116      const {collection, collectionAddress, collectionId} = await helper.eth.createCollection(owner, new CreateCollectionData('Name', 'Desc', 'Prefix', testCase.mode)).send();117118      const result = await collection.methods.mint(...mintingParams).send({from: owner});119120      // Check events:121      const event = result.events.Transfer;122      expect(event.address).to.equal(collectionAddress);123      expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');124      expect(event.returnValues.to).to.equal(receiver);125      if(testCase.mode === 'ft')126        expect(event.returnValues.value).to.equal('100');127128      // Check token exist:129      if(testCase.mode === 'ft') {130        expect(await helper.ft.getBalance(collectionId, {Ethereum: receiver})).to.eq(100n);131      } else {132        const tokenId = event.returnValues.tokenId;133        expect(tokenId).to.be.equal('1');134        expect(await helper.collection.getLastTokenId(collectionId)).to.eq(1);135        expect(await collection.methods.ownerOfCross(tokenId).call()).to.be.like([receiver, '0']);136      }137    });138  });139140  [141    {mode: 'nft' as const, requiredPallets: []},142    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},143  ].map(testCase => {144    itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mintWithTokenURI() for Ethereum collection`, testCase.requiredPallets, async ({helper}) => {145      const owner = await helper.eth.createAccountWithBalance(donor);146      const receiver = helper.eth.createAccount();147148      const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', '');149      const contract = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);150151      const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();152      const tokenId = result.events.Transfer.returnValues.tokenId;153      expect(tokenId).to.be.equal('1');154155      const event = result.events.Transfer;156      expect(event.address).to.be.equal(collectionAddress);157      expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');158      expect(event.returnValues.to).to.be.equal(receiver);159160      expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');161      expect(await contract.methods.ownerOfCross(tokenId).call()).to.be.like([receiver, '0']);162      // TODO: this wont work right now, need release 919000 first163      // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();164      // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();165      // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);166    });167  });168});