Skip to main content

InstrumentCache

Struct InstrumentCache 

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

Thread-safe instrument cache with multiple lookup indices.

Shared between HTTP client, WebSocket client, and execution client via Arc. Provides O(1) lookups by InstrumentId, market ticker, or clob_pair_id.

Implementations§

Source§

impl InstrumentCache

Source

pub fn new() -> Self

Creates a new empty instrument cache.

Source

pub fn insert(&self, instrument: InstrumentAny, market: PerpetualMarket)

Inserts an instrument with its market data.

This populates the primary storage and all lookup indices.

Source

pub fn insert_many(&self, items: Vec<(InstrumentAny, PerpetualMarket)>)

Bulk inserts instruments with their market data.

Marks the cache as initialized after insertion.

Source

pub fn clear(&self)

Clears all cached data.

Useful for refreshing instruments from the API.

Source

pub fn insert_instrument_only(&self, instrument: InstrumentAny)

Inserts an instrument without market data (InstrumentId lookup only).

Use this for caching instruments when market params are not available. Note: get_by_clob_id() and get_by_market() won’t work for instruments inserted this way - only get() by InstrumentId will work.

Source

pub fn insert_instruments_only(&self, instruments: Vec<InstrumentAny>)

Bulk inserts instruments without market data.

Marks the cache as initialized after insertion.

Source

pub fn get(&self, instrument_id: &InstrumentId) -> Option<InstrumentAny>

Gets an instrument by InstrumentId.

Source

pub fn get_by_market(&self, ticker: &str) -> Option<InstrumentAny>

Gets an instrument by market ticker (e.g., “BTC-USD”).

This is the identifier used in public WebSocket channels.

Source

pub fn get_by_clob_id(&self, clob_pair_id: u32) -> Option<InstrumentAny>

Gets an instrument by clob_pair_id (e.g., 0, 1, 2).

This is the identifier used in blockchain transactions and order messages.

Source

pub fn get_id_by_clob_id(&self, clob_pair_id: u32) -> Option<InstrumentId>

Gets an InstrumentId by clob_pair_id.

Returns directly from index without cloning full instrument.

Source

pub fn get_id_by_market(&self, ticker: &str) -> Option<InstrumentId>

Gets an InstrumentId by market ticker.

Returns directly from index without cloning full instrument.

Source

pub fn get_market_params( &self, instrument_id: &InstrumentId, ) -> Option<PerpetualMarket>

Gets full market parameters by InstrumentId.

Returns the complete PerpetualMarket data including margin requirements, quantization parameters, and current oracle price.

Source

pub fn get_order_market_params( &self, instrument_id: &InstrumentId, ) -> Option<OrderMarketParams>

Gets order market parameters for order building.

Returns the subset of market data needed for constructing orders (quantization, clob_pair_id, etc.).

Source

pub fn update_oracle_price(&self, ticker: &str, oracle_price: Decimal)

Updates oracle price for a market.

Called when receiving price updates via WebSocket v4_markets channel.

Source

pub fn is_initialized(&self) -> bool

Returns whether the cache has been initialized with instrument data.

Source

pub fn len(&self) -> usize

Returns the number of cached instruments.

Source

pub fn is_empty(&self) -> bool

Returns whether the cache is empty.

Source

pub fn all_instruments(&self) -> Vec<InstrumentAny>

Returns all cached instruments.

Useful for WebSocket handler initialization and instrument replay.

Source

pub fn all_instrument_ids(&self) -> Vec<InstrumentId>

Returns all InstrumentIds.

Source

pub fn contains(&self, instrument_id: &InstrumentId) -> bool

Checks if an instrument exists by InstrumentId.

Source

pub fn contains_clob_id(&self, clob_pair_id: u32) -> bool

Checks if an instrument exists by clob_pair_id.

Source

pub fn contains_market(&self, ticker: &str) -> bool

Checks if an instrument exists by market ticker (e.g., “BTC-USD”).

Source

pub fn to_instrument_id_map(&self) -> HashMap<InstrumentId, InstrumentAny>

Returns a HashMap of all instruments keyed by InstrumentId.

This is useful for parsing functions that expect HashMap<InstrumentId, InstrumentAny>. Note: Creates a snapshot copy, so frequent calls should be avoided.

Source

pub fn to_oracle_prices_map(&self) -> HashMap<InstrumentId, Decimal>

Returns a HashMap of oracle prices keyed by InstrumentId.

This is useful for parsing functions like parse_account_state that need oracle prices. Note: Creates a snapshot copy, so frequent calls should be avoided.

Source

pub fn log_missing_clob_pair_id(&self, clob_pair_id: u32)

Logs a warning about a missing instrument for a clob_pair_id, listing known mappings.

Trait Implementations§

Source§

impl Debug for InstrumentCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for InstrumentCache

Source§

fn default() -> InstrumentCache

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> 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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: 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> Same for T

Source§

type Output = T

Should always be Self
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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> Ungil for T
where T: Send,