Skip to content

Commit

Permalink
refac: modify signed integer decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
EjembiEmmanuel committed Jul 9, 2024
1 parent c6ac6fc commit 40dc003
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
22 changes: 5 additions & 17 deletions bin/sozo/src/commands/calldata_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::{self, Result};
use bigdecimal::FromPrimitive;
use cainome::cairo_serde::{ByteArray, CairoSerde};
use num_bigint::BigUint;
use starknet::core::types::{Felt, FromStrError};
Expand Down Expand Up @@ -98,21 +97,10 @@ impl CalldataDecoder for ShortStrCalldataDecoder {
struct SignedIntegerCalldataDecoder;
impl CalldataDecoder for SignedIntegerCalldataDecoder {
fn decode(&self, input: &str) -> DecoderResult<Vec<Felt>> {
match input.parse::<i128>() {
Ok(value) if value <= i8::MAX as i128 && value >= i8::MIN as i128 => {
Ok(vec![Felt::from_i8(value as i8).expect("Invalid numeric string")])
}
Ok(value) if value <= i16::MAX as i128 && value >= i16::MIN as i128 => {
Ok(vec![Felt::from_i16(value as i16).expect("Invalid numeric string")])
}
Ok(value) if value <= i32::MAX as i128 && value >= i32::MIN as i128 => {
Ok(vec![Felt::from_i32(value as i32).expect("Invalid numeric string")])
}
Ok(value) if value <= i64::MAX as i128 && value >= i64::MIN as i128 => {
Ok(vec![Felt::from_i64(value as i64).expect("Invalid numeric string")])
}
Ok(value) => Ok(vec![Felt::from_i128(value).expect("Invalid numeric string")]),
Err(_) => Err(CalldataDecoderError::ParseError("Invalid numeric string".to_string())),
if let Ok(value) = input.parse::<i128>() {
Ok(vec![value.into()])
} else {
Err(CalldataDecoderError::ParseError("Invalid numeric string".to_string()))
}
}
}
Expand Down Expand Up @@ -174,7 +162,7 @@ fn decode_inner(item: &str) -> DecoderResult<Vec<Felt>> {
"u256" => U256CalldataDecoder.decode(value)?,
"str" => StrCalldataDecoder.decode(value)?,
"sstr" => ShortStrCalldataDecoder.decode(value)?,
"-" => SignedIntegerCalldataDecoder.decode(value)?,
"int" => SignedIntegerCalldataDecoder.decode(value)?,
_ => DefaultCalldataDecoder.decode(item)?,
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion bin/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct ExecuteArgs {
- u256: A 256-bit unsigned integer.
- sstr: A cairo short string.
- str: A cairo string (ByteArray).
- -: A signed integer.
- int: A signed integer.
- no prefix: A cairo felt or any type that fit into one felt.")]
pub calldata: Option<String>,

Expand Down

0 comments on commit 40dc003

Please sign in to comment.