@drift-labs/sdk-browser 2.162.0-beta.1 → 2.162.0-beta.2
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/VERSION +1 -1
- package/lib/browser/math/spotBalance.js +2 -2
- package/lib/browser/math/spotPosition.js +2 -2
- package/lib/node/math/spotBalance.js +2 -2
- package/lib/node/math/spotPosition.d.ts.map +1 -1
- package/lib/node/math/spotPosition.js +2 -2
- package/package.json +1 -1
- package/src/math/spotBalance.ts +2 -2
- package/src/math/spotPosition.ts +5 -2
- package/tests/user/test.ts +54 -0
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.162.0-beta.
|
|
1
|
+
2.162.0-beta.2
|
|
@@ -410,7 +410,7 @@ function getSpotAssetValue(tokenAmount, strictOraclePrice, spotMarketAccount, ma
|
|
|
410
410
|
let weight = calculateAssetWeight(tokenAmount, strictOraclePrice.current, spotMarketAccount, marginCategory);
|
|
411
411
|
if (marginCategory === 'Initial' &&
|
|
412
412
|
spotMarketAccount.marketIndex !== numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
|
|
413
|
-
const userCustomAssetWeight = anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.
|
|
413
|
+
const userCustomAssetWeight = anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.sub(new anchor_1.BN(maxMarginRatio)));
|
|
414
414
|
weight = anchor_1.BN.min(weight, userCustomAssetWeight);
|
|
415
415
|
}
|
|
416
416
|
assetValue = assetValue.mul(weight).div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
@@ -424,7 +424,7 @@ function getSpotLiabilityValue(tokenAmount, strictOraclePrice, spotMarketAccount
|
|
|
424
424
|
let weight = calculateLiabilityWeight(tokenAmount, spotMarketAccount, marginCategory);
|
|
425
425
|
if (marginCategory === 'Initial' &&
|
|
426
426
|
spotMarketAccount.marketIndex !== numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
|
|
427
|
-
weight = anchor_1.BN.max(weight, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.
|
|
427
|
+
weight = anchor_1.BN.max(weight, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.add(new anchor_1.BN(maxMarginRatio)));
|
|
428
428
|
}
|
|
429
429
|
if (liquidationBuffer !== undefined) {
|
|
430
430
|
weight = weight.add(liquidationBuffer);
|
|
@@ -45,8 +45,8 @@ function calculateWeightedTokenValue(tokenAmount, tokenValue, oraclePrice, spotM
|
|
|
45
45
|
customMarginRatio &&
|
|
46
46
|
spotMarket.marketIndex !== numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
|
|
47
47
|
const userCustomAssetWeight = tokenValue.gte(numericConstants_1.ZERO)
|
|
48
|
-
? anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.
|
|
49
|
-
: numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.
|
|
48
|
+
? anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.sub(new anchor_1.BN(customMarginRatio)))
|
|
49
|
+
: numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.add(new anchor_1.BN(customMarginRatio));
|
|
50
50
|
weight = tokenValue.gte(numericConstants_1.ZERO)
|
|
51
51
|
? anchor_1.BN.min(weight, userCustomAssetWeight)
|
|
52
52
|
: anchor_1.BN.max(weight, userCustomAssetWeight);
|
|
@@ -410,7 +410,7 @@ function getSpotAssetValue(tokenAmount, strictOraclePrice, spotMarketAccount, ma
|
|
|
410
410
|
let weight = calculateAssetWeight(tokenAmount, strictOraclePrice.current, spotMarketAccount, marginCategory);
|
|
411
411
|
if (marginCategory === 'Initial' &&
|
|
412
412
|
spotMarketAccount.marketIndex !== numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
|
|
413
|
-
const userCustomAssetWeight = anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.
|
|
413
|
+
const userCustomAssetWeight = anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.sub(new anchor_1.BN(maxMarginRatio)));
|
|
414
414
|
weight = anchor_1.BN.min(weight, userCustomAssetWeight);
|
|
415
415
|
}
|
|
416
416
|
assetValue = assetValue.mul(weight).div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
@@ -424,7 +424,7 @@ function getSpotLiabilityValue(tokenAmount, strictOraclePrice, spotMarketAccount
|
|
|
424
424
|
let weight = calculateLiabilityWeight(tokenAmount, spotMarketAccount, marginCategory);
|
|
425
425
|
if (marginCategory === 'Initial' &&
|
|
426
426
|
spotMarketAccount.marketIndex !== numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
|
|
427
|
-
weight = anchor_1.BN.max(weight, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.
|
|
427
|
+
weight = anchor_1.BN.max(weight, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.add(new anchor_1.BN(maxMarginRatio)));
|
|
428
428
|
}
|
|
429
429
|
if (liquidationBuffer !== undefined) {
|
|
430
430
|
weight = weight.add(liquidationBuffer);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spotPosition.d.ts","sourceRoot":"","sources":["../../../src/math/spotPosition.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAM3E,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AASvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAEjE,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAEvE;AAED,MAAM,MAAM,mBAAmB,GAAG;IACjC,WAAW,EAAE,EAAE,CAAC;IA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AAE,iBAAiB,EACpC,iBAAiB,EAAE,iBAAiB,EACpC,cAAc,EAAE,cAAc,EAC9B,iBAAiB,CAAC,EAAE,MAAM,EAC1B,iBAAiB,GAAE,OAAc,GAC/B,mBAAmB,CAkErB;AAED,wBAAgB,2BAA2B,CAC1C,WAAW,EAAE,EAAE,EACf,UAAU,EAAE,EAAE,EACd,WAAW,EAAE,EAAE,EACf,UAAU,EAAE,iBAAiB,EAC7B,cAAc,EAAE,cAAc,EAC9B,iBAAiB,CAAC,EAAE,MAAM,GACxB;IAAE,MAAM,EAAE,EAAE,CAAC;IAAC,kBAAkB,EAAE,EAAE,CAAA;CAAE,CAwCxC;AAED,wBAAgB,iBAAiB,CAChC,WAAW,EAAE,EAAE,EACf,UAAU,EAAE,EAAE,EACd,UAAU,EAAE,EAAE,EACd,iBAAiB,EAAE,iBAAiB,EACpC,UAAU,EAAE,iBAAiB,EAC7B,cAAc,EAAE,cAAc,EAC9B,iBAAiB,CAAC,EAAE,MAAM,GACxB,mBAAmB,CA4BrB"}
|
|
@@ -45,8 +45,8 @@ function calculateWeightedTokenValue(tokenAmount, tokenValue, oraclePrice, spotM
|
|
|
45
45
|
customMarginRatio &&
|
|
46
46
|
spotMarket.marketIndex !== numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
|
|
47
47
|
const userCustomAssetWeight = tokenValue.gte(numericConstants_1.ZERO)
|
|
48
|
-
? anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.
|
|
49
|
-
: numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.
|
|
48
|
+
? anchor_1.BN.max(numericConstants_1.ZERO, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.sub(new anchor_1.BN(customMarginRatio)))
|
|
49
|
+
: numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.add(new anchor_1.BN(customMarginRatio));
|
|
50
50
|
weight = tokenValue.gte(numericConstants_1.ZERO)
|
|
51
51
|
? anchor_1.BN.min(weight, userCustomAssetWeight)
|
|
52
52
|
: anchor_1.BN.max(weight, userCustomAssetWeight);
|
package/package.json
CHANGED
package/src/math/spotBalance.ts
CHANGED
|
@@ -697,7 +697,7 @@ export function getSpotAssetValue(
|
|
|
697
697
|
) {
|
|
698
698
|
const userCustomAssetWeight = BN.max(
|
|
699
699
|
ZERO,
|
|
700
|
-
SPOT_MARKET_WEIGHT_PRECISION.
|
|
700
|
+
SPOT_MARKET_WEIGHT_PRECISION.sub(new BN(maxMarginRatio))
|
|
701
701
|
);
|
|
702
702
|
weight = BN.min(weight, userCustomAssetWeight);
|
|
703
703
|
}
|
|
@@ -735,7 +735,7 @@ export function getSpotLiabilityValue(
|
|
|
735
735
|
) {
|
|
736
736
|
weight = BN.max(
|
|
737
737
|
weight,
|
|
738
|
-
SPOT_MARKET_WEIGHT_PRECISION.
|
|
738
|
+
SPOT_MARKET_WEIGHT_PRECISION.add(new BN(maxMarginRatio))
|
|
739
739
|
);
|
|
740
740
|
}
|
|
741
741
|
|
package/src/math/spotPosition.ts
CHANGED
|
@@ -133,8 +133,11 @@ export function calculateWeightedTokenValue(
|
|
|
133
133
|
spotMarket.marketIndex !== QUOTE_SPOT_MARKET_INDEX
|
|
134
134
|
) {
|
|
135
135
|
const userCustomAssetWeight = tokenValue.gte(ZERO)
|
|
136
|
-
? BN.max(
|
|
137
|
-
|
|
136
|
+
? BN.max(
|
|
137
|
+
ZERO,
|
|
138
|
+
SPOT_MARKET_WEIGHT_PRECISION.sub(new BN(customMarginRatio))
|
|
139
|
+
)
|
|
140
|
+
: SPOT_MARKET_WEIGHT_PRECISION.add(new BN(customMarginRatio));
|
|
138
141
|
|
|
139
142
|
weight = tokenValue.gte(ZERO)
|
|
140
143
|
? BN.min(weight, userCustomAssetWeight)
|
package/tests/user/test.ts
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
|
|
19
19
|
SpotBalanceType,
|
|
20
20
|
MARGIN_PRECISION,
|
|
21
|
+
getSpotAssetValue,
|
|
21
22
|
} from '../../src';
|
|
22
23
|
import { MockUserMap, mockPerpMarkets, mockSpotMarkets } from '../dlob/helpers';
|
|
23
24
|
import { assert } from '../../src/assert/assert';
|
|
@@ -458,6 +459,59 @@ describe('User Tests', () => {
|
|
|
458
459
|
|
|
459
460
|
console.log(worstCaseAfter);
|
|
460
461
|
assert(worstCaseAfter.weight.eq(new BN(0))); // not allowed to increase exposure
|
|
462
|
+
|
|
463
|
+
// customMarginRatio > SPOT weight precision must not throw (BN subn asserted non-negative)
|
|
464
|
+
const depositOnlyNonQuote = Object.assign(
|
|
465
|
+
{},
|
|
466
|
+
myMockUserAccount.spotPositions[1],
|
|
467
|
+
{
|
|
468
|
+
marketIndex: 1,
|
|
469
|
+
scaledBalance: new BN(100).mul(SPOT_MARKET_BALANCE_PRECISION),
|
|
470
|
+
openBids: ZERO,
|
|
471
|
+
openAsks: ZERO,
|
|
472
|
+
}
|
|
473
|
+
);
|
|
474
|
+
for (const ratio of [
|
|
475
|
+
MARGIN_PRECISION.toNumber() * 2,
|
|
476
|
+
MARGIN_PRECISION.toNumber() * 10,
|
|
477
|
+
89478485,
|
|
478
|
+
]) {
|
|
479
|
+
const wc = getWorstCaseTokenAmounts(
|
|
480
|
+
depositOnlyNonQuote,
|
|
481
|
+
solMarket,
|
|
482
|
+
strictOraclePrice,
|
|
483
|
+
'Initial',
|
|
484
|
+
ratio
|
|
485
|
+
);
|
|
486
|
+
assert(wc.weight.eq(new BN(0)));
|
|
487
|
+
assert(wc.weightedTokenValue.eq(ZERO));
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it('getSpotAssetValue: large maxMarginRatio does not throw (matches getFreeCollateral path)', () => {
|
|
492
|
+
const solMarket = Object.assign({}, _.cloneDeep(mockSpotMarkets[1]), {
|
|
493
|
+
marketIndex: 1,
|
|
494
|
+
initialAssetWeight: 8000,
|
|
495
|
+
initialLiabilityWeight: 12000,
|
|
496
|
+
cumulativeDepositInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
|
|
497
|
+
cumulativeBorrowInterest: SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
|
|
498
|
+
});
|
|
499
|
+
const strictOraclePrice = new StrictOraclePrice(PRICE_PRECISION.muln(25));
|
|
500
|
+
const tokenAmount = new BN(100).mul(LAMPORTS_PRECISION);
|
|
501
|
+
|
|
502
|
+
for (const ratio of [
|
|
503
|
+
MARGIN_PRECISION.toNumber() * 2,
|
|
504
|
+
89478485,
|
|
505
|
+
]) {
|
|
506
|
+
const assetValue = getSpotAssetValue(
|
|
507
|
+
tokenAmount,
|
|
508
|
+
strictOraclePrice,
|
|
509
|
+
solMarket,
|
|
510
|
+
ratio,
|
|
511
|
+
'Initial'
|
|
512
|
+
);
|
|
513
|
+
assert(assetValue.eq(ZERO));
|
|
514
|
+
}
|
|
461
515
|
});
|
|
462
516
|
|
|
463
517
|
it('custom margin ratio (sol perp)', async () => {
|