#![allow(clippy::expect_used)]
#![allow(clippy::enum_variant_names)]
use crate::common::{Address, Amount};
use crate::merkle_batch_payment::PoolCommitment;
use crate::utils::get_evm_network;
use alloy::primitives::address;
use alloy::transports::http::reqwest;
use serde::{Deserialize, Serialize};
use serde_with::{DisplayFromStr, serde_as};
use std::str::FromStr;
use std::sync::LazyLock;
#[macro_use]
extern crate tracing;
pub mod common;
pub mod contract;
pub mod cryptography;
pub mod data_payments;
#[cfg(feature = "external-signer")]
pub mod external_signer;
pub mod merkle_batch_payment;
pub mod merkle_payments;
pub mod quoting_metrics;
mod retry;
pub mod testnet;
pub mod transaction_config;
pub mod utils;
pub mod wallet;
pub use retry::GasInfo;
pub use common::Address as RewardsAddress;
pub use data_payments::{EncodedPeerId, PaymentQuote, ProofOfPayment};
const TX_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(24);
static PUBLIC_ARBITRUM_ONE_HTTP_RPC_URL: LazyLock<reqwest::Url> = LazyLock::new(|| {
"https://arb1.arbitrum.io/rpc"
.parse()
.expect("Invalid RPC URL")
});
static PUBLIC_ARBITRUM_SEPOLIA_HTTP_RPC_URL: LazyLock<reqwest::Url> = LazyLock::new(|| {
"https://sepolia-rollup.arbitrum.io/rpc"
.parse()
.expect("Invalid RPC URL")
});
const ARBITRUM_ONE_PAYMENT_TOKEN_ADDRESS: Address =
address!("a78d8321B20c4Ef90eCd72f2588AA985A4BDb684");
const ARBITRUM_SEPOLIA_TEST_PAYMENT_TOKEN_ADDRESS: Address =
address!("4bc1aCE0E66170375462cB4E6Af42Ad4D5EC689C");
const ARBITRUM_ONE_PAYMENT_VAULT_ADDRESS: Address =
address!("9A3EcAc693b699Fc0B2B6A50B5549e50c2320A26");
const ARBITRUM_SEPOLIA_TEST_PAYMENT_VAULT_ADDRESS: Address =
address!("d742E8CFEf27A9a884F3EFfA239Ee2F39c276522");
#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CustomNetwork {
#[serde_as(as = "DisplayFromStr")]
pub rpc_url_http: reqwest::Url,
pub payment_token_address: Address,
pub payment_vault_address: Address,
}
impl CustomNetwork {
pub fn new(rpc_url: &str, payment_token_addr: &str, payment_vault_addr: &str) -> Self {
Self {
rpc_url_http: reqwest::Url::parse(rpc_url).expect("Invalid RPC URL"),
payment_token_address: Address::from_str(payment_token_addr)
.expect("Invalid payment token address"),
payment_vault_address: Address::from_str(payment_vault_addr)
.expect("Invalid payment vault address"),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub enum Network {
#[default]
ArbitrumOne,
ArbitrumSepoliaTest,
Custom(CustomNetwork),
}
impl std::fmt::Display for Network {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Network::ArbitrumOne => write!(f, "evm-arbitrum-one"),
Network::ArbitrumSepoliaTest => write!(f, "evm-arbitrum-sepolia-test"),
Network::Custom(_) => write!(f, "evm-custom"),
}
}
}
impl std::str::FromStr for Network {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"evm-arbitrum-one" => Ok(Network::ArbitrumOne),
"evm-arbitrum-sepolia-test" => Ok(Network::ArbitrumSepoliaTest),
_ => Err(()),
}
}
}
impl Network {
pub fn new(local: bool) -> Result<Self, utils::Error> {
get_evm_network(local, None).inspect_err(|err| {
warn!("Failed to select EVM network from ENV: {err}");
})
}
pub fn new_custom(rpc_url: &str, payment_token_addr: &str, payment_vault_addr: &str) -> Self {
Self::Custom(CustomNetwork::new(
rpc_url,
payment_token_addr,
payment_vault_addr,
))
}
pub fn identifier(&self) -> &str {
match self {
Network::ArbitrumOne => "arbitrum-one",
Network::ArbitrumSepoliaTest => "arbitrum-sepolia-test",
Network::Custom(_) => "custom",
}
}
pub fn rpc_url(&self) -> &reqwest::Url {
match self {
Network::ArbitrumOne => &PUBLIC_ARBITRUM_ONE_HTTP_RPC_URL,
Network::ArbitrumSepoliaTest => &PUBLIC_ARBITRUM_SEPOLIA_HTTP_RPC_URL,
Network::Custom(custom) => &custom.rpc_url_http,
}
}
pub fn payment_token_address(&self) -> &Address {
match self {
Network::ArbitrumOne => &ARBITRUM_ONE_PAYMENT_TOKEN_ADDRESS,
Network::ArbitrumSepoliaTest => &ARBITRUM_SEPOLIA_TEST_PAYMENT_TOKEN_ADDRESS,
Network::Custom(custom) => &custom.payment_token_address,
}
}
pub fn payment_vault_address(&self) -> &Address {
match self {
Network::ArbitrumOne => &ARBITRUM_ONE_PAYMENT_VAULT_ADDRESS,
Network::ArbitrumSepoliaTest => &ARBITRUM_SEPOLIA_TEST_PAYMENT_VAULT_ADDRESS,
Network::Custom(custom) => &custom.payment_vault_address,
}
}
pub fn estimate_merkle_payment_cost(
&self,
depth: u8,
pool_commitments: &[PoolCommitment],
) -> Amount {
if pool_commitments.is_empty() {
return Amount::ZERO;
}
let multiplier = Amount::from(1u64 << depth);
pool_commitments
.iter()
.map(|pool| {
let mut prices: Vec<Amount> = pool.candidates.iter().map(|c| c.price).collect();
prices.sort_unstable(); prices[prices.len() / 2] * multiplier
})
.max()
.unwrap_or(Amount::ZERO)
}
}