Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: alloy-dyn-contract #149

Merged
merged 18 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: clippy
  • Loading branch information
onbjerg committed Jan 24, 2024
commit 5f551bd1291650a9c5088f028894a5374a98174d
20 changes: 11 additions & 9 deletions crates/alloy-dyn-contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ pub struct CallBuilder<P> {

impl<P> CallBuilder<P> {
pub(crate) fn new(provider: P, function: Function, input: Bytes) -> Self {
let mut request = CallRequest::default();
request.input = CallInput { input: Some(input), ..Default::default() };
let request = CallRequest {
input: CallInput { input: Some(input), ..Default::default() },
..Default::default()
};

Self { request, function, provider, block: None, state: None }
}

/// Sets the `from` field in the transaction to the provided value
pub fn from(mut self, from: Address) -> Self {
pub const fn from(mut self, from: Address) -> Self {
self.request.from = Some(from);
self
}
Expand All @@ -48,15 +50,15 @@ impl<P> CallBuilder<P> {
}

/// Sets the `gas` field in the transaction to the provided value
pub fn gas(mut self, gas: U256) -> Self {
pub const fn gas(mut self, gas: U256) -> Self {
self.request.gas = Some(gas);
self
}

/// Sets the `gas_price` field in the transaction to the provided value
/// If the internal transaction is an EIP-1559 one, then it sets both
/// `max_fee_per_gas` and `max_priority_fee_per_gas` to the same value
pub fn gas_price(mut self, gas_price: U256) -> Self {
pub const fn gas_price(mut self, gas_price: U256) -> Self {
// todo: differentiate between eip-1559 and legacy once we have typedtx
self.request.gas_price = Some(gas_price);
self.request.max_fee_per_gas = Some(gas_price);
Expand All @@ -65,19 +67,19 @@ impl<P> CallBuilder<P> {
}

/// Sets the `value` field in the transaction to the provided value
pub fn value(mut self, value: U256) -> Self {
pub const fn value(mut self, value: U256) -> Self {
self.request.value = Some(value);
self
}

/// Sets the `block` field for sending the tx to the chain
pub fn block(mut self, block: BlockId) -> Self {
pub const fn block(mut self, block: BlockId) -> Self {
self.block = Some(block);
self
}

/// Sets the `nonce` field in the transaction to the provided value
pub fn nonce(mut self, nonce: U64) -> Self {
pub const fn nonce(mut self, nonce: U64) -> Self {
self.request.nonce = Some(nonce);
self
}
Expand Down Expand Up @@ -145,7 +147,7 @@ where
CallBuilder {
request: self.request.clone(),
function: self.function.clone(),
block: self.block.clone(),
block: self.block,
state: self.state.clone(),
provider: self.provider.clone(),
}
Expand Down
10 changes: 5 additions & 5 deletions crates/alloy-dyn-contract/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ pub struct ContractInstance<P> {

impl<P> ContractInstance<P> {
/// Creates a new contract from the provided address, provider, and interface.
pub fn new(address: Address, provider: P, interface: Interface) -> Self {
pub const fn new(address: Address, provider: P, interface: Interface) -> Self {
Self { address, provider, interface }
}

/// Returns the contract's address.
pub fn address(&self) -> Address {
pub const fn address(&self) -> Address {
self.address
}

/// Returns a reference to the contract's ABI.
pub fn abi(&self) -> &JsonAbi {
pub const fn abi(&self) -> &JsonAbi {
self.interface.abi()
}

/// Returns a reference to the contract's provider.
pub fn provider_ref(&self) -> &P {
pub const fn provider_ref(&self) -> &P {
&self.provider
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ where
{
fn clone(&self) -> Self {
ContractInstance {
address: self.address.clone(),
address: self.address,
provider: self.provider.clone(),
interface: self.interface.clone(),
}
Expand Down
9 changes: 4 additions & 5 deletions crates/alloy-dyn-contract/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Interface {
}

/// Returns a reference to the contract's ABI
pub fn abi(&self) -> &JsonAbi {
pub const fn abi(&self) -> &JsonAbi {
&self.abi
}

Expand All @@ -110,15 +110,14 @@ impl Interface {
}

pub(crate) fn get_from_selector(&self, selector: &Selector) -> Result<&Function> {
Ok(self
.functions
self.functions
.get(selector)
.map(|(name, index)| &self.abi.functions[name][*index])
.ok_or_else(|| Error::UnknownSelector(selector.clone()))?)
.ok_or_else(|| Error::UnknownSelector(*selector))
}

/// Create a [`ContractInstance`] from this ABI for a contract at the given address.
pub fn connect<P>(self, address: Address, provider: P) -> ContractInstance<P> {
pub const fn connect<P>(self, address: Address, provider: P) -> ContractInstance<P> {
ContractInstance::new(address, provider, self)
}
}
Loading