use super::*;
use anyhow::Error;
pub struct Accounts<'a> {
rest: &'a RestClient,
}
impl<'a> Accounts<'a> {
pub(crate) fn new(rest: &'a RestClient) -> Self {
Self { rest }
}
pub async fn get_subaccounts(&self, address: &Address) -> Result<AddressResponse, Error> {
let rest = &self.rest;
const URI: &str = "/v4/addresses";
let url = format!("{}{URI}/{address}", rest.config.endpoint);
let resp = rest
.client
.get(url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(resp)
}
pub async fn get_subaccount(
&self,
subaccount: &Subaccount,
) -> Result<SubaccountResponseObject, Error> {
let rest = &self.rest;
const URI: &str = "/v4/addresses";
let address = &subaccount.address;
let number = &subaccount.number;
let url = format!(
"{}{URI}/{address}/subaccountNumber/{number}",
rest.config.endpoint
);
let subaccount = rest
.client
.get(url)
.send()
.await?
.error_for_status()?
.json::<SubaccountResponse>()
.await?
.subaccount;
Ok(subaccount)
}
pub async fn get_parent_subaccount(
&self,
subaccount: &ParentSubaccount,
) -> Result<ParentSubaccountResponseObject, Error> {
let rest = &self.rest;
const URI: &str = "/v4/addresses";
let address = &subaccount.address;
let number = &subaccount.number;
let url = format!(
"{}{URI}/{address}/parentSubaccountNumber/{number}",
rest.config.endpoint
);
let subaccount = rest
.client
.get(url)
.send()
.await?
.error_for_status()?
.json::<ParentSubaccountResponse>()
.await?
.subaccount;
Ok(subaccount)
}
#[deprecated(
since = "0.3.0",
note = "Use `get_subaccount_perpetual_positions` instead"
)]
pub async fn list_positions(
&self,
subaccount: &Subaccount,
opts: Option<ListPositionsOpts>,
) -> Result<Vec<PerpetualPositionResponseObject>, Error> {
self.get_subaccount_perpetual_positions(subaccount, opts)
.await
}
pub async fn get_subaccount_perpetual_positions(
&self,
subaccount: &Subaccount,
opts: Option<ListPositionsOpts>,
) -> Result<Vec<PerpetualPositionResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/perpetualPositions";
let url = format!("{}{URI}", rest.config.endpoint);
let query = Query {
address: &subaccount.address,
subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let positions = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<PerpetualPositionResponse>()
.await?
.positions;
Ok(positions)
}
#[deprecated(
since = "0.3.0",
note = "Use `get_parent_subaccount_perpetual_positions` instead"
)]
pub async fn list_parent_positions(
&self,
subaccount: &ParentSubaccount,
opts: Option<ListPositionsOpts>,
) -> Result<Vec<PerpetualPositionResponseObject>, Error> {
self.get_parent_subaccount_perpetual_positions(subaccount, opts)
.await
}
pub async fn get_parent_subaccount_perpetual_positions(
&self,
subaccount: &ParentSubaccount,
opts: Option<ListPositionsOpts>,
) -> Result<Vec<PerpetualPositionResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/perpetualPositions";
let url = format!("{}{URI}/parentSubaccountNumber", rest.config.endpoint);
let query = QueryParent {
address: &subaccount.address,
parent_subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let positions = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<PerpetualPositionResponse>()
.await?
.positions;
Ok(positions)
}
#[deprecated(since = "0.3.0", note = "Use `get_subaccount_asset_positions` instead")]
pub async fn get_asset_positions(
&self,
subaccount: &Subaccount,
) -> Result<Vec<AssetPositionResponseObject>, Error> {
self.get_subaccount_asset_positions(subaccount).await
}
pub async fn get_subaccount_asset_positions(
&self,
subaccount: &Subaccount,
) -> Result<Vec<AssetPositionResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/assetPositions";
let url = format!("{}{URI}", rest.config.endpoint);
let query = Query {
address: &subaccount.address,
subaccount_number: &subaccount.number,
};
let positions = rest
.client
.get(url)
.query(&query)
.send()
.await?
.error_for_status()?
.json::<AssetPositionResponse>()
.await?
.positions;
Ok(positions)
}
pub async fn get_parent_asset_positions(
&self,
subaccount: &ParentSubaccount,
) -> Result<Vec<AssetPositionResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/assetPositions";
let url = format!("{}{URI}/parentSubaccountNumber", rest.config.endpoint);
let query = QueryParent {
address: &subaccount.address,
parent_subaccount_number: &subaccount.number,
};
let positions = rest
.client
.get(url)
.query(&query)
.send()
.await?
.error_for_status()?
.json::<AssetPositionResponse>()
.await?
.positions;
Ok(positions)
}
#[deprecated(since = "0.3.0", note = "Use `get_subaccount_transfers` instead")]
pub async fn get_transfers(
&self,
subaccount: &Subaccount,
opts: Option<GetTransfersOpts>,
) -> Result<Vec<TransferResponseObject>, Error> {
self.get_subaccount_transfers(subaccount, opts).await
}
pub async fn get_subaccount_transfers(
&self,
subaccount: &Subaccount,
opts: Option<GetTransfersOpts>,
) -> Result<Vec<TransferResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/transfers";
let url = format!("{}{URI}", rest.config.endpoint);
let query = Query {
address: &subaccount.address,
subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let transfers = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<TransferResponse>()
.await?
.transfers;
Ok(transfers)
}
#[deprecated(
since = "0.3.0",
note = "Use `get_parent_subaccount_number_transfers` instead"
)]
pub async fn get_parent_transfers(
&self,
subaccount: &ParentSubaccount,
opts: Option<GetTransfersOpts>,
) -> Result<Vec<ParentSubaccountTransferResponseObject>, Error> {
self.get_parent_subaccount_number_transfers(subaccount, opts)
.await
}
pub async fn get_parent_subaccount_number_transfers(
&self,
subaccount: &ParentSubaccount,
opts: Option<GetTransfersOpts>,
) -> Result<Vec<ParentSubaccountTransferResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/transfers";
let url = format!("{}{URI}/parentSubaccountNumber", rest.config.endpoint);
let query = QueryParent {
address: &subaccount.address,
parent_subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let transfers = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<ParentSubaccountTransferResponse>()
.await?
.transfers;
Ok(transfers)
}
#[deprecated(since = "0.3.0", note = "Use `get_subaccount_orders` instead")]
pub async fn list_orders(
&self,
subaccount: &Subaccount,
opts: Option<ListOrdersOpts>,
) -> Result<ListOrdersResponse, Error> {
self.get_subaccount_orders(subaccount, opts).await
}
pub async fn get_subaccount_orders(
&self,
subaccount: &Subaccount,
opts: Option<ListOrdersOpts>,
) -> Result<ListOrdersResponse, Error> {
let rest = &self.rest;
const URI: &str = "/v4/orders";
let url = format!("{}{URI}", rest.config.endpoint);
let query = Query {
address: &subaccount.address,
subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let orders = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(orders)
}
#[deprecated(
since = "0.3.0",
note = "Use `get_parent_subaccount_number_orders` instead"
)]
pub async fn list_parent_orders(
&self,
subaccount: &ParentSubaccount,
opts: Option<ListOrdersOpts>,
) -> Result<ListOrdersResponse, Error> {
self.get_parent_subaccount_number_orders(subaccount, opts)
.await
}
pub async fn get_parent_subaccount_number_orders(
&self,
subaccount: &ParentSubaccount,
opts: Option<ListOrdersOpts>,
) -> Result<ListOrdersResponse, Error> {
let rest = &self.rest;
const URI: &str = "/v4/orders";
let url = format!("{}{URI}/parentSubaccountNumber", rest.config.endpoint);
let query = QueryParent {
address: &subaccount.address,
parent_subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let orders = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(orders)
}
pub async fn get_order(&self, order_id: &OrderId) -> Result<OrderResponseObject, Error> {
let rest = &self.rest;
const URI: &str = "/v4/orders";
let url = format!("{}{URI}/{order_id}", rest.config.endpoint);
let order = rest
.client
.get(url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(order)
}
#[deprecated(since = "0.3.0", note = "Use `get_subaccount_fills` instead")]
pub async fn get_fills(
&self,
subaccount: &Subaccount,
opts: Option<GetFillsOpts>,
) -> Result<Vec<FillResponseObject>, Error> {
self.get_subaccount_fills(subaccount, opts).await
}
pub async fn get_subaccount_fills(
&self,
subaccount: &Subaccount,
opts: Option<GetFillsOpts>,
) -> Result<Vec<FillResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/fills";
let url = format!("{}{URI}", rest.config.endpoint);
let query = Query {
address: &subaccount.address,
subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let fills = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<FillResponse>()
.await?
.fills;
Ok(fills)
}
#[deprecated(
since = "0.3.0",
note = "Use `get_parent_subaccount_number_fills` instead"
)]
pub async fn get_parent_fills(
&self,
subaccount: &ParentSubaccount,
opts: Option<GetFillsOpts>,
) -> Result<Vec<FillResponseObject>, Error> {
self.get_parent_subaccount_number_fills(subaccount, opts)
.await
}
pub async fn get_parent_subaccount_number_fills(
&self,
subaccount: &ParentSubaccount,
opts: Option<GetFillsOpts>,
) -> Result<Vec<FillResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/fills";
let url = format!("{}{URI}/parentSubaccountNumber", rest.config.endpoint);
let query = QueryParent {
address: &subaccount.address,
parent_subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let fills = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<FillResponse>()
.await?
.fills;
Ok(fills)
}
#[deprecated(since = "0.3.0", note = "Use `get_subaccount_historical_pnls` instead")]
pub async fn get_historical_pnl(
&self,
subaccount: &Subaccount,
opts: Option<GetHistoricalPnlOpts>,
) -> Result<Vec<PnlTicksResponseObject>, Error> {
self.get_subaccount_historical_pnls(subaccount, opts).await
}
pub async fn get_subaccount_historical_pnls(
&self,
subaccount: &Subaccount,
opts: Option<GetHistoricalPnlOpts>,
) -> Result<Vec<PnlTicksResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/historical-pnl";
let url = format!("{}{URI}", rest.config.endpoint);
let query = Query {
address: &subaccount.address,
subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let pnls = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<HistoricalPnlResponse>()
.await?
.historical_pnl;
Ok(pnls)
}
#[deprecated(
since = "0.3.0",
note = "Use `get_parent_subaccount_number_historical_pnls` instead"
)]
pub async fn get_parent_historical_pnl(
&self,
subaccount: &ParentSubaccount,
opts: Option<GetHistoricalPnlOpts>,
) -> Result<Vec<PnlTicksResponseObject>, Error> {
self.get_parent_subaccount_number_historical_pnls(subaccount, opts)
.await
}
pub async fn get_parent_subaccount_number_historical_pnls(
&self,
subaccount: &ParentSubaccount,
opts: Option<GetHistoricalPnlOpts>,
) -> Result<Vec<PnlTicksResponseObject>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/historical-pnl";
let url = format!("{}{URI}/parentSubaccountNumber", rest.config.endpoint);
let query = QueryParent {
address: &subaccount.address,
parent_subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let pnls = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<HistoricalPnlResponse>()
.await?
.historical_pnl;
Ok(pnls)
}
#[deprecated(
since = "0.3.0",
note = "Use `get_historical_block_trading_rewards` instead"
)]
pub async fn get_rewards(
&self,
address: &Address,
opts: Option<GetTradingRewardsOpts>,
) -> Result<Vec<HistoricalBlockTradingReward>, Error> {
self.get_historical_block_trading_rewards(address, opts)
.await
}
pub async fn get_historical_block_trading_rewards(
&self,
address: &Address,
opts: Option<GetTradingRewardsOpts>,
) -> Result<Vec<HistoricalBlockTradingReward>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/historicalBlockTradingRewards";
let url = format!("{}{URI}/{address}", rest.config.endpoint);
let options = opts.unwrap_or_default();
let rewards = rest
.client
.get(url)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<HistoricalBlockTradingRewardsResponse>()
.await?
.rewards;
Ok(rewards)
}
#[deprecated(
since = "0.3.0",
note = "Use `get_historical_trading_rewards_aggregations` instead"
)]
pub async fn get_rewards_aggregated(
&self,
address: &Address,
period: TradingRewardAggregationPeriod,
opts: Option<GetAggregationsOpts>,
) -> Result<Vec<HistoricalTradingRewardAggregation>, Error> {
self.get_historical_trading_rewards_aggregations(address, period, opts)
.await
}
pub async fn get_historical_trading_rewards_aggregations(
&self,
address: &Address,
period: TradingRewardAggregationPeriod,
opts: Option<GetAggregationsOpts>,
) -> Result<Vec<HistoricalTradingRewardAggregation>, Error> {
let rest = &self.rest;
const URI: &str = "/v4/historicalTradingRewardAggregations";
let url = format!("{}{URI}/{address}", rest.config.endpoint);
let options = opts.unwrap_or_default();
let aggregated = rest
.client
.get(url)
.query(&[("period", &period)])
.query(&options)
.send()
.await?
.error_for_status()?
.json::<HistoricalTradingRewardAggregationsResponse>()
.await?
.rewards;
Ok(aggregated)
}
pub async fn get_transfers_between(
&self,
source_subaccount: &Subaccount,
recipient_subaccount: &Subaccount,
opts: Option<GetTransfersBetweenOpts>,
) -> Result<TransferBetweenResponse, Error> {
let rest = &self.rest;
const URI: &str = "/v4/transfers/between";
let url = format!("{}{URI}", rest.config.endpoint);
let query = GetTransfersBetweenQuery {
source_address: &source_subaccount.address,
source_subaccount_number: &source_subaccount.number,
recipient_address: &recipient_subaccount.address,
recipient_subaccount_number: &recipient_subaccount.number,
};
let transfers = rest
.client
.get(url)
.query(&query)
.query(&opts)
.send()
.await?
.error_for_status()?
.json::<TransferBetweenResponse>()
.await?;
Ok(transfers)
}
pub async fn search_trader(&self, search_param: &str) -> Result<TraderSearchResponse, Error> {
let rest = &self.rest;
const URI: &str = "/v4/trader/search";
let url = format!("{}{URI}", rest.config.endpoint);
let trader = rest
.client
.get(url)
.query(&[("searchParam", search_param)])
.send()
.await?
.error_for_status()?
.json::<TraderSearchResponse>()
.await?;
Ok(trader)
}
pub async fn get_funding_payments(
&self,
subaccount: &Subaccount,
opts: Option<GetFundingPaymentsOpts>,
) -> Result<FundingPaymentResponse, Error> {
let rest = &self.rest;
const URI: &str = "/v4/fundingPayments";
let url = format!("{}{URI}", rest.config.endpoint);
let query = Query {
address: &subaccount.address,
subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let funding_payments = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<FundingPaymentResponse>()
.await?;
Ok(funding_payments)
}
pub async fn get_funding_payments_for_parent_subaccount(
&self,
subaccount: &ParentSubaccount,
opts: Option<GetFundingPaymentsOpts>,
) -> Result<FundingPaymentResponse, Error> {
let rest = &self.rest;
const URI: &str = "/v4/fundingPayments";
let url = format!("{}{URI}/parentSubaccount", rest.config.endpoint);
let query = QueryParent {
address: &subaccount.address,
parent_subaccount_number: &subaccount.number,
};
let options = opts.unwrap_or_default();
let funding_payments = rest
.client
.get(url)
.query(&query)
.query(&options)
.send()
.await?
.error_for_status()?
.json::<FundingPaymentResponse>()
.await?;
Ok(funding_payments)
}
}