rust-ethereum / ethabi

Encode and decode smart contract invocations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid data when decoding int24

Eduard-Voiculescu opened this issue · comments

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?

having the same issue, whats your solution?

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());