Skip to main content

PriceLevels

Struct PriceLevels 

Source
pub struct PriceLevels { /* private fields */ }
Expand description

Entry point for Titan’s pAMM price levels, structured like crate::PropAmmRouter: a single client wrapping a default PriceLevelsSource you can override.

Implementations§

Source§

impl PriceLevels

Source

pub fn new() -> Self

Client with the default one-shot HTTP snapshot source.

Examples found in repository?
examples/price_levels.rs (line 25)
20async fn main() -> Result<(), Box<dyn std::error::Error>> {
21    // PriceLevels defaults to a one-shot HTTP snapshot source. Set
22    // PRICE_LEVELS_URL to point snapshots and quotes at a specific endpoint.
23    let prices = match std::env::var("PRICE_LEVELS_URL") {
24        Ok(url) => PriceLevels::with_source(Arc::new(PriceLevelsRpcSource::new(url))),
25        Err(_) => PriceLevels::new(),
26    };
27
28    // 1. Full snapshot: every pAMM's order book.
29    let snapshot = prices.get_price_levels().await?;
30    println!(
31        "snapshot @ block {} — {} pAMM(s)",
32        snapshot
33            .block_number
34            .map_or_else(|| "?".to_string(), |b| b.to_string()),
35        snapshot.pamms.len()
36    );
37
38    // Pick the first pAMM/pair that carries an order book to display.
39    let Some((pamm, pair)) = snapshot.pamms.iter().find_map(|entry| {
40        entry
41            .pairs
42            .iter()
43            .find(|pair| !pair.order_book.is_empty())
44            .map(|pair| (entry.pamm, pair))
45    }) else {
46        return Err("snapshot carried no order books".into());
47    };
48
49    println!(
50        "\n{} order book: {} -> {}",
51        venue_name(pamm),
52        token_symbol(pair.token_in),
53        token_symbol(pair.token_out),
54    );
55    for level in &pair.order_book {
56        println!(
57            "  {} -> {}  [{:?}]",
58            fmt(level.amount_in, pair.token_in),
59            fmt(level.amount_out, pair.token_out),
60            level.variant,
61        );
62    }
63
64    // 2. Quote helpers, for that pair and a mid-ladder size. `get_quote`
65    // returns the best across all pAMMs; `get_quote_venue` pins to one.
66    let size = pair.order_book[pair.order_book.len() / 2].amount_in;
67
68    let best = prices
69        .get_quote(pair.token_in, pair.token_out, size)
70        .await?;
71    println!(
72        "\nbest quote:  {} -> {} via {}",
73        fmt(best.amount_in, best.token_in),
74        fmt(best.amount_out, best.token_out),
75        venue_name(best.pamm),
76    );
77
78    let pinned = prices
79        .get_quote_venue(pamm, pair.token_in, pair.token_out, size)
80        .await?;
81    println!(
82        "{} quote: {} -> {}",
83        venue_name(pamm),
84        fmt(pinned.amount_in, pinned.token_in),
85        fmt(pinned.amount_out, pinned.token_out),
86    );
87
88    Ok(())
89}
Source

pub fn with_source(source: Arc<dyn PriceLevelsSource>) -> Self

Client with an explicit snapshot source. When source already speaks HTTP (a PriceLevelsRpcSource), the quote helpers reuse it so a custom endpoint covers both; otherwise quotes go to the default HTTP endpoint.

