Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update mock auth macro to support non root auth #134

Merged
merged 15 commits into from
Jan 17, 2025
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod utils;
ahramy marked this conversation as resolved.
Show resolved Hide resolved

use axelar_soroban_std::{address::AddressExt, auth_invocation, events};
use axelar_soroban_std::{address::AddressExt, auth_invocation, events, mock_auth};
use interchain_token_service::{
event::InterchainTokenDeploymentStartedEvent,
types::{DeployInterchainToken, HubMessage, Message, TokenManagerType},
Expand All @@ -13,7 +13,7 @@ use utils::{setup_env, setup_gas_token};

#[test]
fn deploy_remote_canonical_token_succeeds() {
let (env, client, _, gas_service, _) = setup_env();
let (env, client, gateway, gas_service, _) = setup_env();
let spender = Address::generate(&env);
let gas_token = setup_gas_token(&env, &spender);
let asset = &env.register_stellar_asset_contract_v2(Address::generate(&env));
Expand Down Expand Up @@ -62,9 +62,43 @@ fn deploy_remote_canonical_token_succeeds() {
}
.abi_encode(&env);

let deployed_token_id = client
.mock_all_auths_allowing_non_root_auth()
.deploy_remote_canonical_token(&token_address, &destination_chain, &spender, &gas_token);
let transfer_token_auth = mock_auth!(
ahramy marked this conversation as resolved.
Show resolved Hide resolved
env,
spender,
gas_token.transfer(spender, gas_service.address, gas_token.amount),
&[]
ahramy marked this conversation as resolved.
Show resolved Hide resolved
);

let pay_gas_auth = mock_auth!(
env,
spender,
gas_service.pay_gas(
client.address,
its_hub_chain,
its_hub_address,
payload,
spender,
gas_token,
Bytes::new(&env)
),
&[(transfer_token_auth.invoke).clone()]
ahramy marked this conversation as resolved.
Show resolved Hide resolved
ahramy marked this conversation as resolved.
Show resolved Hide resolved
);

let call_contract_auth = mock_auth!(
env,
spender,
gateway.call_contract(client.address, its_hub_chain, its_hub_address, payload),
&[]
);

env.mock_auths(&[pay_gas_auth, call_contract_auth]);

ahramy marked this conversation as resolved.
Show resolved Hide resolved
let deployed_token_id = client.deploy_remote_canonical_token(
ahramy marked this conversation as resolved.
Show resolved Hide resolved
&token_address,
&destination_chain,
&spender,
&gas_token,
);
assert_eq!(expected_id, deployed_token_id);

goldie::assert!(events::fmt_emitted_event_at_idx::<
Expand Down
41 changes: 28 additions & 13 deletions packages/axelar-soroban-std/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ macro_rules! assert_invoke_auth_ok {
use soroban_sdk::IntoVal;

let call_result = $client
.mock_auths($crate::mock_auth!($caller, $client, $method, $($arg),*))
.mock_auths(&[$crate::mock_auth!(
$client.env,
$caller,
$client.$method($($arg),*),
&[]
ahramy marked this conversation as resolved.
Show resolved Hide resolved
)])
.$method($($arg),*);

match call_result {
Expand All @@ -132,7 +137,12 @@ macro_rules! assert_invoke_auth_err {
use soroban_sdk::{IntoVal, xdr::{ScError, ScErrorCode, ScVal}};

let call_result = $client
.mock_auths($crate::mock_auth!($caller, $client, $method, $($arg),*))
.mock_auths(&[$crate::mock_auth!(
$client.env,
$caller,
$client.$method($($arg),*),
&[]
ahramy marked this conversation as resolved.
Show resolved Hide resolved
)])
.$method($($arg),*);

match call_result {
Expand All @@ -150,15 +160,20 @@ macro_rules! assert_invoke_auth_err {

#[macro_export]
macro_rules! mock_auth {
($caller:expr, $client:ident, $method:ident, $($arg:expr),*) => {
&[soroban_sdk::testutils::MockAuth {
address: &$caller,
invoke: &soroban_sdk::testutils::MockAuthInvoke {
contract: &$client.address,
fn_name: &stringify!($method).replace("try_", ""),
args: ($($arg.clone(),)*).into_val(&$client.env),
sub_invokes: &[],
},
}]
};
(
$env:expr,
$caller:expr,
$client:ident . $method:ident ( $($arg:expr),* $(,)? ),
$sub_invokes:expr
ahramy marked this conversation as resolved.
Show resolved Hide resolved
) => {{
soroban_sdk::testutils::MockAuth {
address: &$caller,
invoke: &soroban_sdk::testutils::MockAuthInvoke {
contract: &$client.address,
fn_name: &stringify!($method).replace("try_", ""),
args: ($($arg.clone(),)*).into_val(&$env),
sub_invokes: $sub_invokes,
},
}
}};
}
Loading