Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gen): fix Felt to always match the regex #862

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description = "Light client for Starknet"
edition = "2021"
name = "beerus"
version = "0.7.0"
version = "0.7.1"
repository = "https://github.com/eigerco/beerus"
license = "MIT"

Expand Down
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
Loading