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: add send_packet handler and connect IBC core to ICS20 contract #94

Merged
merged 5 commits into from
Oct 23, 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
12 changes: 6 additions & 6 deletions cairo-contracts/packages/apps/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ fmt = { workspace = true }

[dependencies]
# external dependencies
openzeppelin_access = { workspace = true }
openzeppelin_token = { workspace = true }
openzeppelin_testing = { workspace = true }
openzeppelin_utils = { workspace = true }
starknet = { workspace = true }
openzeppelin_access = { workspace = true }
openzeppelin_token = { workspace = true }
openzeppelin_utils = { workspace = true }
starknet = { workspace = true }

# ibc dependencies
starknet_ibc_core = { workspace = true }
starknet_ibc_utils = { workspace = true }

[dev-dependencies]
snforge_std = { workspace = true }
openzeppelin_testing = { workspace = true }
snforge_std = { workspace = true }
2 changes: 1 addition & 1 deletion cairo-contracts/packages/apps/src/tests/config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub impl TransferAppConfigImpl of TransferAppConfigTrait {
hosted_denom
}

fn dummy_msg_transder(
fn dummy_msg_transfer(
self: @TransferAppConfig, denom: PrefixedDenom, sender: Participant, receiver: Participant
) -> MsgTransfer {
MsgTransfer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ pub mod TokenTransferComponent {
MsgTransfer, PrefixedDenom, Denom, DenomTrait, PacketData, Memo, TracePrefixTrait,
PrefixedDenomTrait, Participant
};
use starknet_ibc_apps::transfer::{ERC20Contract, ERC20ContractTrait, TransferErrors};
use starknet_ibc_apps::transfer::{ITransferrable, ISendTransfer, ITokenAddress};
use starknet_ibc_core::channel::{Packet, Acknowledgement, IAppCallback};
use starknet_ibc_apps::transfer::{
ITransferrable, ISendTransfer, ITokenAddress, ERC20Contract, ERC20ContractTrait,
TransferErrors
};
use starknet_ibc_core::channel::{
Packet, Acknowledgement, IAppCallback, ChannelContract, ChannelContractTrait,
ChannelEndTrait
};

use starknet_ibc_core::host::{PortId, ChannelId};
use starknet_ibc_utils::{ComputeKey, ValidateBasic};

Expand Down Expand Up @@ -102,9 +108,23 @@ pub mod TokenTransferComponent {
TContractState,
+HasComponent<TContractState>,
+ITransferrable<TContractState>,
+Drop<TContractState>
+Drop<TContractState>,
impl Ownable: OwnableComponent::HasComponent<TContractState>,
> of ISendTransfer<ComponentState<TContractState>> {
// NOTE: We first ensure the validity of the incoming message at the
// application level. Then, send it through the IBC core contract for
// validations related to the TAO layer. If everything checks out, the
// packet is first executed in the core contract, followed by execution
// at the application level.
fn send_transfer(ref self: ComponentState<TContractState>, msg: MsgTransfer) {
self.send_validate(msg.clone());

let channel: ChannelContract = self.owner().into();

let packet = self.construct_send_packet(@channel, msg.clone());

channel.send_packet(packet);

self.send_execute(msg);
}
}
Expand Down Expand Up @@ -208,8 +228,6 @@ pub mod TokenTransferComponent {
}

fn send_execute(ref self: ComponentState<TContractState>, msg: MsgTransfer) {
self.send_validate(msg.clone());

let sender: Option<ContractAddress> = msg.packet_data.sender.clone().try_into();

match @msg.packet_data.denom.base {
Expand All @@ -235,6 +253,35 @@ pub mod TokenTransferComponent {

self.emit_send_event(msg.packet_data);
}

fn construct_send_packet(
self: @ComponentState<TContractState>, channel: @ChannelContract, msg: MsgTransfer
) -> Packet {
let chan_end_on_a = channel
.channel_end(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone());

let port_id_on_b = chan_end_on_a.counterparty_port_id().clone();

let chan_id_on_b = chan_end_on_a.counterparty_channel_id().clone();

let seq_on_a = channel
.next_sequence_send(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone());

let mut data: Array<felt252> = ArrayTrait::new();

msg.packet_data.serialize(ref data);

Packet {
seq_on_a,
port_id_on_a: msg.port_id_on_a,
chan_id_on_a: msg.chan_id_on_a,
port_id_on_b,
chan_id_on_b,
data,
timeout_height_on_b: msg.timeout_height_on_b,
timeout_timestamp_on_b: msg.timeout_timestamp_on_b
}
}
}

#[generate_trait]
Expand Down Expand Up @@ -443,19 +490,22 @@ pub mod TokenTransferComponent {
}

// -----------------------------------------------------------
// Transfer Owner Assertion
// Transfer Owner
// -----------------------------------------------------------

#[generate_trait]
pub(crate) impl OwnerAssertionImpl<
pub(crate) impl TransferOwnerImpl<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
impl Ownable: OwnableComponent::HasComponent<TContractState>,
> of OwnerAssertionTrait<TContractState> {
> of TransferOwnerTrait<TContractState> {
fn owner(self: @ComponentState<TContractState>) -> ContractAddress {
get_dep_component!(self, Ownable).owner()
}

fn assert_owner(self: @ComponentState<TContractState>) {
let ownable_comp = get_dep_component!(self, Ownable);
assert(ownable_comp.owner() == get_caller_address(), TransferErrors::INVALID_OWNER);
assert(self.owner() == get_caller_address(), TransferErrors::INVALID_OWNER);
}
}

Expand Down
10 changes: 10 additions & 0 deletions cairo-contracts/packages/clients/src/cometbft/component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ pub mod CometClientComponent {
comet_client_state.latest_height
}

fn latest_timestamp(
self: @ComponentState<TContractState>, client_sequence: u64
) -> Timestamp {
let latest_height = self.latest_height(client_sequence);

let consensus_state = self.read_consensus_state(client_sequence, latest_height);

consensus_state.timestamp
}

fn status(self: @ComponentState<TContractState>, client_sequence: u64) -> Status {
let comet_client_state: CometClientState = self.read_client_state(client_sequence);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use core::num::traits::Zero;
use starknet_ibc_clients::cometbft::CometErrors;
use starknet_ibc_core::client::Status;
use starknet_ibc_core::client::{Status, Timestamp};

#[derive(Clone, Debug, Drop, Hash, PartialEq, Serde, starknet::Store)]
pub struct CometConsensusState {
pub timestamp: u64,
pub timestamp: Timestamp,
pub root: felt252,
}

Expand All @@ -14,6 +14,10 @@ pub impl CometConsensusStateImpl of CometConsensusStateTrait {
self.root.is_zero() && self.timestamp.is_zero()
}

fn timestamp(self: @CometConsensusState) -> u64 {
*self.timestamp.timestamp
}

fn deserialize(consensus_state: Array<felt252>,) -> CometConsensusState {
let mut consensus_state_span = consensus_state.span();

Expand All @@ -27,9 +31,9 @@ pub impl CometConsensusStateImpl of CometConsensusStateTrait {
}

fn status(self: @CometConsensusState, host_timestamp: u64, trusting_period: u64) -> Status {
assert(host_timestamp >= *self.timestamp, CometErrors::INVALID_HEADER_TIMESTAMP);
assert(host_timestamp >= self.timestamp(), CometErrors::INVALID_HEADER_TIMESTAMP);

let elapsed_time = host_timestamp - *self.timestamp;
let elapsed_time = host_timestamp - self.timestamp();

if elapsed_time < trusting_period {
Status::Active
Expand Down
6 changes: 4 additions & 2 deletions cairo-contracts/packages/clients/src/cometbft/header.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use starknet_ibc_clients::cometbft::{CometErrors, CometConsensusState};
use starknet_ibc_core::client::Height;
use starknet_ibc_core::client::{Height, U64IntoTimestamp};

#[derive(Clone, Debug, Drop, Hash, PartialEq, Serde, starknet::Store)]
pub struct CometHeader {
Expand Down Expand Up @@ -29,6 +29,8 @@ pub struct SignedHeader {

pub impl CometHeaderIntoConsensusState of Into<CometHeader, CometConsensusState> {
fn into(self: CometHeader) -> CometConsensusState {
CometConsensusState { timestamp: self.signed_header.time, root: self.signed_header.root, }
CometConsensusState {
timestamp: self.signed_header.time.into(), root: self.signed_header.root
}
}
}
4 changes: 3 additions & 1 deletion cairo-contracts/packages/clients/src/tests/config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub impl CometClientConfigImpl of CometClientConfigTrait {

let mut serialized_consensus_state: Array<felt252> = ArrayTrait::new();

let consensus_state = CometConsensusState { timestamp: *self.latest_timestamp, root: '1' };
let consensus_state = CometConsensusState {
timestamp: self.latest_timestamp.clone().into(), root: '1'
};

Serde::serialize(@consensus_state, ref serialized_consensus_state);

Expand Down
Loading
Loading