From 56450759dce97a1caee197b441625d8d93fa02eb Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Mon, 30 Sep 2024 22:53:12 -0400 Subject: [PATCH 01/14] fix(interchain-token-service): ignore empty event attributes --- .../interchain-token-service/src/events.rs | 142 +++++++++++++---- .../src/primitives.rs | 2 +- ...essage_received_with_all_attributes.golden | 105 +++++++++++++ ...sage_received_with_empty_attributes.golden | 143 ++++++++++++++++++ packages/axelar-wasm-std/src/event.rs | 30 +++- 5 files changed, 394 insertions(+), 28 deletions(-) create mode 100644 contracts/interchain-token-service/src/testdata/message_received_with_all_attributes.golden create mode 100644 contracts/interchain-token-service/src/testdata/message_received_with_empty_attributes.golden diff --git a/contracts/interchain-token-service/src/events.rs b/contracts/interchain-token-service/src/events.rs index 37b98ec6c..aa5989a37 100644 --- a/contracts/interchain-token-service/src/events.rs +++ b/contracts/interchain-token-service/src/events.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::Attribute; +use axelar_wasm_std::event::EventExt; use router_api::{Address, ChainNameRaw, CrossChainId}; use crate::primitives::Message; @@ -45,11 +45,10 @@ fn make_message_event( destination_chain: ChainNameRaw, msg: Message, ) -> cosmwasm_std::Event { - let mut attrs = vec![ - Attribute::new("cc_id", cc_id.to_string()), - Attribute::new("destination_chain", destination_chain.to_string()), - Attribute::new("message_type", msg.as_ref().to_string()), - ]; + let event = cosmwasm_std::Event::new(event_name) + .add_attribute("cc_id", cc_id.to_string()) + .add_attribute("destination_chain", destination_chain.to_string()) + .add_attribute("message_type", msg.as_ref().to_string()); match msg { Message::InterchainTransfer { @@ -59,13 +58,12 @@ fn make_message_event( amount, data, } => { - attrs.extend(vec![ - Attribute::new("token_id", token_id.to_string()), - Attribute::new("source_address", source_address.to_string()), - Attribute::new("destination_address", destination_address.to_string()), - Attribute::new("amount", amount.to_string()), - Attribute::new("data", data.to_string()), - ]); + event + .add_attribute("token_id", token_id.to_string()) + .add_attribute_if_nonempty("source_address", source_address.to_string()) + .add_attribute_if_nonempty("destination_address", destination_address.to_string()) + .add_attribute("amount", amount.to_string()) + .add_attribute_if_nonempty("data", data.to_string()) } Message::DeployInterchainToken { token_id, @@ -74,26 +72,118 @@ fn make_message_event( decimals, minter, } => { - attrs.extend(vec![ - Attribute::new("token_id", token_id.to_string()), - Attribute::new("name", name), - Attribute::new("symbol", symbol), - Attribute::new("decimals", decimals.to_string()), - Attribute::new("minter", minter.to_string()), - ]); + event + .add_attribute("token_id", token_id.to_string()) + .add_attribute_if_nonempty("name", name) + .add_attribute_if_nonempty("symbol", symbol) + .add_attribute("decimals", decimals.to_string()) + .add_attribute_if_nonempty("minter", minter.to_string()) } Message::DeployTokenManager { token_id, token_manager_type, params, } => { - attrs.extend(vec![ - Attribute::new("token_id", token_id.to_string()), - Attribute::new("token_manager_type", format!("{:?}", token_manager_type)), - Attribute::new("params", params.to_string()), - ]); + event + .add_attribute("token_id", token_id.to_string()) + .add_attribute("token_manager_type", token_manager_type.as_ref().to_string()) + .add_attribute_if_nonempty("params", params.to_string()) } } +} + +#[cfg(test)] +mod test { + use cosmwasm_std::HexBinary; + use router_api::CrossChainId; + + use crate::{events::Event, Message, TokenId, TokenManagerType}; + + #[test] + fn message_received_with_all_attributes() { + let test_cases: Vec = vec![ + Message::InterchainTransfer { + token_id: TokenId::new([1; 32]), + source_address: HexBinary::from([1; 32]), + destination_address: HexBinary::from([1, 2, 3, 4]), + amount: 1u64.into(), + data: HexBinary::from([1, 2, 3, 4]), + }, + Message::DeployInterchainToken { + token_id: TokenId::new([1; 32]), + name: "Test".into(), + symbol: "TST".into(), + decimals: 18, + minter: HexBinary::from([1; 32]), + }, + Message::DeployTokenManager { + token_id: TokenId::new([1; 32]), + token_manager_type: TokenManagerType::MintBurn, + params: HexBinary::from([1, 2, 3, 4]), + }, + ]; + + let events: Vec<_> = test_cases.into_iter().map(|message| { + let event = Event::MessageReceived { + cc_id: CrossChainId::new("source", "hash").unwrap(), + destination_chain: "destination".parse().unwrap(), + message, + }; + + cosmwasm_std::Event::from(event) + }).collect(); - cosmwasm_std::Event::new(event_name).add_attributes(attrs) + goldie::assert_json!(events); + } + + #[test] + fn message_received_with_empty_attributes() { + let test_cases: Vec = vec![ + Message::InterchainTransfer { + token_id: TokenId::new([1; 32]), + source_address: HexBinary::from([1; 32]), + destination_address: HexBinary::from([1, 2, 3, 4]), + amount: 1u64.into(), + data: HexBinary::default(), + }, + Message::InterchainTransfer { + token_id: TokenId::new([1; 32]), + source_address: HexBinary::default(), + destination_address: HexBinary::default(), + amount: 1u64.into(), + data: HexBinary::default(), + }, + Message::DeployInterchainToken { + token_id: TokenId::new([1; 32]), + name: "Test".into(), + symbol: "TST".into(), + decimals: 18, + minter: HexBinary::default(), + }, + Message::DeployInterchainToken { + token_id: TokenId::new([1; 32]), + name: "".into(), + symbol: "".into(), + decimals: 0, + minter: HexBinary::default(), + }, + Message::DeployTokenManager { + token_id: TokenId::new([1; 32]), + token_manager_type: TokenManagerType::MintBurn, + params: HexBinary::default(), + }, + ]; + + let events: Vec<_> = test_cases.into_iter().map(|message| { + let event = Event::MessageReceived { + cc_id: CrossChainId::new("source", "hash").unwrap(), + destination_chain: "destination".parse().unwrap(), + message, + }; + + cosmwasm_std::Event::from(event) + }).collect(); + + goldie::assert_json!(events); + } } diff --git a/contracts/interchain-token-service/src/primitives.rs b/contracts/interchain-token-service/src/primitives.rs index 205cd4cce..3c50d301a 100644 --- a/contracts/interchain-token-service/src/primitives.rs +++ b/contracts/interchain-token-service/src/primitives.rs @@ -23,7 +23,7 @@ impl Display for TokenId { /// The supported types of token managers that can be deployed by ITS contracts. #[cw_serde] -#[derive(Eq, Copy, FromRepr)] +#[derive(Eq, Copy, FromRepr, strum::AsRefStr)] #[repr(u8)] pub enum TokenManagerType { NativeInterchainToken, diff --git a/contracts/interchain-token-service/src/testdata/message_received_with_all_attributes.golden b/contracts/interchain-token-service/src/testdata/message_received_with_all_attributes.golden new file mode 100644 index 000000000..0f3eb14ce --- /dev/null +++ b/contracts/interchain-token-service/src/testdata/message_received_with_all_attributes.golden @@ -0,0 +1,105 @@ +[ + { + "type": "message_received", + "attributes": [ + { + "key": "cc_id", + "value": "source_hash" + }, + { + "key": "destination_chain", + "value": "destination" + }, + { + "key": "message_type", + "value": "InterchainTransfer" + }, + { + "key": "token_id", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "source_address", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "destination_address", + "value": "01020304" + }, + { + "key": "amount", + "value": "1" + }, + { + "key": "data", + "value": "01020304" + } + ] + }, + { + "type": "message_received", + "attributes": [ + { + "key": "cc_id", + "value": "source_hash" + }, + { + "key": "destination_chain", + "value": "destination" + }, + { + "key": "message_type", + "value": "DeployInterchainToken" + }, + { + "key": "token_id", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "name", + "value": "Test" + }, + { + "key": "symbol", + "value": "TST" + }, + { + "key": "decimals", + "value": "18" + }, + { + "key": "minter", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + } + ] + }, + { + "type": "message_received", + "attributes": [ + { + "key": "cc_id", + "value": "source_hash" + }, + { + "key": "destination_chain", + "value": "destination" + }, + { + "key": "message_type", + "value": "DeployTokenManager" + }, + { + "key": "token_id", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "token_manager_type", + "value": "MintBurn" + }, + { + "key": "params", + "value": "01020304" + } + ] + } +] \ No newline at end of file diff --git a/contracts/interchain-token-service/src/testdata/message_received_with_empty_attributes.golden b/contracts/interchain-token-service/src/testdata/message_received_with_empty_attributes.golden new file mode 100644 index 000000000..0aa3dfa37 --- /dev/null +++ b/contracts/interchain-token-service/src/testdata/message_received_with_empty_attributes.golden @@ -0,0 +1,143 @@ +[ + { + "type": "message_received", + "attributes": [ + { + "key": "cc_id", + "value": "source_hash" + }, + { + "key": "destination_chain", + "value": "destination" + }, + { + "key": "message_type", + "value": "InterchainTransfer" + }, + { + "key": "token_id", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "source_address", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "destination_address", + "value": "01020304" + }, + { + "key": "amount", + "value": "1" + } + ] + }, + { + "type": "message_received", + "attributes": [ + { + "key": "cc_id", + "value": "source_hash" + }, + { + "key": "destination_chain", + "value": "destination" + }, + { + "key": "message_type", + "value": "InterchainTransfer" + }, + { + "key": "token_id", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "amount", + "value": "1" + } + ] + }, + { + "type": "message_received", + "attributes": [ + { + "key": "cc_id", + "value": "source_hash" + }, + { + "key": "destination_chain", + "value": "destination" + }, + { + "key": "message_type", + "value": "DeployInterchainToken" + }, + { + "key": "token_id", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "name", + "value": "Test" + }, + { + "key": "symbol", + "value": "TST" + }, + { + "key": "decimals", + "value": "18" + } + ] + }, + { + "type": "message_received", + "attributes": [ + { + "key": "cc_id", + "value": "source_hash" + }, + { + "key": "destination_chain", + "value": "destination" + }, + { + "key": "message_type", + "value": "DeployInterchainToken" + }, + { + "key": "token_id", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "decimals", + "value": "0" + } + ] + }, + { + "type": "message_received", + "attributes": [ + { + "key": "cc_id", + "value": "source_hash" + }, + { + "key": "destination_chain", + "value": "destination" + }, + { + "key": "message_type", + "value": "DeployTokenManager" + }, + { + "key": "token_id", + "value": "0101010101010101010101010101010101010101010101010101010101010101" + }, + { + "key": "token_manager_type", + "value": "MintBurn" + } + ] + } +] \ No newline at end of file diff --git a/packages/axelar-wasm-std/src/event.rs b/packages/axelar-wasm-std/src/event.rs index 7f0236675..cc3514e97 100644 --- a/packages/axelar-wasm-std/src/event.rs +++ b/packages/axelar-wasm-std/src/event.rs @@ -5,6 +5,11 @@ pub trait EventExt { where K: Into, V: Into; + + fn add_attribute_if_nonempty(self, key: K, value: V) -> Self + where + K: Into, + V: Into; } impl EventExt for Event { @@ -18,11 +23,24 @@ impl EventExt for Event { None => self, } } + + fn add_attribute_if_nonempty(self, key: K, value: V) -> Self + where + K: Into, + V: Into, + { + let value: String = value.into(); + + match value.is_empty() { + false => self.add_attribute(key, value), + true => self, + } + } } #[cfg(test)] mod test { - use cosmwasm_std::{Event, Int128}; + use cosmwasm_std::{Event, HexBinary, Int128}; use super::EventExt; @@ -35,4 +53,14 @@ mod test { goldie::assert_json!(event) } + + #[test] + fn add_attribute_if_nonempty() { + let event = Event::new("test") + .add_attribute_if_nonempty("foo", "bar") + .add_attribute_if_nonempty("int", "") + .add_attribute_if_nonempty("baz", HexBinary::default().to_string()); + + goldie::assert_json!(event) + } } From 466e32df1e353b109f1ea5aeddcae2f8adc021e9 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Thu, 10 Oct 2024 01:06:32 -0400 Subject: [PATCH 02/14] add nonempty HexBinary --- .../axelar-wasm-std/src/nonempty/hexbinary.rs | 60 +++++++++++++++++++ packages/axelar-wasm-std/src/nonempty/mod.rs | 2 + 2 files changed, 62 insertions(+) create mode 100644 packages/axelar-wasm-std/src/nonempty/hexbinary.rs diff --git a/packages/axelar-wasm-std/src/nonempty/hexbinary.rs b/packages/axelar-wasm-std/src/nonempty/hexbinary.rs new file mode 100644 index 000000000..89381604f --- /dev/null +++ b/packages/axelar-wasm-std/src/nonempty/hexbinary.rs @@ -0,0 +1,60 @@ +use cosmwasm_schema::cw_serde; +use std::ops::Deref; + +use crate::nonempty::Error; + +#[cw_serde] +#[derive(Eq, Hash)] +pub struct HexBinary(cosmwasm_std::HexBinary); + +impl TryFrom for HexBinary { + type Error = Error; + + fn try_from(value: cosmwasm_std::HexBinary) -> Result { + if value.is_empty() { + Err(Error::InvalidValue("empty".to_string())) + } else { + Ok(HexBinary(value)) + } + } +} + +impl TryFrom<&[u8]> for HexBinary { + type Error = Error; + + fn try_from(value: &[u8]) -> Result { + HexBinary::try_from(cosmwasm_std::HexBinary::from(value)) + } +} + +impl From for cosmwasm_std::HexBinary { + fn from(value: HexBinary) -> Self { + value.0 + } +} + +impl Deref for HexBinary { + type Target = cosmwasm_std::HexBinary; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +#[cfg(test)] +mod tests { + use crate::nonempty::{Error, HexBinary}; + + #[test] + fn test_non_empty_hexbinary() { + assert!(HexBinary::try_from(cosmwasm_std::HexBinary::from(&[1, 2, 3])).is_ok()) + } + + #[test] + fn test_empty_non_empty_hexbinary() { + assert_eq!( + HexBinary::try_from(cosmwasm_std::HexBinary::from(&[])).unwrap_err(), + Error::InvalidValue("empty".to_string()) + ) + } +} diff --git a/packages/axelar-wasm-std/src/nonempty/mod.rs b/packages/axelar-wasm-std/src/nonempty/mod.rs index ad23fc850..983163284 100644 --- a/packages/axelar-wasm-std/src/nonempty/mod.rs +++ b/packages/axelar-wasm-std/src/nonempty/mod.rs @@ -3,9 +3,11 @@ mod string; mod timestamp; mod uint; mod vec; +mod hexbinary; pub use error::Error; pub use string::String; pub use timestamp::Timestamp; pub use uint::{Uint128, Uint256, Uint64}; pub use vec::Vec; +pub use hexbinary::HexBinary; From f0810e4b5b2631e64b0c457c0859b966b6243cb7 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Thu, 10 Oct 2024 20:56:14 -0400 Subject: [PATCH 03/14] update ITS message primitives --- .../interchain-token-service/src/primitives.rs | 17 +++++++++-------- .../axelar-wasm-std/src/nonempty/hexbinary.rs | 13 ++++++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/contracts/interchain-token-service/src/primitives.rs b/contracts/interchain-token-service/src/primitives.rs index 3c50d301a..38d6b12ab 100644 --- a/contracts/interchain-token-service/src/primitives.rs +++ b/contracts/interchain-token-service/src/primitives.rs @@ -1,10 +1,11 @@ use std::fmt::Display; use cosmwasm_schema::cw_serde; -use cosmwasm_std::{HexBinary, Uint256}; +use cosmwasm_std::Uint256; use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey}; use router_api::ChainNameRaw; use strum::FromRepr; +use axelar_wasm_std::nonempty; /// A unique 32-byte identifier for linked cross-chain tokens across ITS contracts. #[cw_serde] @@ -44,27 +45,27 @@ pub enum Message { /// The unique identifier of the token being transferred token_id: TokenId, /// The address that called the ITS contract on the source chain - source_address: HexBinary, + source_address: nonempty::HexBinary, /// The address that the token will be sent to on the destination chain /// If data is not empty, this address will given the token and executed as a contract on the destination chain - destination_address: HexBinary, + destination_address: nonempty::HexBinary, /// The amount of tokens to transfer amount: Uint256, /// An optional payload to be provided to the destination address, if `data` is not empty - data: HexBinary, + data: Option, }, /// Deploy a new interchain token on the destination chain DeployInterchainToken { /// The unique identifier of the token to be deployed token_id: TokenId, /// The name of the token - name: String, + name: nonempty::String, /// The symbol of the token - symbol: String, + symbol: nonempty::String, /// The number of decimal places the token supports decimals: u8, /// The address that will be the initial minter of the token (in addition to the ITS contract) - minter: HexBinary, + minter: Option, }, /// Deploy a new token manager on the destination chain DeployTokenManager { @@ -73,7 +74,7 @@ pub enum Message { /// The type of token manager to deploy token_manager_type: TokenManagerType, /// The parameters to be provided to the token manager contract - params: HexBinary, + params: nonempty::HexBinary, }, } diff --git a/packages/axelar-wasm-std/src/nonempty/hexbinary.rs b/packages/axelar-wasm-std/src/nonempty/hexbinary.rs index 89381604f..c5ec38f56 100644 --- a/packages/axelar-wasm-std/src/nonempty/hexbinary.rs +++ b/packages/axelar-wasm-std/src/nonempty/hexbinary.rs @@ -4,6 +4,7 @@ use std::ops::Deref; use crate::nonempty::Error; #[cw_serde] +#[serde(try_from = "cosmwasm_std::HexBinary")] #[derive(Eq, Hash)] pub struct HexBinary(cosmwasm_std::HexBinary); @@ -19,11 +20,11 @@ impl TryFrom for HexBinary { } } -impl TryFrom<&[u8]> for HexBinary { +impl TryFrom> for HexBinary { type Error = Error; - fn try_from(value: &[u8]) -> Result { - HexBinary::try_from(cosmwasm_std::HexBinary::from(value)) + fn try_from(value: std::vec::Vec) -> Result { + cosmwasm_std::HexBinary::from(value).try_into() } } @@ -33,6 +34,12 @@ impl From for cosmwasm_std::HexBinary { } } +impl From for std::vec::Vec { + fn from(value: HexBinary) -> Self { + value.0.into() + } +} + impl Deref for HexBinary { type Target = cosmwasm_std::HexBinary; From 6d03115bfcb872fc718cdd856946e734d72014b7 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Thu, 10 Oct 2024 20:56:32 -0400 Subject: [PATCH 04/14] update abi encode/decode --- contracts/interchain-token-service/src/abi.rs | 62 ++++++++++++------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/contracts/interchain-token-service/src/abi.rs b/contracts/interchain-token-service/src/abi.rs index 18ec31929..89d5959cc 100644 --- a/contracts/interchain-token-service/src/abi.rs +++ b/contracts/interchain-token-service/src/abi.rs @@ -1,6 +1,6 @@ use alloy_primitives::{FixedBytes, U256}; use alloy_sol_types::{sol, SolValue}; -use axelar_wasm_std::{FnExt, IntoContractError}; +use axelar_wasm_std::{FnExt, IntoContractError, nonempty}; use cosmwasm_std::{HexBinary, Uint256}; use error_stack::{bail, ensure, report, Report, ResultExt}; use router_api::ChainNameRaw; @@ -63,14 +63,18 @@ sol! { #[derive(thiserror::Error, Debug, IntoContractError)] pub enum Error { - #[error("failed to decode ITS message")] - MessageDecodeFailed, + #[error("insufficient message length")] + InsufficientMessageLength, #[error("invalid message type")] InvalidMessageType, #[error("invalid chain name")] InvalidChainName, #[error("invalid token manager type")] InvalidTokenManagerType, + #[error(transparent)] + NonEmpty(#[from] nonempty::Error), + #[error(transparent)] + AbiDecodeFailed(#[from] alloy_sol_types::Error), } impl Message { @@ -88,7 +92,7 @@ impl Message { sourceAddress: Vec::::from(source_address).into(), destinationAddress: Vec::::from(destination_address).into(), amount: U256::from_le_bytes(amount.to_le_bytes()), - data: Vec::::from(data).into(), + data: into_vec(data).into(), } .abi_encode_params(), Message::DeployInterchainToken { @@ -100,10 +104,10 @@ impl Message { } => DeployInterchainToken { messageType: MessageType::DeployInterchainToken.into(), tokenId: FixedBytes::<32>::new(token_id.into()), - name, - symbol, + name: name.into(), + symbol: symbol.into(), decimals, - minter: Vec::::from(minter).into(), + minter: into_vec(minter).into(), } .abi_encode_params(), Message::DeployTokenManager { @@ -122,49 +126,50 @@ impl Message { } pub fn abi_decode(payload: &[u8]) -> Result> { - ensure!(payload.len() >= 32, Error::MessageDecodeFailed); + ensure!(payload.len() >= 32, Error::InsufficientMessageLength); let message_type = MessageType::abi_decode(&payload[0..32], true) + .map_err(Error::AbiDecodeFailed) .change_context(Error::InvalidMessageType)?; let message = match message_type { MessageType::InterchainTransfer => { let decoded = InterchainTransfer::abi_decode_params(payload, true) - .change_context(Error::MessageDecodeFailed)?; + .map_err(Error::AbiDecodeFailed)?; Message::InterchainTransfer { token_id: TokenId::new(decoded.tokenId.into()), - source_address: HexBinary::from(decoded.sourceAddress.to_vec()), - destination_address: HexBinary::from(decoded.destinationAddress.as_ref()), + source_address: Vec::::from(decoded.sourceAddress).try_into().map_err(Error::NonEmpty)?, + destination_address: Vec::::from(decoded.destinationAddress).try_into().map_err(Error::NonEmpty)?, amount: Uint256::from_le_bytes(decoded.amount.to_le_bytes()), - data: HexBinary::from(decoded.data.as_ref()), + data: from_vec(decoded.data.into())?, } } MessageType::DeployInterchainToken => { let decoded = DeployInterchainToken::abi_decode_params(payload, true) - .change_context(Error::MessageDecodeFailed)?; + .map_err(Error::AbiDecodeFailed)?; Message::DeployInterchainToken { token_id: TokenId::new(decoded.tokenId.into()), - name: decoded.name, - symbol: decoded.symbol, + name: decoded.name.try_into().map_err(Error::NonEmpty)?, + symbol: decoded.symbol.try_into().map_err(Error::NonEmpty)?, decimals: decoded.decimals, - minter: HexBinary::from(decoded.minter.as_ref()), + minter: from_vec(decoded.minter.into())?, } } MessageType::DeployTokenManager => { let decoded = DeployTokenManager::abi_decode_params(payload, true) - .change_context(Error::MessageDecodeFailed)?; + .map_err(Error::AbiDecodeFailed)?; let token_manager_type = u8::try_from(decoded.tokenManagerType) .change_context(Error::InvalidTokenManagerType)? .then(TokenManagerType::from_repr) - .ok_or_else(|| report!(Error::InvalidTokenManagerType))?; + .ok_or_else(|| Error::InvalidTokenManagerType)?; Message::DeployTokenManager { token_id: TokenId::new(decoded.tokenId.into()), token_manager_type, - params: HexBinary::from(decoded.params.as_ref()), + params: Vec::::from(decoded.params).try_into().map_err(Error::NonEmpty)?, } } _ => bail!(Error::InvalidMessageType), @@ -201,15 +206,16 @@ impl HubMessage { } pub fn abi_decode(payload: &[u8]) -> Result> { - ensure!(payload.len() >= 32, Error::MessageDecodeFailed); + ensure!(payload.len() >= 32, Error::InsufficientMessageLength); let message_type = MessageType::abi_decode(&payload[0..32], true) + .map_err(Error::AbiDecodeFailed) .change_context(Error::InvalidMessageType)?; let hub_message = match message_type { MessageType::SendToHub => { let decoded = SendToHub::abi_decode_params(payload, true) - .change_context(Error::MessageDecodeFailed)?; + .map_err(Error::AbiDecodeFailed)?; HubMessage::SendToHub { destination_chain: ChainNameRaw::try_from(decoded.destination_chain) @@ -219,7 +225,7 @@ impl HubMessage { } MessageType::ReceiveFromHub => { let decoded = ReceiveFromHub::abi_decode_params(payload, true) - .change_context(Error::MessageDecodeFailed)?; + .map_err(Error::AbiDecodeFailed)?; HubMessage::ReceiveFromHub { source_chain: ChainNameRaw::try_from(decoded.source_chain) @@ -246,6 +252,18 @@ impl From for U256 { } } +fn into_vec(value: Option) -> std::vec::Vec { + value.map(|v| v.into()).unwrap_or_default() +} + +fn from_vec(value: std::vec::Vec) -> Result, Error> { + if value.is_empty() { + None + } else { + Some(nonempty::HexBinary::try_from(value)?) + }.then(Ok) +} + #[cfg(test)] mod tests { use std::str::FromStr; From 882508dbdaf0e525072745185003d77020656e43 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Thu, 10 Oct 2024 22:11:36 -0400 Subject: [PATCH 05/14] update event --- .../interchain-token-service/src/events.rs | 54 +++++++++---------- packages/axelar-wasm-std/src/event.rs | 30 +---------- 2 files changed, 28 insertions(+), 56 deletions(-) diff --git a/contracts/interchain-token-service/src/events.rs b/contracts/interchain-token-service/src/events.rs index aa5989a37..6507b97af 100644 --- a/contracts/interchain-token-service/src/events.rs +++ b/contracts/interchain-token-service/src/events.rs @@ -60,10 +60,10 @@ fn make_message_event( } => { event .add_attribute("token_id", token_id.to_string()) - .add_attribute_if_nonempty("source_address", source_address.to_string()) - .add_attribute_if_nonempty("destination_address", destination_address.to_string()) + .add_attribute("source_address", source_address.to_string()) + .add_attribute("destination_address", destination_address.to_string()) .add_attribute("amount", amount.to_string()) - .add_attribute_if_nonempty("data", data.to_string()) + .add_attribute_if_some("data", data.map(|data| data.to_string())) } Message::DeployInterchainToken { token_id, @@ -74,10 +74,10 @@ fn make_message_event( } => { event .add_attribute("token_id", token_id.to_string()) - .add_attribute_if_nonempty("name", name) - .add_attribute_if_nonempty("symbol", symbol) + .add_attribute("name", name) + .add_attribute("symbol", symbol) .add_attribute("decimals", decimals.to_string()) - .add_attribute_if_nonempty("minter", minter.to_string()) + .add_attribute_if_some("minter", minter.map(|minter| minter.to_string())) } Message::DeployTokenManager { token_id, @@ -87,7 +87,7 @@ fn make_message_event( event .add_attribute("token_id", token_id.to_string()) .add_attribute("token_manager_type", token_manager_type.as_ref().to_string()) - .add_attribute_if_nonempty("params", params.to_string()) + .add_attribute("params", params.to_string()) } } } @@ -104,22 +104,22 @@ mod test { let test_cases: Vec = vec![ Message::InterchainTransfer { token_id: TokenId::new([1; 32]), - source_address: HexBinary::from([1; 32]), - destination_address: HexBinary::from([1, 2, 3, 4]), + source_address: HexBinary::from([1; 32]).try_into().unwrap(), + destination_address: HexBinary::from([1, 2, 3, 4]).try_into().unwrap(), amount: 1u64.into(), - data: HexBinary::from([1, 2, 3, 4]), + data: Some(HexBinary::from([1, 2, 3, 4]).try_into().unwrap()), }, Message::DeployInterchainToken { token_id: TokenId::new([1; 32]), - name: "Test".into(), - symbol: "TST".into(), + name: "Test".try_into().unwrap(), + symbol: "TST".try_into().unwrap(), decimals: 18, - minter: HexBinary::from([1; 32]), + minter: Some(HexBinary::from([1; 32]).try_into().unwrap()), }, Message::DeployTokenManager { token_id: TokenId::new([1; 32]), token_manager_type: TokenManagerType::MintBurn, - params: HexBinary::from([1, 2, 3, 4]), + params: HexBinary::from([1, 2, 3, 4]).try_into().unwrap(), }, ]; @@ -141,36 +141,36 @@ mod test { let test_cases: Vec = vec![ Message::InterchainTransfer { token_id: TokenId::new([1; 32]), - source_address: HexBinary::from([1; 32]), - destination_address: HexBinary::from([1, 2, 3, 4]), + source_address: HexBinary::from([1; 32]).try_into().unwrap(), + destination_address: HexBinary::from([1, 2, 3, 4]).try_into().unwrap(), amount: 1u64.into(), - data: HexBinary::default(), + data: None, }, Message::InterchainTransfer { token_id: TokenId::new([1; 32]), - source_address: HexBinary::default(), - destination_address: HexBinary::default(), + source_address: HexBinary::from([0u8]).try_into().unwrap(), + destination_address: HexBinary::from([0u8]).try_into().unwrap(), amount: 1u64.into(), - data: HexBinary::default(), + data: None, }, Message::DeployInterchainToken { token_id: TokenId::new([1; 32]), - name: "Test".into(), - symbol: "TST".into(), + name: "Test".try_into().unwrap(), + symbol: "TST".try_into().unwrap(), decimals: 18, - minter: HexBinary::default(), + minter: None, }, Message::DeployInterchainToken { token_id: TokenId::new([1; 32]), - name: "".into(), - symbol: "".into(), + name: "t".try_into().unwrap(), + symbol: "T".try_into().unwrap(), decimals: 0, - minter: HexBinary::default(), + minter: None, }, Message::DeployTokenManager { token_id: TokenId::new([1; 32]), token_manager_type: TokenManagerType::MintBurn, - params: HexBinary::default(), + params: HexBinary::from([0u8]).try_into().unwrap(), }, ]; diff --git a/packages/axelar-wasm-std/src/event.rs b/packages/axelar-wasm-std/src/event.rs index cc3514e97..7f0236675 100644 --- a/packages/axelar-wasm-std/src/event.rs +++ b/packages/axelar-wasm-std/src/event.rs @@ -5,11 +5,6 @@ pub trait EventExt { where K: Into, V: Into; - - fn add_attribute_if_nonempty(self, key: K, value: V) -> Self - where - K: Into, - V: Into; } impl EventExt for Event { @@ -23,24 +18,11 @@ impl EventExt for Event { None => self, } } - - fn add_attribute_if_nonempty(self, key: K, value: V) -> Self - where - K: Into, - V: Into, - { - let value: String = value.into(); - - match value.is_empty() { - false => self.add_attribute(key, value), - true => self, - } - } } #[cfg(test)] mod test { - use cosmwasm_std::{Event, HexBinary, Int128}; + use cosmwasm_std::{Event, Int128}; use super::EventExt; @@ -53,14 +35,4 @@ mod test { goldie::assert_json!(event) } - - #[test] - fn add_attribute_if_nonempty() { - let event = Event::new("test") - .add_attribute_if_nonempty("foo", "bar") - .add_attribute_if_nonempty("int", "") - .add_attribute_if_nonempty("baz", HexBinary::default().to_string()); - - goldie::assert_json!(event) - } } From c978357bee06e77547314ea7fcf1719d98d10755 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Thu, 10 Oct 2024 22:12:06 -0400 Subject: [PATCH 06/14] update tests --- contracts/interchain-token-service/src/abi.rs | 94 +++++++++---------- .../interchain-token-service/src/contract.rs | 12 +-- .../src/contract/execute.rs | 69 +++++++------- .../interchain-token-service/tests/execute.rs | 14 +-- .../tests/utils/messages.rs | 6 +- 5 files changed, 97 insertions(+), 98 deletions(-) diff --git a/contracts/interchain-token-service/src/abi.rs b/contracts/interchain-token-service/src/abi.rs index 89d5959cc..2133d1572 100644 --- a/contracts/interchain-token-service/src/abi.rs +++ b/contracts/interchain-token-service/src/abi.rs @@ -271,13 +271,17 @@ mod tests { use alloy_primitives::{FixedBytes, U256}; use alloy_sol_types::SolValue; use assert_ok::assert_ok; - use axelar_wasm_std::assert_err_contains; + use axelar_wasm_std::{assert_err_contains, nonempty}; use cosmwasm_std::{HexBinary, Uint256}; use router_api::ChainNameRaw; use crate::abi::{DeployTokenManager, Error, MessageType, SendToHub}; use crate::{HubMessage, Message, TokenManagerType}; + fn from_hex(hex: &str) -> nonempty::HexBinary { + HexBinary::from_hex(hex).unwrap().try_into().unwrap() + } + #[test] fn interchain_transfer_encode_decode() { let remote_chain = ChainNameRaw::from_str("chain").unwrap(); @@ -287,48 +291,40 @@ mod tests { destination_chain: remote_chain.clone(), message: Message::InterchainTransfer { token_id: [0u8; 32].into(), - source_address: HexBinary::from_hex("").unwrap(), - destination_address: HexBinary::from_hex("").unwrap(), + source_address: from_hex("00"), + destination_address: from_hex("00"), amount: Uint256::zero(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }, HubMessage::SendToHub { destination_chain: remote_chain.clone(), message: Message::InterchainTransfer { token_id: [255u8; 32].into(), - source_address: HexBinary::from_hex("4F4495243837681061C4743b74B3eEdf548D56A5") - .unwrap(), - destination_address: HexBinary::from_hex( - "4F4495243837681061C4743b74B3eEdf548D56A5", - ) - .unwrap(), + source_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"), + destination_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"), amount: Uint256::MAX, - data: HexBinary::from_hex("abcd").unwrap(), + data: Some(from_hex("abcd")), }, }, HubMessage::ReceiveFromHub { source_chain: remote_chain.clone(), message: Message::InterchainTransfer { token_id: [0u8; 32].into(), - source_address: HexBinary::from_hex("").unwrap(), - destination_address: HexBinary::from_hex("").unwrap(), + source_address: from_hex("00"), + destination_address: from_hex("00"), amount: Uint256::zero(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }, HubMessage::ReceiveFromHub { source_chain: remote_chain.clone(), message: Message::InterchainTransfer { token_id: [255u8; 32].into(), - source_address: HexBinary::from_hex("4F4495243837681061C4743b74B3eEdf548D56A5") - .unwrap(), - destination_address: HexBinary::from_hex( - "4F4495243837681061C4743b74B3eEdf548D56A5", - ) - .unwrap(), + source_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"), + destination_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"), amount: Uint256::MAX, - data: HexBinary::from_hex("abcd").unwrap(), + data: Some(from_hex("abcd")), }, }, ]; @@ -356,60 +352,60 @@ mod tests { destination_chain: remote_chain.clone(), message: Message::DeployInterchainToken { token_id: [0u8; 32].into(), - name: "".into(), - symbol: "".into(), + name: "t".try_into().unwrap(), + symbol: "T".try_into().unwrap(), decimals: 0, - minter: HexBinary::from_hex("").unwrap(), + minter: None, }, }, HubMessage::SendToHub { destination_chain: remote_chain.clone(), message: Message::DeployInterchainToken { token_id: [1u8; 32].into(), - name: "Test Token".into(), - symbol: "TST".into(), + name: "Test Token".try_into().unwrap(), + symbol: "TST".try_into().unwrap(), decimals: 18, - minter: HexBinary::from_hex("1234").unwrap(), + minter: Some(from_hex("1234")), }, }, HubMessage::SendToHub { destination_chain: remote_chain.clone(), message: Message::DeployInterchainToken { token_id: [0u8; 32].into(), - name: "Unicode Token 🪙".into(), - symbol: "UNI🔣".into(), + name: "Unicode Token 🪙".try_into().unwrap(), + symbol: "UNI🔣".try_into().unwrap(), decimals: 255, - minter: HexBinary::from_hex("abcd").unwrap(), + minter: Some(from_hex("abcd")), }, }, HubMessage::ReceiveFromHub { source_chain: remote_chain.clone(), message: Message::DeployInterchainToken { token_id: [0u8; 32].into(), - name: "".into(), - symbol: "".into(), + name: "t".try_into().unwrap(), + symbol: "T".try_into().unwrap(), decimals: 0, - minter: HexBinary::from_hex("").unwrap(), + minter: None, }, }, HubMessage::ReceiveFromHub { source_chain: remote_chain.clone(), message: Message::DeployInterchainToken { token_id: [1u8; 32].into(), - name: "Test Token".into(), - symbol: "TST".into(), + name: "Test Token".try_into().unwrap(), + symbol: "TST".try_into().unwrap(), decimals: 18, - minter: HexBinary::from_hex("1234").unwrap(), + minter: Some(from_hex("1234")), }, }, HubMessage::ReceiveFromHub { source_chain: remote_chain.clone(), message: Message::DeployInterchainToken { token_id: [0u8; 32].into(), - name: "Unicode Token 🪙".into(), - symbol: "UNI🔣".into(), + name: "Unicode Token 🪙".try_into().unwrap(), + symbol: "UNI🔣".try_into().unwrap(), decimals: 255, - minter: HexBinary::from_hex("abcd").unwrap(), + minter: Some(from_hex("abcd")), }, }, ]; @@ -438,7 +434,7 @@ mod tests { message: Message::DeployTokenManager { token_id: [0u8; 32].into(), token_manager_type: TokenManagerType::NativeInterchainToken, - params: HexBinary::default(), + params: from_hex("00"), }, }, HubMessage::SendToHub { @@ -446,7 +442,7 @@ mod tests { message: Message::DeployTokenManager { token_id: [1u8; 32].into(), token_manager_type: TokenManagerType::Gateway, - params: HexBinary::from_hex("1234").unwrap(), + params: from_hex("1234"), }, }, HubMessage::ReceiveFromHub { @@ -454,7 +450,7 @@ mod tests { message: Message::DeployTokenManager { token_id: [0u8; 32].into(), token_manager_type: TokenManagerType::NativeInterchainToken, - params: HexBinary::default(), + params: from_hex("00"), }, }, HubMessage::ReceiveFromHub { @@ -462,7 +458,7 @@ mod tests { message: Message::DeployTokenManager { token_id: [1u8; 32].into(), token_manager_type: TokenManagerType::Gateway, - params: HexBinary::from_hex("1234").unwrap(), + params: from_hex("1234"), }, }, ]; @@ -574,10 +570,10 @@ mod tests { destination_chain: ChainNameRaw::from_str("large-data-chain").unwrap(), message: Message::InterchainTransfer { token_id: [0u8; 32].into(), - source_address: HexBinary::from_hex("1234").unwrap(), - destination_address: HexBinary::from_hex("5678").unwrap(), + source_address: from_hex("1234"), + destination_address: from_hex("5678"), amount: Uint256::from(1u128), - data: HexBinary::from(large_data), + data: Some(large_data.try_into().unwrap()), }, }; @@ -592,10 +588,10 @@ mod tests { destination_chain: ChainNameRaw::from_str("chain").unwrap(), message: Message::DeployInterchainToken { token_id: [0u8; 32].into(), - name: "Unicode Token 🪙".into(), - symbol: "UNI🔣".into(), + name: "Unicode Token 🪙".try_into().unwrap(), + symbol: "UNI🔣".try_into().unwrap(), decimals: 18, - minter: HexBinary::from_hex("abcd").unwrap(), + minter: Some(from_hex("abcd")), }, }; diff --git a/contracts/interchain-token-service/src/contract.rs b/contracts/interchain-token-service/src/contract.rs index 8e13f06b3..2a24fcd05 100644 --- a/contracts/interchain-token-service/src/contract.rs +++ b/contracts/interchain-token-service/src/contract.rs @@ -248,10 +248,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::InterchainTransfer { token_id: token_id.clone(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(its_address).unwrap(), + source_address: source_address.clone().try_into().unwrap(), + destination_address: HexBinary::from_hex(its_address).unwrap().try_into().unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; @@ -276,10 +276,10 @@ mod tests { destination_chain: source_chain.clone(), message: Message::InterchainTransfer { token_id: token_id.clone(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(its_address).unwrap(), + source_address: source_address.clone().try_into().unwrap(), + destination_address: HexBinary::from_hex(its_address).unwrap().try_into().unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; diff --git a/contracts/interchain-token-service/src/contract/execute.rs b/contracts/interchain-token-service/src/contract/execute.rs index 36f57d9d5..264728c11 100644 --- a/contracts/interchain-token-service/src/contract/execute.rs +++ b/contracts/interchain-token-service/src/contract/execute.rs @@ -328,10 +328,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::InterchainTransfer { token_id: token_id.clone(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.clone().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; assert_ok!(execute_message( @@ -352,10 +352,13 @@ mod tests { destination_chain: source_chain.clone(), message: Message::InterchainTransfer { token_id, - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; @@ -401,10 +404,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::InterchainTransfer { token_id: [0u8; 32].into(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; let res = execute_message( @@ -454,10 +457,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::InterchainTransfer { token_id: token_id.clone(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), amount: amount_in_msg.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; let res = execute_message( @@ -508,10 +511,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::InterchainTransfer { token_id: token_id.clone(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; let res = execute_message( @@ -560,10 +563,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::InterchainTransfer { token_id: token_id.clone(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; let res = execute_message( @@ -612,10 +615,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::InterchainTransfer { token_id: token_id.clone(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.clone().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; let res = assert_ok!(execute_message( @@ -651,10 +654,10 @@ mod tests { destination_chain: second_destination_chain.clone(), message: Message::InterchainTransfer { token_id, - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; @@ -699,10 +702,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::InterchainTransfer { token_id: token_id.clone(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.clone().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), amount: Uint256::one(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; @@ -742,10 +745,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::InterchainTransfer { token_id: token_id.clone(), - source_address: source_address.clone(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap(), + source_address: source_address.clone().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), amount: coin.amount.into(), - data: HexBinary::from_hex("").unwrap(), + data: None, }, }; let res = execute_message( @@ -791,10 +794,10 @@ mod tests { destination_chain: destination_chain.clone(), message: Message::DeployInterchainToken { token_id: token_id.clone(), - name: "foobar".to_string(), - symbol: "FOO".to_string(), + name: "foobar".try_into().unwrap(), + symbol: "FOO".try_into().unwrap(), decimals: 10u8, - minter: HexBinary::from([0u8; 32]), + minter: Some(HexBinary::from([0u8; 32]).try_into().unwrap()), }, }; let res = execute_message( diff --git a/contracts/interchain-token-service/tests/execute.rs b/contracts/interchain-token-service/tests/execute.rs index 16e2b8957..0206619e8 100644 --- a/contracts/interchain-token-service/tests/execute.rs +++ b/contracts/interchain-token-service/tests/execute.rs @@ -107,22 +107,22 @@ fn execute_hub_message_succeeds() { let test_messages = vec![ Message::InterchainTransfer { token_id: token_id.clone(), - source_address: HexBinary::from([1; 32]), - destination_address: HexBinary::from([2; 32]), + source_address: HexBinary::from([1; 32]).try_into().unwrap(), + destination_address: HexBinary::from([2; 32]).try_into().unwrap(), amount: 1u64.into(), - data: HexBinary::from([1, 2, 3, 4]), + data: Some(HexBinary::from([1, 2, 3, 4]).try_into().unwrap()), }, Message::DeployInterchainToken { token_id: token_id.clone(), - name: "Test".into(), - symbol: "TST".into(), + name: "Test".try_into().unwrap(), + symbol: "TST".try_into().unwrap(), decimals: 18, - minter: HexBinary::from([1; 32]), + minter: Some(HexBinary::from([1; 32]).try_into().unwrap()), }, Message::DeployTokenManager { token_id: token_id.clone(), token_manager_type: TokenManagerType::MintBurn, - params: HexBinary::from([1, 2, 3, 4]), + params: HexBinary::from([1, 2, 3, 4]).try_into().unwrap(), }, ]; diff --git a/contracts/interchain-token-service/tests/utils/messages.rs b/contracts/interchain-token-service/tests/utils/messages.rs index cc843b590..2e077c570 100644 --- a/contracts/interchain-token-service/tests/utils/messages.rs +++ b/contracts/interchain-token-service/tests/utils/messages.rs @@ -5,10 +5,10 @@ use router_api::{Address, ChainNameRaw, CrossChainId}; pub fn dummy_message() -> Message { Message::InterchainTransfer { token_id: TokenId::new([2; 32]), - source_address: HexBinary::from_hex("1234").unwrap(), - destination_address: HexBinary::from_hex("5678").unwrap(), + source_address: HexBinary::from_hex("1234").unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex("5678").unwrap().try_into().unwrap(), amount: Uint256::from(1000u64), - data: HexBinary::from_hex("abcd").unwrap(), + data: Some(HexBinary::from_hex("abcd").unwrap().try_into().unwrap()), } } From 950653b011033063e6b0b124c47e31c9141d0809 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Thu, 10 Oct 2024 22:12:10 -0400 Subject: [PATCH 07/14] update testdata --- ...ploy_interchain_token_encode_decode.golden | 4 ++-- .../deploy_token_manager_encode_decode.golden | 4 ++-- .../interchain_transfer_encode_decode.golden | 4 ++-- ...sage_received_with_empty_attributes.golden | 20 +++++++++++++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/contracts/interchain-token-service/src/testdata/deploy_interchain_token_encode_decode.golden b/contracts/interchain-token-service/src/testdata/deploy_interchain_token_encode_decode.golden index e642fc6d3..76198c155 100644 --- a/contracts/interchain-token-service/src/testdata/deploy_interchain_token_encode_decode.golden +++ b/contracts/interchain-token-service/src/testdata/deploy_interchain_token_encode_decode.golden @@ -1,8 +1,8 @@ [ - "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000017400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000154000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000001010101010101010101010101010101010101010101010101010101010101010100000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000a5465737420546f6b656e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003545354000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000ff00000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000012556e69636f646520546f6b656e20f09faa9900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007554e49f09f94a3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002abcd000000000000000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000017400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000154000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000001010101010101010101010101010101010101010101010101010101010101010100000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000a5465737420546f6b656e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003545354000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000ff00000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000012556e69636f646520546f6b656e20f09faa9900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007554e49f09f94a3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002abcd000000000000000000000000000000000000000000000000000000000000" ] \ No newline at end of file diff --git a/contracts/interchain-token-service/src/testdata/deploy_token_manager_encode_decode.golden b/contracts/interchain-token-service/src/testdata/deploy_token_manager_encode_decode.golden index 9bd957c4a..f4d085e67 100644 --- a/contracts/interchain-token-service/src/testdata/deploy_token_manager_encode_decode.golden +++ b/contracts/interchain-token-service/src/testdata/deploy_token_manager_encode_decode.golden @@ -1,6 +1,6 @@ [ - "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000201010101010101010101010101010101010101010101010101010101010101010000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000201010101010101010101010101010101010101010101010101010101010101010000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000" ] \ No newline at end of file diff --git a/contracts/interchain-token-service/src/testdata/interchain_transfer_encode_decode.golden b/contracts/interchain-token-service/src/testdata/interchain_transfer_encode_decode.golden index eb9766e8a..44fdf0c6e 100644 --- a/contracts/interchain-token-service/src/testdata/interchain_transfer_encode_decode.golden +++ b/contracts/interchain-token-service/src/testdata/interchain_transfer_encode_decode.golden @@ -1,6 +1,6 @@ [ - "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a500000000000000000000000000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a50000000000000000000000000000000000000000000000000000000000000000000000000000000000000002abcd000000000000000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a500000000000000000000000000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a50000000000000000000000000000000000000000000000000000000000000000000000000000000000000002abcd000000000000000000000000000000000000000000000000000000000000" ] \ No newline at end of file diff --git a/contracts/interchain-token-service/src/testdata/message_received_with_empty_attributes.golden b/contracts/interchain-token-service/src/testdata/message_received_with_empty_attributes.golden index 0aa3dfa37..dcc2a9da3 100644 --- a/contracts/interchain-token-service/src/testdata/message_received_with_empty_attributes.golden +++ b/contracts/interchain-token-service/src/testdata/message_received_with_empty_attributes.golden @@ -51,6 +51,14 @@ "key": "token_id", "value": "0101010101010101010101010101010101010101010101010101010101010101" }, + { + "key": "source_address", + "value": "00" + }, + { + "key": "destination_address", + "value": "00" + }, { "key": "amount", "value": "1" @@ -109,6 +117,14 @@ "key": "token_id", "value": "0101010101010101010101010101010101010101010101010101010101010101" }, + { + "key": "name", + "value": "t" + }, + { + "key": "symbol", + "value": "T" + }, { "key": "decimals", "value": "0" @@ -137,6 +153,10 @@ { "key": "token_manager_type", "value": "MintBurn" + }, + { + "key": "params", + "value": "00" } ] } From d5e69dfa7c397556d5ea33ad1119851880ebebef Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Thu, 10 Oct 2024 22:12:44 -0400 Subject: [PATCH 08/14] fmt --- contracts/interchain-token-service/src/abi.rs | 21 +++-- .../interchain-token-service/src/contract.rs | 10 ++- .../src/contract/execute.rs | 45 ++++++++-- .../interchain-token-service/src/events.rs | 82 ++++++++++--------- .../src/primitives.rs | 2 +- .../axelar-wasm-std/src/nonempty/hexbinary.rs | 3 +- packages/axelar-wasm-std/src/nonempty/mod.rs | 4 +- 7 files changed, 106 insertions(+), 61 deletions(-) diff --git a/contracts/interchain-token-service/src/abi.rs b/contracts/interchain-token-service/src/abi.rs index 2133d1572..fef38676f 100644 --- a/contracts/interchain-token-service/src/abi.rs +++ b/contracts/interchain-token-service/src/abi.rs @@ -1,6 +1,6 @@ use alloy_primitives::{FixedBytes, U256}; use alloy_sol_types::{sol, SolValue}; -use axelar_wasm_std::{FnExt, IntoContractError, nonempty}; +use axelar_wasm_std::{nonempty, FnExt, IntoContractError}; use cosmwasm_std::{HexBinary, Uint256}; use error_stack::{bail, ensure, report, Report, ResultExt}; use router_api::ChainNameRaw; @@ -139,8 +139,12 @@ impl Message { Message::InterchainTransfer { token_id: TokenId::new(decoded.tokenId.into()), - source_address: Vec::::from(decoded.sourceAddress).try_into().map_err(Error::NonEmpty)?, - destination_address: Vec::::from(decoded.destinationAddress).try_into().map_err(Error::NonEmpty)?, + source_address: Vec::::from(decoded.sourceAddress) + .try_into() + .map_err(Error::NonEmpty)?, + destination_address: Vec::::from(decoded.destinationAddress) + .try_into() + .map_err(Error::NonEmpty)?, amount: Uint256::from_le_bytes(decoded.amount.to_le_bytes()), data: from_vec(decoded.data.into())?, } @@ -169,7 +173,9 @@ impl Message { Message::DeployTokenManager { token_id: TokenId::new(decoded.tokenId.into()), token_manager_type, - params: Vec::::from(decoded.params).try_into().map_err(Error::NonEmpty)?, + params: Vec::::from(decoded.params) + .try_into() + .map_err(Error::NonEmpty)?, } } _ => bail!(Error::InvalidMessageType), @@ -214,8 +220,8 @@ impl HubMessage { let hub_message = match message_type { MessageType::SendToHub => { - let decoded = SendToHub::abi_decode_params(payload, true) - .map_err(Error::AbiDecodeFailed)?; + let decoded = + SendToHub::abi_decode_params(payload, true).map_err(Error::AbiDecodeFailed)?; HubMessage::SendToHub { destination_chain: ChainNameRaw::try_from(decoded.destination_chain) @@ -261,7 +267,8 @@ fn from_vec(value: std::vec::Vec) -> Result, Err None } else { Some(nonempty::HexBinary::try_from(value)?) - }.then(Ok) + } + .then(Ok) } #[cfg(test)] diff --git a/contracts/interchain-token-service/src/contract.rs b/contracts/interchain-token-service/src/contract.rs index 2a24fcd05..7f6001090 100644 --- a/contracts/interchain-token-service/src/contract.rs +++ b/contracts/interchain-token-service/src/contract.rs @@ -249,7 +249,10 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(its_address).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(its_address) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), data: None, }, @@ -277,7 +280,10 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(its_address).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(its_address) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), data: None, }, diff --git a/contracts/interchain-token-service/src/contract/execute.rs b/contracts/interchain-token-service/src/contract/execute.rs index 264728c11..0e3543410 100644 --- a/contracts/interchain-token-service/src/contract/execute.rs +++ b/contracts/interchain-token-service/src/contract/execute.rs @@ -329,7 +329,10 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), data: None, }, @@ -405,7 +408,10 @@ mod tests { message: Message::InterchainTransfer { token_id: [0u8; 32].into(), source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), data: None, }, @@ -458,7 +464,10 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: amount_in_msg.into(), data: None, }, @@ -512,7 +521,10 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), data: None, }, @@ -564,7 +576,10 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), data: None, }, @@ -616,7 +631,10 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), data: None, }, @@ -655,7 +673,10 @@ mod tests { message: Message::InterchainTransfer { token_id, source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), data: None, }, @@ -703,7 +724,10 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: Uint256::one(), data: None, }, @@ -746,7 +770,10 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS).unwrap().try_into().unwrap(), + destination_address: HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap(), amount: coin.amount.into(), data: None, }, diff --git a/contracts/interchain-token-service/src/events.rs b/contracts/interchain-token-service/src/events.rs index 6507b97af..d86b74bc9 100644 --- a/contracts/interchain-token-service/src/events.rs +++ b/contracts/interchain-token-service/src/events.rs @@ -57,38 +57,35 @@ fn make_message_event( destination_address, amount, data, - } => { - event - .add_attribute("token_id", token_id.to_string()) - .add_attribute("source_address", source_address.to_string()) - .add_attribute("destination_address", destination_address.to_string()) - .add_attribute("amount", amount.to_string()) - .add_attribute_if_some("data", data.map(|data| data.to_string())) - } + } => event + .add_attribute("token_id", token_id.to_string()) + .add_attribute("source_address", source_address.to_string()) + .add_attribute("destination_address", destination_address.to_string()) + .add_attribute("amount", amount.to_string()) + .add_attribute_if_some("data", data.map(|data| data.to_string())), Message::DeployInterchainToken { token_id, name, symbol, decimals, minter, - } => { - event - .add_attribute("token_id", token_id.to_string()) - .add_attribute("name", name) - .add_attribute("symbol", symbol) - .add_attribute("decimals", decimals.to_string()) - .add_attribute_if_some("minter", minter.map(|minter| minter.to_string())) - } + } => event + .add_attribute("token_id", token_id.to_string()) + .add_attribute("name", name) + .add_attribute("symbol", symbol) + .add_attribute("decimals", decimals.to_string()) + .add_attribute_if_some("minter", minter.map(|minter| minter.to_string())), Message::DeployTokenManager { token_id, token_manager_type, params, - } => { - event - .add_attribute("token_id", token_id.to_string()) - .add_attribute("token_manager_type", token_manager_type.as_ref().to_string()) - .add_attribute("params", params.to_string()) - } + } => event + .add_attribute("token_id", token_id.to_string()) + .add_attribute( + "token_manager_type", + token_manager_type.as_ref().to_string(), + ) + .add_attribute("params", params.to_string()), } } @@ -97,7 +94,8 @@ mod test { use cosmwasm_std::HexBinary; use router_api::CrossChainId; - use crate::{events::Event, Message, TokenId, TokenManagerType}; + use crate::events::Event; + use crate::{Message, TokenId, TokenManagerType}; #[test] fn message_received_with_all_attributes() { @@ -123,15 +121,18 @@ mod test { }, ]; - let events: Vec<_> = test_cases.into_iter().map(|message| { - let event = Event::MessageReceived { - cc_id: CrossChainId::new("source", "hash").unwrap(), - destination_chain: "destination".parse().unwrap(), - message, - }; + let events: Vec<_> = test_cases + .into_iter() + .map(|message| { + let event = Event::MessageReceived { + cc_id: CrossChainId::new("source", "hash").unwrap(), + destination_chain: "destination".parse().unwrap(), + message, + }; - cosmwasm_std::Event::from(event) - }).collect(); + cosmwasm_std::Event::from(event) + }) + .collect(); goldie::assert_json!(events); } @@ -174,15 +175,18 @@ mod test { }, ]; - let events: Vec<_> = test_cases.into_iter().map(|message| { - let event = Event::MessageReceived { - cc_id: CrossChainId::new("source", "hash").unwrap(), - destination_chain: "destination".parse().unwrap(), - message, - }; + let events: Vec<_> = test_cases + .into_iter() + .map(|message| { + let event = Event::MessageReceived { + cc_id: CrossChainId::new("source", "hash").unwrap(), + destination_chain: "destination".parse().unwrap(), + message, + }; - cosmwasm_std::Event::from(event) - }).collect(); + cosmwasm_std::Event::from(event) + }) + .collect(); goldie::assert_json!(events); } diff --git a/contracts/interchain-token-service/src/primitives.rs b/contracts/interchain-token-service/src/primitives.rs index 38d6b12ab..215d921e5 100644 --- a/contracts/interchain-token-service/src/primitives.rs +++ b/contracts/interchain-token-service/src/primitives.rs @@ -1,11 +1,11 @@ use std::fmt::Display; +use axelar_wasm_std::nonempty; use cosmwasm_schema::cw_serde; use cosmwasm_std::Uint256; use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey}; use router_api::ChainNameRaw; use strum::FromRepr; -use axelar_wasm_std::nonempty; /// A unique 32-byte identifier for linked cross-chain tokens across ITS contracts. #[cw_serde] diff --git a/packages/axelar-wasm-std/src/nonempty/hexbinary.rs b/packages/axelar-wasm-std/src/nonempty/hexbinary.rs index c5ec38f56..edc4f7912 100644 --- a/packages/axelar-wasm-std/src/nonempty/hexbinary.rs +++ b/packages/axelar-wasm-std/src/nonempty/hexbinary.rs @@ -1,6 +1,7 @@ -use cosmwasm_schema::cw_serde; use std::ops::Deref; +use cosmwasm_schema::cw_serde; + use crate::nonempty::Error; #[cw_serde] diff --git a/packages/axelar-wasm-std/src/nonempty/mod.rs b/packages/axelar-wasm-std/src/nonempty/mod.rs index 983163284..107122b24 100644 --- a/packages/axelar-wasm-std/src/nonempty/mod.rs +++ b/packages/axelar-wasm-std/src/nonempty/mod.rs @@ -1,13 +1,13 @@ mod error; +mod hexbinary; mod string; mod timestamp; mod uint; mod vec; -mod hexbinary; pub use error::Error; +pub use hexbinary::HexBinary; pub use string::String; pub use timestamp::Timestamp; pub use uint::{Uint128, Uint256, Uint64}; pub use vec::Vec; -pub use hexbinary::HexBinary; From bbacc1690e4c1ba680f848681d8bd0c46b8cf044 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Thu, 10 Oct 2024 23:09:24 -0400 Subject: [PATCH 09/14] add test coverage --- contracts/interchain-token-service/src/abi.rs | 53 +++++++++++++++++++ .../src/primitives.rs | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/contracts/interchain-token-service/src/abi.rs b/contracts/interchain-token-service/src/abi.rs index fef38676f..a7106aff0 100644 --- a/contracts/interchain-token-service/src/abi.rs +++ b/contracts/interchain-token-service/src/abi.rs @@ -282,6 +282,7 @@ mod tests { use cosmwasm_std::{HexBinary, Uint256}; use router_api::ChainNameRaw; + use super::{DeployInterchainToken, InterchainTransfer}; use crate::abi::{DeployTokenManager, Error, MessageType, SendToHub}; use crate::{HubMessage, Message, TokenManagerType}; @@ -350,6 +351,58 @@ mod tests { } } + #[test] + fn fail_decode_on_empty_fields() { + let test_cases = vec![ + InterchainTransfer { + messageType: MessageType::InterchainTransfer.into(), + tokenId: FixedBytes::<32>::new([1u8; 32]), + sourceAddress: vec![1, 2].into(), + destinationAddress: vec![].into(), + amount: U256::from(0), + data: vec![].into(), + } + .abi_encode_params(), + DeployInterchainToken { + messageType: MessageType::DeployInterchainToken.into(), + tokenId: FixedBytes::<32>::new([1u8; 32]), + name: "".into(), + symbol: "TEST".into(), + decimals: 0, + minter: vec![].into(), + } + .abi_encode_params(), + DeployInterchainToken { + messageType: MessageType::DeployInterchainToken.into(), + tokenId: FixedBytes::<32>::new([1u8; 32]), + name: "Test".into(), + symbol: "".into(), + decimals: 0, + minter: vec![].into(), + } + .abi_encode_params(), + DeployTokenManager { + messageType: MessageType::DeployTokenManager.into(), + tokenId: FixedBytes::<32>::new([1u8; 32]), + tokenManagerType: TokenManagerType::NativeInterchainToken.into(), + params: vec![].into(), + } + .abi_encode_params(), + ]; + + for message in test_cases { + let payload = SendToHub { + messageType: MessageType::SendToHub.into(), + destination_chain: "destination".into(), + message: message.into(), + } + .abi_encode_params(); + + let result = HubMessage::abi_decode(&payload); + assert_err_contains!(result, Error, Error::NonEmpty(..)); + } + } + #[test] fn deploy_interchain_token_encode_decode() { let remote_chain = ChainNameRaw::from_str("chain").unwrap(); diff --git a/contracts/interchain-token-service/src/primitives.rs b/contracts/interchain-token-service/src/primitives.rs index 215d921e5..d312c6391 100644 --- a/contracts/interchain-token-service/src/primitives.rs +++ b/contracts/interchain-token-service/src/primitives.rs @@ -64,7 +64,7 @@ pub enum Message { symbol: nonempty::String, /// The number of decimal places the token supports decimals: u8, - /// The address that will be the initial minter of the token (in addition to the ITS contract) + /// An additional minter of the token (optional). ITS on the external chain is always a minter. minter: Option, }, /// Deploy a new token manager on the destination chain From 09e1fef10809b871c430966070ca47892fdaf80b Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Fri, 11 Oct 2024 00:35:01 -0400 Subject: [PATCH 10/14] use nonempty uint256 --- contracts/interchain-token-service/src/abi.rs | 32 +++++++++++++++---- .../interchain-token-service/src/contract.rs | 4 +-- .../src/contract/execute.rs | 24 +++++++------- .../interchain-token-service/src/events.rs | 6 ++-- .../src/primitives.rs | 10 +----- .../interchain_transfer_encode_decode.golden | 4 +-- .../interchain-token-service/tests/execute.rs | 2 +- .../tests/utils/messages.rs | 4 +-- packages/axelar-wasm-std/src/nonempty/uint.rs | 18 ++++++++++- 9 files changed, 66 insertions(+), 38 deletions(-) diff --git a/contracts/interchain-token-service/src/abi.rs b/contracts/interchain-token-service/src/abi.rs index a7106aff0..536d84b9f 100644 --- a/contracts/interchain-token-service/src/abi.rs +++ b/contracts/interchain-token-service/src/abi.rs @@ -145,7 +145,9 @@ impl Message { destination_address: Vec::::from(decoded.destinationAddress) .try_into() .map_err(Error::NonEmpty)?, - amount: Uint256::from_le_bytes(decoded.amount.to_le_bytes()), + amount: Uint256::from_le_bytes(decoded.amount.to_le_bytes()) + .try_into() + .map_err(Error::NonEmpty)?, data: from_vec(decoded.data.into())?, } } @@ -301,7 +303,7 @@ mod tests { token_id: [0u8; 32].into(), source_address: from_hex("00"), destination_address: from_hex("00"), - amount: Uint256::zero(), + amount: 1u64.try_into().unwrap(), data: None, }, }, @@ -311,7 +313,7 @@ mod tests { token_id: [255u8; 32].into(), source_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"), destination_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"), - amount: Uint256::MAX, + amount: Uint256::MAX.try_into().unwrap(), data: Some(from_hex("abcd")), }, }, @@ -321,7 +323,7 @@ mod tests { token_id: [0u8; 32].into(), source_address: from_hex("00"), destination_address: from_hex("00"), - amount: Uint256::zero(), + amount: 1u64.try_into().unwrap(), data: None, }, }, @@ -331,7 +333,7 @@ mod tests { token_id: [255u8; 32].into(), source_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"), destination_address: from_hex("4F4495243837681061C4743b74B3eEdf548D56A5"), - amount: Uint256::MAX, + amount: Uint256::MAX.try_into().unwrap(), data: Some(from_hex("abcd")), }, }, @@ -359,6 +361,24 @@ mod tests { tokenId: FixedBytes::<32>::new([1u8; 32]), sourceAddress: vec![1, 2].into(), destinationAddress: vec![].into(), + amount: U256::from(1), + data: vec![].into(), + } + .abi_encode_params(), + InterchainTransfer { + messageType: MessageType::InterchainTransfer.into(), + tokenId: FixedBytes::<32>::new([1u8; 32]), + sourceAddress: vec![].into(), + destinationAddress: vec![1, 2].into(), + amount: U256::from(1), + data: vec![].into(), + } + .abi_encode_params(), + InterchainTransfer { + messageType: MessageType::InterchainTransfer.into(), + tokenId: FixedBytes::<32>::new([1u8; 32]), + sourceAddress: vec![1, 2].into(), + destinationAddress: vec![1, 2].into(), amount: U256::from(0), data: vec![].into(), } @@ -632,7 +652,7 @@ mod tests { token_id: [0u8; 32].into(), source_address: from_hex("1234"), destination_address: from_hex("5678"), - amount: Uint256::from(1u128), + amount: Uint256::from(1u128).try_into().unwrap(), data: Some(large_data.try_into().unwrap()), }, }; diff --git a/contracts/interchain-token-service/src/contract.rs b/contracts/interchain-token-service/src/contract.rs index 7f6001090..1f5ed6b42 100644 --- a/contracts/interchain-token-service/src/contract.rs +++ b/contracts/interchain-token-service/src/contract.rs @@ -253,7 +253,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; @@ -284,7 +284,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; diff --git a/contracts/interchain-token-service/src/contract/execute.rs b/contracts/interchain-token-service/src/contract/execute.rs index 0e3543410..fd882e258 100644 --- a/contracts/interchain-token-service/src/contract/execute.rs +++ b/contracts/interchain-token-service/src/contract/execute.rs @@ -1,6 +1,6 @@ use axelar_core_std::nexus; use axelar_wasm_std::{nonempty, FnExt, IntoContractError}; -use cosmwasm_std::{Coin, DepsMut, HexBinary, QuerierWrapper, Response, Storage, Uint128}; +use cosmwasm_std::{Coin, DepsMut, HexBinary, QuerierWrapper, Response, Storage, Uint128, Uint256}; use error_stack::{bail, ensure, report, Result, ResultExt}; use router_api::{Address, ChainName, ChainNameRaw, CrossChainId}; use sha3::{Digest, Keccak256}; @@ -158,7 +158,7 @@ fn gateway_token_transfer( match (gateway_denom, message) { (Some(denom), Message::InterchainTransfer { amount, .. }) => Ok(Some(Coin { denom: denom.to_string(), - amount: Uint128::try_from(*amount).change_context(Error::TransferAmountOverflow)?, + amount: Uint128::try_from(Uint256::from(*amount)).change_context(Error::TransferAmountOverflow)?, })), _ => Ok(None), } @@ -333,7 +333,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; @@ -360,7 +360,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; @@ -412,7 +412,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; @@ -468,7 +468,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: amount_in_msg.into(), + amount: amount_in_msg.try_into().unwrap(), data: None, }, }; @@ -525,7 +525,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; @@ -580,7 +580,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; @@ -635,7 +635,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; @@ -677,7 +677,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; @@ -728,7 +728,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: Uint256::one(), + amount: Uint256::one().try_into().unwrap(), data: None, }, }; @@ -774,7 +774,7 @@ mod tests { .unwrap() .try_into() .unwrap(), - amount: coin.amount.into(), + amount: coin.amount.try_into().unwrap(), data: None, }, }; diff --git a/contracts/interchain-token-service/src/events.rs b/contracts/interchain-token-service/src/events.rs index d86b74bc9..db7ce05ca 100644 --- a/contracts/interchain-token-service/src/events.rs +++ b/contracts/interchain-token-service/src/events.rs @@ -104,7 +104,7 @@ mod test { token_id: TokenId::new([1; 32]), source_address: HexBinary::from([1; 32]).try_into().unwrap(), destination_address: HexBinary::from([1, 2, 3, 4]).try_into().unwrap(), - amount: 1u64.into(), + amount: 1u64.try_into().unwrap(), data: Some(HexBinary::from([1, 2, 3, 4]).try_into().unwrap()), }, Message::DeployInterchainToken { @@ -144,14 +144,14 @@ mod test { token_id: TokenId::new([1; 32]), source_address: HexBinary::from([1; 32]).try_into().unwrap(), destination_address: HexBinary::from([1, 2, 3, 4]).try_into().unwrap(), - amount: 1u64.into(), + amount: 1u64.try_into().unwrap(), data: None, }, Message::InterchainTransfer { token_id: TokenId::new([1; 32]), source_address: HexBinary::from([0u8]).try_into().unwrap(), destination_address: HexBinary::from([0u8]).try_into().unwrap(), - amount: 1u64.into(), + amount: 1u64.try_into().unwrap(), data: None, }, Message::DeployInterchainToken { diff --git a/contracts/interchain-token-service/src/primitives.rs b/contracts/interchain-token-service/src/primitives.rs index d312c6391..55c8d3e7f 100644 --- a/contracts/interchain-token-service/src/primitives.rs +++ b/contracts/interchain-token-service/src/primitives.rs @@ -2,7 +2,6 @@ use std::fmt::Display; use axelar_wasm_std::nonempty; use cosmwasm_schema::cw_serde; -use cosmwasm_std::Uint256; use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey}; use router_api::ChainNameRaw; use strum::FromRepr; @@ -50,7 +49,7 @@ pub enum Message { /// If data is not empty, this address will given the token and executed as a contract on the destination chain destination_address: nonempty::HexBinary, /// The amount of tokens to transfer - amount: Uint256, + amount: nonempty::Uint256, /// An optional payload to be provided to the destination address, if `data` is not empty data: Option, }, @@ -114,13 +113,6 @@ impl Message { | Message::DeployTokenManager { token_id, .. } => token_id.clone(), } } - - pub fn transfer_amount(&self) -> Option { - match self { - Message::InterchainTransfer { amount, .. } => Some(amount.to_owned()), - _ => None, - } - } } impl TokenId { diff --git a/contracts/interchain-token-service/src/testdata/interchain_transfer_encode_decode.golden b/contracts/interchain-token-service/src/testdata/interchain_transfer_encode_decode.golden index 44fdf0c6e..a29babff8 100644 --- a/contracts/interchain-token-service/src/testdata/interchain_transfer_encode_decode.golden +++ b/contracts/interchain-token-service/src/testdata/interchain_transfer_encode_decode.golden @@ -1,6 +1,6 @@ [ - "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a500000000000000000000000000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a50000000000000000000000000000000000000000000000000000000000000000000000000000000000000002abcd000000000000000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000005636861696e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a500000000000000000000000000000000000000000000000000000000000000000000000000000000000000144f4495243837681061c4743b74b3eedf548d56a50000000000000000000000000000000000000000000000000000000000000000000000000000000000000002abcd000000000000000000000000000000000000000000000000000000000000" ] \ No newline at end of file diff --git a/contracts/interchain-token-service/tests/execute.rs b/contracts/interchain-token-service/tests/execute.rs index 0206619e8..368d60d39 100644 --- a/contracts/interchain-token-service/tests/execute.rs +++ b/contracts/interchain-token-service/tests/execute.rs @@ -109,7 +109,7 @@ fn execute_hub_message_succeeds() { token_id: token_id.clone(), source_address: HexBinary::from([1; 32]).try_into().unwrap(), destination_address: HexBinary::from([2; 32]).try_into().unwrap(), - amount: 1u64.into(), + amount: 1u64.try_into().unwrap(), data: Some(HexBinary::from([1, 2, 3, 4]).try_into().unwrap()), }, Message::DeployInterchainToken { diff --git a/contracts/interchain-token-service/tests/utils/messages.rs b/contracts/interchain-token-service/tests/utils/messages.rs index 2e077c570..041cfbb01 100644 --- a/contracts/interchain-token-service/tests/utils/messages.rs +++ b/contracts/interchain-token-service/tests/utils/messages.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::{HexBinary, Uint256}; +use cosmwasm_std::HexBinary; use interchain_token_service::{HubMessage, Message, TokenId}; use router_api::{Address, ChainNameRaw, CrossChainId}; @@ -7,7 +7,7 @@ pub fn dummy_message() -> Message { token_id: TokenId::new([2; 32]), source_address: HexBinary::from_hex("1234").unwrap().try_into().unwrap(), destination_address: HexBinary::from_hex("5678").unwrap().try_into().unwrap(), - amount: Uint256::from(1000u64), + amount: 1000u64.try_into().unwrap(), data: Some(HexBinary::from_hex("abcd").unwrap().try_into().unwrap()), } } diff --git a/packages/axelar-wasm-std/src/nonempty/uint.rs b/packages/axelar-wasm-std/src/nonempty/uint.rs index 3af6af52a..e084b0a42 100644 --- a/packages/axelar-wasm-std/src/nonempty/uint.rs +++ b/packages/axelar-wasm-std/src/nonempty/uint.rs @@ -1,4 +1,4 @@ -use std::fmt; +use std::{fmt, ops::Deref}; use cosmwasm_schema::cw_serde; use into_inner_derive::IntoInner; @@ -72,6 +72,14 @@ impl From for cosmwasm_std::Uint256 { } } +impl TryFrom for Uint256 { + type Error = Error; + + fn try_from(value: u64) -> Result { + cosmwasm_std::Uint256::from(value).try_into() + } +} + impl TryFrom for Uint256 { type Error = Error; @@ -84,6 +92,14 @@ impl TryFrom for Uint256 { } } +impl Deref for Uint256 { + type Target = cosmwasm_std::Uint256; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + #[cw_serde] #[derive(Copy, PartialOrd, Eq, IntoInner)] pub struct Uint128(cosmwasm_std::Uint128); From b8b903d7f5c97af5ab202a3d088bd8c93ebece73 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Fri, 11 Oct 2024 00:49:49 -0400 Subject: [PATCH 11/14] fmt --- contracts/interchain-token-service/src/contract/execute.rs | 3 ++- packages/axelar-wasm-std/src/nonempty/uint.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/contracts/interchain-token-service/src/contract/execute.rs b/contracts/interchain-token-service/src/contract/execute.rs index fd882e258..5fabfa0e7 100644 --- a/contracts/interchain-token-service/src/contract/execute.rs +++ b/contracts/interchain-token-service/src/contract/execute.rs @@ -158,7 +158,8 @@ fn gateway_token_transfer( match (gateway_denom, message) { (Some(denom), Message::InterchainTransfer { amount, .. }) => Ok(Some(Coin { denom: denom.to_string(), - amount: Uint128::try_from(Uint256::from(*amount)).change_context(Error::TransferAmountOverflow)?, + amount: Uint128::try_from(Uint256::from(*amount)) + .change_context(Error::TransferAmountOverflow)?, })), _ => Ok(None), } diff --git a/packages/axelar-wasm-std/src/nonempty/uint.rs b/packages/axelar-wasm-std/src/nonempty/uint.rs index e084b0a42..861fedaba 100644 --- a/packages/axelar-wasm-std/src/nonempty/uint.rs +++ b/packages/axelar-wasm-std/src/nonempty/uint.rs @@ -1,4 +1,5 @@ -use std::{fmt, ops::Deref}; +use std::fmt; +use std::ops::Deref; use cosmwasm_schema::cw_serde; use into_inner_derive::IntoInner; From aaf95290ad1a96eb9b7b582a0b87dad9a5f25210 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Fri, 11 Oct 2024 00:50:29 -0400 Subject: [PATCH 12/14] typo --- contracts/interchain-token-service/tests/query.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/interchain-token-service/tests/query.rs b/contracts/interchain-token-service/tests/query.rs index 6351f7309..93f96af01 100644 --- a/contracts/interchain-token-service/tests/query.rs +++ b/contracts/interchain-token-service/tests/query.rs @@ -39,11 +39,11 @@ fn query_its_contract() { } #[test] -fn query_all_its_contractes() { +fn query_all_its_contracts() { let mut deps = mock_dependencies(); utils::instantiate_contract(deps.as_mut()).unwrap(); - let its_contractes = vec![ + let its_contracts = vec![ ( "ethereum".parse::().unwrap(), "0x1234567890123456789012345678901234567890" @@ -60,10 +60,10 @@ fn query_all_its_contractes() { .into_iter() .collect::>(); - for (chain, address) in its_contractes.iter() { + for (chain, address) in its_contracts.iter() { utils::register_its_contract(deps.as_mut(), chain.clone(), address.clone()).unwrap(); } let queried_addresses = assert_ok!(utils::query_all_its_contracts(deps.as_ref())); - assert_eq!(queried_addresses, its_contractes); + assert_eq!(queried_addresses, its_contracts); } From 4e897f5edb21f2923e5a7c1fc292a063c2f606fb Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Fri, 11 Oct 2024 01:47:05 -0400 Subject: [PATCH 13/14] add helper --- .../src/contract/execute.rs | 59 ++++++------------- 1 file changed, 18 insertions(+), 41 deletions(-) diff --git a/contracts/interchain-token-service/src/contract/execute.rs b/contracts/interchain-token-service/src/contract/execute.rs index 5fabfa0e7..a97ec57eb 100644 --- a/contracts/interchain-token-service/src/contract/execute.rs +++ b/contracts/interchain-token-service/src/contract/execute.rs @@ -243,8 +243,8 @@ mod tests { use axelar_core_std::nexus; use axelar_core_std::nexus::query::IsChainRegisteredResponse; use axelar_core_std::query::AxelarQueryMsg; - use axelar_wasm_std::assert_err_contains; use axelar_wasm_std::msg_id::HexTxHashAndEventIndex; + use axelar_wasm_std::{assert_err_contains, nonempty}; use axelarnet_gateway::msg::QueryMsg; use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage}; use cosmwasm_std::{ @@ -263,6 +263,13 @@ mod tests { const GATEWAY_TOKEN_DENOM: &str = "eth"; const ITS_ADDRESS: &str = "68d30f47F19c07bCCEf4Ac7FAE2Dc12FCa3e0dC9"; + fn dummy_its_address() -> nonempty::HexBinary { + HexBinary::from_hex(ITS_ADDRESS) + .unwrap() + .try_into() + .unwrap() + } + #[test] fn gateway_token_id_should_be_idempotent() { let mut deps = init(); @@ -330,10 +337,7 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: coin.amount.try_into().unwrap(), data: None, }, @@ -357,10 +361,7 @@ mod tests { message: Message::InterchainTransfer { token_id, source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: coin.amount.try_into().unwrap(), data: None, }, @@ -409,10 +410,7 @@ mod tests { message: Message::InterchainTransfer { token_id: [0u8; 32].into(), source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: coin.amount.try_into().unwrap(), data: None, }, @@ -465,10 +463,7 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: amount_in_msg.try_into().unwrap(), data: None, }, @@ -522,10 +517,7 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: coin.amount.try_into().unwrap(), data: None, }, @@ -577,10 +569,7 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: coin.amount.try_into().unwrap(), data: None, }, @@ -632,10 +621,7 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: coin.amount.try_into().unwrap(), data: None, }, @@ -674,10 +660,7 @@ mod tests { message: Message::InterchainTransfer { token_id, source_address: source_address.try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: coin.amount.try_into().unwrap(), data: None, }, @@ -725,10 +708,7 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: Uint256::one().try_into().unwrap(), data: None, }, @@ -771,10 +751,7 @@ mod tests { message: Message::InterchainTransfer { token_id: token_id.clone(), source_address: source_address.clone().try_into().unwrap(), - destination_address: HexBinary::from_hex(ITS_ADDRESS) - .unwrap() - .try_into() - .unwrap(), + destination_address: dummy_its_address(), amount: coin.amount.try_into().unwrap(), data: None, }, From 535c34e097e21da59b83d3f72d94621bfcf89b47 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Tue, 15 Oct 2024 13:10:49 -0400 Subject: [PATCH 14/14] address comments --- contracts/interchain-token-service/src/abi.rs | 2 -- packages/axelar-wasm-std/src/nonempty/hexbinary.rs | 6 +++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/contracts/interchain-token-service/src/abi.rs b/contracts/interchain-token-service/src/abi.rs index 536d84b9f..4bbffd0be 100644 --- a/contracts/interchain-token-service/src/abi.rs +++ b/contracts/interchain-token-service/src/abi.rs @@ -129,7 +129,6 @@ impl Message { ensure!(payload.len() >= 32, Error::InsufficientMessageLength); let message_type = MessageType::abi_decode(&payload[0..32], true) - .map_err(Error::AbiDecodeFailed) .change_context(Error::InvalidMessageType)?; let message = match message_type { @@ -217,7 +216,6 @@ impl HubMessage { ensure!(payload.len() >= 32, Error::InsufficientMessageLength); let message_type = MessageType::abi_decode(&payload[0..32], true) - .map_err(Error::AbiDecodeFailed) .change_context(Error::InvalidMessageType)?; let hub_message = match message_type { diff --git a/packages/axelar-wasm-std/src/nonempty/hexbinary.rs b/packages/axelar-wasm-std/src/nonempty/hexbinary.rs index edc4f7912..d0044a71e 100644 --- a/packages/axelar-wasm-std/src/nonempty/hexbinary.rs +++ b/packages/axelar-wasm-std/src/nonempty/hexbinary.rs @@ -51,11 +51,15 @@ impl Deref for HexBinary { #[cfg(test)] mod tests { + use assert_ok::assert_ok; + use crate::nonempty::{Error, HexBinary}; #[test] fn test_non_empty_hexbinary() { - assert!(HexBinary::try_from(cosmwasm_std::HexBinary::from(&[1, 2, 3])).is_ok()) + assert_ok!(HexBinary::try_from(cosmwasm_std::HexBinary::from(&[ + 1, 2, 3 + ]))); } #[test]