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 check_is_internal(&self) -> DispatchResult {25 Ok(())26 }27}2829impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {30 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {31 &self.032 }33 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {34 self.035 }36}3738impl<T: Config> Deref for NativeFungibleHandle<T> {39 type Target = SubstrateRecorder<T>;4041 fn deref(&self) -> &Self::Target {42 &self.043 }44}45#[frame_support::pallet]46pub mod pallet {47 use super::*;48 use alloc::string::String;49 use frame_support::{50 dispatch::PostDispatchInfo,51 ensure,52 pallet_prelude::{DispatchResultWithPostInfo, Pays},53 traits::{Currency, ExistenceRequirement, Get},54 };55 use pallet_balances::WeightInfo;56 use pallet_common::{57 erc::CrossAccountId, Error as CommonError, Pallet as PalletCommon,58 NATIVE_FUNGIBLE_COLLECTION_ID,59 };60 use pallet_structure::Pallet as PalletStructure;61 use sp_core::U256;62 use sp_runtime::DispatchError;63 use up_data_structs::{budget::Budget, mapping::TokenAddressMapping, TokenId};6465 #[pallet::config]66 pub trait Config:67 frame_system::Config68 + pallet_evm_coder_substrate::Config69 + pallet_common::Config70 + pallet_structure::Config71 {72 73 type Currency: frame_support::traits::Currency<74 Self::AccountId,75 Balance = Self::CurrencyBalance,76 >;77 78 type CurrencyBalance: Into<U256> + TryFrom<U256> + TryFrom<u128> + Into<u128>;7980 81 type Decimals: Get<u8>;82 83 type Name: Get<String>;84 85 type Symbol: Get<String>;8687 88 type WeightInfo: WeightInfo;89 }90 #[pallet::pallet]91 pub struct Pallet<T>(_);9293 impl<T: Config> Pallet<T> {94 95 96 97 98 99 100 fn check_allowed(101 spender: &T::CrossAccountId,102 from: &T::CrossAccountId,103 nesting_budget: &dyn Budget,104 ) -> Result<u128, DispatchError> {105 if let Some((collection_id, token_id)) =106 T::CrossTokenAddressMapping::address_to_token(from)107 {108 ensure!(109 <PalletStructure<T>>::check_indirectly_owned(110 spender.clone(),111 collection_id,112 token_id,113 None,114 nesting_budget115 )?,116 <CommonError<T>>::ApprovedValueTooLow,117 );118 } else if !spender.conv_eq(from) {119 return Ok(0);120 }121122 Ok(<T as Config>::Currency::free_balance(from.as_sub()).into())123 }124125 126 127 128 129 130 131 132 133 pub fn transfer(134 _collection: &NativeFungibleHandle<T>,135 from: &T::CrossAccountId,136 to: &T::CrossAccountId,137 amount: u128,138 nesting_budget: &dyn Budget,139 ) -> DispatchResultWithPostInfo {140 <PalletCommon<T>>::ensure_correct_receiver(to)?;141142 if from != to && amount != 0 {143 <T as Config>::Currency::transfer(144 from.as_sub(),145 to.as_sub(),146 amount147 .try_into()148 .map_err(|_| sp_runtime::ArithmeticError::Overflow)?,149 ExistenceRequirement::AllowDeath,150 )?;151152 <PalletStructure<T>>::nest_if_sent_to_token(153 from.clone(),154 to,155 NATIVE_FUNGIBLE_COLLECTION_ID,156 TokenId::default(),157 nesting_budget,158 )?;159160 let balance_from: u128 =161 <T as Config>::Currency::free_balance(from.as_sub()).into();162 if balance_from == 0 {163 <PalletStructure<T>>::unnest_if_nested(164 from,165 NATIVE_FUNGIBLE_COLLECTION_ID,166 TokenId::default(),167 );168 }169 };170171 Ok(PostDispatchInfo {172 actual_weight: Some(<SelfWeightOf<T>>::transfer()),173 pays_fee: Pays::Yes,174 })175 }176177 178 179 180 181 182 183 184 185 186 187 188 pub fn transfer_from(189 collection: &NativeFungibleHandle<T>,190 spender: &T::CrossAccountId,191 from: &T::CrossAccountId,192 to: &T::CrossAccountId,193 amount: u128,194 nesting_budget: &dyn Budget,195 ) -> DispatchResultWithPostInfo {196 let allowance = Self::check_allowed(spender, from, nesting_budget)?;197 if allowance < amount {198 return Err(<CommonError<T>>::ApprovedValueTooLow.into());199 }200 Self::transfer(collection, from, to, amount, nesting_budget)201 }202 }203}