Skip to content

Commit

Permalink
Introduced data transformer
Browse files Browse the repository at this point in the history
commit-id:8d990066
  • Loading branch information
integraledelebesgue committed Oct 9, 2024
1 parent b2ef942 commit a8b29ef
Show file tree
Hide file tree
Showing 14 changed files with 2,150 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/sncast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ cairo-lang-casm.workspace = true
cairo-lang-sierra-to-casm.workspace = true
cairo-lang-utils.workspace = true
cairo-lang-sierra.workspace = true
cairo-lang-parser.workspace = true
cairo-lang-syntax.workspace = true
cairo-lang-diagnostics.workspace = true
cairo-lang-filesystem.workspace = true
itertools.workspace = true
num-traits.workspace = true
num-bigint.workspace = true
starknet-types-core.workspace = true
cairo-vm.workspace = true
blockifier.workspace = true
Expand Down
47 changes: 47 additions & 0 deletions crates/sncast/src/helpers/calldata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use serde::{Deserialize, Serialize};
use starknet::core::types::{ContractClass, Felt};

use super::data_transformer::transformer::transform;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Calldata {
Serialized(Vec<Felt>),
Expressions(String),
}

impl From<Vec<String>> for Calldata {
fn from(calldata: Vec<String>) -> Self {
let maybe_serialized = calldata
.iter()
.map(|arg| {
Felt::from_dec_str(arg)
.or_else(|_| Felt::from_hex(arg))
.ok()
})
.collect::<Option<Vec<_>>>();

if let Some(serialized) = maybe_serialized {
Self::Serialized(serialized)
} else {
Self::Expressions(calldata.join(" "))
}
}
}

impl Calldata {
/// Serialize Calldata if it's given as a list of Cairo-like expressions (Strings),
/// return it as already serialized otherwise
pub fn serialized(
self,
class_definition: ContractClass,
function_selector: &Felt,
) -> anyhow::Result<Vec<Felt>> {
match self {
Calldata::Serialized(serialized) => Ok(serialized),
Calldata::Expressions(ref expressions) => {
transform(expressions, class_definition, function_selector)
}
}
}
}
2 changes: 2 additions & 0 deletions crates/sncast/src/helpers/data_transformer/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod sierra_abi;
pub mod transformer;
Loading

0 comments on commit a8b29ef

Please sign in to comment.