@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.31
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/README.md +13 -13
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +16 -16
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +31 -28
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +22 -11
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +17 -17
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +37 -35
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/pda.d.ts +10 -4
- package/lib/addresses/pda.js +51 -14
- package/lib/admin.d.ts +42 -26
- package/lib/admin.js +228 -81
- package/lib/clearingHouse.d.ts +130 -49
- package/lib/clearingHouse.js +1256 -367
- package/lib/clearingHouseConfig.d.ts +4 -4
- package/lib/clearingHouseUser.d.ts +51 -39
- package/lib/clearingHouseUser.js +408 -191
- package/lib/clearingHouseUserStats.d.ts +18 -0
- package/lib/clearingHouseUserStats.js +49 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.d.ts +7 -9
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +25 -12
- package/lib/constants/numericConstants.js +35 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +7 -7
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +53 -0
- package/lib/dlob/DLOB.d.ts +74 -0
- package/lib/dlob/DLOB.js +595 -0
- package/lib/dlob/DLOBNode.d.ts +54 -0
- package/lib/dlob/DLOBNode.js +85 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +139 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +8 -2
- package/lib/events/types.js +6 -0
- package/lib/examples/makeTradeExample.js +20 -8
- package/lib/factory/bigNum.d.ts +2 -0
- package/lib/factory/bigNum.js +48 -10
- package/lib/idl/clearing_house.json +4889 -1529
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +21 -6
- package/lib/index.js +25 -6
- package/lib/math/amm.d.ts +9 -6
- package/lib/math/amm.js +91 -66
- package/lib/math/auction.js +4 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +11 -10
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +77 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +72 -10
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +6 -2
- package/lib/math/orders.js +78 -3
- package/lib/math/position.d.ts +21 -13
- package/lib/math/position.js +76 -36
- package/lib/math/repeg.js +14 -5
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +192 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/trade.d.ts +10 -10
- package/lib/math/trade.js +32 -39
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +12 -92
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +13 -4
- package/lib/tx/types.d.ts +1 -1
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +566 -138
- package/lib/types.js +129 -5
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +12 -6
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +46 -42
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +31 -11
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +64 -59
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +1 -2
- package/src/addresses/pda.ts +88 -14
- package/src/admin.ts +452 -147
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +2066 -467
- package/src/clearingHouseConfig.ts +4 -3
- package/src/clearingHouseUser.ts +747 -294
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +30 -31
- package/src/constants/numericConstants.ts +48 -25
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +73 -0
- package/src/dlob/DLOB.ts +953 -0
- package/src/dlob/DLOBNode.ts +167 -0
- package/src/dlob/NodeList.ts +189 -0
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +19 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/examples/makeTradeExample.ts +32 -14
- package/src/factory/bigNum.ts +65 -13
- package/src/idl/clearing_house.json +4889 -1529
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +21 -6
- package/src/math/amm.ts +171 -88
- package/src/math/auction.ts +5 -1
- package/src/math/conversion.ts +2 -2
- package/src/math/funding.ts +20 -18
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +127 -0
- package/src/math/market.ts +145 -14
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +138 -4
- package/src/math/position.ts +119 -58
- package/src/math/repeg.ts +16 -6
- package/src/math/spotBalance.ts +319 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +53 -59
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/orderParams.ts +24 -137
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +533 -140
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/src/util/computeUnits.js +21 -11
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/tests/bn/test.ts +24 -10
- package/tests/dlob/helpers.ts +397 -0
- package/tests/dlob/test.ts +3688 -0
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -34
- package/lib/constants/markets.d.ts +0 -19
- package/lib/math/bankBalance.d.ts +0 -9
- package/lib/math/bankBalance.js +0 -75
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -134
- package/src/constants/banks.ts +0 -43
- package/src/math/bankBalance.ts +0 -112
- package/src/math/state.ts +0 -14
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/orders.ts +0 -244
- package/src/util/computeUnits.js.map +0 -1
package/lib/factory/bigNum.js
CHANGED
|
@@ -10,6 +10,9 @@ class BigNum {
|
|
|
10
10
|
this.val = new anchor_1.BN(val);
|
|
11
11
|
this.precision = new anchor_1.BN(precisionVal);
|
|
12
12
|
}
|
|
13
|
+
bigNumFromParam(bn) {
|
|
14
|
+
return anchor_1.BN.isBN(bn) ? BigNum.from(bn) : bn;
|
|
15
|
+
}
|
|
13
16
|
add(bn) {
|
|
14
17
|
(0, assert_1.assert)(bn.precision.eq(this.precision), 'Adding unequal precisions');
|
|
15
18
|
return BigNum.from(this.val.add(bn.val), this.precision);
|
|
@@ -19,7 +22,7 @@ class BigNum {
|
|
|
19
22
|
return BigNum.from(this.val.sub(bn.val), this.precision);
|
|
20
23
|
}
|
|
21
24
|
mul(bn) {
|
|
22
|
-
const mulVal =
|
|
25
|
+
const mulVal = this.bigNumFromParam(bn);
|
|
23
26
|
return BigNum.from(this.val.mul(mulVal.val), this.precision.add(mulVal.precision));
|
|
24
27
|
}
|
|
25
28
|
/**
|
|
@@ -28,12 +31,12 @@ class BigNum {
|
|
|
28
31
|
* @returns
|
|
29
32
|
*/
|
|
30
33
|
scalarMul(bn) {
|
|
31
|
-
if (
|
|
34
|
+
if (anchor_1.BN.isBN(bn))
|
|
32
35
|
return BigNum.from(this.val.mul(bn), this.precision);
|
|
33
36
|
return BigNum.from(this.val.mul(bn.val), this.precision.add(bn.precision)).shift(bn.precision.neg());
|
|
34
37
|
}
|
|
35
38
|
div(bn) {
|
|
36
|
-
if (
|
|
39
|
+
if (anchor_1.BN.isBN(bn))
|
|
37
40
|
return BigNum.from(this.val.div(bn), this.precision);
|
|
38
41
|
return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
|
|
39
42
|
}
|
|
@@ -73,35 +76,35 @@ class BigNum {
|
|
|
73
76
|
.toPrecision(precision);
|
|
74
77
|
}
|
|
75
78
|
gt(bn, ignorePrecision) {
|
|
76
|
-
const comparisonVal =
|
|
79
|
+
const comparisonVal = this.bigNumFromParam(bn);
|
|
77
80
|
if (!ignorePrecision && !comparisonVal.eq(numericConstants_1.ZERO)) {
|
|
78
81
|
(0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
|
|
79
82
|
}
|
|
80
83
|
return this.val.gt(comparisonVal.val);
|
|
81
84
|
}
|
|
82
85
|
lt(bn, ignorePrecision) {
|
|
83
|
-
const comparisonVal =
|
|
86
|
+
const comparisonVal = this.bigNumFromParam(bn);
|
|
84
87
|
if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
|
|
85
88
|
(0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
|
|
86
89
|
}
|
|
87
90
|
return this.val.lt(comparisonVal.val);
|
|
88
91
|
}
|
|
89
92
|
gte(bn, ignorePrecision) {
|
|
90
|
-
const comparisonVal =
|
|
93
|
+
const comparisonVal = this.bigNumFromParam(bn);
|
|
91
94
|
if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
|
|
92
95
|
(0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
|
|
93
96
|
}
|
|
94
97
|
return this.val.gte(comparisonVal.val);
|
|
95
98
|
}
|
|
96
99
|
lte(bn, ignorePrecision) {
|
|
97
|
-
const comparisonVal =
|
|
100
|
+
const comparisonVal = this.bigNumFromParam(bn);
|
|
98
101
|
if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
|
|
99
102
|
(0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
|
|
100
103
|
}
|
|
101
104
|
return this.val.lte(comparisonVal.val);
|
|
102
105
|
}
|
|
103
106
|
eq(bn, ignorePrecision) {
|
|
104
|
-
const comparisonVal =
|
|
107
|
+
const comparisonVal = this.bigNumFromParam(bn);
|
|
105
108
|
if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
|
|
106
109
|
(0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
|
|
107
110
|
}
|
|
@@ -134,7 +137,8 @@ class BigNum {
|
|
|
134
137
|
*/
|
|
135
138
|
print() {
|
|
136
139
|
(0, assert_1.assert)(this.precision.gte(numericConstants_1.ZERO), 'Tried to print a BN with precision lower than zero');
|
|
137
|
-
const
|
|
140
|
+
const isNeg = this.isNeg();
|
|
141
|
+
const plainString = this.abs().toString();
|
|
138
142
|
const precisionNum = this.precision.toNumber();
|
|
139
143
|
// make a string with at least the precisionNum number of zeroes
|
|
140
144
|
let printString = [
|
|
@@ -151,6 +155,9 @@ class BigNum {
|
|
|
151
155
|
// add zero if leading delim
|
|
152
156
|
if (printString[0] === BigNum.delim)
|
|
153
157
|
printString = `0${printString}`;
|
|
158
|
+
// Add minus if negative
|
|
159
|
+
if (isNeg)
|
|
160
|
+
printString = `-${printString}`;
|
|
154
161
|
// remove trailing delim
|
|
155
162
|
if (printString[printString.length - 1] === BigNum.delim)
|
|
156
163
|
printString = printString.slice(0, printString.length - 1);
|
|
@@ -182,6 +189,8 @@ class BigNum {
|
|
|
182
189
|
: useTradePrecision
|
|
183
190
|
? this.toTradePrecision()
|
|
184
191
|
: this.print();
|
|
192
|
+
if (!printVal.includes(BigNum.delim))
|
|
193
|
+
return printVal;
|
|
185
194
|
return printVal.replace(/0+$/g, '').replace(/\.$/, '').replace(/,$/, '');
|
|
186
195
|
}
|
|
187
196
|
debug() {
|
|
@@ -203,6 +212,9 @@ class BigNum {
|
|
|
203
212
|
.join('');
|
|
204
213
|
return `${leftSide}${BigNum.delim}${filledRightSide}`;
|
|
205
214
|
}
|
|
215
|
+
getZeroes(count) {
|
|
216
|
+
return new Array(count).fill('0').join('');
|
|
217
|
+
}
|
|
206
218
|
/**
|
|
207
219
|
* Pretty print to the specified number of significant figures
|
|
208
220
|
* @param fixedPrecision
|
|
@@ -211,6 +223,12 @@ class BigNum {
|
|
|
211
223
|
toPrecision(fixedPrecision, trailingZeroes = false) {
|
|
212
224
|
const printString = this.print();
|
|
213
225
|
let precisionPrintString = printString.slice(0, fixedPrecision + 1);
|
|
226
|
+
const thisString = this.toString();
|
|
227
|
+
if (!printString.includes(BigNum.delim) &&
|
|
228
|
+
thisString.length < fixedPrecision) {
|
|
229
|
+
const precisionMismatch = fixedPrecision - thisString.length;
|
|
230
|
+
return BigNum.from(thisString + this.getZeroes(precisionMismatch), precisionMismatch).toPrecision(fixedPrecision, trailingZeroes);
|
|
231
|
+
}
|
|
214
232
|
if (!precisionPrintString.includes(BigNum.delim) ||
|
|
215
233
|
precisionPrintString[precisionPrintString.length - 1] === BigNum.delim) {
|
|
216
234
|
precisionPrintString = printString.slice(0, fixedPrecision);
|
|
@@ -261,10 +279,27 @@ class BigNum {
|
|
|
261
279
|
* @returns
|
|
262
280
|
*/
|
|
263
281
|
toNotional(useTradePrecision, precisionOverride) {
|
|
282
|
+
var _a;
|
|
264
283
|
const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
|
|
265
|
-
const
|
|
284
|
+
const usingCustomPrecision = true && (useTradePrecision || precisionOverride);
|
|
285
|
+
let val = usingCustomPrecision
|
|
266
286
|
? this.prettyPrint(useTradePrecision, precisionOverride)
|
|
267
287
|
: BigNum.fromPrint(this.toFixed(2), new anchor_1.BN(2)).prettyPrint();
|
|
288
|
+
// Append two trailing zeroes if not using custom precision
|
|
289
|
+
if (!usingCustomPrecision) {
|
|
290
|
+
const [_, rightSide] = val.split(BigNum.delim);
|
|
291
|
+
const trailingLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
|
|
292
|
+
if (trailingLength < 2) {
|
|
293
|
+
const numHasDecimals = this.print().includes(BigNum.delim);
|
|
294
|
+
// Handle case where pretty print won't include the decimal point
|
|
295
|
+
if (trailingLength === 0 && numHasDecimals) {
|
|
296
|
+
val = `${val}.00`;
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
val = `${val}${new Array(2 - trailingLength).fill('0').join('')}`;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
268
303
|
return `${prefix}${val.replace('-', '')}`;
|
|
269
304
|
}
|
|
270
305
|
toMillified(precision = 3) {
|
|
@@ -273,6 +308,9 @@ class BigNum {
|
|
|
273
308
|
if (!leftSide) {
|
|
274
309
|
return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
|
|
275
310
|
}
|
|
311
|
+
if (leftSide.length <= precision) {
|
|
312
|
+
return this.toPrecision(precision);
|
|
313
|
+
}
|
|
276
314
|
if (leftSide.length <= 3) {
|
|
277
315
|
return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
|
|
278
316
|
}
|