@drift-labs/sdk 0.2.0-master.18 → 0.2.0-master.20
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/clearingHouse.d.ts +11 -11
- package/lib/clearingHouse.js +78 -18
- package/lib/clearingHouseUser.js +7 -25
- package/lib/clearingHouseUserStats.d.ts +2 -1
- package/lib/clearingHouseUserStats.js +13 -0
- package/lib/config.js +1 -1
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/idl/clearing_house.json +200 -129
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/lib/math/amm.d.ts +1 -0
- package/lib/math/amm.js +13 -1
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +5 -5
- package/lib/math/orders.d.ts +3 -0
- package/lib/math/orders.js +35 -1
- package/lib/types.d.ts +25 -7
- package/package.json +1 -1
- package/src/clearingHouse.ts +113 -19
- package/src/clearingHouseUser.ts +11 -49
- package/src/clearingHouseUserStats.ts +23 -1
- package/src/config.ts +1 -1
- package/src/events/types.ts +3 -0
- package/src/idl/clearing_house.json +200 -129
- package/src/index.ts +0 -1
- package/src/math/amm.ts +27 -1
- package/src/math/market.ts +6 -6
- package/src/math/orders.ts +96 -1
- package/src/types.ts +27 -7
- package/lib/orders.d.ts +0 -7
- package/lib/orders.js +0 -59
- package/src/events/types.js +0 -20
- package/src/math/amm.js +0 -369
- package/src/orders.ts +0 -91
- package/src/types.js +0 -125
package/src/index.ts
CHANGED
|
@@ -41,7 +41,6 @@ export * from './math/trade';
|
|
|
41
41
|
export * from './math/orders';
|
|
42
42
|
export * from './math/repeg';
|
|
43
43
|
export * from './math/margin';
|
|
44
|
-
export * from './orders';
|
|
45
44
|
export * from './orderParams';
|
|
46
45
|
export * from './slot/SlotSubscriber';
|
|
47
46
|
export * from './wallet';
|
package/src/math/amm.ts
CHANGED
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
isVariant,
|
|
20
20
|
} from '../types';
|
|
21
21
|
import { assert } from '../assert/assert';
|
|
22
|
-
import { squareRootBN } from '..';
|
|
22
|
+
import { squareRootBN, standardizeBaseAssetAmount } from '..';
|
|
23
23
|
|
|
24
24
|
import { OraclePriceData } from '../oracles/types';
|
|
25
25
|
import {
|
|
@@ -623,3 +623,29 @@ export function calculateQuoteAssetAmountSwapped(
|
|
|
623
623
|
|
|
624
624
|
return quoteAssetAmount;
|
|
625
625
|
}
|
|
626
|
+
|
|
627
|
+
export function calculateMaxBaseAssetAmountFillable(
|
|
628
|
+
amm: AMM,
|
|
629
|
+
orderDirection: PositionDirection
|
|
630
|
+
): BN {
|
|
631
|
+
const maxFillSize = amm.baseAssetReserve.div(
|
|
632
|
+
new BN(amm.maxBaseAssetAmountRatio)
|
|
633
|
+
);
|
|
634
|
+
let maxBaseAssetAmountOnSide: BN;
|
|
635
|
+
if (isVariant(orderDirection, 'long')) {
|
|
636
|
+
maxBaseAssetAmountOnSide = BN.max(
|
|
637
|
+
ZERO,
|
|
638
|
+
amm.maxBaseAssetReserve.sub(amm.baseAssetReserve)
|
|
639
|
+
);
|
|
640
|
+
} else {
|
|
641
|
+
maxBaseAssetAmountOnSide = BN.max(
|
|
642
|
+
ZERO,
|
|
643
|
+
amm.baseAssetReserve.sub(amm.minBaseAssetReserve)
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
return standardizeBaseAssetAmount(
|
|
648
|
+
BN.min(maxFillSize, maxBaseAssetAmountOnSide),
|
|
649
|
+
amm.baseAssetAmountStepSize
|
|
650
|
+
);
|
|
651
|
+
}
|
package/src/math/market.ts
CHANGED
|
@@ -139,9 +139,9 @@ export function calculateMarketMarginRatio(
|
|
|
139
139
|
return marginRatio;
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
export function
|
|
142
|
+
export function calculateUnrealizedAssetWeight(
|
|
143
143
|
market: MarketAccount,
|
|
144
|
-
|
|
144
|
+
unrealizedPnl: BN,
|
|
145
145
|
marginCategory: MarginCategory
|
|
146
146
|
): BN {
|
|
147
147
|
let assetWeight: BN;
|
|
@@ -149,13 +149,13 @@ export function calculateUnsettledAssetWeight(
|
|
|
149
149
|
switch (marginCategory) {
|
|
150
150
|
case 'Initial':
|
|
151
151
|
assetWeight = calculateSizeDiscountAssetWeight(
|
|
152
|
-
|
|
153
|
-
market.
|
|
154
|
-
new BN(market.
|
|
152
|
+
unrealizedPnl,
|
|
153
|
+
market.unrealizedImfFactor,
|
|
154
|
+
new BN(market.unrealizedInitialAssetWeight)
|
|
155
155
|
);
|
|
156
156
|
break;
|
|
157
157
|
case 'Maintenance':
|
|
158
|
-
assetWeight = new BN(market.
|
|
158
|
+
assetWeight = new BN(market.unrealizedMaintenanceAssetWeight);
|
|
159
159
|
break;
|
|
160
160
|
}
|
|
161
161
|
|
package/src/math/orders.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
isOneOfVariant,
|
|
4
|
+
isVariant,
|
|
5
|
+
MarketAccount,
|
|
6
|
+
Order,
|
|
7
|
+
PositionDirection,
|
|
8
|
+
} from '../types';
|
|
3
9
|
import { ZERO, TWO } from '../constants/numericConstants';
|
|
4
10
|
import { BN } from '@project-serum/anchor';
|
|
5
11
|
import { OraclePriceData } from '../oracles/types';
|
|
6
12
|
import { getAuctionPrice, isAuctionComplete } from './auction';
|
|
7
13
|
import { calculateAskPrice, calculateBidPrice } from './market';
|
|
14
|
+
import {
|
|
15
|
+
calculateMaxBaseAssetAmountFillable,
|
|
16
|
+
calculateMaxBaseAssetAmountToTrade,
|
|
17
|
+
} from './amm';
|
|
8
18
|
|
|
9
19
|
export function isOrderRiskIncreasing(
|
|
10
20
|
user: ClearingHouseUser,
|
|
@@ -148,3 +158,88 @@ export function getLimitPrice(
|
|
|
148
158
|
|
|
149
159
|
return limitPrice;
|
|
150
160
|
}
|
|
161
|
+
|
|
162
|
+
export function isFillableByVAMM(
|
|
163
|
+
order: Order,
|
|
164
|
+
market: MarketAccount,
|
|
165
|
+
oraclePriceData: OraclePriceData,
|
|
166
|
+
slot: number
|
|
167
|
+
): boolean {
|
|
168
|
+
return (
|
|
169
|
+
isAuctionComplete(order, slot) &&
|
|
170
|
+
!calculateBaseAssetAmountForAmmToFulfill(
|
|
171
|
+
order,
|
|
172
|
+
market,
|
|
173
|
+
oraclePriceData,
|
|
174
|
+
slot
|
|
175
|
+
).eq(ZERO)
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function calculateBaseAssetAmountForAmmToFulfill(
|
|
180
|
+
order: Order,
|
|
181
|
+
market: MarketAccount,
|
|
182
|
+
oraclePriceData: OraclePriceData,
|
|
183
|
+
slot: number
|
|
184
|
+
): BN {
|
|
185
|
+
if (
|
|
186
|
+
isOneOfVariant(order.orderType, ['triggerMarket', 'triggerLimit']) &&
|
|
187
|
+
order.triggered === false
|
|
188
|
+
) {
|
|
189
|
+
return ZERO;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const limitPrice = getLimitPrice(order, market, oraclePriceData, slot);
|
|
193
|
+
const baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(
|
|
194
|
+
order,
|
|
195
|
+
market,
|
|
196
|
+
limitPrice,
|
|
197
|
+
oraclePriceData
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
const maxBaseAssetAmount = calculateMaxBaseAssetAmountFillable(
|
|
201
|
+
market.amm,
|
|
202
|
+
order.direction
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
return BN.min(maxBaseAssetAmount, baseAssetAmount);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function calculateBaseAssetAmountToFillUpToLimitPrice(
|
|
209
|
+
order: Order,
|
|
210
|
+
market: MarketAccount,
|
|
211
|
+
limitPrice: BN,
|
|
212
|
+
oraclePriceData: OraclePriceData
|
|
213
|
+
): BN {
|
|
214
|
+
const [maxAmountToTrade, direction] = calculateMaxBaseAssetAmountToTrade(
|
|
215
|
+
market.amm,
|
|
216
|
+
limitPrice,
|
|
217
|
+
order.direction,
|
|
218
|
+
oraclePriceData
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const baseAssetAmount = standardizeBaseAssetAmount(
|
|
222
|
+
maxAmountToTrade,
|
|
223
|
+
market.amm.baseAssetAmountStepSize
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
// Check that directions are the same
|
|
227
|
+
const sameDirection = isSameDirection(direction, order.direction);
|
|
228
|
+
if (!sameDirection) {
|
|
229
|
+
return ZERO;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return baseAssetAmount.gt(order.baseAssetAmount)
|
|
233
|
+
? order.baseAssetAmount
|
|
234
|
+
: baseAssetAmount;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function isSameDirection(
|
|
238
|
+
firstDirection: PositionDirection,
|
|
239
|
+
secondDirection: PositionDirection
|
|
240
|
+
): boolean {
|
|
241
|
+
return (
|
|
242
|
+
(isVariant(firstDirection, 'long') && isVariant(secondDirection, 'long')) ||
|
|
243
|
+
(isVariant(firstDirection, 'short') && isVariant(secondDirection, 'short'))
|
|
244
|
+
);
|
|
245
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -106,6 +106,15 @@ export type CandleResolution =
|
|
|
106
106
|
| 'W'
|
|
107
107
|
| 'M';
|
|
108
108
|
|
|
109
|
+
export type NewUserRecord = {
|
|
110
|
+
ts: BN;
|
|
111
|
+
userAuthority: PublicKey;
|
|
112
|
+
user: PublicKey;
|
|
113
|
+
userId: number;
|
|
114
|
+
name: number[];
|
|
115
|
+
referrer: PublicKey;
|
|
116
|
+
};
|
|
117
|
+
|
|
109
118
|
export type DepositRecord = {
|
|
110
119
|
ts: BN;
|
|
111
120
|
userAuthority: PublicKey;
|
|
@@ -117,6 +126,7 @@ export type DepositRecord = {
|
|
|
117
126
|
bankIndex: BN;
|
|
118
127
|
amount: BN;
|
|
119
128
|
oraclePrice: BN;
|
|
129
|
+
referrer: PublicKey;
|
|
120
130
|
from?: PublicKey;
|
|
121
131
|
to?: PublicKey;
|
|
122
132
|
};
|
|
@@ -282,6 +292,9 @@ export type OrderRecord = {
|
|
|
282
292
|
fillerReward: BN;
|
|
283
293
|
quoteAssetAmountSurplus: BN;
|
|
284
294
|
oraclePrice: BN;
|
|
295
|
+
referrer: PublicKey;
|
|
296
|
+
referrerReward: BN;
|
|
297
|
+
refereeDiscount: BN;
|
|
285
298
|
};
|
|
286
299
|
|
|
287
300
|
export type StateAccount = {
|
|
@@ -330,9 +343,9 @@ export type MarketAccount = {
|
|
|
330
343
|
pnlPool: PoolBalance;
|
|
331
344
|
liquidationFee: BN;
|
|
332
345
|
imfFactor: BN;
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
346
|
+
unrealizedImfFactor: BN;
|
|
347
|
+
unrealizedInitialAssetWeight: number;
|
|
348
|
+
unrealizedMaintenanceAssetWeight: number;
|
|
336
349
|
};
|
|
337
350
|
|
|
338
351
|
export type BankAccount = {
|
|
@@ -424,6 +437,8 @@ export type AMM = {
|
|
|
424
437
|
maxSpread: number;
|
|
425
438
|
marketPosition: UserPosition;
|
|
426
439
|
marketPositionPerLp: UserPosition;
|
|
440
|
+
maxBaseAssetReserve: BN;
|
|
441
|
+
minBaseAssetReserve: BN;
|
|
427
442
|
};
|
|
428
443
|
|
|
429
444
|
// # User Account Types
|
|
@@ -455,11 +470,12 @@ export type UserStatsAccount = {
|
|
|
455
470
|
totalFeePaid: BN;
|
|
456
471
|
totalFeeRebate: BN;
|
|
457
472
|
totalTokenDiscount: BN;
|
|
458
|
-
totalReferralReward: BN;
|
|
459
473
|
totalRefereeDiscount: BN;
|
|
460
474
|
};
|
|
461
|
-
authority: PublicKey;
|
|
462
475
|
referrer: PublicKey;
|
|
476
|
+
isReferrer: boolean;
|
|
477
|
+
totalReferrerReward: BN;
|
|
478
|
+
authority: PublicKey;
|
|
463
479
|
};
|
|
464
480
|
|
|
465
481
|
export type UserAccount = {
|
|
@@ -472,6 +488,7 @@ export type UserAccount = {
|
|
|
472
488
|
beingLiquidated: boolean;
|
|
473
489
|
bankrupt: boolean;
|
|
474
490
|
nextLiquidationId: number;
|
|
491
|
+
nextOrderId: BN;
|
|
475
492
|
};
|
|
476
493
|
|
|
477
494
|
export type UserBankBalance = {
|
|
@@ -499,9 +516,7 @@ export type Order = {
|
|
|
499
516
|
triggerPrice: BN;
|
|
500
517
|
triggerCondition: OrderTriggerCondition;
|
|
501
518
|
triggered: boolean;
|
|
502
|
-
discountTier: OrderDiscountTier;
|
|
503
519
|
existingPositionDirection: PositionDirection;
|
|
504
|
-
referrer: PublicKey;
|
|
505
520
|
postOnly: boolean;
|
|
506
521
|
immediateOrCancel: boolean;
|
|
507
522
|
oraclePriceOffset: BN;
|
|
@@ -577,6 +592,11 @@ export type TakerInfo = {
|
|
|
577
592
|
order: Order;
|
|
578
593
|
};
|
|
579
594
|
|
|
595
|
+
export type ReferrerInfo = {
|
|
596
|
+
referrer: PublicKey;
|
|
597
|
+
referrerStats: PublicKey;
|
|
598
|
+
};
|
|
599
|
+
|
|
580
600
|
// # Misc Types
|
|
581
601
|
export interface IWallet {
|
|
582
602
|
signTransaction(tx: Transaction): Promise<Transaction>;
|
package/lib/orders.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
|
-
import { MarketAccount, Order } from './types';
|
|
3
|
-
import { BN } from '.';
|
|
4
|
-
import { OraclePriceData } from '.';
|
|
5
|
-
export declare function calculateBaseAssetAmountMarketCanExecute(market: MarketAccount, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
6
|
-
export declare function calculateAmountToTradeForLimit(market: MarketAccount, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
7
|
-
export declare function calculateAmountToTradeForTriggerLimit(market: MarketAccount, order: Order): BN;
|
package/lib/orders.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateAmountToTradeForTriggerLimit = exports.calculateAmountToTradeForLimit = exports.calculateBaseAssetAmountMarketCanExecute = void 0;
|
|
4
|
-
const types_1 = require("./types");
|
|
5
|
-
const _1 = require(".");
|
|
6
|
-
const numericConstants_1 = require("./constants/numericConstants");
|
|
7
|
-
const amm_1 = require("./math/amm");
|
|
8
|
-
function calculateBaseAssetAmountMarketCanExecute(market, order, oraclePriceData) {
|
|
9
|
-
if ((0, types_1.isVariant)(order.orderType, 'limit')) {
|
|
10
|
-
return calculateAmountToTradeForLimit(market, order, oraclePriceData);
|
|
11
|
-
}
|
|
12
|
-
else if ((0, types_1.isVariant)(order.orderType, 'triggerLimit')) {
|
|
13
|
-
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
14
|
-
}
|
|
15
|
-
else if ((0, types_1.isVariant)(order.orderType, 'market')) {
|
|
16
|
-
return numericConstants_1.ZERO;
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
return calculateAmountToTradeForTriggerMarket(market, order);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
exports.calculateBaseAssetAmountMarketCanExecute = calculateBaseAssetAmountMarketCanExecute;
|
|
23
|
-
function calculateAmountToTradeForLimit(market, order, oraclePriceData) {
|
|
24
|
-
let limitPrice = order.price;
|
|
25
|
-
if (!order.oraclePriceOffset.eq(numericConstants_1.ZERO)) {
|
|
26
|
-
if (!oraclePriceData) {
|
|
27
|
-
throw Error('Cant calculate limit price for oracle offset oracle without OraclePriceData');
|
|
28
|
-
}
|
|
29
|
-
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
30
|
-
}
|
|
31
|
-
const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(market.amm, limitPrice, order.direction, oraclePriceData);
|
|
32
|
-
const baseAssetAmount = (0, _1.standardizeBaseAssetAmount)(maxAmountToTrade, market.amm.baseAssetAmountStepSize);
|
|
33
|
-
// Check that directions are the same
|
|
34
|
-
const sameDirection = isSameDirection(direction, order.direction);
|
|
35
|
-
if (!sameDirection) {
|
|
36
|
-
return numericConstants_1.ZERO;
|
|
37
|
-
}
|
|
38
|
-
return baseAssetAmount.gt(order.baseAssetAmount)
|
|
39
|
-
? order.baseAssetAmount
|
|
40
|
-
: baseAssetAmount;
|
|
41
|
-
}
|
|
42
|
-
exports.calculateAmountToTradeForLimit = calculateAmountToTradeForLimit;
|
|
43
|
-
function calculateAmountToTradeForTriggerLimit(market, order) {
|
|
44
|
-
if (!order.triggered) {
|
|
45
|
-
return numericConstants_1.ZERO;
|
|
46
|
-
}
|
|
47
|
-
return calculateAmountToTradeForLimit(market, order);
|
|
48
|
-
}
|
|
49
|
-
exports.calculateAmountToTradeForTriggerLimit = calculateAmountToTradeForTriggerLimit;
|
|
50
|
-
function isSameDirection(firstDirection, secondDirection) {
|
|
51
|
-
return (((0, types_1.isVariant)(firstDirection, 'long') && (0, types_1.isVariant)(secondDirection, 'long')) ||
|
|
52
|
-
((0, types_1.isVariant)(firstDirection, 'short') && (0, types_1.isVariant)(secondDirection, 'short')));
|
|
53
|
-
}
|
|
54
|
-
function calculateAmountToTradeForTriggerMarket(market, order) {
|
|
55
|
-
if (!order.triggered) {
|
|
56
|
-
return numericConstants_1.ZERO;
|
|
57
|
-
}
|
|
58
|
-
return order.baseAssetAmount;
|
|
59
|
-
}
|
package/src/events/types.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultEventSubscriptionOptions = void 0;
|
|
4
|
-
exports.DefaultEventSubscriptionOptions = {
|
|
5
|
-
eventTypes: [
|
|
6
|
-
'DepositRecord',
|
|
7
|
-
'FundingPaymentRecord',
|
|
8
|
-
'LiquidationRecord',
|
|
9
|
-
'OrderRecord',
|
|
10
|
-
'FundingRateRecord',
|
|
11
|
-
],
|
|
12
|
-
maxEventsPerType: 4096,
|
|
13
|
-
orderBy: 'blockchain',
|
|
14
|
-
orderDir: 'asc',
|
|
15
|
-
commitment: 'confirmed',
|
|
16
|
-
maxTx: 4096,
|
|
17
|
-
logProviderConfig: {
|
|
18
|
-
type: 'websocket',
|
|
19
|
-
},
|
|
20
|
-
};
|