Skip to content

Commit

Permalink
refactor(axelarnet-gateway): drop call contract with token to nexus (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
haiyizxx authored Oct 22, 2024
1 parent 7decfc9 commit a291e51
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 176 deletions.
52 changes: 9 additions & 43 deletions contracts/axelarnet-gateway/src/contract/execute.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::iter;
use std::str::FromStr;

use axelar_core_std::nexus;
use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
use axelar_wasm_std::token::GetToken;
use axelar_wasm_std::{address, FnExt, IntoContractError};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
Addr, BankMsg, Coin, CosmosMsg, DepsMut, Event, HexBinary, MessageInfo, QuerierWrapper,
Response, Storage,
Addr, CosmosMsg, DepsMut, Event, HexBinary, MessageInfo, QuerierWrapper, Response, Storage,
};
use error_stack::{bail, ensure, report, ResultExt};
use itertools::Itertools;
Expand Down Expand Up @@ -108,11 +105,9 @@ pub fn call_contract(
.inspect_err(|err| panic_if_already_exists(err, &msg.cc_id))
.change_context(Error::SaveRoutableMessage)?;

let token = info.single_token().change_context(Error::InvalidToken)?;
let event = AxelarnetGatewayEvent::ContractCalled {
msg: msg.clone(),
payload: call_contract.payload,
token: token.clone(),
};

route_messages(storage, querier, info.sender, vec![msg]).map(|res| res.add_event(event.into()))
Expand All @@ -125,9 +120,7 @@ pub fn route_messages(
msgs: Vec<Message>,
) -> Result<Response<nexus::execute::Message>> {
let Config {
chain_name,
router,
nexus,
chain_name, router, ..
} = state::load_config(storage);

let router = Router::new(router);
Expand Down Expand Up @@ -157,11 +150,9 @@ pub fn route_messages(
&chain_name,
)? {
RoutingDestination::This => {
prepare_msgs_for_execution(storage, chain_name.clone(), msgs.collect())
}
RoutingDestination::Nexus => {
route_messages_to_nexus(&client, &nexus, msgs.collect())
prepare_for_execution(storage, chain_name.clone(), msgs.collect())
}
RoutingDestination::Nexus => route_to_nexus(&client, msgs.collect()),
RoutingDestination::Router => route_to_router(&router, msgs.collect()),
}?;

Expand Down Expand Up @@ -236,7 +227,7 @@ fn panic_if_already_exists(err: &state::Error, cc_id: &CrossChainId) {
}

// Because the messages came from the router, we can assume they are already verified
fn prepare_msgs_for_execution(
fn prepare_for_execution(
store: &mut dyn Storage,
chain_name: ChainName,
msgs: Vec<Message>,
Expand Down Expand Up @@ -327,38 +318,13 @@ fn determine_routing_destination(
.then(Ok)
}

/// Route message to the Nexus module
fn route_to_nexus(
client: &nexus::Client,
nexus: &Addr,
msg: Message,
token: Option<Coin>,
) -> Result<Vec<CosmosMsg<nexus::execute::Message>>> {
let msg: nexus::execute::Message = (msg, token.clone()).into();

token
.map(|token| BankMsg::Send {
to_address: nexus.to_string(),
amount: vec![token],
})
.map(Into::into)
.into_iter()
.chain(iter::once(client.route_message(msg)))
.collect::<Vec<_>>()
.then(Ok)
}

pub fn route_messages_to_nexus(
client: &nexus::Client,
nexus: &Addr,
msgs: Vec<Message>,
) -> Result<CosmosMsgWithEvent> {
/// Route messages to the Nexus module
pub fn route_to_nexus(client: &nexus::Client, msgs: Vec<Message>) -> Result<CosmosMsgWithEvent> {
let nexus_msgs = msgs
.clone()
.into_iter()
.map(|msg| route_to_nexus(client, nexus, msg, None))
.collect::<Result<Vec<_>>>()?
.then(|msgs| msgs.concat());
.map(|msg| client.route_message(msg.into()))
.collect();

Ok((
nexus_msgs,
Expand Down
15 changes: 5 additions & 10 deletions contracts/axelarnet-gateway/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use axelar_wasm_std::event::EventExt;
use cosmwasm_std::{Attribute, Coin, Event, HexBinary};
use cosmwasm_std::{Attribute, Event, HexBinary};
use router_api::Message;

pub enum AxelarnetGatewayEvent {
ContractCalled {
msg: Message,
payload: HexBinary,
token: Option<Coin>,
},
/// Uses the same event name as `GatewayEvent` for consistency
Routing {
Expand All @@ -20,13 +18,10 @@ pub enum AxelarnetGatewayEvent {
impl From<AxelarnetGatewayEvent> for Event {
fn from(other: AxelarnetGatewayEvent) -> Self {
match other {
AxelarnetGatewayEvent::ContractCalled {
msg,
payload,
token,
} => make_message_event("contract_called", msg)
.add_attribute("payload", payload.to_string())
.add_attribute_if_some("token", token.map(|token| token.to_string())),
AxelarnetGatewayEvent::ContractCalled { msg, payload } => {
make_message_event("contract_called", msg)
.add_attribute("payload", payload.to_string())
}
AxelarnetGatewayEvent::Routing { msg } => make_message_event("routing", msg),
AxelarnetGatewayEvent::MessageExecuted { msg } => {
make_message_event("message_executed", msg)
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion contracts/axelarnet-gateway/tests/utils/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ pub fn dummy_from_nexus(payload: &impl AsRef<[u8]>) -> nexus::execute::Message {
source_tx_id: "source-chain".as_bytes().to_vec().try_into().unwrap(),
source_tx_index: 0,
id: "source-chain-0".to_string(),
token: None,
}
}
15 changes: 5 additions & 10 deletions packages/axelar-core-std/src/nexus/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr;

use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
use axelar_wasm_std::nonempty;
use cosmwasm_std::{Coin, CosmosMsg, CustomMsg};
use cosmwasm_std::{CosmosMsg, CustomMsg};
use error_stack::{Report, Result, ResultExt};
use router_api::{Address, ChainName, ChainNameRaw, CrossChainId};
use schemars::JsonSchema;
Expand All @@ -22,8 +22,6 @@ pub struct Message {
pub source_tx_id: nonempty::Vec<u8>,
pub source_tx_index: u64,
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<Coin>,
}

impl CustomMsg for Message {}
Expand All @@ -39,8 +37,8 @@ fn parse_message_id(message_id: &str) -> Result<(nonempty::Vec<u8>, u64), Error>
Ok((tx_id, id.event_index.into()))
}

impl From<(router_api::Message, Option<Coin>)> for Message {
fn from((msg, token): (router_api::Message, Option<Coin>)) -> Self {
impl From<router_api::Message> for Message {
fn from(msg: router_api::Message) -> Self {
// fallback to using all 0's as the tx ID if it's not in the expected format
let (source_tx_id, source_tx_index) =
parse_message_id(&msg.cc_id.message_id).unwrap_or((vec![0; 32].try_into().unwrap(), 0));
Expand All @@ -54,7 +52,6 @@ impl From<(router_api::Message, Option<Coin>)> for Message {
source_tx_id,
source_tx_index,
id: msg.cc_id.message_id.into(),
token,
}
}
}
Expand Down Expand Up @@ -88,7 +85,6 @@ mod test {
use std::vec;

use axelar_wasm_std::msg_id::{Base58TxDigestAndEventIndex, HexTxHashAndEventIndex};
use cosmwasm_std::Coin;
use router_api::CrossChainId;

use super::Message;
Expand All @@ -108,7 +104,6 @@ mod test {
source_tx_id: msg_id.tx_hash.to_vec().try_into().unwrap(),
source_tx_index: msg_id.event_index as u64,
id: msg_id.to_string(),
token: None,
};

let router_msg = router_api::Message::try_from(msg.clone());
Expand Down Expand Up @@ -138,7 +133,7 @@ mod test {
payload_hash: [1; 32],
};

let nexus_msg = Message::from((msg.clone(), None));
let nexus_msg = Message::from(msg.clone());
goldie::assert_json!(nexus_msg);
}

Expand All @@ -161,7 +156,7 @@ mod test {
payload_hash: [1; 32],
};

let nexus_msg = Message::from((msg.clone(), Some(Coin::new(100, "test"))));
let nexus_msg = Message::from(msg.clone());
goldie::assert_json!(nexus_msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,5 @@
0
],
"source_tx_index": 0,
"id": "8qbHbw2BbbTHBW1sbeqakYXVKRQM8Ne7pLK7m6CVfeR-1",
"token": {
"denom": "test",
"amount": "100"
}
"id": "8qbHbw2BbbTHBW1sbeqakYXVKRQM8Ne7pLK7m6CVfeR-1"
}

0 comments on commit a291e51

Please sign in to comment.