Skip to content

Commit

Permalink
feat: allow loading of the entire Sierra class
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Dec 19, 2023
1 parent 7e6a74b commit 60760fe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
33 changes: 23 additions & 10 deletions crates/rs/src/macro_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! TODO: support the full artifact JSON to be able to
//! deploy contracts from abigen.
use quote::ToTokens;
use starknet::core::types::contract::AbiEntry;
use starknet::core::types::contract::{AbiEntry, SierraClass};
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::path::Path;
Expand All @@ -25,6 +25,7 @@ use syn::{
};

use crate::spanned::Spanned;
use crate::utils;

const CARGO_MANIFEST_DIR: &str = "$CARGO_MANIFEST_DIR/";

Expand Down Expand Up @@ -56,15 +57,19 @@ impl Parse for ContractAbi {
json_path
};

let abi = serde_json::from_reader::<_, Vec<AbiEntry>>(
File::open(json_path.value()).map_err(|e| {
syn::Error::new(
json_path.span(),
format!("JSON open file {} error: {}", json_path.value(), e),
)
})?,
)
.map_err(|e| syn::Error::new(json_path.span(), format!("JSON parse error: {}", e)))?;
// To prepare the declare and deploy features, we also
// accept a full Sierra artifact for the ABI.
// To support declare and deploy, the full class must be stored.
let abi = if let Ok(sierra) =
serde_json::from_reader::<_, SierraClass>(open_json_file(&json_path.value())?)
{
sierra.abi
} else {
serde_json::from_reader::<_, Vec<AbiEntry>>(open_json_file(&json_path.value())?)
.map_err(|e| {
syn::Error::new(json_path.span(), format!("JSON parse error: {}", e))
})?
};

let mut output_path: Option<String> = None;
let mut type_aliases = HashMap::new();
Expand Down Expand Up @@ -141,4 +146,12 @@ impl Parse for TypeAlias {
}
}

fn open_json_file(file_path: &str) -> Result<File> {
File::open(file_path).map_err(|e| {
syn::Error::new(
utils::str_to_litstr(file_path).span(),
format!("JSON open file {} error: {}", file_path, e),
)
})
}
// TODO: add test for argument parsing.
5 changes: 5 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ make setup_simple_get_set
```
cargo run --example simple_get_set --features="abigen-rs"
```

## IMPORTANT

Currently Starkli does not support `2.4.0` compiler. The examples are compiled using `2.4.0` to test all the features
including the latest, but if you experience errors while deploying with starkli, consider re-compiling with `2.3.1` (except for `byte_array`, which is only supported by `2.4.0`).
8 changes: 7 additions & 1 deletion examples/simple_get_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ const KATANA_ACCOUNT_0: &str = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa
const KATANA_PRIVKEY_0: &str = "0x1800000000300000180000000000030000000000003006001800006600";
const KATANA_CHAIN_ID: &str = "0x4b4154414e41";

abigen!(MyContract, "./contracts/abi/simple_get_set.abi.json");
// You can load of the sierra class entirely from the artifact.
// Or you can use the extracted abi entries with jq in contracts/abi/.
abigen!(
MyContract,
"./contracts/target/dev/contracts_simple_get_set.contract_class.json"
);
//abigen!(MyContract, "./contracts/abi/simple_get_set.abi.json");

#[tokio::main]
async fn main() {
Expand Down

0 comments on commit 60760fe

Please sign in to comment.