Skip to content

Commit d334c99

Browse files
authored
feat(cast): support base + offset for storage slot calc (#11566)
Closes #11531
1 parent 31ab324 commit d334c99

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

crates/cast/src/cmd/storage.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ pub struct StorageArgs {
4949

5050
/// The storage slot number. If not provided, it gets the full storage layout.
5151
#[arg(value_parser = parse_slot)]
52-
slot: Option<B256>,
52+
base_slot: Option<B256>,
53+
54+
/// The storage offset from the base slot. If not provided, it is assumed to be zero.
55+
#[arg(value_parser = str::parse::<U256>, default_value_t = U256::ZERO)]
56+
offset: U256,
5357

5458
/// The known proxy address. If provided, the storage layout is retrieved from this address.
5559
#[arg(long,value_parser = NameOrAddress::from_str)]
@@ -91,14 +95,22 @@ impl StorageArgs {
9195
pub async fn run(self) -> Result<()> {
9296
let config = self.load_config()?;
9397

94-
let Self { address, slot, block, build, .. } = self;
98+
let Self { address, base_slot, offset, block, build, .. } = self;
9599
let provider = utils::get_provider(&config)?;
96100
let address = address.resolve(&provider).await?;
97101

98102
// Slot was provided, perform a simple RPC call
99-
if let Some(slot) = slot {
103+
if let Some(slot) = base_slot {
100104
let cast = Cast::new(provider);
101-
sh_println!("{}", cast.storage(address, slot, block).await?)?;
105+
sh_println!(
106+
"{}",
107+
cast.storage(
108+
address,
109+
(Into::<U256>::into(slot).saturating_add(offset)).into(),
110+
block
111+
)
112+
.await?
113+
)?;
102114
return Ok(());
103115
}
104116

crates/cast/tests/cli/main.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,40 @@ casttest!(storage, |_prj, cmd| {
19061906
.stdout_eq(str![[r#"
19071907
0x000000000000000000000000000000000000000000000000000000174876e800
19081908
1909+
"#]]);
1910+
1911+
let decimal_slot_offset_from_total_supply_slot = "0x08";
1912+
let decimal_slot_offset_from_total_supply_slot_uint = "8";
1913+
let rpc = next_http_archive_rpc_url();
1914+
cmd.cast_fuse()
1915+
.args([
1916+
"storage",
1917+
usdt,
1918+
total_supply_slot,
1919+
decimal_slot_offset_from_total_supply_slot,
1920+
"--rpc-url",
1921+
&rpc,
1922+
])
1923+
.assert_success()
1924+
.stdout_eq(str![[r#"
1925+
0x0000000000000000000000000000000000000000000000000000000000000006
1926+
1927+
"#]]);
1928+
1929+
let rpc = next_http_archive_rpc_url();
1930+
cmd.cast_fuse()
1931+
.args([
1932+
"storage",
1933+
usdt,
1934+
total_supply_slot,
1935+
decimal_slot_offset_from_total_supply_slot_uint,
1936+
"--rpc-url",
1937+
&rpc,
1938+
])
1939+
.assert_success()
1940+
.stdout_eq(str![[r#"
1941+
0x0000000000000000000000000000000000000000000000000000000000000006
1942+
19091943
"#]]);
19101944
});
19111945

0 commit comments

Comments
 (0)