Skip to content
Open
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
16 changes: 9 additions & 7 deletions graph/src/data/store/scalar/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::bail;
use num_bigint;
use serde::{self, Deserialize, Serialize};
use stable_hash::utils::AsInt;
Expand Down Expand Up @@ -207,14 +208,15 @@ impl BigInt {
}
}

pub fn to_unsigned_u256(&self) -> U256 {
pub fn to_unsigned_u256(&self) -> Result<U256, anyhow::Error> {
let (sign, bytes) = self.to_bytes_le();
assert!(
sign == BigIntSign::NoSign || sign == BigIntSign::Plus,
"negative value encountered for U256: {}",
self
);
U256::from_little_endian(&bytes)
if sign != BigIntSign::Plus && sign != BigIntSign::NoSign {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Minus variant is available. It may be clearer to use it

Suggested change
if sign != BigIntSign::Plus && sign != BigIntSign::NoSign {
if BigIntSign::Minus {

bail!(
"BigInt value is negative, cannot convert to unsigned U256: {}",
self
);
}
Ok(U256::from_little_endian(&bytes))
}

pub fn pow(self, exponent: u8) -> Result<BigInt, anyhow::Error> {
Expand Down
2 changes: 1 addition & 1 deletion graph/src/data_source/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ impl CallDecl {
(ParamType::Int(_), Value::BigInt(i)) => Ok(Token::Int(i.to_signed_u256())),
(ParamType::Uint(_), Value::Int(i)) if *i >= 0 => Ok(Token::Uint((*i).into())),
(ParamType::Uint(_), Value::BigInt(i)) if i.sign() == Sign::Plus => {
Ok(Token::Uint(i.to_unsigned_u256()))
Ok(Token::Uint(i.to_unsigned_u256()?))
}
(ParamType::Array(inner_type), Value::List(values)) => {
self.process_entity_array_values(values, inner_type.as_ref(), param_name)
Expand Down
2 changes: 1 addition & 1 deletion runtime/test/src/test/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ async fn test_abi_big_int(api_version: Version) {
.await;
let new_uint: BigInt = module.asc_get(new_uint_obj).unwrap();
assert_eq!(new_uint, BigInt::from(1_i32));
let new_uint = new_uint.to_unsigned_u256();
let new_uint = new_uint.to_unsigned_u256().unwrap();
assert_eq!(new_uint, U256([1, 0, 0, 0]));

// Test passing in -50 and increment it by 1
Expand Down
5 changes: 4 additions & 1 deletion runtime/wasm/src/to_from/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ impl FromAscObj<AscEnum<EthereumValueKind>> for ethabi::Token {
EthereumValueKind::Uint => {
let ptr: AscPtr<AscBigInt> = AscPtr::from(payload);
let n: BigInt = asc_get(heap, ptr, gas, depth)?;
Token::Uint(n.to_unsigned_u256())
let uint = n
.to_unsigned_u256()
.map_err(DeterministicHostError::Other)?;
Token::Uint(uint)
}
EthereumValueKind::String => {
let ptr: AscPtr<AscString> = AscPtr::from(payload);
Expand Down
Loading