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 11, 2024
1 parent 4717204 commit d25b4a5
Show file tree
Hide file tree
Showing 12 changed files with 2,037 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ jobs:
- name: Run Cheatnet tests
run: cargo test --release -p cheatnet

test-data-transformer:
name: Test Data Transformer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84
- name: Run Data Transformer tests
run: cargo test --release -p data-transformer

test-forge-scarb-plugin:
name: Test Forge Scarb Plugin
runs-on: ubuntu-latest
Expand Down
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 d25b4a5

Please sign in to comment.