Skip to main content

BlockchainHttpRpcClient

Struct BlockchainHttpRpcClient 

Source
pub struct BlockchainHttpRpcClient { /* private fields */ }
Expand description

Client for making HTTP-based RPC requests to blockchain nodes.

This client is designed to interact with Ethereum-compatible blockchain networks, providing methods to execute RPC calls and handle responses in a type-safe manner.

Implementations§

Source§

impl BlockchainHttpRpcClient

Source

pub fn new( http_rpc_url: String, rpc_request_per_second: Option<u32>, proxy_url: Option<String>, ) -> Self

Creates a new HTTP RPC client with the given endpoint URL and optional rate limit.

If rpc_request_per_second is Some(0) or an invalid value, rate limiting is disabled.

§Panics

Panics if the internal HTTP client cannot be created.

Source

pub async fn execute_rpc_call<T: DeserializeOwned>( &self, rpc_request: Value, ) -> Result<T>

Executes an Ethereum JSON-RPC call and deserializes the response into the specified type T.

§Errors

Returns an error if the HTTP RPC request fails or the response cannot be parsed.

Source

pub async fn execute_rpc_call_with_timeout<T: DeserializeOwned>( &self, rpc_request: Value, timeout_secs: Option<u64>, ) -> Result<T>

Executes an Ethereum JSON-RPC call with an optional per-request timeout and deserializes the response into the specified type T.

§Errors

Returns an error if the HTTP RPC request fails or the response cannot be parsed.

Source

pub fn construct_eth_call( &self, to: &str, call_data: &[u8], block: Option<u64>, ) -> Value

Creates a properly formatted eth_call JSON-RPC request object targeting a specific contract address with encoded function data.

Source

pub async fn get_balance( &self, address: &Address, block: Option<u64>, ) -> Result<U256>

Retrieves the balance of the specified Ethereum address at the given block.

§Errors

Returns an error if the RPC call fails or if the returned balance string cannot be parsed as a valid U256.

Source

pub async fn get_balance_with_timeout( &self, address: &Address, block: Option<u64>, timeout_secs: Option<u64>, ) -> Result<U256>

Retrieves the balance of the specified Ethereum address at the given block with an optional per-request timeout.

§Errors

Returns an error if the RPC call fails or if the returned balance string cannot be parsed as a valid U256.

Source

pub async fn get_logs( &self, address: Option<&Address>, topics: Option<Vec<Option<String>>>, from_block: u64, to_block: u64, ) -> Result<Vec<RpcLog>>

Retrieves logs matching the given filter criteria.

This method calls the eth_getLogs RPC method to fetch event logs from the blockchain. It’s commonly used for querying historical events like token transfers, swaps, etc.

§Errors

Returns an error if the RPC call fails or the response cannot be parsed.

Source

pub async fn chain_id(&self) -> Result<u64>

Returns the chain ID reported by the RPC node via eth_chainId.

§Errors

Returns an error if the RPC call fails or the result is missing or malformed.

Source

pub async fn get_code(&self, address: &Address) -> Result<Bytes>

Returns the deployed bytecode at the given address via eth_getCode at the latest block.

An address with no deployed contract returns empty bytes.

§Errors

Returns an error if the RPC call fails or the result is missing or malformed.

Source

pub async fn get_transaction_count_pending( &self, address: &Address, ) -> Result<u64>

Returns the next nonce for the given address via eth_getTransactionCount with the pending tag, making the pending pool state authoritative.

§Errors

Returns an error if the RPC call fails or the result is missing or malformed.

Source

pub async fn get_transaction_count_latest( &self, address: &Address, ) -> Result<u64>

Returns the latest mined nonce for an address.

§Errors

Returns an error if the RPC call fails or the result is missing or malformed.

Source

pub async fn estimate_gas( &self, from: &Address, to: &Address, value: U256, data: &[u8], ) -> Result<u64>

Estimates the gas required for a transaction via eth_estimateGas.

§Errors

Returns an error if the RPC call fails (including node-side revert of the simulated transaction) or the result is missing or malformed.

Source

pub async fn max_priority_fee_per_gas(&self) -> Result<u128>

Returns the node’s suggested max priority fee per gas in wei via eth_maxPriorityFeePerGas.

§Errors

Returns an error if the RPC call fails or the result is missing or malformed.

Source

pub async fn latest_block(&self) -> Result<RpcBlock>

Returns the latest block via eth_getBlockByNumber with the latest tag.

§Errors

Returns an error if the RPC call fails or the result is missing or malformed.

Source

pub async fn finalized_block(&self) -> Result<RpcBlock>

Returns the consensus finalized block.

§Errors

Returns an error if the endpoint does not support the finalized tag, the RPC call fails, or the result is missing or malformed.

Source

pub async fn block_by_number( &self, number: u64, full_transactions: bool, ) -> Result<RpcBlock>

Returns a numbered canonical block, optionally with full transactions.

§Errors

Returns an error if the RPC call fails or the result is missing or malformed.

Source

pub async fn get_transaction_receipt( &self, tx_hash: &B256, ) -> Result<Option<RpcTransactionReceipt>>

Returns the receipt for the given transaction hash via eth_getTransactionReceipt.

A null result maps to Ok(None): the transaction is pending and no receipt exists yet.

§Errors

Returns an error if the RPC call fails or the response is malformed.

Source

pub async fn send_raw_transaction( &self, raw_tx: &[u8], expected_tx_hash: &B256, ) -> Result<B256, BroadcastError>

Broadcasts a signed raw transaction via eth_sendRawTransaction.

Broadcast failures classify before retry: an already known response is acceptance (returns expected_tx_hash), and a timeout after sending is BroadcastError::TimeoutAfterSend so the caller reconciles through the persisted record rather than rebroadcasting.

§Errors

Returns a BroadcastError classifying the failure. Errors are sanitized: they carry the numeric RPC code only, never the endpoint URL or the signed payload.

Trait Implementations§

Source§

impl Debug for BlockchainHttpRpcClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Ungil for T
where T: Send,

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more