1#![cfg_attr(not(feature = "std"), no_std)]23extern crate alloc;4use core::ops::Deref;56use frame_support::sp_runtime::DispatchResult;7use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};8pub use pallet::*;910pub mod common;11pub mod erc;1213pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;141516pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);17impl<T: Config> NativeFungibleHandle<T> {18 19 pub fn new() -> NativeFungibleHandle<T> {20 Self(SubstrateRecorder::new(u64::MAX))21 }2223 24 pub fn new_with_gas_limit(gas_limit: u64) -> NativeFungibleHandle<T> {25 Self(SubstrateRecorder::new(gas_limit))26 }2728 29 pub fn check_is_internal(&self) -> DispatchResult {30 Ok(())31 }32}3334impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {35 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {36 &self.037 }38 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {39 self.040 }41}4243impl<T: Config> Deref for NativeFungibleHandle<T> {44 type Target = SubstrateRecorder<T>;4546 fn deref(&self) -> &Self::Target {47 &self.048 }49}50#[frame_support::pallet]51pub mod pallet {52 use super::*;53 use alloc::string::String;54 use frame_support::{55 dispatch::PostDispatchInfo,56 ensure,57 pallet_prelude::{DispatchResultWithPostInfo, Pays},58 traits::{Currency, ExistenceRequirement, Get},59 };60 use pallet_balances::WeightInfo;61 use pallet_common::{erc::CrossAccountId, Error as CommonError, Pallet as PalletCommon};62 use pallet_structure::Pallet as PalletStructure;63 use sp_core::U256;64 use sp_runtime::DispatchError;65 use up_data_structs::{budget::Budget, mapping::TokenAddressMapping};6667 #[pallet::config]68 pub trait Config:69 frame_system::Config70 + pallet_evm_coder_substrate::Config71 + pallet_common::Config72 + pallet_structure::Config73 {74 75 type Currency: frame_support::traits::Currency<76 Self::AccountId,77 Balance = Self::CurrencyBalance,78 >;79 80 type CurrencyBalance: Into<U256> + TryFrom<U256> + TryFrom<u128> + Into<u128>;8182 83 type Decimals: Get<u8>;84 85 type Name: Get<String>;86 87 type Symbol: Get<String>;8889 90 type WeightInfo: WeightInfo;91 }92 #[pallet::pallet]93 pub struct Pallet<T>(_);9495 impl<T: Config> Pallet<T> {96 97 98 99 100 101 102 fn check_allowed(103 spender: &T::CrossAccountId,104 from: &T::CrossAccountId,105 nesting_budget: &dyn Budget,106 ) -> Result<u128, DispatchError> {107 if let Some((collection_id, token_id)) =108 T::CrossTokenAddressMapping::address_to_token(from)109 {110 ensure!(111 <PalletStructure<T>>::check_indirectly_owned(112 spender.clone(),113 collection_id,114 token_id,115 None,116 nesting_budget117 )?,118 <CommonError<T>>::ApprovedValueTooLow,119 );120 } else if !spender.conv_eq(from) {121 return Ok(0);122 }123124 Ok(<T as Config>::Currency::free_balance(from.as_sub()).into())125 }126127 128 129 130 131 132 133 134 135 pub fn transfer(136 _collection: &NativeFungibleHandle<T>,137 from: &T::CrossAccountId,138 to: &T::CrossAccountId,139 amount: u128,140 _nesting_budget: &dyn Budget,141 ) -> DispatchResultWithPostInfo {142 <PalletCommon<T>>::ensure_correct_receiver(to)?;143144 if from != to && amount != 0 {145 <T as Config>::Currency::transfer(146 from.as_sub(),147 to.as_sub(),148 amount149 .try_into()150 .map_err(|_| sp_runtime::ArithmeticError::Overflow)?,151 ExistenceRequirement::AllowDeath,152 )?;153 };154155 Ok(PostDispatchInfo {156 actual_weight: Some(<SelfWeightOf<T>>::transfer_allow_death()),157 pays_fee: Pays::Yes,158 })159 }160161 162 163 164 165 166 167 168 169 170 171 172 pub fn transfer_from(173 collection: &NativeFungibleHandle<T>,174 spender: &T::CrossAccountId,175 from: &T::CrossAccountId,176 to: &T::CrossAccountId,177 amount: u128,178 nesting_budget: &dyn Budget,179 ) -> DispatchResultWithPostInfo {180 let allowance = Self::check_allowed(spender, from, nesting_budget)?;181 if allowance < amount {182 return Err(<CommonError<T>>::ApprovedValueTooLow.into());183 }184 Self::transfer(collection, from, to, amount, nesting_budget)185 }186 }187}