Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ alloy-sol-macro = "0.8.15"
alloy-sol-types = "0.8.15"
alloy = { version = "0.8.1", features = [
"json-rpc",
"rpc-client",
"rpc-types",
] }
anyhow = "1.0"
Expand Down
67 changes: 65 additions & 2 deletions src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use alloy::rpc::types::{
Block, BlockId, BlockNumberOrTag, FeeHistory, Filter, FilterBlockOption, Log, Transaction,
TransactionReceipt,
};
pub use alloy::transports::Authorization as AlloyAuthorization;
pub use alloy_primitives::{Address, BlockHash, BlockNumber, Bytes, TxHash, U128, U256, U64, U8};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -220,23 +221,85 @@ pub struct ProviderConfig {
}

#[derive(Clone, Debug, Deserialize, Serialize, Hash, Eq, PartialEq)]
pub enum Authorization {
Basic(String),
Bearer(String),
Raw(String),
}

impl From<Authorization> for AlloyAuthorization {
fn from(auth: Authorization) -> AlloyAuthorization {
match auth {
Authorization::Basic(value) => AlloyAuthorization::Basic(value),
Authorization::Bearer(value) => AlloyAuthorization::Bearer(value),
Authorization::Raw(value) => AlloyAuthorization::Raw(value),
}
}
}

#[derive(Clone, Debug, Serialize, Hash, Eq, PartialEq)]
pub enum NodeOrRpcUrl {
Node {
kns_update: crate::net::KnsUpdate,
use_as_provider: bool, // false for just-routers inside saved config
},
RpcUrl(String),
RpcUrl {
url: String,
auth: Option<Authorization>,
},
}

impl std::cmp::PartialEq<str> for NodeOrRpcUrl {
fn eq(&self, other: &str) -> bool {
match self {
NodeOrRpcUrl::Node { kns_update, .. } => kns_update.name == other,
NodeOrRpcUrl::RpcUrl(url) => url == other,
NodeOrRpcUrl::RpcUrl { url, .. } => url == other,
}
}
}

impl<'de> Deserialize<'de> for NodeOrRpcUrl {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum RpcUrlHelper {
String(String),
Struct {
url: String,
auth: Option<Authorization>,
},
}

#[derive(Deserialize)]
enum Helper {
Node {
kns_update: crate::net::KnsUpdate,
use_as_provider: bool,
},
RpcUrl(RpcUrlHelper),
}

let helper = Helper::deserialize(deserializer)?;

Ok(match helper {
Helper::Node {
kns_update,
use_as_provider,
} => NodeOrRpcUrl::Node {
kns_update,
use_as_provider,
},
Helper::RpcUrl(url_helper) => match url_helper {
RpcUrlHelper::String(url) => NodeOrRpcUrl::RpcUrl { url, auth: None },
RpcUrlHelper::Struct { url, auth } => NodeOrRpcUrl::RpcUrl { url, auth },
},
})
}
}

/// An EVM chain provider. Create this object to start making RPC calls.
/// Set the chain_id to determine which chain to call: requests will fail
/// unless the node this process is running on has access to a provider
Expand Down