@drift-labs/sdk 0.2.0-master.30 → 0.2.0-master.32
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/lib/admin.d.ts +11 -7
- package/lib/admin.js +50 -12
- package/lib/clearingHouse.d.ts +26 -23
- package/lib/clearingHouse.js +290 -652
- package/lib/clearingHouseUser.d.ts +1 -1
- package/lib/clearingHouseUser.js +9 -12
- package/lib/config.js +1 -1
- package/lib/constants/numericConstants.d.ts +7 -0
- package/lib/constants/numericConstants.js +8 -1
- package/lib/constants/spotMarkets.js +4 -4
- package/lib/dlob/DLOB.d.ts +7 -5
- package/lib/dlob/DLOB.js +159 -88
- package/lib/dlob/DLOBNode.d.ts +2 -0
- package/lib/dlob/DLOBNode.js +3 -0
- package/lib/dlob/NodeList.d.ts +2 -1
- package/lib/dlob/NodeList.js +10 -4
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +14 -0
- package/lib/idl/clearing_house.json +1091 -611
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +31 -28
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +6 -6
- package/lib/math/spotBalance.js +7 -8
- package/lib/math/trade.js +11 -11
- package/lib/types.d.ts +118 -41
- package/lib/types.js +34 -4
- package/package.json +4 -2
- package/src/admin.ts +129 -29
- package/src/clearingHouse.ts +380 -787
- package/src/clearingHouseUser.ts +11 -14
- package/src/config.ts +1 -1
- package/src/constants/numericConstants.ts +7 -0
- package/src/constants/spotMarkets.ts +5 -4
- package/src/dlob/DLOB.ts +221 -103
- package/src/dlob/DLOBNode.ts +5 -0
- package/src/dlob/NodeList.ts +15 -5
- package/src/events/types.ts +3 -0
- package/src/factory/bigNum.ts +23 -0
- package/src/idl/clearing_house.json +1091 -611
- package/src/math/amm.ts +42 -29
- package/src/math/market.ts +9 -4
- package/src/math/spotBalance.ts +11 -8
- package/src/math/trade.ts +11 -11
- package/src/types.ts +82 -41
- package/tests/bn/test.ts +13 -8
- package/tests/dlob/helpers.ts +56 -33
- package/tests/dlob/test.ts +1397 -495
- package/src/assert/assert.js +0 -9
- package/src/events/eventList.js +0 -77
- package/src/examples/makeTradeExample.js +0 -157
- package/src/token/index.js +0 -38
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/util/computeUnits.js +0 -27
- package/src/util/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
package/tests/bn/test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BN } from '
|
|
1
|
+
import { BN } from '../../src';
|
|
2
2
|
import { expect } from 'chai';
|
|
3
3
|
import { BigNum } from '../../src/factory/bigNum';
|
|
4
4
|
import {
|
|
@@ -77,6 +77,8 @@ describe('BigNum Tests', () => {
|
|
|
77
77
|
expect(val.toPrecision(5)).to.equal('1234.5');
|
|
78
78
|
expect(val.toPrecision(11)).to.equal('1234.5678900');
|
|
79
79
|
|
|
80
|
+
expect(BigNum.from('1234').toPrecision(5)).to.equal('1234.0');
|
|
81
|
+
|
|
80
82
|
// Case 2
|
|
81
83
|
const val2 = BigNum.from(1, 5);
|
|
82
84
|
|
|
@@ -99,8 +101,8 @@ describe('BigNum Tests', () => {
|
|
|
99
101
|
.shift(AMM_RESERVE_PRECISION_EXP)
|
|
100
102
|
.div(BigNum.from(entryPriceNum * 10 ** 8));
|
|
101
103
|
|
|
102
|
-
expect(val4.toString()).to.equal('
|
|
103
|
-
expect(val4.print()).to.equal('0.
|
|
104
|
+
expect(val4.toString()).to.equal('25000000');
|
|
105
|
+
expect(val4.print()).to.equal('0.025000000');
|
|
104
106
|
expect(val4.toNum().toFixed(3)).to.equal('0.025');
|
|
105
107
|
expect(val4.toPrecision(4)).to.equal('0.025');
|
|
106
108
|
|
|
@@ -118,8 +120,8 @@ describe('BigNum Tests', () => {
|
|
|
118
120
|
expect(BigNum.fromPrint('1').toMillified(5)).to.equal('1.0000');
|
|
119
121
|
expect(BigNum.fromPrint('12').toMillified(5)).to.equal('12.000');
|
|
120
122
|
expect(BigNum.fromPrint('123').toMillified(5)).to.equal('123.00');
|
|
121
|
-
expect(BigNum.fromPrint('1234').toMillified(5)).to.equal('
|
|
122
|
-
expect(BigNum.fromPrint('12345').toMillified(5)).to.equal('
|
|
123
|
+
expect(BigNum.fromPrint('1234').toMillified(5)).to.equal('1234.0');
|
|
124
|
+
expect(BigNum.fromPrint('12345').toMillified(5)).to.equal('12345');
|
|
123
125
|
expect(BigNum.fromPrint('123456').toMillified(5)).to.equal('123.45K');
|
|
124
126
|
expect(BigNum.fromPrint('1234567').toMillified(5)).to.equal('1.2345M');
|
|
125
127
|
expect(BigNum.fromPrint('12345678').toMillified(5)).to.equal('12.345M');
|
|
@@ -142,8 +144,8 @@ describe('BigNum Tests', () => {
|
|
|
142
144
|
const baseAmountVal1 = '14.33';
|
|
143
145
|
const val1 = BigNum.fromPrint(baseAmountVal1, BASE_PRECISION_EXP);
|
|
144
146
|
|
|
145
|
-
expect(val1.toString()).to.equal('
|
|
146
|
-
expect(val1.print()).to.equal('14.
|
|
147
|
+
expect(val1.toString()).to.equal('14330000000');
|
|
148
|
+
expect(val1.print()).to.equal('14.330000000');
|
|
147
149
|
|
|
148
150
|
const baseAmountVal2 = '34.1';
|
|
149
151
|
const val2 = BigNum.fromPrint(baseAmountVal2, BASE_PRECISION_EXP);
|
|
@@ -225,7 +227,7 @@ describe('BigNum Tests', () => {
|
|
|
225
227
|
.shift(AMM_RESERVE_PRECISION_EXP)
|
|
226
228
|
.div(BigNum.from(entryPriceNum * 10 ** 8));
|
|
227
229
|
|
|
228
|
-
expect(val.toString()).to.equal('
|
|
230
|
+
expect(val.toString()).to.equal('25000000');
|
|
229
231
|
expect(val.printShort()).to.equal('0.025');
|
|
230
232
|
|
|
231
233
|
const val2 = BigNum.from(10000, 4);
|
|
@@ -260,5 +262,8 @@ describe('BigNum Tests', () => {
|
|
|
260
262
|
|
|
261
263
|
const val9 = BigNum.from('1000000000123', 6);
|
|
262
264
|
expect(val9.prettyPrint()).to.equal('1,000,000.000123');
|
|
265
|
+
|
|
266
|
+
const val10 = BigNum.from('100000000000', 6);
|
|
267
|
+
expect(val10.prettyPrint(true)).to.equal('100,000');
|
|
263
268
|
});
|
|
264
269
|
});
|
package/tests/dlob/helpers.ts
CHANGED
|
@@ -1,36 +1,42 @@
|
|
|
1
1
|
import { PublicKey } from '@solana/web3.js';
|
|
2
2
|
import {
|
|
3
|
+
AMM,
|
|
4
|
+
AssetTier,
|
|
5
|
+
PerpPosition,
|
|
3
6
|
BN,
|
|
4
7
|
PerpMarketAccount,
|
|
5
8
|
SpotMarketAccount,
|
|
6
9
|
MarketStatus,
|
|
7
10
|
ContractType,
|
|
8
|
-
DevnetPerpMarkets,
|
|
9
11
|
OracleSource,
|
|
10
12
|
DevnetSpotMarkets,
|
|
13
|
+
BASE_PRECISION,
|
|
14
|
+
QUOTE_PRECISION,
|
|
15
|
+
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
11
16
|
} from '../../src';
|
|
12
17
|
|
|
13
|
-
export const mockPerpPosition = {
|
|
18
|
+
export const mockPerpPosition: PerpPosition = {
|
|
14
19
|
baseAssetAmount: new BN(0),
|
|
15
|
-
remainderBaseAssetAmount: new BN(0),
|
|
16
20
|
lastCumulativeFundingRate: new BN(0),
|
|
17
|
-
marketIndex:
|
|
21
|
+
marketIndex: 0,
|
|
18
22
|
quoteAssetAmount: new BN(0),
|
|
19
23
|
quoteEntryAmount: new BN(0),
|
|
20
|
-
openOrders:
|
|
24
|
+
openOrders: 0,
|
|
21
25
|
openBids: new BN(0),
|
|
22
26
|
openAsks: new BN(0),
|
|
23
27
|
settledPnl: new BN(0),
|
|
24
28
|
lpShares: new BN(0),
|
|
25
|
-
|
|
29
|
+
remainderBaseAssetAmount: 0,
|
|
26
30
|
lastNetBaseAssetAmountPerLp: new BN(0),
|
|
27
31
|
lastNetQuoteAssetAmountPerLp: new BN(0),
|
|
28
32
|
};
|
|
29
33
|
|
|
30
|
-
export const mockAMM = {
|
|
34
|
+
export const mockAMM: AMM = {
|
|
31
35
|
/* these values create a bid/ask price of 12 */
|
|
32
|
-
baseAssetReserve: new BN(
|
|
33
|
-
quoteAssetReserve: new BN(
|
|
36
|
+
baseAssetReserve: new BN(1).mul(BASE_PRECISION),
|
|
37
|
+
quoteAssetReserve: new BN(12)
|
|
38
|
+
.mul(QUOTE_PRECISION)
|
|
39
|
+
.mul(AMM_TO_QUOTE_PRECISION_RATIO),
|
|
34
40
|
sqrtK: new BN(1),
|
|
35
41
|
pegMultiplier: new BN(1),
|
|
36
42
|
maxSlippageRatio: 1_000_000,
|
|
@@ -49,7 +55,7 @@ export const mockAMM = {
|
|
|
49
55
|
lastOraclePriceTwap5min: new BN(0),
|
|
50
56
|
lastOraclePriceTwapTs: new BN(0),
|
|
51
57
|
},
|
|
52
|
-
|
|
58
|
+
lastOracleReservePriceSpreadPct: new BN(0),
|
|
53
59
|
lastOracleConfPct: new BN(0),
|
|
54
60
|
oracle: PublicKey.default,
|
|
55
61
|
oracleSource: OracleSource.PYTH,
|
|
@@ -57,8 +63,6 @@ export const mockAMM = {
|
|
|
57
63
|
cumulativeFundingRateLong: new BN(0),
|
|
58
64
|
cumulativeFundingRateShort: new BN(0),
|
|
59
65
|
cumulativeFundingRateLp: new BN(0),
|
|
60
|
-
cumulativeRepegRebateLong: new BN(0),
|
|
61
|
-
cumulativeRepegRebateShort: new BN(0),
|
|
62
66
|
totalFeeMinusDistributions: new BN(0),
|
|
63
67
|
totalFeeWithdrawn: new BN(0),
|
|
64
68
|
totalFee: new BN(0),
|
|
@@ -78,6 +82,7 @@ export const mockAMM = {
|
|
|
78
82
|
terminalQuoteAssetReserve: new BN(0),
|
|
79
83
|
feePool: {
|
|
80
84
|
balance: new BN(0),
|
|
85
|
+
marketIndex: 0,
|
|
81
86
|
},
|
|
82
87
|
totalExchangeFee: new BN(0),
|
|
83
88
|
totalMmFee: new BN(0),
|
|
@@ -103,7 +108,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
103
108
|
contractType: ContractType.PERPETUAL,
|
|
104
109
|
expiryTs: new BN(0),
|
|
105
110
|
settlementPrice: new BN(0),
|
|
106
|
-
marketIndex:
|
|
111
|
+
marketIndex: 0,
|
|
107
112
|
pubkey: PublicKey.default,
|
|
108
113
|
amm: mockAMM,
|
|
109
114
|
baseAssetAmount: new BN(0),
|
|
@@ -115,6 +120,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
115
120
|
nextFillRecordId: new BN(0),
|
|
116
121
|
pnlPool: {
|
|
117
122
|
balance: new BN(0),
|
|
123
|
+
marketIndex: 0,
|
|
118
124
|
},
|
|
119
125
|
ifLiquidationFee: new BN(0),
|
|
120
126
|
liquidatorFee: new BN(0),
|
|
@@ -134,7 +140,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
134
140
|
contractType: ContractType.PERPETUAL,
|
|
135
141
|
expiryTs: new BN(0),
|
|
136
142
|
settlementPrice: new BN(0),
|
|
137
|
-
marketIndex:
|
|
143
|
+
marketIndex: 1,
|
|
138
144
|
pubkey: PublicKey.default,
|
|
139
145
|
amm: mockAMM,
|
|
140
146
|
baseAssetAmount: new BN(0),
|
|
@@ -146,6 +152,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
146
152
|
nextFillRecordId: new BN(0),
|
|
147
153
|
pnlPool: {
|
|
148
154
|
balance: new BN(0),
|
|
155
|
+
marketIndex: 0,
|
|
149
156
|
},
|
|
150
157
|
ifLiquidationFee: new BN(0),
|
|
151
158
|
liquidatorFee: new BN(0),
|
|
@@ -165,7 +172,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
165
172
|
contractType: ContractType.PERPETUAL,
|
|
166
173
|
expiryTs: new BN(0),
|
|
167
174
|
settlementPrice: new BN(0),
|
|
168
|
-
marketIndex:
|
|
175
|
+
marketIndex: 2,
|
|
169
176
|
pubkey: PublicKey.default,
|
|
170
177
|
amm: mockAMM,
|
|
171
178
|
baseAssetAmount: new BN(0),
|
|
@@ -177,6 +184,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
177
184
|
nextFillRecordId: new BN(0),
|
|
178
185
|
pnlPool: {
|
|
179
186
|
balance: new BN(0),
|
|
187
|
+
marketIndex: 0,
|
|
180
188
|
},
|
|
181
189
|
ifLiquidationFee: new BN(0),
|
|
182
190
|
liquidatorFee: new BN(0),
|
|
@@ -195,7 +203,10 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
|
|
|
195
203
|
|
|
196
204
|
export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
197
205
|
{
|
|
198
|
-
|
|
206
|
+
status: MarketStatus.ACTIVE,
|
|
207
|
+
assetTier: AssetTier.COLLATERAL,
|
|
208
|
+
maxTokenDeposits: new BN(100),
|
|
209
|
+
marketIndex: 0,
|
|
199
210
|
pubkey: PublicKey.default,
|
|
200
211
|
mint: DevnetSpotMarkets[0].mint,
|
|
201
212
|
vault: PublicKey.default,
|
|
@@ -203,17 +214,18 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
203
214
|
insuranceWithdrawEscrowPeriod: new BN(0),
|
|
204
215
|
revenuePool: {
|
|
205
216
|
balance: new BN(0),
|
|
217
|
+
marketIndex: 0,
|
|
206
218
|
},
|
|
207
219
|
totalIfShares: new BN(0),
|
|
208
220
|
userIfShares: new BN(0),
|
|
209
|
-
userIfFactor:
|
|
210
|
-
totalIfFactor:
|
|
221
|
+
userIfFactor: 0,
|
|
222
|
+
totalIfFactor: 0,
|
|
211
223
|
ifLiquidationFee: new BN(0),
|
|
212
224
|
liquidatorFee: new BN(0),
|
|
213
225
|
decimals: 6,
|
|
214
|
-
optimalUtilization:
|
|
215
|
-
optimalBorrowRate:
|
|
216
|
-
maxBorrowRate:
|
|
226
|
+
optimalUtilization: 0,
|
|
227
|
+
optimalBorrowRate: 0,
|
|
228
|
+
maxBorrowRate: 0,
|
|
217
229
|
cumulativeDepositInterest: new BN(0),
|
|
218
230
|
cumulativeBorrowInterest: new BN(0),
|
|
219
231
|
depositBalance: new BN(0),
|
|
@@ -234,6 +246,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
234
246
|
nextFillRecordId: new BN(0),
|
|
235
247
|
spotFeePool: {
|
|
236
248
|
balance: new BN(0),
|
|
249
|
+
marketIndex: 0,
|
|
237
250
|
},
|
|
238
251
|
totalSpotFee: new BN(0),
|
|
239
252
|
oracleSource: OracleSource.PYTH,
|
|
@@ -254,7 +267,10 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
254
267
|
},
|
|
255
268
|
},
|
|
256
269
|
{
|
|
257
|
-
|
|
270
|
+
status: MarketStatus.ACTIVE,
|
|
271
|
+
assetTier: AssetTier.CROSS,
|
|
272
|
+
maxTokenDeposits: new BN(100),
|
|
273
|
+
marketIndex: 1,
|
|
258
274
|
pubkey: PublicKey.default,
|
|
259
275
|
mint: DevnetSpotMarkets[1].mint,
|
|
260
276
|
vault: PublicKey.default,
|
|
@@ -262,17 +278,18 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
262
278
|
insuranceWithdrawEscrowPeriod: new BN(0),
|
|
263
279
|
revenuePool: {
|
|
264
280
|
balance: new BN(0),
|
|
281
|
+
marketIndex: 0,
|
|
265
282
|
},
|
|
266
283
|
totalIfShares: new BN(0),
|
|
267
284
|
userIfShares: new BN(0),
|
|
268
|
-
userIfFactor:
|
|
269
|
-
totalIfFactor:
|
|
285
|
+
userIfFactor: 0,
|
|
286
|
+
totalIfFactor: 0,
|
|
270
287
|
ifLiquidationFee: new BN(0),
|
|
271
288
|
liquidatorFee: new BN(0),
|
|
272
289
|
decimals: 9,
|
|
273
|
-
optimalUtilization:
|
|
274
|
-
optimalBorrowRate:
|
|
275
|
-
maxBorrowRate:
|
|
290
|
+
optimalUtilization: 0,
|
|
291
|
+
optimalBorrowRate: 0,
|
|
292
|
+
maxBorrowRate: 0,
|
|
276
293
|
cumulativeDepositInterest: new BN(0),
|
|
277
294
|
cumulativeBorrowInterest: new BN(0),
|
|
278
295
|
depositBalance: new BN(0),
|
|
@@ -293,6 +310,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
293
310
|
nextFillRecordId: new BN(0),
|
|
294
311
|
spotFeePool: {
|
|
295
312
|
balance: new BN(0),
|
|
313
|
+
marketIndex: 0,
|
|
296
314
|
},
|
|
297
315
|
totalSpotFee: new BN(0),
|
|
298
316
|
oracleSource: OracleSource.PYTH,
|
|
@@ -313,7 +331,10 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
313
331
|
},
|
|
314
332
|
},
|
|
315
333
|
{
|
|
316
|
-
|
|
334
|
+
status: MarketStatus.ACTIVE,
|
|
335
|
+
assetTier: AssetTier.PROTECTED,
|
|
336
|
+
maxTokenDeposits: new BN(100),
|
|
337
|
+
marketIndex: 2,
|
|
317
338
|
pubkey: PublicKey.default,
|
|
318
339
|
mint: DevnetSpotMarkets[2].mint,
|
|
319
340
|
vault: PublicKey.default,
|
|
@@ -321,17 +342,18 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
321
342
|
insuranceWithdrawEscrowPeriod: new BN(0),
|
|
322
343
|
revenuePool: {
|
|
323
344
|
balance: new BN(0),
|
|
345
|
+
marketIndex: 0,
|
|
324
346
|
},
|
|
325
347
|
totalIfShares: new BN(0),
|
|
326
348
|
userIfShares: new BN(0),
|
|
327
|
-
userIfFactor:
|
|
328
|
-
totalIfFactor:
|
|
349
|
+
userIfFactor: 0,
|
|
350
|
+
totalIfFactor: 0,
|
|
329
351
|
ifLiquidationFee: new BN(0),
|
|
330
352
|
liquidatorFee: new BN(0),
|
|
331
353
|
decimals: 6,
|
|
332
|
-
optimalUtilization:
|
|
333
|
-
optimalBorrowRate:
|
|
334
|
-
maxBorrowRate:
|
|
354
|
+
optimalUtilization: 0,
|
|
355
|
+
optimalBorrowRate: 0,
|
|
356
|
+
maxBorrowRate: 0,
|
|
335
357
|
cumulativeDepositInterest: new BN(0),
|
|
336
358
|
cumulativeBorrowInterest: new BN(0),
|
|
337
359
|
depositBalance: new BN(0),
|
|
@@ -352,6 +374,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
|
|
|
352
374
|
nextFillRecordId: new BN(0),
|
|
353
375
|
spotFeePool: {
|
|
354
376
|
balance: new BN(0),
|
|
377
|
+
marketIndex: 0,
|
|
355
378
|
},
|
|
356
379
|
totalSpotFee: new BN(0),
|
|
357
380
|
oracleSource: OracleSource.PYTH,
|