Skip to content

Commit

Permalink
feat: update CE interface with is_mapped value
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Oct 23, 2023
1 parent 66c6235 commit 7a46c49
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions chain-extensions/unified-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ log = { workspace = true }
num-traits = { workspace = true }
pallet-contracts = { workspace = true }
pallet-contracts-primitives = { workspace = true }
pallet-evm = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-core = { workspace = true }
Expand All @@ -33,6 +34,7 @@ std = [
"frame-system/std",
"num-traits/std",
"pallet-contracts/std",
"pallet-evm/std",
"pallet-contracts-primitives/std",
"scale-info/std",
"sp-std/std",
Expand Down
31 changes: 27 additions & 4 deletions chain-extensions/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@

#![cfg_attr(not(feature = "std"), no_std)]

use astar_primitives::evm::{EvmAddress, UnifiedAddressMapper};
use astar_primitives::{
ethereum_checked::AccountMapping,
evm::{EvmAddress, UnifiedAddressMapper},
};
use core::marker::PhantomData;
use sp_runtime::DispatchError;

use frame_support::{traits::Get, DefaultNoBound};
use pallet_contracts::chain_extension::{
ChainExtension, Environment, Ext, InitState, Result as DispatchResult, RetVal,
};
use pallet_evm::AddressMapping;
use pallet_unified_accounts::{EvmToNative, NativeToEvm};
use parity_scale_codec::Encode;
pub use unified_accounts_chain_extension_types::Command::{self, *};

Expand Down Expand Up @@ -58,8 +63,17 @@ where

let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;

// read the storage item
let mapped = NativeToEvm::<T>::get(account_id.clone());

let is_mapped = mapped.is_some();
let evm_address = mapped.unwrap_or_else(|| {
// fallback to default account_id
T::DefaultNativeToEvm::into_h160(account_id)
});
// write to buffer
UA::to_h160_or_default(&account_id).using_encoded(|r| env.write(r, false, None))?;
(evm_address, is_mapped).using_encoded(|r| env.write(r, false, None))?;
}
GetNativeAddress => {
let evm_address: EvmAddress = env.read_as()?;
Expand All @@ -74,9 +88,18 @@ where

let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;

// read the storage item
let mapped = EvmToNative::<T>::get(evm_address.clone());

let is_mapped = mapped.is_some();
let native_address = mapped.unwrap_or_else(|| {
// fallback to default evm_address
T::DefaultEvmToNative::into_account_id(evm_address)
});

// write to buffer
UA::to_account_id_or_default(&evm_address)
.using_encoded(|r| env.write(r, false, None))?;
(native_address, is_mapped).using_encoded(|r| env.write(r, false, None))?;
}
};
Ok(RetVal::Converging(0))
Expand Down
Binary file modified tests/ink-contracts/au_ce_getters.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions tests/integration/src/unified_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ fn unified_accounts_chain_extension_works() {
);
// default h160 address should match
assert_eq!(
call_wasm_contract_method::<H160>(
call_wasm_contract_method::<(H160, bool)>(
ALICE,
contract_id.clone(),
[GET_H160_OR_DEFAULT.to_vec(), ALICE.encode()].concat()
),
UnifiedAccounts::to_h160_or_default(&ALICE)
(UnifiedAccounts::to_h160_or_default(&ALICE), false)
);
// mapped native address should be None
assert_eq!(
Expand All @@ -82,12 +82,12 @@ fn unified_accounts_chain_extension_works() {
);
// default native address should match
assert_eq!(
call_wasm_contract_method::<AccountId>(
call_wasm_contract_method::<(AccountId, bool)>(
ALICE,
contract_id.clone(),
[GET_NATIVE_OR_DEFAULT.to_vec(), alith().encode()].concat()
),
UnifiedAccounts::to_account_id_or_default(&alith())
(UnifiedAccounts::to_account_id_or_default(&alith()), false)
);

//
Expand Down

0 comments on commit 7a46c49

Please sign in to comment.