Examples found in repository?
examples/price_levels.rs (line 24)
20async fn main() -> Result<(), Box<dyn std::error::Error>> {
21    // PriceLevels defaults to a one-shot HTTP snapshot source. Set
22    // PRICE_LEVELS_URL to point snapshots and quotes at a specific endpoint.
23    let prices = match std::env::var("PRICE_LEVELS_URL") {
24        Ok(url) => PriceLevels::with_source(Arc::new(PriceLevelsRpcSource::new(url))),
25        Err(_) => PriceLevels::new(),
26    };
27
28    // 1. Full snapshot: every pAMM's order book.
29    let snapshot = prices.get_price_levels().await?;
30    println!(
31        "snapshot @ block {} — {} pAMM(s)",
32        snapshot
33            .block_number
34            .map_or_else(|| "?".to_string(), |b| b.to_string()),
35        snapshot.pamms.len()
36    );
37
38    // Pick the first pAMM/pair that carries an order book to display.
39    let Some((pamm, pair)) = snapshot.pamms.iter().find_map(|entry| {
40        entry
41            .pairs
42            .iter()
43            .find(|pair| !pair.order_book.is_empty())
44            .map(|pair| (entry.pamm, pair))
45    }) else {
46        return Err("snapshot carried no order books".into());
47    };
48
49    println!(
50        "\n{} order book: {} -> {}",
51        venue_name(pamm),
52        token_symbol(pair.token_in),
53        token_symbol(pair.token_out),
54    );
55    for level in &pair.order_book {
56        println!(
57            "  {} -> {}  [{:?}]",
58            fmt(level.amount_in, pair.token_in),
59            fmt(level.amount_out, pair.token_out),
60            level.variant,
61        );
62    }
63
64    // 2. Quote helpers, for that pair and a mid-ladder size. `get_quote`
65    // returns the best across all pAMMs; `get_quote_venue` pins to one.
66    let size = pair.order_book[pair.order_book.len() / 2].amount_in;
67
68    let best = prices
69        .get_quote(pair.token_in, pair.token_out, size)
70        .await?;
71    println!(
72        "\nbest quote:  {} -> {} via {}",
73        fmt(best.amount_in, best.token_in),
74        fmt(best.amount_out, best.token_out),
75        venue_name(best.pamm),
76    );
77
78    let pinned = prices
79        .get_quote_venue(pamm, pair.token_in, pair.token_out, size)
80        .await?;
81    println!(
82        "{} quote: {} -> {}",
83        venue_name(pamm),
84        fmt(pinned.amount_in, pinned.token_in),
85        fmt(pinned.amount_out, pinned.token_out),
86    );
87
88    Ok(())
89}
Source

pub fn with_source_and_rpc_url( source: Arc<dyn PriceLevelsSource>, rpc_url: impl Into<String>, ) -> Self

Client with an explicit snapshot source and a specific RPC URL for the quote helpers. Use this when pairing a PriceLevelsWsSource with a private or regional deployment so quotes go to the same host as the stream. When source is a PriceLevelsRpcSource, rpc_url is ignored and the source’s own URL is reused.

Source

pub fn source(&self) -> &Arc<dyn PriceLevelsSource>

The snapshot source Self::get_price_levels pulls from.

Source

pub async fn get_price_levels(&self) -> Result<Arc<PriceLevelsSnapshot>>

Latest price-level snapshot from the configured source.

