Skip to content

Commit

Permalink
fix(cast): pass blocknum to cast storage rather than always using lat…
Browse files Browse the repository at this point in the history
…est (#7009)

* fix: pass blockid to get_storage_at()

* fix: test for cast storage --block fix
  • Loading branch information
g-01234 authored Feb 5, 2024
1 parent 7922fd5 commit 317ca38
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
11 changes: 7 additions & 4 deletions crates/cast/bin/cmd/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ impl StorageArgs {
artifact.get_deployed_bytecode_bytes().is_some_and(|b| *b == address_code)
});
if let Some((_, artifact)) = artifact {
return fetch_and_print_storage(provider, address.clone(), artifact, true).await;
return fetch_and_print_storage(provider, address.clone(), block, artifact, true)
.await;
}
}

Expand Down Expand Up @@ -173,7 +174,7 @@ impl StorageArgs {
// Clear temp directory
root.close()?;

fetch_and_print_storage(provider, address, artifact, true).await
fetch_and_print_storage(provider, address, block, artifact, true).await
}
}

Expand Down Expand Up @@ -211,6 +212,7 @@ impl StorageValue {
async fn fetch_and_print_storage(
provider: RetryProvider,
address: NameOrAddress,
block: Option<BlockId>,
artifact: &ConfigurableContractArtifact,
pretty: bool,
) -> Result<()> {
Expand All @@ -219,20 +221,21 @@ async fn fetch_and_print_storage(
Ok(())
} else {
let layout = artifact.storage_layout.as_ref().unwrap().clone();
let values = fetch_storage_slots(provider, address, &layout).await?;
let values = fetch_storage_slots(provider, address, block, &layout).await?;
print_storage(layout, values, pretty)
}
}

async fn fetch_storage_slots(
provider: RetryProvider,
address: NameOrAddress,
block: Option<BlockId>,
layout: &StorageLayout,
) -> Result<Vec<StorageValue>> {
let requests = layout.storage.iter().map(|storage_slot| async {
let slot = B256::from(U256::from_str(&storage_slot.slot)?);
let raw_slot_value =
provider.get_storage_at(address.clone(), slot.to_ethers(), None).await?.to_alloy();
provider.get_storage_at(address.clone(), slot.to_ethers(), block).await?.to_alloy();

let value = StorageValue { slot, raw_slot_value };

Expand Down
26 changes: 26 additions & 0 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,32 @@ casttest!(storage, |_prj, cmd| {
let six = "0x0000000000000000000000000000000000000000000000000000000000000006";
cmd.cast_fuse().args(["storage", usdt, decimals_slot, "--rpc-url", &rpc]);
assert_eq!(cmd.stdout_lossy().trim(), six);

let rpc = next_http_rpc_endpoint();
let total_supply_slot = "0x01";
let issued = "0x000000000000000000000000000000000000000000000000000000174876e800";
let block_before = "4634747";
let block_after = "4634748";
cmd.cast_fuse().args([
"storage",
usdt,
total_supply_slot,
"--rpc-url",
&rpc,
"--block",
block_before,
]);
assert_eq!(cmd.stdout_lossy().trim(), empty);
cmd.cast_fuse().args([
"storage",
usdt,
total_supply_slot,
"--rpc-url",
&rpc,
"--block",
block_after,
]);
assert_eq!(cmd.stdout_lossy().trim(), issued);
});

casttest!(balance, |_prj, cmd| {
Expand Down

0 comments on commit 317ca38

Please sign in to comment.