Skip to content

Commit

Permalink
feat(exe): handle both contract classes cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-melnychuk committed Apr 18, 2024
1 parent 7079e90 commit ec65de0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/experimental-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ hex = "0.4.3"

base64 = "0.22.0"
flate2 = "1.0.28"
cairo-lang-starknet-classes = "2.6.3"

[dev-dependencies]
tracing-subscriber.workspace = true
3 changes: 3 additions & 0 deletions crates/experimental-api/src/exe/err.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cairo_lang_starknet_classes::casm_contract_class::StarknetSierraCompilationError;
use thiserror::Error as ThisError;

#[derive(Debug, ThisError)]
Expand All @@ -24,6 +25,8 @@ pub enum Error {
),
#[error("Program error: {0:?}")]
Program(#[from] cairo_vm::types::errors::program_errors::ProgramError),
#[error("Sierra compilation error: {0:?}")]
SierraCompilation(#[from] StarknetSierraCompilationError),
#[error("{0}")]
Custom(&'static str),
}
Expand Down
23 changes: 19 additions & 4 deletions crates/experimental-api/src/exe/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use blockifier::execution::contract_class::{ContractClassV0, ContractClassV1};
use blockifier::execution::contract_class::ContractClassV0;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::contract_class::ContractClass as CairoContractClass;

use self::gen::DeprecatedContractClass;

Expand Down Expand Up @@ -40,10 +42,23 @@ impl TryFrom<gen::GetClassResult> for ContractClass {
type Error = Error;

fn try_from(value: gen::GetClassResult) -> Result<Self, Self::Error> {
let json = serde_json::to_string(&value)?;
Ok(match value {
gen::GetClassResult::ContractClass(_) => {
let class = ContractClassV1::try_from_json_string(&json)?;
gen::GetClassResult::ContractClass(ref class) => {
let mut json = serde_json::to_value(&value)?;
if let Some(abi) = class.abi.as_ref() {
let abi: serde_json::Value = serde_json::from_str(abi)?;
json["abi"] = abi;
}
let contract_class: CairoContractClass =
serde_json::from_value(json)?;
let casm_contract_class =
CasmContractClass::from_contract_class(
contract_class,
/*add_pythonic_hints=*/ false,
/*max_bytecode_size=*/ u16::MAX as usize,
)?;
let class = casm_contract_class.try_into()?;

ContractClass::V1(class)
}
gen::GetClassResult::DeprecatedContractClass(class) => {
Expand Down
2 changes: 2 additions & 0 deletions crates/experimental-api/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum Error {
Exe(#[from] beerus_experimental_api::exe::err::Error),
#[error("serde failed: {0:?}")]
Json(#[from] serde_json::Error),
#[error("starknet api error: {0:?}")]
Api(#[from] starknet_api::StarknetApiError),
}

impl From<iamgroot::jsonrpc::Error> for Error {
Expand Down
32 changes: 26 additions & 6 deletions crates/experimental-api/tests/exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ use beerus_experimental_api::{
mod common;

#[test]
fn test_call() -> Result<(), common::Error> {
fn test_call_deprecated_contract_class() -> Result<(), common::Error> {
let Ok(url) = std::env::var("BEERUS_EXPERIMENTAL_TEST_STARKNET_URL") else {
return Ok(());
};
let client = Client::new(&url);

// TODO: drop the logging
// cargo add -p beerus-experimental-api tracing-subscriber --dev
// RUST_LOG=beerus_experimental_api=debug cargo test --package beerus-experimental-api --test exe -- test_exec --exact --nocapture
tracing_subscriber::fmt::init();
tracing_subscriber::fmt::try_init().ok();

// TX: 0xcbb2b87d5378e682d650e0e7d36679b4557ba2bfa9d4e285b7168c04376b21
let json = serde_json::json!({
Expand All @@ -39,7 +35,31 @@ fn test_call() -> Result<(), common::Error> {
let function_call: FunctionCall = serde_json::from_value(json)?;

let call_info = call(&client, function_call)?;
println!("{call_info:#?}");
assert!(call_info.execution.retdata.0.is_empty());

Ok(())
}

#[test]
fn test_call_regular_contract_class() -> Result<(), common::Error> {
let Ok(url) = std::env::var("BEERUS_EXPERIMENTAL_TEST_STARKNET_URL") else {
return Ok(());
};
let client = Client::new(&url);
tracing_subscriber::fmt::try_init().ok();

let json = serde_json::json!({
"calldata": [],
"contract_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"entry_point_selector": "0x361458367e696363fbcc70777d07ebbd2394e89fd0adcaf147faccd1d294d60"
});
let function_call: FunctionCall = serde_json::from_value(json)?;

let call_info = call(&client, function_call)?;
println!("{call_info:#?}");
assert_eq!(call_info.execution.retdata.0.len(), 1);
assert_eq!(call_info.execution.retdata.0[0], "0x4574686572".try_into()?);

Ok(())
}

0 comments on commit ec65de0

Please sign in to comment.