difftreelog
chore compile fractionalizer in typescript
in: master
4 files changed
.maintain/scripts/compile_fractionalizer.shdiffbeforeafterboth--- a/.maintain/scripts/compile_fractionalizer.sh
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/bin/sh
-set -eu
-
-dir=$PWD
-
-tmp=$(mktemp -d)
-cd $tmp
-mkdir refungible
-mkdir api
-cp $dir/tests/src/eth/fractionalizer/Fractionalizer.sol refungible/input.sol
-cp $dir/tests/src/eth/api/CollectionHelpers.sol api/CollectionHelpers.sol
-cp $dir/tests/src/eth/api/ContractHelpers.sol api/ContractHelpers.sol
-cp $dir/tests/src/eth/api/UniqueRefungibleToken.sol api/UniqueRefungibleToken.sol
-cp $dir/tests/src/eth/api/UniqueRefungible.sol api/UniqueRefungible.sol
-cp $dir/tests/src/eth/api/UniqueNFT.sol api/UniqueNFT.sol
-solcjs --optimize --bin refungible/input.sol -o $PWD
-solcjs --abi -p refungible/input.sol
-
-mv refungible_input_sol_Fractionalizer.bin $dir/tests/src/eth/fractionalizer/Fractionalizer.bin
-mv refungible_input_sol_Fractionalizer.abi refungible_input_sol_Fractionalizer.json
-prettier refungible_input_sol_Fractionalizer.json > $dir/tests/src/eth/fractionalizer/FractionalizerAbi.json
Makefilediffbeforeafterboth--- a/Makefile
+++ b/Makefile
@@ -78,10 +78,7 @@
INPUT=$(COLLECTION_HELPER_STUBS)/$< OUTPUT=$(COLLECTION_HELPER_STUBS)/CollectionHelpers.raw ./.maintain/scripts/compile_stub.sh
INPUT=$(COLLECTION_HELPER_STUBS)/$< OUTPUT=$(COLLECTION_HELPER_ABI) ./.maintain/scripts/generate_abi.sh
-Fractionalizer:
- ./.maintain/scripts/compile_fractionalizer.sh
-
-evm_stubs: UniqueFungible UniqueNFT UniqueRefungible UniqueRefungibleToken ContractHelpers CollectionHelpers Fractionalizer
+evm_stubs: UniqueFungible UniqueNFT UniqueRefungible UniqueRefungibleToken ContractHelpers CollectionHelpers
.PHONY: _bench
_bench:
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.createAndSetRFTCollection('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.setNftCollectionIsAllowed(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.createAndSetRFTCollection('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.setNftCollectionIsAllowed(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.setNftCollectionIsAllowed(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.setNftCollectionIsAllowed(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});177178179180describe('Negative Integration Tests for fractionalizer', () => {181 itWeb3('call setRFTCollection twice', async ({api, web3, privateKeyWrapper}) => {182 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);183 const {collectionIdAddress} = await createRefungibleCollection(api, web3, owner);184 const refungibleContract = uniqueRefungible(web3, collectionIdAddress, owner);185186 const fractionalizer = await deployFractionalizer(api, web3, owner);187 await refungibleContract.methods.addCollectionAdmin(fractionalizer.options.address).send();188 await fractionalizer.methods.setRFTCollection(collectionIdAddress).send();189190 await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())191 .to.eventually.be.rejectedWith(/RFT collection is already set$/g);192 });193194 itWeb3('call setRFTCollection with NFT collection', async ({api, web3, privateKeyWrapper}) => {195 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);196 const {collectionIdAddress} = await createNonfungibleCollection(api, web3, owner);197 const nftContract = uniqueNFT(web3, collectionIdAddress, owner);198199 const fractionalizer = await deployFractionalizer(api, web3, owner);200 await nftContract.methods.addCollectionAdmin(fractionalizer.options.address).send();201202 await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())203 .to.eventually.be.rejectedWith(/Wrong collection type. Collection is not refungible.$/g);204 });205206 itWeb3('call setRFTCollection while not collection admin', async ({api, web3, privateKeyWrapper}) => {207 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);208 const fractionalizer = await deployFractionalizer(api, web3, owner);209 const {collectionIdAddress} = await createRefungibleCollection(api, web3, owner);210211 await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())212 .to.eventually.be.rejectedWith(/Fractionalizer contract should be an admin of the collection$/g);213 });214215 itWeb3('call setRFTCollection after createAndSetRFTCollection', async ({api, web3, privateKeyWrapper}) => {216 const alice = privateKeyWrapper('//Alice');217 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);218 const fractionalizer = await deployFractionalizer(api, web3, owner);219 const tx = api.tx.balances.transfer(evmToAddress(fractionalizer.options.address), 10n * UNIQUE);220 await submitTransactionAsync(alice, tx);221222 const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send({from: owner});223 const collectionIdAddress = result.events.RFTCollectionSet.returnValues._collection;224225 await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())226 .to.eventually.be.rejectedWith(/RFT collection is already set$/g);227 });228229 itWeb3('call nft2rft without setting RFT collection for contract', async ({api, web3, privateKeyWrapper}) => {230 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);231232 const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);233 const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);234 const nftTokenId = await nftContract.methods.nextTokenId().call();235 await nftContract.methods.mint(owner, nftTokenId).send();236237 const fractionalizer = await deployFractionalizer(api, web3, owner);238239 await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call())240 .to.eventually.be.rejectedWith(/RFT collection is not set$/g);241 });242243 itWeb3('call nft2rft while not owner of NFT token', async ({api, web3, privateKeyWrapper}) => {244 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);245 const nftOwner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);246247 const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);248 const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);249 const nftTokenId = await nftContract.methods.nextTokenId().call();250 await nftContract.methods.mint(owner, nftTokenId).send();251 await nftContract.methods.transfer(nftOwner, 1).send();252253254 const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);255 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send();256257 await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call())258 .to.eventually.be.rejectedWith(/Only token owner could fractionalize it$/g);259 });260261 itWeb3('call nft2rft while not in list of allowed accounts', async ({api, web3, privateKeyWrapper}) => {262 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);263264 const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);265 const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);266 const nftTokenId = await nftContract.methods.nextTokenId().call();267 await nftContract.methods.mint(owner, nftTokenId).send();268269 const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);270271 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send();272 await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call())273 .to.eventually.be.rejectedWith(/Fractionalization of this collection is not allowed by admin$/g);274 });275276 itWeb3('call nft2rft while fractionalizer doesnt have approval for nft token', async ({api, web3, privateKeyWrapper}) => {277 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);278279 const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner);280 const nftContract = uniqueNFT(web3, nftCollectionAddress, owner);281 const nftTokenId = await nftContract.methods.nextTokenId().call();282 await nftContract.methods.mint(owner, nftTokenId).send();283284 const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);285286 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send();287 await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call())288 .to.eventually.be.rejectedWith(/ApprovedValueTooLow$/g);289 });290291 itWeb3('call rft2nft without setting RFT collection for contract', async ({api, web3, privateKeyWrapper}) => {292 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);293294 const fractionalizer = await deployFractionalizer(api, web3, owner);295 const {collectionIdAddress: rftCollectionAddress} = await createRefungibleCollection(api, web3, owner);296 const refungibleContract = uniqueRefungible(web3, rftCollectionAddress, owner);297 const rftTokenId = await refungibleContract.methods.nextTokenId().call();298 await refungibleContract.methods.mint(owner, rftTokenId).send();299 300 await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, rftTokenId).call())301 .to.eventually.be.rejectedWith(/RFT collection is not set$/g);302 });303304 itWeb3('call rft2nft for RFT token that is not from configured RFT collection', async ({api, web3, privateKeyWrapper}) => {305 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);306307 const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner);308 const {collectionIdAddress: rftCollectionAddress} = await createRefungibleCollection(api, web3, owner);309 const refungibleContract = uniqueRefungible(web3, rftCollectionAddress, owner);310 const rftTokenId = await refungibleContract.methods.nextTokenId().call();311 await refungibleContract.methods.mint(owner, rftTokenId).send();312 313 await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, rftTokenId).call())314 .to.eventually.be.rejectedWith(/Wrong RFT collection$/g);315 });316317 itWeb3('call rft2nft for RFT token that was not minted by fractionalizer contract', async ({api, web3, privateKeyWrapper}) => {318 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);319 const {collectionIdAddress: rftCollectionAddress} = await createRefungibleCollection(api, web3, owner);320321 const fractionalizer = await deployFractionalizer(api, web3, owner);322 const refungibleContract = uniqueRefungible(web3, rftCollectionAddress, owner);323324 await refungibleContract.methods.addCollectionAdmin(fractionalizer.options.address).send();325 await fractionalizer.methods.setRFTCollection(rftCollectionAddress).send();326327 const rftTokenId = await refungibleContract.methods.nextTokenId().call();328 await refungibleContract.methods.mint(owner, rftTokenId).send();329 330 await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, rftTokenId).call())331 .to.eventually.be.rejectedWith(/No corresponding NFT token found$/g);332 });333334 itWeb3('call rft2nft without owning all RFT pieces', async ({api, web3, privateKeyWrapper}) => {335 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);336 const receiver = await createEthAccountWithBalance(api, web3, privateKeyWrapper);337338 const {fractionalizer, rftCollectionAddress} = await initFractionalizer(api, web3, privateKeyWrapper, owner);339 const {rftTokenAddress} = await createRFTToken(api, web3, owner, fractionalizer, 100n);340 341 const {tokenId} = tokenIdFromAddress(rftTokenAddress);342 const refungibleTokenContract = uniqueRefungibleToken(web3, rftTokenAddress, owner);343 await refungibleTokenContract.methods.transfer(receiver, 50).send();344 await refungibleTokenContract.methods.approve(fractionalizer.options.address, 50).send();345 await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, tokenId).call())346 .to.eventually.be.rejectedWith(/Not all pieces are owned by the caller$/g);347 });348});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});tests/src/eth/util/helpers.tsdiffbeforeafterboth--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -250,7 +250,12 @@
return Web3.utils.toChecksumAddress(subToEthLowercase(eth));
}
-export function compileContract(name: string, src: string) {
+export interface CompiledContract {
+ abi: any,
+ object: string,
+}
+
+export function compileContract(name: string, src: string) : CompiledContract {
const out = JSON.parse(solc.compile(JSON.stringify({
language: 'Solidity',
sources: {