Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
davebryson committed May 25, 2024
1 parent 9f90faf commit 47ba2f3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 13 deletions.
99 changes: 88 additions & 11 deletions src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ impl ContractAbi {
/// Parse the `abi` and `bytecode` from a compiled contract's json file.
/// Note: `raw` is un-parsed json.
pub fn from_full_json(raw: &str) -> Self {
let co = serde_json::from_str::<ContractObject>(raw).expect("parsing abi json");
let co =
serde_json::from_str::<ContractObject>(raw).expect("Abi: failed to parse abi to json");
if co.abi.is_none() {
panic!("ABI not found in file")
panic!("Abi: ABI not found in file")
}
if co.bytecode.is_none() {
panic!("Bytecode not found in file")
panic!("Abi: Bytecode not found in file")
}
Self {
abi: co.abi.unwrap(),
Expand All @@ -33,7 +34,7 @@ impl ContractAbi {
/// Parse the `abi` and `bytecode`
/// Note: `raw` is un-parsed json.
pub fn from_abi_bytecode(raw: &str, bytecode: Option<Vec<u8>>) -> Self {
let abi = serde_json::from_str::<JsonAbi>(raw).expect("parsing abi input");
let abi = serde_json::from_str::<JsonAbi>(raw).expect("Abi: failed to parse abi");
Self {
abi,
bytecode: bytecode.map(Bytes::from),
Expand All @@ -43,7 +44,7 @@ impl ContractAbi {
/// Parse an ABI (without bytecode) from a `Vec` of contract function definitions.
/// See [human readable abi](https://docs.ethers.org/v5/api/utils/abi/formats/#abi-formats--human-readable-abi)
pub fn from_human_readable(input: Vec<&str>) -> Self {
let abi = JsonAbi::parse(input).expect("valid solidity functions information");
let abi = JsonAbi::parse(input).expect("Abi: Invalid solidity function(s) format");
Self {
abi,
bytecode: None,
Expand Down Expand Up @@ -77,7 +78,7 @@ impl ContractAbi {
pub fn encode_constructor(&self, args: &str) -> Result<(Vec<u8>, bool)> {
let bytecode = match self.bytecode() {
Some(b) => b,
_ => bail!("Missing contract bytecode!"),
_ => bail!("Abi: Missing contract bytecode!"),
};

let constructor = match &self.abi.constructor {
Expand All @@ -93,7 +94,7 @@ impl ContractAbi {

let ty = DynSolType::Tuple(types);
let dynavalues = ty.coerce_str(args).map_err(|_| {
anyhow!("Error coercing the arguments for the constructor. Check the input argument(s)")
anyhow!("Abi: Error coercing the arguments for the constructor. Check the input argument(s)")
})?;
let encoded_args = dynavalues.abi_encode_params();
let is_payable = matches!(constructor.state_mutability, StateMutability::Payable);
Expand All @@ -110,7 +111,7 @@ impl ContractAbi {
let ty = DynSolType::Tuple(types);
ty.coerce_str(args).map_err(|_| {
anyhow!(
"Error coercing the arguments for the function call. Check the input argument(s)"
"Abi: Error coercing the arguments for the function call. Check the input argument(s)"
)
})
}
Expand Down Expand Up @@ -139,7 +140,7 @@ impl ContractAbi {
) -> anyhow::Result<(Vec<u8>, bool, DynSolType)> {
let funcs = match self.abi.function(name) {
Some(funcs) => funcs,
_ => bail!("Function {} not found in the ABI!", name),
_ => bail!("Abi: Function {} not found in the ABI!", name),
};

for f in funcs {
Expand All @@ -164,7 +165,7 @@ impl ContractAbi {
// if we get here, it means we didn't find a function that
// matched the input arguments
Err(anyhow::anyhow!(
"Arguments to the function do not match what is expected"
"Abi: Arguments to the function do not match what is expected"
))
}
}
Expand All @@ -173,8 +174,9 @@ impl ContractAbi {
mod tests {

use super::*;
use alloy_primitives::{Address, U256};
use alloy_primitives::{Address, FixedBytes, U256};
use alloy_sol_types::{sol, SolCall};
use hex::FromHex;

sol! {

Expand All @@ -198,6 +200,24 @@ mod tests {
}
}

sol! {
struct A {
uint256 value;
address owner;
bool isok;
}

struct B {
bytes data;
}

contract KitchenSink {
function check_types(uint256, bool, address, string, bytes32);
function check_both(A, B);
function check_blend(string, uint160, A);
}
}

#[test]
fn check_constructor_encoding() {
let input = vec!["constructor()"];
Expand Down Expand Up @@ -266,4 +286,61 @@ mod tests {

assert_eq!(sc, ac);
}

#[test]
fn encode_kitchen_sink() {
let addy = "0x023e09e337f5a6c82e62fe5ae4b6396d34930751";
// fixed bytes below
// "0101010101010101010101010101010101010101010101010101010101010101";

// encode with sol!
let expected_check_types = KitchenSink::check_typesCall {
_0: U256::from(1),
_1: true,
_2: Address::from_hex(addy).unwrap(),
_3: "bob".to_string(),
_4: FixedBytes::from_slice(&[1u8; 32]),
}
.abi_encode();

let abi = ContractAbi::from_human_readable(vec![
"function check_types(uint256, bool, address, string, bytes32)",
"function check_both(tuple(uint256, address, bool), tuple(bytes))",
"function check_blend(string, uint160, tuple(uint256, address, bool))",
]);

// encode with abi: python input format
let input = "(1, true, 0x023e09e337f5a6c82e62fe5ae4b6396d34930751, 'bob', 0101010101010101010101010101010101010101010101010101010101010101)";
let (actual, _, _) = abi.encode_function("check_types", input).unwrap();
assert_eq!(expected_check_types, actual);

let expected_check_both = KitchenSink::check_bothCall {
_0: A {
value: U256::from(10),
owner: Address::from_hex(addy).unwrap(),
isok: false,
},
_1: B { data: Bytes::new() },
}
.abi_encode();

let input_both = "((10, 0x023e09e337f5a6c82e62fe5ae4b6396d34930751, false),(0x))";
let (actualboth, _, _) = abi.encode_function("check_both", input_both).unwrap();
assert_eq!(expected_check_both, actualboth);

let expected_check_blend = KitchenSink::check_blendCall {
_0: "bob".to_string(),
_1: U256::from(5),
_2: A {
value: U256::from(10),
owner: Address::from_hex(addy).unwrap(),
isok: false,
},
}
.abi_encode();

let input_blend = "(bob, 5,(10, 0x023e09e337f5a6c82e62fe5ae4b6396d34930751, false))";
let (actualblend, _, _) = abi.encode_function("check_blend", input_blend).unwrap();
assert_eq!(expected_check_blend, actualblend)
}
}
4 changes: 2 additions & 2 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum SnapShotSource {
}

/// A single AccountRecord and it's associated storage. `SnapShot` stores
/// a map of these.
/// a map of Accounts.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SnapShotAccountRecord {
pub nonce: u64,
Expand All @@ -23,7 +23,7 @@ pub struct SnapShotAccountRecord {
pub storage: BTreeMap<U256, U256>,
}

/// The high-level objects containing the all the snapshot information.
/// The high-level objects containing all the snapshot information.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SnapShot {
pub source: SnapShotSource,
Expand Down

0 comments on commit 47ba2f3

Please sign in to comment.