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
refactor: split call and call_with_overrides
  • Loading branch information
onbjerg committed Jan 25, 2024
commit 0dac5168d9c5ba31e4bed9a6d0b33a7773931c5f
13 changes: 8 additions & 5 deletions crates/alloy-dyn-contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ where
///
/// This function _does not_ send a transaction from your account
pub async fn call(&self) -> Result<Vec<DynSolValue>> {
let bytes = self
.provider
.call(self.request.clone(), self.block, self.state.clone())
.await
.map_err(Error::from)?;
let bytes = if let Some(state) = &self.state {
self.provider
.call_with_overrides(self.request.clone(), self.block, state.clone())
.await
.map_err(Error::from)?
} else {
self.provider.call(self.request.clone(), self.block).await.map_err(Error::from)?
};

// decode output
let data = self.function.abi_decode_output(bytes.as_ref(), true)?;
Expand Down
25 changes: 13 additions & 12 deletions crates/providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,18 @@ pub trait TempProvider: Send + Sync {
async fn syncing(&self) -> TransportResult<SyncStatus>;

/// Execute a smart contract call with [CallRequest] without publishing a transaction.
async fn call(&self, tx: CallRequest, block: Option<BlockId>) -> TransportResult<Bytes>;

/// Execute a smart contract call with [CallRequest] and state overrides, without publishing a transaction.
///
/// # Note
///
/// Not all client implementations support state overrides.
async fn call(
async fn call_with_overrides(
&self,
tx: CallRequest,
block: Option<BlockId>,
state: Option<StateOverride>,
state: StateOverride,
) -> TransportResult<Bytes>;

/// Estimate the gas needed for a transaction.
Expand Down Expand Up @@ -368,24 +371,22 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
}

/// Execute a smart contract call with [CallRequest] without publishing a transaction.
async fn call(&self, tx: CallRequest, block: Option<BlockId>) -> TransportResult<Bytes> {
self.inner.prepare("eth_call", (tx, block.unwrap_or_default())).await
}

/// Execute a smart contract call with [CallRequest] and state overrides, without publishing a transaction.
///
/// # Note
///
/// Not all client implementations support state overrides.
async fn call(
async fn call_with_overrides(
&self,
tx: CallRequest,
block: Option<BlockId>,
state: Option<StateOverride>,
state: StateOverride,
) -> TransportResult<Bytes> {
// todo: if we just set `None` for the 3rd parameter, will this be ok for clients who do not
// expect a third parameter? if not, should we split this into `call` and
// `call_with_overrides`?
if let Some(state) = state {
self.inner.prepare("eth_call", (tx, block.unwrap_or_default(), state)).await
} else {
self.inner.prepare("eth_call", (tx, block.unwrap_or_default())).await
}
self.inner.prepare("eth_call", (tx, block.unwrap_or_default(), state)).await
}

/// Estimate the gas needed for a transaction.
Expand Down