Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/liquidation price #39

Merged
merged 5 commits into from
Mar 1, 2024
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
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
Loading