Skip to content

Commit

Permalink
partial commit for hyp_erc721 comp
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Aug 16, 2024
1 parent 9021d2d commit 2cbd4b3
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 11 deletions.
13 changes: 10 additions & 3 deletions cairo/src/contracts/client/mailboxclient_component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pub mod MailboxclientComponent {
use hyperlane_starknet::interfaces::{
IMailboxClient, IMailboxDispatcher, IMailboxDispatcherTrait
};
use openzeppelin::access::ownable::{OwnableComponent, OwnableComponent::InternalImpl};
use openzeppelin::access::ownable::{
OwnableComponent, OwnableComponent::InternalImpl, OwnableComponent::Ownable
};
use openzeppelin::upgrades::{interface::IUpgradeable, upgradeable::UpgradeableComponent};
use starknet::{ContractAddress, contract_address_const};

Expand All @@ -24,6 +26,7 @@ pub mod MailboxclientComponent {
pub impl MailboxClient<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
impl Owner: OwnableComponent::HasComponent<TContractState>
> of IMailboxClient<ComponentState<TContractState>> {
/// Sets the address of the application's custom hook.
Expand Down Expand Up @@ -93,11 +96,15 @@ pub mod MailboxclientComponent {
ref self: ComponentState<TContractState>,
_hook: ContractAddress,
_interchain_security_module: ContractAddress,
_owner: ContractAddress
) {
let ownable_comp = get_dep_component!(@self, Owner);
ownable_comp.assert_only_owner();
let ownable_comp_read = get_dep_component!(@self, Owner);
ownable_comp_read.assert_only_owner();
self.set_hook(_hook);
self.set_interchain_security_module(_interchain_security_module);

let mut ownable_comp_write = get_dep_component_mut!(ref self, Owner);
ownable_comp_write.transfer_ownership(_owner);
}

/// Determines if a message associated to a given id is the mailbox's latest dispatched
Expand Down
6 changes: 3 additions & 3 deletions cairo/src/contracts/client/router_component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub mod RouterComponent {
impl Router<
TContractState,
+HasComponent<TContractState>,
impl MailBoxClient: MailboxclientComponent::HasComponent<TContractState>,
+MailboxclientComponent::HasComponent<TContractState>,
impl Owner: OwnableComponent::HasComponent<TContractState>,
+Drop<TContractState>
> of super::IRouter<ComponentState<TContractState>> {
Expand Down Expand Up @@ -151,8 +151,8 @@ pub mod RouterComponent {
pub impl RouterComponentInternalImpl<
TContractState,
+HasComponent<TContractState>,
impl MailBoxClient: MailboxclientComponent::HasComponent<TContractState>,
+Drop<TContractState>
+Drop<TContractState>,
impl MailBoxClient: MailboxclientComponent::HasComponent<TContractState>
> of InternalTrait<TContractState> {
fn initialize(ref self: ComponentState<TContractState>, _mailbox: ContractAddress) {
let mut mailbox_comp = get_dep_component_mut!(ref self, MailBoxClient);
Expand Down
112 changes: 112 additions & 0 deletions cairo/src/contracts/token/components/hyp_erc721_component.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use starknet::{ContractAddress, ClassHash};

#[starknet::interface]
pub trait IHypErc721<TState> {
fn initialize(
ref self: TState,
mint_amount: u256,
name: ByteArray,
symbol: ByteArray,
hook: ContractAddress,
interchain_security_module: ContractAddress,
owner: ContractAddress
);
}

#[starknet::component]
pub mod HypErc721 {
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::components::token_message::TokenMessageTrait;
use hyperlane_starknet::contracts::token::components::token_router::TokenRouterComponent;
use openzeppelin::access::ownable::{
OwnableComponent, OwnableComponent::InternalImpl as OwnableInternalImpl
};
use openzeppelin::introspection::src5::{
SRC5Component, SRC5Component::SRC5Impl, SRC5Component::InternalTrait as SRC5InternalTrait
};
use openzeppelin::token::erc721::{
ERC721Component, ERC721Component::ERC721,
ERC721Component::InternalImpl as ERC721InternalImpl,
ERC721Component::InternalTrait as ERC721InternalTrait,
ERC721Component::ERC721HooksTrait,
};

use starknet::{ContractAddress, ClassHash};


#[storage]
struct Storage {}

#[embeddable_as(HypErc721Impl)]
impl HypErc721<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
+OwnableComponent::HasComponent<TContractState>,
+SRC5Component::HasComponent<TContractState>,
+ERC721Component::ERC721HooksTrait<TContractState>,
impl MailBoxClient: MailboxclientComponent::HasComponent<TContractState>,
impl ERC721: ERC721Component::HasComponent<TContractState>,
> of super::IHypErc721<ComponentState<TContractState>> {
fn initialize(
ref self: ComponentState<TContractState>,
mint_amount: u256,
name: ByteArray,
symbol: ByteArray,
hook: ContractAddress,
interchain_security_module: ContractAddress,
owner: ContractAddress
) {
let mut mailbox_comp = get_dep_component_mut!(ref self, MailBoxClient);
mailbox_comp._MailboxClient_initialize(hook, interchain_security_module, owner);

let mut erc721_comp = get_dep_component_mut!(ref self, ERC721);
erc721_comp.initializer(name, symbol, "");

let caller = starknet::get_caller_address();

let mut i = 0;
while i < mint_amount {
erc721_comp.mint(caller, i.into());
i += 1;
};
}
}

#[generate_trait]
impl HypErc721InternalImpl<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
+SRC5Component::HasComponent<TContractState>,
impl ERC721: ERC721Component::HasComponent<TContractState>,
+ERC721Component::ERC721HooksTrait<TContractState>,

> of InternalTrait<TContractState> {
fn transfer_from_sender(ref self: ComponentState<TContractState>, token_id: u256) {
let erc721_comp_read = get_dep_component!(@self, ERC721);
assert!(
erc721_comp_read.owner_of(token_id) == starknet::get_caller_address(),
"Caller is not owner of token"
);

let mut erc721_comp_write = get_dep_component_mut!(ref self, ERC721);
erc721_comp_write.burn(token_id);
}

fn transfer_to(ref self: ComponentState<TContractState>, recipient: u256, token_id: u256) {
let mut erc721_comp_write = get_dep_component_mut!(ref self, ERC721);
erc721_comp_write.mint(recipient, token_id);
}
}
}

10 changes: 5 additions & 5 deletions cairo/src/contracts/token/components/token_router.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ pub mod TokenRouterComponent {
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>,
+MailboxclientComponent::HasComponent<TContractState>,
+RouterComponent::HasComponent<TContractState>,
+OwnableComponent::HasComponent<TContractState>,
+GasRouterComponent::HasComponent<TContractState>,
> of super::ITokenRouter<ComponentState<TContractState>> {
fn transfer_remote(
ref self: ComponentState<TContractState>,
Expand Down Expand Up @@ -103,9 +103,9 @@ pub mod TokenRouterComponent {
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
+OwnableComponent::HasComponent<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) {
Expand Down
1 change: 1 addition & 0 deletions cairo/src/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub trait IMailboxClient<TContractState> {
ref self: TContractState,
_hook: ContractAddress,
_interchain_security_module: ContractAddress,
_owner: ContractAddress
);

fn get_hook(self: @TContractState) -> ContractAddress;
Expand Down
1 change: 1 addition & 0 deletions cairo/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod contracts {
}
pub mod components {
pub mod fast_token_router;
pub mod hyp_erc721_component;
pub mod token_message;
pub mod token_router;
}
Expand Down

0 comments on commit 2cbd4b3

Please sign in to comment.