Skip to content

Commit

Permalink
docs(provider): add examples to raw_request{,dyn}
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Apr 9, 2024
1 parent cd5a2c7 commit 71e0950
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
39 changes: 39 additions & 0 deletions crates/provider/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,24 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
/* ---------------------------------------- raw calls --------------------------------------- */

/// Sends a raw JSON-RPC request.
///
/// # Examples
///
/// ```no_run
/// # async fn example(provider: impl alloy_provider::Provider) -> Result<(), Box<dyn std::error::Error>> {
/// use alloy_rpc_types::BlockNumberOrTag;
///
/// // No parameters: `()`
/// let block_number = provider.raw_request("eth_blockNumber".into(), ()).await?;
///
/// // One param: `(param,)` or `[param]`
/// let block = provider.raw_request("eth_getBlockByNumber".into(), (BlockNumberOrTag::Latest,)).await?;
///
/// // Two or more parameters: `(param1, param2, ...)` or `[param1, param2, ...]`
/// let full_block = provider.raw_request("eth_getBlockByNumber".into(), (BlockNumberOrTag::Latest, true)).await?;
/// # Ok(())
/// # }
/// ```
async fn raw_request<P, R>(&self, method: Cow<'static, str>, params: P) -> TransportResult<R>
where
P: RpcParam,
Expand All @@ -1038,6 +1056,27 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
}

/// Sends a raw JSON-RPC request with type-erased parameters and return.
///
/// # Examples
///
/// ```no_run
/// # async fn example(provider: impl alloy_provider::Provider) -> Result<(), Box<dyn std::error::Error>> {
/// use alloy_rpc_types::BlockNumberOrTag;
///
/// // No parameters: `()`
/// let params = serde_json::value::to_raw_value(&())?;
/// let block_number = provider.raw_request_dyn("eth_blockNumber".into(), &params).await?;
///
/// // One param: `(param,)` or `[param]`
/// let params = serde_json::value::to_raw_value(&(BlockNumberOrTag::Latest,))?;
/// let block = provider.raw_request_dyn("eth_getBlockByNumber".into(), &params).await?;
///
/// // Two or more parameters: `(param1, param2, ...)` or `[param1, param2, ...]`
/// let params = serde_json::value::to_raw_value(&(BlockNumberOrTag::Latest, true))?;
/// let full_block = provider.raw_request_dyn("eth_getBlockByNumber".into(), &params).await?;
/// # Ok(())
/// # }
/// ```
async fn raw_request_dyn(
&self,
method: Cow<'static, str>,
Expand Down
2 changes: 2 additions & 0 deletions crates/rpc-types/src/eth/log.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unknown_lints, non_local_definitions)]

use alloy_primitives::{LogData, B256};
use serde::{Deserialize, Serialize};

Expand Down
2 changes: 2 additions & 0 deletions crates/rpc-types/src/eth/transaction/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unknown_lints, non_local_definitions)]

use crate::{Log, WithOtherFields};
use alloy_consensus::{AnyReceiptEnvelope, ReceiptEnvelope, TxType};
use alloy_primitives::{Address, B256};
Expand Down

0 comments on commit 71e0950

Please sign in to comment.