difftreelog
feat impl decimals, name, symbol
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 frame_support::traits::Currency;4use pallet_common::erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult};5use pallet_evm_coder_substrate::{6 call, dispatch_to_evm,7 execution::{PreDispatch, Result},8 frontier_contract, WithRecorder, SubstrateRecorder,9};10use sp_core::{U256};11use sp_std::vec::Vec;1213frontier_contract! {14 macro_rules! NativeFungibleHandle_result {...}15 impl<T: Config> Contract for NativeFungibleHandle<T> {...}16}1718#[derive(ToLog)]19pub enum ERC20Events {20 Transfer {21 #[indexed]22 from: Address,23 #[indexed]24 to: Address,25 value: U256,26 },27 Approval {28 #[indexed]29 owner: Address,30 #[indexed]31 spender: Address,32 value: U256,33 },34}3536pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);3738impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {39 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {40 &self.041 }42 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {43 self.044 }45}4647#[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]48impl<T: Config> NativeFungibleHandle<T> {49 fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {50 Ok(U256::zero())51 }5253 // #[weight(<SelfWeightOf<T>>::approve())]54 fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {55 // self.consume_store_reads(1)?;56 Err("Approve not supported now".into())57 }58 fn balance_of(&self, owner: Address) -> Result<U256> {59 // self.consume_store_reads(1)?;60 let owner = T::CrossAccountId::from_eth(owner);61 let a = <T as Config>::Currency::free_balance(owner.as_sub());62 Ok(a.into())63 }6465 fn decimals(&self) -> Result<u8> {66 // Ok(if let CollectionMode::Fungible(decimals) = &self.mode {67 // *decimals68 // } else {69 // unreachable!70 // })7172 // From config 1873 todo!()74 }7576 fn name(&self) -> Result<String> {77 // Ok(decode_utf16(self.name.iter().copied())78 // .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))79 // .collect::<String>())80 todo!()81 }8283 fn symbol(&self) -> Result<String> {84 // Ok(String::from_utf8_lossy(&self.token_prefix).into())85 todo!()86 }8788 fn total_supply(&self) -> Result<U256> {89 // self.consume_store_reads(1)?;90 // Ok(<TotalSupply<T>>::get(self.id).into())91 todo!()92 }9394 // #[weight(<SelfWeightOf<T>>::transfer())]95 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {96 // let caller = T::CrossAccountId::from_eth(caller);97 // let to = T::CrossAccountId::from_eth(to);98 // let amount = amount.try_into().map_err(|_| "amount overflow")?;99 // let budget = self100 // .recorder101 // .weight_calls_budget(<StructureWeight<T>>::find_parent());102103 // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;104 // Ok(true)105 todo!()106 }107108 // #[weight(<SelfWeightOf<T>>::transfer_from())]109 fn transfer_from(110 &mut self,111 caller: Caller,112 from: Address,113 to: Address,114 amount: U256,115 ) -> Result<bool> {116 // let caller = T::CrossAccountId::from_eth(caller);117 // let from = T::CrossAccountId::from_eth(from);118 // let to = T::CrossAccountId::from_eth(to);119 // let amount = amount.try_into().map_err(|_| "amount overflow")?;120 // let budget = self121 // .recorder122 // .weight_calls_budget(<StructureWeight<T>>::find_parent());123124 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)125 // .map_err(dispatch_to_evm::<T>)?;126 // Ok(true)127 todo!()128 }129}130131#[solidity_interface(132 name = UniqueNativeFungible,133 is(ERC20),134 enum(derive(PreDispatch))135)]136impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}137138generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);139generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);140141impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>142where143 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,144{145 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");146147 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {148 call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)149 }150}1use crate::Config;2use evm_coder::{abi::AbiType, AbiCoder, ToLog, generate_stubgen, solidity_interface, types::*};3use frame_support::traits::Currency;4use pallet_common::erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult};5use pallet_evm_coder_substrate::{6 call, dispatch_to_evm,7 execution::{PreDispatch, Result},8 frontier_contract, WithRecorder, SubstrateRecorder,9};10use sp_core::{U256, Get};11use sp_std::vec::Vec;1213frontier_contract! {14 macro_rules! NativeFungibleHandle_result {...}15 impl<T: Config> Contract for NativeFungibleHandle<T> {...}16}1718#[derive(ToLog)]19pub enum ERC20Events {20 Transfer {21 #[indexed]22 from: Address,23 #[indexed]24 to: Address,25 value: U256,26 },27 Approval {28 #[indexed]29 owner: Address,30 #[indexed]31 spender: Address,32 value: U256,33 },34}3536pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);3738impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {39 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {40 &self.041 }42 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {43 self.044 }45}4647#[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]48impl<T: Config> NativeFungibleHandle<T> {49 fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {50 Ok(U256::zero())51 }5253 // #[weight(<SelfWeightOf<T>>::approve())]54 fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {55 // self.consume_store_reads(1)?;56 Err("Approve not supported now".into())57 }58 fn balance_of(&self, owner: Address) -> Result<U256> {59 // self.consume_store_reads(1)?;60 let owner = T::CrossAccountId::from_eth(owner);61 let a = <T as Config>::Currency::free_balance(owner.as_sub());62 Ok(a.into())63 }6465 fn decimals(&self) -> Result<u8> {66 Ok(T::Decimals::get())67 }6869 fn name(&self) -> Result<String> {70 Ok(T::Name::get())71 }7273 fn symbol(&self) -> Result<String> {74 Ok(T::Symbol::get())75 }7677 fn total_supply(&self) -> Result<U256> {78 // self.consume_store_reads(1)?;79 // Ok(<TotalSupply<T>>::get(self.id).into())80 todo!()81 }8283 // #[weight(<SelfWeightOf<T>>::transfer())]84 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {85 // let caller = T::CrossAccountId::from_eth(caller);86 // let to = T::CrossAccountId::from_eth(to);87 // let amount = amount.try_into().map_err(|_| "amount overflow")?;88 // let budget = self89 // .recorder90 // .weight_calls_budget(<StructureWeight<T>>::find_parent());9192 // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;93 // Ok(true)94 todo!()95 }9697 // #[weight(<SelfWeightOf<T>>::transfer_from())]98 fn transfer_from(99 &mut self,100 caller: Caller,101 from: Address,102 to: Address,103 amount: U256,104 ) -> Result<bool> {105 // let caller = T::CrossAccountId::from_eth(caller);106 // let from = T::CrossAccountId::from_eth(from);107 // let to = T::CrossAccountId::from_eth(to);108 // let amount = amount.try_into().map_err(|_| "amount overflow")?;109 // let budget = self110 // .recorder111 // .weight_calls_budget(<StructureWeight<T>>::find_parent());112113 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)114 // .map_err(dispatch_to_evm::<T>)?;115 // Ok(true)116 todo!()117 }118}119120#[solidity_interface(121 name = UniqueNativeFungible,122 is(ERC20),123 enum(derive(PreDispatch))124)]125impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}126127generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);128generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);129130impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>131where132 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,133{134 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");135136 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {137 call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)138 }139}pallets/balances-adapter/src/lib.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -8,6 +8,7 @@
#[frame_support::pallet]
pub mod pallet {
+ use frame_support::traits::Get;
use sp_core::U256;
#[pallet::config]
@@ -17,6 +18,10 @@
Balance = Self::CurrencyBalance,
>;
type CurrencyBalance: Into<U256>;
+
+ type Decimals: Get<u8>;
+ type Name: Get<String>;
+ type Symbol: Get<String>;
}
#[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
@@ -23,7 +23,7 @@
weights::CommonWeights,
RelayChainBlockNumberProvider,
},
- Runtime, RuntimeEvent, RuntimeCall, RuntimeOrigin, Balances,
+ Runtime, RuntimeEvent, RuntimeCall, RuntimeOrigin, RUNTIME_NAME, TOKEN_SYMBOL, Balances,
};
use frame_support::traits::{ConstU32, ConstU64, Currency};
use up_common::{
@@ -51,8 +51,11 @@
pub mod preimage;
parameter_types! {
+ pub const CollectionCreationPrice: Balance = 2 * UNIQUE;
+ pub const Decimals: u8 = 32;
pub TreasuryAccountId: AccountId = TreasuryModuleId::get().into_account_truncating();
- pub const CollectionCreationPrice: Balance = 2 * UNIQUE;
+ pub Name: String = RUNTIME_NAME.to_string();
+ pub Symbol: String = TOKEN_SYMBOL.to_string();
}
impl pallet_common::Config for Runtime {
@@ -86,6 +89,9 @@
impl pallet_balances_adapter::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = <Balances as Currency<Self::AccountId>>::Balance;
+ type Decimals = Decimals;
+ type Name = Name;
+ type Symbol = Symbol;
}
parameter_types! {