difftreelog
tests(refungible-pallet): add negative tests for fractionalizer contract
in: master
1 file changed
tests/src/eth/fractionalizer/fractionalizer.test.tsdiffbeforeafterboth1// 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 fractionalizerAbi from './FractionalizerAbi.json';23import {submitTransactionAsync} from '../../substrate/substrate-api';24import {UNIQUE} from '../../util/helpers';25import {collectionIdToAddress, createEthAccountWithBalance, createNonfungibleCollection, createRefungibleCollection, GAS_ARGS, itWeb3, tokenIdFromAddress, uniqueNFT, uniqueRefungible, uniqueRefungibleToken} from '../util/helpers';26import {Contract} from 'web3-eth-contract';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;3536async function deployFractionalizer(api: ApiPromise, web3: Web3, owner: string) {37 const fractionalizerContract = new web3.eth.Contract(fractionalizerAbi as any, undefined, {38 from: owner,39 ...GAS_ARGS,40 });41 return await fractionalizerContract.deploy({data: (await readFile(`${__dirname}/Fractionalizer.bin`)).toString()}).send({from: owner});42}4344async function initFractionalizer(api: ApiPromise, web3: Web3, privateKeyWrapper: (account: string) => IKeyringPair, owner: string) {45 const fractionalizer = await deployFractionalizer(api, web3, owner);46 const tx = api.tx.balances.transfer(evmToAddress(fractionalizer.options.address), 10n * UNIQUE);47 const alice = privateKeyWrapper('//Alice');48 await submitTransactionAsync(alice, tx);49 const result = await fractionalizer.methods.mintRFTCollection('A', 'B', 'C').send();50 const rftCollectionAddress = result.events.RFTCollectionSet.returnValues._collection;51 return {fractionalizer, rftCollectionAddress};52}5354async function createRFTToken(api: ApiPromise, web3: Web3, owner: string, fractionalizer: Contract, amount: bigint) {55 const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);56 const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);57 const nftTokenId = await nftContract.methods.nextTokenId().call();58 await nftContract.methods.mint(owner, nftTokenId).send();5960 await fractionalizer.methods.setAllowlist(nftCollectionAddress, true).send();61 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send();62 const result = await fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, amount).send();63 const {_collection, _tokenId, _rftToken} = result.events.Fractionalized.returnValues;64 return {65 nftCollectionAddress: _collection,66 nftTokenId: _tokenId,67 rftTokenAddress: _rftToken,68 };69}7071describe('Fractionalizer contract usage', () => {72 itWeb3('Set RFT collection', async ({api, web3, privateKeyWrapper}) => {73 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);74 const fractionalizer = await deployFractionalizer(api, web3, owner);75 const {collectionIdAddress} = await createRefungibleCollection(api, web3, owner);76 const refungibleContract = uniqueRefungible(web3, collectionIdAddress, owner);77 await refungibleContract.methods.addCollectionAdmin(fractionalizer.options.address).send();78 const result = await fractionalizer.methods.setRFTCollection(collectionIdAddress).send();79 expect(result.events).to.be.like({80 RFTCollectionSet: {81 returnValues: {82 _collection: collectionIdAddress,83 },84 },85 });86 });8788 itWeb3('Mint RFT collection', async ({api, web3, privateKeyWrapper}) => {89 const alice = privateKeyWrapper('//Alice');90 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);91 const fractionalizer = await deployFractionalizer(api, web3, owner);92 const tx = api.tx.balances.transfer(evmToAddress(fractionalizer.options.address), 10n * UNIQUE);93 await submitTransactionAsync(alice, tx);9495 const result = await fractionalizer.methods.mintRFTCollection('A', 'B', 'C').send({from: owner});96 expect(result.events).to.be.like({97 RFTCollectionSet: {},98 });99 expect(result.events.RFTCollectionSet.returnValues._collection).to.be.ok;100 });101102 itWeb3('Set Allowlist', async ({api, web3, privateKeyWrapper}) => {103 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);104 const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner); 105106 const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);107 const result1 = await fractionalizer.methods.setAllowlist(nftCollectionAddress, true).send({from: owner});108 expect(result1.events).to.be.like({109 AllowListSet: {110 returnValues: {111 _collection: nftCollectionAddress,112 _status: true,113 },114 },115 });116 const result2 = await fractionalizer.methods.setAllowlist(nftCollectionAddress, false).send({from: owner});117 expect(result2.events).to.be.like({118 AllowListSet: {119 returnValues: {120 _collection: nftCollectionAddress,121 _status: false,122 },123 },124 });125 });126127 itWeb3('NFT to RFT', async ({api, web3, privateKeyWrapper}) => {128 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);129130 const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);131 const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);132 const nftTokenId = await nftContract.methods.nextTokenId().call();133 await nftContract.methods.mint(owner, nftTokenId).send();134135 const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);136137 await fractionalizer.methods.setAllowlist(nftCollectionAddress, true).send();138 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send();139 const result = await fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).send();140 expect(result.events).to.be.like({141 Fractionalized: {142 returnValues: {143 _collection: nftCollectionAddress,144 _tokenId: nftTokenId,145 _amount: '100',146 },147 },148 });149 const rftTokenAddress = result.events.Fractionalized.returnValues._rftToken;150 const rftTokenContract = uniqueRefungibleToken(web3, rftTokenAddress, owner);151 expect(await rftTokenContract.methods.balanceOf(owner).call()).to.equal('100');152 });153154 itWeb3('RFT to NFT', async ({api, web3, privateKeyWrapper}) => {155 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);156157 const {fractionalizer, rftCollectionAddress} = await initFractionalizer(api, web3, privateKeyWrapper, owner);158 const {rftTokenAddress, nftCollectionAddress, nftTokenId} = await createRFTToken(api, web3, owner, fractionalizer, 100n);159160 const {collectionId, tokenId} = tokenIdFromAddress(rftTokenAddress);161 const refungibleAddress = collectionIdToAddress(collectionId);162 expect(rftCollectionAddress).to.be.equal(refungibleAddress);163 const refungibleTokenContract = uniqueRefungibleToken(web3, rftTokenAddress, owner);164 await refungibleTokenContract.methods.approve(fractionalizer.options.address, 100).send();165 const result = await fractionalizer.methods.rft2nft(refungibleAddress, tokenId).send();166 expect(result.events).to.be.like({167 DeFractionalized: {168 returnValues: {169 _rftToken: rftTokenAddress,170 _nftCollection: nftCollectionAddress,171 _nftTokenId: nftTokenId,172 },173 },174 });175 });176});