Skip to content

Commit

Permalink
feat: use claim account in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Sep 25, 2023
1 parent 27ecc4c commit 434e940
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion tests/integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository.workspace = true

[dependencies]
hex = { workspace = true }
libsecp256k1 = { workspace = true, features = ["hmac", "static-context"] }
parity-scale-codec = { workspace = true }

# frontier
Expand All @@ -29,9 +30,9 @@ sp-io = { workspace = true }
sp-runtime = { workspace = true }

# astar dependencies
pallet-unified-accounts = { workspace = true }
pallet-ethereum-checked = { workspace = true }
pallet-evm-precompile-assets-erc20 = { workspace = true }
pallet-unified-accounts = { workspace = true }

astar-primitives = { workspace = true }
astar-runtime = { workspace = true, features = ["std"], optional = true }
Expand Down
36 changes: 28 additions & 8 deletions tests/integration/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,37 @@ pub use frame_support::{
weights::Weight,
};
pub use pallet_evm::AddressMapping;
pub use pallet_unified_accounts::UnifiedAddressMapper;
pub use sp_core::{H160, H256, U256};
pub use sp_io::hashing::keccak_256;
pub use sp_runtime::{AccountId32, MultiAddress};

pub use astar_primitives::ethereum_checked::AccountMapping;
pub use astar_primitives::{ethereum_checked::AccountMapping, evm::UnifiedAddressMapper};

#[cfg(feature = "shibuya")]
pub use shibuya::*;
#[cfg(feature = "shibuya")]
mod shibuya {
use super::*;
pub use pallet_unified_accounts::SignatureHelper;
pub use shibuya_runtime::*;

/// 1 SBY.
pub const UNIT: Balance = SBY;

pub fn alith_secret_key() -> libsecp256k1::SecretKey {
libsecp256k1::SecretKey::parse(&keccak_256(b"Alith")).unwrap()
}

/// H160 address mapped to `ALICE`.
pub fn alith() -> H160 {
h160_from(ALICE)
UnifiedAccounts::eth_address(&alith_secret_key())
}

/// Convert `H160` to `AccountId32`.
pub fn account_id_from(address: H160) -> AccountId32 {
<Runtime as pallet_evm::Config>::AddressMapping::into_account_id(address)
}

/// Convert `AccountId32` to `H160`.
pub fn h160_from(account_id: AccountId32) -> H160 {
<Runtime as pallet_ethereum_checked::Config>::AccountMapping::into_h160(account_id)
}

/// Deploy an EVM contract with code.
pub fn deploy_evm_contract(code: &str) -> H160 {
assert_ok!(EVM::create2(
Expand Down Expand Up @@ -106,6 +106,26 @@ mod shibuya {
address
}

/// Build the signature payload for given native account and eth private key
fn get_evm_signature(who: &AccountId32, secret: &libsecp256k1::SecretKey) -> [u8; 65] {
// sign the payload
UnifiedAccounts::eth_sign_prehash(
&<Runtime as pallet_unified_accounts::Config>::SignatureHelper::build_signing_payload(
who,
),
secret,
)
}

/// Create the mappings for the accounts
pub fn connect_accounts(who: &AccountId32, secret: &libsecp256k1::SecretKey) {
assert_ok!(UnifiedAccounts::claim_evm_address(
RuntimeOrigin::signed(who.clone()),
UnifiedAccounts::eth_address(secret),
get_evm_signature(who, secret)
));
}

pub fn claim_default_accounts(account: AccountId) {
let default_h160 = UnifiedAccounts::to_default_h160(&account);
assert_ok!(UnifiedAccounts::claim_default_evm_address(
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/src/xvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const CALL_EVM_PAYBLE_NAME: &'static str = "call_xvm_payable";
fn evm_payable_call_via_xvm_works() {
new_test_ext().execute_with(|| {
// create account mappings
claim_default_accounts(ALICE);
connect_accounts(&ALICE, &alith_secret_key());

let evm_payable_addr = deploy_evm_contract(EVM_PAYABLE);

Expand Down Expand Up @@ -229,7 +229,7 @@ fn wasm_payable_call_via_xvm_works() {
fn calling_wasm_payable_from_evm_fails_if_caller_contract_balance_below_ed() {
new_test_ext().execute_with(|| {
// create account mappings
claim_default_accounts(ALICE);
connect_accounts(&ALICE, &alith_secret_key());

let wasm_payable_addr = deploy_wasm_contract(WASM_PAYABLE_NAME);
let call_wasm_payable_addr = deploy_evm_contract(CALL_WASM_PAYBLE);
Expand Down Expand Up @@ -271,7 +271,7 @@ fn calling_wasm_payable_from_evm_fails_if_caller_contract_balance_below_ed() {
fn calling_wasm_payable_from_evm_works() {
new_test_ext().execute_with(|| {
// create account mappings
claim_default_accounts(ALICE);
connect_accounts(&ALICE, &alith_secret_key());

let wasm_payable_addr = deploy_wasm_contract(WASM_PAYABLE_NAME);
let call_wasm_payable_addr = deploy_evm_contract(CALL_WASM_PAYBLE);
Expand Down Expand Up @@ -304,7 +304,7 @@ fn calling_wasm_payable_from_evm_works() {
fn calling_evm_payable_from_wasm_works() {
new_test_ext().execute_with(|| {
// create account mappings
claim_default_accounts(ALICE);
connect_accounts(&ALICE, &alith_secret_key());

let evm_payable_addr = deploy_evm_contract(EVM_PAYABLE);
let wasm_address = deploy_wasm_contract(CALL_EVM_PAYBLE_NAME);
Expand Down Expand Up @@ -348,7 +348,7 @@ fn calling_evm_payable_from_wasm_works() {
fn reentrance_not_allowed() {
new_test_ext().execute_with(|| {
// create account mappings
claim_default_accounts(ALICE);
connect_accounts(&ALICE, &alith_secret_key());

// Call path: WASM -> EVM -> WASM
let call_evm_payable_address = deploy_wasm_contract(CALL_EVM_PAYBLE_NAME);
Expand Down

0 comments on commit 434e940

Please sign in to comment.