Examples found in repository?
examples/price_levels.rs (line 29)
20async fn main() -> Result<(), Box<dyn std::error::Error>> {
21    // PriceLevels defaults to a one-shot HTTP snapshot source. Set
22    // PRICE_LEVELS_URL to point snapshots and quotes at a specific endpoint.
23    let prices = match std::env::var("PRICE_LEVELS_URL") {
24        Ok(url) => PriceLevels::with_source(Arc::new(PriceLevelsRpcSource::new(url))),
25        Err(_) => PriceLevels::new(),
26    };
27
28    // 1. Full snapshot: every pAMM's order book.
29    let snapshot = prices.get_price_levels().await?;
30    println!(
31        "snapshot @ block {} — {} pAMM(s)",
32        snapshot
33            .block_number
34            .map_or_else(|| "?".to_string(), |b| b.to_string()),
35        snapshot.pamms.len()
36    );
37
38    // Pick the first pAMM/pair that carries an order book to display.
39    let Some((pamm, pair)) = snapshot.pamms.iter().find_map(|entry| {
40        entry
41            .pairs
42            .iter()
43            .find(|pair| !pair.order_book.is_empty())
44            .map(|pair| (entry.pamm, pair))
45    }) else {
46        return Err("snapshot carried no order books".into());
47    };
48
49    println!(
50        "\n{} order book: {} -> {}",
51        venue_name(pamm),
52        token_symbol(pair.token_in),
53        token_symbol(pair.token_out),
54    );
55    for level in &pair.order_book {
56        println!(
57            "  {} -> {}  [{:?}]",
58            fmt(level.amount_in, pair.token_in),
59            fmt(level.amount_out, pair.token_out),
60            level.variant,
61        );
62    }
63
64    // 2. Quote helpers, for that pair and a mid-ladder size. `get_quote`
65    // returns the best across all pAMMs; `get_quote_venue` pins to one.
66    let size = pair.order_book[pair.order_book.len() / 2].amount_in;
67
68    let best = prices
69        .get_quote(pair.token_in, pair.token_out, size)
70        .await?;
71    println!(
72        "\nbest quote:  {} -> {} via {}",
73        fmt(best.amount_in, best.token_in),
74        fmt(best.amount_out, best.token_out),
75        venue_name(best.pamm),
76    );
77
78    let pinned = prices
79        .get_quote_venue(pamm, pair.token_in, pair.token_out, size)
80        .await?;
81    println!(
82        "{} quote: {} -> {}",
83        venue_name(pamm),
84        fmt(pinned.amount_in, pinned.token_in),
85        fmt(pinned.amount_out, pinned.token_out),
86    );
87
88    Ok(())
89}
Source

pub async fn get_quote( &self, token_in: Address, token_out: Address, amount_in: U256, ) -> Result<TitanQuote>

Best quote across all pAMMs for a size (titan_getPammQuote).

Examples found in repository?
examples/price_levels.rs (line 69)
20async fn main() -> Result<(), Box<dyn std::error::Error>> {
21    // PriceLevels defaults to a one-shot HTTP snapshot source. Set
22    // PRICE_LEVELS_URL to point snapshots and quotes at a specific endpoint.
23    let prices = match std::env::var("PRICE_LEVELS_URL") {
24        Ok(url) => PriceLevels::with_source(Arc::new(PriceLevelsRpcSource::new(url))),
25        Err(_) => PriceLevels::new(),
26    };
27
28    // 1. Full snapshot: every pAMM's order book.
29    let snapshot = prices.get_price_levels().await?;
30    println!(
31        "snapshot @ block {} — {} pAMM(s)",
32        snapshot
33            .block_number
34            .map_or_else(|| "?".to_string(), |b| b.to_string()),
35        snapshot.pamms.len()
36    );
37
38    // Pick the first pAMM/pair that carries an order book to display.
39    let Some((pamm, pair)) = snapshot.pamms.iter().find_map(|entry| {
40        entry
41            .pairs
42            .iter()
43            .find(|pair| !pair.order_book.is_empty())
44            .map(|pair| (entry.pamm, pair))
45    }) else {
46        return Err("snapshot carried no order books".into());
47    };
48
49    println!(
50        "\n{} order book: {} -> {}",
51        venue_name(pamm),
52        token_symbol(pair.token_in),
53        token_symbol(pair.token_out),
54    );
55    for level in &pair.order_book {
56        println!(
57            "  {} -> {}  [{:?}]",
58            fmt(level.amount_in, pair.token_in),
59            fmt(level.amount_out, pair.token_out),
60            level.variant,
61        );
62    }
63
64    // 2. Quote helpers, for that pair and a mid-ladder size. `get_quote`
65    // returns the best across all pAMMs; `get_quote_venue` pins to one.
66    let size = pair.order_book[pair.order_book.len() / 2].amount_in;
67
68    let best = prices
69        .get_quote(pair.token_in, pair.token_out, size)
70        .await?;
71    println!(
72        "\nbest quote:  {} -> {} via {}",
73        fmt(best.amount_in, best.token_in),
74        fmt(best.amount_out, best.token_out),
75        venue_name(best.pamm),
76    );
77
78    let pinned = prices
79        .get_quote_venue(pamm, pair.token_in, pair.token_out, size)
80        .await?;
81    println!(
82        "{} quote: {} -> {}",
83        venue_name(pamm),
84        fmt(pinned.amount_in, pinned.token_in),
85        fmt(pinned.amount_out, pinned.token_out),
86    );
87
88    Ok(())
89}
Source

