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

difftreelog

source

tests/src/eth/fractionalizer/fractionalizer.test.ts19.6 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/>.161718import Web3 from 'web3';19import {ApiPromise} from '@polkadot/api';20import {evmToAddress} from '@polkadot/util-crypto';21import {readFile} from 'fs/promises';22import {submitTransactionAsync} from '../../substrate/substrate-api';23import {UNIQUE} from '../../util/helpers';24import {collectionIdToAddress, CompiledContract, createEthAccountWithBalance, createNonfungibleCollection, createRefungibleCollection, GAS_ARGS, itWeb3, tokenIdFromAddress, uniqueNFT, uniqueRefungible, uniqueRefungibleToken} from '../util/helpers';25import {Contract} from 'web3-eth-contract';26import * as solc from 'solc';2728import chai from 'chai';29import chaiAsPromised from 'chai-as-promised';30import chaiLike from 'chai-like';31import {IKeyringPair} from '@polkadot/types/types';32chai.use(chaiAsPromised);33chai.use(chaiLike);34const expect = chai.expect;35let fractionalizer: CompiledContract;3637async function compileFractionalizer() {38  if (!fractionalizer) {39    const input = {40      language: 'Solidity',41      sources: {42        ['Fractionalizer.sol']: {43          content: (await readFile(`${__dirname}/Fractionalizer.sol`)).toString(),44        },45      },46      settings: {47        outputSelection: {48          '*': {49            '*': ['*'],50          },51        },52      },53    };54    const json = JSON.parse(solc.compile(JSON.stringify(input), {import: await findImports()}));55    const out = json.contracts['Fractionalizer.sol']['Fractionalizer'];5657    fractionalizer = {58      abi: out.abi,59      object: '0x' + out.evm.bytecode.object,60    };61  }62  return fractionalizer;63}6465async function findImports() {66  const collectionHelpers = (await readFile(`${__dirname}/../api/CollectionHelpers.sol`)).toString();67  const contractHelpers = (await readFile(`${__dirname}/../api/ContractHelpers.sol`)).toString();68  const uniqueRefungibleToken = (await readFile(`${__dirname}/../api/UniqueRefungibleToken.sol`)).toString();69  const uniqueRefungible = (await readFile(`${__dirname}/../api/UniqueRefungible.sol`)).toString();70  const uniqueNFT = (await readFile(`${__dirname}/../api/UniqueNFT.sol`)).toString();7172  return function(path: string) {73    switch (path) {74      case 'api/CollectionHelpers.sol': return {contents: `${collectionHelpers}`};75      case 'api/ContractHelpers.sol': return {contents: `${contractHelpers}`};76      case 'api/UniqueRefungibleToken.sol': return {contents: `${uniqueRefungibleToken}`};77      case 'api/UniqueRefungible.sol': return {contents: `${uniqueRefungible}`};78      case 'api/UniqueNFT.sol': return {contents: `${uniqueNFT}`};79      default: return {error: 'File not found'};80    }81  };82}8384async function deployFractionalizer(web3: Web3, owner: string) {85  const compiled = await compileFractionalizer();86  const fractionalizerContract = new web3.eth.Contract(compiled.abi, undefined, {87    data: compiled.object,88    from: owner,89    ...GAS_ARGS,90  });91  return await fractionalizerContract.deploy({data: compiled.object}).send({from: owner});92}9394async function initFractionalizer(api: ApiPromise, web3: Web3, privateKeyWrapper: (account: string) => IKeyringPair, owner: string) {95  const fractionalizer = await deployFractionalizer(web3, owner);96  const tx = api.tx.balances.transfer(evmToAddress(fractionalizer.options.address), 10n * UNIQUE);97  const alice = privateKeyWrapper('//Alice');98  await submitTransactionAsync(alice, tx);99  const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send();100  const rftCollectionAddress = result.events.RFTCollectionSet.returnValues._collection;101  return {fractionalizer, rftCollectionAddress};102}103104async function createRFTToken(api: ApiPromise, web3: Web3, owner: string, fractionalizer: Contract, amount: bigint) {105  const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);106  const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);107  const nftTokenId = await nftContract.methods.nextTokenId().call();108  await nftContract.methods.mint(owner, nftTokenId).send();109110  await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send();111  await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send();112  const result = await fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, amount).send();113  const {_collection, _tokenId, _rftToken} = result.events.Fractionalized.returnValues;114  return {115    nftCollectionAddress: _collection,116    nftTokenId: _tokenId,117    rftTokenAddress: _rftToken,118  };119}120121describe('Fractionalizer contract usage', () => {122  itWeb3('Set RFT collection', async ({api, web3, privateKeyWrapper}) => {123    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);124    const fractionalizer = await deployFractionalizer(web3, owner);125    const {collectionIdAddress} = await createRefungibleCollection(api, web3, owner);126    const refungibleContract = uniqueRefungible(web3, collectionIdAddress, owner);127    await refungibleContract.methods.addCollectionAdmin(fractionalizer.options.address).send();128    const result = await fractionalizer.methods.setRFTCollection(collectionIdAddress).send();129    expect(result.events).to.be.like({130      RFTCollectionSet: {131        returnValues: {132          _collection: collectionIdAddress,133        },134      },135    });136  });137138  itWeb3('Mint RFT collection', async ({api, web3, privateKeyWrapper}) => {139    const alice = privateKeyWrapper('//Alice');140    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);141    const fractionalizer = await deployFractionalizer(web3, owner);142    const tx = api.tx.balances.transfer(evmToAddress(fractionalizer.options.address), 10n * UNIQUE);143    await submitTransactionAsync(alice, tx);144145    const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send({from: owner});146    expect(result.events).to.be.like({147      RFTCollectionSet: {},148    });149    expect(result.events.RFTCollectionSet.returnValues._collection).to.be.ok;150  });151152  itWeb3('Set Allowlist', async ({api, web3, privateKeyWrapper}) => {153    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);154    const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);    155156    const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);157    const result1 = await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send({from: owner});158    expect(result1.events).to.be.like({159      AllowListSet: {160        returnValues: {161          _collection: nftCollectionAddress,162          _status: true,163        },164      },165    });166    const result2 = await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, false).send({from: owner});167    expect(result2.events).to.be.like({168      AllowListSet: {169        returnValues: {170          _collection: nftCollectionAddress,171          _status: false,172        },173      },174    });175  });176177  itWeb3('NFT to RFT', async ({api, web3, privateKeyWrapper}) => {178    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);179180    const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);181    const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);182    const nftTokenId = await nftContract.methods.nextTokenId().call();183    await nftContract.methods.mint(owner, nftTokenId).send();184185    const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);186187    await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send();188    await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send();189    const result = await fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).send();190    expect(result.events).to.be.like({191      Fractionalized: {192        returnValues: {193          _collection: nftCollectionAddress,194          _tokenId: nftTokenId,195          _amount: '100',196        },197      },198    });199    const rftTokenAddress = result.events.Fractionalized.returnValues._rftToken;200    const rftTokenContract = uniqueRefungibleToken(web3, rftTokenAddress, owner);201    expect(await rftTokenContract.methods.balanceOf(owner).call()).to.equal('100');202  });203204  itWeb3('RFT to NFT', async ({api, web3, privateKeyWrapper}) => {205    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);206207    const {fractionalizer, rftCollectionAddress} = await initFractionalizer(api, web3, privateKeyWrapper, owner);208    const {rftTokenAddress, nftCollectionAddress, nftTokenId} = await createRFTToken(api, web3, owner, fractionalizer, 100n);209210    const {collectionId, tokenId} = tokenIdFromAddress(rftTokenAddress);211    const refungibleAddress = collectionIdToAddress(collectionId);212    expect(rftCollectionAddress).to.be.equal(refungibleAddress);213    const refungibleTokenContract = uniqueRefungibleToken(web3, rftTokenAddress, owner);214    await refungibleTokenContract.methods.approve(fractionalizer.options.address, 100).send();215    const result = await fractionalizer.methods.rft2nft(refungibleAddress, tokenId).send();216    expect(result.events).to.be.like({217      Defractionalized: {218        returnValues: {219          _rftToken: rftTokenAddress,220          _nftCollection: nftCollectionAddress,221          _nftTokenId: nftTokenId,222        },223      },224    });225  });226});227228229230describe('Negative Integration Tests for fractionalizer', () => {231  itWeb3('call setRFTCollection twice', async ({api, web3, privateKeyWrapper}) => {232    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);233    const {collectionIdAddress} = await createRefungibleCollection(api, web3, owner);234    const refungibleContract = uniqueRefungible(web3, collectionIdAddress, owner);235236    const fractionalizer = await deployFractionalizer(web3, owner);237    await refungibleContract.methods.addCollectionAdmin(fractionalizer.options.address).send();238    await fractionalizer.methods.setRFTCollection(collectionIdAddress).send();239240    await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())241      .to.eventually.be.rejectedWith(/RFT collection is already set$/g);242  });243244  itWeb3('call setRFTCollection with NFT collection', async ({api, web3, privateKeyWrapper}) => {245    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);246    const {collectionIdAddress} = await createNonfungibleCollection(api, web3, owner);247    const nftContract = uniqueNFT(web3, collectionIdAddress, owner);248249    const fractionalizer = await deployFractionalizer(web3, owner);250    await nftContract.methods.addCollectionAdmin(fractionalizer.options.address).send();251252    await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())253      .to.eventually.be.rejectedWith(/Wrong collection type. Collection is not refungible.$/g);254  });255256  itWeb3('call setRFTCollection while not collection admin', async ({api, web3, privateKeyWrapper}) => {257    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);258    const fractionalizer = await deployFractionalizer(web3, owner);259    const {collectionIdAddress} = await createRefungibleCollection(api, web3, owner);260261    await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())262      .to.eventually.be.rejectedWith(/Fractionalizer contract should be an admin of the collection$/g);263  });264265  itWeb3('call setRFTCollection after createAndSetRFTCollection', async ({api, web3, privateKeyWrapper}) => {266    const alice = privateKeyWrapper('//Alice');267    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);268    const fractionalizer = await deployFractionalizer(web3, owner);269    const tx = api.tx.balances.transfer(evmToAddress(fractionalizer.options.address), 10n * UNIQUE);270    await submitTransactionAsync(alice, tx);271272    const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send({from: owner});273    const collectionIdAddress = result.events.RFTCollectionSet.returnValues._collection;274275    await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())276      .to.eventually.be.rejectedWith(/RFT collection is already set$/g);277  });278279  itWeb3('call nft2rft without setting RFT collection for contract', async ({api, web3, privateKeyWrapper}) => {280    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);281282    const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);283    const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);284    const nftTokenId = await nftContract.methods.nextTokenId().call();285    await nftContract.methods.mint(owner, nftTokenId).send();286287    const fractionalizer = await deployFractionalizer(web3, owner);288289    await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call())290      .to.eventually.be.rejectedWith(/RFT collection is not set$/g);291  });292293  itWeb3('call nft2rft while not owner of NFT token', async ({api, web3, privateKeyWrapper}) => {294    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);295    const nftOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);296297    const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);298    const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);299    const nftTokenId = await nftContract.methods.nextTokenId().call();300    await nftContract.methods.mint(owner, nftTokenId).send();301    await nftContract.methods.transfer(nftOwner, 1).send();302303304    const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);305    await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send();306307    await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call())308      .to.eventually.be.rejectedWith(/Only token owner could fractionalize it$/g);309  });310311  itWeb3('call nft2rft while not in list of allowed accounts', async ({api, web3, privateKeyWrapper}) => {312    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);313314    const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);315    const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);316    const nftTokenId = await nftContract.methods.nextTokenId().call();317    await nftContract.methods.mint(owner, nftTokenId).send();318319    const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);320321    await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send();322    await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call())323      .to.eventually.be.rejectedWith(/Fractionalization of this collection is not allowed by admin$/g);324  });325326  itWeb3('call nft2rft while fractionalizer doesnt have approval for nft token', async ({api, web3, privateKeyWrapper}) => {327    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);328329    const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);330    const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);331    const nftTokenId = await nftContract.methods.nextTokenId().call();332    await nftContract.methods.mint(owner, nftTokenId).send();333334    const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);335336    await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send();337    await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call())338      .to.eventually.be.rejectedWith(/ApprovedValueTooLow$/g);339  });340341  itWeb3('call rft2nft without setting RFT collection for contract', async ({api, web3, privateKeyWrapper}) => {342    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);343344    const fractionalizer = await deployFractionalizer(web3, owner);345    const {collectionIdAddress: rftCollectionAddress} = await createRefungibleCollection(api, web3, owner);346    const refungibleContract = uniqueRefungible(web3, rftCollectionAddress, owner);347    const rftTokenId = await refungibleContract.methods.nextTokenId().call();348    await refungibleContract.methods.mint(owner, rftTokenId).send();349    350    await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, rftTokenId).call())351      .to.eventually.be.rejectedWith(/RFT collection is not set$/g);352  });353354  itWeb3('call rft2nft for RFT token that is not from configured RFT collection', async ({api, web3, privateKeyWrapper}) => {355    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);356357    const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);358    const {collectionIdAddress: rftCollectionAddress} = await createRefungibleCollection(api, web3, owner);359    const refungibleContract = uniqueRefungible(web3, rftCollectionAddress, owner);360    const rftTokenId = await refungibleContract.methods.nextTokenId().call();361    await refungibleContract.methods.mint(owner, rftTokenId).send();362    363    await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, rftTokenId).call())364      .to.eventually.be.rejectedWith(/Wrong RFT collection$/g);365  });366367  itWeb3('call rft2nft for RFT token that was not minted by fractionalizer contract', async ({api, web3, privateKeyWrapper}) => {368    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);369    const {collectionIdAddress: rftCollectionAddress} = await createRefungibleCollection(api, web3, owner);370371    const fractionalizer = await deployFractionalizer(web3, owner);372    const refungibleContract = uniqueRefungible(web3, rftCollectionAddress, owner);373374    await refungibleContract.methods.addCollectionAdmin(fractionalizer.options.address).send();375    await fractionalizer.methods.setRFTCollection(rftCollectionAddress).send();376377    const rftTokenId = await refungibleContract.methods.nextTokenId().call();378    await refungibleContract.methods.mint(owner, rftTokenId).send();379    380    await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, rftTokenId).call())381      .to.eventually.be.rejectedWith(/No corresponding NFT token found$/g);382  });383384  itWeb3('call rft2nft without owning all RFT pieces', async ({api, web3, privateKeyWrapper}) => {385    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);386    const receiver = await createEthAccountWithBalance(api, web3, privateKeyWrapper);387388    const {fractionalizer, rftCollectionAddress} = await initFractionalizer(api, web3, privateKeyWrapper, owner);389    const {rftTokenAddress} = await createRFTToken(api, web3, owner, fractionalizer, 100n);390    391    const {tokenId} = tokenIdFromAddress(rftTokenAddress);392    const refungibleTokenContract = uniqueRefungibleToken(web3, rftTokenAddress, owner);393    await refungibleTokenContract.methods.transfer(receiver, 50).send();394    await refungibleTokenContract.methods.approve(fractionalizer.options.address, 50).send();395    await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, tokenId).call())396      .to.eventually.be.rejectedWith(/Not all pieces are owned by the caller$/g);397  });398});