pub struct BaseExchange {
pub config: ExchangeConfig,
pub http_client: HttpClient,
pub rate_limiter: Option<RateLimiter>,
pub market_cache: Arc<RwLock<MarketCache>>,
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
rate_limiter: Option<RateLimiter>Rate limiter instance
market_cache: Arc<RwLock<MarketCache>>Thread-safe market data cache
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<Self>
pub fn new(config: ExchangeConfig) -> Result<Self>
Sourcepub async fn load_markets(
&self,
reload: bool,
) -> Result<HashMap<String, Market>>
pub async fn load_markets( &self, reload: bool, ) -> Result<HashMap<String, Market>>
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 set_markets(
&self,
markets: Vec<Market>,
currencies: Option<Vec<Currency>>,
) -> Result<Vec<Market>>
pub async fn set_markets( &self, markets: Vec<Market>, currencies: Option<Vec<Currency>>, ) -> Result<Vec<Market>>
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 async fn market_by_id(&self, id: &str) -> Result<Market>
pub async fn market_by_id(&self, id: &str) -> Result<Market>
Sourcepub async fn throttle(&self) -> Result<()>
pub async fn throttle(&self) -> Result<()>
Applies rate limiting if enabled
Blocks until rate limit quota is available.
§Returns
Returns Ok(()) after rate limit check passes.
Sourcepub fn check_required_credentials(&self) -> Result<()>
pub fn check_required_credentials(&self) -> Result<()>
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 parse_json(&self, response: &str) -> Result<Value>
pub fn parse_json(&self, response: &str) -> Result<Value>
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_balance(&self, balance_data: &Value) -> Result<Balance>
pub fn parse_balance(&self, balance_data: &Value) -> Result<Balance>
Sourcepub fn parse_order_book(
&self,
orderbook_data: &Value,
timestamp: Option<i64>,
) -> Result<OrderBook>
pub fn parse_order_book( &self, orderbook_data: &Value, timestamp: Option<i64>, ) -> Result<OrderBook>
Sourcepub async fn calculate_fee(
&self,
symbol: &str,
_order_type: OrderType,
_side: OrderSide,
amount: Decimal,
price: Decimal,
taker_or_maker: Option<&str>,
) -> Result<Fee>
pub async fn calculate_fee( &self, symbol: &str, _order_type: OrderType, _side: OrderSide, amount: Decimal, price: Decimal, taker_or_maker: Option<&str>, ) -> Result<Fee>
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>
pub async fn amount_to_precision( &self, symbol: &str, amount: Decimal, ) -> Result<Decimal>
Converts an amount to the precision required by the market
Sourcepub async fn price_to_precision(
&self,
symbol: &str,
price: Decimal,
) -> Result<Decimal>
pub async fn price_to_precision( &self, symbol: &str, price: Decimal, ) -> Result<Decimal>
Converts a price to the precision required by the market
Sourcepub async fn cost_to_precision(
&self,
symbol: &str,
cost: Decimal,
) -> Result<Decimal>
pub async fn cost_to_precision( &self, symbol: &str, cost: Decimal, ) -> Result<Decimal>
Converts a cost to the precision required by the market