Skip to content

Commit

Permalink
remove immediateOrCancel (unsupported)
Browse files Browse the repository at this point in the history
Add get SOL balance endpoint
  • Loading branch information
jordy25519 committed Feb 6, 2024
1 parent 73d6bf5 commit 2a1e26c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ $ curl -X GET \
"postOnly": true,
"reduceOnly": false,
"userOrderId": 101,
"orderId": 35,
"immediateOrCancel": false
"orderId": 35
},
{
"orderType": "limit",
Expand All @@ -175,7 +174,6 @@ $ curl -X GET \
"reduceOnly": false,
"userOrderId": 103,
"orderId": 50,
"immediateOrCancel": false,
"oraclePriceOffset": "20.000000"
}
]
Expand Down Expand Up @@ -235,7 +233,6 @@ $ curl localhost:8080/v2/orders -X POST \
"postOnly": true,
"orderType": "limit",
"userOrderId": 101,
"immediateOrCancel": false,
"reduceOnly": false,
"maxTs": 1707112301
},
Expand Down Expand Up @@ -322,7 +319,6 @@ $ curl localhost:8080/v2/orders/cancelAndPlace -X POST -H 'content-type: applica
"price": 99.0,
"postOnly": true,
"orderType": "limit",
"immediateOrCancel": false,
"reduceOnly": false
}]
}
Expand Down Expand Up @@ -401,7 +397,6 @@ event payloads can be distinguished by "channel" field and the "data" payload is
"direction": "buy",
"reduceOnly": false,
"postOnly": false,
"immediateOrCancel": false,
"auctionDuration": 0
},
"ts": 1704777347,
Expand Down
18 changes: 16 additions & 2 deletions src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, sync::Arc};

use drift_sdk::{
constants::ProgramData,
constants::{ProgramData, BASE_PRECISION},
dlob::DLOBClient,
types::{
Context, MarketId, MarketType, ModifyOrderParams, RpcSendTransactionConfig, SdkError,
Expand All @@ -11,13 +11,14 @@ use drift_sdk::{
};
use futures_util::{stream::FuturesUnordered, StreamExt};
use log::{error, warn};
use rust_decimal::Decimal;
use thiserror::Error;

use crate::types::{
get_market_decimals, AllMarketsResponse, CancelAndPlaceRequest, CancelOrdersRequest,
GetOrderbookRequest, GetOrdersRequest, GetOrdersResponse, GetPositionsRequest,
GetPositionsResponse, Market, ModifyOrdersRequest, Order, OrderbookL2, PlaceOrdersRequest,
SpotPosition, TxResponse,
SolBalanceResponse, SpotPosition, TxResponse,
};

pub type GatewayResult<T> = Result<T, ControllerError>;
Expand Down Expand Up @@ -76,6 +77,19 @@ impl AppState {
}
}

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

/// Cancel orders
///
/// There are 4 intended scenarios for cancellation, in order of priority:
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ async fn get_orderbook(controller: web::Data<AppState>, body: web::Bytes) -> imp
}
}

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

#[actix_web::main]
async fn main() -> std::io::Result<()> {
let config: GatewayConfig = argh::from_env();
Expand Down Expand Up @@ -194,7 +199,8 @@ async fn main() -> std::io::Result<()> {
.service(cancel_orders)
.service(modify_orders)
.service(get_orderbook)
.service(cancel_and_place_orders),
.service(cancel_and_place_orders)
.service(get_sol_balance),
)
})
.bind((config.host, config.port))?
Expand Down
8 changes: 5 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ pub struct PlaceOrder {
#[serde(default)]
reduce_only: bool,
#[serde(default)]
immediate_or_cancel: bool,
#[serde(default)]
oracle_price_offset: Option<Decimal>,
max_ts: Option<i64>,
}
Expand Down Expand Up @@ -310,7 +308,6 @@ impl PlaceOrder {
PositionDirection::Long
},
price,
immediate_or_cancel: self.immediate_or_cancel,
reduce_only: self.reduce_only,
post_only: if self.post_only {
PostOnlyParam::MustPostOnly // this will report the failure to the gateway caller
Expand Down Expand Up @@ -558,6 +555,11 @@ pub(crate) fn get_market_decimals(program_data: &ProgramData, market: Market) ->
}
}

#[derive(Serialize)]
pub struct SolBalanceResponse {
pub balance: Decimal,
}

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

0 comments on commit 2a1e26c

Please sign in to comment.