2023-02-06 21:27:01 +00:00
|
|
|
use crate::contract::ContractInstance;
|
2020-10-27 10:57:01 +00:00
|
|
|
|
2021-10-27 22:07:24 +00:00
|
|
|
pub use ethers_core::abi::AbiError;
|
2020-10-27 10:57:01 +00:00
|
|
|
use ethers_core::{
|
2022-03-02 13:58:18 +00:00
|
|
|
abi::{Abi, Detokenize, Error, Event, Function, FunctionExt, RawLog, Token, Tokenize},
|
2020-10-29 07:48:24 +00:00
|
|
|
types::{Address, Bytes, Selector, H256},
|
2020-10-27 10:57:01 +00:00
|
|
|
};
|
|
|
|
use ethers_providers::Middleware;
|
|
|
|
|
2021-09-13 17:43:54 +00:00
|
|
|
use std::{
|
2023-02-06 21:27:01 +00:00
|
|
|
borrow::Borrow,
|
2021-09-13 17:43:54 +00:00
|
|
|
collections::{BTreeMap, HashMap},
|
|
|
|
fmt::Debug,
|
|
|
|
hash::Hash,
|
|
|
|
};
|
2020-10-27 10:57:01 +00:00
|
|
|
|
|
|
|
/// A reduced form of `Contract` which just takes the `abi` and produces
|
|
|
|
/// ABI encoded data for its functions.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct BaseContract {
|
|
|
|
pub(crate) abi: Abi,
|
|
|
|
|
|
|
|
/// A mapping from method signature to a name-index pair for accessing
|
|
|
|
/// functions in the contract ABI. This is used to avoid allocation when
|
|
|
|
/// searching for matching functions by signature.
|
2022-03-19 17:05:39 +00:00
|
|
|
// Adapted from: <https://github.com/gnosis/ethcontract-rs/blob/master/src/contract.rs>
|
2020-11-12 10:08:20 +00:00
|
|
|
pub methods: HashMap<Selector, (String, usize)>,
|
2020-10-27 10:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Abi> for BaseContract {
|
|
|
|
/// Creates a new `BaseContract` from the abi.
|
|
|
|
fn from(abi: Abi) -> Self {
|
|
|
|
let methods = create_mapping(&abi.functions, |function| function.selector());
|
|
|
|
Self { abi, methods }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BaseContract {
|
2020-10-29 07:48:24 +00:00
|
|
|
/// Returns the ABI encoded data for the provided function and arguments
|
|
|
|
///
|
|
|
|
/// If the function exists multiple times and you want to use one of the overloaded
|
|
|
|
/// versions, consider using `encode_with_selector`
|
|
|
|
pub fn encode<T: Tokenize>(&self, name: &str, args: T) -> Result<Bytes, AbiError> {
|
|
|
|
let function = self.abi.function(name)?;
|
2020-12-31 19:08:12 +00:00
|
|
|
encode_function_data(function, args)
|
2020-10-29 07:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the ABI encoded data for the provided function selector and arguments
|
|
|
|
pub fn encode_with_selector<T: Tokenize>(
|
|
|
|
&self,
|
|
|
|
signature: Selector,
|
|
|
|
args: T,
|
|
|
|
) -> Result<Bytes, AbiError> {
|
|
|
|
let function = self.get_from_signature(signature)?;
|
2020-12-31 19:08:12 +00:00
|
|
|
encode_function_data(function, args)
|
2020-10-29 07:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Decodes the provided ABI encoded function arguments with the selected function name.
|
|
|
|
///
|
|
|
|
/// If the function exists multiple times and you want to use one of the overloaded
|
|
|
|
/// versions, consider using `decode_with_selector`
|
2020-11-12 10:08:20 +00:00
|
|
|
pub fn decode<D: Detokenize, T: AsRef<[u8]>>(
|
2020-10-29 07:48:24 +00:00
|
|
|
&self,
|
|
|
|
name: &str,
|
2020-11-12 10:08:20 +00:00
|
|
|
bytes: T,
|
2020-10-29 07:48:24 +00:00
|
|
|
) -> Result<D, AbiError> {
|
|
|
|
let function = self.abi.function(name)?;
|
2020-12-31 19:08:12 +00:00
|
|
|
decode_function_data(function, bytes, true)
|
2020-10-29 07:48:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 13:58:18 +00:00
|
|
|
/// Decodes the provided ABI encoded function arguments with the selected function name.
|
|
|
|
///
|
|
|
|
/// If the function exists multiple times and you want to use one of the overloaded
|
|
|
|
/// versions, consider using `decode_with_selector`
|
|
|
|
///
|
|
|
|
/// Returns a [`Token`] vector, which lets you decode function arguments dynamically
|
|
|
|
/// without knowing the return type.
|
|
|
|
pub fn decode_raw<T: AsRef<[u8]>>(&self, name: &str, bytes: T) -> Result<Vec<Token>, AbiError> {
|
|
|
|
let function = self.abi.function(name)?;
|
|
|
|
decode_function_data_raw(function, bytes, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decodes the provided ABI encoded function output with the selected function name.
|
|
|
|
///
|
|
|
|
/// If the function exists multiple times and you want to use one of the overloaded
|
|
|
|
/// versions, consider using `decode_with_selector`
|
|
|
|
pub fn decode_output<D: Detokenize, T: AsRef<[u8]>>(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
bytes: T,
|
|
|
|
) -> Result<D, AbiError> {
|
|
|
|
let function = self.abi.function(name)?;
|
|
|
|
decode_function_data(function, bytes, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decodes the provided ABI encoded function output with the selected function name.
|
|
|
|
///
|
|
|
|
/// If the function exists multiple times and you want to use one of the overloaded
|
|
|
|
/// versions, consider using `decode_with_selector`
|
|
|
|
///
|
|
|
|
/// Returns a [`Token`] vector, which lets you decode function arguments dynamically
|
|
|
|
/// without knowing the return type.
|
|
|
|
pub fn decode_output_raw<T: AsRef<[u8]>>(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
bytes: T,
|
|
|
|
) -> Result<Vec<Token>, AbiError> {
|
|
|
|
let function = self.abi.function(name)?;
|
|
|
|
decode_function_data_raw(function, bytes, false)
|
|
|
|
}
|
|
|
|
|
2020-10-29 07:48:24 +00:00
|
|
|
/// Decodes for a given event name, given the `log.topics` and
|
|
|
|
/// `log.data` fields from the transaction receipt
|
|
|
|
pub fn decode_event<D: Detokenize>(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
topics: Vec<H256>,
|
|
|
|
data: Bytes,
|
|
|
|
) -> Result<D, AbiError> {
|
|
|
|
let event = self.abi.event(name)?;
|
|
|
|
decode_event(event, topics, data)
|
|
|
|
}
|
|
|
|
|
2022-03-02 13:58:18 +00:00
|
|
|
/// Decodes for a given event name, given the `log.topics` and
|
|
|
|
/// `log.data` fields from the transaction receipt
|
|
|
|
///
|
|
|
|
/// Returns a [`Token`] vector, which lets you decode function arguments dynamically
|
|
|
|
/// without knowing the return type.
|
|
|
|
pub fn decode_event_raw(
|
|
|
|
&self,
|
|
|
|
name: &str,
|
|
|
|
topics: Vec<H256>,
|
|
|
|
data: Bytes,
|
|
|
|
) -> Result<Vec<Token>, AbiError> {
|
|
|
|
let event = self.abi.event(name)?;
|
|
|
|
decode_event_raw(event, topics, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decodes the provided ABI encoded bytes with the selected function selector
|
|
|
|
///
|
|
|
|
/// Returns a [`Token`] vector, which lets you decode function arguments dynamically
|
|
|
|
/// without knowing the return type.
|
|
|
|
pub fn decode_with_selector_raw<T: AsRef<[u8]>>(
|
|
|
|
&self,
|
|
|
|
signature: Selector,
|
|
|
|
bytes: T,
|
|
|
|
) -> Result<Vec<Token>, AbiError> {
|
|
|
|
let function = self.get_from_signature(signature)?;
|
|
|
|
decode_function_data_raw(function, bytes, true)
|
|
|
|
}
|
|
|
|
|
2020-10-29 07:48:24 +00:00
|
|
|
/// Decodes the provided ABI encoded bytes with the selected function selector
|
2020-11-12 10:08:20 +00:00
|
|
|
pub fn decode_with_selector<D: Detokenize, T: AsRef<[u8]>>(
|
2020-10-29 07:48:24 +00:00
|
|
|
&self,
|
|
|
|
signature: Selector,
|
2020-11-12 10:08:20 +00:00
|
|
|
bytes: T,
|
2020-10-29 07:48:24 +00:00
|
|
|
) -> Result<D, AbiError> {
|
|
|
|
let function = self.get_from_signature(signature)?;
|
2020-12-31 19:08:12 +00:00
|
|
|
decode_function_data(function, bytes, true)
|
2020-10-29 07:48:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 22:52:25 +00:00
|
|
|
/// Decode the provided ABI encoded bytes as the output of the provided
|
|
|
|
/// function selector
|
2022-03-02 13:58:18 +00:00
|
|
|
pub fn decode_output_with_selector<D: Detokenize, T: AsRef<[u8]>>(
|
|
|
|
&self,
|
|
|
|
signature: Selector,
|
|
|
|
bytes: T,
|
|
|
|
) -> Result<D, AbiError> {
|
|
|
|
let function = self.get_from_signature(signature)?;
|
|
|
|
decode_function_data(function, bytes, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decodes the provided ABI encoded bytes with the selected function selector
|
|
|
|
///
|
|
|
|
/// Returns a [`Token`] vector, which lets you decode function arguments dynamically
|
|
|
|
/// without knowing the return type.
|
|
|
|
pub fn decode_output_with_selector_raw<T: AsRef<[u8]>>(
|
|
|
|
&self,
|
|
|
|
signature: Selector,
|
|
|
|
bytes: T,
|
|
|
|
) -> Result<Vec<Token>, AbiError> {
|
|
|
|
let function = self.get_from_signature(signature)?;
|
|
|
|
decode_function_data_raw(function, bytes, false)
|
|
|
|
}
|
|
|
|
|
2020-10-29 07:48:24 +00:00
|
|
|
fn get_from_signature(&self, signature: Selector) -> Result<&Function, AbiError> {
|
|
|
|
Ok(self
|
|
|
|
.methods
|
|
|
|
.get(&signature)
|
|
|
|
.map(|(name, index)| &self.abi.functions[name][*index])
|
2020-12-31 17:19:14 +00:00
|
|
|
.ok_or_else(|| Error::InvalidName(hex::encode(signature)))?)
|
2020-10-29 07:48:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 10:57:01 +00:00
|
|
|
/// Returns a reference to the contract's ABI
|
|
|
|
pub fn abi(&self) -> &Abi {
|
|
|
|
&self.abi
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Upgrades a `BaseContract` into a full fledged contract with an address and middleware.
|
2023-02-06 21:27:01 +00:00
|
|
|
pub fn into_contract<B, M>(self, address: Address, client: B) -> ContractInstance<B, M>
|
|
|
|
where
|
|
|
|
B: Borrow<M>,
|
|
|
|
M: Middleware,
|
|
|
|
{
|
|
|
|
ContractInstance::new(address, self, client)
|
2020-10-27 10:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<Abi> for BaseContract {
|
|
|
|
fn as_ref(&self) -> &Abi {
|
|
|
|
self.abi()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 13:58:18 +00:00
|
|
|
pub fn decode_event_raw(
|
2020-10-29 07:48:24 +00:00
|
|
|
event: &Event,
|
|
|
|
topics: Vec<H256>,
|
|
|
|
data: Bytes,
|
2022-03-02 13:58:18 +00:00
|
|
|
) -> Result<Vec<Token>, AbiError> {
|
|
|
|
Ok(event
|
2021-10-29 12:29:35 +00:00
|
|
|
.parse_log(RawLog { topics, data: data.to_vec() })?
|
2020-10-29 07:48:24 +00:00
|
|
|
.params
|
|
|
|
.into_iter()
|
|
|
|
.map(|param| param.value)
|
2022-03-02 13:58:18 +00:00
|
|
|
.collect::<Vec<_>>())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decode_event<D: Detokenize>(
|
|
|
|
event: &Event,
|
|
|
|
topics: Vec<H256>,
|
|
|
|
data: Bytes,
|
|
|
|
) -> Result<D, AbiError> {
|
|
|
|
let tokens = decode_event_raw(event, topics, data)?;
|
2020-10-29 07:48:24 +00:00
|
|
|
Ok(D::from_tokens(tokens)?)
|
|
|
|
}
|
|
|
|
|
2020-12-31 19:08:12 +00:00
|
|
|
/// Helper for ABI encoding arguments for a specific function
|
|
|
|
pub fn encode_function_data<T: Tokenize>(function: &Function, args: T) -> Result<Bytes, AbiError> {
|
2020-10-29 07:48:24 +00:00
|
|
|
let tokens = args.into_tokens();
|
|
|
|
Ok(function.encode_input(&tokens).map(Into::into)?)
|
|
|
|
}
|
|
|
|
|
2020-12-31 19:08:12 +00:00
|
|
|
/// Helper for ABI decoding raw data based on a function's input or output.
|
2022-03-02 13:58:18 +00:00
|
|
|
pub fn decode_function_data_raw<T: AsRef<[u8]>>(
|
2020-10-29 07:48:24 +00:00
|
|
|
function: &Function,
|
2020-11-12 10:08:20 +00:00
|
|
|
bytes: T,
|
2020-10-29 07:48:24 +00:00
|
|
|
is_input: bool,
|
2022-03-02 13:58:18 +00:00
|
|
|
) -> Result<Vec<Token>, AbiError> {
|
2020-11-12 10:08:20 +00:00
|
|
|
let bytes = bytes.as_ref();
|
2022-03-02 13:58:18 +00:00
|
|
|
Ok(if is_input {
|
2020-11-12 10:08:20 +00:00
|
|
|
if bytes.len() < 4 || bytes[..4] != function.selector() {
|
2021-10-29 12:29:35 +00:00
|
|
|
return Err(AbiError::WrongSelector)
|
2020-11-12 10:08:20 +00:00
|
|
|
}
|
|
|
|
function.decode_input(&bytes[4..])?
|
2020-10-29 07:48:24 +00:00
|
|
|
} else {
|
2020-11-12 10:08:20 +00:00
|
|
|
function.decode_output(bytes)?
|
2022-03-02 13:58:18 +00:00
|
|
|
})
|
|
|
|
}
|
2020-10-29 07:48:24 +00:00
|
|
|
|
2022-03-02 13:58:18 +00:00
|
|
|
/// Helper for ABI decoding raw data based on a function's input or output.
|
|
|
|
pub fn decode_function_data<D: Detokenize, T: AsRef<[u8]>>(
|
|
|
|
function: &Function,
|
|
|
|
bytes: T,
|
|
|
|
is_input: bool,
|
|
|
|
) -> Result<D, AbiError> {
|
|
|
|
let tokens = decode_function_data_raw(function, bytes, is_input)?;
|
2020-10-29 07:48:24 +00:00
|
|
|
Ok(D::from_tokens(tokens)?)
|
|
|
|
}
|
|
|
|
|
2020-10-27 10:57:01 +00:00
|
|
|
/// Utility function for creating a mapping between a unique signature and a
|
|
|
|
/// name-index pair for accessing contract ABI items.
|
|
|
|
fn create_mapping<T, S, F>(
|
2021-09-13 17:43:54 +00:00
|
|
|
elements: &BTreeMap<String, Vec<T>>,
|
2020-10-27 10:57:01 +00:00
|
|
|
signature: F,
|
|
|
|
) -> HashMap<S, (String, usize)>
|
|
|
|
where
|
|
|
|
S: Hash + Eq,
|
|
|
|
F: Fn(&T) -> S,
|
|
|
|
{
|
|
|
|
let signature = &signature;
|
|
|
|
elements
|
|
|
|
.iter()
|
|
|
|
.flat_map(|(name, sub_elements)| {
|
|
|
|
sub_elements
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(move |(index, element)| (signature(element), (name.to_owned(), index)))
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2020-10-29 07:48:24 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use ethers_core::{abi::parse_abi, types::U256};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_function_inputs() {
|
|
|
|
let abi = BaseContract::from(parse_abi(&[
|
|
|
|
"function approve(address _spender, uint256 value) external view returns (bool, bool)"
|
|
|
|
]).unwrap());
|
|
|
|
|
2021-10-29 12:29:35 +00:00
|
|
|
let spender = "7a250d5630b4cf539739df2c5dacb4c659f2488d".parse::<Address>().unwrap();
|
2020-10-29 07:48:24 +00:00
|
|
|
let amount = U256::MAX;
|
|
|
|
|
|
|
|
let encoded = abi.encode("approve", (spender, amount)).unwrap();
|
|
|
|
|
2020-12-31 17:19:14 +00:00
|
|
|
assert_eq!(hex::encode(&encoded), "095ea7b30000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
2020-10-29 07:48:24 +00:00
|
|
|
|
|
|
|
let (spender2, amount2): (Address, U256) = abi.decode("approve", encoded).unwrap();
|
|
|
|
assert_eq!(spender, spender2);
|
|
|
|
assert_eq!(amount, amount2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_parse_events() {
|
|
|
|
let abi = BaseContract::from(
|
|
|
|
parse_abi(&[
|
|
|
|
"event Approval(address indexed owner, address indexed spender, uint256 value)",
|
|
|
|
])
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let topics = vec![
|
|
|
|
"8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
|
|
|
|
"000000000000000000000000e4e60fdf9bf188fa57b7a5022230363d5bd56d08",
|
|
|
|
"0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d",
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.map(|hash| hash.parse::<H256>().unwrap())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let data = Bytes::from(
|
2020-12-31 17:19:14 +00:00
|
|
|
hex::decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
2020-10-29 07:48:24 +00:00
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let (owner, spender, value): (Address, Address, U256) =
|
|
|
|
abi.decode_event("Approval", topics, data).unwrap();
|
|
|
|
assert_eq!(value, U256::MAX);
|
2021-10-29 12:29:35 +00:00
|
|
|
assert_eq!(owner, "e4e60fdf9bf188fa57b7a5022230363d5bd56d08".parse::<Address>().unwrap());
|
|
|
|
assert_eq!(spender, "7a250d5630b4cf539739df2c5dacb4c659f2488d".parse::<Address>().unwrap());
|
2020-10-29 07:48:24 +00:00
|
|
|
}
|
|
|
|
}
|