Skip to content

Commit

Permalink
Fix/liquidation price (drift-labs#39)
Browse files Browse the repository at this point in the history
* use updated liquidation price
* bump sdk version
* add readme note
* CI: use lockfile in cache restore key
  • Loading branch information
jordy25519 authored Mar 1, 2024
1 parent 3aea6a8 commit 168284a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ jobs:
continue-on-error: false
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
restore-keys: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Format
run: cargo fmt --all -- --check
- name: Build
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ get extended position info for perps positions
$ curl localhost:8080/v2/positionInfo/0
```

note: unrealized PnL is based on the oracle price at time of query

**Response**
```json
{
Expand Down
31 changes: 15 additions & 16 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use drift_sdk::{
dlob::DLOBClient,
event_subscriber::try_parse_log,
event_subscriber::CommitmentConfig,
liquidation::calculate_liquidation_price,
liquidation::calculate_liquidation_price_and_unrealized_pnl,
types::{
Context, MarketId, MarketType, ModifyOrderParams, RpcSendTransactionConfig, SdkError,
SdkResult, VersionedMessage,
Expand Down Expand Up @@ -208,23 +208,22 @@ impl AppState {
market: Market,
) -> GatewayResult<PerpPosition> {
let sub_account = self.resolve_sub_account(sub_account_id);
if let Some(perp_position) = self
.client
.perp_position(&sub_account, market.market_index)
.await?
{
let (oracle_price, user) = tokio::join!(
self.client.oracle_price(market.as_market_id()),
self.client.get_user_account(&sub_account)
);

let unrealized_pnl = perp_position.get_unrealized_pnl(oracle_price?);
let liquidation_price =
calculate_liquidation_price(&self.client, &user?, market.market_index).await?;
let (perp_position, user) = tokio::join!(
self.client.perp_position(&sub_account, market.market_index),
self.client.get_user_account(&sub_account),
);

if let Some(perp_position) = perp_position? {
let result = calculate_liquidation_price_and_unrealized_pnl(
&self.client,
&user?,
market.market_index,
)
.await?;
let mut p: PerpPosition = perp_position.into();
p.set_extended_info(PerpPositionExtended {
liquidation_price: Decimal::new(liquidation_price, PRICE_DECIMALS),
unrealized_pnl: Decimal::new(unrealized_pnl.unwrap() as i64, PRICE_DECIMALS),
liquidation_price: Decimal::new(result.liquidation_price, PRICE_DECIMALS),
unrealized_pnl: Decimal::new(result.unrealized_pnl as i64, PRICE_DECIMALS),
});

Ok(p)
Expand Down

0 comments on commit 168284a

Please sign in to comment.