-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(starknet_contract_manager_types): add crate
- Loading branch information
Showing
6 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,18 @@ | ||
[package] | ||
name = "starknet_contract_manager_types" | ||
edition.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
version.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
async-trait.workspace = true | ||
cairo-lang-starknet-classes.workspace = true | ||
papyrus_proc_macros.workspace = true | ||
serde = { workspace = true, features = ["derive"] } | ||
starknet_api.workspace = true | ||
starknet_sequencer_infra.workspace = true | ||
thiserror.workspace = true |
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 @@ | ||
use async_trait::async_trait; | ||
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; | ||
use papyrus_proc_macros::handle_response_variants; | ||
use serde::{Deserialize, Serialize}; | ||
use starknet_api::core::{ClassHash, CompiledClassHash}; | ||
use starknet_api::state::SierraContractClass; | ||
use starknet_sequencer_infra::component_client::ClientError; | ||
use starknet_sequencer_infra::component_definitions::ComponentClient; | ||
use thiserror::Error; | ||
|
||
pub type ContractManagerResult<T> = Result<T, ContractManagerError>; | ||
pub type ContractManagerClientResult<T> = Result<T, ContractManagerClientError>; | ||
|
||
// TODO: export. | ||
type ContractId = ClassHash; | ||
type ExecutableClass = CasmContractClass; | ||
type ExecutableClassHash = CompiledClassHash; | ||
|
||
/// Serves as the contract manager's shared interface. | ||
/// Requires `Send + Sync` to allow transferring and sharing resources (inputs, futures) across | ||
/// threads. | ||
#[async_trait] | ||
pub trait ContractManagerClient: Send + Sync { | ||
// TODO(native): make generic in executable type. | ||
async fn add_contract( | ||
&self, | ||
contract_id: ContractId, | ||
contract: SierraContractClass, | ||
) -> ContractManagerClientResult<ExecutableClassHash>; | ||
|
||
async fn get_executable( | ||
&self, | ||
contract_id: ContractId, | ||
) -> ContractManagerClientResult<ExecutableClass>; | ||
|
||
async fn get_sierra( | ||
&self, | ||
contract_id: ContractId, | ||
) -> ContractManagerClientResult<SierraContractClass>; | ||
} | ||
|
||
#[derive(Clone, Debug, Error, Eq, PartialEq, Serialize, Deserialize)] | ||
pub enum ContractManagerError { | ||
#[error("Compilation failed: {0}")] | ||
CompilationUtilError(String), | ||
#[error("Contract of hash: {contract_id} not found")] | ||
ContractNotFound { contract_id: ContractId }, | ||
} | ||
|
||
#[derive(Clone, Debug, Error)] | ||
pub enum ContractManagerClientError { | ||
#[error(transparent)] | ||
ClientError(#[from] ClientError), | ||
#[error(transparent)] | ||
ContractManagerError(#[from] ContractManagerError), | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub enum ContractManagerRequest { | ||
AddContract(ContractId, SierraContractClass), | ||
GetExecutable(ContractId), | ||
GetSierra(ContractId), | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub enum ContractManagerResponse { | ||
AddContract(ContractManagerResult<ExecutableClassHash>), | ||
GetExecutable(ContractManagerResult<ExecutableClass>), | ||
GetSierra(ContractManagerResult<SierraContractClass>), | ||
} | ||
|
||
#[async_trait] | ||
impl<ComponentClientType> ContractManagerClient for ComponentClientType | ||
where | ||
ComponentClientType: | ||
Send + Sync + ComponentClient<ContractManagerRequest, ContractManagerResponse>, | ||
{ | ||
async fn add_contract( | ||
&self, | ||
contract_id: ContractId, | ||
contract: SierraContractClass, | ||
) -> ContractManagerClientResult<ExecutableClassHash> { | ||
let request = ContractManagerRequest::AddContract(contract_id, contract); | ||
let response = self.send(request).await; | ||
handle_response_variants!( | ||
ContractManagerResponse, | ||
AddContract, | ||
ContractManagerClientError, | ||
ContractManagerError | ||
) | ||
} | ||
|
||
async fn get_executable( | ||
&self, | ||
contract_id: ContractId, | ||
) -> ContractManagerClientResult<ExecutableClass> { | ||
let request = ContractManagerRequest::GetExecutable(contract_id); | ||
let response = self.send(request).await; | ||
handle_response_variants!( | ||
ContractManagerResponse, | ||
GetExecutable, | ||
ContractManagerClientError, | ||
ContractManagerError | ||
) | ||
} | ||
|
||
async fn get_sierra( | ||
&self, | ||
contract_id: ContractId, | ||
) -> ContractManagerClientResult<SierraContractClass> { | ||
let request = ContractManagerRequest::GetSierra(contract_id); | ||
let response = self.send(request).await; | ||
handle_response_variants!( | ||
ContractManagerResponse, | ||
GetSierra, | ||
ContractManagerClientError, | ||
ContractManagerError | ||
) | ||
} | ||
} |
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