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): remote interchain token #118

Merged
merged 19 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

98 changes: 85 additions & 13 deletions contracts/interchain-token-service/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
use axelar_gas_service::AxelarGasServiceClient;
use axelar_gateway::{executable::AxelarExecutableInterface, AxelarGatewayMessagingClient};
use axelar_soroban_std::assert_ok;
use axelar_soroban_std::events::Event;
use axelar_soroban_std::token::validate_token_metadata;
use axelar_soroban_std::{
address::AddressExt, ensure, interfaces, types::Token, Ownable, Upgradable,
};
use interchain_token::InterchainTokenClient;
use soroban_sdk::token::StellarAssetClient;
use soroban_sdk::token::{self, StellarAssetClient};
use soroban_sdk::xdr::{FromXdr, ToXdr};
use soroban_sdk::{contract, contractimpl, panic_with_error, Address, Bytes, BytesN, Env, String};
use soroban_token_sdk::metadata::TokenMetadata;

use crate::abi::{get_message_type, MessageType as EncodedMessageType};
use crate::error::ContractError;
use crate::event::{
InterchainTokenDeployedEvent, InterchainTokenDeploymentStartedEvent,
InterchainTokenIdClaimedEvent, InterchainTransferReceivedEvent, InterchainTransferSentEvent,
TrustedChainRemovedEvent, TrustedChainSetEvent,
};
use crate::executable::InterchainTokenExecutableClient;
use crate::interface::InterchainTokenServiceInterface;
use crate::storage_types::{DataKey, TokenIdConfigValue};
use crate::token_handler;
use crate::types::{HubMessage, InterchainTransfer, Message, TokenManagerType};
use crate::types::{
DeployInterchainToken, HubMessage, InterchainTransfer, Message, TokenManagerType,
};

