Skip to content

Commit

Permalink
feat(cast): Implemented cast index (seth feature parity #29) (#676)
Browse files Browse the repository at this point in the history
* feat(cast): Implemented cast index (seth feature parity #29)

* made the suggested changes after review #676
  • Loading branch information
psushi authored Feb 5, 2022
1 parent 7023b66 commit 4e89a5a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,32 @@ impl SimpleCast {

Ok(code)
}
/// Prints the slot number for the specified mapping type and input data
/// Uses abi_encode to pad the data to 32 bytes.
/// For value types v, slot number of v is keccak256(concat(h(v) , p)) where h is the padding
/// function and p is slot number of the mapping.
/// ```
/// # use cast::SimpleCast as Cast;
///
/// # fn main() -> eyre::Result<()> {
///
/// assert_eq!(Cast::index("address", "uint256" ,"0xD0074F4E6490ae3f888d1d4f7E3E43326bD3f0f5" ,"2").unwrap().as_str(),"0x9525a448a9000053a4d151336329d6563b7e80b24f8e628e95527f218e8ab5fb");
/// assert_eq!(Cast::index("uint256", "uint256" ,"42" ,"6").unwrap().as_str(),"0xfc808b0f31a1e6b9cf25ff6289feae9b51017b392cc8e25620a94a38dcdafcc1");
/// # Ok(())
/// # }
/// ```
pub fn index(
from_type: &str,
to_type: &str,
from_value: &str,
slot_number: &str,
) -> Result<String> {
let sig = format!("x({},{})", from_type, to_type);
let encoded = Self::abi_encode(&sig, &[from_value, slot_number])?;
let location: String = Self::keccak(&encoded)?;
Ok(location)
}
}

fn strip_0x(s: &str) -> &str {
Expand Down
4 changes: 4 additions & 0 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ async fn main() -> eyre::Result<()> {
Subcommands::AbiEncode { sig, args } => {
println!("{}", SimpleCast::abi_encode(&sig, &args)?);
}
Subcommands::Index { from_type, to_type, from_value, slot_number } => {
let encoded = SimpleCast::index(&from_type, &to_type, &from_value, &slot_number)?;
println!("{}", encoded);
}
Subcommands::FourByte { selector } => {
let sigs = foundry_utils::fourbyte(&selector).await?;
sigs.iter().for_each(|sig| println!("{}", sig.0));
Expand Down
14 changes: 14 additions & 0 deletions cli/src/opts/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ pub enum Subcommands {
#[clap(allow_hyphen_values = true)]
args: Vec<String>,
},
#[clap(name = "index")]
#[clap(
about = "Get storage slot of value from mapping type, mapping slot number and input value"
)]
Index {
#[clap(help = "mapping key type")]
from_type: String,
#[clap(help = "mapping value type")]
to_type: String,
#[clap(help = "the value")]
from_value: String,
#[clap(help = "storage slot of the mapping")]
slot_number: String,
},
#[clap(name = "4byte")]
#[clap(about = "Fetches function signatures given the selector from 4byte.directory")]
FourByte {
Expand Down

0 comments on commit 4e89a5a

Please sign in to comment.