Skip to main content

gmsol_sdk/market/
caluclator.rs

1use gmsol_model::price::{Price, Prices};
2use gmsol_programs::{gmsol_store::types::MarketMeta, model::MarketModel};
3use solana_sdk::pubkey::Pubkey;
4
5/// Performs Market calculations.
6pub trait MarketCalculator {
7    /// Returns [`Prices`] corresponding to the given [`MarketMeta`].
8    fn get_token_prices_for_market_meta(&self, meta: &MarketMeta) -> Option<Prices<u128>> {
9        let index_token_price = self.get_token_price(&meta.index_token_mint)?;
10        let long_token_price = self.get_token_price(&meta.long_token_mint)?;
11        let short_token_price = self.get_token_price(&meta.short_token_mint)?;
12        Some(Prices {
13            index_token_price,
14            long_token_price,
15            short_token_price,
16        })
17    }
18
19    /// Returns [`MarketModel`] and [`Prices`] corresponding to the given market token.
20    fn get_market_model_with_prices(
21        &self,
22        market_token: &Pubkey,
23    ) -> crate::Result<(&MarketModel, Prices<u128>)> {
24        let market = self
25            .get_market_model(market_token)
26            .ok_or_else(|| crate::Error::NotFound)?;
27        let meta = &market.meta;
28        let prices = self
29            .get_token_prices_for_market_meta(meta)
30            .ok_or_else(|| crate::Error::NotFound)?;
31        Ok((market, prices))
32    }
33
34    /// Returns [`MarketModel`] corresponding to the given market token.
35    fn get_market_model(&self, market_token: &Pubkey) -> Option<&MarketModel>;
36
37    /// Returns [`Price`] corresponding to the given token address.
38    fn get_token_price(&self, token: &Pubkey) -> Option<Price<u128>>;
39}