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

Invalid data when decoding int24 #274

Open
Eduard-Voiculescu opened this issue Jul 9, 2022 · 2 comments
Open

Invalid data when decoding int24 #274

Eduard-Voiculescu opened this issue Jul 9, 2022 · 2 comments

Comments

@Eduard-Voiculescu
Copy link

Eduard-Voiculescu commented Jul 9, 2022

Hi, so I am using ethabi library to try and decode an int24 on a Mint event. It seems that the returned value is not correct.

Here is how I reproduce the issue.

First I check etherscan for the Mint event (log ord 29).

The values in decimal for tickLower and tickHigher are, respectively, -50580 and -36720.
The values in hex for tickLower and tickHigher are, respectively, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff3a6c and 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7090.

Rust main.rs to reproduce the issue:

use bigdecimal::ToPrimitive;
use num_bigint::BigInt;
use ethabi;
use ethabi::Token;

fn main() {
	// bytes representation for ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff3a6c
	let bytes_array = [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 58, 108];
	let mut bytes_str = String::new();

	for bytes in &bytes_array {
		bytes_str.push_str(&format!("{:x}", bytes));
	}

	println!("{:?}", bytes_str);
	println!("{:?}", bytes_array);

	let eth_int: Vec<Token> = ethabi::decode(
		&[ethabi::ParamType::Int(24usize)],
		bytes_array.as_slice(),
	).unwrap();
	println!("{:?}", eth_int);

	let bi = BigInt::from_signed_bytes_be(&bytes_array);
	println!("{}", bi.to_i32().unwrap());
}

And this is the result I get when running the code:

"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff3a6c"
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 58, 108]
[Int(115792089237316195423570985008687907853269984665640564039457584007913129589356)]
-50580

Dependencies needed:

num-bigint = "0.4"
bigdecimal = "0.3"

The decoded value is an "int" which is equal to 115792089237316195423570985008687907853269984665640564039457584007913129589356 however, when looking at etherscan the value is off and doesn't seem correct. It should be -50580.

I was able to hack my way with this issue by converting the value to a BigInt which works correctly.

Am I doing something wrong or is there something else that I have to do to get the correct value?

@philipp1992
Copy link

having the same issue, whats your solution?

@Eduard-Voiculescu
Copy link
Author

Depends on what you want to do, in my use-case I did this:

let bi = BigInt::from_signed_bytes_be(&bytes_array);
println!("{}", bi.to_i32().unwrap());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants