@drift-labs/sdk-browser 2.162.0-beta.0 → 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 CHANGED
@@ -1 +1 @@
1
- 2.162.0-beta.0
1
+ 2.162.0-beta.2
@@ -15693,6 +15693,66 @@
15693
15693
  ]
15694
15694
  }
15695
15695
  },
15696
+ {
15697
+ "name": "MarginTypeConfig",
15698
+ "type": {
15699
+ "kind": "enum",
15700
+ "variants": [
15701
+ {
15702
+ "name": "Default",
15703
+ "fields": [
15704
+ {
15705
+ "defined": "MarginRequirementType"
15706
+ }
15707
+ ]
15708
+ },
15709
+ {
15710
+ "name": "IsolatedPositionOverride",
15711
+ "fields": [
15712
+ {
15713
+ "name": "marketIndex",
15714
+ "type": "u16"
15715
+ },
15716
+ {
15717
+ "name": "marginRequirementType",
15718
+ "type": {
15719
+ "defined": "MarginRequirementType"
15720
+ }
15721
+ },
15722
+ {
15723
+ "name": "defaultIsolatedMarginRequirementType",
15724
+ "type": {
15725
+ "defined": "MarginRequirementType"
15726
+ }
15727
+ },
15728
+ {
15729
+ "name": "crossMarginRequirementType",
15730
+ "type": {
15731
+ "defined": "MarginRequirementType"
15732
+ }
15733
+ }
15734
+ ]
15735
+ },
15736
+ {
15737
+ "name": "CrossMarginOverride",
15738
+ "fields": [
15739
+ {
15740
+ "name": "marginRequirementType",
15741
+ "type": {
15742
+ "defined": "MarginRequirementType"
15743
+ }
15744
+ },
15745
+ {
15746
+ "name": "defaultMarginRequirementType",
15747
+ "type": {
15748
+ "defined": "MarginRequirementType"
15749
+ }
15750
+ }
15751
+ ]
15752
+ }
15753
+ ]
15754
+ }
15755
+ },
15696
15756
  {
15697
15757
  "name": "OracleSource",
15698
15758
  "type": {
@@ -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.subn(maxMarginRatio));
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.addn(maxMarginRatio));
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.subn(customMarginRatio))
49
- : numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.addn(customMarginRatio);
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);
@@ -15693,6 +15693,66 @@
15693
15693
  ]
15694
15694
  }
15695
15695
  },
15696
+ {
15697
+ "name": "MarginTypeConfig",
15698
+ "type": {
15699
+ "kind": "enum",
15700
+ "variants": [
15701
+ {
15702
+ "name": "Default",
15703
+ "fields": [
15704
+ {
15705
+ "defined": "MarginRequirementType"
15706
+ }
15707
+ ]
15708
+ },
15709
+ {
15710
+ "name": "IsolatedPositionOverride",
15711
+ "fields": [
15712
+ {
15713
+ "name": "marketIndex",
15714
+ "type": "u16"
15715
+ },
15716
+ {
15717
+ "name": "marginRequirementType",
15718
+ "type": {
15719
+ "defined": "MarginRequirementType"
15720
+ }
15721
+ },
15722
+ {
15723
+ "name": "defaultIsolatedMarginRequirementType",
15724
+ "type": {
15725
+ "defined": "MarginRequirementType"
15726
+ }
15727
+ },
15728
+ {
15729
+ "name": "crossMarginRequirementType",
15730
+ "type": {
15731
+ "defined": "MarginRequirementType"
15732
+ }
15733
+ }
15734
+ ]
15735
+ },
15736
+ {
15737
+ "name": "CrossMarginOverride",
15738
+ "fields": [
15739
+ {
15740
+ "name": "marginRequirementType",
15741
+ "type": {
15742
+ "defined": "MarginRequirementType"
15743
+ }
15744
+ },
15745
+ {
15746
+ "name": "defaultMarginRequirementType",
15747
+ "type": {
15748
+ "defined": "MarginRequirementType"
15749
+ }
15750
+ }
15751
+ ]
15752
+ }
15753
+ ]
15754
+ }
15755
+ },
15696
15756
  {
15697
15757
  "name": "OracleSource",
15698
15758
  "type": {
@@ -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.subn(maxMarginRatio));
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.addn(maxMarginRatio));
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,E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CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAEvE;AAED,MAAM,MAAM,mBAAmB,GAAG;IACjC,WAAW,EAAE,EAAE,CAAC;IAChB,WAAW,EAAE,EAAE,CAAC;IAChB,UAAU,EAAE,EAAE,CAAC;IACf,MAAM,EAAE,EAAE,CAAC;IACX,kBAAkB,EAAE,EAAE,CAAC;IACvB,0BAA0B,MAAC;CAC3B,CAAC;AAEF,wBAAgB,wBAAwB,CACvC,YAAY,EAAE,YAAY,EAC1B,iBAAiB,EAAE,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.subn(customMarginRatio))
49
- : numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.addn(customMarginRatio);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk-browser",
3
- "version": "2.162.0-beta.0",
3
+ "version": "2.162.0-beta.2",
4
4
  "main": "lib/node/index.js",
