difftreelog
feat impl approve, allowance, balance_of
in: master
3 files changed
pallets/balances-adapter/src/erc.rsdiffbeforeafterboth1use crate::Config;2use evm_coder::{abi::AbiType, AbiCoder, ToLog, generate_stubgen, solidity_interface, types::*};3use pallet_common::erc::{CommonEvmHandler, PrecompileHandle, PrecompileResult};4use pallet_evm_coder_substrate::{5 call, dispatch_to_evm,6 execution::{PreDispatch, Result},7 frontier_contract, WithRecorder, SubstrateRecorder,8};9use sp_core::{U256};10use sp_std::vec::Vec;1112frontier_contract! {13 macro_rules! NativeFungibleHandle_result {...}14 impl<T: Config> Contract for NativeFungibleHandle<T> {...}15}1617#[derive(ToLog)]18pub enum ERC20Events {19 Transfer {20 #[indexed]21 from: Address,22 #[indexed]23 to: Address,24 value: U256,25 },26 Approval {27 #[indexed]28 owner: Address,29 #[indexed]30 spender: Address,31 value: U256,32 },33}3435pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);3637impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {38 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {39 &self.040 }41 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {42 self.043 }44}4546#[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]47impl<T: Config> NativeFungibleHandle<T> {48 fn name(&self) -> Result<String> {49 // Ok(decode_utf16(self.name.iter().copied())50 // .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))51 // .collect::<String>())52 todo!()53 }5455 fn symbol(&self) -> Result<String> {56 // Ok(String::from_utf8_lossy(&self.token_prefix).into())57 todo!()58 }59 fn total_supply(&self) -> Result<U256> {60 // self.consume_store_reads(1)?;61 // Ok(<TotalSupply<T>>::get(self.id).into())62 todo!()63 }6465 fn decimals(&self) -> Result<u8> {66 // Ok(if let CollectionMode::Fungible(decimals) = &self.mode {67 // *decimals68 // } else {69 // unreachable!()70 // })71 todo!()72 }7374 fn balance_of(&self, owner: Address) -> Result<U256> {75 // self.consume_store_reads(1)?;76 // let owner = T::CrossAccountId::from_eth(owner);77 // let balance = <Balance<T>>::get((self.id, owner));78 // Ok(balance.into())79 todo!()80 }8182 // #[weight(<SelfWeightOf<T>>::transfer())]83 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {84 // let caller = T::CrossAccountId::from_eth(caller);85 // let to = T::CrossAccountId::from_eth(to);86 // let amount = amount.try_into().map_err(|_| "amount overflow")?;87 // let budget = self88 // .recorder89 // .weight_calls_budget(<StructureWeight<T>>::find_parent());9091 // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;92 // Ok(true)93 todo!()94 }9596 // #[weight(<SelfWeightOf<T>>::transfer_from())]97 fn transfer_from(98 &mut self,99 caller: Caller,100 from: Address,101 to: Address,102 amount: U256,103 ) -> Result<bool> {104 // let caller = T::CrossAccountId::from_eth(caller);105 // let from = T::CrossAccountId::from_eth(from);106 // let to = T::CrossAccountId::from_eth(to);107 // let amount = amount.try_into().map_err(|_| "amount overflow")?;108 // let budget = self109 // .recorder110 // .weight_calls_budget(<StructureWeight<T>>::find_parent());111112 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)113 // .map_err(dispatch_to_evm::<T>)?;114 // Ok(true)115 todo!()116 }117118 // #[weight(<SelfWeightOf<T>>::approve())]119 fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> 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, amount)125 // .map_err(dispatch_to_evm::<T>)?;126 // Ok(true)127 todo!()128 }129130 fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {131 // self.consume_store_reads(1)?;132 // let owner = T::CrossAccountId::from_eth(owner);133 // let spender = T::CrossAccountId::from_eth(spender);134135 // Ok(<Allowance<T>>::get((self.id, owner, spender)).into())136 todo!()137 }138}139140#[solidity_interface(141 name = UniqueNativeFungible,142 is(ERC20),143 enum(derive(PreDispatch))144)]145impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}146147generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);148generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);149150impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>151where152 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,153{154 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");155156 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {157 call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)158 }159}pallets/balances-adapter/src/lib.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -8,8 +8,16 @@
#[frame_support::pallet]
pub mod pallet {
+ use sp_core::U256;
+
#[pallet::config]
- pub trait Config: frame_system::Config + pallet_evm_coder_substrate::Config {}
+ pub trait Config: frame_system::Config + pallet_evm_coder_substrate::Config {
+ type Currency: frame_support::traits::Currency<
+ Self::AccountId,
+ Balance = Self::CurrencyBalance,
+ >;
+ type CurrencyBalance: Into<U256>;
+ }
#[pallet::pallet]
pub struct Pallet<T>(_);
runtime/common/config/pallets/mod.rsdiffbeforeafterboth--- a/runtime/common/config/pallets/mod.rs
+++ b/runtime/common/config/pallets/mod.rs
@@ -25,7 +25,7 @@
},
Runtime, RuntimeEvent, RuntimeCall, RuntimeOrigin, Balances,
};
-use frame_support::traits::{ConstU32, ConstU64};
+use frame_support::traits::{ConstU32, ConstU64, Currency};
use up_common::{
types::{AccountId, Balance, BlockNumber},
constants::*,
@@ -84,7 +84,8 @@
type WeightInfo = pallet_nonfungible::weights::SubstrateWeight<Self>;
}
impl pallet_balances_adapter::Config for Runtime {
- // type WeightInfo = pallet_nonfungible::weights::SubstrateWeight<Self>;
+ type Currency = Balances;
+ type CurrencyBalance = <Balances as Currency<Self::AccountId>>::Balance;
}
parameter_types! {