const ITS_HUB_CHAIN_NAME: &str = "axelar";
const PREFIX_INTERCHAIN_TOKEN_ID: &str = "its-interchain-token-id";
Expand Down Expand Up @@ -183,7 +188,7 @@ impl InterchainTokenServiceInterface for InterchainTokenService {
env: &Env,
caller: Address,
salt: BytesN<32>,
token_meta_data: TokenMetadata,
token_metadata: TokenMetadata,
initial_supply: i128,
minter: Option<Address>,
) -> Result<BytesN<32>, ContractError> {
Expand Down Expand Up @@ -211,12 +216,22 @@ impl InterchainTokenServiceInterface for InterchainTokenService {
Self::interchain_token_wasm_hash(env),
(
env.current_contract_address(),
initial_minter,
initial_minter.clone(),
token_id.clone(),
token_meta_data,
token_metadata.clone(),
),
);

InterchainTokenDeployedEvent {
token_id: token_id.clone(),
token_address: deployed_address.clone(),
name: token_metadata.name,
symbol: token_metadata.symbol,
decimals: token_metadata.decimal,
minter: initial_minter,
}
.emit(env);

if initial_supply > 0 {
StellarAssetClient::new(env, &deployed_address).mint(&caller, &initial_supply);

Expand All @@ -239,17 +254,74 @@ impl InterchainTokenServiceInterface for InterchainTokenService {
Ok(token_id)
}

/// Deploys an interchain token to a remote chain.
///
/// This function initiates the deployment of an interchain token to a specified
/// destination chain. It validates the token metadata, emits a deployment event,
/// and triggers the necessary cross-chain call.
///
/// # Arguments
/// - `env`: Reference to the contract environment.
/// - `caller`: Address of the caller initiating the deployment. The caller must authenticate.
/// - `salt`: A 32-byte unique salt used for token deployment.
/// - `destination_chain`: The name of the destination chain where the token will be deployed.
/// - `gas_token`: The token used to pay for the gas cost of the cross-chain call.
///
/// # Returns
/// - `Result<BytesN<32>, ContractError>`: On success, returns the token ID (`BytesN<32>`).
/// On failure, returns a `ContractError`.
///
/// # Errors
/// - `ContractError::InvalidTokenId`: If the token ID does not exist in the persistent storage.
/// - Any error propagated from `pay_gas_and_call_contract`.
fn deploy_remote_interchain_token(
_env: &Env,
_caller: Address,
_salt: BytesN<32>,
_minter: Option<Bytes>,
_destination_chain: String,
_gas_token: Token,
env: &Env,
caller: Address,
salt: BytesN<32>,
destination_chain: String,
gas_token: Token,
) -> Result<BytesN<32>, ContractError> {
// TODO: implementation
caller.require_auth();

todo!()
let deploy_salt = Self::interchain_token_deploy_salt(env, caller.clone(), salt);
let token_id = Self::interchain_token_id(env, Address::zero(env), deploy_salt);
let token_address = env
.storage()
.persistent()
.get::<_, TokenIdConfigValue>(&DataKey::TokenIdConfigKey(token_id.clone()))
.ok_or(ContractError::InvalidTokenId)?
.token_address;
let token = token::Client::new(env, &token_address);
let token_metadata = TokenMetadata {
name: token.name(),
decimal: token.decimals(),
symbol: token.symbol(),
};

assert_ok!(validate_token_metadata(token_metadata.clone()));

let message = Message::DeployInterchainToken(DeployInterchainToken {
token_id: token_id.clone(),
name: token_metadata.name.clone(),
symbol: token_metadata.symbol.clone(),
decimals: token_metadata.decimal as u8,
minter: None,
});

InterchainTokenDeploymentStartedEvent {
token_id: token_id.clone(),
token_address,
destination_chain: destination_chain.clone(),
name: token_metadata.name,
symbol: token_metadata.symbol,
decimals: token_metadata.decimal,
minter: None,
}
.emit(env);

Self::pay_gas_and_call_contract(env, caller, destination_chain, message, gas_token)?;

Ok(token_id)
}

fn interchain_transfer(
Expand Down
2 changes: 2 additions & 0 deletions contracts/interchain-token-service/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ pub enum ContractError {
InvalidDestinationAddress = 13,
InvalidHubChain = 14,
TokenAlreadyRegistered = 15,
InvalidTokenMetaData = 16,
InvalidTokenId = 17,
}
89 changes: 89 additions & 0 deletions contracts/interchain-token-service/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ pub struct TrustedChainRemovedEvent {
pub chain: String,
}

#[derive(Debug, PartialEq, Eq)]
pub struct InterchainTokenDeployedEvent {
pub token_id: BytesN<32>,
pub token_address: Address,
pub name: String,
pub symbol: String,
pub decimals: u32,
pub minter: Option<Address>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct InterchainTokenDeploymentStartedEvent {
pub token_id: BytesN<32>,
pub token_address: Address,
pub destination_chain: String,
pub name: String,
pub symbol: String,
pub decimals: u32,
pub minter: Option<Address>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct InterchainTokenIdClaimedEvent {
pub token_id: BytesN<32>,
Expand Down Expand Up @@ -63,6 +84,43 @@ impl Event for TrustedChainRemovedEvent {
}
}

impl Event for InterchainTokenDeployedEvent {
fn topics(&self, env: &Env) -> impl Topics + Debug {
(
Symbol::new(env, "interchain_token_deployed"),
self.token_id.to_val(),
self.token_address.to_val(),
self.name.to_val(),
self.symbol.to_val(),
self.decimals,
self.minter.clone(),
)
}

fn data(&self, env: &Env) -> impl IntoVal<Env, Val> + Debug {
Vec::<Val>::new(env)
}
}

impl Event for InterchainTokenDeploymentStartedEvent {
fn topics(&self, env: &Env) -> impl Topics + Debug {
(
Symbol::new(env, "token_deployment_started"),
self.token_id.to_val(),
self.token_address.to_val(),
self.destination_chain.to_val(),
self.name.to_val(),
self.symbol.to_val(),
self.decimals,
self.minter.clone(),
)
}

fn data(&self, env: &Env) -> impl IntoVal<Env, Val> + Debug {
Vec::<Val>::new(env)
}
}

impl Event for InterchainTokenIdClaimedEvent {
fn topics(&self, env: &Env) -> impl Topics + Debug {
(
Expand Down Expand Up @@ -121,6 +179,37 @@ impl_event_testutils!(TrustedChainSetEvent, (Symbol, String), ());
#[cfg(any(test, feature = "testutils"))]
impl_event_testutils!(TrustedChainRemovedEvent, (Symbol, String), ());

#[cfg(any(test, feature = "testutils"))]
impl_event_testutils!(
InterchainTokenDeployedEvent,
(
Symbol,
BytesN<32>,
Address,
String,
String,
u32,
Option<Address>
),
()
);

#[cfg(any(test, feature = "testutils"))]
impl_event_testutils!(
InterchainTokenDeploymentStartedEvent,
(
Symbol,
BytesN<32>,
Address,
String,
String,
String,
u32,
Option<Address>
),
()
);

#[cfg(any(test, feature = "testutils"))]
impl_event_testutils!(
InterchainTokenIdClaimedEvent,
Expand Down
11 changes: 5 additions & 6 deletions contracts/interchain-token-service/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ pub trait InterchainTokenServiceInterface: AxelarExecutableInterface {
env: &Env,
deployer: Address,
salt: BytesN<32>,
token_meta_data: TokenMetadata,
token_metadata: TokenMetadata,
initial_supply: i128,
minter: Option<Address>,
) -> Result<BytesN<32>, ContractError>;

fn deploy_remote_interchain_token(
_env: &Env,
env: &Env,
caller: Address,
_salt: BytesN<32>,
_minter: Option<Bytes>,
_destination_chain: String,
_gas_token: Token,
salt: BytesN<32>,
destination_chain: String,
gas_token: Token,
) -> Result<BytesN<32>, ContractError>;

fn interchain_transfer(
Expand Down
6 changes: 3 additions & 3 deletions contracts/interchain-token-service/tests/canonical_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const PREFIX_CANONICAL_TOKEN_SALT: &str = "canonical-token-salt";

#[test]
fn register_canonical_token_succeeds() {
let (env, client, _, _) = setup_env();
let (env, client, _, _, _) = setup_env();
let token_address = Address::generate(&env);

let chain_name = client.chain_name();
Expand Down Expand Up @@ -45,7 +45,7 @@ fn register_canonical_token_succeeds() {

#[test]
fn register_canonical_token_fails_if_already_registered() {
let (env, client, _, _) = setup_env();
let (env, client, _, _, _) = setup_env();
let token_address = Address::generate(&env);

client.register_canonical_token(&token_address);
Expand All @@ -58,7 +58,7 @@ fn register_canonical_token_fails_if_already_registered() {

#[test]
fn canonical_token_id_derivation() {
let (env, client, _, _) = setup_env();
let (env, client, _, _, _) = setup_env();
let token_address = Address::generate(&env);

let chain_name = client.chain_name();
Expand Down
Loading