5
5
  "types": "lib/node/index.d.ts",
6
6
  "module": "./lib/browser/index.js",
@@ -15693,6 +15693,66 @@
15693
15693
  ]
15694
15694
  }
15695
15695
  },
15696
+ {
15697
+ "name": "MarginTypeConfig",
15698
+ "type": {
15699
+ "kind": "enum",
15700
+ "variants": [
15701
+ {
15702
+ "name": "Default",
15703
+ "fields": [
15704
+ {
15705
+ "defined": "MarginRequirementType"
15706
+ }
15707
+ ]
15708
+ },
15709
+ {
15710
+ "name": "IsolatedPositionOverride",
15711
+ "fields": [
15712
+ {
15713
+ "name": "marketIndex",
15714
+ "type": "u16"
15715
+ },
15716
+ {
15717
+ "name": "marginRequirementType",
15718
+ "type": {
15719
+ "defined": "MarginRequirementType"
15720
+ }
15721
+ },
15722
+ {
15723
+ "name": "defaultIsolatedMarginRequirementType",
15724
+ "type": {
15725
+ "defined": "MarginRequirementType"
15726
+ }
15727
+ },
15728
+ {
15729
+ "name": "crossMarginRequirementType",
15730
+ "type": {
15731
+ "defined": "MarginRequirementType"
15732
+ }
15733
+ }
15734
+ ]
15735
+ },
15736
+ {
15737
+ "name": "CrossMarginOverride",
15738
+ "fields": [
15739
+ {
15740
+ "name": "marginRequirementType",
15741
+ "type": {
15742
+ "defined": "MarginRequirementType"
15743
+ }
15744
+ },
15745
+ {
15746
+ "name": "defaultMarginRequirementType",
15747
+ "type": {
15748
+ "defined": "MarginRequirementType"
15749
+ }
15750
+ }
15751
+ ]
15752
+ }
15753
+ ]
15754
+ }
15755
+ },
15696
15756
  {
15697
15757
  "name": "OracleSource",
15698
15758
  "type": {
@@ -697,7 +697,7 @@ export function getSpotAssetValue(
697
697
  ) {
698
698
  const userCustomAssetWeight = BN.max(
699
699
  ZERO,
700
- SPOT_MARKET_WEIGHT_PRECISION.subn(maxMarginRatio)
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.addn(maxMarginRatio)
738
+ SPOT_MARKET_WEIGHT_PRECISION.add(new BN(maxMarginRatio))
739
739
  );
740
740
  }
741
741
 
@@ -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(ZERO, SPOT_MARKET_WEIGHT_PRECISION.subn(customMarginRatio))
137
- : SPOT_MARKET_WEIGHT_PRECISION.addn(customMarginRatio);
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)
@@ -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 () => {