Skip to content

Commit

Permalink
Feat/leverage (#50)
Browse files Browse the repository at this point in the history
* add user leverage endpoint
  • Loading branch information
akashr-gsr authored May 7, 2024
1 parent ae39a66 commit 8c51e57
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Self hosted API gateway to easily interact with Drift V2 Protocol
- [`GET` Transaction Events](#get-transaction-events)
- [`GET` SOL Balance](#get-sol-balance)
- [`GET` Margin Info](#get-margin-info)
- [`GET` Leverage](#get-leverage)
- [`POST` Place Orders](#place-orders)
- [`PATCH` Modify Orders](#modify-orders)
- [`DELETE` Cancel Orders](#cancel-orders)
Expand Down Expand Up @@ -209,6 +210,21 @@ $ curl localhost:8080/v2/user/marginInfo
}
```

## Get Leverage
Returns the account leverage

```bash
$ curl localhost:8080/v2/leverage
```

**Response**

```json
{
"leverage" : "0.094489"
}
```

## Get Market Info

Returns market details (perps only)
Expand Down
13 changes: 12 additions & 1 deletion src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use drift_sdk::math::leverage::get_leverage;
use drift_sdk::{
constants::{ProgramData, BASE_PRECISION},
dlob_client::DLOBClient,
Expand Down Expand Up @@ -26,7 +27,7 @@ use crate::{
GetOrderbookRequest, GetOrdersRequest, GetOrdersResponse, GetPositionsRequest,
GetPositionsResponse, Market, MarketInfoResponse, ModifyOrdersRequest, Order, OrderbookL2,
PerpPosition, PerpPositionExtended, PlaceOrdersRequest, SolBalanceResponse, SpotPosition,
TxEventsResponse, TxResponse, UserMarginResponse, PRICE_DECIMALS,
TxEventsResponse, TxResponse, UserMarginResponse, UserLeverageResponse, PRICE_DECIMALS,
},
websocket::map_drift_event_for_account,
Context, LOG_TARGET,
Expand Down Expand Up @@ -216,6 +217,16 @@ impl AppState {
.map_err(|err| ControllerError::Sdk(err))
}

pub async fn get_leverage(&self, ctx: Context) -> GatewayResult<UserLeverageResponse> {
let sub_account = self.resolve_sub_account(ctx.sub_account_id);
get_leverage(
&self.client,
&self.client.get_user_account(&sub_account).await?,
)
.map(Into::into)
.map_err(|err| ControllerError::Sdk(err))
}

pub async fn get_position_extended(
&self,
ctx: Context,
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ async fn get_margin_info(
handle_result(controller.get_margin_info(ctx.0).await)
}

#[get("/leverage")]
async fn get_leverage(controller: web::Data<AppState>, ctx: web::Query<Context>) -> impl Responder {
handle_result(controller.get_leverage(ctx.0).await)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
let config: GatewayConfig = argh::from_env();
Expand Down Expand Up @@ -277,7 +282,8 @@ async fn main() -> std::io::Result<()> {
.service(get_positions_extended)
.service(get_tx_events)
.service(get_market_info)
.service(get_margin_info),
.service(get_margin_info)
.service(get_leverage),
)
})
.keep_alive(Duration::from_secs(config.keep_alive_timeout as u64))
Expand Down
13 changes: 13 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,19 @@ impl From<MarginRequirementInfo> for UserMarginResponse {
}
}

#[derive(Serialize, Debug)]
pub struct UserLeverageResponse {
pub leverage: Decimal,
}

impl From<u128> for UserLeverageResponse {
fn from(value: u128) -> Self {
Self {
leverage: Decimal::from_i128_with_scale(value as i128, PRICE_DECIMALS).normalize(),
}
}
}

#[cfg(test)]
mod tests {
use drift_sdk::{
Expand Down

0 comments on commit 8c51e57

Please sign in to comment.