-
Notifications
You must be signed in to change notification settings - Fork 187
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
Signed integer parameters are decoded as unsigned integers #249
Comments
@nlordell this seems important for negative value logs right? should we add an I256 type to ethabi (either the one from ethers-core, and we can remove it from ethers, or the one you built in the past?) and use that as |
My favourite place to add it would be That being said, I think including it here also makes a lot of sense and I just marginally prefer having it in the aforementioned crates over |
Yep, supportive of importing to this package, and then seeing if they'd be willing to upstream. |
Need this fixed ! |
Just solved this kind of problem. In case someone needs it:
|
Thanks for idea. I need more precise numbers to store in json as a string so I've slightly changed this function. Please note that there is +1 term in formula because it's usually not visible in f64 but is notable in integer string representation. fn convert_uint256_to_int256_string(value: &U256) -> String {
let half_of_max_u256 = U256::from(u128::MAX);
let is_negative = *value > half_of_max_u256;
if is_negative {
let amount = U256::MAX - *value + 1;
format!("-{amount}")
} else {
value.to_string()
}
} I use this function in conjunction with matching Uint type versus Int (which implies for Uint) like this to make JSON of desired type depending of field type: let json_value = match token {
ethabi::Token::Address(address) => json!(format!("0x{}", address.encode_hex::<String>())),
ethabi::Token::FixedBytes(bytes) => json!(format!("0x{}", bytes.encode_hex::<String>())),
ethabi::Token::Bytes(bytes) => json!(format!("0x{}", bytes.encode_hex::<String>())),
ethabi::Token::Int(uint_value) => {
// Working around bug in ethabi library where int == uint in ethereum_types, so no negative values avaliable
let int_value_string = convert_uint256_to_int256_string(uint_value);
json!(int_value_string)
},
ethabi::Token::Uint(uint_value) => json!(uint_value.to_string()),
ethabi::Token::Bool(bool_value) => json!(if *bool_value { "true" } else { "false" }),
ethabi::Token::String(string_value) => json!(string_value),
...
}; |
Consider the following function. In this context
data
is the data from the smart contract event logs andethabi::decode
is being used to decode this data.This is supposed to parse the data of an event of the following signature:
This code fails to compile because, counterintuitively,
change
is of the typeU256
, which is an unsigned type. This stems from the following line:ethabi/ethabi/src/lib.rs
Line 91 in d862110
which seems completely wrong.
Is this done on purpose and, if so, how should one decode signed parameters? There seems to be no mention of it in the docs.
The text was updated successfully, but these errors were encountered: