fix: serialize eth_getStorageAt position param as quantity (#1281)

* fix: serialize eth_getStorageAt position param as quantity

* test: add quantity test
This commit is contained in:
Matthias Seitz 2022-05-18 16:49:32 +02:00 committed by GitHub
parent d77f92519c
commit 7509e42d3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 2 deletions

View File

@ -728,12 +728,15 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
NameOrAddress::Address(addr) => addr,
};
// position is a QUANTITY according to the [spec](https://eth.wiki/json-rpc/API#eth_getstorageat): integer of the position in the storage, converting this to a U256
// will make sure the number is formatted correctly as [quantity](https://eips.ethereum.org/EIPS/eip-1474#quantity)
let position = U256::from_big_endian(location.as_bytes());
let position = utils::serialize(&position);
let from = utils::serialize(&from);
let location = utils::serialize(&location);
let block = utils::serialize(&block.unwrap_or_else(|| BlockNumber::Latest.into()));
// get the hex encoded value.
let value: String = self.request("eth_getStorageAt", [from, location, block]).await?;
let value: String = self.request("eth_getStorageAt", [from, position, block]).await?;
// get rid of the 0x prefix and left pad it with zeroes.
let value = format!("{:0>64}", value.replace("0x", ""));
Ok(H256::from_slice(&Vec::from_hex(value)?))
@ -1526,6 +1529,22 @@ mod tests {
};
use futures_util::StreamExt;
#[test]
fn convert_h256_u256_quantity() {
let hash: H256 = H256::zero();
let quantity = U256::from_big_endian(hash.as_bytes());
assert_eq!(format!("{quantity:#x}"), "0x0");
assert_eq!(utils::serialize(&quantity).to_string(), "\"0x0\"");
let address: Address = "0x295a70b2de5e3953354a6a8344e616ed314d7251".parse().unwrap();
let block = BlockNumber::Latest;
let params =
[utils::serialize(&address), utils::serialize(&quantity), utils::serialize(&block)];
let params = serde_json::to_string(&params).unwrap();
assert_eq!(params, r#"["0x295a70b2de5e3953354a6a8344e616ed314d7251","0x0","latest"]"#);
}
#[tokio::test]
// Test vector from: https://docs.ethers.io/ethers.js/v5-beta/api-providers.html#id2
async fn mainnet_resolve_name() {