Skip to content

Commit

Permalink
feat(chains): add polygon amoy and use alloy-rs (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfourzerofour committed Feb 6, 2024
1 parent cc4e3e6 commit 529ac84
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 38 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/alchemyplatform/rundler"

[workspace.dependencies]
alloy-chains = "0.1.10"
anyhow = "1.0.70"
async-trait = "0.1.73"
cargo-husky = { version = "1", default-features = false, features = ["user-hooks"] }
Expand Down
1 change: 1 addition & 0 deletions bin/rundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rundler-types = { path = "../../crates/types" }
rundler-utils = { path = "../../crates/utils" }

# CLI dependencies
alloy-chains.workspace = true
anyhow.workspace = true
clap = { version = "4.4.4", features = ["derive", "env"] }
dotenv = "0.15.0"
Expand Down
9 changes: 5 additions & 4 deletions bin/rundler/src/cli/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

use std::{collections::HashMap, net::SocketAddr, time::Duration};

use alloy_chains::NamedChain;
use anyhow::Context;
use clap::Args;
use ethers::types::{Chain, H256};
use ethers::types::H256;
use rundler_pool::{LocalPoolBuilder, PoolConfig, PoolTask, PoolTaskArgs};
use rundler_sim::MempoolConfig;
use rundler_task::spawn_tasks_with_shutdown;
Expand Down Expand Up @@ -194,9 +195,9 @@ const LARGE_HISTORY_SIZE: u64 = 128;
// Mainnets that are known to not have large reorgs can use the small history
// size. Use the large history size for all testnets because I don't trust them.
const SMALL_HISTORY_CHAIN_IDS: &[u64] = &[
Chain::Mainnet as u64,
Chain::Arbitrum as u64,
Chain::Optimism as u64,
NamedChain::Mainnet as u64,
NamedChain::Arbitrum as u64,
NamedChain::Optimism as u64,
];

fn default_chain_history_size(chain_id: u64) -> u64 {
Expand Down
1 change: 1 addition & 0 deletions crates/builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rundler-task = { path = "../task" }
rundler-types = { path = "../types" }
rundler-utils = { path = "../utils" }

alloy-chains.workspace = true
anyhow.workspace = true
async-trait.workspace = true
enum_dispatch = "0.3.11"
Expand Down
8 changes: 4 additions & 4 deletions crates/builder/src/sender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod flashbots;
mod raw;
use std::{str::FromStr, sync::Arc, time::Duration};

use alloy_chains::NamedChain;
use anyhow::{bail, Context, Error};
use async_trait::async_trait;
pub(crate) use bloxroute::PolygonBloxrouteTransactionSender;
Expand All @@ -26,8 +27,7 @@ use ethers::{
prelude::SignerMiddleware,
providers::{JsonRpcClient, Middleware, Provider, ProviderError},
types::{
transaction::eip2718::TypedTransaction, Address, Bytes, Chain, TransactionReceipt, H256,
U256,
transaction::eip2718::TypedTransaction, Address, Bytes, TransactionReceipt, H256, U256,
},
};
use ethers_signers::Signer;
Expand Down Expand Up @@ -150,7 +150,7 @@ impl TransactionSenderType {
ConditionalTransactionSender::new(client, signer),
),
Self::Flashbots => {
if chain_id != Chain::Mainnet as u64 {
if chain_id != NamedChain::Mainnet as u64 {
return Err(SenderConstructorErrors::InvalidChainForSender(
chain_id,
self.into_snake_case(),
Expand All @@ -160,7 +160,7 @@ impl TransactionSenderType {
}
Self::PolygonBloxroute => {
if let Some(header) = bloxroute_header {
if chain_id == Chain::Polygon as u64 {
if chain_id == NamedChain::Polygon as u64 {
return Err(SenderConstructorErrors::InvalidChainForSender(
chain_id,
self.into_snake_case(),
Expand Down
1 change: 1 addition & 0 deletions crates/sim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rundler-provider = { path = "../provider" }
rundler-types = { path = "../types" }
rundler-utils = { path = "../utils" }

alloy-chains.workspace = true
anyhow.workspace = true
arrayvec = "0.7.2"
async-trait.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions crates/sim/src/estimation/estimation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ fn estimation_proxy_bytecode_with_target(target: Address) -> Bytes {

#[cfg(test)]
mod tests {
use alloy_chains::NamedChain;
use ethers::{
abi::{AbiEncode, Address},
providers::JsonRpcError,
types::Chain,
utils::hex,
};
use rundler_provider::{MockEntryPoint, MockProvider, ProviderError};
Expand Down Expand Up @@ -555,7 +555,7 @@ mod tests {
// Chose arbitrum
let provider = Arc::new(provider);
let estimator: GasEstimatorImpl<MockProvider, MockEntryPoint> = GasEstimatorImpl::new(
Chain::Arbitrum as u64,
NamedChain::Arbitrum as u64,
provider.clone(),
entry,
settings,
Expand Down Expand Up @@ -607,7 +607,7 @@ mod tests {
// Chose OP
let provider = Arc::new(provider);
let estimator: GasEstimatorImpl<MockProvider, MockEntryPoint> = GasEstimatorImpl::new(
Chain::Optimism as u64,
NamedChain::Optimism as u64,
provider.clone(),
entry,
settings,
Expand Down
13 changes: 8 additions & 5 deletions crates/sim/src/gas/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@

use std::{cmp, fmt::Debug, sync::Arc};

use alloy_chains::NamedChain;
use anyhow::Context;
use ethers::{
abi::AbiEncode,
types::{Address, Chain, U256},
types::{Address, U256},
};
use rundler_provider::Provider;
use rundler_types::{
chain::{ARBITRUM_CHAIN_IDS, OP_BEDROCK_CHAIN_IDS, POLYGON_CHAIN_IDS},
chain::{
ARBITRUM_CHAIN_IDS, OP_BEDROCK_CHAIN_IDS, POLYGON_CHAIN_IDS, POLYGON_TESTNET_CHAIN_IDS,
},
GasFees, UserOperation,
};
use rundler_utils::math;
Expand Down Expand Up @@ -405,9 +408,9 @@ pub const OPTIMISM_BEDROCK_MAX_PRIORITY_FEE_MIN: u64 = 100_000;
/// Returns the minimum max priority fee per gas for the given chain id.
pub fn get_min_max_priority_fee_per_gas(chain_id: u64) -> U256 {
match chain_id {
x if x == Chain::Mainnet as u64 => ETHEREUM_MAINNET_MAX_PRIORITY_FEE_MIN.into(),
x if x == Chain::Polygon as u64 => POLYGON_MAINNET_MAX_PRIORITY_FEE_MIN.into(),
x if x == Chain::PolygonMumbai as u64 => POLYGON_MUMBAI_MAX_PRIORITY_FEE_MIN.into(),
x if x == NamedChain::Mainnet as u64 => ETHEREUM_MAINNET_MAX_PRIORITY_FEE_MIN.into(),
x if x == NamedChain::Polygon as u64 => POLYGON_MAINNET_MAX_PRIORITY_FEE_MIN.into(),
x if POLYGON_TESTNET_CHAIN_IDS.contains(&x) => POLYGON_MUMBAI_MAX_PRIORITY_FEE_MIN.into(),
x if OP_BEDROCK_CHAIN_IDS.contains(&x) => OPTIMISM_BEDROCK_MAX_PRIORITY_FEE_MIN.into(),
_ => U256::zero(),
}
Expand Down
15 changes: 8 additions & 7 deletions crates/sim/src/precheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ pub enum PrecheckViolation {
mod tests {
use std::str::FromStr;

use ethers::types::{Bytes, Chain};
use alloy_chains::NamedChain;
use ethers::types::Bytes;
use rundler_provider::{MockEntryPoint, MockProvider};

use super::*;
Expand Down Expand Up @@ -572,7 +573,7 @@ mod tests {
#[tokio::test]
async fn test_check_fees() {
let settings = Settings {
chain_id: Chain::Optimism as u64,
chain_id: NamedChain::Optimism as u64,
base_fee_accept_percent: 80,
priority_fee_mode: gas::PriorityFeeMode::PriorityFeeIncreasePercent(0),
..Default::default()
Expand All @@ -584,7 +585,7 @@ mod tests {
async_data.base_fee = 5_000.into();
async_data.min_pre_verification_gas = 1_000.into();

let mintip = get_min_max_priority_fee_per_gas(Chain::Optimism as u64);
let mintip = get_min_max_priority_fee_per_gas(NamedChain::Optimism as u64);
let op = UserOperation {
max_fee_per_gas: U256::from(math::percent(5000, settings.base_fee_accept_percent))
+ mintip,
Expand Down Expand Up @@ -638,7 +639,7 @@ mod tests {
#[tokio::test]
async fn test_check_fees_min() {
let settings = Settings {
chain_id: Chain::Optimism as u64,
chain_id: NamedChain::Optimism as u64,
base_fee_accept_percent: 100,
priority_fee_mode: gas::PriorityFeeMode::PriorityFeeIncreasePercent(0),
..Default::default()
Expand All @@ -650,7 +651,7 @@ mod tests {
async_data.base_fee = 5_000.into();
async_data.min_pre_verification_gas = 1_000.into();

let mintip = get_min_max_priority_fee_per_gas(Chain::Optimism as u64);
let mintip = get_min_max_priority_fee_per_gas(NamedChain::Optimism as u64);
let undertip = mintip - U256::from(1);

let op = UserOperation {
Expand All @@ -664,8 +665,8 @@ mod tests {
let res = prechecker.check_gas(&op, async_data);
let mut expected = ArrayVec::<PrecheckViolation, 6>::new();
expected.push(PrecheckViolation::MaxPriorityFeePerGasTooLow(
get_min_max_priority_fee_per_gas(Chain::Optimism as u64) - U256::from(1),
get_min_max_priority_fee_per_gas(Chain::Optimism as u64),
get_min_max_priority_fee_per_gas(NamedChain::Optimism as u64) - U256::from(1),
get_min_max_priority_fee_per_gas(NamedChain::Optimism as u64),
));

assert_eq!(res, expected);
Expand Down
2 changes: 2 additions & 0 deletions crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ repository.workspace = true
[dependencies]
rundler-utils = { path = "../utils" }

alloy-chains.workspace = true
anyhow.workspace = true
chrono = "0.4.24"
constcat = "0.4.1"
ethers.workspace = true
parse-display = "0.8.0"
serde.workspace = true
Expand Down
41 changes: 26 additions & 15 deletions crates/types/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,48 @@

//! Grouped/Labeled chain IDs for various networks

use ethers::types::Chain;
use alloy_chains::NamedChain;
use constcat::concat_slices;

/// Known chain IDs that use the Optimism Bedrock stack
pub const OP_BEDROCK_CHAIN_IDS: &[u64] = &[
Chain::Optimism as u64,
Chain::OptimismGoerli as u64,
11155420, // OptimismSepolia
Chain::Base as u64,
Chain::BaseGoerli as u64,
84532, // BaseSepolia
NamedChain::Optimism as u64,
NamedChain::OptimismGoerli as u64,
NamedChain::OptimismSepolia as u64,
NamedChain::Base as u64,
NamedChain::BaseGoerli as u64,
NamedChain::BaseSepolia as u64,
];

// TODO use chain from ethers types once my PR is merged into ethers
// https://github.com/gakonst/ethers-rs/pull/2657
/// Known chain IDs for the Base ecosystem
pub const ARBITRUM_CHAIN_IDS: &[u64] = &[
Chain::Arbitrum as u64,
Chain::ArbitrumGoerli as u64,
421614, /* ArbitrumSepolia */
Chain::ArbitrumNova as u64,
NamedChain::Arbitrum as u64,
NamedChain::ArbitrumGoerli as u64,
NamedChain::ArbitrumSepolia as u64,
NamedChain::ArbitrumNova as u64,
];

/// Known chain IDs for the Base ecosystem
pub const BASE_CHAIN_IDS: &[u64] = &[
Chain::Base as u64,
Chain::BaseGoerli as u64,
84532, /* BaseSepolia */
NamedChain::Base as u64,
NamedChain::BaseGoerli as u64,
NamedChain::BaseSepolia as u64,
];

/// Known chain IDs for the Polygon ecosystem
pub const POLYGON_CHAIN_IDS: &[u64] = &[Chain::Polygon as u64, Chain::PolygonMumbai as u64];
pub const POLYGON_CHAIN_IDS: &[u64] =
concat_slices!([u64]: POLYGON_TESTNET_CHAIN_IDS, POLYGON_MAINNET_CHAIN_IDS);

/// Known chain IDs for the Polygon ecosystem
pub const POLYGON_TESTNET_CHAIN_IDS: &[u64] = &[
NamedChain::PolygonMumbai as u64,
80002, // PolygonAmoy - Change to named chain once there is a new release on alloy-rs/chains
];

/// Known chain IDs for the Polygon ecosystem
pub const POLYGON_MAINNET_CHAIN_IDS: &[u64] = &[NamedChain::Polygon as u64];

/// Return true if the chain ID has a dynamic preVerificationGas field
pub fn is_dynamic_pvg(chain_id: u64) -> bool {
Expand Down

0 comments on commit 529ac84

Please sign in to comment.