pub async fn get_quote_venue( &self, venue: Address, token_in: Address, token_out: Address, amount_in: U256, ) -> Result<TitanQuote>

Quote from a specific venue for a size (titan_getPammQuoteVenue).

Examples found in repository?
examples/price_levels.rs (line 79)
20async fn main() -> Result<(), Box<dyn std::error::Error>> {
21    // PriceLevels defaults to a one-shot HTTP snapshot source. Set
22    // PRICE_LEVELS_URL to point snapshots and quotes at a specific endpoint.
23    let prices = match std::env::var("PRICE_LEVELS_URL") {
24        Ok(url) => PriceLevels::with_source(Arc::new(PriceLevelsRpcSource::new(url))),
25        Err(_) => PriceLevels::new(),
26    };
27
28    // 1. Full snapshot: every pAMM's order book.
29    let snapshot = prices.get_price_levels().await?;
30    println!(
31        "snapshot @ block {} — {} pAMM(s)",
32        snapshot
33            .block_number
34            .map_or_else(|| "?".to_string(), |b| b.to_string()),
35        snapshot.pamms.len()
36    );
37
38    // Pick the first pAMM/pair that carries an order book to display.
39    let Some((pamm, pair)) = snapshot.pamms.iter().find_map(|entry| {
40        entry
41            .pairs
42            .iter()
43            .find(|pair| !pair.order_book.is_empty())
44            .map(|pair| (entry.pamm, pair))
45    }) else {
46        return Err("snapshot carried no order books".into());
47    };
48
49    println!(
50        "\n{} order book: {} -> {}",
51        venue_name(pamm),
52        token_symbol(pair.token_in),
53        token_symbol(pair.token_out),
54    );
55    for level in &pair.order_book {
56        println!(
57            "  {} -> {}  [{:?}]",
58            fmt(level.amount_in, pair.token_in),
59            fmt(level.amount_out, pair.token_out),
60            level.variant,
61        );
62    }
63
64    // 2. Quote helpers, for that pair and a mid-ladder size. `get_quote`
65    // returns the best across all pAMMs; `get_quote_venue` pins to one.
66    let size = pair.order_book[pair.order_book.len() / 2].amount_in;
67
68    let best = prices
69        .get_quote(pair.token_in, pair.token_out, size)
70        .await?;
71    println!(
72        "\nbest quote:  {} -> {} via {}",
73        fmt(best.amount_in, best.token_in),
74        fmt(best.amount_out, best.token_out),
75        venue_name(best.pamm),
76    );
77
78    let pinned = prices
79        .get_quote_venue(pamm, pair.token_in, pair.token_out, size)
80        .await?;
81    println!(
82        "{} quote: {} -> {}",
83        venue_name(pamm),
84        fmt(pinned.amount_in, pinned.token_in),
85        fmt(pinned.amount_out, pinned.token_out),
86    );
87
88    Ok(())
89}
Source

pub fn close(&self)

Tear down the snapshot source (closes the stream socket, if any). The HTTP quote path holds no connection, so there is nothing else to release.

Trait Implementations§

Source§

impl Default for PriceLevels

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

Source§

fn exact_from(value: T) -> U

Source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

Source§

fn exact_into(self) -> U

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

Source§

fn wrapping_into(self) -> U