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.soldiffbeforeafterboth--- a/tests/src/eth/api/UniqueFungible.sol
+++ b/tests/src/eth/api/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
interface Dummy {
}
@@ -12,8 +18,32 @@
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}
-/// @title A contract that allows you to work with collections.
-/// @dev the ERC-165 identifier for this interface is 0xe54be640
+// Inline
+interface ERC20Events {
+ event Transfer(address indexed from, address indexed to, uint256 value);
+ event Approval(
+ address indexed owner,
+ address indexed spender,
+ uint256 value
+ );
+}
+
+// Selector: 40c10f19
+interface ERC20Mintable is Dummy, ERC165 {
+ // Selector: mint(address,uint256) 40c10f19
+ function mint(address to, uint256 amount) external returns (bool);
+}
+
+// Selector: 63034ac5
+interface ERC20UniqueExtensions is Dummy, ERC165 {
+ // Selector: burnFrom(address,uint256) 79cc6790
+ function burnFrom(address from, uint256 amount) external returns (bool);
+
+ // Selector: mintBulk((address,uint256)[]) 1acf2d55
+ function mintBulk(Tuple0[] memory amounts) external returns (bool);
+}
+
+// Selector: 6cf113cd
interface Collection is Dummy, ERC165 {
/// Set collection property.
///
@@ -207,30 +237,8 @@
/// @dev EVM selector for this function is: 0xd34b55b8,
/// or in textual repr: uniqueCollectionType()
function uniqueCollectionType() external returns (string memory);
-
- /// Changes collection owner to another account
- ///
- /// @dev Owner can be changed only by current owner
- /// @param newOwner new owner account
- /// @dev EVM selector for this function is: 0x13af4035,
- /// or in textual repr: setOwner(address)
- function setOwner(address newOwner) external;
-
- /// Changes collection owner to another substrate account
- ///
- /// @dev Owner can be changed only by current owner
- /// @param newOwner new owner substrate account
- /// @dev EVM selector for this function is: 0xb212138f,
- /// or in textual repr: setOwnerSubstrate(uint256)
- function setOwnerSubstrate(uint256 newOwner) external;
}
-/// @dev anonymous struct
-struct Tuple6 {
- address field_0;
- uint256 field_1;
-}
-
/// @dev the ERC-165 identifier for this interface is 0x79cc6790
interface ERC20UniqueExtensions is Dummy, ERC165 {
/// @dev EVM selector for this function is: 0x79cc6790,
@@ -298,6 +306,7 @@
Dummy,
ERC165,
ERC20,
+ ERC20Mintable,
ERC20UniqueExtensions,
Collection
{}
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.jsondiffbeforeafterboth1[2 {3 "anonymous": false,4 "inputs": [5 {6 "indexed": true,7 "internalType": "address",8 "name": "owner",9 "type": "address"10 },11 {12 "indexed": true,13 "internalType": "address",14 "name": "spender",15 "type": "address"16 },17 {18 "indexed": false,19 "internalType": "uint256",20 "name": "value",21 "type": "uint256"22 }23 ],24 "name": "Approval",25 "type": "event"26 },27 {28 "anonymous": false,29 "inputs": [30 {31 "indexed": true,32 "internalType": "address",33 "name": "from",34 "type": "address"35 },36 {37 "indexed": true,38 "internalType": "address",39 "name": "to",40 "type": "address"41 },42 {43 "indexed": false,44 "internalType": "uint256",45 "name": "value",46 "type": "uint256"47 }48 ],49 "name": "Transfer",50 "type": "event"51 },52 {53 "inputs": [54 { "internalType": "address", "name": "newAdmin", "type": "address" }55 ],56 "name": "addCollectionAdmin",57 "outputs": [],58 "stateMutability": "nonpayable",59 "type": "function"60 },61 {62 "inputs": [63 { "internalType": "uint256", "name": "newAdmin", "type": "uint256" }64 ],65 "name": "addCollectionAdminSubstrate",66 "outputs": [],67 "stateMutability": "nonpayable",68 "type": "function"69 },70 {71 "inputs": [72 { "internalType": "address", "name": "user", "type": "address" }73 ],74 "name": "addToCollectionAllowList",75 "outputs": [],76 "stateMutability": "nonpayable",77 "type": "function"78 },79 {80 "inputs": [81 { "internalType": "address", "name": "owner", "type": "address" },82 { "internalType": "address", "name": "spender", "type": "address" }83 ],84 "name": "allowance",85 "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],86 "stateMutability": "view",87 "type": "function"88 },89 {90 "inputs": [91 { "internalType": "address", "name": "spender", "type": "address" },92 { "internalType": "uint256", "name": "amount", "type": "uint256" }93 ],94 "name": "approve",95 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],96 "stateMutability": "nonpayable",97 "type": "function"98 },99 {100 "inputs": [101 { "internalType": "address", "name": "owner", "type": "address" }102 ],103 "name": "balanceOf",104 "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],105 "stateMutability": "view",106 "type": "function"107 },108 {109 "inputs": [110 { "internalType": "address", "name": "from", "type": "address" },111 { "internalType": "uint256", "name": "amount", "type": "uint256" }112 ],113 "name": "burnFrom",114 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],115 "stateMutability": "nonpayable",116 "type": "function"117 },118 {119 "inputs": [{ "internalType": "string", "name": "key", "type": "string" }],120 "name": "collectionProperty",121 "outputs": [{ "internalType": "bytes", "name": "", "type": "bytes" }],122 "stateMutability": "view",123 "type": "function"124 },125 {126 "inputs": [],127 "name": "confirmCollectionSponsorship",128 "outputs": [],129 "stateMutability": "nonpayable",130 "type": "function"131 },132 {133 "inputs": [],134 "name": "contractAddress",135 "outputs": [{ "internalType": "address", "name": "", "type": "address" }],136 "stateMutability": "view",137 "type": "function"138 },139 {140 "inputs": [],141 "name": "decimals",142 "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }],143 "stateMutability": "view",144 "type": "function"145 },146 {147 "inputs": [{ "internalType": "string", "name": "key", "type": "string" }],148 "name": "deleteCollectionProperty",149 "outputs": [],150 "stateMutability": "nonpayable",151 "type": "function"152 },153 {154 "inputs": [],155 "name": "getCollectionSponsor",156 "outputs": [157 {158 "components": [159 { "internalType": "address", "name": "field_0", "type": "address" },160 { "internalType": "uint256", "name": "field_1", "type": "uint256" }161 ],162 "internalType": "struct Tuple6",163 "name": "",164 "type": "tuple"165 }166 ],167 "stateMutability": "view",168 "type": "function"169 },170 {171 "inputs": [],172 "name": "hasCollectionPendingSponsor",173 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],174 "stateMutability": "view",175 "type": "function"176 },177 {178 "inputs": [179 { "internalType": "address", "name": "user", "type": "address" }180 ],181 "name": "isOwnerOrAdmin",182 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],183 "stateMutability": "view",184 "type": "function"185 },186 {187 "inputs": [188 { "internalType": "uint256", "name": "user", "type": "uint256" }189 ],190 "name": "isOwnerOrAdminSubstrate",191 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],192 "stateMutability": "view",193 "type": "function"194 },195 {196 "inputs": [],197 "name": "name",198 "outputs": [{ "internalType": "string", "name": "", "type": "string" }],199 "stateMutability": "view",200 "type": "function"201 },202 {203 "inputs": [204 { "internalType": "address", "name": "admin", "type": "address" }205 ],206 "name": "removeCollectionAdmin",207 "outputs": [],208 "stateMutability": "nonpayable",209 "type": "function"210 },211 {212 "inputs": [213 { "internalType": "uint256", "name": "admin", "type": "uint256" }214 ],215 "name": "removeCollectionAdminSubstrate",216 "outputs": [],217 "stateMutability": "nonpayable",218 "type": "function"219 },220 {221 "inputs": [],222 "name": "removeCollectionSponsor",223 "outputs": [],224 "stateMutability": "nonpayable",225 "type": "function"226 },227 {228 "inputs": [229 { "internalType": "address", "name": "user", "type": "address" }230 ],231 "name": "removeFromCollectionAllowList",232 "outputs": [],233 "stateMutability": "nonpayable",234 "type": "function"235 },236 {237 "inputs": [{ "internalType": "uint8", "name": "mode", "type": "uint8" }],238 "name": "setCollectionAccess",239 "outputs": [],240 "stateMutability": "nonpayable",241 "type": "function"242 },243 {244 "inputs": [245 { "internalType": "string", "name": "limit", "type": "string" },246 { "internalType": "uint32", "name": "value", "type": "uint32" }247 ],248 "name": "setCollectionLimit",249 "outputs": [],250 "stateMutability": "nonpayable",251 "type": "function"252 },253 {254 "inputs": [255 { "internalType": "string", "name": "limit", "type": "string" },256 { "internalType": "bool", "name": "value", "type": "bool" }257 ],258 "name": "setCollectionLimit",259 "outputs": [],260 "stateMutability": "nonpayable",261 "type": "function"262 },263 {264 "inputs": [{ "internalType": "bool", "name": "mode", "type": "bool" }],265 "name": "setCollectionMintMode",266 "outputs": [],267 "stateMutability": "nonpayable",268 "type": "function"269 },270 {271 "inputs": [{ "internalType": "bool", "name": "enable", "type": "bool" }],272 "name": "setCollectionNesting",273 "outputs": [],274 "stateMutability": "nonpayable",275 "type": "function"276 },277 {278 "inputs": [279 { "internalType": "bool", "name": "enable", "type": "bool" },280 {281 "internalType": "address[]",282 "name": "collections",283 "type": "address[]"284 }285 ],286 "name": "setCollectionNesting",287 "outputs": [],288 "stateMutability": "nonpayable",289 "type": "function"290 },291 {292 "inputs": [293 { "internalType": "string", "name": "key", "type": "string" },294 { "internalType": "bytes", "name": "value", "type": "bytes" }295 ],296 "name": "setCollectionProperty",297 "outputs": [],298 "stateMutability": "nonpayable",299 "type": "function"300 },301 {302 "inputs": [303 { "internalType": "address", "name": "sponsor", "type": "address" }304 ],305 "name": "setCollectionSponsor",306 "outputs": [],307 "stateMutability": "nonpayable",308 "type": "function"309 },310 {311 "inputs": [312 { "internalType": "uint256", "name": "sponsor", "type": "uint256" }313 ],314 "name": "setCollectionSponsorSubstrate",315 "outputs": [],316 "stateMutability": "nonpayable",317 "type": "function"318 },319 {320 "inputs": [321 { "internalType": "address", "name": "newOwner", "type": "address" }322 ],323 "name": "setOwner",324 "outputs": [],325 "stateMutability": "nonpayable",326 "type": "function"327 },328 {329 "inputs": [330 { "internalType": "uint256", "name": "newOwner", "type": "uint256" }331 ],332 "name": "setOwnerSubstrate",333 "outputs": [],334 "stateMutability": "nonpayable",335 "type": "function"336 },337 {338 "inputs": [339 { "internalType": "bytes4", "name": "interfaceID", "type": "bytes4" }340 ],341 "name": "supportsInterface",342 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],343 "stateMutability": "view",344 "type": "function"345 },346 {347 "inputs": [],348 "name": "symbol",349 "outputs": [{ "internalType": "string", "name": "", "type": "string" }],350 "stateMutability": "view",351 "type": "function"352 },353 {354 "inputs": [],355 "name": "totalSupply",356 "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],357 "stateMutability": "view",358 "type": "function"359 },360 {361 "inputs": [362 { "internalType": "address", "name": "to", "type": "address" },363 { "internalType": "uint256", "name": "amount", "type": "uint256" }364 ],365 "name": "transfer",366 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],367 "stateMutability": "nonpayable",368 "type": "function"369 },370 {371 "inputs": [372 { "internalType": "address", "name": "from", "type": "address" },373 { "internalType": "address", "name": "to", "type": "address" },374 { "internalType": "uint256", "name": "amount", "type": "uint256" }375 ],376 "name": "transferFrom",377 "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],378 "stateMutability": "nonpayable",379 "type": "function"380 },381 {382 "inputs": [],383 "name": "uniqueCollectionType",384 "outputs": [{ "internalType": "string", "name": "", "type": "string" }],385 "stateMutability": "nonpayable",386 "type": "function"387 }388]