Skip to main content

RestClient

Struct RestClient 

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

Async REST client for AsterDex Futures V3 API.

Supports both public (unsigned) and authenticated (signed) endpoints. Use new_public for public-only access, from_env or new for authenticated access.

Implementations§

Source§

impl RestClient

Source

pub fn from_env() -> Result<Self, AsterDexError>

Construct authenticated client from environment variables.

Reads: ASTERDEX_USER, ASTERDEX_SIGNER_ADDRESS, ASTERDEX_SIGNER_PRIVATE_KEY. Optional: ASTERDEX_BASE_URL (defaults to https://fapi.asterdex.com).

§Errors
  • AsterDexError::ConfigError if any required env var is missing or invalid
Source

pub fn new( base_url: &str, credentials: Credentials, ) -> Result<Self, AsterDexError>

Construct authenticated client with explicit base URL and credentials.

§Errors
  • AsterDexError::ConfigError if base URL is invalid
Source

pub fn new_public(base_url: &str) -> Result<Self, AsterDexError>

Construct unauthenticated (public) client.

Only public endpoints (unsigned GET) will work. Signed endpoints will return AsterDexError::ConfigError.

§Errors
  • AsterDexError::ConfigError if base URL is invalid
Source

pub async fn raw_get( &self, path: &str, params: &[(&str, &str)], ) -> Result<ApiResponse<Value>, AsterDexError>

Make an unsigned GET request and return the raw JSON response.

Use when the typed response struct does not cover all fields, or for debugging. Error mapping (429 → RateLimited, etc.) applies identically to typed methods.

§Errors
  • AsterDexError::ApiError — if the server returns a 4xx error with {code, msg}
  • AsterDexError::RateLimited — if the server returns 429
  • AsterDexError::NetworkError — if a network-level error occurs
§Examples
let resp = client.raw_get("/fapi/v3/exchangeInfo", &[]).await?;
println!("{}", resp.data);
Source

pub async fn raw_post( &self, path: &str, params: &[(&str, &str)], ) -> Result<ApiResponse<Value>, AsterDexError>

Make an unsigned POST request and return the raw JSON response.

§Errors
  • AsterDexError::ApiError — if the server returns a 4xx error with {code, msg}
  • AsterDexError::RateLimited — if the server returns 429
  • AsterDexError::NetworkError — if a network-level error occurs
Source

pub async fn raw_delete( &self, path: &str, params: &[(&str, &str)], ) -> Result<ApiResponse<Value>, AsterDexError>

Make an unsigned DELETE request and return the raw JSON response.

§Errors
  • AsterDexError::ApiError — if the server returns a 4xx error with {code, msg}
  • AsterDexError::RateLimited — if the server returns 429
  • AsterDexError::NetworkError — if a network-level error occurs
Source

pub async fn raw_signed_get( &self, path: &str, params: &[(&str, &str)], ) -> Result<ApiResponse<Value>, AsterDexError>

Make a signed GET request and return the raw JSON response (EIP-712 authenticated).

Requires credentials to be set (use RestClient::from_env()).

§Errors
  • AsterDexError::ConfigError — if credentials are not set
  • AsterDexError::ApiError — if the server returns a 4xx error with {code, msg}
  • AsterDexError::RateLimited — if the server returns 429
  • AsterDexError::NetworkError — if a network-level error occurs
Source

pub async fn raw_signed_post( &self, path: &str, params: &[(&str, &str)], ) -> Result<ApiResponse<Value>, AsterDexError>

Make a signed POST request and return the raw JSON response.

Requires credentials to be set (use RestClient::from_env()).

§Errors
  • AsterDexError::ConfigError — if credentials are not set
  • AsterDexError::ApiError — if the server returns a 4xx error with {code, msg}
  • AsterDexError::RateLimited — if the server returns 429
  • AsterDexError::NetworkError — if a network-level error occurs
Source

pub async fn raw_signed_delete( &self, path: &str, params: &[(&str, &str)], ) -> Result<ApiResponse<Value>, AsterDexError>

Make a signed DELETE request and return the raw JSON response.

Requires credentials to be set (use RestClient::from_env()).

§Errors
  • AsterDexError::ConfigError — if credentials are not set
  • AsterDexError::ApiError — if the server returns a 4xx error with {code, msg}
  • AsterDexError::RateLimited — if the server returns 429
  • AsterDexError::NetworkError — if a network-level error occurs
Source§

impl RestClient

Source

pub async fn get_server_time( &self, ) -> Result<ApiResponse<ServerTimeResponse>, AsterDexError>

Get server time in milliseconds.

Use to detect clock drift before placing orders. This is a public endpoint (no authentication required).

Source

pub async fn ping(&self) -> Result<ApiResponse<PingResponse>, AsterDexError>

Ping the REST API to check connectivity.

Public endpoint — no authentication required.

Source

pub async fn get_exchange_info( &self, ) -> Result<ApiResponse<ExchangeInfoResponse>, AsterDexError>

Get exchange information including all trading symbols.

Public endpoint — no authentication required.

Source

pub async fn get_depth( &self, symbol: &str, limit: Option<u32>, ) -> Result<ApiResponse<DepthResponse>, AsterDexError>

Get order book depth for a symbol.

Public endpoint — no authentication required.

Source

pub async fn get_trades( &self, symbol: &str, limit: Option<u32>, ) -> Result<ApiResponse<Vec<TradeRecord>>, AsterDexError>

Get recent trades for a symbol.

Public endpoint — no authentication required.

Source

pub async fn get_historical_trades( &self, symbol: &str, limit: Option<u32>, from_id: Option<i64>, ) -> Result<ApiResponse<Vec<TradeRecord>>, AsterDexError>

Get historical (older) trades for a symbol.

Public endpoint — no authentication required.

Source

pub async fn get_agg_trades( &self, symbol: &str, from_id: Option<i64>, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>, ) -> Result<ApiResponse<Vec<AggTrade>>, AsterDexError>

Get compressed/aggregate trades for a symbol.

Public endpoint — no authentication required.

Source

pub async fn get_klines( &self, symbol: &str, interval: KlineInterval, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>, ) -> Result<ApiResponse<Vec<Kline>>, AsterDexError>

Get kline/candlestick data for a symbol.

Public endpoint — no authentication required.

Source

pub async fn get_index_price_klines( &self, pair: &str, interval: KlineInterval, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>, ) -> Result<ApiResponse<Vec<Kline>>, AsterDexError>

Get kline/candlestick data for an index price.

Public endpoint — no authentication required.

Source

pub async fn get_mark_price_klines( &self, symbol: &str, interval: KlineInterval, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>, ) -> Result<ApiResponse<Vec<Kline>>, AsterDexError>

Get kline/candlestick data for a mark price.

Public endpoint — no authentication required.

Source

pub async fn get_mark_price( &self, symbol: Option<&str>, ) -> Result<ApiResponse<MarkPriceShape>, AsterDexError>

Get mark price and funding rate for a symbol (or all symbols if None).

Public endpoint — no authentication required.

Source

pub async fn get_funding_rate( &self, symbol: Option<&str>, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>, ) -> Result<ApiResponse<Vec<FundingRateRecord>>, AsterDexError>

Get funding rate history for a symbol.

Public endpoint — no authentication required.

Source

pub async fn get_funding_info( &self, ) -> Result<ApiResponse<Vec<FundingInfoResponse>>, AsterDexError>

Get funding rate information for all symbols.

Public endpoint — no authentication required.

Source

pub async fn get_ticker_24hr( &self, symbol: Option<&str>, ) -> Result<ApiResponse<Ticker24hrShape>, AsterDexError>

Get 24-hour ticker price change statistics.

Public endpoint — no authentication required.

Source

pub async fn get_ticker_price( &self, symbol: Option<&str>, ) -> Result<ApiResponse<TickerPriceShape>, AsterDexError>

Get latest price for a symbol or all symbols.

Public endpoint — no authentication required.

Source

pub async fn get_book_ticker( &self, symbol: Option<&str>, ) -> Result<ApiResponse<BookTickerShape>, AsterDexError>

Get best price/quantity on the order book for a symbol.

Public endpoint — no authentication required.

Source

pub async fn get_index_references( &self, symbol: Option<&str>, ) -> Result<ApiResponse<Vec<IndexReferencesResponse>>, AsterDexError>

Get index price references for a symbol.

Public endpoint — no authentication required.

Source§

impl RestClient

Source

pub async fn place_order( &self, params: PlaceOrderParams, ) -> Result<ApiResponse<OrderResponse>, AsterDexError>

Place a new futures order.

§Errors
  • ApiError { code: -1102 } — required parameter missing (e.g., price for LIMIT)
  • ApiError { code: -1121 } — invalid symbol
  • RateLimited — HTTP 429
  • IpBanned — HTTP 418
Source

pub async fn cancel_order( &self, params: CancelOrderParams, ) -> Result<ApiResponse<CancelOrderResponse>, AsterDexError>

Cancel an open order.

One of params.order_id or params.orig_client_order_id must be set.

§Errors
  • ApiError { code: -2011 } — order not found
  • ApiError { code: -1102 } — neither orderId nor origClientOrderId provided
Source

pub async fn modify_order( &self, params: ModifyOrderParams, ) -> Result<ApiResponse<OrderResponse>, AsterDexError>

Modify an existing open LIMIT order in-place (price and/or quantity).

Calls PUT /fapi/v3/order directly. The original order_id is preserved — this is a true amendment, not a cancel + re-place. At least one of params.quantity or params.price must be set. Either params.order_id or params.orig_client_order_id must be provided to identify the order.

Only LIMIT orders can be modified. If the new qty/price fails exchange filters the modification is rejected and the original order remains unchanged.

§Errors
  • ApiError { code: -2011 } — order not found
  • ApiError { code: -1102 } — required parameter missing
Source

pub async fn get_open_orders( &self, symbol: Option<&str>, ) -> Result<ApiResponse<Vec<OrderResponse>>, AsterDexError>

Get all open orders. Pass None for all symbols or Some("BTCUSDT") for one symbol.

Source

pub async fn get_position_risk( &self, symbol: Option<&str>, ) -> Result<ApiResponse<Vec<PositionResponse>>, AsterDexError>

Get position risk information. Pass None for all symbols or Some("BTCUSDT") for one symbol.

Source§

impl RestClient

Source

pub async fn get_balance( &self, ) -> Result<ApiResponse<Vec<Balance>>, AsterDexError>

Get account balance across all assets.

Requires authentication. Returns wallet, cross-wallet, and available balances for each asset held in the futures account.

§Errors
  • ConfigError — credentials not set
  • ApiError — exchange error (e.g. invalid timestamp)
  • RateLimited — HTTP 429
Source

pub async fn get_account_with_join_margin( &self, ) -> Result<ApiResponse<AccountInfoResponse>, AsterDexError>

Get full account info with join margin details.

Requires authentication.

§Errors
  • ConfigError — credentials not set
Source

pub async fn get_position_side_dual( &self, ) -> Result<ApiResponse<PositionSideDualResponse>, AsterDexError>

Get the current position side dual mode setting.

Returns dual_side_position = true when hedge mode is active, false for one-way mode.

§Errors
  • ConfigError — credentials not set
Source

pub async fn set_position_side_dual( &self, dual_side_position: bool, ) -> Result<ApiResponse<StatusMsgResponse>, AsterDexError>

Enable or disable position side dual (hedge) mode.

Pass true to enable hedge mode, false for one-way mode.

§Errors
  • ConfigError — credentials not set
  • ApiError { code: -4059 } — no need to change position side
Source

pub async fn get_leverage_bracket( &self, symbol: Option<&str>, ) -> Result<ApiResponse<Vec<LeverageBracket>>, AsterDexError>

Get leverage bracket information.

Pass Some("BTCUSDT") for a specific symbol, or None for all symbols.

§Errors
  • ConfigError — credentials not set
  • ApiError { code: -1121 } — invalid symbol
Source§

impl RestClient

Source

pub async fn place_batch_orders( &self, orders: Vec<PlaceOrderParams>, ) -> Result<ApiResponse<Vec<BatchOrderResult>>, AsterDexError>

Place multiple orders (max 5). Returns a mixed array — check each item for order_id (success) or code/msg (error).

Source

pub async fn cancel_batch_orders( &self, symbol: &str, order_ids: Vec<i64>, ) -> Result<ApiResponse<Vec<BatchOrderResult>>, AsterDexError>

Cancel multiple orders by ID list.

Source

pub async fn cancel_all_open_orders( &self, symbol: &str, ) -> Result<ApiResponse<CancelAllResponse>, AsterDexError>

Cancel all open orders for a symbol.

Source§

impl RestClient

Source

pub async fn set_leverage( &self, symbol: &str, leverage: u32, ) -> Result<ApiResponse<SetLeverageResponse>, AsterDexError>

Set leverage for a symbol.

Source

pub async fn set_margin_type( &self, symbol: &str, margin_type: &str, ) -> Result<ApiResponse<StatusMsgResponse>, AsterDexError>

Set margin type (ISOLATED or CROSSED) for a symbol.

Source§

impl RestClient

Source

pub async fn create_listen_key( &self, ) -> Result<ApiResponse<ListenKeyResponse>, AsterDexError>

Create a new user data stream listen key.

The returned listen key is valid for ~60 minutes. Renew with renew_listen_key every ~30 minutes to prevent expiry. The caller is responsible for scheduling renewal.

§Errors
  • ConfigError — credentials not set
  • RateLimited — HTTP 429
  • IpBanned — HTTP 418
Source

pub async fn renew_listen_key( &self, listen_key: &str, ) -> Result<ApiResponse<EmptyResponse>, AsterDexError>

Extend the validity of an existing listen key by ~60 minutes.

Call every ~30 minutes to prevent expiry. The caller is responsible for scheduling this renewal — the SDK does not auto-renew (ADR-010).

§Errors
  • ConfigError — credentials not set
  • ApiError { code: -1125 } — listen key not found or already expired
Source

pub async fn delete_listen_key( &self, listen_key: &str, ) -> Result<ApiResponse<EmptyResponse>, AsterDexError>

Delete/invalidate a listen key.

After deletion, the WebSocket user data stream using this key will be closed by the server.

§Errors
  • ConfigError — credentials not set
  • ApiError { code: -1125 } — listen key not found
Source§

impl RestClient

Source

pub async fn get_order( &self, params: GetOrderParams, ) -> Result<ApiResponse<OrderResponse>, AsterDexError>

Query a specific order by ID or client order ID.

Source

pub async fn get_open_order( &self, params: GetOrderParams, ) -> Result<ApiResponse<OrderResponse>, AsterDexError>

Query a specific open order by ID or client order ID.

Source

pub async fn get_all_orders( &self, params: AllOrdersParams, ) -> Result<ApiResponse<Vec<OrderResponse>>, AsterDexError>

Get all orders (open + historical) for a symbol.

Source

pub async fn get_user_trades( &self, params: UserTradesParams, ) -> Result<ApiResponse<Vec<UserTrade>>, AsterDexError>

Get user trade history for a symbol.

Source

pub async fn get_income( &self, params: IncomeParams, ) -> Result<ApiResponse<Vec<IncomeRecord>>, AsterDexError>

Get income history (funding fees, realized PnL, etc.).

Source

pub async fn set_position_margin( &self, params: PositionMarginParams, ) -> Result<ApiResponse<PositionMarginResponse>, AsterDexError>

Add or reduce isolated position margin (type 1 = add, type 2 = reduce).

Source

pub async fn get_position_margin_history( &self, symbol: &str, margin_type: Option<u8>, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>, ) -> Result<ApiResponse<Vec<PositionMarginHistoryRecord>>, AsterDexError>

Get position margin change history.

Source

pub async fn get_adl_quantile( &self, symbol: Option<&str>, ) -> Result<ApiResponse<Vec<AdlQuantileResponse>>, AsterDexError>

Get auto-deleverage quantile for positions.

Source

pub async fn get_force_orders( &self, symbol: Option<&str>, auto_close_type: Option<&str>, start_time: Option<u64>, end_time: Option<u64>, limit: Option<u32>, ) -> Result<ApiResponse<Vec<OrderResponse>>, AsterDexError>

Get forced liquidation order history.

Source

pub async fn get_commission_rate( &self, symbol: &str, ) -> Result<ApiResponse<CommissionRateResponse>, AsterDexError>

Get user commission rate for a symbol.

Source

pub async fn get_multi_assets_margin( &self, ) -> Result<ApiResponse<MultiAssetsModeResponse>, AsterDexError>

Get multi-assets margin mode status.

Source

pub async fn set_multi_assets_margin( &self, enabled: bool, ) -> Result<ApiResponse<StatusMsgResponse>, AsterDexError>

Change multi-assets margin mode.

Source

pub async fn countdown_cancel_all( &self, params: CountdownParams, ) -> Result<ApiResponse<CountdownResponse>, AsterDexError>

Set countdown to auto-cancel all orders. countdown_time = 0 disables.

Source

pub async fn noop(&self) -> Result<ApiResponse<EmptyResponse>, AsterDexError>

No-operation signed endpoint — useful for testing auth.

Source§

impl RestClient

Source

pub async fn get_mmp( &self, symbol: Option<&str>, ) -> Result<ApiResponse<Vec<MmpResponse>>, AsterDexError>

Get MMP (Market Maker Protection) configuration.

Pass Some("BTCUSDT") to query a specific symbol, or None to query all symbols.

§Errors
  • ConfigError — credentials not set
  • ApiError { code: -1121 } — invalid or unknown symbol
  • RateLimited — HTTP 429
Source

pub async fn update_mmp( &self, params: MmpUpdateParams, ) -> Result<ApiResponse<bool>, AsterDexError>

Create or update MMP configuration for a symbol (POST /fapi/v3/mmp).

§Errors
  • ConfigError — credentials not set
  • ApiError — invalid parameters or symbol
  • RateLimited — HTTP 429
Source

pub async fn delete_mmp( &self, symbol: &str, ) -> Result<ApiResponse<bool>, AsterDexError>

Delete MMP configuration for a symbol (DELETE /fapi/v3/mmp).

§Errors
  • ConfigError — credentials not set
  • ApiError — no MMP configured for the symbol
  • RateLimited — HTTP 429
Source

pub async fn reset_mmp( &self, symbol: &str, ) -> Result<ApiResponse<bool>, AsterDexError>

Reset (unfreeze) a frozen MMP for a symbol (POST /fapi/v3/mmpReset).

§Errors
  • ConfigError — credentials not set
  • ApiError — no frozen MMP for the symbol
  • RateLimited — HTTP 429
Source§

impl RestClient

Source

pub async fn wallet_transfer( &self, params: WalletTransferParams, ) -> Result<ApiResponse<WalletTransferResponse>, AsterDexError>

Transfer assets between wallets.

The transfer_type field identifies the wallet pair (e.g. spot→futures, futures→spot). Consult the API docs for valid values.

§Errors
  • ConfigError — credentials not set
  • ApiError — invalid asset, amount, or transfer type
  • RateLimited — HTTP 429
  • IpBanned — HTTP 418
Source

pub async fn sub_account_bind( &self, sub_uid: &str, ) -> Result<ApiResponse<Value>, AsterDexError>

Bind a sub-account to the current account.

The response structure is not yet documented — returned as raw JSON until API docs are clarified. Refactor to a typed struct in a follow-up story.

§Errors
  • ConfigError — credentials not set
  • ApiError — invalid sub-account UID or binding already exists
  • RateLimited — HTTP 429
  • IpBanned — HTTP 418

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.

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

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

Source§

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