From 9ee9d22addfda4291b1ecf99efdeab265cf80f8c Mon Sep 17 00:00:00 2001 From: dwiekawki Date: Wed, 5 Feb 2025 12:17:24 +0300 Subject: [PATCH] feat(gen): fix `Felt` to always match the regex --- src/gen.rs | 19 +++++++++++++++++++ tests/exe.rs | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/gen.rs b/src/gen.rs index be4687a0..a2a315da 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -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 { + let value = &fix(value); if FELT_REGEX.is_match(value) { Ok(Self(value.to_string())) } else { diff --git a/tests/exe.rs b/tests/exe.rs index d37256df..229b1c0d 100644 --- a/tests/exe.rs +++ b/tests/exe.rs @@ -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, block_id: gen::BlockId) -> State { let block = client.getBlockWithTxHashes(block_id).unwrap(); let gen::GetBlockWithTxHashesResult::BlockWithTxHashes(block) = block