From 2cbd4b36ebfd8a54f06e7c8a1ea8f43d22d78e39 Mon Sep 17 00:00:00 2001 From: Alex Metelli Date: Fri, 16 Aug 2024 17:36:43 +0800 Subject: [PATCH] partial commit for hyp_erc721 comp --- .../client/mailboxclient_component.cairo | 13 +- .../contracts/client/router_component.cairo | 6 +- .../components/hyp_erc721_component.cairo | 112 ++++++++++++++++++ .../token/components/token_router.cairo | 10 +- cairo/src/interfaces.cairo | 1 + cairo/src/lib.cairo | 1 + 6 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 cairo/src/contracts/token/components/hyp_erc721_component.cairo diff --git a/cairo/src/contracts/client/mailboxclient_component.cairo b/cairo/src/contracts/client/mailboxclient_component.cairo index ff18fda..d3fe269 100644 --- a/cairo/src/contracts/client/mailboxclient_component.cairo +++ b/cairo/src/contracts/client/mailboxclient_component.cairo @@ -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}; @@ -24,6 +26,7 @@ pub mod MailboxclientComponent { pub impl MailboxClient< TContractState, +HasComponent, + +Drop, impl Owner: OwnableComponent::HasComponent > of IMailboxClient> { /// Sets the address of the application's custom hook. @@ -93,11 +96,15 @@ pub mod MailboxclientComponent { ref self: ComponentState, _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 diff --git a/cairo/src/contracts/client/router_component.cairo b/cairo/src/contracts/client/router_component.cairo index 4a66be6..24b3fbf 100644 --- a/cairo/src/contracts/client/router_component.cairo +++ b/cairo/src/contracts/client/router_component.cairo @@ -45,7 +45,7 @@ pub mod RouterComponent { impl Router< TContractState, +HasComponent, - impl MailBoxClient: MailboxclientComponent::HasComponent, + +MailboxclientComponent::HasComponent, impl Owner: OwnableComponent::HasComponent, +Drop > of super::IRouter> { @@ -151,8 +151,8 @@ pub mod RouterComponent { pub impl RouterComponentInternalImpl< TContractState, +HasComponent, - impl MailBoxClient: MailboxclientComponent::HasComponent, - +Drop + +Drop, + impl MailBoxClient: MailboxclientComponent::HasComponent > of InternalTrait { fn initialize(ref self: ComponentState, _mailbox: ContractAddress) { let mut mailbox_comp = get_dep_component_mut!(ref self, MailBoxClient); diff --git a/cairo/src/contracts/token/components/hyp_erc721_component.cairo b/cairo/src/contracts/token/components/hyp_erc721_component.cairo new file mode 100644 index 0000000..1bbfc0b --- /dev/null +++ b/cairo/src/contracts/token/components/hyp_erc721_component.cairo @@ -0,0 +1,112 @@ +use starknet::{ContractAddress, ClassHash}; + +#[starknet::interface] +pub trait IHypErc721 { + 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, + +Drop, + +OwnableComponent::HasComponent, + +SRC5Component::HasComponent, + +ERC721Component::ERC721HooksTrait, + impl MailBoxClient: MailboxclientComponent::HasComponent, + impl ERC721: ERC721Component::HasComponent, + > of super::IHypErc721> { + fn initialize( + ref self: ComponentState, + 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, + +Drop, + +SRC5Component::HasComponent, + impl ERC721: ERC721Component::HasComponent, + +ERC721Component::ERC721HooksTrait, + + > of InternalTrait { + fn transfer_from_sender(ref self: ComponentState, 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, recipient: u256, token_id: u256) { + let mut erc721_comp_write = get_dep_component_mut!(ref self, ERC721); + erc721_comp_write.mint(recipient, token_id); + } + } +} + diff --git a/cairo/src/contracts/token/components/token_router.cairo b/cairo/src/contracts/token/components/token_router.cairo index d032254..1f04893 100644 --- a/cairo/src/contracts/token/components/token_router.cairo +++ b/cairo/src/contracts/token/components/token_router.cairo @@ -62,10 +62,10 @@ pub mod TokenRouterComponent { TContractState, +HasComponent, +Drop, - impl MailBoxClient: MailboxclientComponent::HasComponent, - impl Router: RouterComponent::HasComponent, - impl Owner: OwnableComponent::HasComponent, - impl GasRouter: GasRouterComponent::HasComponent, + +MailboxclientComponent::HasComponent, + +RouterComponent::HasComponent, + +OwnableComponent::HasComponent, + +GasRouterComponent::HasComponent, > of super::ITokenRouter> { fn transfer_remote( ref self: ComponentState, @@ -103,9 +103,9 @@ pub mod TokenRouterComponent { TContractState, +HasComponent, +Drop, + +OwnableComponent::HasComponent, impl MailBoxClient: MailboxclientComponent::HasComponent, impl Router: RouterComponent::HasComponent, - impl Owner: OwnableComponent::HasComponent, impl GasRouter: GasRouterComponent::HasComponent, > of InternalTrait { fn initialize(ref self: ComponentState, mailbox: ContractAddress) { diff --git a/cairo/src/interfaces.cairo b/cairo/src/interfaces.cairo index 8bc5b5f..dd9f109 100644 --- a/cairo/src/interfaces.cairo +++ b/cairo/src/interfaces.cairo @@ -149,6 +149,7 @@ pub trait IMailboxClient { ref self: TContractState, _hook: ContractAddress, _interchain_security_module: ContractAddress, + _owner: ContractAddress ); fn get_hook(self: @TContractState) -> ContractAddress; diff --git a/cairo/src/lib.cairo b/cairo/src/lib.cairo index f9468e7..29ab81e 100644 --- a/cairo/src/lib.cairo +++ b/cairo/src/lib.cairo @@ -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; }