From e38fb0c231d7a1798ab5f5cbec9797ff95e8da42 Mon Sep 17 00:00:00 2001 From: Grigoriy Simonov Date: Thu, 11 Aug 2022 09:44:00 +0000 Subject: [PATCH] chore: add Fractionalizer contract documentation, prevent QTZ/UNQ transfers from nonowners, tests for TransfersNotAllowed --- --- a/pallets/refungible/src/erc_token.rs +++ b/pallets/refungible/src/erc_token.rs @@ -186,7 +186,7 @@ .weight_calls_budget(>::find_parent()); >::transfer(self, &caller, &to, self.1, amount, &budget) - .map_err(|_| "transfer error")?; + .map_err(dispatch_to_evm::)?; Ok(true) } --- a/tests/src/eth/fractionalizer/Fractionalizer.sol +++ b/tests/src/eth/fractionalizer/Fractionalizer.sol @@ -6,6 +6,9 @@ import {UniqueRefungible} from "../api/UniqueRefungible.sol"; import {UniqueNFT} from "../api/UniqueNFT.sol"; +/// @dev Fractionalization contract. It stores mappings between NFT and RFT tokens, +/// stores allowlist of NFT tokens available for fractionalization, has methods +/// for fractionalization and defractionalization of NFT tokens. contract Fractionalizer { struct Token { address _collection; @@ -17,9 +20,10 @@ mapping(address => Token) rft2nftMapping; bytes32 refungibleCollectionType = keccak256(bytes("ReFungible")); - constructor() { - } + //TODO: add nonPayable modifier after Solidity updates to 0.9. + receive() external payable onlyOwner {} + /// @dev Method modifier to only allow contract owner to call it. modifier onlyOwner() { address contracthelpersAddress = 0x842899ECF380553E8a4de75bF534cdf6fBF64049; ContractHelpers contractHelpers = ContractHelpers(contracthelpersAddress); @@ -28,11 +32,26 @@ _; } + /// @dev This emits when RFT collection setting is changed. event RFTCollectionSet(address _collection); + + /// @dev This emits when NFT collection is allowed or disallowed. event AllowListSet(address _collection, bool _status); + + /// @dev This emits when NFT token is fractionalized by contract. event Fractionalized(address _collection, uint256 _tokenId, address _rftToken, uint128 _amount); + + /// @dev This emits when NFT token is defractionalized by contract. event Defractionalized(address _rftToken, address _nftCollection, uint256 _nftTokenId); + /// Set RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens + /// would be created in this collection. + /// @dev Throws if RFT collection is already configured for this contract. + /// Throws if collection of wrong type (NFT, Fungible) is provided instead + /// of RFT collection. + /// Throws if `msg.sender` is not owner or admin of provided RFT collection. + /// Can only be called by contract owner. + /// @param _collection address of RFT collection. function setRFTCollection(address _collection) public onlyOwner { require( rftCollection == address(0), @@ -53,6 +72,13 @@ emit RFTCollectionSet(rftCollection); } + /// Creates and sets RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens + /// would be created in this collection. + /// @dev Throws if RFT collection is already configured for this contract. + /// Can only be called by contract owner. + /// @param _name name for created RFT collection. + /// @param _description description for created RFT collection. + /// @param _tokenPrefix token prefix for created RFT collection. function createAndSetRFTCollection(string calldata _name, string calldata _description, string calldata _tokenPrefix) public onlyOwner { require( rftCollection == address(0), @@ -63,11 +89,25 @@ emit RFTCollectionSet(rftCollection); } + /// Allow or disallow NFT collection tokens from being fractionalized by this contract. + /// @dev Can only be called by contract owner. + /// @param collection NFT token address. + /// @param status `true` to allow and `false` to disallow NFT token. function setNftCollectionIsAllowed(address collection, bool status) public onlyOwner { nftCollectionAllowList[collection] = status; emit AllowListSet(collection, status); } + /// Fractionilize NFT token. + /// @dev Takes NFT token from `msg.sender` and transfers RFT token to `msg.sender` + /// instead. Creates new RFT token if provided NFT token never was fractionalized + /// by this contract or existing RFT token if it was. + /// Throws if RFT collection isn't configured for this contract. + /// Throws if fractionalization of provided NFT token is not allowed + /// Throws if `msg.sender` is not owner of provided NFT token + /// @param _collection NFT collection address + /// @param _token id of NFT token to be fractionalized + /// @param _pieces number of pieces new RFT token would have function nft2rft(address _collection, uint256 _token, uint128 _pieces) public { require( rftCollection != address(0), @@ -109,6 +149,15 @@ emit Fractionalized(_collection, _token, rftTokenAddress, _pieces); } + /// Defrationalize NFT token. + /// @dev Takes RFT token from `msg.sender` and transfers corresponding NFT token + /// to `msg.sender` instead. + /// Throws if RFT collection isn't configured for this contract. + /// Throws if provided RFT token is no from configured RFT collection. + /// Throws if RFT token was not created by this contract. + /// Throws if `msg.sender` isn't owner of all RFT token pieces. + /// @param _collection RFT collection address + /// @param _token id of RFT token function rft2nft(address _collection, uint256 _token) public { require( rftCollection != address(0), --- a/tests/src/eth/fractionalizer/fractionalizer.test.ts +++ b/tests/src/eth/fractionalizer/fractionalizer.test.ts @@ -19,17 +19,15 @@ import {ApiPromise} from '@polkadot/api'; import {evmToAddress} from '@polkadot/util-crypto'; import {readFile} from 'fs/promises'; -import {submitTransactionAsync} from '../../substrate/substrate-api'; -import {UNIQUE} from '../../util/helpers'; +import {executeTransaction, submitTransactionAsync} from '../../substrate/substrate-api'; +import {getCreateCollectionResult, getCreateItemResult, UNIQUE} from '../../util/helpers'; import {collectionIdToAddress, CompiledContract, createEthAccountWithBalance, createNonfungibleCollection, createRefungibleCollection, GAS_ARGS, itWeb3, tokenIdFromAddress, uniqueNFT, uniqueRefungible, uniqueRefungibleToken} from '../util/helpers'; import {Contract} from 'web3-eth-contract'; import * as solc from 'solc'; import chai from 'chai'; -import chaiAsPromised from 'chai-as-promised'; import chaiLike from 'chai-like'; import {IKeyringPair} from '@polkadot/types/types'; -chai.use(chaiAsPromised); chai.use(chaiLike); const expect = chai.expect; let fractionalizer: CompiledContract; @@ -93,9 +91,8 @@ async function initFractionalizer(api: ApiPromise, web3: Web3, privateKeyWrapper: (account: string) => IKeyringPair, owner: string) { const fractionalizer = await deployFractionalizer(web3, owner); - const tx = api.tx.balances.transfer(evmToAddress(fractionalizer.options.address), 10n * UNIQUE); - const alice = privateKeyWrapper('//Alice'); - await submitTransactionAsync(alice, tx); + const amount = 10n * UNIQUE; + await web3.eth.sendTransaction({from: owner, to: fractionalizer.options.address, value: `${amount}`, ...GAS_ARGS}); const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send(); const rftCollectionAddress = result.events.RFTCollectionSet.returnValues._collection; return {fractionalizer, rftCollectionAddress}; @@ -151,8 +148,7 @@ itWeb3('Set Allowlist', async ({api, web3, privateKeyWrapper}) => { const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); - const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner); - + const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner); const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner); const result1 = await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send({from: owner}); expect(result1.events).to.be.like({ @@ -238,7 +234,7 @@ await fractionalizer.methods.setRFTCollection(collectionIdAddress).send(); await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call()) - .to.eventually.be.rejectedWith(/RFT collection is already set$/g); + .to.be.rejectedWith(/RFT collection is already set$/g); }); itWeb3('call setRFTCollection with NFT collection', async ({api, web3, privateKeyWrapper}) => { @@ -250,7 +246,7 @@ await nftContract.methods.addCollectionAdmin(fractionalizer.options.address).send(); await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call()) - .to.eventually.be.rejectedWith(/Wrong collection type. Collection is not refungible.$/g); + .to.be.rejectedWith(/Wrong collection type. Collection is not refungible.$/g); }); itWeb3('call setRFTCollection while not collection admin', async ({api, web3, privateKeyWrapper}) => { @@ -259,7 +255,7 @@ const {collectionIdAddress} = await createRefungibleCollection(api, web3, owner); await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call()) - .to.eventually.be.rejectedWith(/Fractionalizer contract should be an admin of the collection$/g); + .to.be.rejectedWith(/Fractionalizer contract should be an admin of the collection$/g); }); itWeb3('call setRFTCollection after createAndSetRFTCollection', async ({api, web3, privateKeyWrapper}) => { @@ -273,7 +269,7 @@ const collectionIdAddress = result.events.RFTCollectionSet.returnValues._collection; await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call()) - .to.eventually.be.rejectedWith(/RFT collection is already set$/g); + .to.be.rejectedWith(/RFT collection is already set$/g); }); itWeb3('call nft2rft without setting RFT collection for contract', async ({api, web3, privateKeyWrapper}) => { @@ -287,7 +283,7 @@ const fractionalizer = await deployFractionalizer(web3, owner); await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call()) - .to.eventually.be.rejectedWith(/RFT collection is not set$/g); + .to.be.rejectedWith(/RFT collection is not set$/g); }); itWeb3('call nft2rft while not owner of NFT token', async ({api, web3, privateKeyWrapper}) => { @@ -305,7 +301,7 @@ await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send(); await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call()) - .to.eventually.be.rejectedWith(/Only token owner could fractionalize it$/g); + .to.be.rejectedWith(/Only token owner could fractionalize it$/g); }); itWeb3('call nft2rft while not in list of allowed accounts', async ({api, web3, privateKeyWrapper}) => { @@ -320,7 +316,7 @@ await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send(); await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call()) - .to.eventually.be.rejectedWith(/Fractionalization of this collection is not allowed by admin$/g); + .to.be.rejectedWith(/Fractionalization of this collection is not allowed by admin$/g); }); itWeb3('call nft2rft while fractionalizer doesnt have approval for nft token', async ({api, web3, privateKeyWrapper}) => { @@ -335,7 +331,7 @@ await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send(); await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call()) - .to.eventually.be.rejectedWith(/ApprovedValueTooLow$/g); + .to.be.rejectedWith(/ApprovedValueTooLow$/g); }); itWeb3('call rft2nft without setting RFT collection for contract', async ({api, web3, privateKeyWrapper}) => { @@ -348,7 +344,7 @@ await refungibleContract.methods.mint(owner, rftTokenId).send(); await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, rftTokenId).call()) - .to.eventually.be.rejectedWith(/RFT collection is not set$/g); + .to.be.rejectedWith(/RFT collection is not set$/g); }); itWeb3('call rft2nft for RFT token that is not from configured RFT collection', async ({api, web3, privateKeyWrapper}) => { @@ -361,7 +357,7 @@ await refungibleContract.methods.mint(owner, rftTokenId).send(); await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, rftTokenId).call()) - .to.eventually.be.rejectedWith(/Wrong RFT collection$/g); + .to.be.rejectedWith(/Wrong RFT collection$/g); }); itWeb3('call rft2nft for RFT token that was not minted by fractionalizer contract', async ({api, web3, privateKeyWrapper}) => { @@ -378,7 +374,7 @@ await refungibleContract.methods.mint(owner, rftTokenId).send(); await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, rftTokenId).call()) - .to.eventually.be.rejectedWith(/No corresponding NFT token found$/g); + .to.be.rejectedWith(/No corresponding NFT token found$/g); }); itWeb3('call rft2nft without owning all RFT pieces', async ({api, web3, privateKeyWrapper}) => { @@ -393,6 +389,82 @@ await refungibleTokenContract.methods.transfer(receiver, 50).send(); await refungibleTokenContract.methods.approve(fractionalizer.options.address, 50).send(); await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, tokenId).call()) - .to.eventually.be.rejectedWith(/Not all pieces are owned by the caller$/g); + .to.be.rejectedWith(/Not all pieces are owned by the caller$/g); + }); + + itWeb3('send QTZ/UNQ to contract from non owner', async ({api, web3, privateKeyWrapper}) => { + const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + const payer = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + + const fractionalizer = await deployFractionalizer(web3, owner); + const amount = 10n * UNIQUE; + await expect(web3.eth.sendTransaction({from: payer, to: fractionalizer.options.address, value: `${amount}`, ...GAS_ARGS})).to.be.rejected; + }); + + itWeb3('fractionalize NFT with NFT transfers disallowed', async ({api, web3, privateKeyWrapper}) => { + const alice = privateKeyWrapper('//Alice'); + let collectionId; + { + const tx = api.tx.unique.createCollectionEx({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'}); + const events = await submitTransactionAsync(alice, tx); + const result = getCreateCollectionResult(events); + collectionId = result.collectionId; + } + const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + let nftTokenId; + { + const createData = {nft: {}}; + const tx = api.tx.unique.createItem(collectionId, {Ethereum: owner}, createData as any); + const events = await executeTransaction(api, alice, tx); + const result = getCreateItemResult(events); + nftTokenId = result.itemId; + } + { + const tx = api.tx.unique.setTransfersEnabledFlag(collectionId, false); + await executeTransaction(api, alice, tx); + } + const nftCollectionAddress = collectionIdToAddress(collectionId); + const {fractionalizer} = await initFractionalizer(api, web3, privateKeyWrapper, owner); + await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send(); + + const nftContract = uniqueNFT(web3, nftCollectionAddress, owner); + await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send(); + await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100).call()) + .to.be.rejectedWith(/TransferNotAllowed$/g); + }); + + itWeb3('fractionalize NFT with RFT transfers disallowed', async ({api, web3, privateKeyWrapper}) => { + const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper); + const alice = privateKeyWrapper('//Alice'); + + let collectionId; + { + const tx = api.tx.unique.createCollectionEx({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'ReFungible'}); + const events = await submitTransactionAsync(alice, tx); + const result = getCreateCollectionResult(events); + collectionId = result.collectionId; + } + const rftCollectionAddress = collectionIdToAddress(collectionId); + const fractionalizer = await deployFractionalizer(web3, owner); + { + const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, {Ethereum: fractionalizer.options.address}); + await submitTransactionAsync(alice, changeAdminTx); + } + await fractionalizer.methods.setRFTCollection(rftCollectionAddress).send(); + { + const tx = api.tx.unique.setTransfersEnabledFlag(collectionId, false); + await executeTransaction(api, alice, tx); + } + + const {collectionIdAddress: nftCollectionAddress} = await createNonfungibleCollection(api, web3, owner); + const nftContract = uniqueNFT(web3, nftCollectionAddress, owner); + const nftTokenId = await nftContract.methods.nextTokenId().call(); + await nftContract.methods.mint(owner, nftTokenId).send(); + + await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send(); + await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send(); + + await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftTokenId, 100n).call()) + .to.be.rejectedWith(/TransferNotAllowed$/g); }); }); \ No newline at end of file -- gitstuff