@drift-labs/sdk 2.15.0-beta.0 → 2.16.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/constants/perpMarkets.js +10 -0
- package/lib/events/fetchLogs.js +4 -4
- package/lib/idl/drift.json +1 -1
- package/lib/math/market.d.ts +1 -0
- package/lib/math/market.js +14 -3
- package/lib/math/spotBalance.d.ts +1 -1
- package/lib/math/spotBalance.js +8 -8
- package/lib/math/spotMarket.d.ts +2 -1
- package/lib/math/spotMarket.js +15 -1
- package/lib/math/spotPosition.js +3 -3
- package/lib/user.d.ts +41 -10
- package/lib/user.js +229 -95
- package/package.json +1 -1
- package/src/constants/perpMarkets.ts +10 -0
- package/src/events/fetchLogs.ts +4 -4
- package/src/idl/drift.json +1 -1
- package/src/math/market.ts +25 -2
- package/src/math/spotBalance.ts +8 -8
- package/src/math/spotMarket.ts +27 -1
- package/src/math/spotPosition.ts +3 -3
- package/src/user.ts +544 -234
package/src/math/spotMarket.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
isVariant,
|
|
4
|
+
MarginCategory,
|
|
5
|
+
SpotBalanceType,
|
|
6
|
+
SpotMarketAccount,
|
|
7
|
+
} from '../types';
|
|
8
|
+
import { calculateAssetWeight, calculateLiabilityWeight } from './spotBalance';
|
|
9
|
+
import { MARGIN_PRECISION } from '../constants/numericConstants';
|
|
3
10
|
|
|
4
11
|
export function castNumberToSpotPrecision(
|
|
5
12
|
value: number,
|
|
@@ -7,3 +14,22 @@ export function castNumberToSpotPrecision(
|
|
|
7
14
|
): BN {
|
|
8
15
|
return new BN(value * Math.pow(10, spotMarket.decimals));
|
|
9
16
|
}
|
|
17
|
+
|
|
18
|
+
export function calculateSpotMarketMarginRatio(
|
|
19
|
+
market: SpotMarketAccount,
|
|
20
|
+
marginCategory: MarginCategory,
|
|
21
|
+
size: BN,
|
|
22
|
+
balanceType: SpotBalanceType
|
|
23
|
+
): number {
|
|
24
|
+
if (isVariant(balanceType, 'deposit')) {
|
|
25
|
+
const assetWeight = calculateAssetWeight(size, market, marginCategory);
|
|
26
|
+
return MARGIN_PRECISION.sub(assetWeight).toNumber();
|
|
27
|
+
} else {
|
|
28
|
+
const liabilityWeight = calculateLiabilityWeight(
|
|
29
|
+
size,
|
|
30
|
+
market,
|
|
31
|
+
marginCategory
|
|
32
|
+
);
|
|
33
|
+
return liabilityWeight.sub(MARGIN_PRECISION).toNumber();
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/math/spotPosition.ts
CHANGED
|
@@ -29,16 +29,16 @@ export function getWorstCaseTokenAmounts(
|
|
|
29
29
|
const tokenAmountAllBidsFill = tokenAmount.add(spotPosition.openBids);
|
|
30
30
|
const tokenAmountAllAsksFill = tokenAmount.add(spotPosition.openAsks);
|
|
31
31
|
|
|
32
|
-
if (
|
|
32
|
+
if (tokenAmountAllBidsFill.abs().gt(tokenAmountAllAsksFill.abs())) {
|
|
33
33
|
const worstCaseQuoteTokenAmount = getTokenValue(
|
|
34
|
-
spotPosition.
|
|
34
|
+
spotPosition.openBids.neg(),
|
|
35
35
|
spotMarketAccount.decimals,
|
|
36
36
|
oraclePriceData
|
|
37
37
|
);
|
|
38
38
|
return [tokenAmountAllBidsFill, worstCaseQuoteTokenAmount];
|
|
39
39
|
} else {
|
|
40
40
|
const worstCaseQuoteTokenAmount = getTokenValue(
|
|
41
|
-
spotPosition.
|
|
41
|
+
spotPosition.openAsks.neg(),
|
|
42
42
|
spotMarketAccount.decimals,
|
|
43
43
|
oraclePriceData
|
|
44
44
|
);
|