git.delta.rocks / unique-network / refs/commits / 118b8d5eaba9

difftreelog

feature: add `createERC721MetadataCompatibleRFTCollection` method

Trubnikov Sergey2022-07-25parent: #e516992.patch.diff
in: master

4 files changed

modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -237,6 +237,33 @@
 		Ok(address)
 	}
 
+	#[weight(<SelfWeightOf<T>>::create_collection())]
+	#[solidity(rename_selector = "createERC721MetadataCompatibleRFTCollection")]
+	fn create_refungible_collection_with_properties(
+		&mut self,
+		caller: caller,
+		name: string,
+		description: string,
+		token_prefix: string,
+		base_uri: string,
+	) -> Result<address> {
+		let (caller, name, description, token_prefix, base_uri_value) =
+			convert_data::<T>(caller, name, description, token_prefix, base_uri)?;
+		let data = make_data::<T>(
+			name,
+			CollectionMode::NFT,
+			description,
+			token_prefix,
+			base_uri_value,
+			true,
+		)?;
+		let collection_id = <pallet_refungible::Pallet<T>>::init_collection(caller.clone(), data)
+			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
+
+		let address = pallet_common::eth::collection_id_to_address(collection_id);
+		Ok(address)
+	}
+
 	/// Check if a collection exists
 	/// @param collection_address Address of the collection in question
 	/// @return bool Does the collection exist?
