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

Implement components for declaring and deploying contracts #44

Merged
merged 9 commits into from
Aug 7, 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
749 changes: 727 additions & 22 deletions relayer/Cargo.lock

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ rust-version = "1.79"
overflow-checks = true

[workspace.dependencies]
async-trait = { version = "0.1.81" }
ibc = { version = "0.53.0" }
cgp-core = { version = "0.1.0" }
cgp-error-eyre = { version = "0.1.0" }
cgp-component-macro = { version = "0.1.0" }

clap = { version = "4.5.8" }
starknet = { version = "0.11.0" }
url = { version = "2.4.0" }
eyre = { version = "0.6.12" }
tokio = { version = "1.38" }
clap = { version = "4.5.8" }
starknet = { version = "0.11.0" }
url = { version = "2.4.0" }
eyre = { version = "0.6.12" }
tokio = { version = "1.38" }
serde_json = { version = "1.0" }

cairo-lang-starknet-classes = { version = "2.6.4" }
cainome-cairo-serde = { version = "0.1.0" }

hermes-runtime-components = { version = "0.1.0" }
hermes-async-runtime-components = { version = "0.1.0" }
Expand Down Expand Up @@ -127,3 +130,6 @@ hermes-wasm-test-components = { git = "https://github.com/informalsystems/herm
hermes-starknet-chain-components = { path = "./crates/starknet-chain-components" }
hermes-starknet-test-components = { path = "./crates/starknet-test-components" }
hermes-starknet-chain-context = { path = "./crates/starknet-chain-context" }

cairo-lang-starknet-classes = { git = "https://github.com/starkware-libs/cairo", tag = "v2.6.4" }
cainome-cairo-serde = { git = "https://github.com/cartridge-gg/cainome" }
4 changes: 3 additions & 1 deletion relayer/crates/starknet-chain-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ cgp-core = { workspace = true }
hermes-relayer-components = { workspace = true }
hermes-test-components = { workspace = true }

starknet = { workspace = true }
starknet = { workspace = true }
serde_json = { workspace = true }
cairo-lang-starknet-classes = { workspace = true }
17 changes: 17 additions & 0 deletions relayer/crates/starknet-chain-components/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub use hermes_test_components::chain::traits::types::amount::AmountTypeComponen
pub use hermes_test_components::chain::traits::types::denom::DenomTypeComponent;

use crate::impls::contract::call::CallStarknetContract;
use crate::impls::contract::declare::DeclareSierraContract;
use crate::impls::contract::deploy::DeployStarknetContract;
use crate::impls::contract::invoke::InvokeStarknetContract;
use crate::impls::contract::message::BuildInvokeContractCall;
use crate::impls::messages::transfer::BuildTransferErc20TokenMessage;
Expand All @@ -30,6 +32,7 @@ use crate::impls::types::address::ProvideFeltAddressType;
use crate::impls::types::amount::ProvideU256Amount;
use crate::impls::types::blob::ProvideFeltBlobType;
use crate::impls::types::chain_id::ProvideFeltChainId;
use crate::impls::types::contract::ProvideStarknetContractTypes;
use crate::impls::types::denom::ProvideTokenAddressDenom;
use crate::impls::types::event::ProvideStarknetEvent;
use crate::impls::types::message::ProvideCallMessage;
Expand All @@ -38,12 +41,17 @@ use crate::impls::types::transaction::ProvideCallTransaction;
use crate::impls::types::tx_hash::ProvideFeltTxHash;
use crate::impls::types::tx_response::ProvideStarknetTxResponse;
pub use crate::traits::contract::call::ContractCallerComponent;
pub use crate::traits::contract::declare::ContractDeclarerComponent;
pub use crate::traits::contract::deploy::ContractDeployerComponent;
pub use crate::traits::contract::invoke::ContractInvokerComponent;
pub use crate::traits::contract::message::InvokeContractMessageBuilderComponent;
pub use crate::traits::messages::transfer::TransferTokenMessageBuilderComponent;
pub use crate::traits::queries::token_balance::TokenBalanceQuerierComponent;
pub use crate::traits::transfer::TokenTransferComponent;
pub use crate::traits::types::blob::BlobTypeComponent;
pub use crate::traits::types::contract_class::{
ContractClassHashTypeComponent, ContractClassTypeComponent,
};
pub use crate::traits::types::method::MethodSelectorTypeComponent;

define_components! {
Expand All @@ -70,6 +78,11 @@ define_components! {
ProvideStarknetTxResponse,
MethodSelectorTypeComponent:
ProvideFeltMethodSelector,
[
ContractClassTypeComponent,
ContractClassHashTypeComponent,
]:
ProvideStarknetContractTypes,
MessageSenderComponent:
SendCallMessages,
TxSubmitterComponent:
Expand All @@ -84,6 +97,10 @@ define_components! {
CallStarknetContract,
ContractInvokerComponent:
InvokeStarknetContract,
ContractDeclarerComponent:
DeclareSierraContract,
ContractDeployerComponent:
DeployStarknetContract,
InvokeContractMessageBuilderComponent:
BuildInvokeContractCall,
RetryableErrorComponent:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use std::sync::Arc;

use cairo_lang_starknet_classes::casm_contract_class::{
CasmContractClass, StarknetSierraCompilationError,
};
use cairo_lang_starknet_classes::contract_class::ContractClass;
use cgp_core::error::CanRaiseError;
use hermes_relayer_components::transaction::traits::poll_tx_response::CanPollTxResponse;
use starknet::accounts::Account;
use starknet::core::types::contract::{
CompiledClass, ComputeClassHashError, JsonError, SierraClass,
};
use starknet::core::types::{BlockId, BlockTag, Felt, RevertedInvocation};
use starknet::providers::Provider;

use crate::traits::account::{CanRaiseAccountErrors, HasStarknetAccount};
use crate::traits::contract::declare::ContractDeclarer;
use crate::traits::provider::HasStarknetProvider;
use crate::traits::types::contract_class::{HasContractClassHashType, HasContractClassType};
use crate::types::tx_response::TxResponse;

pub struct DeclareSierraContract;

impl<Chain> ContractDeclarer<Chain> for DeclareSierraContract
where
Chain: HasContractClassType<ContractClass = SierraClass>
+ HasContractClassHashType<ContractClassHash = Felt>
+ HasStarknetProvider
+ HasStarknetAccount
+ CanPollTxResponse<TxHash = Felt, TxResponse = TxResponse>
+ CanRaiseAccountErrors
+ CanRaiseError<serde_json::error::Error>
+ CanRaiseError<JsonError>
+ CanRaiseError<ComputeClassHashError>
+ CanRaiseError<RevertedInvocation>
+ CanRaiseError<StarknetSierraCompilationError>,
{
async fn declare_contract(
chain: &Chain,
contract_class: &SierraClass,
) -> Result<Felt, Chain::Error> {
let provider = chain.provider();
let account = chain.account();

let class_hash = contract_class.class_hash().map_err(Chain::raise_error)?;

let class_exist_result = provider
.get_class(BlockId::Tag(BlockTag::Pending), class_hash)
.await;

if class_exist_result.is_ok() {
return Ok(class_hash);
}

// Compile Sierra class to Casm, following code in starkli
let casm_class_hash = {
let mut class = contract_class.clone();
class.abi.clear();

let sierra_class_json = serde_json::to_string(&class).map_err(Chain::raise_error)?;

let contract_class: ContractClass =
serde_json::from_str(&sierra_class_json).map_err(Chain::raise_error)?;

let casm_contract =
CasmContractClass::from_contract_class(contract_class, false, 180000)
.map_err(Chain::raise_error)?;

let casm_class_json =
serde_json::to_string(&casm_contract).map_err(Chain::raise_error)?;

let casm_class = serde_json::from_str::<CompiledClass>(&casm_class_json)
.map_err(Chain::raise_error)?;

casm_class.class_hash().map_err(Chain::raise_error)?
};

let flattened_class = contract_class
.clone()
.flatten()
.map_err(Chain::raise_error)?;

let declaration = account.declare_v3(Arc::new(flattened_class), casm_class_hash);

let declare_result = declaration.send().await.map_err(Chain::raise_error)?;

let tx_response = chain
.poll_tx_response(&declare_result.transaction_hash)
.await?;

if let Some(reverted) = tx_response.is_reverted() {
return Err(Chain::raise_error(reverted));
}

Ok(declare_result.class_hash)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use cgp_core::error::CanRaiseError;
use hermes_relayer_components::transaction::traits::poll_tx_response::CanPollTxResponse;
use hermes_test_components::chain::traits::types::address::HasAddressType;
use starknet::contract::ContractFactory;
use starknet::core::types::{Felt, RevertedInvocation};
use starknet::macros::felt;
use starknet::signers::SigningKey;

use crate::traits::account::{CanRaiseAccountErrors, HasStarknetAccount};
use crate::traits::contract::deploy::ContractDeployer;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::contract_class::HasContractClassHashType;
use crate::types::tx_response::TxResponse;

pub struct DeployStarknetContract;

const DEFAULT_UDC_ADDRESS: Felt =
felt!("0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf");

impl<Chain> ContractDeployer<Chain> for DeployStarknetContract
where
Chain: HasContractClassHashType<ContractClassHash = Felt>
+ HasAddressType<Address = Felt>
+ HasBlobType<Blob = Vec<Felt>>
+ CanPollTxResponse<TxHash = Felt, TxResponse = TxResponse>
+ HasStarknetAccount
+ CanRaiseAccountErrors
+ CanRaiseError<RevertedInvocation>,
{
async fn deploy_contract(
chain: &Chain,
class_hash: &Felt,
unique: bool,
constructor_call_data: &Vec<Felt>,
) -> Result<Felt, Chain::Error> {
let account = chain.account();

let factory = ContractFactory::new_with_udc(*class_hash, account, DEFAULT_UDC_ADDRESS);

let salt = SigningKey::from_random().secret_scalar();

let contract_deployment = factory.deploy_v3(constructor_call_data.clone(), salt, unique);

let deployed_address = contract_deployment.deployed_address();

let tx_hash = contract_deployment
.send()
.await
.map_err(Chain::raise_error)?
.transaction_hash;

let tx_response = chain.poll_tx_response(&tx_hash).await?;

if let Some(reverted) = tx_response.is_reverted() {
return Err(Chain::raise_error(reverted));
}

Ok(deployed_address)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod call;
pub mod declare;
pub mod deploy;
pub mod invoke;
pub mod message;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use cgp_core::Async;
use starknet::core::types::contract::SierraClass;
use starknet::core::types::Felt;

use crate::traits::types::contract_class::{
ProvideContractClassHashType, ProvideContractClassType,
};

pub struct ProvideStarknetContractTypes;

impl<Chain: Async> ProvideContractClassType<Chain> for ProvideStarknetContractTypes {
type ContractClass = SierraClass;
}

impl<Chain: Async> ProvideContractClassHashType<Chain> for ProvideStarknetContractTypes {
type ContractClassHash = Felt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod address;
pub mod amount;
pub mod blob;
pub mod chain_id;
pub mod contract;
pub mod denom;
pub mod event;
pub mod message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use cgp_core::prelude::*;

use crate::traits::types::contract_class::{HasContractClassHashType, HasContractClassType};

#[derive_component(ContractDeclarerComponent, ContractDeclarer<Chain>)]
#[async_trait]
pub trait CanDeclareContract:
HasContractClassType + HasContractClassHashType + HasErrorType
{
async fn declare_contract(
&self,
contract_class: &Self::ContractClass,
) -> Result<Self::ContractClassHash, Self::Error>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use cgp_core::prelude::*;
use hermes_test_components::chain::traits::types::address::HasAddressType;

use crate::traits::types::blob::HasBlobType;
use crate::traits::types::contract_class::HasContractClassHashType;

#[derive_component(ContractDeployerComponent, ContractDeployer<Chain>)]
#[async_trait]
pub trait CanDeployContract:
HasContractClassHashType + HasBlobType + HasAddressType + HasErrorType
{
async fn deploy_contract(
&self,
class_hash: &Self::ContractClassHash,
unique: bool,
constructor_call_data: &Self::Blob,
) -> Result<Self::Address, Self::Error>;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod call;
pub mod declare;
pub mod deploy;
pub mod invoke;
pub mod message;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use cgp_core::prelude::*;

#[derive_component(ContractClassTypeComponent, ProvideContractClassType<Chain>)]
pub trait HasContractClassType: Async {
type ContractClass: Async;
}

#[derive_component(ContractClassHashTypeComponent, ProvideContractClassHashType<Chain>)]
pub trait HasContractClassHashType: Async {
type ContractClassHash: Async;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod blob;
pub mod contract_class;
pub mod method;
Loading
Loading