Skip to content

Commit

Permalink
Implemented data transformation logic
Browse files Browse the repository at this point in the history
commit-id:8d990066
  • Loading branch information
integraledelebesgue committed Oct 16, 2024
1 parent f65ba8a commit 9d004a6
Show file tree
Hide file tree
Showing 12 changed files with 2,041 additions and 1 deletion.
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.

47 changes: 47 additions & 0 deletions crates/data-transformer/src/calldata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use super::transformer::transform;
use serde::{Deserialize, Serialize};
use starknet::core::types::{ContractClass, Felt};

#[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 the calldata.
/// If it's given as a list of `Felt`s, return it immediately.
/// Otherwise, try to interpret is as a comma-separated sequence of Cairo expressions.
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)
}
}
}
}
6 changes: 6 additions & 0 deletions crates/data-transformer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
pub mod cairo_types;
mod calldata;
mod sierra_abi;
mod transformer;

pub use calldata::Calldata;
pub use transformer::transform;
Loading

0 comments on commit 9d004a6

Please sign in to comment.