addedtests/src/eth/api/UniqueRefungible.soldiffbeforeafterboth
--- /dev/null
+++ b/tests/src/eth/api/UniqueRefungible.sol
@@ -0,0 +1,163 @@
+// SPDX-License-Identifier: OTHER
+// This code is automatically generated
+
+pragma solidity >=0.8.0 <0.9.0;
+
+// Common stubs holder
+interface Dummy {
+
+}
+
+interface ERC165 is Dummy {
+	function supportsInterface(bytes4 interfaceID) external view returns (bool);
+}
+
+// Selector: 7d9262e6
+interface Collection is Dummy, ERC165 {
+	// Set collection property.
+	//
+	// @param key Property key.
+	// @param value Propery value.
+	//
+	// Selector: setCollectionProperty(string,bytes) 2f073f66
+	function setCollectionProperty(string memory key, bytes memory value)
+		external;
+
+	// Delete collection property.
+	//
+	// @param key Property key.
+	//
+	// Selector: deleteCollectionProperty(string) 7b7debce
+	function deleteCollectionProperty(string memory key) external;
+
+	// Get collection property.
+	//
+	// @dev Throws error if key not found.
+	//
+	// @param key Property key.
+	// @return bytes The property corresponding to the key.
+	//
+	// Selector: collectionProperty(string) cf24fd6d
+	function collectionProperty(string memory key)
+		external
+		view
+		returns (bytes memory);
+
+	// Set the sponsor of the collection.
+	//
+	// @dev In order for sponsorship to work, it must be confirmed on behalf of the sponsor.
+	//
+	// @param sponsor Address of the sponsor from whose account funds will be debited for operations with the contract.
+	//
+	// Selector: setCollectionSponsor(address) 7623402e
+	function setCollectionSponsor(address sponsor) external;
+
+	// Collection sponsorship confirmation.
+	//
+	// @dev After setting the sponsor for the collection, it must be confirmed with this function.
+	//
+	// Selector: confirmCollectionSponsorship() 3c50e97a
+	function confirmCollectionSponsorship() external;
+
+	// Set limits for the collection.
+	// @dev Throws error if limit not found.
+	// @param limit Name of the limit. Valid names:
+	// 	"accountTokenOwnershipLimit",
+	// 	"sponsoredDataSize",
+	// 	"sponsoredDataRateLimit",
+	// 	"tokenLimit",
+	// 	"sponsorTransferTimeout",
+	// 	"sponsorApproveTimeout"
+	// @param value Value of the limit.
+	//
+	// Selector: setCollectionLimit(string,uint32) 6a3841db
+	function setCollectionLimit(string memory limit, uint32 value) external;
+
+	// Set limits for the collection.
+	// @dev Throws error if limit not found.
+	// @param limit Name of the limit. Valid names:
+	// 	"ownerCanTransfer",
+	// 	"ownerCanDestroy",
+	// 	"transfersEnabled"
+	// @param value Value of the limit.
+	//
+	// Selector: setCollectionLimit(string,bool) 993b7fba
+	function setCollectionLimit(string memory limit, bool value) external;
+
+	// Get contract address.
+	//
+	// Selector: contractAddress() f6b4dfb4
+	function contractAddress() external view returns (address);
+
+	// Add collection admin by substrate address.
+	// @param new_admin Substrate administrator address.
+	//
+	// Selector: addCollectionAdminSubstrate(uint256) 5730062b
+	function addCollectionAdminSubstrate(uint256 newAdmin) external;
+
+	// Remove collection admin by substrate address.
+	// @param admin Substrate administrator address.
+	//
+	// Selector: removeCollectionAdminSubstrate(uint256) 4048fcf9
+	function removeCollectionAdminSubstrate(uint256 admin) external;
+
+	// Add collection admin.
+	// @param new_admin Address of the added administrator.
+	//
+	// Selector: addCollectionAdmin(address) 92e462c7
+	function addCollectionAdmin(address newAdmin) external;
+
+	// Remove collection admin.
+	//
+	// @param new_admin Address of the removed administrator.
+	//
+	// Selector: removeCollectionAdmin(address) fafd7b42
+	function removeCollectionAdmin(address admin) external;
+
+	// Toggle accessibility of collection nesting.
+	//
+	// @param enable If "true" degenerates to nesting: 'Owner' else to nesting: 'Disabled'
+	//
+	// Selector: setCollectionNesting(bool) 112d4586
+	function setCollectionNesting(bool enable) external;
+
+	// Toggle accessibility of collection nesting.
+	//
+	// @param enable If "true" degenerates to nesting: {OwnerRestricted: [1, 2, 3]} else to nesting: 'Disabled'
+	// @param collections Addresses of collections that will be available for nesting.
+	//
+	// Selector: setCollectionNesting(bool,address[]) 64872396
+	function setCollectionNesting(bool enable, address[] memory collections)
+		external;
+
+	// Set the collection access method.
+	// @param mode Access mode
+	// 	0 for Normal
+	// 	1 for AllowList
+	//
+	// Selector: setCollectionAccess(uint8) 41835d4c
+	function setCollectionAccess(uint8 mode) external;
+
+	// Add the user to the allowed list.
+	//
+	// @param user Address of a trusted user.
+	//
+	// Selector: addToCollectionAllowList(address) 67844fe6
+	function addToCollectionAllowList(address user) external;
+
+	// Remove the user from the allowed list.
+	//
+	// @param user Address of a removed user.
+	//
+	// Selector: removeFromCollectionAllowList(address) 85c51acb
+	function removeFromCollectionAllowList(address user) external;
+
+	// Switch permission for minting.
+	//
+	// @param mode Enable if "true".
+	//
+	// Selector: setCollectionMintMode(bool) 00018e84
+	function setCollectionMintMode(bool mode) external;
+}
+
+interface UniqueRefungible is Dummy, ERC165, Collection {}
modifiedtests/src/eth/nonFungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/nonFungible.test.ts
+++ b/tests/src/eth/nonFungible.test.ts
@@ -72,7 +72,7 @@
   });
 });
 
