From 2cae55d2bcd8e2cacdd87d3c190782651d864280 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 13 Apr 2024 07:38:46 +0200 Subject: [PATCH] feat: add rpc-types-anvil --- Cargo.toml | 1 + crates/rpc-types-anvil/Cargo.toml | 21 +++++ crates/rpc-types-anvil/README.md | 3 + crates/rpc-types-anvil/src/lib.rs | 127 ++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 crates/rpc-types-anvil/Cargo.toml create mode 100644 crates/rpc-types-anvil/README.md create mode 100644 crates/rpc-types-anvil/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 772ef75478c3..27fb18dee0d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ alloy-provider = { version = "0.1.0", default-features = false, path = "crates/p alloy-pubsub = { version = "0.1.0", default-features = false, path = "crates/pubsub" } alloy-rpc-client = { version = "0.1.0", default-features = false, path = "crates/rpc-client" } alloy-rpc-types-engine = { version = "0.1.0", default-features = false, path = "crates/rpc-types-engine" } +alloy-rpc-types-anvil = { version = "0.1.0", default-features = false, path = "crates/rpc-types-anvil" } alloy-rpc-types-trace = { version = "0.1.0", default-features = false, path = "crates/rpc-types-trace" } alloy-rpc-types = { version = "0.1.0", default-features = false, path = "crates/rpc-types" } alloy-serde = { version = "0.1.0", default-features = false, path = "crates/serde" } diff --git a/crates/rpc-types-anvil/Cargo.toml b/crates/rpc-types-anvil/Cargo.toml new file mode 100644 index 000000000000..2bfd48f9dcd7 --- /dev/null +++ b/crates/rpc-types-anvil/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "alloy-rpc-types-anvil" +description = "Anvil RPC types" + +version.workspace = true +edition.workspace = true +rust-version.workspace = true +authors.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +exclude.workspace = true + +[dependencies] +alloy-primitives = { workspace = true, features = ["serde"] } +alloy-serde.workspace = true + +serde.workspace = true + +[dev-dependencies] +serde_json.workspace = true diff --git a/crates/rpc-types-anvil/README.md b/crates/rpc-types-anvil/README.md new file mode 100644 index 000000000000..6ed5056087b8 --- /dev/null +++ b/crates/rpc-types-anvil/README.md @@ -0,0 +1,3 @@ +# alloy-rpc-types-anvil + +RPC types for Anvil,Hardhat,Ganache dev nodes. diff --git a/crates/rpc-types-anvil/src/lib.rs b/crates/rpc-types-anvil/src/lib.rs new file mode 100644 index 000000000000..6d2bc4047b15 --- /dev/null +++ b/crates/rpc-types-anvil/src/lib.rs @@ -0,0 +1,127 @@ +#![doc = include_str!("../README.md")] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg", + html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico" +)] +#![warn( + missing_copy_implementations, + missing_debug_implementations, + unreachable_pub, + clippy::missing_const_for_fn, + rustdoc::all +)] +#![allow(missing_docs)] +#![cfg_attr(not(test), warn(unused_crate_dependencies))] +#![deny(unused_must_use, rust_2018_idioms)] +#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] + +use alloy_primitives::{TxHash, B256, U256}; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; + +/// Represents the params to set forking which can take various forms +/// - untagged +/// - tagged `forking` +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct Forking { + pub json_rpc_url: Option, + pub block_number: Option, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NodeInfo { + #[serde(with = "alloy_serde::u64_hex")] + pub current_block_number: u64, + pub current_block_timestamp: u64, + pub current_block_hash: B256, + pub hard_fork: String, + pub transaction_order: String, + pub environment: NodeEnvironment, + pub fork_config: NodeForkConfig, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NodeEnvironment { + pub base_fee: U256, + pub chain_id: u64, + pub gas_limit: U256, + pub gas_price: U256, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NodeForkConfig { + pub fork_url: Option, + pub fork_block_number: Option, + pub fork_retry_backoff: Option, +} + +/// Anvil equivalent of `hardhat_metadata`. +/// Metadata about the current Anvil instance. +/// See +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Metadata { + pub client_version: String, + pub chain_id: u64, + pub instance_id: B256, + pub latest_block_number: u64, + pub latest_block_hash: B256, + pub forked_network: Option, + pub snapshots: BTreeMap, +} + +/// Information about the forked network. +/// See +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ForkedNetwork { + pub chain_id: u64, + pub fork_block_number: u64, + pub fork_block_hash: TxHash, +} + +/// Additional `evm_mine` options +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum MineOptions { + Options { + #[serde(with = "alloy_serde::u64_hex_or_decimal_opt")] + timestamp: Option, + // If `blocks` is given, it will mine exactly blocks number of blocks, regardless of any + // other blocks mined or reverted during it's operation + blocks: Option, + }, + /// The timestamp the block should be mined with + #[serde(with = "alloy_serde::u64_hex_or_decimal_opt")] + Timestamp(Option), +} + +impl Default for MineOptions { + fn default() -> Self { + MineOptions::Options { timestamp: None, blocks: None } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn serde_forking() { + let s = r#"{"forking": {"jsonRpcUrl": "https://ethereumpublicnode.com", + "blockNumber": "18441649" + } + }"#; + let f: Forking = serde_json::from_str(s).unwrap(); + assert_eq!( + f, + Forking { + json_rpc_url: Some("https://ethereumpublicnode.com".into()), + block_number: Some(18441649) + } + ); + } +}