difftreelog
feature/mint-for-fungible-token
in: master
5 files changed
pallets/fungible/src/erc.rsdiffbeforeafterboth--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -129,7 +129,23 @@
}
}
-#[solidity_interface(name = ERC20UniqueExtensions)]
+#[solidity_interface(name = "ERC20Mintable")]
+impl<T: Config> FungibleHandle<T> {
+ #[weight(<SelfWeightOf<T>>::create_item())]
+ fn mint(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let to = T::CrossAccountId::from_eth(to);
+ let amount = amount.try_into().map_err(|_| "amount overflow")?;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
+ <Pallet<T>>::create_item(&self, &caller, (to, amount), &budget)
+ .map_err(dispatch_to_evm::<T>)?;
+ Ok(true)
+ }
+}
+
+#[solidity_interface(name = "ERC20UniqueExtensions")]
impl<T: Config> FungibleHandle<T> {
#[weight(<SelfWeightOf<T>>::burn_from())]
fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {
@@ -144,12 +160,29 @@
.map_err(dispatch_to_evm::<T>)?;
Ok(true)
}
+
+ #[weight(<SelfWeightOf<T>>::create_multiple_items_ex(amounts.len() as u32))]
+ fn mint_bulk(&mut self, caller: caller, amounts: Vec<(address, uint256)>) -> Result<bool> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
+ let amounts = amounts
+ .into_iter()
+ .map(|(to, amount)| Ok((T::CrossAccountId::from_eth(to), amount.try_into().map_err(|_| "amount overflow")?)))
+ .collect::<Result<_>>()?;
+
+ <Pallet<T>>::create_multiple_items(&self, &caller, amounts, &budget)
+ .map_err(dispatch_to_evm::<T>)?;
+ Ok(true)
+ }
}
#[solidity_interface(
name = UniqueFungible,
is(
ERC20,
+ ERC20Mintable,
ERC20UniqueExtensions,
Collection(common_mut, CollectionHandle<T>),
)
pallets/fungible/src/stubs/UniqueFungible.soldiffbeforeafterboth--- a/pallets/fungible/src/stubs/UniqueFungible.sol
+++ b/pallets/fungible/src/stubs/UniqueFungible.sol
@@ -3,7 +3,13 @@
pragma solidity >=0.8.0 <0.9.0;
-/// @dev common stubs holder
+// Anonymous struct
+struct Tuple0 {
+ address field_0;
+ uint256 field_1;
+}
+
+// Common stubs holder
contract Dummy {
uint8 dummy;
string stub_error = "this contract is implemented in native";
@@ -21,8 +27,49 @@
}
}
-/// @title A contract that allows you to work with collections.
-/// @dev the ERC-165 identifier for this interface is 0xe54be640
+// Inline
+contract ERC20Events {
+ event Transfer(address indexed from, address indexed to, uint256 value);
+ event Approval(
+ address indexed owner,
+ address indexed spender,
+ uint256 value
+ );
+}
+
+// Selector: 40c10f19
+contract ERC20Mintable is Dummy, ERC165 {
+ // Selector: mint(address,uint256) 40c10f19
+ function mint(address to, uint256 amount) public returns (bool) {
+ require(false, stub_error);
+ to;
+ amount;
+ dummy = 0;
+ return false;
+ }
+}
+
+// Selector: 63034ac5
+contract ERC20UniqueExtensions is Dummy, ERC165 {
+ // Selector: burnFrom(address,uint256) 79cc6790
+ function burnFrom(address from, uint256 amount) public returns (bool) {
+ require(false, stub_error);
+ from;
+ amount;
+ dummy = 0;
+ return false;
+ }
+
+ // Selector: mintBulk((address,uint256)[]) 1acf2d55
+ function mintBulk(Tuple0[] memory amounts) public returns (bool) {
+ require(false, stub_error);
+ amounts;
+ dummy = 0;
+ return false;
+ }
+}
+
+// Selector: 6cf113cd
contract Collection is Dummy, ERC165 {
/// Set collection property.
///
@@ -476,6 +523,7 @@
Dummy,
ERC165,
ERC20,
+ ERC20Mintable,
ERC20UniqueExtensions,
Collection
{}
tests/src/eth/api/UniqueFungible.soldiffbeforeafterboth1// SPDX-License-Identifier: OTHER2// This code is automatically generated34pragma solidity >=0.8.0 <0.9.0;56// Anonymous struct7struct Tuple0 {8 address field_0;9 uint256 field_1;10}1112// Common stubs holder13interface Dummy {1415}1617interface ERC165 is Dummy {18 function supportsInterface(bytes4 interfaceID) external view returns (bool);19}2021// Inline22interface ERC20Events {23 event Transfer(address indexed from, address indexed to, uint256 value);24 event Approval(25 address indexed owner,26 address indexed spender,27 uint256 value28 );29}3031// Selector: 40c10f1932interface ERC20Mintable is Dummy, ERC165 {33 // Selector: mint(address,uint256) 40c10f1934 function mint(address to, uint256 amount) external returns (bool);35}3637// Selector: 63034ac538interface ERC20UniqueExtensions is Dummy, ERC165 {39 // Selector: burnFrom(address,uint256) 79cc679040 function burnFrom(address from, uint256 amount) external returns (bool);4142 // Selector: mintBulk((address,uint256)[]) 1acf2d5543 function mintBulk(Tuple0[] memory amounts) external returns (bool);44}4546// Selector: 6cf113cd47interface Collection is Dummy, ERC165 {48 /// Set collection property.49 ///50 /// @param key Property key.51 /// @param value Propery value.52 /// @dev EVM selector for this function is: 0x2f073f66,53 /// or in textual repr: setCollectionProperty(string,bytes)54 function setCollectionProperty(string memory key, bytes memory value)55 external;5657 /// Delete collection property.58 ///59 /// @param key Property key.60 /// @dev EVM selector for this function is: 0x7b7debce,61 /// or in textual repr: deleteCollectionProperty(string)62 function deleteCollectionProperty(string memory key) external;6364 /// Get collection property.65 ///66 /// @dev Throws error if key not found.67 ///68 /// @param key Property key.69 /// @return bytes The property corresponding to the key.70 /// @dev EVM selector for this function is: 0xcf24fd6d,71 /// or in textual repr: collectionProperty(string)72 function collectionProperty(string memory key)73 external74 view75 returns (bytes memory);7677 /// Set the sponsor of the collection.78 ///79 /// @dev In order for sponsorship to work, it must be confirmed on behalf of the sponsor.80 ///81 /// @param sponsor Address of the sponsor from whose account funds will be debited for operations with the contract.82 /// @dev EVM selector for this function is: 0x7623402e,83 /// or in textual repr: setCollectionSponsor(address)84 function setCollectionSponsor(address sponsor) external;8586 /// Set the substrate sponsor of the collection.87 ///88 /// @dev In order for sponsorship to work, it must be confirmed on behalf of the sponsor.89 ///90 /// @param sponsor Substrate address of the sponsor from whose account funds will be debited for operations with the contract.91 /// @dev EVM selector for this function is: 0xc74d6751,92 /// or in textual repr: setCollectionSponsorSubstrate(uint256)93 function setCollectionSponsorSubstrate(uint256 sponsor) external;9495 /// @dev EVM selector for this function is: 0x058ac185,96 /// or in textual repr: hasCollectionPendingSponsor()97 function hasCollectionPendingSponsor() external view returns (bool);9899 /// Collection sponsorship confirmation.100 ///101 /// @dev After setting the sponsor for the collection, it must be confirmed with this function.102 /// @dev EVM selector for this function is: 0x3c50e97a,103 /// or in textual repr: confirmCollectionSponsorship()104 function confirmCollectionSponsorship() external;105106 /// Remove collection sponsor.107 /// @dev EVM selector for this function is: 0x6e0326a3,108 /// or in textual repr: removeCollectionSponsor()109 function removeCollectionSponsor() external;110111 /// Get current sponsor.112 ///113 /// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.114 /// @dev EVM selector for this function is: 0xb66bbc14,115 /// or in textual repr: getCollectionSponsor()116 function getCollectionSponsor() external view returns (Tuple6 memory);117118 /// Set limits for the collection.119 /// @dev Throws error if limit not found.120 /// @param limit Name of the limit. Valid names:121 /// "accountTokenOwnershipLimit",122 /// "sponsoredDataSize",123 /// "sponsoredDataRateLimit",124 /// "tokenLimit",125 /// "sponsorTransferTimeout",126 /// "sponsorApproveTimeout"127 /// @param value Value of the limit.128 /// @dev EVM selector for this function is: 0x6a3841db,129 /// or in textual repr: setCollectionLimit(string,uint32)130 function setCollectionLimit(string memory limit, uint32 value) external;131132 /// Set limits for the collection.133 /// @dev Throws error if limit not found.134 /// @param limit Name of the limit. Valid names:135 /// "ownerCanTransfer",136 /// "ownerCanDestroy",137 /// "transfersEnabled"138 /// @param value Value of the limit.139 /// @dev EVM selector for this function is: 0x993b7fba,140 /// or in textual repr: setCollectionLimit(string,bool)141 function setCollectionLimit(string memory limit, bool value) external;142143 /// Get contract address.144 /// @dev EVM selector for this function is: 0xf6b4dfb4,145 /// or in textual repr: contractAddress()146 function contractAddress() external view returns (address);147148 /// Add collection admin by substrate address.149 /// @param newAdmin Substrate administrator address.150 /// @dev EVM selector for this function is: 0x5730062b,151 /// or in textual repr: addCollectionAdminSubstrate(uint256)152 function addCollectionAdminSubstrate(uint256 newAdmin) external;153154 /// Remove collection admin by substrate address.155 /// @param admin Substrate administrator address.156 /// @dev EVM selector for this function is: 0x4048fcf9,157 /// or in textual repr: removeCollectionAdminSubstrate(uint256)158 function removeCollectionAdminSubstrate(uint256 admin) external;159160 /// Add collection admin.161 /// @param newAdmin Address of the added administrator.162 /// @dev EVM selector for this function is: 0x92e462c7,163 /// or in textual repr: addCollectionAdmin(address)164 function addCollectionAdmin(address newAdmin) external;165166 /// Remove collection admin.167 ///168 /// @param admin Address of the removed administrator.169 /// @dev EVM selector for this function is: 0xfafd7b42,170 /// or in textual repr: removeCollectionAdmin(address)171 function removeCollectionAdmin(address admin) external;172173 /// Toggle accessibility of collection nesting.174 ///175 /// @param enable If "true" degenerates to nesting: 'Owner' else to nesting: 'Disabled'176 /// @dev EVM selector for this function is: 0x112d4586,177 /// or in textual repr: setCollectionNesting(bool)178 function setCollectionNesting(bool enable) external;179180 /// Toggle accessibility of collection nesting.181 ///182 /// @param enable If "true" degenerates to nesting: {OwnerRestricted: [1, 2, 3]} else to nesting: 'Disabled'183 /// @param collections Addresses of collections that will be available for nesting.184 /// @dev EVM selector for this function is: 0x64872396,185 /// or in textual repr: setCollectionNesting(bool,address[])186 function setCollectionNesting(bool enable, address[] memory collections)187 external;188189 /// Set the collection access method.190 /// @param mode Access mode191 /// 0 for Normal192 /// 1 for AllowList193 /// @dev EVM selector for this function is: 0x41835d4c,194 /// or in textual repr: setCollectionAccess(uint8)195 function setCollectionAccess(uint8 mode) external;196197 /// Add the user to the allowed list.198 ///199 /// @param user Address of a trusted user.200 /// @dev EVM selector for this function is: 0x67844fe6,201 /// or in textual repr: addToCollectionAllowList(address)202 function addToCollectionAllowList(address user) external;203204 /// Remove the user from the allowed list.205 ///206 /// @param user Address of a removed user.207 /// @dev EVM selector for this function is: 0x85c51acb,208 /// or in textual repr: removeFromCollectionAllowList(address)209 function removeFromCollectionAllowList(address user) external;210211 /// Switch permission for minting.212 ///213 /// @param mode Enable if "true".214 /// @dev EVM selector for this function is: 0x00018e84,215 /// or in textual repr: setCollectionMintMode(bool)216 function setCollectionMintMode(bool mode) external;217218 /// Check that account is the owner or admin of the collection219 ///220 /// @param user account to verify221 /// @return "true" if account is the owner or admin222 /// @dev EVM selector for this function is: 0x9811b0c7,223 /// or in textual repr: isOwnerOrAdmin(address)224 function isOwnerOrAdmin(address user) external view returns (bool);225226 /// Check that substrate account is the owner or admin of the collection227 ///228 /// @param user account to verify229 /// @return "true" if account is the owner or admin230 /// @dev EVM selector for this function is: 0x68910e00,231 /// or in textual repr: isOwnerOrAdminSubstrate(uint256)232 function isOwnerOrAdminSubstrate(uint256 user) external view returns (bool);233234 /// Returns collection type235 ///236 /// @return `Fungible` or `NFT` or `ReFungible`237 /// @dev EVM selector for this function is: 0xd34b55b8,238 /// or in textual repr: uniqueCollectionType()239 function uniqueCollectionType() external returns (string memory);240}241242/// @dev the ERC-165 identifier for this interface is 0x79cc6790243interface ERC20UniqueExtensions is Dummy, ERC165 {244 /// @dev EVM selector for this function is: 0x79cc6790,245 /// or in textual repr: burnFrom(address,uint256)246 function burnFrom(address from, uint256 amount) external returns (bool);247}248249/// @dev inlined interface250interface ERC20Events {251 event Transfer(address indexed from, address indexed to, uint256 value);252 event Approval(253 address indexed owner,254 address indexed spender,255 uint256 value256 );257}258259/// @dev the ERC-165 identifier for this interface is 0x942e8b22260interface ERC20 is Dummy, ERC165, ERC20Events {261 /// @dev EVM selector for this function is: 0x06fdde03,262 /// or in textual repr: name()263 function name() external view returns (string memory);264265 /// @dev EVM selector for this function is: 0x95d89b41,266 /// or in textual repr: symbol()267 function symbol() external view returns (string memory);268269 /// @dev EVM selector for this function is: 0x18160ddd,270 /// or in textual repr: totalSupply()271 function totalSupply() external view returns (uint256);272273 /// @dev EVM selector for this function is: 0x313ce567,274 /// or in textual repr: decimals()275 function decimals() external view returns (uint8);276277 /// @dev EVM selector for this function is: 0x70a08231,278 /// or in textual repr: balanceOf(address)279 function balanceOf(address owner) external view returns (uint256);280281 /// @dev EVM selector for this function is: 0xa9059cbb,282 /// or in textual repr: transfer(address,uint256)283 function transfer(address to, uint256 amount) external returns (bool);284285 /// @dev EVM selector for this function is: 0x23b872dd,286 /// or in textual repr: transferFrom(address,address,uint256)287 function transferFrom(288 address from,289 address to,290 uint256 amount291 ) external returns (bool);292293 /// @dev EVM selector for this function is: 0x095ea7b3,294 /// or in textual repr: approve(address,uint256)295 function approve(address spender, uint256 amount) external returns (bool);296297 /// @dev EVM selector for this function is: 0xdd62ed3e,298 /// or in textual repr: allowance(address,address)299 function allowance(address owner, address spender)300 external301 view302 returns (uint256);303}304305interface UniqueFungible is306 Dummy,307 ERC165,308 ERC20,309 ERC20Mintable,310 ERC20UniqueExtensions,311 Collection312{}tests/src/eth/fungible.test.tsdiffbeforeafterboth--- a/tests/src/eth/fungible.test.ts
+++ b/tests/src/eth/fungible.test.ts
@@ -14,10 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
-import {approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess, UNIQUE} from '../util/helpers';
-import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, transferBalanceToEth} from './util/helpers';
+import {approveExpectSuccess, createCollection, createCollectionExpectSuccess, createFungibleItemExpectSuccess, transferExpectSuccess, transferFromExpectSuccess, UNIQUE} from '../util/helpers';
+import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, evmCollection, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, transferBalanceToEth} from './util/helpers';
import fungibleAbi from './fungibleAbi.json';
import {expect} from 'chai';
+import {submitTransactionAsync} from '../substrate/substrate-api';
describe('Fungible: Information getting', () => {
itWeb3('totalSupply', async ({api, web3, privateKeyWrapper}) => {
@@ -58,6 +59,129 @@
});
describe('Fungible: Plain calls', () => {
+ itWeb3('Can perform mint()', async ({web3, api, privateKeyWrapper}) => {
+ const alice = privateKeyWrapper('//Alice');
+ const collection = await createCollection(api, alice, {
+ name: 'token name',
+ mode: {type: 'Fungible', decimalPoints: 0},
+ });
+
+ const receiver = createEthAccount(web3);
+
+ const collectionIdAddress = collectionIdToAddress(collection.collectionId);
+ const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+ const changeAdminTx = api.tx.unique.addCollectionAdmin(collection.collectionId, {Ethereum: owner});
+ await submitTransactionAsync(alice, changeAdminTx);
+
+ const collectionContract = evmCollection(web3, owner, collectionIdAddress, {type: 'Fungible', decimalPoints: 0});
+ const result = await collectionContract.methods.mint(receiver, 100).send();
+ const events = normalizeEvents(result.events);
+
+ expect(events).to.be.deep.equal([
+ {
+ address: collectionIdAddress,
+ event: 'Transfer',
+ args: {
+ from: '0x0000000000000000000000000000000000000000',
+ to: receiver,
+ value: '100',
+ },
+ },
+ ]);
+ });
+
+ itWeb3('Can perform mintBulk()', async ({web3, api, privateKeyWrapper}) => {
+ const alice = privateKeyWrapper('//Alice');
+ const collection = await createCollection(api, alice, {
+ name: 'token name',
+ mode: {type: 'Fungible', decimalPoints: 0},
+ });
+
+ const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+ const receiver1 = createEthAccount(web3);
+ const receiver2 = createEthAccount(web3);
+ const receiver3 = createEthAccount(web3);
+
+ const collectionIdAddress = collectionIdToAddress(collection.collectionId);
+ const changeAdminTx = api.tx.unique.addCollectionAdmin(collection.collectionId, {Ethereum: owner});
+ await submitTransactionAsync(alice, changeAdminTx);
+
+ const collectionContract = evmCollection(web3, owner, collectionIdAddress, {type: 'Fungible', decimalPoints: 0});
+ const result = await collectionContract.methods.mintBulk([
+ [receiver1, 10],
+ [receiver2, 20],
+ [receiver3, 30],
+ ]).call();
+ console.log(result);
+ const events = normalizeEvents(result.events);
+
+ expect(events).to.be.deep.equal([
+ {
+ address:collectionIdAddress,
+ event: 'Transfer',
+ args: {
+ from: '0x0000000000000000000000000000000000000000',
+ to: receiver1,
+ value: '10',
+ },
+ },
+ {
+ address:collectionIdAddress,
+ event: 'Transfer',
+ args: {
+ from: '0x0000000000000000000000000000000000000000',
+ to: receiver2,
+ value: '20',
+ },
+ },
+ {
+ address:collectionIdAddress,
+ event: 'Transfer',
+ args: {
+ from: '0x0000000000000000000000000000000000000000',
+ to: receiver3,
+ value: '30',
+ },
+ },
+ ]);
+ });
+
+ itWeb3('Can perform burn()', async ({web3, api, privateKeyWrapper}) => {
+ const alice = privateKeyWrapper('//Alice');
+ const collection = await createCollection(api, alice, {
+ name: 'token name',
+ mode: {type: 'Fungible', decimalPoints: 0},
+ });
+
+ const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+ const changeAdminTx = api.tx.unique.addCollectionAdmin(collection.collectionId, {Ethereum: owner});
+ await submitTransactionAsync(alice, changeAdminTx);
+ const receiver = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+
+ const collectionIdAddress = collectionIdToAddress(collection.collectionId);
+ const collectionContract = evmCollection(web3, owner, collectionIdAddress, {type: 'Fungible', decimalPoints: 0});
+ await collectionContract.methods.mint(receiver, 100).send();
+
+ const result = await collectionContract.methods.burnFrom(receiver, 49).send({from: receiver});
+
+ const events = normalizeEvents(result.events);
+
+ expect(events).to.be.deep.equal([
+ {
+ address: collectionIdAddress,
+ event: 'Transfer',
+ args: {
+ from: receiver,
+ to: '0x0000000000000000000000000000000000000000',
+ value: '49',
+ },
+ },
+ ]);
+
+ const balance = await collectionContract.methods.balanceOf(receiver).call();
+ expect(balance).to.equal('51');
+ });
+
itWeb3('Can perform approve()', async ({web3, api, privateKeyWrapper}) => {
const collection = await createCollectionExpectSuccess({
name: 'token name',
tests/src/eth/fungibleAbi.jsondiffbeforeafterboth--- a/tests/src/eth/fungibleAbi.json
+++ b/tests/src/eth/fungibleAbi.json
@@ -151,6 +151,33 @@
"type": "function"
},
{
+ "inputs": [
+ { "internalType": "address", "name": "to", "type": "address" },
+ { "internalType": "uint256", "name": "amount", "type": "uint256" }
+ ],
+ "name": "mint",
+ "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
+ "stateMutability": "nonpayable",
+ "type": "function"
+ },
+ {
+ "inputs": [
+ {
+ "components": [
+ { "internalType": "address", "name": "field_0", "type": "address" },
+ { "internalType": "uint256", "name": "field_1", "type": "uint256" }
+ ],
+ "internalType": "struct Tuple0[]",
+ "name": "amounts",
+ "type": "tuple[]"
+ }
+ ],
+ "name": "mintBulk",
+ "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
+ "stateMutability": "nonpayable",
+ "type": "function"
+ },
+ {
"inputs": [],
"name": "getCollectionSponsor",
"outputs": [