@drift-labs/sdk 2.143.0-beta.7 → 2.143.0-beta.8
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/margin.d.ts +2 -1
- package/lib/browser/math/margin.js +31 -3
- package/lib/browser/math/market.js +38 -18
- package/lib/node/math/margin.d.ts +2 -1
- package/lib/node/math/margin.d.ts.map +1 -1
- package/lib/node/math/margin.js +31 -3
- package/lib/node/math/market.d.ts.map +1 -1
- package/lib/node/math/market.js +38 -18
- package/package.json +1 -1
- package/src/math/margin.ts +44 -5
- package/src/math/market.ts +61 -31
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.143.0-beta.
|
|
1
|
+
2.143.0-beta.8
|
|
@@ -5,7 +5,7 @@ import { DriftClient } from '../driftClient';
|
|
|
5
5
|
import { PerpMarketAccount, PerpPosition, PositionDirection, UserAccount } from '../types';
|
|
6
6
|
import { PublicKey } from '@solana/web3.js';
|
|
7
7
|
export declare function calculateSizePremiumLiabilityWeight(size: BN, // AMM_RESERVE_PRECISION
|
|
8
|
-
imfFactor: BN, liabilityWeight: BN, precision: BN): BN;
|
|
8
|
+
imfFactor: BN, liabilityWeight: BN, precision: BN, isBounded?: boolean): BN;
|
|
9
9
|
export declare function calculateSizeDiscountAssetWeight(size: BN, // AMM_RESERVE_PRECISION
|
|
10
10
|
imfFactor: BN, assetWeight: BN): BN;
|
|
11
11
|
export declare function calculateOraclePriceForPerpMargin(perpPosition: PerpPosition, market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
@@ -43,3 +43,4 @@ export declare function calculateUserMaxPerpOrderSize(driftClient: DriftClient,
|
|
|
43
43
|
tradeSize: BN;
|
|
44
44
|
oppositeSideTradeSize: BN;
|
|
45
45
|
};
|
|
46
|
+
export declare function calcHighLeverageModeInitialMarginRatioFromSize(preSizeAdjMarginRatio: BN, sizeAdjMarginRatio: BN, defaultMarginRatio: BN): BN;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateUserMaxPerpOrderSize = exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
|
|
3
|
+
exports.calcHighLeverageModeInitialMarginRatioFromSize = exports.calculateUserMaxPerpOrderSize = exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
|
|
4
4
|
const utils_1 = require("./utils");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
@@ -11,7 +11,7 @@ const user_1 = require("../user");
|
|
|
11
11
|
const types_1 = require("../types");
|
|
12
12
|
const assert_1 = require("../assert/assert");
|
|
13
13
|
function calculateSizePremiumLiabilityWeight(size, // AMM_RESERVE_PRECISION
|
|
14
|
-
imfFactor, liabilityWeight, precision) {
|
|
14
|
+
imfFactor, liabilityWeight, precision, isBounded = true) {
|
|
15
15
|
if (imfFactor.eq(numericConstants_1.ZERO)) {
|
|
16
16
|
return liabilityWeight;
|
|
17
17
|
}
|
|
@@ -23,7 +23,13 @@ imfFactor, liabilityWeight, precision) {
|
|
|
23
23
|
.mul(imfFactor)
|
|
24
24
|
.div(denom) // 1e5
|
|
25
25
|
);
|
|
26
|
-
|
|
26
|
+
let maxLiabilityWeight;
|
|
27
|
+
if (isBounded) {
|
|
28
|
+
maxLiabilityWeight = anchor_1.BN.max(liabilityWeight, sizePremiumLiabilityWeight);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
maxLiabilityWeight = sizePremiumLiabilityWeight;
|
|
32
|
+
}
|
|
27
33
|
return maxLiabilityWeight;
|
|
28
34
|
}
|
|
29
35
|
exports.calculateSizePremiumLiabilityWeight = calculateSizePremiumLiabilityWeight;
|
|
@@ -199,3 +205,25 @@ function calculateUserMaxPerpOrderSize(driftClient, userAccountKey, userAccount,
|
|
|
199
205
|
return user.getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide);
|
|
200
206
|
}
|
|
201
207
|
exports.calculateUserMaxPerpOrderSize = calculateUserMaxPerpOrderSize;
|
|
208
|
+
function calcHighLeverageModeInitialMarginRatioFromSize(preSizeAdjMarginRatio, sizeAdjMarginRatio, defaultMarginRatio) {
|
|
209
|
+
let result;
|
|
210
|
+
if (sizeAdjMarginRatio.lt(preSizeAdjMarginRatio)) {
|
|
211
|
+
const sizePctDiscountFactor = numericConstants_1.PERCENTAGE_PRECISION.sub(preSizeAdjMarginRatio
|
|
212
|
+
.sub(sizeAdjMarginRatio)
|
|
213
|
+
.mul(numericConstants_1.PERCENTAGE_PRECISION)
|
|
214
|
+
.div(preSizeAdjMarginRatio.div(new anchor_1.BN(5))));
|
|
215
|
+
const hlmMarginDelta = anchor_1.BN.max(preSizeAdjMarginRatio.sub(defaultMarginRatio), new anchor_1.BN(1));
|
|
216
|
+
const hlmMarginDeltaProportion = hlmMarginDelta
|
|
217
|
+
.mul(sizePctDiscountFactor)
|
|
218
|
+
.div(numericConstants_1.PERCENTAGE_PRECISION);
|
|
219
|
+
result = hlmMarginDeltaProportion.add(defaultMarginRatio);
|
|
220
|
+
}
|
|
221
|
+
else if (sizeAdjMarginRatio.eq(preSizeAdjMarginRatio)) {
|
|
222
|
+
result = defaultMarginRatio;
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
result = sizeAdjMarginRatio;
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
exports.calcHighLeverageModeInitialMarginRatioFromSize = calcHighLeverageModeInitialMarginRatioFromSize;
|
|
@@ -61,29 +61,49 @@ function calculateOracleSpread(price, oraclePriceData) {
|
|
|
61
61
|
}
|
|
62
62
|
exports.calculateOracleSpread = calculateOracleSpread;
|
|
63
63
|
function calculateMarketMarginRatio(market, size, marginCategory, customMarginRatio = 0, userHighLeverageMode = false) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
if (market.status === 'Settlement')
|
|
65
|
+
return 0;
|
|
66
|
+
const isHighLeverageUser = userHighLeverageMode &&
|
|
67
67
|
market.highLeverageMarginRatioInitial > 0 &&
|
|
68
|
-
market.highLeverageMarginRatioMaintenance
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
let marginRatio;
|
|
68
|
+
market.highLeverageMarginRatioMaintenance > 0;
|
|
69
|
+
const marginRatioInitial = isHighLeverageUser
|
|
70
|
+
? market.highLeverageMarginRatioInitial
|
|
71
|
+
: market.marginRatioInitial;
|
|
72
|
+
const marginRatioMaintenance = isHighLeverageUser
|
|
73
|
+
? market.highLeverageMarginRatioMaintenance
|
|
74
|
+
: market.marginRatioMaintenance;
|
|
75
|
+
let defaultMarginRatio;
|
|
77
76
|
switch (marginCategory) {
|
|
78
|
-
case 'Initial':
|
|
79
|
-
|
|
80
|
-
marginRatio = Math.max((0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRationInitial), numericConstants_1.MARGIN_PRECISION).toNumber(), customMarginRatio);
|
|
77
|
+
case 'Initial':
|
|
78
|
+
defaultMarginRatio = marginRatioInitial;
|
|
81
79
|
break;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
80
|
+
case 'Maintenance':
|
|
81
|
+
defaultMarginRatio = marginRatioMaintenance;
|
|
85
82
|
break;
|
|
83
|
+
default:
|
|
84
|
+
throw new Error('Invalid margin category');
|
|
85
|
+
}
|
|
86
|
+
let marginRatio;
|
|
87
|
+
if (isHighLeverageUser && marginCategory !== 'Maintenance') {
|
|
88
|
+
// Use ordinary-mode initial/fill ratios for size-adjusted calc
|
|
89
|
+
let preSizeAdjMarginRatio;
|
|
90
|
+
switch (marginCategory) {
|
|
91
|
+
case 'Initial':
|
|
92
|
+
preSizeAdjMarginRatio = market.marginRatioInitial;
|
|
93
|
+
break;
|
|
94
|
+
default:
|
|
95
|
+
preSizeAdjMarginRatio = marginRatioMaintenance;
|
|
96
|
+
break;
|
|
86
97
|
}
|
|
98
|
+
const sizeAdjMarginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(preSizeAdjMarginRatio), numericConstants_1.MARGIN_PRECISION, false).toNumber();
|
|
99
|
+
marginRatio = (0, margin_1.calcHighLeverageModeInitialMarginRatioFromSize)(new anchor_1.BN(preSizeAdjMarginRatio), new anchor_1.BN(sizeAdjMarginRatio), new anchor_1.BN(defaultMarginRatio)).toNumber();
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const sizeAdjMarginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(defaultMarginRatio), numericConstants_1.MARGIN_PRECISION, true).toNumber();
|
|
103
|
+
marginRatio = Math.max(defaultMarginRatio, sizeAdjMarginRatio);
|
|
104
|
+
}
|
|
105
|
+
if (marginCategory === 'Initial') {
|
|
106
|
+
marginRatio = Math.max(marginRatio, customMarginRatio);
|
|
87
107
|
}
|
|
88
108
|
return marginRatio;
|
|
89
109
|
}
|
|
@@ -5,7 +5,7 @@ import { DriftClient } from '../driftClient';
|
|
|
5
5
|
import { PerpMarketAccount, PerpPosition, PositionDirection, UserAccount } from '../types';
|
|
6
6
|
import { PublicKey } from '@solana/web3.js';
|
|
7
7
|
export declare function calculateSizePremiumLiabilityWeight(size: BN, // AMM_RESERVE_PRECISION
|
|
8
|
-
imfFactor: BN, liabilityWeight: BN, precision: BN): BN;
|
|
8
|
+
imfFactor: BN, liabilityWeight: BN, precision: BN, isBounded?: boolean): BN;
|
|
9
9
|
export declare function calculateSizeDiscountAssetWeight(size: BN, // AMM_RESERVE_PRECISION
|
|
10
10
|
imfFactor: BN, assetWeight: BN): BN;
|
|
11
11
|
export declare function calculateOraclePriceForPerpMargin(perpPosition: PerpPosition, market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
@@ -43,4 +43,5 @@ export declare function calculateUserMaxPerpOrderSize(driftClient: DriftClient,
|
|
|
43
43
|
tradeSize: BN;
|
|
44
44
|
oppositeSideTradeSize: BN;
|
|
45
45
|
};
|
|
46
|
+
export declare function calcHighLeverageModeInitialMarginRatioFromSize(preSizeAdjMarginRatio: BN, sizeAdjMarginRatio: BN, defaultMarginRatio: BN): BN;
|
|
46
47
|
//# sourceMappingURL=margin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"margin.d.ts","sourceRoot":"","sources":["../../../src/math/margin.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"margin.d.ts","sourceRoot":"","sources":["../../../src/math/margin.ts"],"names":[],"mappings":";AAcA,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAGnD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C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,MAAM,EACzB,QAAQ,EAAE,EAAE,EACZ,kBAAkB,CAAC,EAAE,MAAM,EAC3B,oBAAoB,CAAC,EAAE,OAAO,EAC9B,UAAU,CAAC,EAAE,EAAE,GACb,EAAE,CA0BJ;AAED;;;;GAIG;AACH,wBAAgB,0CAA0C,CACzD,WAAW,EAAE,WAAW,EACxB,iBAAiB,EAAE,MAAM,EACzB,QAAQ,EAAE,EAAE,EACZ,eAAe,EAAE,MAAM,EACvB,kBAAkB,CAAC,EAAE,MAAM,EAC3B,oBAAoB,CAAC,EAAE,OAAO,EAC9B,aAAa,CAAC,EAAE,EAAE,GAChB,EAAE,CAgCJ;AAED,wBAAgB,iCAAiC,CAChD,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,MAAM,EACvB,QAAQ,EAAE,EAAE,GACV,EAAE,CAuBJ;AAED,wBAAgB,yBAAyB,CACxC,cAAc,EAAE,EAAE,EAClB,mBAAmB,EAAE,EAAE,EACvB,WAAW,EAAE,EAAE,GACb,EAAE,CAYJ;AAED,wBAAgB,6BAA6B,CAC5C,WAAW,EAAE,WAAW,EACxB,cAAc,EAAE,SAAS,EACzB,WAAW,EAAE,WAAW,EACxB,iBAAiB,EAAE,MAAM,EACzB,SAAS,EAAE,iBAAiB,GAC1B;IAAE,SAAS,EAAE,EAAE,CAAC;IAAC,qBAAqB,EAAE,EAAE,CAAA;CAAE,CAmB9C;AAED,wBAAgB,8CAA8C,CAC7D,qBAAqB,EAAE,EAAE,EACzB,kBAAkB,EAAE,EAAE,EACtB,kBAAkB,EAAE,EAAE,GACpB,EAAE,CA4BJ"}
|
package/lib/node/math/margin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateUserMaxPerpOrderSize = exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
|
|
3
|
+
exports.calcHighLeverageModeInitialMarginRatioFromSize = exports.calculateUserMaxPerpOrderSize = exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
|
|
4
4
|
const utils_1 = require("./utils");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
@@ -11,7 +11,7 @@ const user_1 = require("../user");
|
|
|
11
11
|
const types_1 = require("../types");
|
|
12
12
|
const assert_1 = require("../assert/assert");
|
|
13
13
|
function calculateSizePremiumLiabilityWeight(size, // AMM_RESERVE_PRECISION
|
|
14
|
-
imfFactor, liabilityWeight, precision) {
|
|
14
|
+
imfFactor, liabilityWeight, precision, isBounded = true) {
|
|
15
15
|
if (imfFactor.eq(numericConstants_1.ZERO)) {
|
|
16
16
|
return liabilityWeight;
|
|
17
17
|
}
|
|
@@ -23,7 +23,13 @@ imfFactor, liabilityWeight, precision) {
|
|
|
23
23
|
.mul(imfFactor)
|
|
24
24
|
.div(denom) // 1e5
|
|
25
25
|
);
|
|
26
|
-
|
|
26
|
+
let maxLiabilityWeight;
|
|
27
|
+
if (isBounded) {
|
|
28
|
+
maxLiabilityWeight = anchor_1.BN.max(liabilityWeight, sizePremiumLiabilityWeight);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
maxLiabilityWeight = sizePremiumLiabilityWeight;
|
|
32
|
+
}
|
|
27
33
|
return maxLiabilityWeight;
|
|
28
34
|
}
|
|
29
35
|
exports.calculateSizePremiumLiabilityWeight = calculateSizePremiumLiabilityWeight;
|
|
@@ -199,3 +205,25 @@ function calculateUserMaxPerpOrderSize(driftClient, userAccountKey, userAccount,
|
|
|
199
205
|
return user.getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide);
|
|
200
206
|
}
|
|
201
207
|
exports.calculateUserMaxPerpOrderSize = calculateUserMaxPerpOrderSize;
|
|
208
|
+
function calcHighLeverageModeInitialMarginRatioFromSize(preSizeAdjMarginRatio, sizeAdjMarginRatio, defaultMarginRatio) {
|
|
209
|
+
let result;
|
|
210
|
+
if (sizeAdjMarginRatio.lt(preSizeAdjMarginRatio)) {
|
|
211
|
+
const sizePctDiscountFactor = numericConstants_1.PERCENTAGE_PRECISION.sub(preSizeAdjMarginRatio
|
|
212
|
+
.sub(sizeAdjMarginRatio)
|
|
213
|
+
.mul(numericConstants_1.PERCENTAGE_PRECISION)
|
|
214
|
+
.div(preSizeAdjMarginRatio.div(new anchor_1.BN(5))));
|
|
215
|
+
const hlmMarginDelta = anchor_1.BN.max(preSizeAdjMarginRatio.sub(defaultMarginRatio), new anchor_1.BN(1));
|
|
216
|
+
const hlmMarginDeltaProportion = hlmMarginDelta
|
|
217
|
+
.mul(sizePctDiscountFactor)
|
|
218
|
+
.div(numericConstants_1.PERCENTAGE_PRECISION);
|
|
219
|
+
result = hlmMarginDeltaProportion.add(defaultMarginRatio);
|
|
220
|
+
}
|
|
221
|
+
else if (sizeAdjMarginRatio.eq(preSizeAdjMarginRatio)) {
|
|
222
|
+
result = defaultMarginRatio;
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
result = sizeAdjMarginRatio;
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
exports.calcHighLeverageModeInitialMarginRatioFromSize = calcHighLeverageModeInitialMarginRatioFromSize;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"market.d.ts","sourceRoot":"","sources":["../../../src/math/market.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AACvC,OAAO,EACN,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EAIjB,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"market.d.ts","sourceRoot":"","sources":["../../../src/math/market.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AACvC,OAAO,EACN,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EAIjB,MAAM,UAAU,CAAC;AAclB,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAYtE,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAGpC;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACpC,MAAM,EAAE,iBAAiB,EACzB,iBAAiB,EAAE,iBAAiB,GAClC,EAAE,CAOJ;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,iBAAiB,EACzB,iBAAiB,EAAE,iBAAiB,EACpC,UAAU,CAAC,EAAE,EAAE,GACb,EAAE,CAWJ;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,iBAAiB,EACzB,iBAAiB,EAAE,iBAAiB,EACpC,UAAU,CAAC,EAAE,EAAE,GACb,EAAE,CAWJ;AAED,wBAAgB,4BAA4B,CAC3C,eAAe,EAAE,EAAE,EACnB,SAAS,EAAE,iBAAiB,EAC5B,MAAM,EAAE,iBAAiB,GACvB,iBAAiB,CAgBnB;AAED,wBAAgB,4BAA4B,CAC3C,MAAM,EAAE,iBAAiB,EACzB,iBAAiB,EAAE,iBAAiB,GAClC,EAAE,CAGJ;AAED,wBAAgB,qBAAqB,CACpC,KA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wBAAgB,eAAe,CAC9B,MAAM,EAAE,iBAAiB,EACzB,WAAW,EAAE,EAAE,EACf,GAAG,EAAE,EAAE,EACP,cAAc,EAAE,OAAO,GACrB,EAAE,CA2BJ"}
|
package/lib/node/math/market.js
CHANGED
|
@@ -61,29 +61,49 @@ function calculateOracleSpread(price, oraclePriceData) {
|
|
|
61
61
|
}
|
|
62
62
|
exports.calculateOracleSpread = calculateOracleSpread;
|
|
63
63
|
function calculateMarketMarginRatio(market, size, marginCategory, customMarginRatio = 0, userHighLeverageMode = false) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
if (market.status === 'Settlement')
|
|
65
|
+
return 0;
|
|
66
|
+
const isHighLeverageUser = userHighLeverageMode &&
|
|
67
67
|
market.highLeverageMarginRatioInitial > 0 &&
|
|
68
|
-
market.highLeverageMarginRatioMaintenance
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
let marginRatio;
|
|
68
|
+
market.highLeverageMarginRatioMaintenance > 0;
|
|
69
|
+
const marginRatioInitial = isHighLeverageUser
|
|
70
|
+
? market.highLeverageMarginRatioInitial
|
|
71
|
+
: market.marginRatioInitial;
|
|
72
|
+
const marginRatioMaintenance = isHighLeverageUser
|
|
73
|
+
? market.highLeverageMarginRatioMaintenance
|
|
74
|
+
: market.marginRatioMaintenance;
|
|
75
|
+
let defaultMarginRatio;
|
|
77
76
|
switch (marginCategory) {
|
|
78
|
-
case 'Initial':
|
|
79
|
-
|
|
80
|
-
marginRatio = Math.max((0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRationInitial), numericConstants_1.MARGIN_PRECISION).toNumber(), customMarginRatio);
|
|
77
|
+
case 'Initial':
|
|
78
|
+
defaultMarginRatio = marginRatioInitial;
|
|
81
79
|
break;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
80
|
+
case 'Maintenance':
|
|
81
|
+
defaultMarginRatio = marginRatioMaintenance;
|
|
85
82
|
break;
|
|
83
|
+
default:
|
|
84
|
+
throw new Error('Invalid margin category');
|
|
85
|
+
}
|
|
86
|
+
let marginRatio;
|
|
87
|
+
if (isHighLeverageUser && marginCategory !== 'Maintenance') {
|
|
88
|
+
// Use ordinary-mode initial/fill ratios for size-adjusted calc
|
|
89
|
+
let preSizeAdjMarginRatio;
|
|
90
|
+
switch (marginCategory) {
|
|
91
|
+
case 'Initial':
|
|
92
|
+
preSizeAdjMarginRatio = market.marginRatioInitial;
|
|
93
|
+
break;
|
|
94
|
+
default:
|
|
95
|
+
preSizeAdjMarginRatio = marginRatioMaintenance;
|
|
96
|
+
break;
|
|
86
97
|
}
|
|
98
|
+
const sizeAdjMarginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(preSizeAdjMarginRatio), numericConstants_1.MARGIN_PRECISION, false).toNumber();
|
|
99
|
+
marginRatio = (0, margin_1.calcHighLeverageModeInitialMarginRatioFromSize)(new anchor_1.BN(preSizeAdjMarginRatio), new anchor_1.BN(sizeAdjMarginRatio), new anchor_1.BN(defaultMarginRatio)).toNumber();
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const sizeAdjMarginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(defaultMarginRatio), numericConstants_1.MARGIN_PRECISION, true).toNumber();
|
|
103
|
+
marginRatio = Math.max(defaultMarginRatio, sizeAdjMarginRatio);
|
|
104
|
+
}
|
|
105
|
+
if (marginCategory === 'Initial') {
|
|
106
|
+
marginRatio = Math.max(marginRatio, customMarginRatio);
|
|
87
107
|
}
|
|
88
108
|
return marginRatio;
|
|
89
109
|
}
|
package/package.json
CHANGED
package/src/math/margin.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
MARGIN_PRECISION,
|
|
11
11
|
PRICE_PRECISION,
|
|
12
12
|
QUOTE_PRECISION,
|
|
13
|
+
PERCENTAGE_PRECISION,
|
|
13
14
|
} from '../constants/numericConstants';
|
|
14
15
|
import { BN } from '@coral-xyz/anchor';
|
|
15
16
|
import { OraclePriceData } from '../oracles/types';
|
|
@@ -32,7 +33,8 @@ export function calculateSizePremiumLiabilityWeight(
|
|
|
32
33
|
size: BN, // AMM_RESERVE_PRECISION
|
|
33
34
|
imfFactor: BN,
|
|
34
35
|
liabilityWeight: BN,
|
|
35
|
-
precision: BN
|
|
36
|
+
precision: BN,
|
|
37
|
+
isBounded = true
|
|
36
38
|
): BN {
|
|
37
39
|
if (imfFactor.eq(ZERO)) {
|
|
38
40
|
return liabilityWeight;
|
|
@@ -53,10 +55,13 @@ export function calculateSizePremiumLiabilityWeight(
|
|
|
53
55
|
.div(denom) // 1e5
|
|
54
56
|
);
|
|
55
57
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
sizePremiumLiabilityWeight
|
|
59
|
-
|
|
58
|
+
let maxLiabilityWeight;
|
|
59
|
+
if (isBounded) {
|
|
60
|
+
maxLiabilityWeight = BN.max(liabilityWeight, sizePremiumLiabilityWeight);
|
|
61
|
+
} else {
|
|
62
|
+
maxLiabilityWeight = sizePremiumLiabilityWeight;
|
|
63
|
+
}
|
|
64
|
+
|
|
60
65
|
return maxLiabilityWeight;
|
|
61
66
|
}
|
|
62
67
|
|
|
@@ -370,3 +375,37 @@ export function calculateUserMaxPerpOrderSize(
|
|
|
370
375
|
|
|
371
376
|
return user.getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide);
|
|
372
377
|
}
|
|
378
|
+
|
|
379
|
+
export function calcHighLeverageModeInitialMarginRatioFromSize(
|
|
380
|
+
preSizeAdjMarginRatio: BN,
|
|
381
|
+
sizeAdjMarginRatio: BN,
|
|
382
|
+
defaultMarginRatio: BN
|
|
383
|
+
): BN {
|
|
384
|
+
let result: BN;
|
|
385
|
+
|
|
386
|
+
if (sizeAdjMarginRatio.lt(preSizeAdjMarginRatio)) {
|
|
387
|
+
const sizePctDiscountFactor = PERCENTAGE_PRECISION.sub(
|
|
388
|
+
preSizeAdjMarginRatio
|
|
389
|
+
.sub(sizeAdjMarginRatio)
|
|
390
|
+
.mul(PERCENTAGE_PRECISION)
|
|
391
|
+
.div(preSizeAdjMarginRatio.div(new BN(5)))
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
const hlmMarginDelta = BN.max(
|
|
395
|
+
preSizeAdjMarginRatio.sub(defaultMarginRatio),
|
|
396
|
+
new BN(1)
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
const hlmMarginDeltaProportion = hlmMarginDelta
|
|
400
|
+
.mul(sizePctDiscountFactor)
|
|
401
|
+
.div(PERCENTAGE_PRECISION);
|
|
402
|
+
|
|
403
|
+
result = hlmMarginDeltaProportion.add(defaultMarginRatio);
|
|
404
|
+
} else if (sizeAdjMarginRatio.eq(preSizeAdjMarginRatio)) {
|
|
405
|
+
result = defaultMarginRatio;
|
|
406
|
+
} else {
|
|
407
|
+
result = sizeAdjMarginRatio;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return result;
|
|
411
|
+
}
|
package/src/math/market.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
import {
|
|
20
20
|
calculateSizeDiscountAssetWeight,
|
|
21
21
|
calculateSizePremiumLiabilityWeight,
|
|
22
|
+
calcHighLeverageModeInitialMarginRatioFromSize,
|
|
22
23
|
} from './margin';
|
|
23
24
|
import { MMOraclePriceData, OraclePriceData } from '../oracles/types';
|
|
24
25
|
import {
|
|
@@ -143,45 +144,74 @@ export function calculateMarketMarginRatio(
|
|
|
143
144
|
customMarginRatio = 0,
|
|
144
145
|
userHighLeverageMode = false
|
|
145
146
|
): number {
|
|
146
|
-
|
|
147
|
-
let marginRatioMaintenance;
|
|
147
|
+
if (market.status === 'Settlement') return 0;
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
const isHighLeverageUser =
|
|
150
150
|
userHighLeverageMode &&
|
|
151
151
|
market.highLeverageMarginRatioInitial > 0 &&
|
|
152
|
-
market.highLeverageMarginRatioMaintenance
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
152
|
+
market.highLeverageMarginRatioMaintenance > 0;
|
|
153
|
+
|
|
154
|
+
const marginRatioInitial = isHighLeverageUser
|
|
155
|
+
? market.highLeverageMarginRatioInitial
|
|
156
|
+
: market.marginRatioInitial;
|
|
157
|
+
|
|
158
|
+
const marginRatioMaintenance = isHighLeverageUser
|
|
159
|
+
? market.highLeverageMarginRatioMaintenance
|
|
160
|
+
: market.marginRatioMaintenance;
|
|
160
161
|
|
|
161
|
-
let
|
|
162
|
+
let defaultMarginRatio: number;
|
|
162
163
|
switch (marginCategory) {
|
|
163
|
-
case 'Initial':
|
|
164
|
-
|
|
165
|
-
marginRatio = Math.max(
|
|
166
|
-
calculateSizePremiumLiabilityWeight(
|
|
167
|
-
size,
|
|
168
|
-
new BN(market.imfFactor),
|
|
169
|
-
new BN(marginRationInitial),
|
|
170
|
-
MARGIN_PRECISION
|
|
171
|
-
).toNumber(),
|
|
172
|
-
customMarginRatio
|
|
173
|
-
);
|
|
164
|
+
case 'Initial':
|
|
165
|
+
defaultMarginRatio = marginRatioInitial;
|
|
174
166
|
break;
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
marginRatio = calculateSizePremiumLiabilityWeight(
|
|
178
|
-
size,
|
|
179
|
-
new BN(market.imfFactor),
|
|
180
|
-
new BN(marginRatioMaintenance),
|
|
181
|
-
MARGIN_PRECISION
|
|
182
|
-
).toNumber();
|
|
167
|
+
case 'Maintenance':
|
|
168
|
+
defaultMarginRatio = marginRatioMaintenance;
|
|
183
169
|
break;
|
|
170
|
+
default:
|
|
171
|
+
throw new Error('Invalid margin category');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
let marginRatio: number;
|
|
175
|
+
|
|
176
|
+
if (isHighLeverageUser && marginCategory !== 'Maintenance') {
|
|
177
|
+
// Use ordinary-mode initial/fill ratios for size-adjusted calc
|
|
178
|
+
let preSizeAdjMarginRatio: number;
|
|
179
|
+
switch (marginCategory) {
|
|
180
|
+
case 'Initial':
|
|
181
|
+
preSizeAdjMarginRatio = market.marginRatioInitial;
|
|
182
|
+
break;
|
|
183
|
+
default:
|
|
184
|
+
preSizeAdjMarginRatio = marginRatioMaintenance;
|
|
185
|
+
break;
|
|
184
186
|
}
|
|
187
|
+
|
|
188
|
+
const sizeAdjMarginRatio = calculateSizePremiumLiabilityWeight(
|
|
189
|
+
size,
|
|
190
|
+
new BN(market.imfFactor),
|
|
191
|
+
new BN(preSizeAdjMarginRatio),
|
|
192
|
+
MARGIN_PRECISION,
|
|
193
|
+
false
|
|
194
|
+
).toNumber();
|
|
195
|
+
|
|
196
|
+
marginRatio = calcHighLeverageModeInitialMarginRatioFromSize(
|
|
197
|
+
new BN(preSizeAdjMarginRatio),
|
|
198
|
+
new BN(sizeAdjMarginRatio),
|
|
199
|
+
new BN(defaultMarginRatio)
|
|
200
|
+
).toNumber();
|
|
201
|
+
} else {
|
|
202
|
+
const sizeAdjMarginRatio = calculateSizePremiumLiabilityWeight(
|
|
203
|
+
size,
|
|
204
|
+
new BN(market.imfFactor),
|
|
205
|
+
new BN(defaultMarginRatio),
|
|
206
|
+
MARGIN_PRECISION,
|
|
207
|
+
true
|
|
208
|
+
).toNumber();
|
|
209
|
+
|
|
210
|
+
marginRatio = Math.max(defaultMarginRatio, sizeAdjMarginRatio);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (marginCategory === 'Initial') {
|
|
214
|
+
marginRatio = Math.max(marginRatio, customMarginRatio);
|
|
185
215
|
}
|
|
186
216
|
|
|
187
217
|
return marginRatio;
|