This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move BaseContract to own file
- Loading branch information
Showing
6 changed files
with
85 additions
and
68 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::Contract; | ||
|
||
use ethers_core::{ | ||
abi::{Abi, FunctionExt}, | ||
types::{Address, Selector}, | ||
}; | ||
use ethers_providers::Middleware; | ||
|
||
use std::{collections::HashMap, fmt::Debug, hash::Hash, sync::Arc}; | ||
|
||
/// 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. | ||
// Adapted from: https://github.com/gnosis/ethcontract-rs/blob/master/src/contract.rs | ||
pub(crate) methods: HashMap<Selector, (String, usize)>, | ||
} | ||
|
||
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 { | ||
/// 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. | ||
pub fn into_contract<M: Middleware>( | ||
self, | ||
address: Address, | ||
client: impl Into<Arc<M>>, | ||
) -> Contract<M> { | ||
Contract::new(address, self, client) | ||
} | ||
} | ||
|
||
impl AsRef<Abi> for BaseContract { | ||
fn as_ref(&self) -> &Abi { | ||
self.abi() | ||
} | ||
} | ||
|
||
/// 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>( | ||
elements: &HashMap<String, Vec<T>>, | ||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters