git.delta.rocks / unique-network / refs/commits / edbaeb3cd480

difftreelog

source

tests/src/eth/reFungible.test.ts19.1 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, requirePallets, Pallets} from '../util/helpers';18import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, evmCollection, evmCollectionHelpers, getCollectionAddressFromResult, itWeb3, normalizeEvents, recordEthFee, recordEvents, tokenIdToAddress, uniqueRefungibleToken} from './util/helpers';19import {expect} from 'chai';2021describe('Refungible: Information getting', () => {22  before(async function() {23    await requirePallets(this, [Pallets.ReFungible]);24  });2526  itWeb3('totalSupply', async ({api, web3, privateKeyWrapper}) => {27    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);28    const helper = evmCollectionHelpers(web3, caller);29    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();30    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);31    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});32    const nextTokenId = await contract.methods.nextTokenId().call();33    await contract.methods.mint(caller, nextTokenId).send();34    const totalSupply = await contract.methods.totalSupply().call();35    expect(totalSupply).to.equal('1');36  });3738  itWeb3('balanceOf', async ({api, web3, privateKeyWrapper}) => {39    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);40    const helper = evmCollectionHelpers(web3, caller);41    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();42    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);43    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});4445    {46      const nextTokenId = await contract.methods.nextTokenId().call();47      await contract.methods.mint(caller, nextTokenId).send();48    }49    {50      const nextTokenId = await contract.methods.nextTokenId().call();51      await contract.methods.mint(caller, nextTokenId).send();52    }53    {54      const nextTokenId = await contract.methods.nextTokenId().call();55      await contract.methods.mint(caller, nextTokenId).send();56    }5758    const balance = await contract.methods.balanceOf(caller).call();5960    expect(balance).to.equal('3');61  });6263  itWeb3('ownerOf', async ({api, web3, privateKeyWrapper}) => {64    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);65    const helper = evmCollectionHelpers(web3, caller);66    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();67    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);68    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});6970    const tokenId = await contract.methods.nextTokenId().call();71    await contract.methods.mint(caller, tokenId).send();7273    const owner = await contract.methods.ownerOf(tokenId).call();7475    expect(owner).to.equal(caller);76  });7778  itWeb3('ownerOf after burn', async ({api, web3, privateKeyWrapper}) => {79    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);80    const receiver = createEthAccount(web3);81    const helper = evmCollectionHelpers(web3, caller);82    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();83    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);84    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});8586    const tokenId = await contract.methods.nextTokenId().call();87    await contract.methods.mint(caller, tokenId).send();8889    const tokenAddress = tokenIdToAddress(collectionId, tokenId);90    const tokenContract = uniqueRefungibleToken(web3, tokenAddress, caller);9192    await tokenContract.methods.repartition(2).send();93    await tokenContract.methods.transfer(receiver, 1).send();9495    await tokenContract.methods.burnFrom(caller, 1).send();9697    const owner = await contract.methods.ownerOf(tokenId).call();9899    expect(owner).to.equal(receiver);100  });101102  itWeb3('ownerOf for partial ownership', async ({api, web3, privateKeyWrapper}) => {103    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);104    const receiver = createEthAccount(web3);105    const helper = evmCollectionHelpers(web3, caller);106    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();107    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);108    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});109110    const tokenId = await contract.methods.nextTokenId().call();111    await contract.methods.mint(caller, tokenId).send();112113    const tokenAddress = tokenIdToAddress(collectionId, tokenId);114    const tokenContract = uniqueRefungibleToken(web3, tokenAddress, caller);115116    await tokenContract.methods.repartition(2).send();117    await tokenContract.methods.transfer(receiver, 1).send();118119    const owner = await contract.methods.ownerOf(tokenId).call();120121    expect(owner).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');122  });123});124125describe('Refungible: Plain calls', () => {126  before(async function() {127    await requirePallets(this, [Pallets.ReFungible]);128  });129130  itWeb3('Can perform mint()', async ({web3, api, privateKeyWrapper}) => {131    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);132    const helper = evmCollectionHelpers(web3, owner);133    let result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();134    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);135    const receiver = createEthAccount(web3);136    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});137    const nextTokenId = await contract.methods.nextTokenId().call();138139    expect(nextTokenId).to.be.equal('1');140    result = await contract.methods.mintWithTokenURI(141      receiver,142      nextTokenId,143      'Test URI',144    ).send();145146    const events = normalizeEvents(result.events);147148    expect(events).to.include.deep.members([149      {150        address: collectionIdAddress,151        event: 'Transfer',152        args: {153          from: '0x0000000000000000000000000000000000000000',154          to: receiver,155          tokenId: nextTokenId,156        },157      },158    ]);159160    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');161  });162163  itWeb3('Can perform mintBulk()', async ({web3, api, privateKeyWrapper}) => {164    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);165    const helper = evmCollectionHelpers(web3, caller);166    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();167    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);168    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});169170    const receiver = createEthAccount(web3);171172    {173      const nextTokenId = await contract.methods.nextTokenId().call();174      expect(nextTokenId).to.be.equal('1');175      const result = await contract.methods.mintBulkWithTokenURI(176        receiver,177        [178          [nextTokenId, 'Test URI 0'],179          [+nextTokenId + 1, 'Test URI 1'],180          [+nextTokenId + 2, 'Test URI 2'],181        ],182      ).send();183      const events = normalizeEvents(result.events);184185      expect(events).to.include.deep.members([186        {187          address: collectionIdAddress,188          event: 'Transfer',189          args: {190            from: '0x0000000000000000000000000000000000000000',191            to: receiver,192            tokenId: nextTokenId,193          },194        },195        {196          address: collectionIdAddress,197          event: 'Transfer',198          args: {199            from: '0x0000000000000000000000000000000000000000',200            to: receiver,201            tokenId: String(+nextTokenId + 1),202          },203        },204        {205          address: collectionIdAddress,206          event: 'Transfer',207          args: {208            from: '0x0000000000000000000000000000000000000000',209            to: receiver,210            tokenId: String(+nextTokenId + 2),211          },212        },213      ]);214215      expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');216      expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');217      expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');218    }219  });220221  itWeb3('Can perform burn()', async ({web3, api, privateKeyWrapper}) => {222    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);223    const helper = evmCollectionHelpers(web3, caller);224    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();225    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);226    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});227228    const tokenId = await contract.methods.nextTokenId().call();229    await contract.methods.mint(caller, tokenId).send();230    {231      const result = await contract.methods.burn(tokenId).send();232      const events = normalizeEvents(result.events);233      expect(events).to.include.deep.members([234        {235          address: collectionIdAddress,236          event: 'Transfer',237          args: {238            from: caller,239            to: '0x0000000000000000000000000000000000000000',240            tokenId: tokenId.toString(),241          },242        },243      ]);244    }245  });246247  itWeb3('Can perform transferFrom()', async ({web3, api, privateKeyWrapper}) => {248    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);249    const helper = evmCollectionHelpers(web3, caller);250    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();251    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);252    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});253254    const receiver = createEthAccount(web3);255256    const tokenId = await contract.methods.nextTokenId().call();257    await contract.methods.mint(caller, tokenId).send();258259    const address = tokenIdToAddress(collectionId, tokenId);260    const tokenContract = uniqueRefungibleToken(web3, address, caller);261    await tokenContract.methods.repartition(15).send();262263    {264      const erc20Events = await recordEvents(tokenContract, async () => {265        const result = await contract.methods.transferFrom(caller, receiver, tokenId).send();266        const events = normalizeEvents(result.events);267        expect(events).to.include.deep.members([268          {269            address: collectionIdAddress,270            event: 'Transfer',271            args: {272              from: caller,273              to: receiver,274              tokenId: tokenId.toString(),275            },276          },277        ]);278      });279280      expect(erc20Events).to.include.deep.members([281        {282          address,283          event: 'Transfer',284          args: {285            from: caller,286            to: receiver,287            value: '15',288          },289        },290      ]);291    }292293    {294      const balance = await contract.methods.balanceOf(receiver).call();295      expect(+balance).to.equal(1);296    }297298    {299      const balance = await contract.methods.balanceOf(caller).call();300      expect(+balance).to.equal(0);301    }302  });303304  itWeb3('Can perform transfer()', async ({web3, api, privateKeyWrapper}) => {305    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);306    const helper = evmCollectionHelpers(web3, caller);307    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();308    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);309    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});310311    const receiver = createEthAccount(web3);312313    const tokenId = await contract.methods.nextTokenId().call();314    await contract.methods.mint(caller, tokenId).send();315316    {317      const result = await contract.methods.transfer(receiver, tokenId).send();318      const events = normalizeEvents(result.events);319      expect(events).to.include.deep.members([320        {321          address: collectionIdAddress,322          event: 'Transfer',323          args: {324            from: caller,325            to: receiver,326            tokenId: tokenId.toString(),327          },328        },329      ]);330    }331332    {333      const balance = await contract.methods.balanceOf(caller).call();334      expect(+balance).to.equal(0);335    }336337    {338      const balance = await contract.methods.balanceOf(receiver).call();339      expect(+balance).to.equal(1);340    }341  });342343  itWeb3('transfer event on transfer from partial ownership to full ownership', async ({api, web3, privateKeyWrapper}) => {344    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);345    const receiver = createEthAccount(web3);346    const helper = evmCollectionHelpers(web3, caller);347    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();348    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);349    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});350351    const tokenId = await contract.methods.nextTokenId().call();352    await contract.methods.mint(caller, tokenId).send();353354    const tokenAddress = tokenIdToAddress(collectionId, tokenId);355    const tokenContract = uniqueRefungibleToken(web3, tokenAddress, caller);356357    await tokenContract.methods.repartition(2).send();358    await tokenContract.methods.transfer(receiver, 1).send();359360    const events =  await recordEvents(contract, async () =>361      await tokenContract.methods.transfer(receiver, 1).send());362    expect(events).to.deep.equal([363      {364        address: collectionIdAddress,365        event: 'Transfer',366        args: {367          from: '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF',368          to: receiver,369          tokenId: tokenId.toString(),370        },371      },372    ]);373  });374375  itWeb3('transfer event on transfer from full ownership to partial ownership', async ({api, web3, privateKeyWrapper}) => {376    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);377    const receiver = createEthAccount(web3);378    const helper = evmCollectionHelpers(web3, caller);379    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();380    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);381    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});382383    const tokenId = await contract.methods.nextTokenId().call();384    await contract.methods.mint(caller, tokenId).send();385386    const tokenAddress = tokenIdToAddress(collectionId, tokenId);387    const tokenContract = uniqueRefungibleToken(web3, tokenAddress, caller);388389    await tokenContract.methods.repartition(2).send();390391    const events =  await recordEvents(contract, async () =>392      await tokenContract.methods.transfer(receiver, 1).send());393394    expect(events).to.deep.equal([395      {396        address: collectionIdAddress,397        event: 'Transfer',398        args: {399          from: caller,400          to: '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF',401          tokenId: tokenId.toString(),402        },403      },404    ]);405  });406});407408describe('RFT: Fees', () => {409  before(async function() {410    await requirePallets(this, [Pallets.ReFungible]);411  });412413  itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {414    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);415    const helper = evmCollectionHelpers(web3, caller);416    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();417    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);418    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});419420    const receiver = createEthAccount(web3);421422    const tokenId = await contract.methods.nextTokenId().call();423    await contract.methods.mint(caller, tokenId).send();424425    const cost = await recordEthFee(api, caller, () => contract.methods.transferFrom(caller, receiver, tokenId).send());426    expect(cost < BigInt(0.2 * Number(UNIQUE)));427    expect(cost > 0n);428  });429430  itWeb3('transfer() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {431    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);432    const helper = evmCollectionHelpers(web3, caller);433    const result = await helper.methods.createRefungibleCollection('Mint collection', '6', '6').send();434    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);435    const contract = evmCollection(web3, caller, collectionIdAddress, {type: 'ReFungible'});436437    const receiver = createEthAccount(web3);438439    const tokenId = await contract.methods.nextTokenId().call();440    await contract.methods.mint(caller, tokenId).send();441442    const cost = await recordEthFee(api, caller, () => contract.methods.transfer(receiver, tokenId).send());443    expect(cost < BigInt(0.2 * Number(UNIQUE)));444    expect(cost > 0n);445  });446});447448describe('Common metadata', () => {449  before(async function() {450    await requirePallets(this, [Pallets.ReFungible]);451  });452453  itWeb3('Returns collection name', async ({api, web3, privateKeyWrapper}) => {454    const collection = await createCollectionExpectSuccess({455      name: 'token name',456      mode: {type: 'ReFungible'},457    });458    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);459460    const address = collectionIdToAddress(collection);461    const contract = evmCollection(web3, caller, address, {type: 'ReFungible'});462    const name = await contract.methods.name().call();463464    expect(name).to.equal('token name');465  });466467  itWeb3('Returns symbol name', async ({api, web3, privateKeyWrapper}) => {468    const collection = await createCollectionExpectSuccess({469      tokenPrefix: 'TOK',470      mode: {type: 'ReFungible'},471    });472    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);473474    const address = collectionIdToAddress(collection);475    const contract = evmCollection(web3, caller, address, {type: 'ReFungible'});476    const symbol = await contract.methods.symbol().call();477478    expect(symbol).to.equal('TOK');479  });480});