From 116a2e2a7278ae9dbb14c036496f092904116550 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:36:33 +0100 Subject: [PATCH 01/17] create erc20 token on starknet --- .../src/tests/ics20.rs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index aa5b94bd..7fc222b6 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -27,10 +27,12 @@ use hermes_starknet_chain_components::impls::types::message::StarknetMessage; use hermes_starknet_chain_components::traits::contract::call::CanCallContract; use hermes_starknet_chain_components::traits::contract::declare::CanDeclareContract; use hermes_starknet_chain_components::traits::contract::deploy::CanDeployContract; +use hermes_starknet_chain_components::traits::queries::token_balance::CanQueryTokenBalance; use hermes_starknet_chain_components::types::cosmos::height::Height; use hermes_starknet_chain_components::types::cosmos::timestamp::Timestamp; use hermes_starknet_chain_components::types::events::ics20::IbcTransferEvent; use hermes_starknet_chain_components::types::events::packet::PacketRelayEvents; +use hermes_starknet_chain_components::types::messages::erc20::deploy::DeployErc20TokenMessage; use hermes_starknet_chain_components::types::messages::ibc::channel::PortId; use hermes_starknet_chain_components::types::messages::ibc::denom::{ Denom, PrefixedDenom, TracePrefix, @@ -599,6 +601,45 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { assert_eq!(balance_starknet_b_step_2.quantity, 0u64.into()); + // send starknet native token to cosmos + + let wallet_starknet_relayer = &starknet_chain_driver.relayer_wallet; + let address_starknet_relayer = &wallet_starknet_relayer.account_address; + + let erc20_token_supply = 1_000_000_000u128; + + let erc20_token_address = { + let deploy_message = DeployErc20TokenMessage { + name: "token".into(), + symbol: "token".into(), + fixed_supply: erc20_token_supply.into(), + recipient: *address_starknet_relayer, + owner: *address_starknet_relayer, + }; + + let calldata = StarknetCairoEncoding.encode(&deploy_message)?; + + starknet_chain + .deploy_contract(&erc20_class_hash, false, &calldata) + .await? + }; + + info!( + "deployed ERC20 contract to address: {:?}", + erc20_token_address + ); + + let balance_starknet_relayer_step_0 = starknet_chain + .query_token_balance(&erc20_token_address, address_starknet_relayer) + .await?; + + info!("initial balance: {}", balance_starknet_relayer_step_0); + + assert_eq!( + balance_starknet_relayer_step_0.quantity, + erc20_token_supply.into() + ); + Ok(()) }) } From bd79735c3f6f2809cd3f71311c48f42b1f3f4bf9 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:39:51 +0100 Subject: [PATCH 02/17] topup starknet wallet b with native erc20 --- .../src/tests/ics20.rs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index 7fc222b6..7a8c7248 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -27,7 +27,9 @@ use hermes_starknet_chain_components::impls::types::message::StarknetMessage; use hermes_starknet_chain_components::traits::contract::call::CanCallContract; use hermes_starknet_chain_components::traits::contract::declare::CanDeclareContract; use hermes_starknet_chain_components::traits::contract::deploy::CanDeployContract; +use hermes_starknet_chain_components::traits::messages::transfer::CanBuildTransferTokenMessage; use hermes_starknet_chain_components::traits::queries::token_balance::CanQueryTokenBalance; +use hermes_starknet_chain_components::types::amount::StarknetAmount; use hermes_starknet_chain_components::types::cosmos::height::Height; use hermes_starknet_chain_components::types::cosmos::timestamp::Timestamp; use hermes_starknet_chain_components::types::events::ics20::IbcTransferEvent; @@ -640,6 +642,29 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { erc20_token_supply.into() ); + { + let transfer_amount = transfer_quantity.into(); + + let message = starknet_chain.build_transfer_token_message( + address_starknet_b, + &StarknetAmount::new(transfer_amount, erc20_token_address), + )?; + + let response = starknet_chain.send_message(message).await?; + + info!("performed top-up of {transfer_quantity} tokens"); + + info!("response: {:?}", response); + + let balance = starknet_chain + .query_token_balance(&erc20_token_address, address_starknet_b) + .await?; + + info!("top-up balance: {}", balance); + + assert_eq!(balance.quantity, transfer_quantity.into()); + } + Ok(()) }) } From 192b46092d5e3405c8c807ce62b21c24586199e8 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:41:14 +0100 Subject: [PATCH 03/17] approve ics20 contract to spend on behalf of starknet relayer --- .../src/tests/ics20.rs | 53 +++++++++++++------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index 7a8c7248..b7701930 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -4,6 +4,7 @@ use core::time::Duration; use std::path::PathBuf; use std::time::SystemTime; +use cgp::prelude::*; use hermes_chain_components::traits::queries::client_state::CanQueryClientStateWithLatestHeight; use hermes_cosmos_chain_components::types::channel::CosmosInitChannelOptions; use hermes_cosmos_chain_components::types::connection::CosmosInitConnectionOptions; @@ -27,9 +28,7 @@ use hermes_starknet_chain_components::impls::types::message::StarknetMessage; use hermes_starknet_chain_components::traits::contract::call::CanCallContract; use hermes_starknet_chain_components::traits::contract::declare::CanDeclareContract; use hermes_starknet_chain_components::traits::contract::deploy::CanDeployContract; -use hermes_starknet_chain_components::traits::messages::transfer::CanBuildTransferTokenMessage; use hermes_starknet_chain_components::traits::queries::token_balance::CanQueryTokenBalance; -use hermes_starknet_chain_components::types::amount::StarknetAmount; use hermes_starknet_chain_components::types::cosmos::height::Height; use hermes_starknet_chain_components::types::cosmos::timestamp::Timestamp; use hermes_starknet_chain_components::types::events::ics20::IbcTransferEvent; @@ -61,7 +60,7 @@ use ibc::core::host::types::identifiers::{ConnectionId, PortId as IbcPortId}; use ibc::primitives::Timestamp as IbcTimestamp; use sha2::{Digest, Sha256}; use starknet::accounts::Call; -use starknet::core::types::Felt; +use starknet::core::types::{Felt, U256}; use starknet::macros::{selector, short_string}; use tracing::info; @@ -642,27 +641,47 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { erc20_token_supply.into() ); - { - let transfer_amount = transfer_quantity.into(); + // { + // let transfer_amount = transfer_quantity.into(); - let message = starknet_chain.build_transfer_token_message( - address_starknet_b, - &StarknetAmount::new(transfer_amount, erc20_token_address), - )?; + // let message = starknet_chain.build_transfer_token_message( + // address_starknet_b, + // &StarknetAmount::new(transfer_amount, erc20_token_address), + // )?; - let response = starknet_chain.send_message(message).await?; + // let response = starknet_chain.send_message(message).await?; - info!("performed top-up of {transfer_quantity} tokens"); + // info!("performed top-up of {transfer_quantity} tokens"); - info!("response: {:?}", response); + // info!("response: {:?}", response); - let balance = starknet_chain - .query_token_balance(&erc20_token_address, address_starknet_b) - .await?; + // let balance = starknet_chain + // .query_token_balance(&erc20_token_address, address_starknet_b) + // .await?; + + // info!("top-up balance: {}", balance); + + // assert_eq!(balance.quantity, transfer_quantity.into()); + // } + + { + // approve ics20 contract to spend the tokens for address_starknet_b + let call_data = cairo_encoding.encode(&product![ + ics20_contract_address, + U256::from(transfer_quantity) + ])?; - info!("top-up balance: {}", balance); + let call = Call { + to: erc20_token_address, + selector: selector!("approve"), + calldata: call_data, + }; + + let message = StarknetMessage::new(call); + + let response = starknet_chain.send_message(message).await?; - assert_eq!(balance.quantity, transfer_quantity.into()); + info!("ERC20 approve response: {:?}", response); } Ok(()) From a1bdb775afb9f7e01549ec064da75acf33686227 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:42:39 +0100 Subject: [PATCH 04/17] ibc transfer packet data for starknet --- .../src/tests/ics20.rs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index b7701930..c9019c28 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -684,6 +684,31 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { info!("ERC20 approve response: {:?}", response); } + // submit ics20 transfer from Starknet to Cosmos + + let _starknet_ic20_packet_data = { + let denom = PrefixedDenom { + trace_path: vec![], + base: Denom::Native(erc20_token_address), + }; + + let amount = transfer_quantity.into(); + + let sender = Participant::Native(*address_starknet_relayer); + + let receiver = Participant::External(address_cosmos_a.clone()); + + let memo = String::new(); + + TransferPacketData { + denom, + amount, + sender, + receiver, + memo, + } + }; + Ok(()) }) } From 3c9330b785363001486064a7d2ef0a1605447a80 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:43:29 +0100 Subject: [PATCH 05/17] ibc transfer send message for starknet --- .../src/tests/ics20.rs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index c9019c28..4549cd6f 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -686,7 +686,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { // submit ics20 transfer from Starknet to Cosmos - let _starknet_ic20_packet_data = { + let starknet_ic20_packet_data = { let denom = PrefixedDenom { trace_path: vec![], base: Denom::Native(erc20_token_address), @@ -709,6 +709,29 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { } }; + // create ibc transfer message + + let _starknet_ics20_send_message = { + let current_starknet_time = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH)? + .as_secs(); + + MsgTransfer { + port_id_on_a: PortId { + port_id: ics20_port.to_string(), + }, + chan_id_on_a: starknet_channel_id.clone(), + packet_data: starknet_ic20_packet_data, + timeout_height_on_b: Height { + revision_number: 0, + revision_height: 0, + }, + timeout_timestamp_on_b: Timestamp { + timestamp: current_starknet_time + 1800, + }, + } + }; + Ok(()) }) } From f2cee6ad0eb1675d6d397b7df4142e01f230bdac Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:44:40 +0100 Subject: [PATCH 06/17] submit ibc transfer message on starknet --- .../src/tests/ics20.rs | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index 4549cd6f..fb9c8353 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -711,7 +711,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { // create ibc transfer message - let _starknet_ics20_send_message = { + let starknet_ics20_send_message = { let current_starknet_time = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH)? .as_secs(); @@ -732,6 +732,47 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { } }; + // submit to ics20 contract + + let (_send_packet_event, _send_ics20_event) = { + let call_data = cairo_encoding.encode(&starknet_ics20_send_message)?; + + let call = Call { + to: ics20_contract_address, + selector: selector!("send_transfer"), + calldata: call_data, + }; + + let message = StarknetMessage::new(call); + + let response = starknet_chain.send_message(message).await?; + + info!("ICS20 send packet response: {:?}", response); + + let mut ibc_packet_events: Vec = + event_encoding.filter_decode_events(&response.events)?; + + info!("IBC packet events: {:?}", ibc_packet_events); + + let mut ibc_transfer_events: Vec = + event_encoding.filter_decode_events(&response.events)?; + + info!("IBC transfer events: {:?}", ibc_transfer_events); + + assert_eq!(ibc_packet_events.len(), 1); + assert_eq!(ibc_transfer_events.len(), 1); + + let Some(PacketRelayEvents::Send(send_packet_event)) = ibc_packet_events.pop() else { + panic!("expected send packet event"); + }; + + let Some(IbcTransferEvent::Send(send_ics20_event)) = ibc_transfer_events.pop() else { + panic!("expected send ics20 event"); + }; + + (send_packet_event, send_ics20_event) + }; + Ok(()) }) } From 1a0b09ac12bf0285c34b2457718357725c067bd4 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:46:06 +0100 Subject: [PATCH 07/17] build starknet packet for cosmos --- .../src/tests/ics20.rs | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index fb9c8353..f5b203bc 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -52,6 +52,7 @@ use hermes_test_components::bootstrap::traits::chain::CanBootstrapChain; use hermes_test_components::chain::traits::queries::balance::CanQueryBalance; use hermes_test_components::chain::traits::transfer::ibc_transfer::CanIbcTransferToken; use ibc::apps::transfer::types::packet::PacketData; +use ibc::apps::transfer::types::PrefixedCoin; use ibc::core::channel::types::packet::Packet as IbcPacket; use ibc::core::channel::types::timeout::{TimeoutHeight, TimeoutTimestamp}; use ibc::core::client::types::Height as IbcHeight; @@ -734,7 +735,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { // submit to ics20 contract - let (_send_packet_event, _send_ics20_event) = { + let (send_packet_event, send_ics20_event) = { let call_data = cairo_encoding.encode(&starknet_ics20_send_message)?; let call = Call { @@ -773,6 +774,46 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { (send_packet_event, send_ics20_event) }; + // create ibc packet + + let _starknet_ibc_packet = { + let timeout_height_on_b = IbcHeight::new( + send_packet_event.timeout_height_on_b.revision_number, + send_packet_event.timeout_height_on_b.revision_height, + ) + .map(TimeoutHeight::At) + .unwrap_or_else(|_| TimeoutHeight::Never); + + let timeout_timestamp_on_b = (send_packet_event.timeout_timestamp_on_b.timestamp > 0) + .then(|| { + TimeoutTimestamp::At(IbcTimestamp::from_nanoseconds( + send_packet_event.timeout_timestamp_on_b.timestamp * 1_000_000_000, + )) + }) + .unwrap_or(TimeoutTimestamp::Never); + + let ibc_transfer_packet_data = PacketData { + token: PrefixedCoin { + denom: send_ics20_event.denom.to_string().parse()?, + amount: send_ics20_event.amount.to_string().parse()?, + }, + sender: send_ics20_event.sender.to_string().into(), + receiver: send_ics20_event.receiver.to_string().into(), + memo: send_ics20_event.memo.into(), + }; + + IbcPacket { + seq_on_a: send_packet_event.sequence_on_a.sequence.into(), + port_id_on_a: send_packet_event.port_id_on_a.port_id.parse()?, + chan_id_on_a: send_packet_event.channel_id_on_a.channel_id.parse()?, + port_id_on_b: send_packet_event.port_id_on_b.port_id.parse()?, + chan_id_on_b: send_packet_event.channel_id_on_b.channel_id.parse()?, + data: serde_json::to_vec(&ibc_transfer_packet_data).unwrap(), + timeout_height_on_b, + timeout_timestamp_on_b, + } + }; + Ok(()) }) } From ea5944657feddf0fd92c52278e0a58df9785920f Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:46:38 +0100 Subject: [PATCH 08/17] relay packet to cosmos --- .../crates/starknet-integration-tests/src/tests/ics20.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index f5b203bc..5605f33b 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -776,7 +776,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { // create ibc packet - let _starknet_ibc_packet = { + let starknet_ibc_packet = { let timeout_height_on_b = IbcHeight::new( send_packet_event.timeout_height_on_b.revision_number, send_packet_event.timeout_height_on_b.revision_height, @@ -814,6 +814,12 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { } }; + // relay the packet via starknet to cosmos relay + + starknet_to_cosmos_relay + .relay_packet(&starknet_ibc_packet) + .await?; + Ok(()) }) } From aa1179be038780bf211ec93a58441f68087ac9d3 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:47:36 +0100 Subject: [PATCH 09/17] query and assert balances --- .../src/tests/ics20.rs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index 5605f33b..6dc598be 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -11,7 +11,9 @@ use hermes_cosmos_chain_components::types::connection::CosmosInitConnectionOptio use hermes_cosmos_integration_tests::init::init_test_runtime; use hermes_cosmos_relayer::contexts::build::CosmosBuilder; use hermes_cosmos_relayer::contexts::chain::CosmosChain; +use hermes_cosmos_test_components::chain::impls::transfer::amount::derive_ibc_denom; use hermes_cosmos_test_components::chain::types::amount::Amount; +use hermes_cosmos_test_components::chain::types::denom::Denom as IbcDenom; use hermes_cosmos_wasm_relayer::context::cosmos_bootstrap::CosmosWithWasmClientBootstrap; use hermes_encoding_components::traits::decode::CanDecode; use hermes_encoding_components::traits::encode::CanEncode; @@ -820,6 +822,41 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { .relay_packet(&starknet_ibc_packet) .await?; + // query balances + + let cosmos_ibc_denom = derive_ibc_denom( + &ics20_port, + &cosmos_channel_id, + &IbcDenom::base(&erc20_token_address.to_string()), + )?; + + info!("cosmos ibc denom: {:?}", cosmos_ibc_denom); + + let balance_cosmos_a_step_3 = cosmos_chain + .query_balance(address_cosmos_a, &cosmos_ibc_denom) + .await?; + + info!( + "cosmos balance after transfer from starknet: {}", + balance_cosmos_a_step_3 + ); + + assert_eq!(balance_cosmos_a_step_3.quantity, transfer_quantity); + + let balance_starknet_relayer_step_3 = starknet_chain + .query_token_balance(&erc20_token_address, address_starknet_relayer) + .await?; + + info!( + "starknet balance after transfer from starknet: {}", + balance_starknet_relayer_step_3 + ); + + assert_eq!( + balance_starknet_relayer_step_3.quantity, + (erc20_token_supply - transfer_quantity).into() + ); + Ok(()) }) } From d53e31af8eeb0053b9a928dac05b6af94e0a8f11 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:49:28 +0100 Subject: [PATCH 10/17] send the ibc token from cosmos to starknet --- .../starknet-integration-tests/src/tests/ics20.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index 6dc598be..d071721a 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -857,6 +857,21 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { (erc20_token_supply - transfer_quantity).into() ); + // send the tokens back to starknet + + let packet = >::ibc_transfer_token( + cosmos_chain, + &cosmos_channel_id, + &IbcPortId::transfer(), + wallet_cosmos_a, + address_starknet_relayer, + &Amount::new(transfer_quantity, cosmos_ibc_denom.clone()), + &None, + ) + .await?; + + cosmos_to_starknet_relay.relay_packet(&packet).await?; + Ok(()) }) } From b9558497f61f398cbbc9b6eb50d384666f774beb Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:49:44 +0100 Subject: [PATCH 11/17] query and assert balances --- .../src/tests/ics20.rs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index d071721a..ff24b489 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -872,6 +872,31 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { cosmos_to_starknet_relay.relay_packet(&packet).await?; + let balance_cosmos_a_step_4 = cosmos_chain + .query_balance(address_cosmos_a, &cosmos_ibc_denom) + .await?; + + info!( + "cosmos balance after transfer back to starknet: {}", + balance_cosmos_a_step_4 + ); + + assert_eq!(balance_cosmos_a_step_4.quantity, 0u64.into()); + + let balance_starknet_relayer_step_4 = starknet_chain + .query_token_balance(&erc20_token_address, address_starknet_relayer) + .await?; + + info!( + "starknet balance after transfer back to starknet: {}", + balance_starknet_relayer_step_4 + ); + + assert_eq!( + balance_starknet_relayer_step_4.quantity, + balance_starknet_relayer_step_0.quantity + ); + Ok(()) }) } From cc7194a425ba770e0ca5fb2c7bcd4da784d83cfe Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:49:59 +0100 Subject: [PATCH 12/17] refactor --- .../src/tests/ics20.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index ff24b489..5fec6c38 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -372,7 +372,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { let address_cosmos_a = &wallet_cosmos_a.address; let wallet_starknet_b = &starknet_chain_driver.user_wallet_b; let address_starknet_b = &wallet_starknet_b.account_address; - let transfer_quantity = 1000; + let transfer_quantity = 1_000u128; let denom_cosmos = &cosmos_chain_driver.genesis_config.transfer_denom; let balance_cosmos_a_step_0 = cosmos_chain @@ -380,7 +380,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { .await?; info!( - "cosmos balance before transfer: {:?}", + "cosmos balance before transfer: {}", balance_cosmos_a_step_0 ); @@ -402,10 +402,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { .query_balance(address_cosmos_a, denom_cosmos) .await?; - info!( - "cosmos balance after transfer: {:?}", - balance_cosmos_a_step_1 - ); + info!("cosmos balance after transfer: {}", balance_cosmos_a_step_1); assert_eq!( balance_cosmos_a_step_0.quantity, @@ -433,11 +430,11 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { info!("ics20 token address: {:?}", ics20_token_address); let balance_starknet_b_step_1 = starknet_chain - .query_balance(address_starknet_b, &ics20_token_address) + .query_token_balance(&ics20_token_address, address_starknet_b) .await?; info!( - "starknet balance after transfer: {:?}", + "starknet balance after transfer: {}", balance_starknet_b_step_1 ); @@ -585,7 +582,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { .await?; info!( - "cosmos balance after transfer back: {:?}", + "cosmos balance after transfer back: {}", balance_cosmos_a_step_2 ); @@ -595,11 +592,11 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { ); let balance_starknet_b_step_2 = starknet_chain - .query_balance(address_starknet_b, &ics20_token_address) + .query_token_balance(&ics20_token_address, address_starknet_b) .await?; info!( - "starknet balance after transfer back: {:?}", + "starknet balance after transfer back: {}", balance_starknet_b_step_2 ); From 15f683a4730d17c01c32c3c60f6deb2c0fff2b10 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:50:12 +0100 Subject: [PATCH 13/17] fix packet conversion --- .../src/impls/messages/packet.rs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/relayer/crates/starknet-chain-components/src/impls/messages/packet.rs b/relayer/crates/starknet-chain-components/src/impls/messages/packet.rs index e948885a..6ed03860 100644 --- a/relayer/crates/starknet-chain-components/src/impls/messages/packet.rs +++ b/relayer/crates/starknet-chain-components/src/impls/messages/packet.rs @@ -267,6 +267,18 @@ where let trace_path: Vec = serde_json::from_str(&trace_path_json).unwrap(); + let denom_string = ibc_ics20_packet_data + .token + .denom + .base_denom + .as_str() + .to_string(); + + let base_denom = denom_string + .parse() + .map(Denom::Native) + .unwrap_or_else(|_| Denom::Hosted(denom_string)); + let denom = PrefixedDenom { trace_path: trace_path .into_iter() @@ -280,14 +292,7 @@ where }, ) .collect(), - base: Denom::Hosted( - ibc_ics20_packet_data - .token - .denom - .base_denom - .as_str() - .to_string(), - ), + base: base_denom, }; let amount = { From d4f2c768fefbb3141d335a3c2ecfebb4be652b12 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 12:56:56 +0100 Subject: [PATCH 14/17] consistent PrefixedCoin conversion --- relayer/crates/starknet-integration-tests/src/tests/ics20.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index 5fec6c38..9b1a140b 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -553,7 +553,10 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { .unwrap_or(TimeoutTimestamp::Never); let ibc_transfer_packet_data = PacketData { - token: format!("{}{}", send_ics20_event.amount, send_ics20_event.denom).parse()?, + token: PrefixedCoin { + denom: send_ics20_event.denom.to_string().parse()?, + amount: send_ics20_event.amount.to_string().parse()?, + }, sender: send_ics20_event.sender.to_string().into(), receiver: send_ics20_event.receiver.to_string().into(), memo: send_ics20_event.memo.into(), From bf09dbedab85ad106e2c24ee3f842b09a51de8f2 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 16:51:32 +0100 Subject: [PATCH 15/17] use genesis erc20 token --- .../src/tests/ics20.rs | 70 ++++--------------- 1 file changed, 12 insertions(+), 58 deletions(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index 9b1a140b..1e6166d7 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -35,7 +35,6 @@ use hermes_starknet_chain_components::types::cosmos::height::Height; use hermes_starknet_chain_components::types::cosmos::timestamp::Timestamp; use hermes_starknet_chain_components::types::events::ics20::IbcTransferEvent; use hermes_starknet_chain_components::types::events::packet::PacketRelayEvents; -use hermes_starknet_chain_components::types::messages::erc20::deploy::DeployErc20TokenMessage; use hermes_starknet_chain_components::types::messages::ibc::channel::PortId; use hermes_starknet_chain_components::types::messages::ibc::denom::{ Denom, PrefixedDenom, TracePrefix, @@ -605,68 +604,23 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { assert_eq!(balance_starknet_b_step_2.quantity, 0u64.into()); - // send starknet native token to cosmos + // send starknet erc20 token to cosmos let wallet_starknet_relayer = &starknet_chain_driver.relayer_wallet; let address_starknet_relayer = &wallet_starknet_relayer.account_address; + let erc20_token_address = &starknet_chain_driver.genesis_config.transfer_denom; - let erc20_token_supply = 1_000_000_000u128; - - let erc20_token_address = { - let deploy_message = DeployErc20TokenMessage { - name: "token".into(), - symbol: "token".into(), - fixed_supply: erc20_token_supply.into(), - recipient: *address_starknet_relayer, - owner: *address_starknet_relayer, - }; - - let calldata = StarknetCairoEncoding.encode(&deploy_message)?; - - starknet_chain - .deploy_contract(&erc20_class_hash, false, &calldata) - .await? - }; - - info!( - "deployed ERC20 contract to address: {:?}", - erc20_token_address - ); + info!("erc20 token address: {:?}", erc20_token_address); let balance_starknet_relayer_step_0 = starknet_chain - .query_token_balance(&erc20_token_address, address_starknet_relayer) + .query_token_balance(erc20_token_address, address_starknet_relayer) .await?; - info!("initial balance: {}", balance_starknet_relayer_step_0); - - assert_eq!( - balance_starknet_relayer_step_0.quantity, - erc20_token_supply.into() + info!( + "erc20 balance on starknet: {}", + balance_starknet_relayer_step_0 ); - // { - // let transfer_amount = transfer_quantity.into(); - - // let message = starknet_chain.build_transfer_token_message( - // address_starknet_b, - // &StarknetAmount::new(transfer_amount, erc20_token_address), - // )?; - - // let response = starknet_chain.send_message(message).await?; - - // info!("performed top-up of {transfer_quantity} tokens"); - - // info!("response: {:?}", response); - - // let balance = starknet_chain - // .query_token_balance(&erc20_token_address, address_starknet_b) - // .await?; - - // info!("top-up balance: {}", balance); - - // assert_eq!(balance.quantity, transfer_quantity.into()); - // } - { // approve ics20 contract to spend the tokens for address_starknet_b let call_data = cairo_encoding.encode(&product![ @@ -675,7 +629,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { ])?; let call = Call { - to: erc20_token_address, + to: *erc20_token_address, selector: selector!("approve"), calldata: call_data, }; @@ -692,7 +646,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { let starknet_ic20_packet_data = { let denom = PrefixedDenom { trace_path: vec![], - base: Denom::Native(erc20_token_address), + base: Denom::Native(*erc20_token_address), }; let amount = transfer_quantity.into(); @@ -844,7 +798,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { assert_eq!(balance_cosmos_a_step_3.quantity, transfer_quantity); let balance_starknet_relayer_step_3 = starknet_chain - .query_token_balance(&erc20_token_address, address_starknet_relayer) + .query_token_balance(erc20_token_address, address_starknet_relayer) .await?; info!( @@ -854,7 +808,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { assert_eq!( balance_starknet_relayer_step_3.quantity, - (erc20_token_supply - transfer_quantity).into() + balance_starknet_relayer_step_0.quantity - transfer_quantity.into() ); // send the tokens back to starknet @@ -884,7 +838,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { assert_eq!(balance_cosmos_a_step_4.quantity, 0u64.into()); let balance_starknet_relayer_step_4 = starknet_chain - .query_token_balance(&erc20_token_address, address_starknet_relayer) + .query_token_balance(erc20_token_address, address_starknet_relayer) .await?; info!( From c8b370cf244bf7891234e02368929d5a6282764b Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 16:58:41 +0100 Subject: [PATCH 16/17] query starknet chain timestamp --- .../starknet-integration-tests/src/tests/ics20.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index 1e6166d7..3a5b48eb 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use std::time::SystemTime; use cgp::prelude::*; +use hermes_chain_components::traits::queries::chain_status::CanQueryChainStatus; use hermes_chain_components::traits::queries::client_state::CanQueryClientStateWithLatestHeight; use hermes_cosmos_chain_components::types::channel::CosmosInitChannelOptions; use hermes_cosmos_chain_components::types::connection::CosmosInitConnectionOptions; @@ -472,9 +473,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { // create ibc transfer message let starknet_ics20_send_message = { - let current_starknet_time = SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH)? - .as_secs(); + let current_starknet_time = starknet_chain.query_chain_status().await?.time; MsgTransfer { port_id_on_a: PortId { @@ -487,7 +486,8 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { revision_height: 0, }, timeout_timestamp_on_b: Timestamp { - timestamp: current_starknet_time + 1800, + timestamp: u64::try_from(current_starknet_time.unix_timestamp()).unwrap() + + 1800, }, } }; @@ -669,9 +669,7 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { // create ibc transfer message let starknet_ics20_send_message = { - let current_starknet_time = SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH)? - .as_secs(); + let current_starknet_time = starknet_chain.query_chain_status().await?.time; MsgTransfer { port_id_on_a: PortId { @@ -684,7 +682,8 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { revision_height: 0, }, timeout_timestamp_on_b: Timestamp { - timestamp: current_starknet_time + 1800, + timestamp: u64::try_from(current_starknet_time.unix_timestamp()).unwrap() + + 1800, }, } }; From 7c075158f89b29217f6bedf972ba8838d5f753d7 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Fri, 10 Jan 2025 17:42:12 +0100 Subject: [PATCH 17/17] rm redundant todo comment --- relayer/crates/starknet-integration-tests/src/tests/ics20.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs index 3a5b48eb..00ad0e0b 100644 --- a/relayer/crates/starknet-integration-tests/src/tests/ics20.rs +++ b/relayer/crates/starknet-integration-tests/src/tests/ics20.rs @@ -440,8 +440,6 @@ fn test_starknet_ics20_contract() -> Result<(), Error> { assert_eq!(balance_starknet_b_step_1.quantity, transfer_quantity.into()); - // TODO(rano): send back the ics20 token to cosmos - // create ibc transfer packet data let starknet_ic20_packet_data = {