-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented data transformation logic
commit-id:8d990066
- Loading branch information
1 parent
f65ba8a
commit 1c7d955
Showing
12 changed files
with
2,060 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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| { | ||
if arg | ||
.chars() | ||
.all(|c| c.is_ascii_hexdigit() || c == 'x' || c == 'X') | ||
{ | ||
Felt::from_dec_str(arg) | ||
.or_else(|_| Felt::from_hex(arg)) | ||
.ok() | ||
} else { | ||
None | ||
} | ||
}) | ||
.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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.