Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ FROM rust:1.84.1 AS builder

RUN apt-get update && apt-get install -y libgcc1 jq
WORKDIR /build
COPY . .
RUN rustup component add rustfmt
RUN SO_URL=$(curl -s https://api.github.com/repos/drift-labs/drift-ffi-sys/releases/latest | jq -r '.assets[] | select(.name=="libdrift_ffi_sys.so") | .browser_download_url') &&\
curl -L -o libdrift_ffi_sys.so "$SO_URL" &&\
cp libdrift_ffi_sys.so /usr/local/lib

COPY . .
# DEV: choose to build drift system libs from source or not
# a) default: use prebuilt lib (faster build time)
RUN CARGO_DRIFT_FFI_PATH="/usr/local/lib" cargo build --release
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Self hosted API gateway to easily interact with Drift V2 Protocol
- [`GET` Perp Position Info](#get-position-info-perps-only)
- [`GET` Transaction Events](#get-transaction-events)
- [`GET` SOL Balance](#get-sol-balance)
- [`GET` Authority](#get-authority)
- [`GET` Margin Info](#get-margin-info)
- [`GET` Leverage](#get-leverage)
- [`POST` Leverage](#set-leverage)
Expand Down Expand Up @@ -569,9 +570,20 @@ $ curl localhost:8080/v2/balance
```

```json
{ "balance": "0.12" }
{ "balance": "0.12", "pubkey": "key" }
```

### Get Authority
Return the on-chain SOL balance of the transaction signer (`DRIFT_GATEWAY_KEY`)
```bash
$ curl localhost:8080/v2/authority
```

```json
{ "pubkey": "key" }
```


### Place Orders

- use sub-zero `amount` to indicate sell/offer order
Expand Down
22 changes: 15 additions & 7 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ use thiserror::Error;

use crate::{
types::{
get_market_decimals, scale_decimal_to_u64, AllMarketsResponse, CancelAndPlaceRequest,
CancelOrdersRequest, GetOrdersRequest, GetOrdersResponse, GetPositionsRequest,
GetPositionsResponse, Market, MarketInfoResponse, ModifyOrdersRequest, Order, PerpPosition,
PerpPositionExtended, PlaceOrdersRequest, SolBalanceResponse, SpotPosition, SwapRequest,
TxEventsResponse, TxResponse, UserCollateralResponse, UserLeverageResponse,
UserMarginResponse, PRICE_DECIMALS,
get_market_decimals, scale_decimal_to_u64, AllMarketsResponse, AuthorityResponse,
CancelAndPlaceRequest, CancelOrdersRequest, GetOrdersRequest, GetOrdersResponse,
GetPositionsRequest, GetPositionsResponse, Market, MarketInfoResponse, ModifyOrdersRequest,
Order, PerpPosition, PerpPositionExtended, PlaceOrdersRequest, SolBalanceResponse,
SpotPosition, SwapRequest, TxEventsResponse, TxResponse, UserCollateralResponse,
UserLeverageResponse, UserMarginResponse, PRICE_DECIMALS,
},
websocket::map_drift_event_for_account,
Context, LOG_TARGET,
Expand Down Expand Up @@ -298,17 +298,25 @@ impl AppState {

/// Return SOL balance of the tx signing account
pub async fn get_sol_balance(&self) -> GatewayResult<SolBalanceResponse> {
let pubkey = self.authority();
let balance = self
.client
.rpc()
.get_balance(&self.wallet.signer())
.get_balance(pubkey)
.await
.map_err(|err| ControllerError::Sdk(err.into()))?;
Ok(SolBalanceResponse {
balance: Decimal::new(balance as i64, BASE_PRECISION.ilog10()).normalize(),
pubkey: pubkey.to_string(),
})
}

/// Return Pubkey of Authority (signer)
pub fn get_authority(&self) -> GatewayResult<AuthorityResponse> {
let pubkey = self.wallet.authority().to_string();
Ok(AuthorityResponse { pubkey })
}

/// Cancel orders
///
/// There are 4 intended scenarios for cancellation, in order of priority:
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ async fn get_sol_balance(controller: web::Data<AppState>) -> impl Responder {
handle_result(controller.get_sol_balance().await)
}

#[get("/authority")]
async fn get_authority(controller: web::Data<AppState>) -> impl Responder {
handle_result(controller.get_authority())
}

#[get("/transactionEvent/{tx_sig}")]
async fn get_tx_events(
controller: web::Data<AppState>,
Expand Down Expand Up @@ -381,6 +386,7 @@ async fn main() -> std::io::Result<()> {
.service(modify_orders)
.service(cancel_and_place_orders)
.service(get_sol_balance)
.service(get_authority)
.service(get_positions_extended)
.service(get_tx_events)
.service(get_market_info)
Expand Down
6 changes: 6 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,12 @@ pub(crate) fn get_market_decimals(program_data: &ProgramData, market: Market) ->
#[derive(Serialize, Debug)]
pub struct SolBalanceResponse {
pub balance: Decimal,
pub pubkey: String,
}

#[derive(Serialize, Debug)]
pub struct AuthorityResponse {
pub pubkey: String,
}

#[derive(Serialize, Debug)]
Expand Down
Loading