@drift-labs/sdk 2.33.1-beta.1 → 2.33.1-beta.3
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/idl/drift.json +5 -2
- package/lib/math/oracles.js +2 -3
- package/lib/types.d.ts +2 -2
- package/lib/user.js +9 -6
- package/package.json +1 -1
- package/src/idl/drift.json +5 -2
- package/src/math/oracles.ts +7 -7
- package/src/types.ts +2 -2
- package/src/user.ts +12 -8
- package/tests/dlob/helpers.ts +2 -2
- package/tests/dlob/test.ts +2 -1
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.33.1-beta.
|
|
1
|
+
2.33.1-beta.3
|
package/lib/idl/drift.json
CHANGED
|
@@ -6900,11 +6900,11 @@
|
|
|
6900
6900
|
"kind": "struct",
|
|
6901
6901
|
"fields": [
|
|
6902
6902
|
{
|
|
6903
|
-
"name": "
|
|
6903
|
+
"name": "markOraclePercentDivergence",
|
|
6904
6904
|
"type": "u64"
|
|
6905
6905
|
},
|
|
6906
6906
|
{
|
|
6907
|
-
"name": "
|
|
6907
|
+
"name": "oracleTwap5minPercentDivergence",
|
|
6908
6908
|
"type": "u64"
|
|
6909
6909
|
}
|
|
6910
6910
|
]
|
|
@@ -7637,6 +7637,9 @@
|
|
|
7637
7637
|
{
|
|
7638
7638
|
"name": "Initial"
|
|
7639
7639
|
},
|
|
7640
|
+
{
|
|
7641
|
+
"name": "Fill"
|
|
7642
|
+
},
|
|
7640
7643
|
{
|
|
7641
7644
|
"name": "Maintenance"
|
|
7642
7645
|
}
|
package/lib/math/oracles.js
CHANGED
|
@@ -45,9 +45,8 @@ function isOracleTooDivergent(amm, oraclePriceData, oracleGuardRails, now) {
|
|
|
45
45
|
.div(sinceStart.add(sinceLastUpdate));
|
|
46
46
|
const oracleSpread = oracleTwap5min.sub(oraclePriceData.price);
|
|
47
47
|
const oracleSpreadPct = oracleSpread.mul(numericConstants_1.PRICE_PRECISION).div(oracleTwap5min);
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
.gte(numericConstants_1.BID_ASK_SPREAD_PRECISION.mul(oracleGuardRails.priceDivergence.markOracleDivergenceNumerator).div(oracleGuardRails.priceDivergence.markOracleDivergenceDenominator));
|
|
48
|
+
const maxDivergence = index_1.BN.max(oracleGuardRails.priceDivergence.markOraclePercentDivergence, numericConstants_1.PERCENTAGE_PRECISION.div(new index_1.BN(10)));
|
|
49
|
+
const tooDivergent = oracleSpreadPct.abs().gte(maxDivergence);
|
|
51
50
|
return tooDivergent;
|
|
52
51
|
}
|
|
53
52
|
exports.isOracleTooDivergent = isOracleTooDivergent;
|
package/lib/types.d.ts
CHANGED
|
@@ -1035,8 +1035,8 @@ export type OrderFillerRewardStructure = {
|
|
|
1035
1035
|
};
|
|
1036
1036
|
export type OracleGuardRails = {
|
|
1037
1037
|
priceDivergence: {
|
|
1038
|
-
|
|
1039
|
-
|
|
1038
|
+
markOraclePercentDivergence: BN;
|
|
1039
|
+
oracleTwap5MinPercentDivergence: BN;
|
|
1040
1040
|
};
|
|
1041
1041
|
validity: {
|
|
1042
1042
|
slotsBeforeStaleForAmm: BN;
|
package/lib/user.js
CHANGED
|
@@ -1113,7 +1113,6 @@ class User {
|
|
|
1113
1113
|
const outOraclePrice = this.getOracleDataForSpotMarket(outMarketIndex).price;
|
|
1114
1114
|
const inPrecision = new _1.BN(10 ** inMarket.decimals);
|
|
1115
1115
|
const outPrecision = new _1.BN(10 ** outMarket.decimals);
|
|
1116
|
-
const outSaferThanIn = inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
1117
1116
|
const inSpotPosition = this.getSpotPosition(inMarketIndex) ||
|
|
1118
1117
|
this.getEmptySpotPosition(inMarketIndex);
|
|
1119
1118
|
const outSpotPosition = this.getSpotPosition(outMarketIndex) ||
|
|
@@ -1137,6 +1136,12 @@ class User {
|
|
|
1137
1136
|
let inSwap = numericConstants_1.ZERO;
|
|
1138
1137
|
let outSwap = numericConstants_1.ZERO;
|
|
1139
1138
|
const inTokenAmount = this.getTokenAmount(inMarketIndex);
|
|
1139
|
+
const outTokenAmount = this.getTokenAmount(outMarketIndex);
|
|
1140
|
+
const outSaferThanIn =
|
|
1141
|
+
// selling asset to close borrow
|
|
1142
|
+
(inTokenAmount.gt(numericConstants_1.ZERO) && outTokenAmount.lt(numericConstants_1.ZERO)) ||
|
|
1143
|
+
// buying asset with higher initial asset weight
|
|
1144
|
+
inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
1140
1145
|
if (freeCollateral.lt(numericConstants_1.ONE)) {
|
|
1141
1146
|
if (outSaferThanIn && inTokenAmount.gt(numericConstants_1.ZERO)) {
|
|
1142
1147
|
inSwap = inTokenAmount;
|
|
@@ -1145,11 +1150,9 @@ class User {
|
|
|
1145
1150
|
}
|
|
1146
1151
|
else {
|
|
1147
1152
|
let minSwap = numericConstants_1.ZERO;
|
|
1148
|
-
let maxSwap = freeCollateral
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
.div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.div(new _1.BN(100)))
|
|
1152
|
-
.div(inOraclePrice); // just assume user can go 100x
|
|
1153
|
+
let maxSwap = _1.BN.max(freeCollateral.mul(inPrecision).mul(new _1.BN(100)).div(inOraclePrice), // 100x current free collateral
|
|
1154
|
+
inTokenAmount.abs().mul(new _1.BN(10)) // 10x current position
|
|
1155
|
+
);
|
|
1153
1156
|
inSwap = maxSwap.div(numericConstants_1.TWO);
|
|
1154
1157
|
const error = freeCollateral.div(new _1.BN(10000));
|
|
1155
1158
|
let i = 0;
|
package/package.json
CHANGED
package/src/idl/drift.json
CHANGED
|
@@ -6900,11 +6900,11 @@
|
|
|
6900
6900
|
"kind": "struct",
|
|
6901
6901
|
"fields": [
|
|
6902
6902
|
{
|
|
6903
|
-
"name": "
|
|
6903
|
+
"name": "markOraclePercentDivergence",
|
|
6904
6904
|
"type": "u64"
|
|
6905
6905
|
},
|
|
6906
6906
|
{
|
|
6907
|
-
"name": "
|
|
6907
|
+
"name": "oracleTwap5minPercentDivergence",
|
|
6908
6908
|
"type": "u64"
|
|
6909
6909
|
}
|
|
6910
6910
|
]
|
|
@@ -7637,6 +7637,9 @@
|
|
|
7637
7637
|
{
|
|
7638
7638
|
"name": "Initial"
|
|
7639
7639
|
},
|
|
7640
|
+
{
|
|
7641
|
+
"name": "Fill"
|
|
7642
|
+
},
|
|
7640
7643
|
{
|
|
7641
7644
|
"name": "Maintenance"
|
|
7642
7645
|
}
|
package/src/math/oracles.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
ONE,
|
|
8
8
|
ZERO,
|
|
9
9
|
FIVE_MINUTE,
|
|
10
|
+
PERCENTAGE_PRECISION,
|
|
10
11
|
} from '../constants/numericConstants';
|
|
11
12
|
import { BN, HistoricalOracleData, PerpMarketAccount } from '../index';
|
|
12
13
|
import { assert } from '../assert/assert';
|
|
@@ -78,13 +79,12 @@ export function isOracleTooDivergent(
|
|
|
78
79
|
const oracleSpread = oracleTwap5min.sub(oraclePriceData.price);
|
|
79
80
|
const oracleSpreadPct = oracleSpread.mul(PRICE_PRECISION).div(oracleTwap5min);
|
|
80
81
|
|
|
81
|
-
const
|
|
82
|
-
.
|
|
83
|
-
.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
);
|
|
82
|
+
const maxDivergence = BN.max(
|
|
83
|
+
oracleGuardRails.priceDivergence.markOraclePercentDivergence,
|
|
84
|
+
PERCENTAGE_PRECISION.div(new BN(10))
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const tooDivergent = oracleSpreadPct.abs().gte(maxDivergence);
|
|
88
88
|
|
|
89
89
|
return tooDivergent;
|
|
90
90
|
}
|
package/src/types.ts
CHANGED
|
@@ -1013,8 +1013,8 @@ export type OrderFillerRewardStructure = {
|
|
|
1013
1013
|
|
|
1014
1014
|
export type OracleGuardRails = {
|
|
1015
1015
|
priceDivergence: {
|
|
1016
|
-
|
|
1017
|
-
|
|
1016
|
+
markOraclePercentDivergence: BN;
|
|
1017
|
+
oracleTwap5MinPercentDivergence: BN;
|
|
1018
1018
|
};
|
|
1019
1019
|
validity: {
|
|
1020
1020
|
slotsBeforeStaleForAmm: BN;
|
package/src/user.ts
CHANGED
|
@@ -2009,9 +2009,6 @@ export class User {
|
|
|
2009
2009
|
const inPrecision = new BN(10 ** inMarket.decimals);
|
|
2010
2010
|
const outPrecision = new BN(10 ** outMarket.decimals);
|
|
2011
2011
|
|
|
2012
|
-
const outSaferThanIn =
|
|
2013
|
-
inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
2014
|
-
|
|
2015
2012
|
const inSpotPosition =
|
|
2016
2013
|
this.getSpotPosition(inMarketIndex) ||
|
|
2017
2014
|
this.getEmptySpotPosition(inMarketIndex);
|
|
@@ -2053,6 +2050,14 @@ export class User {
|
|
|
2053
2050
|
let inSwap = ZERO;
|
|
2054
2051
|
let outSwap = ZERO;
|
|
2055
2052
|
const inTokenAmount = this.getTokenAmount(inMarketIndex);
|
|
2053
|
+
const outTokenAmount = this.getTokenAmount(outMarketIndex);
|
|
2054
|
+
|
|
2055
|
+
const outSaferThanIn =
|
|
2056
|
+
// selling asset to close borrow
|
|
2057
|
+
(inTokenAmount.gt(ZERO) && outTokenAmount.lt(ZERO)) ||
|
|
2058
|
+
// buying asset with higher initial asset weight
|
|
2059
|
+
inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
2060
|
+
|
|
2056
2061
|
if (freeCollateral.lt(ONE)) {
|
|
2057
2062
|
if (outSaferThanIn && inTokenAmount.gt(ZERO)) {
|
|
2058
2063
|
inSwap = inTokenAmount;
|
|
@@ -2060,11 +2065,10 @@ export class User {
|
|
|
2060
2065
|
}
|
|
2061
2066
|
} else {
|
|
2062
2067
|
let minSwap = ZERO;
|
|
2063
|
-
let maxSwap =
|
|
2064
|
-
.mul(inPrecision)
|
|
2065
|
-
.mul(
|
|
2066
|
-
|
|
2067
|
-
.div(inOraclePrice); // just assume user can go 100x
|
|
2068
|
+
let maxSwap = BN.max(
|
|
2069
|
+
freeCollateral.mul(inPrecision).mul(new BN(100)).div(inOraclePrice), // 100x current free collateral
|
|
2070
|
+
inTokenAmount.abs().mul(new BN(10)) // 10x current position
|
|
2071
|
+
);
|
|
2068
2072
|
inSwap = maxSwap.div(TWO);
|
|
2069
2073
|
const error = freeCollateral.div(new BN(10000));
|
|
2070
2074
|
|
package/tests/dlob/helpers.ts
CHANGED
|
@@ -511,8 +511,8 @@ export const mockStateAccount: StateAccount = {
|
|
|
511
511
|
liquidationDuration: 0,
|
|
512
512
|
oracleGuardRails: {
|
|
513
513
|
priceDivergence: {
|
|
514
|
-
|
|
515
|
-
|
|
514
|
+
markOraclePercentDivergence: new BN(0),
|
|
515
|
+
oracleTwap5MinPercentDivergence: new BN(0),
|
|
516
516
|
},
|
|
517
517
|
validity: {
|
|
518
518
|
slotsBeforeStaleForAmm: new BN(0),
|
package/tests/dlob/test.ts
CHANGED
|
@@ -22,10 +22,11 @@ import {
|
|
|
22
22
|
ZERO,
|
|
23
23
|
convertToNumber,
|
|
24
24
|
QUOTE_PRECISION,
|
|
25
|
+
isVariant,
|
|
26
|
+
TWO
|
|
25
27
|
} from '../../src';
|
|
26
28
|
|
|
27
29
|
import { mockPerpMarkets, mockSpotMarkets, mockStateAccount } from './helpers';
|
|
28
|
-
import { isVariant, TWO } from '../../lib';
|
|
29
30
|
import { DLOBOrdersCoder } from '../../src/dlob/DLOBOrders';
|
|
30
31
|
|
|
31
32
|
function insertOrderToDLOB(
|