Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(interchain-token-service): add associated error types for ITS executable interface #142

Merged
merged 18 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions contracts/interchain-token-service/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use axelar_soroban_std::{ensure, interfaces, Operatable, Ownable, Upgradable};
use interchain_token::InterchainTokenClient;
use soroban_sdk::token::{self, StellarAssetClient};
use soroban_sdk::xdr::{FromXdr, ToXdr};
use soroban_sdk::{contract, contractimpl, Address, Bytes, BytesN, Env, String};
use soroban_sdk::{
contract, contractimpl, vec, Address, Bytes, BytesN, Env, IntoVal, String, Symbol,
};
use soroban_token_sdk::metadata::TokenMetadata;

use crate::abi::{get_message_type, MessageType as EncodedMessageType};
Expand All @@ -20,7 +22,6 @@ use crate::event::{
InterchainTokenIdClaimedEvent, InterchainTransferReceivedEvent, InterchainTransferSentEvent,
TrustedChainRemovedEvent, TrustedChainSetEvent,
};
use crate::executable::InterchainTokenExecutableClient;
use crate::flow_limit::FlowDirection;
use crate::interface::InterchainTokenServiceInterface;
use crate::storage_types::{DataKey, TokenIdConfigValue};
Expand All @@ -33,6 +34,7 @@ const ITS_HUB_CHAIN_NAME: &str = "axelar";
const PREFIX_INTERCHAIN_TOKEN_ID: &str = "its-interchain-token-id";
const PREFIX_INTERCHAIN_TOKEN_SALT: &str = "interchain-token-salt";
const PREFIX_CANONICAL_TOKEN_SALT: &str = "canonical-token-salt";
const EXECUTE_WITH_TOKEN: &str = "execute_with_interchain_token";

#[contract]
#[derive(Operatable, Ownable, Upgradable)]
Expand Down Expand Up @@ -554,16 +556,23 @@ impl InterchainTokenService {
let token_address = token_config_value.token_address;

if let Some(payload) = data {
let executable =
InterchainTokenExecutableClient::new(env, &destination_address);
executable.execute_with_interchain_token(
&source_chain,
&message_id,
&source_address,
&payload,
&token_id,
&token_address,
&amount,
let call_data = vec![
&env,
source_chain.to_val(),
message_id.to_val(),
source_address.to_val(),
payload.to_val(),
token_id.to_val(),
token_address.to_val(),
amount.into_val(env),
];

// Due to limitations of the soroban-sdk, there is no type-safe client for contract execution.
// The invocation will panic on error, so we can safely cast the return value to `()` and discard it.
env.invoke_contract::<()>(
&destination_address,
&Symbol::new(env, EXECUTE_WITH_TOKEN),
call_data,
);
}
}
Expand Down
10 changes: 6 additions & 4 deletions contracts/interchain-token-service/src/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
//!
//! This is similar to the [AxelarExecutableInterface] but meant for messages sent with an ITS token.

use soroban_sdk::{contractclient, Address, Bytes, BytesN, Env, String};
use soroban_sdk::{Address, Bytes, BytesN, Env, String};

/// Interface for an Interchain Token Executable app.
#[contractclient(name = "InterchainTokenExecutableClient")]
pub trait InterchainTokenExecutableInterface {
type Error: Into<soroban_sdk::Error>;

/// Return the trusted interchain token service contract address.
fn interchain_token_service(env: &Env) -> Address;
cgorenflo marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -24,10 +25,11 @@ pub trait InterchainTokenExecutableInterface {
token_id: BytesN<32>,
token_address: Address,
amount: i128,
);
) -> Result<(), <Self as InterchainTokenExecutableInterface>::Error>;

/// Ensure that only the interchain token service can call [`execute_with_interchain_token`].
fn validate(env: &Env) {
fn validate(env: &Env) -> Result<(), <Self as InterchainTokenExecutableInterface>::Error> {
cgorenflo marked this conversation as resolved.
Show resolved Hide resolved
Self::interchain_token_service(env).require_auth();
Ok(())
}
}
16 changes: 12 additions & 4 deletions contracts/interchain-token-service/tests/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ mod test {
use axelar_soroban_std::impl_event_testutils;
use interchain_token_service::executable::InterchainTokenExecutableInterface;
use soroban_sdk::{
contract, contractimpl, contracttype, Address, Bytes, BytesN, Env, IntoVal, String, Symbol,
Topics, Val,
contract, contracterror, contractimpl, contracttype, Address, Bytes, BytesN, Env, IntoVal,
String, Symbol, Topics, Val,
};

#[contract]
Expand Down Expand Up @@ -66,8 +66,15 @@ mod test {
(Bytes)
);

#[contracterror]
pub enum ContractError {
SomeError = 1,
}

#[contractimpl]
impl InterchainTokenExecutableInterface for ExecutableContract {
type Error = ContractError;

fn interchain_token_service(env: &Env) -> Address {
env.storage()
.instance()
Expand All @@ -84,8 +91,8 @@ mod test {
token_id: BytesN<32>,
token_address: Address,
amount: i128,
) {
Self::validate(env);
) -> Result<(), ContractError> {
let _ = Self::validate(env);
cgorenflo marked this conversation as resolved.
Show resolved Hide resolved

cgorenflo marked this conversation as resolved.
Show resolved Hide resolved
env.storage().persistent().set(&DataKey::Message, &payload);

Expand All @@ -99,6 +106,7 @@ mod test {
amount,
}
.emit(env);
Ok(())
cgorenflo marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading