difftreelog
added repartition method to refungible erc20 extensions
in: master
7 files changed
pallets/refungible/src/erc.rsdiffbeforeafterboth--- a/pallets/refungible/src/erc.rs
+++ b/pallets/refungible/src/erc.rs
@@ -1,3 +1,19 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
extern crate alloc;
use evm_coder::{generate_stubgen, solidity_interface, types::*};
pallets/refungible/src/erc_token.rsdiffbeforeafterboth--- a/pallets/refungible/src/erc_token.rs
+++ b/pallets/refungible/src/erc_token.rs
@@ -149,6 +149,15 @@
.map_err(dispatch_to_evm::<T>)?;
Ok(true)
}
+
+ #[weight(<SelfWeightOf<T>>::repartition_item())]
+ fn repartition(&mut self, caller: caller, amount: uint256) -> Result<bool> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let amount = amount.try_into().map_err(|_| "amount overflow")?;
+
+ <Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;
+ Ok(true)
+ }
}
impl<T: Config> RefungibleTokenHandle<T> {
pallets/refungible/src/stubs/UniqueRefungibleToken.rawdiffbeforeafterbothbinary blob — no preview
pallets/refungible/src/stubs/UniqueRefungibleToken.soldiffbeforeafterboth1// SPDX-License-Identifier: OTHER2// This code is automatically generated34pragma solidity >=0.8.0 <0.9.0;56// Common stubs holder7contract Dummy {8 uint8 dummy;9 string stub_error = "this contract is implemented in native";10}1112contract ERC165 is Dummy {13 function supportsInterface(bytes4 interfaceID)14 external15 view16 returns (bool)17 {18 require(false, stub_error);19 interfaceID;20 return true;21 }22}2324// Inline25contract ERC20Events {26 event Transfer(address indexed from, address indexed to, uint256 value);27 event Approval(28 address indexed owner,29 address indexed spender,30 uint256 value31 );32}3334// Selector: 79cc679035contract ERC20UniqueExtensions is Dummy, ERC165 {36 // Selector: burnFrom(address,uint256) 79cc679037 function burnFrom(address from, uint256 amount) public returns (bool) {38 require(false, stub_error);39 from;40 amount;41 dummy = 0;42 return false;43 }44}4546// Selector: 942e8b2247contract ERC20 is Dummy, ERC165, ERC20Events {48 // Selector: name() 06fdde0349 function name() public view returns (string memory) {50 require(false, stub_error);51 dummy;52 return "";53 }5455 // Selector: symbol() 95d89b4156 function symbol() public view returns (string memory) {57 require(false, stub_error);58 dummy;59 return "";60 }6162 // Selector: totalSupply() 18160ddd63 function totalSupply() public view returns (uint256) {64 require(false, stub_error);65 dummy;66 return 0;67 }6869 // Selector: decimals() 313ce56770 function decimals() public view returns (uint8) {71 require(false, stub_error);72 dummy;73 return 0;74 }7576 // Selector: balanceOf(address) 70a0823177 function balanceOf(address owner) public view returns (uint256) {78 require(false, stub_error);79 owner;80 dummy;81 return 0;82 }8384 // Selector: transfer(address,uint256) a9059cbb85 function transfer(address to, uint256 amount) public returns (bool) {86 require(false, stub_error);87 to;88 amount;89 dummy = 0;90 return false;91 }9293 // Selector: transferFrom(address,address,uint256) 23b872dd94 function transferFrom(95 address from,96 address to,97 uint256 amount98 ) public returns (bool) {99 require(false, stub_error);100 from;101 to;102 amount;103 dummy = 0;104 return false;105 }106107 // Selector: approve(address,uint256) 095ea7b3108 function approve(address spender, uint256 amount) public returns (bool) {109 require(false, stub_error);110 spender;111 amount;112 dummy = 0;113 return false;114 }115116 // Selector: allowance(address,address) dd62ed3e117 function allowance(address owner, address spender)118 public119 view120 returns (uint256)121 {122 require(false, stub_error);123 owner;124 spender;125 dummy;126 return 0;127 }128}129130contract UniqueRefungibleToken is Dummy, ERC165, ERC20, ERC20UniqueExtensions {}1// SPDX-License-Identifier: OTHER2// This code is automatically generated34pragma solidity >=0.8.0 <0.9.0;56// Common stubs holder7contract Dummy {8 uint8 dummy;9 string stub_error = "this contract is implemented in native";10}1112contract ERC165 is Dummy {13 function supportsInterface(bytes4 interfaceID)14 external15 view16 returns (bool)17 {18 require(false, stub_error);19 interfaceID;20 return true;21 }22}2324// Inline25contract ERC20Events {26 event Transfer(address indexed from, address indexed to, uint256 value);27 event Approval(28 address indexed owner,29 address indexed spender,30 uint256 value31 );32}3334// Selector: 942e8b2235contract ERC20 is Dummy, ERC165, ERC20Events {36 // Selector: name() 06fdde0337 function name() public view returns (string memory) {38 require(false, stub_error);39 dummy;40 return "";41 }4243 // Selector: symbol() 95d89b4144 function symbol() public view returns (string memory) {45 require(false, stub_error);46 dummy;47 return "";48 }4950 // Selector: totalSupply() 18160ddd51 function totalSupply() public view returns (uint256) {52 require(false, stub_error);53 dummy;54 return 0;55 }5657 // Selector: decimals() 313ce56758 function decimals() public view returns (uint8) {59 require(false, stub_error);60 dummy;61 return 0;62 }6364 // Selector: balanceOf(address) 70a0823165 function balanceOf(address owner) public view returns (uint256) {66 require(false, stub_error);67 owner;68 dummy;69 return 0;70 }7172 // Selector: transfer(address,uint256) a9059cbb73 function transfer(address to, uint256 amount) public returns (bool) {74 require(false, stub_error);75 to;76 amount;77 dummy = 0;78 return false;79 }8081 // Selector: transferFrom(address,address,uint256) 23b872dd82 function transferFrom(83 address from,84 address to,85 uint256 amount86 ) public returns (bool) {87 require(false, stub_error);88 from;89 to;90 amount;91 dummy = 0;92 return false;93 }9495 // Selector: approve(address,uint256) 095ea7b396 function approve(address spender, uint256 amount) public returns (bool) {97 require(false, stub_error);98 spender;99 amount;100 dummy = 0;101 return false;102 }103104 // Selector: allowance(address,address) dd62ed3e105 function allowance(address owner, address spender)106 public107 view108 returns (uint256)109 {110 require(false, stub_error);111 owner;112 spender;113 dummy;114 return 0;115 }116}117118// Selector: ab8deb37119contract ERC20UniqueExtensions is Dummy, ERC165 {120 // Selector: burnFrom(address,uint256) 79cc6790121 function burnFrom(address from, uint256 amount) public returns (bool) {122 require(false, stub_error);123 from;124 amount;125 dummy = 0;126 return false;127 }128129 // Selector: repartition(uint256) d2418ca7130 function repartition(uint256 amount) public returns (bool) {131 require(false, stub_error);132 amount;133 dummy = 0;134 return false;135 }136}137138contract UniqueRefungibleToken is Dummy, ERC165, ERC20, ERC20UniqueExtensions {}tests/src/eth/api/UniqueRefungibleToken.soldiffbeforeafterboth--- a/tests/src/eth/api/UniqueRefungibleToken.sol
+++ b/tests/src/eth/api/UniqueRefungibleToken.sol
@@ -22,12 +22,6 @@
);
}
-// Selector: 79cc6790
-interface ERC20UniqueExtensions is Dummy, ERC165 {
- // Selector: burnFrom(address,uint256) 79cc6790
- function burnFrom(address from, uint256 amount) external returns (bool);
-}
-
// Selector: 942e8b22
interface ERC20 is Dummy, ERC165, ERC20Events {
// Selector: name() 06fdde03
@@ -65,6 +59,15 @@
returns (uint256);
}
+// Selector: ab8deb37
+interface ERC20UniqueExtensions is Dummy, ERC165 {
+ // Selector: burnFrom(address,uint256) 79cc6790
+ function burnFrom(address from, uint256 amount) external returns (bool);
+
+ // Selector: repartition(uint256) d2418ca7
+ function repartition(uint256 amount) external returns (bool);
+}
+
interface UniqueRefungibleToken is
Dummy,
ERC165,
tests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth--- a/tests/src/eth/reFungibleToken.test.ts
+++ b/tests/src/eth/reFungibleToken.test.ts
@@ -210,6 +210,39 @@
expect(+balance).to.equal(50);
}
});
+
+ itWeb3('Can perform repartition()', async ({web3, api, privateKeyWrapper}) => {
+ const alice = privateKeyWrapper('//Alice');
+
+ const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;
+
+ const owner = createEthAccount(web3);
+ await transferBalanceToEth(api, alice, owner);
+
+ const receiver = createEthAccount(web3);
+ await transferBalanceToEth(api, alice, receiver);
+
+ const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;
+
+ const address = tokenIdToAddress(collectionId, tokenId);
+ const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});
+
+ await contract.methods.repartition(200).send({from: owner});
+ expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(200);
+ await contract.methods.transfer(receiver, 110).send({from: owner});
+ expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(90);
+ expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(110);
+
+ await expect(contract.methods.repartition(80).send({from: owner})).to.eventually.be.rejected;
+
+ await contract.methods.transfer(receiver, 90).send({from: owner});
+ expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);
+ expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);
+
+ await contract.methods.repartition(150).send({from: receiver});
+ await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected;
+ expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);
+ });
});
describe('Refungible: Fees', () => {
tests/src/eth/reFungibleTokenAbi.jsondiffbeforeafterboth--- a/tests/src/eth/reFungibleTokenAbi.json
+++ b/tests/src/eth/reFungibleTokenAbi.json
@@ -104,6 +104,15 @@
},
{
"inputs": [
+ { "internalType": "uint256", "name": "amount", "type": "uint256" }
+ ],
+ "name": "repartition",
+ "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
+ "stateMutability": "nonpayable",
+ "type": "function"
+ },
+ {
+ "inputs": [
{ "internalType": "bytes4", "name": "interfaceID", "type": "bytes4" }
],
"name": "supportsInterface",