Skip to content

Commit

Permalink
Merge branch 'main' into dani/signers
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Dec 7, 2023
2 parents 05d1978 + 4033f21 commit 1551501
Show file tree
Hide file tree
Showing 13 changed files with 332 additions and 130 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@ jobs:
cache-on-failure: true
- run: cargo doc --workspace --all-features --no-deps --document-private-items
env:
RUSTDOCFLAGS: "--cfg docsrs -D warnings"
RUSTDOCFLAGS:
--cfg docsrs -D warnings --show-type-layout --generate-link-to-definition
--enable-index-page -Zunstable-options
- name: Deploy documentation
uses: peaceiris/actions-gh-pages@v3
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: target/doc
force_orphan: true

fmt:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ alloy-transport = { version = "0.1.0", path = "crates/transport" }
alloy-transport-http = { version = "0.1.0", path = "crates/transport-http" }
alloy-transport-ws = { version = "0.1.0", path = "crates/transport-ws" }

alloy-primitives = { version = "0.5.0", default-features = false, features = ["std"] }
alloy-sol-types = { version = "0.5.0", default-features = false, features = ["std"] }
alloy-primitives = { version = "0.5.1", default-features = false, features = ["std"] }
alloy-sol-types = { version = "0.5.1", default-features = false, features = ["std"] }
alloy-rlp = "0.3"

# crypto
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ feature-parity in Alloy. No action is currently needed from devs.
This repository contains the following crates:

- [`alloy-json-rpc`] - Core data types for JSON-RPC 2.0 clients.
- [`alloy-transports`] - Transport implementations for JSON-RPC 2.0 clients.
- [`alloy-transport`] - Transport implementations for JSON-RPC 2.0 clients.
- [`alloy-networks`] - Network abstraction for RPC types. Allows capturing
different RPC param and response types on a per-network basis.
- [`alloy-providers`] - A client trait for interacting with Ethereum-like RPC
endpoints. Abstract over `alloy_networks::Network`, which allows capturing
different RPC types on a per-network basis.

[`alloy-json-rpc`]: ./crates/json-rpc
[`alloy-transports`]: ./crates/transports
[`alloy-transport`]: ./crates/transport
[`alloy-networks`]: ./crates/networks
[`alloy-providers`]: ./crates/providers

Expand Down
18 changes: 7 additions & 11 deletions crates/json-rpc/src/packet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{Id, Response, SerializedRequest};
use serde::{
de::{self, Deserializer, MapAccess, SeqAccess, Visitor},
ser::SerializeSeq,
Deserialize, Serialize,
};
use serde_json::value::RawValue;
Expand Down Expand Up @@ -35,14 +34,8 @@ impl Serialize for RequestPacket {
S: serde::Serializer,
{
match self {
RequestPacket::Single(single) => single.serialized().serialize(serializer),
RequestPacket::Batch(batch) => {
let mut seq = serializer.serialize_seq(Some(batch.len()))?;
for req in batch {
seq.serialize_element(req.serialized())?;
}
seq.end()
}
RequestPacket::Single(single) => single.serialize(serializer),
RequestPacket::Batch(batch) => batch.serialize(serializer),
}
}
}
Expand All @@ -54,8 +47,11 @@ impl RequestPacket {
}

/// Serialize the packet as a boxed [`RawValue`].
pub fn serialize(&self) -> serde_json::Result<Box<RawValue>> {
serde_json::to_string(self).and_then(RawValue::from_string)
pub fn serialize(self) -> serde_json::Result<Box<RawValue>> {
match self {
RequestPacket::Single(single) => Ok(single.take_request()),
RequestPacket::Batch(batch) => serde_json::value::to_raw_value(&batch),
}
}

/// Get the request IDs of all subscription requests in the packet.
Expand Down
18 changes: 12 additions & 6 deletions crates/json-rpc/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@ where
///
/// If serialization of the params fails.
pub fn box_params(self) -> PartiallySerializedRequest {
Request {
meta: self.meta,
params: RawValue::from_string(serde_json::to_string(&self.params).unwrap()).unwrap(),
}
Request { meta: self.meta, params: serde_json::value::to_raw_value(&self.params).unwrap() }
}

/// Serialize the request, including the request parameters.
pub fn serialize(self) -> serde_json::Result<SerializedRequest> {
let request = serde_json::to_string(&self)?;
Ok(SerializedRequest { meta: self.meta, request: RawValue::from_string(request)? })
let request = serde_json::value::to_raw_value(&self)?;
Ok(SerializedRequest { meta: self.meta, request })
}
}

