Skip to content

Commit

Permalink
token router comp
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Aug 14, 2024
1 parent 978368b commit b1e8202
Showing 1 changed file with 137 additions and 47 deletions.
184 changes: 137 additions & 47 deletions cairo/src/contracts/token/libs/token_router.cairo
Original file line number Diff line number Diff line change
@@ -1,91 +1,181 @@
use alexandria_bytes::Bytes;
use starknet::ContractAddress;

#[starknet::interface]
pub trait ITokenRouter<TState> {
fn initialize(ref self: TState);
fn transfer_remote(
self: @TState, destination: u32, recipient: Array<u8>, amount_or_id: u256
) -> u256;
fn transfer_remote_with_hook(
self: @TState,
ref self: TState,
destination: u32,
// TODO: make this fixed size once we switch to 2.7.0
recipient: Array<u8>,
recipient: u256,
amount_or_id: u256,
hook_metadata: u256,
hook: u256
value: u256,
hook_metadata: Option<Bytes>,
hook: Option<ContractAddress>
) -> u256;
fn balance_of(self: @TState, account: u256) -> u256;
}

#[starknet::component]
pub mod TokenRouter {
pub mod TokenRouterComponent {
use alexandria_bytes::{Bytes, BytesTrait};
use hyperlane_starknet::contracts::client::gas_router_component::{
GasRouterComponent, GasRouterComponent::GasRouterInternalImpl
};
use hyperlane_starknet::contracts::client::mailboxclient_component::{
MailboxclientComponent, MailboxclientComponent::MailboxClientInternalImpl,
MailboxclientComponent::MailboxClient
};
use hyperlane_starknet::contracts::client::router_component::{
RouterComponent, RouterComponent::RouterComponentInternalImpl, IRouter,
};
use hyperlane_starknet::contracts::token::libs::token_message::TokenMessageTrait;
use openzeppelin::access::ownable::{
OwnableComponent, OwnableComponent::InternalImpl as OwnableInternalImpl
};
use starknet::ContractAddress;

#[storage]
struct Storage {}

fn constructor() {}
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
SentTransferRemote: SentTransferRemote,
ReceivedTransferRemote: ReceivedTransferRemote,
}

impl TokenRouterImpl<
TContractState, +HasComponent<TContractState>,
> of super::ITokenRouter<ComponentState<TContractState>> {
fn initialize(ref self: ComponentState<TContractState>) {}
#[derive(Drop, starknet::Event)]
pub struct SentTransferRemote {
pub destination: u32,
pub recipient: u256,
pub amount: u256,
}

fn transfer_remote(
self: @ComponentState<TContractState>,
destination: u32,
// TODO: make this fixed size once we switch to 2.7.0
recipient: Array<u8>,
amount_or_id: u256
) -> u256 {
0
}
#[derive(Drop, starknet::Event)]
pub struct ReceivedTransferRemote {
pub origin: u32,
pub recipient: u256,
pub amount: u256,
}

fn transfer_remote_with_hook(
self: @ComponentState<TContractState>,
#[embeddable_as(TokenRouterImpl)]
impl TokenRouter<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
impl MailBoxClient: MailboxclientComponent::HasComponent<TContractState>,
impl Router: RouterComponent::HasComponent<TContractState>,
impl Owner: OwnableComponent::HasComponent<TContractState>,
impl GasRouter: GasRouterComponent::HasComponent<TContractState>,
> of super::ITokenRouter<ComponentState<TContractState>> {
fn transfer_remote(
ref self: ComponentState<TContractState>,
destination: u32,
// TODO: make this fixed size once we switch to 2.7.0
recipient: Array<u8>,
recipient: u256,
amount_or_id: u256,
hook_metadata: u256,
hook: u256
value: u256,
hook_metadata: Option<Bytes>,
hook: Option<ContractAddress>
) -> u256 {
0
}

fn balance_of(self: @ComponentState<TContractState>, account: u256) -> u256 {
0
match hook_metadata {
Option::Some(hook_metadata) => {
self
._transfer_remote(
destination,
recipient,
amount_or_id,
value,
Option::Some(hook_metadata),
hook
)
},
Option::None => {
self
._transfer_remote(
destination, recipient, amount_or_id, value, Option::None, Option::None
)
}
}
}
}

#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
pub impl TokenRouterInternalImpl<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
impl MailBoxClient: MailboxclientComponent::HasComponent<TContractState>,
impl Router: RouterComponent::HasComponent<TContractState>,
impl Owner: OwnableComponent::HasComponent<TContractState>,
impl GasRouter: GasRouterComponent::HasComponent<TContractState>,
> of InternalTrait<TContractState> {
fn initialize(ref self: ComponentState<TContractState>, mailbox: ContractAddress) {
let mut gas_router_comp = get_dep_component_mut!(ref self, GasRouter);
gas_router_comp.initialize(mailbox);
}

fn _transfer_remote(
ref self: ComponentState<TContractState>,
destination: u32,
// TODO: make this fixed size once we switch to 2.7.0
recipient: Array<u8>,
recipient: u256,
amount_or_id: u256,
value: u256,
hook_metadata: Option<Array<u8>>,
hook_metadata: Option<Bytes>,
hook: Option<ContractAddress>
) -> u256 {
0
let token_metadata = self._transfer_from_sender(amount_or_id);
let token_message = TokenMessageTrait::format(recipient, amount_or_id, token_metadata);

let mut router_comp = get_dep_component!(@self, Router);
let mailbox_comp = get_dep_component!(@self, MailBoxClient);
let gas_router_comp = get_dep_component!(@self, GasRouter);

let mut message_id = 0;

match hook_metadata {
Option::Some(hook_metadata) => {
if !hook.is_some() {
panic!("Transfer remote invalid arguments, missing hook");
}

message_id = router_comp
._Router_dispatch(
destination, value, token_message, hook_metadata, hook.unwrap()
);
},
Option::None => {
let hook_metadata = gas_router_comp._Gas_router_hook_metadata(destination);
let hook = mailbox_comp.get_hook();
message_id = router_comp
._Router_dispatch(destination, value, token_message, hook_metadata, hook);
}
}

self.emit(SentTransferRemote { destination, recipient, amount: amount_or_id, });

message_id
}

fn _transfer_from_sender(
ref self: ComponentState<TContractState>, amount_or_id: u256
) -> u256 {
0
) -> Bytes {
BytesTrait::new_empty()
}

fn _handle(ref self: ComponentState<TContractState>, origin: u32, message: u256) {}
fn _handle(ref self: ComponentState<TContractState>, origin: u32, message: Bytes) {
let recipient = message.recipient();
let amount = message.amount();
let metadata = message.metadata();

self._transfer_to(recipient, amount, metadata);

self.emit(ReceivedTransferRemote { origin, recipient, amount, });
}

fn _transfer_to(
ref self: ComponentState<TContractState>,
recipient: ContractAddress,
recipient: u256,
amount_or_id: u256,
metadata: Array<u8>
metadata: Bytes
) {}
}
}
Expand Down

0 comments on commit b1e8202

Please sign in to comment.