diff --git a/Dockerfile b/Dockerfile index d65c81c..814bd7f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index 2cf8446..0fe1c24 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/src/controller.rs b/src/controller.rs index 881b01c..09cb311 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -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, @@ -298,17 +298,25 @@ impl AppState { /// Return SOL balance of the tx signing account pub async fn get_sol_balance(&self) -> GatewayResult { + 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 { + let pubkey = self.wallet.authority().to_string(); + Ok(AuthorityResponse { pubkey }) + } + /// Cancel orders /// /// There are 4 intended scenarios for cancellation, in order of priority: diff --git a/src/main.rs b/src/main.rs index 8106176..8205440 100644 --- a/src/main.rs +++ b/src/main.rs @@ -171,6 +171,11 @@ async fn get_sol_balance(controller: web::Data) -> impl Responder { handle_result(controller.get_sol_balance().await) } +#[get("/authority")] +async fn get_authority(controller: web::Data) -> impl Responder { + handle_result(controller.get_authority()) +} + #[get("/transactionEvent/{tx_sig}")] async fn get_tx_events( controller: web::Data, @@ -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) diff --git a/src/types.rs b/src/types.rs index 303eeff..54d4a4a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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)]