From 20602cb0860674b50aa59ecb9ebbcc6438c77d46 Mon Sep 17 00:00:00 2001 From: 0xbigz <83473873+0xbigz@users.noreply.github.com> Date: Fri, 7 Apr 2023 18:39:45 -0400 Subject: [PATCH 1/2] bigz/sdk-if-unstake-math --- sdk/src/math/insurance.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/sdk/src/math/insurance.ts b/sdk/src/math/insurance.ts index 79a259660..4ea44775c 100644 --- a/sdk/src/math/insurance.ts +++ b/sdk/src/math/insurance.ts @@ -33,3 +33,32 @@ export function unstakeSharesToAmount( return amount; } + +export function unstakeSharesToAmountWithOpenRequest( + nShares: BN, + withdrawRequestShares: BN, + withdrawRequestAmount: BN, + totalIfShares: BN, + insuranceFundVaultBalance: BN +): BN { + let stakedAmount: BN; + if (totalIfShares.gt(ZERO)) { + stakedAmount = BN.max( + ZERO, + nShares + .sub(withdrawRequestShares) + .mul(insuranceFundVaultBalance) + .div(totalIfShares) + ); + } else { + stakedAmount = ZERO; + } + + const withdrawAmount = BN.min( + withdrawRequestAmount, + withdrawRequestShares.mul(insuranceFundVaultBalance).div(totalIfShares) + ); + const amount = withdrawAmount.add(stakedAmount); + + return amount; +} From 8fa022189fbda3a898e189a0260cd9c1f7a139bb Mon Sep 17 00:00:00 2001 From: 0xbigz <83473873+0xbigz@users.noreply.github.com> Date: Fri, 7 Apr 2023 18:42:17 -0400 Subject: [PATCH 2/2] changelog update --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f4d9f337..1f80fa70d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Features - +- sdk: more accurate calculation of insurance stake value during unstake request ([#426](https://github.com/drift-labs/protocol-v2/pull/426)) ### Fixes ### Breaking