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 11, 2024
1 parent 9026cb8 commit 6463d9b
Show file tree
Hide file tree
Showing 11 changed files with 2,027 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.

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 immedietely.

Check warning on line 33 in crates/data-transformer/src/calldata.rs

View workflow job for this annotation

GitHub Actions / Check typos

"immedietely" should be "immediately".

Check warning on line 33 in crates/data-transformer/src/calldata.rs

View workflow job for this annotation

GitHub Actions / Check typos

"immedietely" should be "immediately".

Check warning on line 33 in crates/data-transformer/src/calldata.rs

View workflow job for this annotation

GitHub Actions / Check typos

"immedietely" should be "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 6463d9b

Please sign in to comment.