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

chore: simplify auth test utils #125

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 24 additions & 32 deletions contracts/example/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
#![cfg(test)]
extern crate std;

use axelar_gas_service::AxelarGasService;
use axelar_gas_service::AxelarGasServiceClient;
use axelar_gateway::testutils::{self, generate_proof, get_approve_hash, TestSignerSet};
use axelar_gateway::types::Message;
use axelar_gateway::AxelarGatewayClient;
use axelar_soroban_std::types::Token;
use axelar_soroban_std::{assert_last_emitted_event, auth_invocation};
use example::Example;
use example::ExampleClient;
use soroban_sdk::testutils::{AuthorizedFunction, AuthorizedInvocation};
use soroban_sdk::token::StellarAssetClient;
use soroban_sdk::{
testutils::Address as _, testutils::BytesN as _, vec, Address, BytesN, Env, String,
use axelar_gas_service::{AxelarGasService, AxelarGasServiceClient};
use axelar_gateway::{
testutils::{self, generate_proof, get_approve_hash, TestSignerSet},
types::Message,
AxelarGatewayClient,
};
use soroban_sdk::{Bytes, IntoVal, Symbol};
use axelar_soroban_std::{assert_last_emitted_event, auth_invocation, types::Token};
use example::{Example, ExampleClient};
use soroban_sdk::testutils::{Address as _, AuthorizedFunction, AuthorizedInvocation, BytesN as _};
use soroban_sdk::token::StellarAssetClient;
use soroban_sdk::{vec, Address, Bytes, BytesN, Env, IntoVal, String, Symbol};

fn setup_gateway<'a>(env: &Env) -> (TestSignerSet, AxelarGatewayClient<'a>) {
let (signers, client) = testutils::setup_gateway(env, 0, 5);
Expand Down Expand Up @@ -49,7 +45,7 @@ fn gmp_example() {
let source_chain = String::from_str(&env, "source");
let (_, source_gateway_client) = setup_gateway(&env);
let source_gateway_id = source_gateway_client.address;
let (_source_gas_service_client, _source_gas_collector, source_gas_service_id) =
let (source_gas_service_client, _source_gas_collector, source_gas_service_id) =
setup_gas_service(&env);
let source_app = setup_app(&env, &source_gateway_id, &source_gas_service_id);

Expand All @@ -73,13 +69,14 @@ fn gmp_example() {

// Initiate cross-chain contract call, sending message from source to destination
let asset = &env.register_stellar_asset_contract_v2(user.clone());
let asset_client = StellarAssetClient::new(&env, &asset.address());
let gas_amount: i128 = 100;
let gas_token = Token {
address: asset.address(),
amount: gas_amount,
};

StellarAssetClient::new(&env, &asset.address()).mint(&user, &gas_amount);
asset_client.mint(&user, &gas_amount);

source_app.send(
&user,
Expand All @@ -89,20 +86,16 @@ fn gmp_example() {
&gas_token,
);

let transfer_auth = auth_invocation!(&env,
"transfer",
asset.address() =>
(
&user,
source_gas_service_id.clone(),
gas_token.amount
)
let transfer_auth = auth_invocation!(
&env,
user,
asset_client.transfer(&user, source_gas_service_id, gas_token.amount)
);

let pay_gas_auth = auth_invocation!(&env,
"pay_gas",
source_gas_service_id.clone() =>
(
let pay_gas_auth = auth_invocation!(
&env,
user,
source_gas_service_client.pay_gas(
source_app.address.clone(),
destination_chain.clone(),
destination_address.clone(),
Expand All @@ -114,11 +107,10 @@ fn gmp_example() {
transfer_auth
);

let send_auth = auth_invocation!(&env,
let send_auth = auth_invocation!(
&env,
user,
"send",
source_app.address.clone() =>
(
source_app.send(
&user,
destination_chain.clone(),
destination_address.clone(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
mod utils;

use axelar_soroban_std::{address::AddressExt, events};
use axelar_soroban_std::{address::AddressExt, auth_invocation, events};
use interchain_token_service::{
event::InterchainTokenDeploymentStartedEvent, types::TokenManagerType,
event::InterchainTokenDeploymentStartedEvent,
types::{DeployInterchainToken, HubMessage, Message, TokenManagerType},
};
use soroban_sdk::{testutils::Address as _, token::StellarAssetClient, Address, String};
use soroban_sdk::testutils::{Address as _, AuthorizedFunction, AuthorizedInvocation};
use soroban_sdk::token::{self, StellarAssetClient};
use soroban_sdk::{Address, Bytes, IntoVal, String, Symbol};
use soroban_token_sdk::metadata::TokenMetadata;
use utils::{setup_env, setup_gas_token};

#[test]
fn deploy_remote_canonical_token_succeeds() {
let (env, client, _, _, _) = setup_env();

let (env, client, _, 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 All @@ -23,20 +26,42 @@ fn deploy_remote_canonical_token_succeeds() {
let token_address = asset.address();
let expected_deploy_salt = client.canonical_token_deploy_salt(&token_address);
let expected_id = client.interchain_token_id(&Address::zero(&env), &expected_deploy_salt);

assert_eq!(client.register_canonical_token(&token_address), expected_id);
assert_eq!(client.token_address(&expected_id), token_address);

assert_eq!(
client.token_manager_type(&expected_id),
TokenManagerType::LockUnlock
);

let destination_chain = String::from_str(&env, "ethereum");
let its_hub_chain = String::from_str(&env, "axelar");
let its_hub_address = String::from_str(&env, "its_hub_address");

client
.mock_all_auths()
.set_trusted_chain(&destination_chain);

let token = token::Client::new(&env, &asset.address());
let token_metadata = TokenMetadata {
name: token.name(),
decimal: token.decimals(),
symbol: token.symbol(),
};

let message = Message::DeployInterchainToken(DeployInterchainToken {
token_id: expected_id.clone(),
name: token_metadata.name.clone(),
symbol: token_metadata.symbol.clone(),
decimals: token_metadata.decimal as u8,
minter: None,
});

let payload = HubMessage::SendToHub {
destination_chain: destination_chain.clone(),
message,
}
.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);
Expand All @@ -45,6 +70,33 @@ fn deploy_remote_canonical_token_succeeds() {
goldie::assert!(events::fmt_emitted_event_at_idx::<
InterchainTokenDeploymentStartedEvent,
>(&env, -4));

let transfer_auth = auth_invocation!(
&env,
spender,
gas_token.transfer(
spender.clone(),
gas_service.address.clone(),
gas_token.amount
)
);

let gas_service_auth = auth_invocation!(
&env,
spender,
gas_service.pay_gas(
client.address,
its_hub_chain,
its_hub_address,
payload,
spender,
gas_token,
Bytes::new(&env)
),
transfer_auth
);

assert_eq!(env.auths(), gas_service_auth);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,18 @@
mod utils;

use axelar_soroban_std::assert_contract_err;
use axelar_soroban_std::auth_invocation;
use axelar_soroban_std::events;
use interchain_token_service::error::ContractError;
use interchain_token_service::event::InterchainTokenDeploymentStartedEvent;
use interchain_token_service::types::DeployInterchainToken;
use interchain_token_service::types::HubMessage;
use interchain_token_service::types::Message;
use soroban_sdk::testutils::AuthorizedFunction;
use soroban_sdk::testutils::AuthorizedInvocation;
use soroban_sdk::Bytes;
use soroban_sdk::IntoVal;
use soroban_sdk::Symbol;
use soroban_sdk::{testutils::Address as _, Address, BytesN, String};
use axelar_soroban_std::{assert_contract_err, auth_invocation, events};
use interchain_token_service::{
error::ContractError,
event::InterchainTokenDeploymentStartedEvent,
types::{DeployInterchainToken, HubMessage, Message},
};
use soroban_sdk::testutils::{Address as _, AuthorizedFunction, AuthorizedInvocation};
use soroban_sdk::{Address, Bytes, BytesN, IntoVal, String, Symbol};
use soroban_token_sdk::metadata::TokenMetadata;
use utils::setup_env;
use utils::setup_gas_token;
use utils::TokenMetadataExt;
use utils::{setup_env, setup_gas_token, TokenMetadataExt};

#[test]
fn deploy_remote_interchain_token_succeeds() {
let (env, client, _, _, _) = setup_env();

let sender = Address::generate(&env);
let gas_token = setup_gas_token(&env, &sender);
let minter: Option<Address> = None;
let salt = BytesN::<32>::from_array(&env, &[1; 32]);
let token_metadata = TokenMetadata::new(&env, "name", "symbol", 6);
let initial_supply = 1;

let token_id = client.mock_all_auths().deploy_interchain_token(
&sender,
&salt,
&token_metadata,
&initial_supply,
&minter,
);

let destination_chain = String::from_str(&env, "ethereum");
client
.mock_all_auths()
.set_trusted_chain(&destination_chain);

let deployed_token_id = client.mock_all_auths().deploy_remote_interchain_token(
&sender,
&salt,
&destination_chain,
&gas_token,
);
assert_eq!(token_id, deployed_token_id);

goldie::assert!(events::fmt_emitted_event_at_idx::<
InterchainTokenDeploymentStartedEvent,
>(&env, -4));
}

#[test]
fn deploy_remote_interchain_token_auth_test() {
let (env, client, _, gas_service, _) = setup_env();

let sender = Address::generate(&env);
Expand Down Expand Up @@ -89,36 +44,36 @@ fn deploy_remote_interchain_token_auth_test() {
&destination_chain,
&gas_token,
);

assert_eq!(token_id, deployed_token_id);

goldie::assert!(events::fmt_emitted_event_at_idx::<
InterchainTokenDeploymentStartedEvent,
>(&env, -4));

let message = Message::DeployInterchainToken(DeployInterchainToken {
token_id,
name: token_metadata.name.clone(),
symbol: token_metadata.symbol.clone(),
decimals: token_metadata.decimal as u8,
minter: None,
});

let payload = HubMessage::SendToHub {
destination_chain: destination_chain.clone(),
message,
}
.abi_encode(&env);

let transfer_auth = auth_invocation!(&env,
"transfer",
gas_token.clone().address =>
(
&sender,
gas_service.address.clone(),
gas_token.amount
)
let transfer_auth = auth_invocation!(
&env,
sender,
gas_token.transfer(&sender, gas_service.address.clone(), gas_token.amount)
);

let pay_gas_auth = auth_invocation!(&env,
"pay_gas",
gas_service.address =>
(
let pay_gas_auth = auth_invocation!(
&env,
sender,
gas_service.pay_gas(
client.address.clone(),
its_hub_chain,
its_hub_address,
Expand All @@ -130,16 +85,10 @@ fn deploy_remote_interchain_token_auth_test() {
transfer_auth
);

let deploy_remote_interchain_token_auth = auth_invocation!(&env,
let deploy_remote_interchain_token_auth = auth_invocation!(
&env,
sender,
"deploy_remote_interchain_token",
client.address =>
(
&sender,
salt,
destination_chain,
gas_token
),
client.deploy_remote_interchain_token(&sender, salt, destination_chain, gas_token),
pay_gas_auth
);

Expand Down
Loading