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

difftreelog

source

tests/src/eth/reFungible.test.ts19.0 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 {createCollectionExpectSuccess, UNIQUE} from '../util/helpers';18import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, evmCollection, evmCollectionHelpers, GAS_ARGS, getCollectionAddressFromResult, itWeb3, normalizeEvents, recordEthFee, recordEvents, tokenIdToAddress} from './util/helpers';19import reFungibleTokenAbi from './reFungibleTokenAbi.json';20import {expect} from 'chai';2122describe('Refungible: Information getting', () => {23  itWeb3('totalSupply', async ({api, web3, privateKeyWrapper}) => {24    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);25    const helper = evmCollectionHelpers(web3, caller);26    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();27    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);28    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});29    const nextTokenId = await contract.methods.nextTokenId().call();30    await contract.methods.mint(caller, nextTokenId).send();31    const totalSupply = await contract.methods.totalSupply().call();32    expect(totalSupply).to.equal('1');33  });3435  itWeb3('balanceOf', async ({api, web3, privateKeyWrapper}) => {36    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);37    const helper = evmCollectionHelpers(web3, caller);38    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();39    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);40    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});4142    {43      const nextTokenId = await contract.methods.nextTokenId().call();44      await contract.methods.mint(caller, nextTokenId).send();45    }46    {47      const nextTokenId = await contract.methods.nextTokenId().call();48      await contract.methods.mint(caller, nextTokenId).send();49    }50    {51      const nextTokenId = await contract.methods.nextTokenId().call();52      await contract.methods.mint(caller, nextTokenId).send();53    }5455    const balance = await contract.methods.balanceOf(caller).call();5657    expect(balance).to.equal('3');58  });5960  itWeb3('ownerOf', async ({api, web3, privateKeyWrapper}) => {61    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);62    const helper = evmCollectionHelpers(web3, caller);63    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();64    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);65    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});6667    const tokenId = await contract.methods.nextTokenId().call();68    await contract.methods.mint(caller, tokenId).send();6970    const owner = await contract.methods.ownerOf(tokenId).call();7172    expect(owner).to.equal(caller);73  });7475  itWeb3('ownerOf after burn', async ({api, web3, privateKeyWrapper}) => {76    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);77    const receiver = createEthAccount(web3);78    const helper = evmCollectionHelpers(web3, caller);79    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();80    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);81    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});8283    const tokenId = await contract.methods.nextTokenId().call();84    await contract.methods.mint(caller, tokenId).send();8586    const tokenAddress = tokenIdToAddress(collectionId, tokenId);87    const tokenContract = new web3.eth.Contract(reFungibleTokenAbi as any, tokenAddress, {from: caller, ...GAS_ARGS});8889    await tokenContract.methods.repartition(2).send();90    await tokenContract.methods.transfer(receiver, 1).send();9192    await tokenContract.methods.burnFrom(caller, 1).send();9394    const owner = await contract.methods.ownerOf(tokenId).call();9596    expect(owner).to.equal(receiver);97  });9899  itWeb3('ownerOf for partial ownership', async ({api, web3, privateKeyWrapper}) => {100    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);101    const receiver = createEthAccount(web3);102    const helper = evmCollectionHelpers(web3, caller);103    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();104    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);105    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});106107    const tokenId = await contract.methods.nextTokenId().call();108    await contract.methods.mint(caller, tokenId).send();109110    const tokenAddress = tokenIdToAddress(collectionId, tokenId);111    const tokenContract = new web3.eth.Contract(reFungibleTokenAbi as any, tokenAddress, {from: caller, ...GAS_ARGS});112113    await tokenContract.methods.repartition(2).send();114    await tokenContract.methods.transfer(receiver, 1).send();115116    const owner = await contract.methods.ownerOf(tokenId).call();117118    expect(owner).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');119  });120});121122describe('Refungible: Plain calls', () => {123  itWeb3('Can perform mint()', async ({web3, api, privateKeyWrapper}) => {124    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);125    const helper = evmCollectionHelpers(web3, owner);126    let result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();127    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);128    const receiver = createEthAccount(web3);129    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});130    const nextTokenId = await contract.methods.nextTokenId().call();131132    expect(nextTokenId).to.be.equal('1');133    result = await contract.methods.mintWithTokenURI(134      receiver,135      nextTokenId,136      'Test URI',137    ).send();138139    const events = normalizeEvents(result.events);140141    expect(events).to.include.deep.members([142      {143        address: collectionIdAddress,144        event: 'Transfer',145        args: {146          from: '0x0000000000000000000000000000000000000000',147          to: receiver,148          tokenId: nextTokenId,149        },150      },151    ]);152153    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');154  });155156  itWeb3('Can perform mintBulk()', async ({web3, api, privateKeyWrapper}) => {157    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);158    const helper = evmCollectionHelpers(web3, caller);159    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();160    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);161    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});162163    const receiver = createEthAccount(web3);164165    {166      const nextTokenId = await contract.methods.nextTokenId().call();167      expect(nextTokenId).to.be.equal('1');168      const result = await contract.methods.mintBulkWithTokenURI(169        receiver,170        [171          [nextTokenId, 'Test URI 0'],172          [+nextTokenId + 1, 'Test URI 1'],173          [+nextTokenId + 2, 'Test URI 2'],174        ],175      ).send();176      const events = normalizeEvents(result.events);177178      expect(events).to.include.deep.members([179        {180          address: collectionIdAddress,181          event: 'Transfer',182          args: {183            from: '0x0000000000000000000000000000000000000000',184            to: receiver,185            tokenId: nextTokenId,186          },187        },188        {189          address: collectionIdAddress,190          event: 'Transfer',191          args: {192            from: '0x0000000000000000000000000000000000000000',193            to: receiver,194            tokenId: String(+nextTokenId + 1),195          },196        },197        {198          address: collectionIdAddress,199          event: 'Transfer',200          args: {201            from: '0x0000000000000000000000000000000000000000',202            to: receiver,203            tokenId: String(+nextTokenId + 2),204          },205        },206      ]);207208      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');209      expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');210      expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');211    }212  });213214  itWeb3('Can perform burn()', async ({web3, api, privateKeyWrapper}) => {215    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);216    const helper = evmCollectionHelpers(web3, caller);217    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();218    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);219    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});220221    const tokenId = await contract.methods.nextTokenId().call();222    await contract.methods.mint(caller, tokenId).send();223    {224      const result = await contract.methods.burn(tokenId).send();225      const events = normalizeEvents(result.events);226      expect(events).to.include.deep.members([227        {228          address: collectionIdAddress,229          event: 'Transfer',230          args: {231            from: caller,232            to: '0x0000000000000000000000000000000000000000',233            tokenId: tokenId.toString(),234          },235        },236      ]);237    }238  });239240  itWeb3('Can perform transferFrom()', async ({web3, api, privateKeyWrapper}) => {241    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);242    const helper = evmCollectionHelpers(web3, caller);243    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();244    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);245    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});246247    const receiver = createEthAccount(web3);248249    const tokenId = await contract.methods.nextTokenId().call();250    await contract.methods.mint(caller, tokenId).send();251252    const address = tokenIdToAddress(collectionId, tokenId);253    const tokenContract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: caller, ...GAS_ARGS});254    await tokenContract.methods.repartition(15).send();255256    {257      const erc20Events = await recordEvents(tokenContract, async () => {258        const result = await contract.methods.transferFrom(caller, receiver, tokenId).send();259        const events = normalizeEvents(result.events);260        expect(events).to.include.deep.members([261          {262            address: collectionIdAddress,263            event: 'Transfer',264            args: {265              from: caller,266              to: receiver,267              tokenId: tokenId.toString(),268            },269          },270        ]);271      });272      273      expect(erc20Events).to.include.deep.members([274        {275          address,276          event: 'Transfer',277          args: {278            from: caller,279            to: receiver,280            value: '15',281          },282        },283      ]);284    }285286    {287      const balance = await contract.methods.balanceOf(receiver).call();288      expect(+balance).to.equal(1);289    }290291    {292      const balance = await contract.methods.balanceOf(caller).call();293      expect(+balance).to.equal(0);294    }295  });296297  itWeb3('Can perform transfer()', async ({web3, api, privateKeyWrapper}) => {298    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);299    const helper = evmCollectionHelpers(web3, caller);300    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();301    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);302    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});303304    const receiver = createEthAccount(web3);305306    const tokenId = await contract.methods.nextTokenId().call();307    await contract.methods.mint(caller, tokenId).send();308309    {310      const result = await contract.methods.transfer(receiver, tokenId).send();311      const events = normalizeEvents(result.events);312      expect(events).to.include.deep.members([313        {314          address: collectionIdAddress,315          event: 'Transfer',316          args: {317            from: caller,318            to: receiver,319            tokenId: tokenId.toString(),320          },321        },322      ]);323    }324325    {326      const balance = await contract.methods.balanceOf(caller).call();327      expect(+balance).to.equal(0);328    }329330    {331      const balance = await contract.methods.balanceOf(receiver).call();332      expect(+balance).to.equal(1);333    }334  });335336  itWeb3('transfer event on transfer from partial ownership to full ownership', async ({api, web3, privateKeyWrapper}) => {337    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);338    const receiver = createEthAccount(web3);339    const helper = evmCollectionHelpers(web3, caller);340    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();341    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);342    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});343344    const tokenId = await contract.methods.nextTokenId().call();345    await contract.methods.mint(caller, tokenId).send();346347    const tokenAddress = tokenIdToAddress(collectionId, tokenId);348    const tokenContract = new web3.eth.Contract(reFungibleTokenAbi as any, tokenAddress, {from: caller, ...GAS_ARGS});349350    await tokenContract.methods.repartition(2).send();351    await tokenContract.methods.transfer(receiver, 1).send();352353    const events =  await recordEvents(contract, async () => 354      await tokenContract.methods.transfer(receiver, 1).send());355    expect(events).to.deep.equal([356      {357        address: collectionIdAddress,358        event: 'Transfer',359        args: {360          from: '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF',361          to: receiver,362          tokenId: tokenId.toString(),363        },364      },365    ]);366  });367368  itWeb3('transfer event on transfer from full ownership to partial ownership', async ({api, web3, privateKeyWrapper}) => {369    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);370    const receiver = createEthAccount(web3);371    const helper = evmCollectionHelpers(web3, caller);372    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();373    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);374    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});375376    const tokenId = await contract.methods.nextTokenId().call();377    await contract.methods.mint(caller, tokenId).send();378379    const tokenAddress = tokenIdToAddress(collectionId, tokenId);380    const tokenContract = new web3.eth.Contract(reFungibleTokenAbi as any, tokenAddress, {from: caller, ...GAS_ARGS});381382    await tokenContract.methods.repartition(2).send();383    384    const events =  await recordEvents(contract, async () => 385      await tokenContract.methods.transfer(receiver, 1).send());386      387    expect(events).to.deep.equal([388      {389        address: collectionIdAddress,390        event: 'Transfer',391        args: {392          from: caller,393          to: '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF',394          tokenId: tokenId.toString(),395        },396      },397    ]);398  });399});400401describe('RFT: Fees', () => {402  itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {403    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);404    const helper = evmCollectionHelpers(web3, caller);405    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();406    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);407    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});408409    const receiver = createEthAccount(web3);410411    const tokenId = await contract.methods.nextTokenId().call();412    await contract.methods.mint(caller, tokenId).send();413414    const cost = await recordEthFee(api, caller, () => contract.methods.transferFrom(caller, receiver, tokenId).send());415    expect(cost < BigInt(0.2 * Number(UNIQUE)));416    expect(cost > 0n);417  });418419  itWeb3('transfer() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {420    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);421    const helper = evmCollectionHelpers(web3, caller);422    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();423    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);424    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});425426    const receiver = createEthAccount(web3);427428    const tokenId = await contract.methods.nextTokenId().call();429    await contract.methods.mint(caller, tokenId).send();430431    const cost = await recordEthFee(api, caller, () => contract.methods.transfer(receiver, tokenId).send());432    expect(cost < BigInt(0.2 * Number(UNIQUE)));433    expect(cost > 0n);434  });435});436437describe('Common metadata', () => {438  itWeb3('Returns collection name', async ({api, web3, privateKeyWrapper}) => {439    const collection = await createCollectionExpectSuccess({440      name: 'token name',441      mode: {type: 'ReFungible'},442    });443    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);444445    const address = collectionIdToAddress(collection);446    const contract = evmCollection(web3, caller, address, {type: 'ReFungible'});447    const name = await contract.methods.name().call();448449    expect(name).to.equal('token name');450  });451452  itWeb3('Returns symbol name', async ({api, web3, privateKeyWrapper}) => {453    const collection = await createCollectionExpectSuccess({454      tokenPrefix: 'TOK',455      mode: {type: 'ReFungible'},456    });457    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);458459    const address = collectionIdToAddress(collection);460    const contract = evmCollection(web3, caller, address, {type: 'ReFungible'});461    const symbol = await contract.methods.symbol().call();462463    expect(symbol).to.equal('TOK');464  });465});