pallet_chain_extension_unified_accounts/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
20
21use astar_primitives::evm::{EvmAddress, UnifiedAddressMapper};
22use core::marker::PhantomData;
23use sp_runtime::DispatchError;
24
25use frame_support::DefaultNoBound;
26use pallet_contracts::chain_extension::{
27 ChainExtension, Environment, Ext, InitState, Result as DispatchResult, RetVal,
28};
29use pallet_unified_accounts::WeightInfo;
30use parity_scale_codec::Encode;
31pub use unified_accounts_chain_extension_types::Command::{self, *};
32
33type UAWeight<T> = <T as pallet_unified_accounts::Config>::WeightInfo;
34
35#[derive(DefaultNoBound)]
36pub struct UnifiedAccountsExtension<T, UA>(PhantomData<(T, UA)>);
37
38impl<T, UA> ChainExtension<T> for UnifiedAccountsExtension<T, UA>
39where
40 T: pallet_contracts::Config + pallet_unified_accounts::Config,
41 UA: UnifiedAddressMapper<T::AccountId>,
42{
43 fn call<E>(&mut self, env: Environment<E, InitState>) -> DispatchResult<RetVal>
44 where
45 E: Ext<T = T>,
46 {
47 let mut env = env.buf_in_buf_out();
48 match env.func_id().try_into().map_err(|_| {
49 DispatchError::Other("Unsupported func id in Unified Accounts Chain Extension")
50 })? {
51 GetEvmAddress => {
52 let account_id: T::AccountId = env.read_as()?;
53 env.charge_weight(UAWeight::<T>::to_h160())?;
55 UA::to_h160(&account_id).using_encoded(|r| env.write(r, false, None))?;
57 }
58 GetEvmAddressOrDefault => {
59 let account_id: T::AccountId = env.read_as()?;
60 env.charge_weight(UAWeight::<T>::to_h160_or_default())?;
62
63 UA::to_h160_or_default(&account_id).using_encoded(|r| env.write(r, false, None))?;
65 }
66 GetNativeAddress => {
67 let evm_address: EvmAddress = env.read_as()?;
68 env.charge_weight(UAWeight::<T>::to_account_id())?;
70 UA::to_account_id(&evm_address).using_encoded(|r| env.write(r, false, None))?;
72 }
73 GetNativeAddressOrDefault => {
74 let evm_address: EvmAddress = env.read_as()?;
75 env.charge_weight(UAWeight::<T>::to_account_id_or_default())?;
77
78 UA::to_account_id_or_default(&evm_address)
80 .using_encoded(|r| env.write(r, false, None))?;
81 }
82 };
83 Ok(RetVal::Converging(0))
84 }
85}