Skip to content

Commit

Permalink
refactor(minor-axelarnet-gateway): simplify the initial implementation (
Browse files Browse the repository at this point in the history
  • Loading branch information
cgorenflo authored Aug 29, 2024
1 parent 8308c8d commit 13390b9
Show file tree
Hide file tree
Showing 33 changed files with 1,359 additions and 967 deletions.
65 changes: 51 additions & 14 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ strum = { version = "0.25", default-features = false, features = ["derive"] }
interchain-token-service = { version = "^0.1.0", path = "interchain-token-service" }
goldie = { version = "0.5" }
axelarnet-gateway = { version = "^0.1.0", path = "contracts/axelarnet-gateway" }
cw-multi-test = "1.2.0"

[workspace.lints.clippy]
arithmetic_side_effects = "deny"
Expand Down
4 changes: 3 additions & 1 deletion contracts/axelarnet-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
error-stack = { workspace = true }
itertools = { workspace = true }
msgs-derive = { workspace = true }
report = { workspace = true }
router-api = { workspace = true }
Expand All @@ -48,7 +49,8 @@ sha3 = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
cw-multi-test = "0.15.1"
cw-multi-test = { workspace = true }
goldie = { workspace = true }

[lints]
workspace = true
69 changes: 69 additions & 0 deletions contracts/axelarnet-gateway/src/clients/external.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, HexBinary, QuerierWrapper, WasmMsg};
use router_api::{Address, CrossChainId};

/// `AxelarExecutableMsg` is a struct containing the args used by the axelarnet gateway to execute a destination contract on Axelar.
/// Each App needs to expose a `ExecuteMsg::Execute(AxelarExecutableMsg)` variant that only the gateway is allowed to call.
#[cw_serde]
pub struct AxelarExecutableMsg {
pub cc_id: CrossChainId,
pub source_address: Address,
pub payload: HexBinary,
}

/// By convention, amplifier-compatible contracts must expose this `Execute` variant.
/// Due to identical json serialization, we can imitate it here so the gateway can call it.
#[cw_serde]
enum ExecuteMsg {
/// Execute the message at the destination contract with the corresponding payload.
Execute(AxelarExecutableMsg),
}

pub struct Client<'a> {
client: client::Client<'a, ExecuteMsg, ()>,
}

impl<'a> Client<'a> {
pub fn new(querier: QuerierWrapper<'a>, destination: &'a Addr) -> Self {
Client {
client: client::Client::new(querier, destination),
}
}

pub fn execute(&self, msg: AxelarExecutableMsg) -> WasmMsg {
self.client.execute(&ExecuteMsg::Execute(msg))
}
}

#[cfg(test)]
mod test {
use cosmwasm_std::testing::mock_dependencies;
use cosmwasm_std::{to_json_binary, Addr, HexBinary, WasmMsg};
use router_api::CrossChainId;

use crate::clients::external;

#[test]
fn execute_message() {
let deps = mock_dependencies();

let destination_addr = Addr::unchecked("axelar-executable");

let executable_msg = external::AxelarExecutableMsg {
source_address: "source-address".parse().unwrap(),
payload: HexBinary::from(vec![1, 2, 3]),
cc_id: CrossChainId::new("source-chain", "message-id").unwrap(),
};

let client = external::Client::new(deps.as_ref().querier, &destination_addr);

assert_eq!(
client.execute(executable_msg.clone()),
WasmMsg::Execute {
contract_addr: destination_addr.to_string(),
msg: to_json_binary(&external::ExecuteMsg::Execute(executable_msg)).unwrap(),
funds: vec![],
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ mod test {
#[test]
fn chain_name() {
let (querier, _, addr) = setup();
let client: Client =
client::Client::new(QuerierWrapper::new(&querier), addr.clone()).into();
let client: Client = client::Client::new(QuerierWrapper::new(&querier), &addr).into();

assert_eq!(
client.chain_name().unwrap(),
Expand All @@ -77,8 +76,7 @@ mod test {
#[test]
fn call_contract() {
let (querier, _, addr) = setup();
let client: Client =
client::Client::new(QuerierWrapper::new(&querier), addr.clone()).into();
let client: Client = client::Client::new(QuerierWrapper::new(&querier), &addr).into();

let destination_chain: ChainName = "destination-chain".parse().unwrap();
let destination_address: Address = "destination-address".parse().unwrap();
Expand Down Expand Up @@ -108,8 +106,7 @@ mod test {
#[test]
fn execute_message() {
let (querier, _, addr) = setup();
let client: Client =
client::Client::new(QuerierWrapper::new(&querier), addr.clone()).into();
let client: Client = client::Client::new(QuerierWrapper::new(&querier), &addr).into();

let payload = HexBinary::from(vec![1, 2, 3]);
let cc_id = CrossChainId::new("source-chain", "message-id").unwrap();
Expand Down
2 changes: 2 additions & 0 deletions contracts/axelarnet-gateway/src/clients/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod external;
pub mod gateway;
Loading

0 comments on commit 13390b9

Please sign in to comment.