Skip to content

Commit 8c51e57

Browse files
authored
Feat/leverage (#50)
* add user leverage endpoint
1 parent ae39a66 commit 8c51e57

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Self hosted API gateway to easily interact with Drift V2 Protocol
2020
- [`GET` Transaction Events](#get-transaction-events)
2121
- [`GET` SOL Balance](#get-sol-balance)
2222
- [`GET` Margin Info](#get-margin-info)
23+
- [`GET` Leverage](#get-leverage)
2324
- [`POST` Place Orders](#place-orders)
2425
- [`PATCH` Modify Orders](#modify-orders)
2526
- [`DELETE` Cancel Orders](#cancel-orders)
@@ -209,6 +210,21 @@ $ curl localhost:8080/v2/user/marginInfo
209210
}
210211
```
211212

213+
## Get Leverage
214+
Returns the account leverage
215+
216+
```bash
217+
$ curl localhost:8080/v2/leverage
218+
```
219+
220+
**Response**
221+
222+
```json
223+
{
224+
"leverage" : "0.094489"
225+
}
226+
```
227+
212228
## Get Market Info
213229

214230
Returns market details (perps only)

src/controller.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use drift_sdk::math::leverage::get_leverage;
12
use drift_sdk::{
23
constants::{ProgramData, BASE_PRECISION},
34
dlob_client::DLOBClient,
@@ -26,7 +27,7 @@ use crate::{
2627
GetOrderbookRequest, GetOrdersRequest, GetOrdersResponse, GetPositionsRequest,
2728
GetPositionsResponse, Market, MarketInfoResponse, ModifyOrdersRequest, Order, OrderbookL2,
2829
PerpPosition, PerpPositionExtended, PlaceOrdersRequest, SolBalanceResponse, SpotPosition,
29-
TxEventsResponse, TxResponse, UserMarginResponse, PRICE_DECIMALS,
30+
TxEventsResponse, TxResponse, UserMarginResponse, UserLeverageResponse, PRICE_DECIMALS,
3031
},
3132
websocket::map_drift_event_for_account,
3233
Context, LOG_TARGET,
@@ -216,6 +217,16 @@ impl AppState {
216217
.map_err(|err| ControllerError::Sdk(err))
217218
}
218219

220+
pub async fn get_leverage(&self, ctx: Context) -> GatewayResult<UserLeverageResponse> {
221+
let sub_account = self.resolve_sub_account(ctx.sub_account_id);
222+
get_leverage(
223+
&self.client,
224+
&self.client.get_user_account(&sub_account).await?,
225+
)
226+
.map(Into::into)
227+
.map_err(|err| ControllerError::Sdk(err))
228+
}
229+
219230
pub async fn get_position_extended(
220231
&self,
221232
ctx: Context,

src/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ async fn get_margin_info(
192192
handle_result(controller.get_margin_info(ctx.0).await)
193193
}
194194

195+
#[get("/leverage")]
196+
async fn get_leverage(controller: web::Data<AppState>, ctx: web::Query<Context>) -> impl Responder {
197+
handle_result(controller.get_leverage(ctx.0).await)
198+
}
199+
195200
#[actix_web::main]
196201
async fn main() -> std::io::Result<()> {
197202
let config: GatewayConfig = argh::from_env();
@@ -277,7 +282,8 @@ async fn main() -> std::io::Result<()> {
277282
.service(get_positions_extended)
278283
.service(get_tx_events)
279284
.service(get_market_info)
280-
.service(get_margin_info),
285+
.service(get_margin_info)
286+
.service(get_leverage),
281287
)
282288
})
283289
.keep_alive(Duration::from_secs(config.keep_alive_timeout as u64))

src/types.rs

+13
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,19 @@ impl From<MarginRequirementInfo> for UserMarginResponse {
616616
}
617617
}
618618

619+
#[derive(Serialize, Debug)]
620+
pub struct UserLeverageResponse {
621+
pub leverage: Decimal,
622+
}
623+
624+
impl From<u128> for UserLeverageResponse {
625+
fn from(value: u128) -> Self {
626+
Self {
627+
leverage: Decimal::from_i128_with_scale(value as i128, PRICE_DECIMALS).normalize(),
628+
}
629+
}
630+
}
631+
619632
#[cfg(test)]
620633
mod tests {
621634
use drift_sdk::{

0 commit comments

Comments
 (0)