pub struct BaseExchange {
pub config: ExchangeConfig,
pub http_client: HttpClient,
pub market_cache: Arc<RwLock<MarketCache>>,
pub market_loading_lock: Arc<Mutex<()>>,
pub capabilities: ExchangeCapabilities,
pub urls: HashMap<String, String>,
pub timeframes: HashMap<String, String>,
pub precision_mode: PrecisionMode,
}Expand description
Base exchange implementation
Fields§
§config: ExchangeConfigExchange configuration
http_client: HttpClientHTTP client for API requests (handles rate limiting internally)
market_cache: Arc<RwLock<MarketCache>>Thread-safe market data cache
market_loading_lock: Arc<Mutex<()>>Mutex to serialize market loading operations and prevent concurrent API calls
capabilities: ExchangeCapabilitiesExchange capability flags
urls: HashMap<String, String>API endpoint URLs
timeframes: HashMap<String, String>Timeframe mappings (e.g., “1m” -> “1”)
precision_mode: PrecisionModePrecision mode for price/amount formatting
Implementations§
Source§impl BaseExchange
impl BaseExchange
Sourcepub fn new(config: ExchangeConfig) -> Result<BaseExchange, Error>
pub fn new(config: ExchangeConfig) -> Result<BaseExchange, Error>
Sourcepub async fn load_markets(
&self,
reload: bool,
) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
pub async fn load_markets( &self, reload: bool, ) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
Loads market data from the exchange
§Arguments
reload- Whether to force reload even if markets are already cached
§Returns
Returns a HashMap of markets indexed by symbol.
§Errors
Returns an error if market data cannot be fetched. This base implementation
always returns NotImplemented error as exchanges must override this method.
Sourcepub async fn load_markets_with_loader<F, Fut>(
&self,
reload: bool,
loader: F,
) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
pub async fn load_markets_with_loader<F, Fut>( &self, reload: bool, loader: F, ) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
Loads market data with a custom loader function, ensuring thread-safe concurrent access.
This method serializes market loading operations to prevent multiple concurrent API calls
when multiple tasks call load_markets simultaneously. Only the first caller will
execute the loader; subsequent callers will wait for the lock and then return cached data.
§Arguments
reload- Whether to force reload even if markets are already cachedloader- An async closure that performs the actual market data fetching. Should returnResult<()>and is responsible for callingset_markets.
§Returns
Returns a HashMap of markets indexed by symbol.
§Errors
Returns an error if the loader function fails.
§Example
let markets = self.base().load_markets_with_loader(reload, || async {
let markets = self.fetch_markets_from_api().await?;
Ok((markets, None))
}).await?;Sourcepub async fn set_markets(
&self,
markets: Vec<Market>,
currencies: Option<Vec<Currency>>,
) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
pub async fn set_markets( &self, markets: Vec<Market>, currencies: Option<Vec<Currency>>, ) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
Sets market and currency data in the cache
§Arguments
markets- Vector of market definitions to cachecurrencies- Optional vector of currency definitions to cache
§Returns
Returns Ok(markets) on successful cache update, preserving ownership for the caller.
§Errors
This method should not fail under normal circumstances.
Sourcepub fn throttle(&self) -> Result<(), Error>
👎Deprecated since 0.2.0: Rate limiting is now handled internally by HttpClient. This method is a no-op.
pub fn throttle(&self) -> Result<(), Error>
Applies rate limiting if enabled
§Deprecated
Rate limiting is now fully managed by HttpClient internally. This method is a no-op and will be removed in a future version.
Sourcepub fn check_required_credentials(&self) -> Result<(), Error>
pub fn check_required_credentials(&self) -> Result<(), Error>
Sourcepub fn nonce(&self) -> i64
pub fn nonce(&self) -> i64
Gets a nonce value (current timestamp in milliseconds)
§Returns
Returns current Unix timestamp in milliseconds.
Sourcepub fn handle_http_error(&self, status_code: u16, response: &str) -> Error
pub fn handle_http_error(&self, status_code: u16, response: &str) -> Error
Sourcepub fn parse_ticker(
&self,
ticker_data: &Value,
market: Option<&Market>,
) -> Result<Ticker, Error>
pub fn parse_ticker( &self, ticker_data: &Value, market: Option<&Market>, ) -> Result<Ticker, Error>
Sourcepub fn parse_trade(
&self,
trade_data: &Value,
market: Option<&Market>,
) -> Result<Trade, Error>
pub fn parse_trade( &self, trade_data: &Value, market: Option<&Market>, ) -> Result<Trade, Error>
Sourcepub fn parse_order(
&self,
order_data: &Value,
market: Option<&Market>,
) -> Result<Order, Error>
pub fn parse_order( &self, order_data: &Value, market: Option<&Market>, ) -> Result<Order, Error>
Sourcepub fn parse_order_book(
&self,
orderbook_data: &Value,
timestamp: Option<i64>,
) -> Result<OrderBook, Error>
pub fn parse_order_book( &self, orderbook_data: &Value, timestamp: Option<i64>, ) -> Result<OrderBook, Error>
Sourcepub async fn calculate_fee(
&self,
symbol: &str,
_order_type: OrderType,
_side: OrderSide,
amount: Decimal,
price: Decimal,
taker_or_maker: Option<&str>,
) -> Result<Fee, Error>
pub async fn calculate_fee( &self, symbol: &str, _order_type: OrderType, _side: OrderSide, amount: Decimal, price: Decimal, taker_or_maker: Option<&str>, ) -> Result<Fee, Error>
Calculates trading fee for a given order
§Arguments
symbol- Trading pair symbolorder_type- Order type (limit, market, etc.)side- Order side (buy or sell)amount- Trade amount in base currencyprice- Trade price in quote currencytaker_or_maker- Optional taker or maker designation
§Returns
Returns a Fee struct containing the currency, cost, and rate.
Sourcepub async fn amount_to_precision(
&self,
symbol: &str,
amount: Decimal,
) -> Result<Decimal, Error>
pub async fn amount_to_precision( &self, symbol: &str, amount: Decimal, ) -> Result<Decimal, Error>
Converts an amount to the precision required by the market
Sourcepub async fn price_to_precision(
&self,
symbol: &str,
price: Decimal,
) -> Result<Decimal, Error>
pub async fn price_to_precision( &self, symbol: &str, price: Decimal, ) -> Result<Decimal, Error>
Converts a price to the precision required by the market
Sourcepub async fn cost_to_precision(
&self,
symbol: &str,
cost: Decimal,
) -> Result<Decimal, Error>
pub async fn cost_to_precision( &self, symbol: &str, cost: Decimal, ) -> Result<Decimal, Error>
Converts a cost to the precision required by the market