-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat implement mailbox client component (#34)
* feat: mailbox client component * feat: merkle tree hook and mailbox client contract simplification * fix: test adjustments * fix: format
- Loading branch information
Showing
6 changed files
with
185 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,39 @@ | ||
#[starknet::contract] | ||
mod mailboxclient { | ||
use alexandria_bytes::{Bytes, BytesTrait, BytesStore}; | ||
use hyperlane_starknet::interfaces::{ | ||
IMailbox, IMailboxDispatcher, IMailboxDispatcherTrait, IInterchainSecurityModuleDispatcher, | ||
IInterchainSecurityModuleDispatcherTrait, IMailboxClient, | ||
use hyperlane_starknet::contracts::client::mailboxclient_component::{ | ||
MailboxclientComponent, MailboxclientComponent::MailboxClientInternalImpl | ||
}; | ||
use openzeppelin::access::ownable::OwnableComponent; | ||
use openzeppelin::upgrades::{interface::IUpgradeable, upgradeable::UpgradeableComponent}; | ||
use starknet::{ContractAddress, contract_address_const, ClassHash}; | ||
|
||
use openzeppelin::access::ownable::ownable::OwnableComponent::InternalTrait; | ||
use openzeppelin::access::ownable::{OwnableComponent,}; | ||
use starknet::ContractAddress; | ||
component!(path: MailboxclientComponent, storage: mailboxclient, event: MailboxclientEvent); | ||
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); | ||
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent); | ||
#[abi(embed_v0)] | ||
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>; | ||
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>; | ||
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>; | ||
|
||
|
||
#[storage] | ||
struct Storage { | ||
mailbox: ContractAddress, | ||
local_domain: u32, | ||
hook: ContractAddress, | ||
interchain_security_module: ContractAddress, | ||
#[substorage(v0)] | ||
ownable: OwnableComponent::Storage, | ||
#[substorage(v0)] | ||
upgradeable: UpgradeableComponent::Storage, | ||
mailboxclient: MailboxclientComponent::Storage, | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, _mailbox: ContractAddress, _owner: ContractAddress,) { | ||
self.mailboxclient.initialize(_mailbox); | ||
self.ownable.initializer(_owner); | ||
} | ||
|
||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
pub enum Event { | ||
#[flat] | ||
OwnableEvent: OwnableComponent::Event, | ||
MailboxclientEvent: MailboxclientComponent::Event, | ||
#[flat] | ||
UpgradeableEvent: UpgradeableComponent::Event, | ||
} | ||
|
||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, _mailbox: ContractAddress, _owner: ContractAddress) { | ||
self.mailbox.write(_mailbox); | ||
let mailbox = IMailboxDispatcher { contract_address: _mailbox }; | ||
let local_domain = mailbox.get_local_domain(); | ||
self.local_domain.write(local_domain); | ||
self.ownable.initializer(_owner); | ||
} | ||
#[abi(embed_v0)] | ||
impl Upgradeable of IUpgradeable<ContractState> { | ||
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { | ||
self.ownable.assert_only_owner(); | ||
self.upgradeable._upgrade(new_class_hash); | ||
} | ||
OwnableEvent: OwnableComponent::Event, | ||
} | ||
|
||
|
||
#[abi(embed_v0)] | ||
impl IMailboxClientImpl of IMailboxClient<ContractState> { | ||
fn set_hook(ref self: ContractState, _hook: ContractAddress) { | ||
self.ownable.assert_only_owner(); | ||
self.hook.write(_hook); | ||
} | ||
|
||
fn set_interchain_security_module(ref self: ContractState, _module: ContractAddress) { | ||
self.ownable.assert_only_owner(); | ||
self.interchain_security_module.write(_module); | ||
} | ||
|
||
fn get_local_domain(self: @ContractState) -> u32 { | ||
self.local_domain.read() | ||
} | ||
|
||
fn get_hook(self: @ContractState) -> ContractAddress { | ||
self.hook.read() | ||
} | ||
|
||
fn get_interchain_security_module(self: @ContractState) -> ContractAddress { | ||
self.interchain_security_module.read() | ||
} | ||
|
||
|
||
fn _MailboxClient_initialize( | ||
ref self: ContractState, | ||
_hook: ContractAddress, | ||
_interchain_security_module: ContractAddress, | ||
) { | ||
self.ownable.assert_only_owner(); | ||
self.set_hook(_hook); | ||
self.set_interchain_security_module(_interchain_security_module); | ||
} | ||
|
||
fn _is_latest_dispatched(self: @ContractState, _id: u256) -> bool { | ||
let mailbox_address = self.mailbox.read(); | ||
let mailbox = IMailboxDispatcher { contract_address: mailbox_address }; | ||
mailbox.get_latest_dispatched_id() == _id | ||
} | ||
|
||
fn _is_delivered(self: @ContractState, _id: u256) -> bool { | ||
let mailbox_address = self.mailbox.read(); | ||
let mailbox = IMailboxDispatcher { contract_address: mailbox_address }; | ||
mailbox.delivered(_id) | ||
} | ||
|
||
fn mailbox(self: @ContractState) -> ContractAddress { | ||
self.mailbox.read() | ||
} | ||
|
||
fn _dispatch( | ||
self: @ContractState, | ||
_destination_domain: u32, | ||
_recipient: ContractAddress, | ||
_message_body: Bytes, | ||
_hook_metadata: Option<Bytes>, | ||
_hook: Option<ContractAddress> | ||
) -> u256 { | ||
let mailbox_address = self.mailbox.read(); | ||
let mailbox = IMailboxDispatcher { contract_address: mailbox_address }; | ||
mailbox.dispatch(_destination_domain, _recipient, _message_body, _hook_metadata, _hook) | ||
} | ||
|
||
fn quote_dispatch( | ||
self: @ContractState, | ||
_destination_domain: u32, | ||
_recipient: ContractAddress, | ||
_message_body: Bytes, | ||
_hook_metadata: Option<Bytes>, | ||
_hook: Option<ContractAddress> | ||
) -> u256 { | ||
let mailbox_address = self.mailbox.read(); | ||
let mailbox = IMailboxDispatcher { contract_address: mailbox_address }; | ||
mailbox | ||
.quote_dispatch( | ||
_destination_domain, _recipient, _message_body, _hook_metadata, _hook | ||
) | ||
} | ||
} | ||
impl MailboxclientImpl = | ||
MailboxclientComponent::MailboxClientImpl<ContractState>; | ||
} |
120 changes: 120 additions & 0 deletions
120
contracts/src/contracts/client/mailboxclient_component.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#[starknet::component] | ||
pub mod MailboxclientComponent { | ||
use alexandria_bytes::{Bytes, BytesTrait}; | ||
use hyperlane_starknet::interfaces::{ | ||
IMailboxClient, IMailboxDispatcher, IMailboxDispatcherTrait | ||
}; | ||
use openzeppelin::access::ownable::{OwnableComponent, OwnableComponent::InternalImpl}; | ||
use openzeppelin::upgrades::{interface::IUpgradeable, upgradeable::UpgradeableComponent}; | ||
use starknet::ContractAddress; | ||
|
||
#[storage] | ||
struct Storage { | ||
mailbox: IMailboxDispatcher, | ||
local_domain: u32, | ||
hook: ContractAddress, | ||
interchain_security_module: ContractAddress, | ||
} | ||
|
||
|
||
#[embeddable_as(MailboxClientImpl)] | ||
impl MailboxClient< | ||
TContractState, | ||
+HasComponent<TContractState>, | ||
impl Owner: OwnableComponent::HasComponent<TContractState> | ||
> of IMailboxClient<ComponentState<TContractState>> { | ||
fn set_hook(ref self: ComponentState<TContractState>, _hook: ContractAddress) { | ||
let ownable_comp = get_dep_component!(@self, Owner); | ||
ownable_comp.assert_only_owner(); | ||
self.hook.write(_hook); | ||
} | ||
|
||
fn set_interchain_security_module( | ||
ref self: ComponentState<TContractState>, _module: ContractAddress | ||
) { | ||
let ownable_comp = get_dep_component!(@self, Owner); | ||
ownable_comp.assert_only_owner(); | ||
self.interchain_security_module.write(_module); | ||
} | ||
|
||
fn get_local_domain(self: @ComponentState<TContractState>) -> u32 { | ||
self.local_domain.read() | ||
} | ||
|
||
fn get_hook(self: @ComponentState<TContractState>) -> ContractAddress { | ||
self.hook.read() | ||
} | ||
|
||
fn get_interchain_security_module( | ||
self: @ComponentState<TContractState> | ||
) -> ContractAddress { | ||
self.interchain_security_module.read() | ||
} | ||
|
||
|
||
fn _MailboxClient_initialize( | ||
ref self: ComponentState<TContractState>, | ||
_hook: ContractAddress, | ||
_interchain_security_module: ContractAddress, | ||
) { | ||
let ownable_comp = get_dep_component!(@self, Owner); | ||
ownable_comp.assert_only_owner(); | ||
self.set_hook(_hook); | ||
self.set_interchain_security_module(_interchain_security_module); | ||
} | ||
|
||
fn _is_latest_dispatched(self: @ComponentState<TContractState>, _id: u256) -> bool { | ||
self.mailbox.read().get_latest_dispatched_id() == _id | ||
} | ||
|
||
fn _is_delivered(self: @ComponentState<TContractState>, _id: u256) -> bool { | ||
self.mailbox.read().delivered(_id) | ||
} | ||
|
||
fn mailbox(self: @ComponentState<TContractState>) -> ContractAddress { | ||
let mailbox: IMailboxDispatcher = self.mailbox.read(); | ||
mailbox.contract_address | ||
} | ||
|
||
fn _dispatch( | ||
self: @ComponentState<TContractState>, | ||
_destination_domain: u32, | ||
_recipient: ContractAddress, | ||
_message_body: Bytes, | ||
_hook_metadata: Option<Bytes>, | ||
_hook: Option<ContractAddress> | ||
) -> u256 { | ||
self | ||
.mailbox | ||
.read() | ||
.dispatch(_destination_domain, _recipient, _message_body, _hook_metadata, _hook) | ||
} | ||
|
||
fn quote_dispatch( | ||
self: @ComponentState<TContractState>, | ||
_destination_domain: u32, | ||
_recipient: ContractAddress, | ||
_message_body: Bytes, | ||
_hook_metadata: Option<Bytes>, | ||
_hook: Option<ContractAddress> | ||
) -> u256 { | ||
self | ||
.mailbox | ||
.read() | ||
.quote_dispatch( | ||
_destination_domain, _recipient, _message_body, _hook_metadata, _hook | ||
) | ||
} | ||
} | ||
|
||
#[generate_trait] | ||
pub impl MailboxClientInternalImpl< | ||
TContractState, | ||
+HasComponent<TContractState>, | ||
impl Owner: OwnableComponent::HasComponent<TContractState> | ||
> of InternalTrait<TContractState> { | ||
fn initialize(ref self: ComponentState<TContractState>, _mailbox: ContractAddress) { | ||
self.mailbox.write(IMailboxDispatcher { contract_address: _mailbox }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.