-describe('Check ERC721 token URI', () => {
+describe('Check ERC721 token URI for NFT', () => {
   itWeb3('Empty tokenURI', async ({web3, api, privateKeyWrapper}) => {
     const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
     const helper = evmCollectionHelpers(web3, owner);
modifiedtests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth
before · tests/src/eth/reFungibleToken.test.ts
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 {approve, createCollection, createRefungibleToken, transfer, transferFrom, UNIQUE} from '../util/helpers';18import {createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, tokenIdToAddress, transferBalanceToEth} from './util/helpers';19import reFungibleTokenAbi from './reFungibleTokenAbi.json';2021import chai from 'chai';22import chaiAsPromised from 'chai-as-promised';23chai.use(chaiAsPromised);24const expect = chai.expect;2526describe('Refungible token: Information getting', () => {27  itWeb3('totalSupply', async ({api, web3, privateKeyWrapper}) => {28    const alice = privateKeyWrapper('//Alice');2930    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;3132    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);3334    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: caller})).itemId;3536    const address = tokenIdToAddress(collectionId, tokenId);37    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: caller, ...GAS_ARGS});38    const totalSupply = await contract.methods.totalSupply().call();3940    expect(totalSupply).to.equal('200');41  });4243  itWeb3('balanceOf', async ({api, web3, privateKeyWrapper}) => {44    const alice = privateKeyWrapper('//Alice');4546    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;4748    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);4950    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: caller})).itemId;5152    const address = tokenIdToAddress(collectionId, tokenId);53    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: caller, ...GAS_ARGS});54    const balance = await contract.methods.balanceOf(caller).call();5556    expect(balance).to.equal('200');57  });5859  itWeb3('decimals', async ({api, web3, privateKeyWrapper}) => {60    const alice = privateKeyWrapper('//Alice');6162    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;6364    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);6566    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: caller})).itemId;6768    const address = tokenIdToAddress(collectionId, tokenId);69    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: caller, ...GAS_ARGS});70    const decimals = await contract.methods.decimals().call();7172    expect(decimals).to.equal('0');73  });74});7576describe('Refungible: Plain calls', () => {77  itWeb3('Can perform approve()', async ({web3, api, privateKeyWrapper}) => {78    const alice = privateKeyWrapper('//Alice');7980    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;8182    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);8384    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;8586    const address = tokenIdToAddress(collectionId, tokenId);8788    const spender = createEthAccount(web3);8990    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});9192    {93      const result = await contract.methods.approve(spender, 100).send({from: owner});94      const events = normalizeEvents(result.events);9596      expect(events).to.be.deep.equal([97        {98          address,99          event: 'Approval',100          args: {101            owner,102            spender,103            value: '100',104          },105        },106      ]);107    }108109    {110      const allowance = await contract.methods.allowance(owner, spender).call();111      expect(+allowance).to.equal(100);112    }113  });114115  itWeb3('Can perform transferFrom()', async ({web3, api, privateKeyWrapper}) => {116    const alice = privateKeyWrapper('//Alice');117118    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;119120    const owner = createEthAccount(web3);121    await transferBalanceToEth(api, alice, owner);122123    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;124125    const spender = createEthAccount(web3);126    await transferBalanceToEth(api, alice, spender);127128    const receiver = createEthAccount(web3);129130    const address = tokenIdToAddress(collectionId, tokenId);131    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});132133    await contract.methods.approve(spender, 100).send();134135    {136      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});137      const events = normalizeEvents(result.events);138      expect(events).to.be.deep.equal([139        {140          address,141          event: 'Transfer',142          args: {143            from: owner,144            to: receiver,145            value: '49',146          },147        },148        {149          address,150          event: 'Approval',151          args: {152            owner,153            spender,154            value: '51',155          },156        },157      ]);158    }159160    {161      const balance = await contract.methods.balanceOf(receiver).call();162      expect(+balance).to.equal(49);163    }164165    {166      const balance = await contract.methods.balanceOf(owner).call();167      expect(+balance).to.equal(151);168    }169  });170171  itWeb3('Can perform transfer()', async ({web3, api, privateKeyWrapper}) => {172    const alice = privateKeyWrapper('//Alice');173174    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;175176    const owner = createEthAccount(web3);177    await transferBalanceToEth(api, alice, owner);178179    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;180181    const receiver = createEthAccount(web3);182    await transferBalanceToEth(api, alice, receiver);183184    const address = tokenIdToAddress(collectionId, tokenId);185    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});186187    {188      const result = await contract.methods.transfer(receiver, 50).send({from: owner});189      const events = normalizeEvents(result.events);190      expect(events).to.be.deep.equal([191        {192          address,193          event: 'Transfer',194          args: {195            from: owner,196            to: receiver,197            value: '50',198          },199        },200      ]);201    }202203    {204      const balance = await contract.methods.balanceOf(owner).call();205      expect(+balance).to.equal(150);206    }207208    {209      const balance = await contract.methods.balanceOf(receiver).call();210      expect(+balance).to.equal(50);211    }212  });213214  itWeb3('Can perform repartition()', async ({web3, api, privateKeyWrapper}) => {215    const alice = privateKeyWrapper('//Alice');216217    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;218219    const owner = createEthAccount(web3);220    await transferBalanceToEth(api, alice, owner);221222    const receiver = createEthAccount(web3);223    await transferBalanceToEth(api, alice, receiver);224225    const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;226227    const address = tokenIdToAddress(collectionId, tokenId);228    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});229230    await contract.methods.repartition(200).send({from: owner});231    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(200);232    await contract.methods.transfer(receiver, 110).send({from: owner});233    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(90);234    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(110);235    236    await expect(contract.methods.repartition(80).send({from: owner})).to.eventually.be.rejected;237238    await contract.methods.transfer(receiver, 90).send({from: owner});239    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);240    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);241242    await contract.methods.repartition(150).send({from: receiver});243    await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected;244    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);245  });246247  itWeb3('Can repartition with increased amount', async ({web3, api, privateKeyWrapper}) => {248    const alice = privateKeyWrapper('//Alice');249250    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;251252    const owner = createEthAccount(web3);253    await transferBalanceToEth(api, alice, owner);254255    const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;256257    const address = tokenIdToAddress(collectionId, tokenId);258    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});259260    const result = await contract.methods.repartition(200).send();261    const events = normalizeEvents(result.events);262263    expect(events).to.deep.equal([264      {265        address,266        event: 'Transfer',267        args: {268          from: '0x0000000000000000000000000000000000000000',269          to: owner,270          value: '100',271        },272      },273    ]);274  });275276  itWeb3('Can repartition with decreased amount', async ({web3, api, privateKeyWrapper}) => {277    const alice = privateKeyWrapper('//Alice');278279    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;280281    const owner = createEthAccount(web3);282    await transferBalanceToEth(api, alice, owner);283284    const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;285286    const address = tokenIdToAddress(collectionId, tokenId);287    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});288289    const result = await contract.methods.repartition(50).send();290    const events = normalizeEvents(result.events);291    expect(events).to.deep.equal([292      {293        address,294        event: 'Transfer',295        args: {296          from: owner,297          to: '0x0000000000000000000000000000000000000000',298          value: '50',299        },300      },301    ]);302  });303});304305describe('Refungible: Fees', () => {306  itWeb3('approve() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {307    const alice = privateKeyWrapper('//Alice');308309    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;310311    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);312    const spender = createEthAccount(web3);313314    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;315316    const address = tokenIdToAddress(collectionId, tokenId);317    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});318319    const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, 100).send({from: owner}));320    expect(cost < BigInt(0.2 * Number(UNIQUE)));321  });322323  itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {324    const alice = privateKeyWrapper('//Alice');325326    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;327328    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);329    const spender = await createEthAccountWithBalance(api, web3, privateKeyWrapper);330331    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;332333    const address = tokenIdToAddress(collectionId, tokenId);334    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});335336    await contract.methods.approve(spender, 100).send({from: owner});337338    const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));339    expect(cost < BigInt(0.2 * Number(UNIQUE)));340  });341342  itWeb3('transfer() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {343    const alice = privateKeyWrapper('//Alice');344345    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;346347    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);348    const receiver = createEthAccount(web3);349350    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;351352    const address = tokenIdToAddress(collectionId, tokenId);353    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});354355    const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));356    expect(cost < BigInt(0.2 * Number(UNIQUE)));357  });358});359360describe('Refungible: Substrate calls', () => {361  itWeb3('Events emitted for approve()', async ({web3, api, privateKeyWrapper}) => {362    const alice = privateKeyWrapper('//Alice');363364    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;365366    const receiver = createEthAccount(web3);367368    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n)).itemId;369370    const address = tokenIdToAddress(collectionId, tokenId);371    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address);372373    const events = await recordEvents(contract, async () => {374      expect(await approve(api, collectionId, tokenId, alice, {Ethereum: receiver}, 100n)).to.be.true;375    });376377    expect(events).to.be.deep.equal([378      {379        address,380        event: 'Approval',381        args: {382          owner: subToEth(alice.address),383          spender: receiver,384          value: '100',385        },386      },387    ]);388  });389390  itWeb3('Events emitted for transferFrom()', async ({web3, api, privateKeyWrapper}) => {391    const alice = privateKeyWrapper('//Alice');392393    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;394    const bob = privateKeyWrapper('//Bob');395396    const receiver = createEthAccount(web3);397398    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n)).itemId;399    expect(await approve(api, collectionId, tokenId, alice, bob.address, 100n)).to.be.true;400401    const address = tokenIdToAddress(collectionId, tokenId);402    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address);403404    const events = await recordEvents(contract, async () => {405      expect(await transferFrom(api, collectionId, tokenId, bob, alice, {Ethereum: receiver},  51n)).to.be.true;406    });407408    expect(events).to.be.deep.equal([409      {410        address,411        event: 'Transfer',412        args: {413          from: subToEth(alice.address),414          to: receiver,415          value: '51',416        },417      },418      {419        address,420        event: 'Approval',421        args: {422          owner: subToEth(alice.address),423          spender: subToEth(bob.address),424          value: '49',425        },426      },427    ]);428  });429430  itWeb3('Events emitted for transfer()', async ({web3, api, privateKeyWrapper}) => {431    const alice = privateKeyWrapper('//Alice');432433    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;434435    const receiver = createEthAccount(web3);436437    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n)).itemId;438439    const address = tokenIdToAddress(collectionId, tokenId);440    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address);441442    const events = await recordEvents(contract, async () => {443      expect(await transfer(api, collectionId, tokenId, alice, {Ethereum: receiver},  51n)).to.be.true;444    });445446    expect(events).to.be.deep.equal([447      {448        address,449        event: 'Transfer',450        args: {451          from: subToEth(alice.address),452          to: receiver,453          value: '51',454        },455      },456    ]);457  });458});
after · tests/src/eth/reFungibleToken.test.ts
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 {approve, createCollection, createRefungibleToken, transfer, transferFrom, UNIQUE} from '../util/helpers';18import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, evmCollection, evmCollectionHelpers, GAS_ARGS, getCollectionAddressFromResult, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, tokenIdToAddress, transferBalanceToEth} from './util/helpers';19import reFungibleTokenAbi from './reFungibleTokenAbi.json';2021import chai from 'chai';22import chaiAsPromised from 'chai-as-promised';23chai.use(chaiAsPromised);24const expect = chai.expect;2526describe('Refungible token: Information getting', () => {27  itWeb3('totalSupply', async ({api, web3, privateKeyWrapper}) => {28    const alice = privateKeyWrapper('//Alice');2930    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;3132    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);3334    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: caller})).itemId;3536    const address = tokenIdToAddress(collectionId, tokenId);37    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: caller, ...GAS_ARGS});38    const totalSupply = await contract.methods.totalSupply().call();3940    expect(totalSupply).to.equal('200');41  });4243  itWeb3('balanceOf', async ({api, web3, privateKeyWrapper}) => {44    const alice = privateKeyWrapper('//Alice');4546    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;4748    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);4950    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: caller})).itemId;5152    const address = tokenIdToAddress(collectionId, tokenId);53    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: caller, ...GAS_ARGS});54    const balance = await contract.methods.balanceOf(caller).call();5556    expect(balance).to.equal('200');57  });5859  itWeb3('decimals', async ({api, web3, privateKeyWrapper}) => {60    const alice = privateKeyWrapper('//Alice');6162    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;6364    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);6566    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: caller})).itemId;6768    const address = tokenIdToAddress(collectionId, tokenId);69    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: caller, ...GAS_ARGS});70    const decimals = await contract.methods.decimals().call();7172    expect(decimals).to.equal('0');73  });74});7576// FIXME: Need erc721 for ReFubgible.77describe.skip('Check ERC721 token URI for ReFungible', () => {78  itWeb3('Empty tokenURI', async ({web3, api, privateKeyWrapper}) => {79    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);80    const helper = evmCollectionHelpers(web3, owner);81    let result = await helper.methods.createERC721MetadataCompatibleCollection('Mint collection', '1', '1', '').send();82    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);83    const receiver = createEthAccount(web3);84    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});85    86    const nextTokenId = await contract.methods.nextTokenId().call();87    expect(nextTokenId).to.be.equal('1');88    result = await contract.methods.mint(89      receiver,90      nextTokenId,91    ).send();9293    const events = normalizeEvents(result.events);94    const address = collectionIdToAddress(collectionId);9596    expect(events).to.be.deep.equal([97      {98        address,99        event: 'Transfer',100        args: {101          from: '0x0000000000000000000000000000000000000000',102          to: receiver,103          tokenId: nextTokenId,104        },105      },106    ]);107108    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');109  });110111  itWeb3('TokenURI from url', async ({web3, api, privateKeyWrapper}) => {112    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);113    const helper = evmCollectionHelpers(web3, owner);114    let result = await helper.methods.createERC721MetadataCompatibleCollection('Mint collection', '1', '1', 'BaseURI_').send();115    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);116    const receiver = createEthAccount(web3);117    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});118    119    const nextTokenId = await contract.methods.nextTokenId().call();120    expect(nextTokenId).to.be.equal('1');121    result = await contract.methods.mint(122      receiver,123      nextTokenId,124    ).send();125    126    // Set URL127    await contract.methods.setProperty(nextTokenId, 'url', Buffer.from('Token URI')).send();128      129    const events = normalizeEvents(result.events);130    const address = collectionIdToAddress(collectionId);131132    expect(events).to.be.deep.equal([133      {134        address,135        event: 'Transfer',136        args: {137          from: '0x0000000000000000000000000000000000000000',138          to: receiver,139          tokenId: nextTokenId,140        },141      },142    ]);143144    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');145  });146147  itWeb3('TokenURI from baseURI + tokenId', async ({web3, api, privateKeyWrapper}) => {148    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);149    const helper = evmCollectionHelpers(web3, owner);150    let result = await helper.methods.createERC721MetadataCompatibleCollection('Mint collection', '1', '1', 'BaseURI_').send();151    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);152    const receiver = createEthAccount(web3);153    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});154    155    const nextTokenId = await contract.methods.nextTokenId().call();156    expect(nextTokenId).to.be.equal('1');157    result = await contract.methods.mint(158      receiver,159      nextTokenId,160    ).send();161          162    const events = normalizeEvents(result.events);163    const address = collectionIdToAddress(collectionId);164165    expect(events).to.be.deep.equal([166      {167        address,168        event: 'Transfer',169        args: {170          from: '0x0000000000000000000000000000000000000000',171          to: receiver,172          tokenId: nextTokenId,173        },174      },175    ]);176177    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + nextTokenId);178  });179180  itWeb3('TokenURI from baseURI + suffix', async ({web3, api, privateKeyWrapper}) => {181    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);182    const helper = evmCollectionHelpers(web3, owner);183    let result = await helper.methods.createERC721MetadataCompatibleCollection('Mint collection', '1', '1', 'BaseURI_').send();184    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);185    const receiver = createEthAccount(web3);186    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});187    188    const nextTokenId = await contract.methods.nextTokenId().call();189    expect(nextTokenId).to.be.equal('1');190    result = await contract.methods.mint(191      receiver,192      nextTokenId,193    ).send();194          195    // Set suffix196    const suffix = '/some/suffix';197    await contract.methods.setProperty(nextTokenId, 'suffix', Buffer.from(suffix)).send();198199    const events = normalizeEvents(result.events);200    const address = collectionIdToAddress(collectionId);201202    expect(events).to.be.deep.equal([203      {204        address,205        event: 'Transfer',206        args: {207          from: '0x0000000000000000000000000000000000000000',208          to: receiver,209          tokenId: nextTokenId,210        },211      },212    ]);213214    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);215  });216});217218describe('Refungible: Plain calls', () => {219  itWeb3('Can perform approve()', async ({web3, api, privateKeyWrapper}) => {220    const alice = privateKeyWrapper('//Alice');221222    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;223224    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);225226    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;227228    const address = tokenIdToAddress(collectionId, tokenId);229230    const spender = createEthAccount(web3);231232    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});233234    {235      const result = await contract.methods.approve(spender, 100).send({from: owner});236      const events = normalizeEvents(result.events);237238      expect(events).to.be.deep.equal([239        {240          address,241          event: 'Approval',242          args: {243            owner,244            spender,245            value: '100',246          },247        },248      ]);249    }250251    {252      const allowance = await contract.methods.allowance(owner, spender).call();253      expect(+allowance).to.equal(100);254    }255  });256257  itWeb3('Can perform transferFrom()', async ({web3, api, privateKeyWrapper}) => {258    const alice = privateKeyWrapper('//Alice');259260    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;261262    const owner = createEthAccount(web3);263    await transferBalanceToEth(api, alice, owner);264265    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;266267    const spender = createEthAccount(web3);268    await transferBalanceToEth(api, alice, spender);269270    const receiver = createEthAccount(web3);271272    const address = tokenIdToAddress(collectionId, tokenId);273    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});274275    await contract.methods.approve(spender, 100).send();276277    {278      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});279      const events = normalizeEvents(result.events);280      expect(events).to.be.deep.equal([281        {282          address,283          event: 'Transfer',284          args: {285            from: owner,286            to: receiver,287            value: '49',288          },289        },290        {291          address,292          event: 'Approval',293          args: {294            owner,295            spender,296            value: '51',297          },298        },299      ]);300    }301302    {303      const balance = await contract.methods.balanceOf(receiver).call();304      expect(+balance).to.equal(49);305    }306307    {308      const balance = await contract.methods.balanceOf(owner).call();309      expect(+balance).to.equal(151);310    }311  });312313  itWeb3('Can perform transfer()', async ({web3, api, privateKeyWrapper}) => {314    const alice = privateKeyWrapper('//Alice');315316    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;317318    const owner = createEthAccount(web3);319    await transferBalanceToEth(api, alice, owner);320321    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;322323    const receiver = createEthAccount(web3);324    await transferBalanceToEth(api, alice, receiver);325326    const address = tokenIdToAddress(collectionId, tokenId);327    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});328329    {330      const result = await contract.methods.transfer(receiver, 50).send({from: owner});331      const events = normalizeEvents(result.events);332      expect(events).to.be.deep.equal([333        {334          address,335          event: 'Transfer',336          args: {337            from: owner,338            to: receiver,339            value: '50',340          },341        },342      ]);343    }344345    {346      const balance = await contract.methods.balanceOf(owner).call();347      expect(+balance).to.equal(150);348    }349350    {351      const balance = await contract.methods.balanceOf(receiver).call();352      expect(+balance).to.equal(50);353    }354  });355356  itWeb3('Can perform repartition()', async ({web3, api, privateKeyWrapper}) => {357    const alice = privateKeyWrapper('//Alice');358359    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;360361    const owner = createEthAccount(web3);362    await transferBalanceToEth(api, alice, owner);363364    const receiver = createEthAccount(web3);365    await transferBalanceToEth(api, alice, receiver);366367    const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;368369    const address = tokenIdToAddress(collectionId, tokenId);370    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});371372    await contract.methods.repartition(200).send({from: owner});373    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(200);374    await contract.methods.transfer(receiver, 110).send({from: owner});375    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(90);376    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(110);377    378    await expect(contract.methods.repartition(80).send({from: owner})).to.eventually.be.rejected;379380    await contract.methods.transfer(receiver, 90).send({from: owner});381    expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);382    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);383384    await contract.methods.repartition(150).send({from: receiver});385    await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected;386    expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);387  });388389  itWeb3('Can repartition with increased amount', async ({web3, api, privateKeyWrapper}) => {390    const alice = privateKeyWrapper('//Alice');391392    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;393394    const owner = createEthAccount(web3);395    await transferBalanceToEth(api, alice, owner);396397    const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;398399    const address = tokenIdToAddress(collectionId, tokenId);400    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});401402    const result = await contract.methods.repartition(200).send();403    const events = normalizeEvents(result.events);404405    expect(events).to.deep.equal([406      {407        address,408        event: 'Transfer',409        args: {410          from: '0x0000000000000000000000000000000000000000',411          to: owner,412          value: '100',413        },414      },415    ]);416  });417418  itWeb3('Can repartition with decreased amount', async ({web3, api, privateKeyWrapper}) => {419    const alice = privateKeyWrapper('//Alice');420421    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;422423    const owner = createEthAccount(web3);424    await transferBalanceToEth(api, alice, owner);425426    const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;427428    const address = tokenIdToAddress(collectionId, tokenId);429    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});430431    const result = await contract.methods.repartition(50).send();432    const events = normalizeEvents(result.events);433    expect(events).to.deep.equal([434      {435        address,436        event: 'Transfer',437        args: {438          from: owner,439          to: '0x0000000000000000000000000000000000000000',440          value: '50',441        },442      },443    ]);444  });445});446447describe('Refungible: Fees', () => {448  itWeb3('approve() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {449    const alice = privateKeyWrapper('//Alice');450451    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;452453    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);454    const spender = createEthAccount(web3);455456    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;457458    const address = tokenIdToAddress(collectionId, tokenId);459    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});460461    const cost = await recordEthFee(api, owner, () => contract.methods.approve(spender, 100).send({from: owner}));462    expect(cost < BigInt(0.2 * Number(UNIQUE)));463  });464465  itWeb3('transferFrom() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {466    const alice = privateKeyWrapper('//Alice');467468    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;469470    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);471    const spender = await createEthAccountWithBalance(api, web3, privateKeyWrapper);472473    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;474475    const address = tokenIdToAddress(collectionId, tokenId);476    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});477478    await contract.methods.approve(spender, 100).send({from: owner});479480    const cost = await recordEthFee(api, spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));481    expect(cost < BigInt(0.2 * Number(UNIQUE)));482  });483484  itWeb3('transfer() call fee is less than 0.2UNQ', async ({web3, api, privateKeyWrapper}) => {485    const alice = privateKeyWrapper('//Alice');486487    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;488489    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);490    const receiver = createEthAccount(web3);491492    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: owner})).itemId;493494    const address = tokenIdToAddress(collectionId, tokenId);495    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});496497    const cost = await recordEthFee(api, owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));498    expect(cost < BigInt(0.2 * Number(UNIQUE)));499  });500});501502describe('Refungible: Substrate calls', () => {503  itWeb3('Events emitted for approve()', async ({web3, api, privateKeyWrapper}) => {504    const alice = privateKeyWrapper('//Alice');505506    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;507508    const receiver = createEthAccount(web3);509510    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n)).itemId;511512    const address = tokenIdToAddress(collectionId, tokenId);513    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address);514515    const events = await recordEvents(contract, async () => {516      expect(await approve(api, collectionId, tokenId, alice, {Ethereum: receiver}, 100n)).to.be.true;517    });518519    expect(events).to.be.deep.equal([520      {521        address,522        event: 'Approval',523        args: {524          owner: subToEth(alice.address),525          spender: receiver,526          value: '100',527        },528      },529    ]);530  });531532  itWeb3('Events emitted for transferFrom()', async ({web3, api, privateKeyWrapper}) => {533    const alice = privateKeyWrapper('//Alice');534535    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;536    const bob = privateKeyWrapper('//Bob');537538    const receiver = createEthAccount(web3);539540    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n)).itemId;541    expect(await approve(api, collectionId, tokenId, alice, bob.address, 100n)).to.be.true;542543    const address = tokenIdToAddress(collectionId, tokenId);544    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address);545546    const events = await recordEvents(contract, async () => {547      expect(await transferFrom(api, collectionId, tokenId, bob, alice, {Ethereum: receiver},  51n)).to.be.true;548    });549550    expect(events).to.be.deep.equal([551      {552        address,553        event: 'Transfer',554        args: {555          from: subToEth(alice.address),556          to: receiver,557          value: '51',558        },559      },560      {561        address,562        event: 'Approval',563        args: {564          owner: subToEth(alice.address),565          spender: subToEth(bob.address),566          value: '49',567        },568      },569    ]);570  });571572  itWeb3('Events emitted for transfer()', async ({web3, api, privateKeyWrapper}) => {573    const alice = privateKeyWrapper('//Alice');574575    const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;576577    const receiver = createEthAccount(web3);578579    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n)).itemId;580581    const address = tokenIdToAddress(collectionId, tokenId);582    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address);583584    const events = await recordEvents(contract, async () => {585      expect(await transfer(api, collectionId, tokenId, alice, {Ethereum: receiver},  51n)).to.be.true;586    });587588    expect(events).to.be.deep.equal([589      {590        address,591        event: 'Transfer',592        args: {593          from: subToEth(alice.address),594          to: receiver,595          value: '51',596        },597      },598    ]);599  });600});