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.rsdiffbeforeafterboth1// 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/>.1617extern crate alloc;18use core::{19 char::{REPLACEMENT_CHARACTER, decode_utf16},20 convert::TryInto,21 ops::Deref,22};23use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};24use pallet_common::{25 CommonWeightInfo,26 erc::{CommonEvmHandler, PrecompileResult},27};28use pallet_evm::{account::CrossAccountId, PrecompileHandle};29use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};30use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};31use sp_std::vec::Vec;32use up_data_structs::TokenId;3334use crate::{35 Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,36 weights::WeightInfo, TotalSupply,37};3839pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);4041#[derive(ToLog)]42pub enum ERC20Events {43 Transfer {44 #[indexed]45 from: address,46 #[indexed]47 to: address,48 value: uint256,49 },50 Approval {51 #[indexed]52 owner: address,53 #[indexed]54 spender: address,55 value: uint256,56 },57}5859#[solidity_interface(name = "ERC20", events(ERC20Events))]60impl<T: Config> RefungibleTokenHandle<T> {61 fn name(&self) -> Result<string> {62 Ok(decode_utf16(self.name.iter().copied())63 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))64 .collect::<string>())65 }66 fn symbol(&self) -> Result<string> {67 Ok(string::from_utf8_lossy(&self.token_prefix).into())68 }69 fn total_supply(&self) -> Result<uint256> {70 self.consume_store_reads(1)?;71 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())72 }7374 fn decimals(&self) -> Result<uint8> {75 // Decimals aren't supported for refungible tokens76 Ok(0)77 }7879 fn balance_of(&self, owner: address) -> Result<uint256> {80 self.consume_store_reads(1)?;81 let owner = T::CrossAccountId::from_eth(owner);82 let balance = <Balance<T>>::get((self.id, self.1, owner));83 Ok(balance.into())84 }85 #[weight(<CommonWeights<T>>::transfer())]86 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {87 let caller = T::CrossAccountId::from_eth(caller);88 let to = T::CrossAccountId::from_eth(to);89 let amount = amount.try_into().map_err(|_| "amount overflow")?;90 let budget = self91 .recorder92 .weight_calls_budget(<StructureWeight<T>>::find_parent());9394 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)95 .map_err(|_| "transfer error")?;96 Ok(true)97 }98 #[weight(<CommonWeights<T>>::transfer_from())]99 fn transfer_from(100 &mut self,101 caller: caller,102 from: address,103 to: address,104 amount: uint256,105 ) -> Result<bool> {106 let caller = T::CrossAccountId::from_eth(caller);107 let from = T::CrossAccountId::from_eth(from);108 let to = T::CrossAccountId::from_eth(to);109 let amount = amount.try_into().map_err(|_| "amount overflow")?;110 let budget = self111 .recorder112 .weight_calls_budget(<StructureWeight<T>>::find_parent());113114 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)115 .map_err(dispatch_to_evm::<T>)?;116 Ok(true)117 }118 #[weight(<SelfWeightOf<T>>::approve())]119 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {120 let caller = T::CrossAccountId::from_eth(caller);121 let spender = T::CrossAccountId::from_eth(spender);122 let amount = amount.try_into().map_err(|_| "amount overflow")?;123124 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)125 .map_err(dispatch_to_evm::<T>)?;126 Ok(true)127 }128 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {129 self.consume_store_reads(1)?;130 let owner = T::CrossAccountId::from_eth(owner);131 let spender = T::CrossAccountId::from_eth(spender);132133 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())134 }135}136137#[solidity_interface(name = "ERC20UniqueExtensions")]138impl<T: Config> RefungibleTokenHandle<T> {139 #[weight(<SelfWeightOf<T>>::burn_from())]140 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {141 let caller = T::CrossAccountId::from_eth(caller);142 let from = T::CrossAccountId::from_eth(from);143 let amount = amount.try_into().map_err(|_| "amount overflow")?;144 let budget = self145 .recorder146 .weight_calls_budget(<StructureWeight<T>>::find_parent());147148 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)149 .map_err(dispatch_to_evm::<T>)?;150 Ok(true)151 }152}153154impl<T: Config> RefungibleTokenHandle<T> {155 pub fn into_inner(self) -> RefungibleHandle<T> {156 self.0157 }158 pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {159 &mut self.0160 }161}162163impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {164 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {165 self.0.recorder()166 }167 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {168 self.0.into_recorder()169 }170}171172impl<T: Config> Deref for RefungibleTokenHandle<T> {173 type Target = RefungibleHandle<T>;174175 fn deref(&self) -> &Self::Target {176 &self.0177 }178}179180#[solidity_interface(name = "UniqueRefungibleToken", is(ERC20, ERC20UniqueExtensions,))]181impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}182183generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);184generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);185186impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>187where188 T::AccountId: From<[u8; 32]>,189{190 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");191192 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {193 call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)194 }195}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/>.1617extern crate alloc;18use core::{19 char::{REPLACEMENT_CHARACTER, decode_utf16},20 convert::TryInto,21 ops::Deref,22};23use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};24use pallet_common::{25 CommonWeightInfo,26 erc::{CommonEvmHandler, PrecompileResult},27};28use pallet_evm::{account::CrossAccountId, PrecompileHandle};29use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};30use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};31use sp_std::vec::Vec;32use up_data_structs::TokenId;3334use crate::{35 Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,36 weights::WeightInfo, TotalSupply,37};3839pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);4041#[derive(ToLog)]42pub enum ERC20Events {43 Transfer {44 #[indexed]45 from: address,46 #[indexed]47 to: address,48 value: uint256,49 },50 Approval {51 #[indexed]52 owner: address,53 #[indexed]54 spender: address,55 value: uint256,56 },57}5859#[solidity_interface(name = "ERC20", events(ERC20Events))]60impl<T: Config> RefungibleTokenHandle<T> {61 fn name(&self) -> Result<string> {62 Ok(decode_utf16(self.name.iter().copied())63 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))64 .collect::<string>())65 }66 fn symbol(&self) -> Result<string> {67 Ok(string::from_utf8_lossy(&self.token_prefix).into())68 }69 fn total_supply(&self) -> Result<uint256> {70 self.consume_store_reads(1)?;71 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())72 }7374 fn decimals(&self) -> Result<uint8> {75 // Decimals aren't supported for refungible tokens76 Ok(0)77 }7879 fn balance_of(&self, owner: address) -> Result<uint256> {80 self.consume_store_reads(1)?;81 let owner = T::CrossAccountId::from_eth(owner);82 let balance = <Balance<T>>::get((self.id, self.1, owner));83 Ok(balance.into())84 }85 #[weight(<CommonWeights<T>>::transfer())]86 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {87 let caller = T::CrossAccountId::from_eth(caller);88 let to = T::CrossAccountId::from_eth(to);89 let amount = amount.try_into().map_err(|_| "amount overflow")?;90 let budget = self91 .recorder92 .weight_calls_budget(<StructureWeight<T>>::find_parent());9394 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)95 .map_err(|_| "transfer error")?;96 Ok(true)97 }98 #[weight(<CommonWeights<T>>::transfer_from())]99 fn transfer_from(100 &mut self,101 caller: caller,102 from: address,103 to: address,104 amount: uint256,105 ) -> Result<bool> {106 let caller = T::CrossAccountId::from_eth(caller);107 let from = T::CrossAccountId::from_eth(from);108 let to = T::CrossAccountId::from_eth(to);109 let amount = amount.try_into().map_err(|_| "amount overflow")?;110 let budget = self111 .recorder112 .weight_calls_budget(<StructureWeight<T>>::find_parent());113114 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)115 .map_err(dispatch_to_evm::<T>)?;116 Ok(true)117 }118 #[weight(<SelfWeightOf<T>>::approve())]119 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {120 let caller = T::CrossAccountId::from_eth(caller);121 let spender = T::CrossAccountId::from_eth(spender);122 let amount = amount.try_into().map_err(|_| "amount overflow")?;123124 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)125 .map_err(dispatch_to_evm::<T>)?;126 Ok(true)127 }128 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {129 self.consume_store_reads(1)?;130 let owner = T::CrossAccountId::from_eth(owner);131 let spender = T::CrossAccountId::from_eth(spender);132133 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())134 }135}136137#[solidity_interface(name = "ERC20UniqueExtensions")]138impl<T: Config> RefungibleTokenHandle<T> {139 #[weight(<SelfWeightOf<T>>::burn_from())]140 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {141 let caller = T::CrossAccountId::from_eth(caller);142 let from = T::CrossAccountId::from_eth(from);143 let amount = amount.try_into().map_err(|_| "amount overflow")?;144 let budget = self145 .recorder146 .weight_calls_budget(<StructureWeight<T>>::find_parent());147148 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)149 .map_err(dispatch_to_evm::<T>)?;150 Ok(true)151 }152153 #[weight(<SelfWeightOf<T>>::repartition_item())]154 fn repartition(&mut self, caller: caller, amount: uint256) -> Result<bool> {155 let caller = T::CrossAccountId::from_eth(caller);156 let amount = amount.try_into().map_err(|_| "amount overflow")?;157158 <Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;159 Ok(true)160 }161}162163impl<T: Config> RefungibleTokenHandle<T> {164 pub fn into_inner(self) -> RefungibleHandle<T> {165 self.0166 }167 pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {168 &mut self.0169 }170}171172impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {173 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {174 self.0.recorder()175 }176 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {177 self.0.into_recorder()178 }179}180181impl<T: Config> Deref for RefungibleTokenHandle<T> {182 type Target = RefungibleHandle<T>;183184 fn deref(&self) -> &Self::Target {185 &self.0186 }187}188189#[solidity_interface(name = "UniqueRefungibleToken", is(ERC20, ERC20UniqueExtensions,))]190impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}191192generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);193generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);194195impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>196where197 T::AccountId: From<[u8; 32]>,198{199 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");200201 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {202 call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)203 }204}pallets/refungible/src/stubs/UniqueRefungibleToken.rawdiffbeforeafterbothbinary blob — no preview
pallets/refungible/src/stubs/UniqueRefungibleToken.soldiffbeforeafterboth--- a/pallets/refungible/src/stubs/UniqueRefungibleToken.sol
+++ b/pallets/refungible/src/stubs/UniqueRefungibleToken.sol
@@ -31,18 +31,6 @@
);
}
-// Selector: 79cc6790
-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: 942e8b22
contract ERC20 is Dummy, ERC165, ERC20Events {
// Selector: name() 06fdde03
@@ -127,4 +115,24 @@
}
}
+// Selector: ab8deb37
+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: repartition(uint256) d2418ca7
+ function repartition(uint256 amount) public returns (bool) {
+ require(false, stub_error);
+ amount;
+ dummy = 0;
+ return false;
+ }
+}
+
contract 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",