Skip to main content

BaseExchange

Struct BaseExchange 

Source
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: ExchangeConfig

Exchange configuration

§http_client: HttpClient

HTTP client for API requests

§market_cache: Arc<RwLock<MarketCache>>

Thread-safe market data cache

§market_loading_lock: Arc<Mutex<()>>

Mutex to serialize market loading operations

§capabilities: ExchangeCapabilities

Exchange capability flags

§urls: HashMap<String, String>

API endpoint URLs

§timeframes: HashMap<String, String>

Timeframe mappings

§precision_mode: PrecisionMode

Precision mode for price/amount formatting

Implementations§

Source§

impl BaseExchange

Source

pub fn new(config: ExchangeConfig) -> Result<BaseExchange, Error>

Creates a new exchange instance

Source

pub async fn load_markets( &self, reload: bool, ) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>

Loads market data from the exchange

Source

pub async fn load_markets_with_loader<F, Fut>( &self, reload: bool, loader: F, ) -> Result<Arc<HashMap<String, Arc<Market>>>, Error>
where F: FnOnce() -> Fut, Fut: Future<Output = Result<(Vec<Market>, Option<Vec<Currency>>), Error>>,

Loads market data with a custom loader function

Source

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

Source

pub async fn market(&self, symbol: &str) -> Result<Arc<Market>, Error>

Gets market information by trading symbol

Source

pub async fn market_by_id(&self, id: &str) -> Result<Arc<Market>, Error>

Gets market information by exchange-specific market ID

Source

pub async fn currency(&self, code: &str) -> Result<Arc<Currency>, Error>

Gets currency information by currency code

Source

pub async fn symbols(&self) -> Result<Vec<String>, Error>

Gets all available trading symbols

Source

pub fn throttle(&self) -> Result<(), Error>

👎Deprecated since 0.2.0: Rate limiting is now handled internally by HttpClient. This method is a no-op.

Applies rate limiting if enabled (deprecated, now handled by HttpClient)

Source

pub fn check_required_credentials(&self) -> Result<(), Error>

Checks that required API credentials are configured

Source

pub fn nonce(&self) -> i64

Gets a nonce value (current timestamp in milliseconds)

Source

pub fn build_query_string(&self, params: &HashMap<String, Value>) -> String

Builds a URL query string from parameters

Source

pub fn parse_json(&self, response: &str) -> Result<Value, Error>

Parses a JSON response string

Source

pub fn handle_http_error(&self, status_code: u16, response: &str) -> Error

Handles HTTP error responses

Source

pub fn safe_string(&self, dict: &Value, key: &str) -> Option<String>

Safely extracts a string value from a JSON object

Source

pub fn safe_integer(&self, dict: &Value, key: &str) -> Option<i64>

Safely extracts an integer value from a JSON object

Source

pub fn safe_float(&self, dict: &Value, key: &str) -> Option<f64>

Safely extracts a float value from a JSON object

Source

pub fn safe_bool(&self, dict: &Value, key: &str) -> Option<bool>

Safely extracts a boolean value from a JSON object

Source

pub fn parse_ticker( &self, ticker_data: &Value, market: Option<&Market>, ) -> Result<Ticker, Error>

Parses raw ticker data from exchange API response

Source

pub fn parse_trade( &self, trade_data: &Value, market: Option<&Market>, ) -> Result<Trade, Error>

Parses raw trade data from exchange API response

Source

pub fn parse_order( &self, order_data: &Value, market: Option<&Market>, ) -> Result<Order, Error>

Parses raw order data from exchange API response

Source

pub fn parse_balance(&self, balance_data: &Value) -> Result<Balance, Error>

Parses raw balance data from exchange API response

Source

pub fn parse_order_book( &self, orderbook_data: &Value, timestamp: Option<i64>, ) -> Result<OrderBook, Error>

Parses raw order book data from exchange API response

Source

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

Source

pub async fn amount_to_precision( &self, symbol: &str, amount: Decimal, ) -> Result<Decimal, Error>

Converts an amount to the precision required by the market

Source

pub async fn price_to_precision( &self, symbol: &str, price: Decimal, ) -> Result<Decimal, Error>

Converts a price to the precision required by the market

Source

pub async fn cost_to_precision( &self, symbol: &str, cost: Decimal, ) -> Result<Decimal, Error>

Converts a cost to the precision required by the market

Source

pub async fn calculate_cost( &self, symbol: &str, amount: Decimal, price: Decimal, ) -> Result<Decimal, Error>

Calculates the cost of a trade

Trait Implementations§

Source§

impl Clone for BaseExchange

Source§

fn clone(&self) -> BaseExchange

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BaseExchange

Source§

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

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

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
Source§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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