Skip to content

Commit

Permalink
feat(e2e): native token transfer from cosmos to starknet (#172)
Browse files Browse the repository at this point in the history
* fix ack type

* rm redundant todo comment

* relay cosmos ics20 packet to starknet

* log client states

* use simd ibc v9 with wasm

* use cosmos_to_starknet_relay

* infallible cosmos packet conversion to cairo packet

* fix packet timeout logic in cairo

* add crypto-bigint dep

* refactor ibc packet to cairo packet conversion

* ics20 transfer from cosmos to starknet

* use cosmos.nix main branch

* return all ibc token addresses

* query starknet balance

* add new wiring

* add IbcTransferSendMessage

* impl Display traits

* fix sender receiver parsing

* transfer back to cosmos from starknet

* fix typo

* 0x1 for ics20 ack success

* fix trace prefix

* fix sec nanosec conversion

* consistent receipt with cosmos

* use Vec<Felt> for ack

* add asserts after transfer back

* cairo consistent ics20 type names
  • Loading branch information
rnbguy authored Jan 10, 2025
1 parent 717b229 commit c13b888
Show file tree
Hide file tree
Showing 20 changed files with 557 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod TokenTransferComponent {
use starknet::ContractAddress;
use starknet::storage::{
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
StoragePointerWriteAccess
StoragePointerWriteAccess, Vec, VecTrait, MutableVecTrait
};
use starknet::{get_contract_address, get_caller_address};
use starknet_ibc_apps::transfer::types::{
Expand All @@ -36,6 +36,7 @@ pub mod TokenTransferComponent {
salt: felt252,
ibc_token_key_to_address: Map<felt252, ContractAddress>,
ibc_token_address_to_key: Map<ContractAddress, felt252>,
token_addresses: Vec<ContractAddress>,
}

#[event]
Expand Down Expand Up @@ -282,6 +283,10 @@ pub mod TokenTransferComponent {

address
}

fn ibc_token_addresses(self: @ComponentState<TContractState>) -> Array<ContractAddress> {
self.read_ibc_token_addresses()
}
}

// -----------------------------------------------------------
Expand Down Expand Up @@ -776,6 +781,8 @@ pub mod TokenTransferComponent {
) {
let denom_key = denom.key();

self.append_ibc_token_address(token_address);

self.write_ibc_token_key_to_address(denom_key, token_address);

self.write_ibc_token_address_to_key(token_address, denom_key);
Expand Down Expand Up @@ -839,6 +846,19 @@ pub mod TokenTransferComponent {
self.ibc_token_key_to_address.read(token_key)
}

fn read_ibc_token_addresses(
self: @ComponentState<TContractState>
) -> Array<ContractAddress> {
let mut addresses = array![];
for i in 0
..self
.token_addresses
.len() {
addresses.append(self.token_addresses.at(i).read());
};
addresses
}

fn read_ibc_token_key(
self: @ComponentState<TContractState>, token_address: ContractAddress
) -> felt252 {
Expand All @@ -860,6 +880,12 @@ pub mod TokenTransferComponent {
self.salt.write(salt);
}

fn append_ibc_token_address(
ref self: ComponentState<TContractState>, token_address: ContractAddress,
) {
self.token_addresses.append().write(token_address);
}

fn write_ibc_token_key_to_address(
ref self: ComponentState<TContractState>,
token_key: felt252,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ pub trait ITransferQuery<TContractState> {
/// ```
/// Hashing the denom is delegated to the client as it is more cost-efficient.
fn ibc_token_address(self: @TContractState, token_key: felt252) -> ContractAddress;

/// Return the contract addresses of all IBC tokens.
fn ibc_token_addresses(self: @TContractState) -> Array<ContractAddress>;
}

5 changes: 3 additions & 2 deletions cairo-contracts/packages/core/src/channel/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ pub impl PacketImpl of PacketTrait {

/// Checks if the packet is timed out.
fn is_timed_out(self: @Packet, latest_height: @Height, latest_timestamp: @Timestamp) -> bool {
!(self.timeout_height_on_b > latest_height
&& self.timeout_timestamp_on_b > latest_timestamp)
!((self.timeout_height_on_b.is_zero() || self.timeout_height_on_b > latest_height)
&& (self.timeout_timestamp_on_b.is_zero()
|| self.timeout_timestamp_on_b > latest_timestamp))
}
}

Expand Down
44 changes: 40 additions & 4 deletions flake.lock

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

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

rust-1_79 = nixpkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain-1.79.toml;

wasm-simapp = cosmos-nix.ibc-go-v8-wasm-simapp;
wasm-simapp = cosmos-nix.ibc-go-v9-wasm-simapp;

osmosis = cosmos-nix.osmosis;

Expand Down
1 change: 1 addition & 0 deletions relayer/Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ cgp_preset! {
(ViaCairo, Felt): EncodeFelt,
(ViaCairo, u128): EncodeU128,
(ViaCairo, U256): EncodeU256,
// TODO(rano): ByteArray and Array<u8> are different types in Cairo
// for now, we CANNOT use Vec<u8> to deserialize to Array<u8>
(ViaCairo, Vec<u8>): EncodeByteArray,
(ViaCairo, Vec<Felt>): EncodeList,
(ViaCairo, bool): EncodeBool,
Expand Down
1 change: 1 addition & 0 deletions relayer/crates/starknet-chain-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ serde = { workspace = true }
serde_json = { workspace = true }
starknet = { workspace = true }
tonic = { workspace = true }
crypto-bigint = "0.5.5"
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ pub use hermes_relayer_components::transaction::traits::submit_tx::TxSubmitterCo
pub use hermes_relayer_components::transaction::traits::types::transaction::TransactionTypeComponent;
pub use hermes_relayer_components::transaction::traits::types::tx_hash::TransactionHashTypeComponent;
pub use hermes_relayer_components::transaction::traits::types::tx_response::TxResponseTypeComponent;
use hermes_test_components::chain::impls::ibc_transfer::SendIbcTransferMessage;
use hermes_test_components::chain::traits::queries::balance::BalanceQuerierComponent;
use hermes_test_components::chain::traits::transfer::ibc_transfer::TokenIbcTransferrerComponent;
use hermes_test_components::chain::traits::transfer::string_memo::ProvideStringMemoType;
pub use hermes_test_components::chain::traits::types::address::AddressTypeComponent;
pub use hermes_test_components::chain::traits::types::amount::AmountTypeComponent;
pub use hermes_test_components::chain::traits::types::denom::DenomTypeComponent;
use hermes_test_components::chain::traits::types::memo::MemoTypeComponent;

use crate::components::types::StarknetChainTypes;
use crate::impls::commitment_prefix::GetStarknetCommitmentPrefix;
Expand Down Expand Up @@ -146,6 +150,10 @@ cgp_preset! {
ProvideU256Amount,
DenomTypeComponent:
ProvideTokenAddressDenom,
MemoTypeComponent:
ProvideStringMemoType,
TokenIbcTransferrerComponent:
SendIbcTransferMessage,
TransactionTypeComponent:
ProvideCallTransaction,
TransactionHashTypeComponent:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ use crate::types::messages::ibc::denom::{
Denom, EncodeDenom, EncodePrefixedDenom, EncodeTracePrefix, PrefixedDenom, TracePrefix,
};
use crate::types::messages::ibc::ibc_transfer::{
EncodeIbcTransferMessage, EncodeParticipant, IbcTransferMessage, Participant,
EncodeMsgTransfer, EncodeParticipant, EncodeTransferPacketData, MsgTransfer, Participant,
TransferPacketData,
};
use crate::types::messages::ibc::packet::{
AckStatus, Acknowledgement, EncodeAckStatus, EncodeAcknowledgement, EncodeMsgAckPacket,
Expand Down Expand Up @@ -107,7 +108,8 @@ delegate_components! {
(ViaCairo, Vec<TracePrefix>): EncodeList,
(ViaCairo, PrefixedDenom): EncodePrefixedDenom,
(ViaCairo, Participant): EncodeParticipant,
(ViaCairo, IbcTransferMessage): EncodeIbcTransferMessage,
(ViaCairo, TransferPacketData): EncodeTransferPacketData,
(ViaCairo, MsgTransfer): EncodeMsgTransfer,
(ViaCairo, Height): EncodeHeight,
(ViaCairo, Timestamp): EncodeTimestamp,
(ViaCairo, Packet): EncodePacket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ where
})
}

fn write_acknowledgement(_ack: &WriteAcknowledgementEvent) -> impl AsRef<Vec<u8>> + Send {
// TODO(rano): ack.acknowledgement.ack is Vec<Felt>
vec![0x1]
fn write_acknowledgement(ack: &WriteAcknowledgementEvent) -> impl AsRef<Vec<u8>> + Send {
ack.acknowledgement
.ack
.iter()
.map(|&felt| felt.try_into().unwrap())
.collect::<Vec<_>>()
}
}
Loading

0 comments on commit c13b888

Please sign in to comment.