Skip to content

Commit

Permalink
feat(gen): fix Felt to always match the regex
Browse files Browse the repository at this point in the history
  • Loading branch information
dwiekawki committed Feb 5, 2025
1 parent a3794b9 commit 9ee9d22
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,27 @@ pub mod gen {
.expect("Felt: valid regex")
});

// The RPC spec regex is not respected anywhere these days,
// thus such un-elegant workaround is necessary ¯\_(ツ)_/¯
fn fix(value: &str) -> String {
let unprefixed = value.strip_prefix("0x").unwrap_or(value);
if unprefixed.is_empty() {
// '0x'
"0x0".to_owned()
} else if unprefixed == "0" {
value.to_owned()
} else if unprefixed.starts_with("0") {
// '0x0...'
let unzeroed = unprefixed.trim_start_matches('0');
format!("0x{unzeroed}")
} else {
value.to_owned()
}
}

impl Felt {
pub fn try_new(value: &str) -> Result<Self, jsonrpc::Error> {
let value = &fix(value);
if FELT_REGEX.is_match(value) {
Ok(Self(value.to_string()))
} else {
Expand Down
20 changes: 20 additions & 0 deletions tests/exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ fn test_call_regular_contract_class() -> Result<(), Error> {
Ok(())
}

#[test]
fn test_issue861() -> Result<(), Error> {
let client = client!();

let json = serde_json::json!({
"calldata": [],
"contract_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"entry_point_selector": "0x01557182e4359a1f0c6301278e8f5b35a776ab58d39892581e357578fb287836"
});
let function_call: FunctionCall = serde_json::from_value(json)?;

let state = get_latest_state(&client);
let call_info = call(client, function_call, state)?;

assert_eq!(call_info.execution.retdata.0.len(), 2);
assert_eq!(call_info.execution.retdata.0[1].to_hex_string(), "0x0");

Ok(())
}

fn get_state(client: &Client<Http>, block_id: gen::BlockId) -> State {
let block = client.getBlockWithTxHashes(block_id).unwrap();
let gen::GetBlockWithTxHashesResult::BlockWithTxHashes(block) = block
Expand Down

0 comments on commit 9ee9d22

Please sign in to comment.