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

fix: serialize slot as h256 66 byte string #1687

Closed
Closed
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
12 changes: 5 additions & 7 deletions ethers-providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,25 +734,23 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
async fn get_storage_at<T: Into<NameOrAddress> + Send + Sync>(
&self,
from: T,
location: H256,
slot: H256,
block: Option<BlockId>,
) -> Result<H256, ProviderError> {
let from = match from.into() {
NameOrAddress::Name(ens_name) => self.resolve_name(&ens_name).await?,
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);
// The slot argument Storage slot argument must have a length of 66, See <https://github.com/NomicFoundation/hardhat/pull/2581>
let slot = utils::serialize(&slot);
let from = utils::serialize(&from);
let block = utils::serialize(&block.unwrap_or_else(|| BlockNumber::Latest.into()));

// get the hex encoded value.
let value: String = self.request("eth_getStorageAt", [from, position, block]).await?;
let value: String = self.request("eth_getStorageAt", [from, slot, block]).await?;
// get rid of the 0x prefix and left pad it with zeroes.
let value = format!("{:0>64}", value.replace("0x", ""));
let value = format!("{:0>64}", value.strip_prefix("0x").unwrap_or(&value));
Ok(H256::from_slice(&Vec::from_hex(value)?))
}

Expand Down
14 changes: 13 additions & 1 deletion ethers-providers/tests/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{convert::TryFrom, time::Duration};
mod eth_tests {
use super::*;
use ethers_core::{
types::{Address, BlockId, TransactionRequest, H256},
types::{Address, BlockId, TransactionRequest, H256, U256},
utils::Anvil,
};
use ethers_providers::RINKEBY;
Expand Down Expand Up @@ -113,6 +113,18 @@ mod eth_tests {

send_zst_requests(provider).await;
}

// compatibility test for `eth_getStorageAt`
#[tokio::test]
#[ignore]
async fn eth_get_storage_at_compat() {
use ethers_core::abi::ethereum_types::BigEndianHash;
let provider = Provider::<Http>::try_from("http://localhost:8545").unwrap();
let acc: Address = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266".parse().unwrap();

let _value =
provider.get_storage_at(acc, H256::from_uint(&U256::from(6u64)), None).await.unwrap();
}
}

#[cfg(feature = "celo")]
Expand Down