difftreelog
fix PR
in: master
4 files changed
pallets/fungible/src/erc.rsdiffbeforeafterboth--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -6,7 +6,7 @@
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
-// Unique Network is disaddress: tod in the hope that it will be useful,
+// 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.
@@ -28,6 +28,7 @@
use pallet_common::{
CollectionHandle,
erc::{CommonEvmHandler, PrecompileResult, CollectionCall},
+ eth::CrossAddress,
};
use sp_std::vec::Vec;
use pallet_evm::{account::CrossAccountId, PrecompileHandle};
@@ -172,11 +173,7 @@
/// @param owner crossAddress The address which owns the funds.
/// @param spender crossAddress The address which will spend the funds.
/// @return A uint256 specifying the amount of tokens still available for the spender.
- fn allowance_cross(
- &self,
- owner: pallet_common::eth::CrossAddress,
- spender: pallet_common::eth::CrossAddress,
- ) -> Result<U256> {
+ fn allowance_cross(&self, owner: CrossAddress, spender: CrossAddress) -> Result<U256> {
let owner = owner.into_sub_cross_account::<T>()?;
let spender = spender.into_sub_cross_account::<T>()?;
@@ -191,12 +188,7 @@
}
#[weight(<SelfWeightOf<T>>::create_item())]
- fn mint_cross(
- &mut self,
- caller: Caller,
- to: pallet_common::eth::CrossAddress,
- amount: U256,
- ) -> Result<bool> {
+ fn mint_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let to = to.into_sub_cross_account::<T>()?;
let amount = amount.try_into().map_err(|_| "amount overflow")?;
@@ -212,7 +204,7 @@
fn approve_cross(
&mut self,
caller: Caller,
- spender: pallet_common::eth::CrossAddress,
+ spender: CrossAddress,
amount: U256,
) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
@@ -253,7 +245,7 @@
fn burn_from_cross(
&mut self,
caller: Caller,
- from: pallet_common::eth::CrossAddress,
+ from: CrossAddress,
amount: U256,
) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
@@ -292,12 +284,7 @@
}
#[weight(<SelfWeightOf<T>>::transfer())]
- fn transfer_cross(
- &mut self,
- caller: Caller,
- to: pallet_common::eth::CrossAddress,
- amount: U256,
- ) -> Result<bool> {
+ fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let to = to.into_sub_cross_account::<T>()?;
let amount = amount.try_into().map_err(|_| "amount overflow")?;
@@ -313,8 +300,8 @@
fn transfer_from_cross(
&mut self,
caller: Caller,
- from: pallet_common::eth::CrossAddress,
- to: pallet_common::eth::CrossAddress,
+ from: CrossAddress,
+ to: CrossAddress,
amount: U256,
) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -165,8 +165,8 @@
pub type Allowance<T: Config> = StorageNMap<
Key = (
Key<Twox64Concat, CollectionId>,
- Key<Blake2_128, T::CrossAccountId>,
- Key<Blake2_128Concat, T::CrossAccountId>,
+ Key<Blake2_128, T::CrossAccountId>, // Owner
+ Key<Blake2_128Concat, T::CrossAccountId>, // Spender
),
Value = u128,
QueryKind = ValueQuery,
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/>.1617//! # Refungible Pallet EVM API for token pieces18//!19//! Provides ERC-20 standart support implementation and EVM API for unique extensions for Refungible Pallet.20//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods.2122use core::{23 char::{REPLACEMENT_CHARACTER, decode_utf16},24 convert::TryInto,25 ops::Deref,26};27use evm_coder::{28 abi::AbiType, ToLog, execution::*, generate_stubgen, solidity_interface, solidity, types::*,29 weight,30};31use pallet_common::{32 CommonWeightInfo,33 erc::{CommonEvmHandler, PrecompileResult},34 eth::collection_id_to_address,35};36use pallet_evm::{account::CrossAccountId, PrecompileHandle};37use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};38use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};39use sp_std::vec::Vec;40use sp_core::U256;41use up_data_structs::TokenId;4243use crate::{44 Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,45 TotalSupply, weights::WeightInfo,46};4748/// Refungible token handle contains information about token's collection and id49///50/// RefungibleTokenHandle doesn't check token's existance upon creation51pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);5253#[solidity_interface(name = ERC1633)]54impl<T: Config> RefungibleTokenHandle<T> {55 fn parent_token(&self) -> Result<Address> {56 Ok(collection_id_to_address(self.id))57 }5859 fn parent_token_id(&self) -> Result<U256> {60 Ok(self.1.into())61 }62}6364#[derive(ToLog)]65pub enum ERC20Events {66 /// @dev This event is emitted when the amount of tokens (value) is sent67 /// from the from address to the to address. In the case of minting new68 /// tokens, the transfer is usually from the 0 address while in the case69 /// of burning tokens the transfer is to 0.70 Transfer {71 #[indexed]72 from: Address,73 #[indexed]74 to: Address,75 value: U256,76 },77 /// @dev This event is emitted when the amount of tokens (value) is approved78 /// by the owner to be used by the spender.79 Approval {80 #[indexed]81 owner: Address,82 #[indexed]83 spender: Address,84 value: U256,85 },86}8788/// @title Standard ERC20 token89///90/// @dev Implementation of the basic standard token.91/// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md92#[solidity_interface(name = ERC20, events(ERC20Events))]93impl<T: Config> RefungibleTokenHandle<T> {94 /// @return the name of the token.95 fn name(&self) -> Result<String> {96 Ok(decode_utf16(self.name.iter().copied())97 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))98 .collect::<String>())99 }100101 /// @return the symbol of the token.102 fn symbol(&self) -> Result<String> {103 Ok(String::from_utf8_lossy(&self.token_prefix).into())104 }105106 /// @dev Total number of tokens in existence107 fn total_supply(&self) -> Result<U256> {108 self.consume_store_reads(1)?;109 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())110 }111112 /// @dev Not supported113 fn decimals(&self) -> Result<u8> {114 // Decimals aren't supported for refungible tokens115 Ok(0)116 }117118 /// @dev Gets the balance of the specified address.119 /// @param owner The address to query the balance of.120 /// @return An uint256 representing the amount owned by the passed address.121 fn balance_of(&self, owner: Address) -> Result<U256> {122 self.consume_store_reads(1)?;123 let owner = T::CrossAccountId::from_eth(owner);124 let balance = <Balance<T>>::get((self.id, self.1, owner));125 Ok(balance.into())126 }127128 /// @dev Transfer token for a specified address129 /// @param to The address to transfer to.130 /// @param amount The amount to be transferred.131 #[weight(<CommonWeights<T>>::transfer())]132 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {133 let caller = T::CrossAccountId::from_eth(caller);134 let to = T::CrossAccountId::from_eth(to);135 let amount = amount.try_into().map_err(|_| "amount overflow")?;136 let budget = self137 .recorder138 .weight_calls_budget(<StructureWeight<T>>::find_parent());139140 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)141 .map_err(dispatch_to_evm::<T>)?;142 Ok(true)143 }144145 /// @dev Transfer tokens from one address to another146 /// @param from address The address which you want to send tokens from147 /// @param to address The address which you want to transfer to148 /// @param amount uint256 the amount of tokens to be transferred149 #[weight(<CommonWeights<T>>::transfer_from())]150 fn transfer_from(151 &mut self,152 caller: Caller,153 from: Address,154 to: Address,155 amount: U256,156 ) -> Result<bool> {157 let caller = T::CrossAccountId::from_eth(caller);158 let from = T::CrossAccountId::from_eth(from);159 let to = T::CrossAccountId::from_eth(to);160 let amount = amount.try_into().map_err(|_| "amount overflow")?;161 let budget = self162 .recorder163 .weight_calls_budget(<StructureWeight<T>>::find_parent());164165 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)166 .map_err(dispatch_to_evm::<T>)?;167 Ok(true)168 }169170 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.171 /// Beware that changing an allowance with this method brings the risk that someone may use both the old172 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this173 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:174 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729175 /// @param spender The address which will spend the funds.176 /// @param amount The amount of tokens to be spent.177 #[weight(<SelfWeightOf<T>>::approve())]178 fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {179 let caller = T::CrossAccountId::from_eth(caller);180 let spender = T::CrossAccountId::from_eth(spender);181 let amount = amount.try_into().map_err(|_| "amount overflow")?;182183 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)184 .map_err(dispatch_to_evm::<T>)?;185 Ok(true)186 }187188 /// @dev Function to check the amount of tokens that an owner allowed to a spender.189 /// @param owner address The address which owns the funds.190 /// @param spender address The address which will spend the funds.191 /// @return A uint256 specifying the amount of tokens still available for the spender.192 fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {193 self.consume_store_reads(1)?;194 let owner = T::CrossAccountId::from_eth(owner);195 let spender = T::CrossAccountId::from_eth(spender);196197 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())198 }199}200201#[solidity_interface(name = ERC20UniqueExtensions)]202impl<T: Config> RefungibleTokenHandle<T>203where204 T::AccountId: From<[u8; 32]>,205{206 /// @dev Function to check the amount of tokens that an owner allowed to a spender.207 /// @param owner crossAddress The address which owns the funds.208 /// @param spender crossAddress The address which will spend the funds.209 /// @return A uint256 specifying the amount of tokens still available for the spender.210 fn allowance_cross(211 &self,212 owner: pallet_common::eth::CrossAddress,213 spender: pallet_common::eth::CrossAddress,214 ) -> Result<U256> {215 let owner = owner.into_sub_cross_account::<T>()?;216 let spender = spender.into_sub_cross_account::<T>()?;217218 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())219 }220221 /// @dev Function that burns an amount of the token of a given account,222 /// deducting from the sender's allowance for said account.223 /// @param from The account whose tokens will be burnt.224 /// @param amount The amount that will be burnt.225 #[weight(<SelfWeightOf<T>>::burn_from())]226 #[solidity(hide)]227 fn burn_from(&mut self, caller: Caller, from: Address, amount: U256) -> Result<bool> {228 let caller = T::CrossAccountId::from_eth(caller);229 let from = T::CrossAccountId::from_eth(from);230 let amount = amount.try_into().map_err(|_| "amount overflow")?;231 let budget = self232 .recorder233 .weight_calls_budget(<StructureWeight<T>>::find_parent());234235 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)236 .map_err(dispatch_to_evm::<T>)?;237 Ok(true)238 }239240 /// @dev Function that burns an amount of the token of a given account,241 /// deducting from the sender's allowance for said account.242 /// @param from The account whose tokens will be burnt.243 /// @param amount The amount that will be burnt.244 #[weight(<SelfWeightOf<T>>::burn_from())]245 fn burn_from_cross(246 &mut self,247 caller: Caller,248 from: pallet_common::eth::CrossAddress,249 amount: U256,250 ) -> Result<bool> {251 let caller = T::CrossAccountId::from_eth(caller);252 let from = from.into_sub_cross_account::<T>()?;253 let amount = amount.try_into().map_err(|_| "amount overflow")?;254 let budget = self255 .recorder256 .weight_calls_budget(<StructureWeight<T>>::find_parent());257258 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)259 .map_err(dispatch_to_evm::<T>)?;260 Ok(true)261 }262263 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.264 /// Beware that changing an allowance with this method brings the risk that someone may use both the old265 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this266 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:267 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729268 /// @param spender The crossaccount which will spend the funds.269 /// @param amount The amount of tokens to be spent.270 #[weight(<SelfWeightOf<T>>::approve())]271 fn approve_cross(272 &mut self,273 caller: Caller,274 spender: pallet_common::eth::CrossAddress,275 amount: U256,276 ) -> Result<bool> {277 let caller = T::CrossAccountId::from_eth(caller);278 let spender = spender.into_sub_cross_account::<T>()?;279 let amount = amount.try_into().map_err(|_| "amount overflow")?;280281 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)282 .map_err(dispatch_to_evm::<T>)?;283 Ok(true)284 }285 /// @dev Function that changes total amount of the tokens.286 /// Throws if `msg.sender` doesn't owns all of the tokens.287 /// @param amount New total amount of the tokens.288 #[weight(<SelfWeightOf<T>>::repartition_item())]289 fn repartition(&mut self, caller: Caller, amount: U256) -> Result<bool> {290 let caller = T::CrossAccountId::from_eth(caller);291 let amount = amount.try_into().map_err(|_| "amount overflow")?;292293 <Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;294 Ok(true)295 }296297 /// @dev Transfer token for a specified address298 /// @param to The crossaccount to transfer to.299 /// @param amount The amount to be transferred.300 #[weight(<CommonWeights<T>>::transfer())]301 fn transfer_cross(302 &mut self,303 caller: Caller,304 to: pallet_common::eth::CrossAddress,305 amount: U256,306 ) -> Result<bool> {307 let caller = T::CrossAccountId::from_eth(caller);308 let to = to.into_sub_cross_account::<T>()?;309 let amount = amount.try_into().map_err(|_| "amount overflow")?;310 let budget = self311 .recorder312 .weight_calls_budget(<StructureWeight<T>>::find_parent());313314 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)315 .map_err(dispatch_to_evm::<T>)?;316 Ok(true)317 }318319 /// @dev Transfer tokens from one address to another320 /// @param from The address which you want to send tokens from321 /// @param to The address which you want to transfer to322 /// @param amount the amount of tokens to be transferred323 #[weight(<CommonWeights<T>>::transfer_from())]324 fn transfer_from_cross(325 &mut self,326 caller: Caller,327 from: pallet_common::eth::CrossAddress,328 to: pallet_common::eth::CrossAddress,329 amount: U256,330 ) -> Result<bool> {331 let caller = T::CrossAccountId::from_eth(caller);332 let from = from.into_sub_cross_account::<T>()?;333 let to = to.into_sub_cross_account::<T>()?;334 let amount = amount.try_into().map_err(|_| "amount overflow")?;335 let budget = self336 .recorder337 .weight_calls_budget(<StructureWeight<T>>::find_parent());338339 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)340 .map_err(dispatch_to_evm::<T>)?;341 Ok(true)342 }343}344345impl<T: Config> RefungibleTokenHandle<T> {346 pub fn into_inner(self) -> RefungibleHandle<T> {347 self.0348 }349 pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {350 &mut self.0351 }352}353354impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {355 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {356 self.0.recorder()357 }358 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {359 self.0.into_recorder()360 }361}362363impl<T: Config> Deref for RefungibleTokenHandle<T> {364 type Target = RefungibleHandle<T>;365366 fn deref(&self) -> &Self::Target {367 &self.0368 }369}370371#[solidity_interface(372 name = UniqueRefungibleToken,373 is(ERC20, ERC20UniqueExtensions, ERC1633)374)]375impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}376377generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);378generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);379380impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>381where382 T::AccountId: From<[u8; 32]>,383{384 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");385386 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {387 call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)388 }389}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/>.1617//! # Refungible Pallet EVM API for token pieces18//!19//! Provides ERC-20 standart support implementation and EVM API for unique extensions for Refungible Pallet.20//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods.2122use core::{23 char::{REPLACEMENT_CHARACTER, decode_utf16},24 convert::TryInto,25 ops::Deref,26};27use evm_coder::{28 abi::AbiType, ToLog, execution::*, generate_stubgen, solidity_interface, solidity, types::*,29 weight,30};31use pallet_common::{32 CommonWeightInfo,33 erc::{CommonEvmHandler, PrecompileResult},34 eth::{collection_id_to_address, CrossAddress},35};36use pallet_evm::{account::CrossAccountId, PrecompileHandle};37use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};38use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};39use sp_std::vec::Vec;40use sp_core::U256;41use up_data_structs::TokenId;4243use crate::{44 Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,45 TotalSupply, weights::WeightInfo,46};4748/// Refungible token handle contains information about token's collection and id49///50/// RefungibleTokenHandle doesn't check token's existance upon creation51pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);5253#[solidity_interface(name = ERC1633)]54impl<T: Config> RefungibleTokenHandle<T> {55 fn parent_token(&self) -> Result<Address> {56 Ok(collection_id_to_address(self.id))57 }5859 fn parent_token_id(&self) -> Result<U256> {60 Ok(self.1.into())61 }62}6364#[derive(ToLog)]65pub enum ERC20Events {66 /// @dev This event is emitted when the amount of tokens (value) is sent67 /// from the from address to the to address. In the case of minting new68 /// tokens, the transfer is usually from the 0 address while in the case69 /// of burning tokens the transfer is to 0.70 Transfer {71 #[indexed]72 from: Address,73 #[indexed]74 to: Address,75 value: U256,76 },77 /// @dev This event is emitted when the amount of tokens (value) is approved78 /// by the owner to be used by the spender.79 Approval {80 #[indexed]81 owner: Address,82 #[indexed]83 spender: Address,84 value: U256,85 },86}8788/// @title Standard ERC20 token89///90/// @dev Implementation of the basic standard token.91/// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md92#[solidity_interface(name = ERC20, events(ERC20Events))]93impl<T: Config> RefungibleTokenHandle<T> {94 /// @return the name of the token.95 fn name(&self) -> Result<String> {96 Ok(decode_utf16(self.name.iter().copied())97 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))98 .collect::<String>())99 }100101 /// @return the symbol of the token.102 fn symbol(&self) -> Result<String> {103 Ok(String::from_utf8_lossy(&self.token_prefix).into())104 }105106 /// @dev Total number of tokens in existence107 fn total_supply(&self) -> Result<U256> {108 self.consume_store_reads(1)?;109 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())110 }111112 /// @dev Not supported113 fn decimals(&self) -> Result<u8> {114 // Decimals aren't supported for refungible tokens115 Ok(0)116 }117118 /// @dev Gets the balance of the specified address.119 /// @param owner The address to query the balance of.120 /// @return An uint256 representing the amount owned by the passed address.121 fn balance_of(&self, owner: Address) -> Result<U256> {122 self.consume_store_reads(1)?;123 let owner = T::CrossAccountId::from_eth(owner);124 let balance = <Balance<T>>::get((self.id, self.1, owner));125 Ok(balance.into())126 }127128 /// @dev Transfer token for a specified address129 /// @param to The address to transfer to.130 /// @param amount The amount to be transferred.131 #[weight(<CommonWeights<T>>::transfer())]132 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {133 let caller = T::CrossAccountId::from_eth(caller);134 let to = T::CrossAccountId::from_eth(to);135 let amount = amount.try_into().map_err(|_| "amount overflow")?;136 let budget = self137 .recorder138 .weight_calls_budget(<StructureWeight<T>>::find_parent());139140 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)141 .map_err(dispatch_to_evm::<T>)?;142 Ok(true)143 }144145 /// @dev Transfer tokens from one address to another146 /// @param from address The address which you want to send tokens from147 /// @param to address The address which you want to transfer to148 /// @param amount uint256 the amount of tokens to be transferred149 #[weight(<CommonWeights<T>>::transfer_from())]150 fn transfer_from(151 &mut self,152 caller: Caller,153 from: Address,154 to: Address,155 amount: U256,156 ) -> Result<bool> {157 let caller = T::CrossAccountId::from_eth(caller);158 let from = T::CrossAccountId::from_eth(from);159 let to = T::CrossAccountId::from_eth(to);160 let amount = amount.try_into().map_err(|_| "amount overflow")?;161 let budget = self162 .recorder163 .weight_calls_budget(<StructureWeight<T>>::find_parent());164165 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)166 .map_err(dispatch_to_evm::<T>)?;167 Ok(true)168 }169170 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.171 /// Beware that changing an allowance with this method brings the risk that someone may use both the old172 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this173 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:174 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729175 /// @param spender The address which will spend the funds.176 /// @param amount The amount of tokens to be spent.177 #[weight(<SelfWeightOf<T>>::approve())]178 fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {179 let caller = T::CrossAccountId::from_eth(caller);180 let spender = T::CrossAccountId::from_eth(spender);181 let amount = amount.try_into().map_err(|_| "amount overflow")?;182183 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)184 .map_err(dispatch_to_evm::<T>)?;185 Ok(true)186 }187188 /// @dev Function to check the amount of tokens that an owner allowed to a spender.189 /// @param owner address The address which owns the funds.190 /// @param spender address The address which will spend the funds.191 /// @return A uint256 specifying the amount of tokens still available for the spender.192 fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {193 self.consume_store_reads(1)?;194 let owner = T::CrossAccountId::from_eth(owner);195 let spender = T::CrossAccountId::from_eth(spender);196197 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())198 }199}200201#[solidity_interface(name = ERC20UniqueExtensions)]202impl<T: Config> RefungibleTokenHandle<T>203where204 T::AccountId: From<[u8; 32]>,205{206 /// @dev Function to check the amount of tokens that an owner allowed to a spender.207 /// @param owner crossAddress The address which owns the funds.208 /// @param spender crossAddress The address which will spend the funds.209 /// @return A uint256 specifying the amount of tokens still available for the spender.210 fn allowance_cross(&self, owner: CrossAddress, spender: CrossAddress) -> Result<U256> {211 let owner = owner.into_sub_cross_account::<T>()?;212 let spender = spender.into_sub_cross_account::<T>()?;213214 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())215 }216217 /// @dev Function that burns an amount of the token of a given account,218 /// deducting from the sender's allowance for said account.219 /// @param from The account whose tokens will be burnt.220 /// @param amount The amount that will be burnt.221 #[weight(<SelfWeightOf<T>>::burn_from())]222 #[solidity(hide)]223 fn burn_from(&mut self, caller: Caller, from: Address, amount: U256) -> Result<bool> {224 let caller = T::CrossAccountId::from_eth(caller);225 let from = T::CrossAccountId::from_eth(from);226 let amount = amount.try_into().map_err(|_| "amount overflow")?;227 let budget = self228 .recorder229 .weight_calls_budget(<StructureWeight<T>>::find_parent());230231 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)232 .map_err(dispatch_to_evm::<T>)?;233 Ok(true)234 }235236 /// @dev Function that burns an amount of the token of a given account,237 /// deducting from the sender's allowance for said account.238 /// @param from The account whose tokens will be burnt.239 /// @param amount The amount that will be burnt.240 #[weight(<SelfWeightOf<T>>::burn_from())]241 fn burn_from_cross(242 &mut self,243 caller: Caller,244 from: CrossAddress,245 amount: U256,246 ) -> Result<bool> {247 let caller = T::CrossAccountId::from_eth(caller);248 let from = from.into_sub_cross_account::<T>()?;249 let amount = amount.try_into().map_err(|_| "amount overflow")?;250 let budget = self251 .recorder252 .weight_calls_budget(<StructureWeight<T>>::find_parent());253254 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)255 .map_err(dispatch_to_evm::<T>)?;256 Ok(true)257 }258259 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.260 /// Beware that changing an allowance with this method brings the risk that someone may use both the old261 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this262 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:263 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729264 /// @param spender The crossaccount which will spend the funds.265 /// @param amount The amount of tokens to be spent.266 #[weight(<SelfWeightOf<T>>::approve())]267 fn approve_cross(268 &mut self,269 caller: Caller,270 spender: CrossAddress,271 amount: U256,272 ) -> Result<bool> {273 let caller = T::CrossAccountId::from_eth(caller);274 let spender = spender.into_sub_cross_account::<T>()?;275 let amount = amount.try_into().map_err(|_| "amount overflow")?;276277 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)278 .map_err(dispatch_to_evm::<T>)?;279 Ok(true)280 }281 /// @dev Function that changes total amount of the tokens.282 /// Throws if `msg.sender` doesn't owns all of the tokens.283 /// @param amount New total amount of the tokens.284 #[weight(<SelfWeightOf<T>>::repartition_item())]285 fn repartition(&mut self, caller: Caller, amount: U256) -> Result<bool> {286 let caller = T::CrossAccountId::from_eth(caller);287 let amount = amount.try_into().map_err(|_| "amount overflow")?;288289 <Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;290 Ok(true)291 }292293 /// @dev Transfer token for a specified address294 /// @param to The crossaccount to transfer to.295 /// @param amount The amount to be transferred.296 #[weight(<CommonWeights<T>>::transfer())]297 fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {298 let caller = T::CrossAccountId::from_eth(caller);299 let to = to.into_sub_cross_account::<T>()?;300 let amount = amount.try_into().map_err(|_| "amount overflow")?;301 let budget = self302 .recorder303 .weight_calls_budget(<StructureWeight<T>>::find_parent());304305 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)306 .map_err(dispatch_to_evm::<T>)?;307 Ok(true)308 }309310 /// @dev Transfer tokens from one address to another311 /// @param from The address which you want to send tokens from312 /// @param to The address which you want to transfer to313 /// @param amount the amount of tokens to be transferred314 #[weight(<CommonWeights<T>>::transfer_from())]315 fn transfer_from_cross(316 &mut self,317 caller: Caller,318 from: CrossAddress,319 to: CrossAddress,320 amount: U256,321 ) -> Result<bool> {322 let caller = T::CrossAccountId::from_eth(caller);323 let from = from.into_sub_cross_account::<T>()?;324 let to = to.into_sub_cross_account::<T>()?;325 let amount = amount.try_into().map_err(|_| "amount overflow")?;326 let budget = self327 .recorder328 .weight_calls_budget(<StructureWeight<T>>::find_parent());329330 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)331 .map_err(dispatch_to_evm::<T>)?;332 Ok(true)333 }334}335336impl<T: Config> RefungibleTokenHandle<T> {337 pub fn into_inner(self) -> RefungibleHandle<T> {338 self.0339 }340 pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {341 &mut self.0342 }343}344345impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {346 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {347 self.0.recorder()348 }349 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {350 self.0.into_recorder()351 }352}353354impl<T: Config> Deref for RefungibleTokenHandle<T> {355 type Target = RefungibleHandle<T>;356357 fn deref(&self) -> &Self::Target {358 &self.0359 }360}361362#[solidity_interface(363 name = UniqueRefungibleToken,364 is(ERC20, ERC20UniqueExtensions, ERC1633)365)]366impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}367368generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);369generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);370371impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>372where373 T::AccountId: From<[u8; 32]>,374{375 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");376377 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {378 call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)379 }380}pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -240,13 +240,13 @@
QueryKind = ValueQuery,
>;
- /// Operator set by a wallet owner that could perform certain transactions on all tokens in the wallet.
+ /// Spender set by a wallet owner that could perform certain transactions on all tokens in the wallet.
#[pallet::storage]
pub type CollectionAllowance<T: Config> = StorageNMap<
Key = (
Key<Twox64Concat, CollectionId>,
Key<Blake2_128Concat, T::CrossAccountId>, // Owner
- Key<Blake2_128Concat, T::CrossAccountId>, // Operator
+ Key<Blake2_128Concat, T::CrossAccountId>, // Spender
),
Value = bool,
QueryKind = ValueQuery,
@@ -1403,18 +1403,18 @@
pub fn set_allowance_for_all(
collection: &RefungibleHandle<T>,
owner: &T::CrossAccountId,
- operator: &T::CrossAccountId,
+ spender: &T::CrossAccountId,
approve: bool,
) -> DispatchResult {
<PalletCommon<T>>::set_allowance_for_all(
collection,
owner,
- operator,
+ spender,
approve,
- || <CollectionAllowance<T>>::insert((collection.id, owner, operator), approve),
+ || <CollectionAllowance<T>>::insert((collection.id, owner, spender), approve),
ERC721Events::ApprovalForAll {
owner: *owner.as_eth(),
- operator: *operator.as_eth(),
+ operator: *spender.as_eth(),
approved: approve,
}
.to_log(collection_id_to_address(collection.id)),
@@ -1425,9 +1425,9 @@
pub fn allowance_for_all(
collection: &RefungibleHandle<T>,
owner: &T::CrossAccountId,
- operator: &T::CrossAccountId,
+ spender: &T::CrossAccountId,
) -> bool {
- <CollectionAllowance<T>>::get((collection.id, owner, operator))
+ <CollectionAllowance<T>>::get((collection.id, owner, spender))
}
pub fn repair_item(collection: &RefungibleHandle<T>, token: TokenId) -> DispatchResult {