Expand Down Expand Up @@ -206,3 +203,12 @@ impl SerializedRequest {
}
}
}

impl Serialize for SerializedRequest {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.request.serialize(serializer)
}
}
109 changes: 25 additions & 84 deletions crates/providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
pub async fn get_transaction_count(
&self,
address: Address,
) -> TransportResult<alloy_primitives::U256>
where
Self: Sync,
{
) -> TransportResult<alloy_primitives::U256> {
self.inner
.prepare(
"eth_getTransactionCount",
Expand All @@ -53,18 +50,16 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
}

/// Gets the last block number available.
pub async fn get_block_number(&self) -> TransportResult<U64>
where
Self: Sync,
{
pub async fn get_block_number(&self) -> TransportResult<U64> {
self.inner.prepare("eth_blockNumber", Cow::<()>::Owned(())).await
}

/// Gets the balance of the account at the specified tag, which defaults to latest.
pub async fn get_balance(&self, address: Address, tag: Option<BlockId>) -> TransportResult<U256>
where
Self: Sync,
{
pub async fn get_balance(
&self,
address: Address,
tag: Option<BlockId>,
) -> TransportResult<U256> {
self.inner
.prepare(
"eth_getBalance",
Expand All @@ -81,10 +76,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
&self,
hash: BlockHash,
full: bool,
) -> TransportResult<Option<Block>>
where
Self: Sync,
{
) -> TransportResult<Option<Block>> {
self.inner
.prepare("eth_getBlockByHash", Cow::<(BlockHash, bool)>::Owned((hash, full)))
.await
Expand All @@ -95,10 +87,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
&self,
number: B,
full: bool,
) -> TransportResult<Option<Block>>
where
Self: Sync,
{
) -> TransportResult<Option<Block>> {
self.inner
.prepare(
"eth_getBlockByNumber",
Expand All @@ -108,31 +97,22 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
}

/// Gets the chain ID.
pub async fn get_chain_id(&self) -> TransportResult<U64>
where
Self: Sync,
{
pub async fn get_chain_id(&self) -> TransportResult<U64> {
self.inner.prepare("eth_chainId", Cow::<()>::Owned(())).await
}
/// Gets the bytecode located at the corresponding [Address].
pub async fn get_code_at<B: Into<BlockId> + Send + Sync>(
&self,
address: Address,
tag: B,
) -> TransportResult<Bytes>
where
Self: Sync,
{
) -> TransportResult<Bytes> {
self.inner
.prepare("eth_getCode", Cow::<(Address, BlockId)>::Owned((address, tag.into())))
.await
}

/// Gets a [Transaction] by its [TxHash].
pub async fn get_transaction_by_hash(&self, hash: TxHash) -> TransportResult<Transaction>
where
Self: Sync,
{
pub async fn get_transaction_by_hash(&self, hash: TxHash) -> TransportResult<Transaction> {
self.inner
.prepare(
"eth_getTransactionByHash",
Expand All @@ -144,38 +124,26 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
}

/// Retrieves a [`Vec<Log>`] with the given [Filter].
pub async fn get_logs(&self, filter: Filter) -> TransportResult<Vec<Log>>
where
Self: Sync,
{
pub async fn get_logs(&self, filter: Filter) -> TransportResult<Vec<Log>> {
self.inner.prepare("eth_getLogs", Cow::<Vec<Filter>>::Owned(vec![filter])).await
}

/// Gets the accounts in the remote node. This is usually empty unless you're using a local
/// node.
pub async fn get_accounts(&self) -> TransportResult<Vec<Address>>
where
Self: Sync,
{
pub async fn get_accounts(&self) -> TransportResult<Vec<Address>> {
self.inner.prepare("eth_accounts", Cow::<()>::Owned(())).await
}

/// Gets the current gas price.
pub async fn get_gas_price(&self) -> TransportResult<U256>
where
Self: Sync,
{
pub async fn get_gas_price(&self) -> TransportResult<U256> {
self.inner.prepare("eth_gasPrice", Cow::<()>::Owned(())).await
}

/// Gets a [TransactionReceipt] if it exists, by its [TxHash].
pub async fn get_transaction_receipt(
&self,
hash: TxHash,
) -> TransportResult<Option<TransactionReceipt>>
where
Self: Sync,
{
) -> TransportResult<Option<TransactionReceipt>> {
self.inner.prepare("eth_getTransactionReceipt", Cow::<Vec<TxHash>>::Owned(vec![hash])).await
}

Expand All @@ -186,10 +154,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
block_count: U256,
last_block: B,
reward_percentiles: &[f64],
) -> TransportResult<FeeHistory>
where
Self: Sync,
{
) -> TransportResult<FeeHistory> {
self.inner
.prepare(
"eth_feeHistory",
Expand All @@ -206,10 +171,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
pub async fn get_block_receipts(
&self,
block: BlockNumberOrTag,
) -> TransportResult<Vec<TransactionReceipt>>
where
Self: Sync,
{
) -> TransportResult<Vec<TransactionReceipt>> {
self.inner.prepare("eth_getBlockReceipts", Cow::<BlockNumberOrTag>::Owned(block)).await
}

Expand All @@ -218,10 +180,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
&self,
tag: B,
idx: U64,
) -> TransportResult<Option<Block>>
where
Self: Sync,
{
) -> TransportResult<Option<Block>> {
let tag = tag.into();
match tag {
BlockId::Hash(hash) => {
Expand All @@ -244,10 +203,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
}

/// Gets syncing info.
pub async fn syncing(&self) -> TransportResult<SyncStatus>
where
Self: Sync,
{
pub async fn syncing(&self) -> TransportResult<SyncStatus> {
self.inner.prepare("eth_syncing", Cow::<()>::Owned(())).await
}

Expand All @@ -256,10 +212,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
&self,
tx: TransactionRequest,
block: Option<BlockId>,
) -> TransportResult<Bytes>
where
Self: Sync,
{
) -> TransportResult<Bytes> {
self.inner
.prepare(
"eth_call",
Expand All @@ -276,10 +229,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
&self,
tx: TransactionRequest,
block: Option<BlockId>,
) -> TransportResult<Bytes>
where
Self: Sync,
{
) -> TransportResult<Bytes> {
if let Some(block_id) = block {
let params = Cow::<(TransactionRequest, BlockId)>::Owned((tx, block_id));
self.inner.prepare("eth_estimateGas", params).await
Expand All @@ -290,10 +240,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
}

/// Sends an already-signed transaction.
pub async fn send_raw_transaction(&self, tx: Bytes) -> TransportResult<TxHash>
where
Self: Sync,
{
pub async fn send_raw_transaction(&self, tx: Bytes) -> TransportResult<TxHash> {
self.inner.prepare("eth_sendRawTransaction", Cow::<Bytes>::Owned(tx)).await
}

Expand All @@ -303,10 +250,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
pub async fn estimate_eip1559_fees(
&self,
estimator: Option<EstimatorFunction>,
) -> TransportResult<(U256, U256)>
where
Self: Sync,
{
) -> TransportResult<(U256, U256)> {
let base_fee_per_gas = match self.get_block_by_number(BlockNumberOrTag::Latest, false).await
{
Ok(Some(block)) => match block.header.base_fee_per_gas {
Expand Down Expand Up @@ -345,10 +289,7 @@ impl<T: Transport + Clone + Send + Sync> Provider<T> {
}

#[cfg(feature = "anvil")]
pub async fn set_code(&self, address: Address, code: &'static str) -> TransportResult<()>
where
Self: Sync,
{
pub async fn set_code(&self, address: Address, code: &'static str) -> TransportResult<()> {
self.inner
.prepare("anvil_setCode", Cow::<(Address, &'static str)>::Owned((address, code)))
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-types/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ impl FilteredParams {
}

/// Returns `true` if the bloom matches the topics
pub fn matches_topics(bloom: Bloom, topic_filters: &Vec<BloomFilter>) -> bool {
pub fn matches_topics(bloom: Bloom, topic_filters: &[BloomFilter]) -> bool {
if topic_filters.is_empty() {
return true;
}
Expand Down
Loading

0 comments on commit 1551501

Please sign in to comment.