Skip to main content

eth_prices/
lib.rs

1/*!
2`eth-prices` is a pricing library & routing engine for EVM assets.
3
4This crate currently exposes protocol-specific quoters that can read a rate at a
5specific block height.
6
7# Overview
8
9Here is a simple example showing off some of the features of `eth-prices`:
10```rust
11use eth_prices::{quoter::Quoter, router::Router, asset::AssetIdentifier, network::NetworkTime, quoter::uniswap_v3::{UniswapV3Quoter, factory::UniswapV3Selector}};
12use alloy::primitives::{address, U256};
13use alloy::providers::{ProviderBuilder, Provider};
14
15#[tokio::main]
16pub async fn main() {
17    let provider = ProviderBuilder::new().connect("https://ethereum-rpc.publicnode.com").await.unwrap().erased();
18
19    let selector = UniswapV3Selector::Pool { pool_address: address!("0x99ac8ca7087fa4a2a1fb6357269965a2014abc35") };
20    let quoter = UniswapV3Quoter::from_selector(&provider, selector).await.unwrap();
21    let router = Router::from_iter(vec![quoter.into()]);
22
23    let token_in = AssetIdentifier::try_from("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap();
24    let token_out = AssetIdentifier::try_from("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
25    let route = router.compute(&token_in, &token_out).unwrap();
26
27    let network = NetworkTime::from_provider_latest(provider, 1.into()).await.unwrap().instant();
28    let amount = U256::from(1_000_000);
29    let quote = route.quote(&network, amount).await.unwrap();
30
31    println!("quote: {:?}", quote);
32}
33```
34
35Today, the main building blocks are:
36- [`quoter::Quoter`] for single-hop quote sources.
37- [`router::Router`] for routing between assets.
38- [`asset::AssetIdentifier`] for identifying ERC-20, fiat, and native assets.
39- [`asset::Asset`] for asset metadata and amount formatting helpers.
40- [`network::NetworkInstant`] for managing network times and providers.
41
42# Quoters
43
44Currently supported quoters include:
45- [`quoter::fixed`] for static conversion rates.
46- [`quoter::uniswap_v2`] for Uniswap v2 pairs.
47- [`quoter::uniswap_v3`] for Uniswap v3 pools.
48- [`quoter::erc4626`] for ERC-4626 vaults.
49- [`quoter::ecb`] for European Central Bank (ECB) rates.
50
51# Features
52
53- `ecb` - Enable European Central Bank (ECB) quoters.
54- `time` - Enable system time integration.
55
56# Routing
57
58Use the [`router::Router`] struct to compute a route and quote the rate.
59A router is essentially a graph of quoters, where can be routed from any point to any other point.
60
61The [`router::AutoRouter`] can be used to automatically discover quoters and build a router.
62It attempts to prioritize quoters based on liquidity and other factors, for best performance manually configuring quoters for a router is recommended.
63
64# Examples
65
66You can find more examples in the [examples](https://github.com/v3xlabs/eth-prices/tree/master/examples) directory.
67*/
68
69pub mod error;
70pub use error::{EthPricesError, Result};
71
72pub mod asset;
73pub mod config;
74pub mod network;
75pub mod provider;
76pub mod quoter;
77pub mod router;
78
79#[cfg(target_arch = "wasm32")]
80pub mod js;
81
82#[cfg(test)]
83pub mod tests;