Skip to content

Commit

Permalink
refactor(minor-interchain-token-service)!: refactor custom token link…
Browse files Browse the repository at this point in the history
…ing (#738)
  • Loading branch information
cjcobb23 authored Jan 10, 2025
1 parent 58d5bac commit 9eb9413
Show file tree
Hide file tree
Showing 16 changed files with 529 additions and 546 deletions.
81 changes: 40 additions & 41 deletions contracts/interchain-token-service/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ sol! {
DeployTokenManager, // note, this case is not supported by the ITS hub
SendToHub,
ReceiveFromHub,
RegisterToken,
LinkToken
LinkToken,
RegisterTokenMetadata,
}

struct InterchainTransfer {
Expand Down Expand Up @@ -55,7 +55,7 @@ sol! {
bytes message;
}

struct RegisterToken {
struct RegisterTokenMetadata {
uint256 messageType;
bytes tokenAddress;
uint8 decimals;
Expand All @@ -67,7 +67,6 @@ sol! {
uint256 tokenManagerType;
bytes sourceToken;
bytes destinationToken;
bool autoscaling;
bytes params;
}
}
Expand Down Expand Up @@ -119,15 +118,6 @@ impl Message {
minter: into_vec(minter).into(),
}
.abi_encode_params(),
Message::RegisterTokenMetadata(primitives::RegisterTokenMetadata {
decimals,
address,
}) => RegisterToken {
messageType: MessageType::RegisterToken.into(),
decimals,
tokenAddress: address.to_vec().into(),
}
.abi_encode_params(),
Message::LinkToken(primitives::LinkToken {
token_id,
token_manager_type,
Expand All @@ -140,7 +130,6 @@ impl Message {
destinationToken: destination_token_address.to_vec().into(),
sourceToken: source_token_address.to_vec().into(),
tokenManagerType: U256::from_le_bytes(token_manager_type.to_le_bytes()),
autoscaling: false,
params: into_vec(params).into(),
}
.abi_encode_params(),
Expand Down Expand Up @@ -187,40 +176,26 @@ impl Message {
}
.into()
}
MessageType::RegisterToken => {
let RegisterToken {
tokenAddress,
decimals,
..
} = RegisterToken::abi_decode_params(payload, true)
.map_err(Error::AbiDecodeFailed)?;

primitives::RegisterTokenMetadata {
decimals,
address: tokenAddress.to_vec().try_into().map_err(Error::NonEmpty)?,
}
.into()
}
MessageType::LinkToken => {
let decoded =
LinkToken::abi_decode_params(payload, true).map_err(Error::AbiDecodeFailed)?;
let LinkToken {
tokenId,
tokenManagerType,
sourceToken,
destinationToken,
params,
..
} = LinkToken::abi_decode_params(payload, true).map_err(Error::AbiDecodeFailed)?;

primitives::LinkToken {
token_id: TokenId::new(decoded.tokenId.into()),
source_token_address: decoded
.sourceToken
.to_vec()
token_id: TokenId::new(tokenId.into()),
source_token_address: Vec::<u8>::from(sourceToken)
.try_into()
.map_err(Error::NonEmpty)?,
token_manager_type: Uint256::from_le_bytes(
decoded.tokenManagerType.to_le_bytes(),
),
destination_token_address: decoded
.destinationToken
.to_vec()
token_manager_type: Uint256::from_le_bytes(tokenManagerType.to_le_bytes()),
destination_token_address: Vec::<u8>::from(destinationToken)
.try_into()
.map_err(Error::NonEmpty)?,
params: from_vec(decoded.params.into())?,
params: from_vec(params.into())?,
}
.into()
}
Expand Down Expand Up @@ -254,6 +229,16 @@ impl HubMessage {
}
.abi_encode_params()
.into(),
HubMessage::RegisterTokenMetadata(primitives::RegisterTokenMetadata {
decimals,
token_address,
}) => RegisterTokenMetadata {
messageType: MessageType::RegisterTokenMetadata.into(),
decimals,
tokenAddress: token_address.to_vec().into(),
}
.abi_encode_params()
.into(),
}
}

Expand Down Expand Up @@ -284,6 +269,20 @@ impl HubMessage {
message: Message::abi_decode(&decoded.message)?,
}
}
MessageType::RegisterTokenMetadata => {
let RegisterTokenMetadata {
tokenAddress,
decimals,
..
} = RegisterTokenMetadata::abi_decode_params(payload, true)
.map_err(Error::AbiDecodeFailed)?;
HubMessage::RegisterTokenMetadata(primitives::RegisterTokenMetadata {
decimals,
token_address: Vec::<u8>::from(tokenAddress)
.try_into()
.map_err(Error::NonEmpty)?,
})
}
_ => bail!(Error::InvalidMessageType),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,16 @@ pub fn register_custom_token(
let existing_token = state::may_load_custom_token(
storage,
source_chain.clone(),
register_token.address.clone(),
register_token.token_address.clone(),
)
.change_context(Error::State)?;
ensure!(
existing_token.is_none(),
Error::TokenAlreadyRegistered(register_token.address)
Error::TokenAlreadyRegistered(register_token.token_address)
);

state::save_custom_token(storage, source_chain, register_token).change_context(Error::State)
state::save_custom_token_metadata(storage, source_chain, register_token)
.change_context(Error::State)
}

#[cfg(test)]
Expand Down
40 changes: 23 additions & 17 deletions contracts/interchain-token-service/src/contract/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ pub fn execute_message(
destination_chain,
message,
} => execute_message_on_hub(deps, cc_id, destination_chain, message),
HubMessage::RegisterTokenMetadata(msg) => {
execute_register_token_metadata(deps.storage, cc_id.source_chain, msg)
}
_ => bail!(Error::InvalidMessageType),
}
}
Expand Down Expand Up @@ -170,10 +173,6 @@ fn apply_to_hub(
apply_to_token_deployment(storage, &source_chain, &destination_chain, deploy_token)
.map(Message::DeployInterchainToken)?
}
Message::RegisterTokenMetadata(register_token) => {
apply_to_register_token(storage, source_chain, register_token)
.map(Message::RegisterTokenMetadata)?
}
Message::LinkToken(link_token) => {
apply_to_link_token(storage, source_chain, destination_chain, link_token)
.map(Message::LinkToken)?
Expand All @@ -182,13 +181,24 @@ fn apply_to_hub(
.then(Result::Ok)
}

fn apply_to_register_token(
fn execute_register_token_metadata(
storage: &mut dyn Storage,
source_chain: ChainNameRaw,
register_token: RegisterTokenMetadata,
) -> Result<RegisterTokenMetadata, Error> {
interceptors::register_custom_token(storage, source_chain, register_token.clone())?;
Ok(register_token)
register_token_metadata: RegisterTokenMetadata,
) -> Result<Response, Error> {
ensure_chain_not_frozen(storage, &source_chain)?;

interceptors::register_custom_token(
storage,
source_chain.clone(),
register_token_metadata.clone(),
)?;

Ok(Response::new().add_event(Event::TokenMetadataRegistered {
token_address: register_token_metadata.token_address,
decimals: register_token_metadata.decimals,
source_chain,
}))
}

fn apply_to_link_token(
Expand Down Expand Up @@ -1001,14 +1011,10 @@ mod tests {
decimals: u8,
token_address: nonempty::HexBinary,
) {
let msg = HubMessage::SendToHub {
destination_chain: "axelar".try_into().unwrap(),
message: RegisterTokenMetadata {
decimals,
address: token_address.clone(),
}
.into(),
};
let msg = HubMessage::RegisterTokenMetadata(RegisterTokenMetadata {
decimals,
token_address: token_address.clone(),
});

let res = assert_ok!(execute_message(
deps.as_mut(),
Expand Down
86 changes: 7 additions & 79 deletions contracts/interchain-token-service/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use axelar_wasm_std::event::EventExt;
use axelar_wasm_std::{nonempty, IntoEvent};
use router_api::{Address, ChainNameRaw, CrossChainId};

use crate::primitives::Message;
use crate::{DeployInterchainToken, InterchainTransfer, LinkToken, RegisterTokenMetadata};

#[derive(IntoEvent)]
pub enum Event {
MessageReceived {
cc_id: CrossChainId,
destination_chain: ChainNameRaw,
message: Message,
},
TokenMetadataRegistered {
source_chain: ChainNameRaw,
token_address: nonempty::HexBinary,
decimals: u8,
},
ItsContractRegistered {
chain: ChainNameRaw,
address: Address,
Expand All @@ -21,83 +26,6 @@ pub enum Event {
ExecutionEnabled,
}

impl From<Event> for cosmwasm_std::Event {
fn from(event: Event) -> Self {
match event {
Event::MessageReceived {
cc_id,
destination_chain,
message,
} => make_message_event("message_received", cc_id, destination_chain, message),
Event::ItsContractRegistered { chain, address } => {
cosmwasm_std::Event::new("its_contract_registered")
.add_attribute_as_string("chain", chain)
.add_attribute_as_string("address", address)
}
Event::ItsContractDeregistered { chain } => {
cosmwasm_std::Event::new("its_contract_deregistered")
.add_attribute_as_string("chain", chain)
}
Event::ExecutionDisabled => cosmwasm_std::Event::new("execution_disabled"),
Event::ExecutionEnabled => cosmwasm_std::Event::new("execution_enabled"),
}
}
}

fn make_message_event(
event_name: &str,
cc_id: CrossChainId,
destination_chain: ChainNameRaw,
msg: Message,
) -> cosmwasm_std::Event {
let event = cosmwasm_std::Event::new(event_name)
.add_attribute_as_string("cc_id", cc_id)
.add_attribute_as_string("destination_chain", destination_chain)
.add_attribute_as_string("message_type", msg.as_ref());

match msg {
Message::InterchainTransfer(InterchainTransfer {
token_id,
source_address,
destination_address,
amount,
data,
}) => event
.add_attribute_as_string("token_id", token_id)
.add_attribute_as_string("source_address", source_address)
.add_attribute_as_string("destination_address", destination_address)
.add_attribute_as_string("amount", amount)
.add_attribute_if_some("data", data.map(|data| data.to_string())),
Message::DeployInterchainToken(DeployInterchainToken {
token_id,
name,
symbol,
decimals,
minter,
}) => event
.add_attribute_as_string("token_id", token_id)
.add_attribute("name", name)
.add_attribute("symbol", symbol)
.add_attribute_as_string("decimals", decimals)
.add_attribute_if_some("minter", minter.map(|minter| minter.to_string())),
Message::RegisterTokenMetadata(RegisterTokenMetadata { address, decimals }) => event
.add_attribute_as_string("decimals", decimals)
.add_attribute_as_string("address", address),
Message::LinkToken(LinkToken {
token_id,
token_manager_type,
source_token_address,
destination_token_address,
params,
}) => event
.add_attribute_as_string("token_id", token_id)
.add_attribute_as_string("token_manager_type", token_manager_type)
.add_attribute_as_string("source_token_address", source_token_address)
.add_attribute_as_string("destination_token_address", destination_token_address)
.add_attribute_if_some("params", params.map(|params| params.to_string())),
}
}

#[cfg(test)]
mod test {
use cosmwasm_std::HexBinary;
Expand Down
17 changes: 5 additions & 12 deletions contracts/interchain-token-service/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ pub enum Message {
/// Deploy a new interchain token on the destination chain
DeployInterchainToken(DeployInterchainToken),

RegisterTokenMetadata(RegisterTokenMetadata),

LinkToken(LinkToken),
}

Expand Down Expand Up @@ -83,13 +81,7 @@ impl From<DeployInterchainToken> for Message {
#[derive(Eq)]
pub struct RegisterTokenMetadata {
pub decimals: u8,
pub address: nonempty::HexBinary,
}

impl From<RegisterTokenMetadata> for Message {
fn from(value: RegisterTokenMetadata) -> Self {
Message::RegisterTokenMetadata(value)
}
pub token_address: nonempty::HexBinary,
}

#[cw_serde]
Expand Down Expand Up @@ -125,13 +117,17 @@ pub enum HubMessage {
source_chain: ChainNameRaw,
message: Message,
},
RegisterTokenMetadata(RegisterTokenMetadata),
}

impl HubMessage {
pub fn message(&self) -> &Message {
match self {
HubMessage::SendToHub { message, .. } => message,
HubMessage::ReceiveFromHub { message, .. } => message,
HubMessage::RegisterTokenMetadata { .. } => {
panic!("no message associated with this hub message type")
}
}
}

Expand All @@ -146,9 +142,6 @@ impl Message {
Message::InterchainTransfer(InterchainTransfer { token_id, .. })
| Message::DeployInterchainToken(DeployInterchainToken { token_id, .. })
| Message::LinkToken(LinkToken { token_id, .. }) => *token_id,
Message::RegisterTokenMetadata(_) => {
panic!("no token id associated with this message type")
}
}
}
}
Expand Down
Loading

0 comments on commit 9eb9413

Please sign in to comment.