@drift-labs/sdk 0.2.0-master.19 → 0.2.0-master.21
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/accounts/types.d.ts +0 -1
- package/lib/clearingHouse.d.ts +13 -13
- package/lib/clearingHouse.js +98 -37
- package/lib/clearingHouseUser.d.ts +9 -1
- package/lib/clearingHouseUser.js +64 -45
- 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 +4 -0
- package/lib/math/orders.js +45 -2
- package/lib/types.d.ts +28 -7
- package/my-script/.env +7 -0
- package/my-script/getUserStats.ts +124 -0
- package/my-script/multiConnections.ts +137 -0
- package/my-script/test-regex.ts +11 -0
- package/my-script/utils.ts +52 -0
- package/my-script/ww18NdhuLSQPCrHSx7V68eZJpe2y311heWeXJfSmP3Q.json +1 -0
- package/my-script/ww2z7N9TG1PLLUQGQF2VKzCFaPtQ5FBhRfeEAuy6c5C.json +1 -0
- package/my-script/ww3StJtTubhwssqAhvSSAc5ifCgKjzmF8hz7Gt2DmSa.json +1 -0
- package/package.json +1 -1
- package/src/clearingHouse.ts +132 -42
- package/src/clearingHouseUser.ts +78 -91
- 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 +114 -2
- package/src/types.ts +30 -7
- package/lib/orders.d.ts +0 -7
- package/lib/orders.js +0 -59
- package/src/addresses/marketAddresses.js +0 -26
- package/src/assert/assert.js +0 -9
- package/src/constants/banks.js +0 -42
- package/src/constants/markets.js +0 -42
- package/src/constants/numericConstants.js +0 -41
- package/src/events/eventList.js +0 -77
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/fetchLogs.js +0 -50
- package/src/events/pollingLogProvider.js +0 -64
- package/src/events/sort.js +0 -44
- package/src/events/txEventCache.js +0 -71
- package/src/events/types.js +0 -20
- package/src/events/webSocketLogProvider.js +0 -41
- package/src/examples/makeTradeExample.js +0 -80
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/math/amm.js +0 -369
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/oracles.js +0 -26
- package/src/math/repeg.js +0 -128
- package/src/math/state.js +0 -15
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/oracles/types.js +0 -2
- package/src/orderParams.js +0 -20
- package/src/orders.ts +0 -91
- package/src/slot/SlotSubscriber.js +0 -39
- package/src/token/index.js +0 -38
- package/src/tokenFaucet.js +0 -189
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/userName.js +0 -20
- 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/src/wallet.js +0 -35
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,
|
|
@@ -129,7 +139,7 @@ export function getLimitPrice(
|
|
|
129
139
|
if (!order.oraclePriceOffset.eq(ZERO)) {
|
|
130
140
|
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
131
141
|
} else if (isOneOfVariant(order.orderType, ['market', 'triggerMarket'])) {
|
|
132
|
-
if (isAuctionComplete(order, slot)) {
|
|
142
|
+
if (!isAuctionComplete(order, slot)) {
|
|
133
143
|
limitPrice = getAuctionPrice(order, slot);
|
|
134
144
|
} else if (!order.price.eq(ZERO)) {
|
|
135
145
|
limitPrice = order.price;
|
|
@@ -148,3 +158,105 @@ 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
|
+
maxAuctionDuration: number
|
|
168
|
+
): boolean {
|
|
169
|
+
return (
|
|
170
|
+
(isAuctionComplete(order, slot) &&
|
|
171
|
+
!calculateBaseAssetAmountForAmmToFulfill(
|
|
172
|
+
order,
|
|
173
|
+
market,
|
|
174
|
+
oraclePriceData,
|
|
175
|
+
slot
|
|
176
|
+
).eq(ZERO)) ||
|
|
177
|
+
isOrderExpired(order, slot, maxAuctionDuration)
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function calculateBaseAssetAmountForAmmToFulfill(
|
|
182
|
+
order: Order,
|
|
183
|
+
market: MarketAccount,
|
|
184
|
+
oraclePriceData: OraclePriceData,
|
|
185
|
+
slot: number
|
|
186
|
+
): BN {
|
|
187
|
+
if (
|
|
188
|
+
isOneOfVariant(order.orderType, ['triggerMarket', 'triggerLimit']) &&
|
|
189
|
+
order.triggered === false
|
|
190
|
+
) {
|
|
191
|
+
return ZERO;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const limitPrice = getLimitPrice(order, market, oraclePriceData, slot);
|
|
195
|
+
const baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(
|
|
196
|
+
order,
|
|
197
|
+
market,
|
|
198
|
+
limitPrice,
|
|
199
|
+
oraclePriceData
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const maxBaseAssetAmount = calculateMaxBaseAssetAmountFillable(
|
|
203
|
+
market.amm,
|
|
204
|
+
order.direction
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
return BN.min(maxBaseAssetAmount, baseAssetAmount);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function calculateBaseAssetAmountToFillUpToLimitPrice(
|
|
211
|
+
order: Order,
|
|
212
|
+
market: MarketAccount,
|
|
213
|
+
limitPrice: BN,
|
|
214
|
+
oraclePriceData: OraclePriceData
|
|
215
|
+
): BN {
|
|
216
|
+
const [maxAmountToTrade, direction] = calculateMaxBaseAssetAmountToTrade(
|
|
217
|
+
market.amm,
|
|
218
|
+
limitPrice,
|
|
219
|
+
order.direction,
|
|
220
|
+
oraclePriceData
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
const baseAssetAmount = standardizeBaseAssetAmount(
|
|
224
|
+
maxAmountToTrade,
|
|
225
|
+
market.amm.baseAssetAmountStepSize
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
// Check that directions are the same
|
|
229
|
+
const sameDirection = isSameDirection(direction, order.direction);
|
|
230
|
+
if (!sameDirection) {
|
|
231
|
+
return ZERO;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return baseAssetAmount.gt(order.baseAssetAmount)
|
|
235
|
+
? order.baseAssetAmount
|
|
236
|
+
: baseAssetAmount;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function isSameDirection(
|
|
240
|
+
firstDirection: PositionDirection,
|
|
241
|
+
secondDirection: PositionDirection
|
|
242
|
+
): boolean {
|
|
243
|
+
return (
|
|
244
|
+
(isVariant(firstDirection, 'long') && isVariant(secondDirection, 'long')) ||
|
|
245
|
+
(isVariant(firstDirection, 'short') && isVariant(secondDirection, 'short'))
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function isOrderExpired(
|
|
250
|
+
order: Order,
|
|
251
|
+
slot: number,
|
|
252
|
+
maxAuctionDuration: number
|
|
253
|
+
): boolean {
|
|
254
|
+
if (
|
|
255
|
+
!isVariant(order.orderType, 'market') ||
|
|
256
|
+
!isVariant(order.status, 'open')
|
|
257
|
+
) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return new BN(slot).sub(order.slot).gt(new BN(maxAuctionDuration));
|
|
262
|
+
}
|
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 = {
|
|
@@ -313,6 +326,8 @@ export type StateAccount = {
|
|
|
313
326
|
numberOfMarkets: BN;
|
|
314
327
|
numberOfBanks: BN;
|
|
315
328
|
minOrderQuoteAssetAmount: BN;
|
|
329
|
+
maxAuctionDuration: number;
|
|
330
|
+
minAuctionDuration: number;
|
|
316
331
|
};
|
|
317
332
|
|
|
318
333
|
export type MarketAccount = {
|
|
@@ -330,9 +345,9 @@ export type MarketAccount = {
|
|
|
330
345
|
pnlPool: PoolBalance;
|
|
331
346
|
liquidationFee: BN;
|
|
332
347
|
imfFactor: BN;
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
348
|
+
unrealizedImfFactor: BN;
|
|
349
|
+
unrealizedInitialAssetWeight: number;
|
|
350
|
+
unrealizedMaintenanceAssetWeight: number;
|
|
336
351
|
};
|
|
337
352
|
|
|
338
353
|
export type BankAccount = {
|
|
@@ -424,6 +439,8 @@ export type AMM = {
|
|
|
424
439
|
maxSpread: number;
|
|
425
440
|
marketPosition: UserPosition;
|
|
426
441
|
marketPositionPerLp: UserPosition;
|
|
442
|
+
maxBaseAssetReserve: BN;
|
|
443
|
+
minBaseAssetReserve: BN;
|
|
427
444
|
};
|
|
428
445
|
|
|
429
446
|
// # User Account Types
|
|
@@ -455,11 +472,12 @@ export type UserStatsAccount = {
|
|
|
455
472
|
totalFeePaid: BN;
|
|
456
473
|
totalFeeRebate: BN;
|
|
457
474
|
totalTokenDiscount: BN;
|
|
458
|
-
totalReferralReward: BN;
|
|
459
475
|
totalRefereeDiscount: BN;
|
|
460
476
|
};
|
|
461
|
-
authority: PublicKey;
|
|
462
477
|
referrer: PublicKey;
|
|
478
|
+
isReferrer: boolean;
|
|
479
|
+
totalReferrerReward: BN;
|
|
480
|
+
authority: PublicKey;
|
|
463
481
|
};
|
|
464
482
|
|
|
465
483
|
export type UserAccount = {
|
|
@@ -472,6 +490,7 @@ export type UserAccount = {
|
|
|
472
490
|
beingLiquidated: boolean;
|
|
473
491
|
bankrupt: boolean;
|
|
474
492
|
nextLiquidationId: number;
|
|
493
|
+
nextOrderId: BN;
|
|
475
494
|
};
|
|
476
495
|
|
|
477
496
|
export type UserBankBalance = {
|
|
@@ -499,9 +518,7 @@ export type Order = {
|
|
|
499
518
|
triggerPrice: BN;
|
|
500
519
|
triggerCondition: OrderTriggerCondition;
|
|
501
520
|
triggered: boolean;
|
|
502
|
-
discountTier: OrderDiscountTier;
|
|
503
521
|
existingPositionDirection: PositionDirection;
|
|
504
|
-
referrer: PublicKey;
|
|
505
522
|
postOnly: boolean;
|
|
506
523
|
immediateOrCancel: boolean;
|
|
507
524
|
oraclePriceOffset: BN;
|
|
@@ -574,9 +591,15 @@ export type MakerInfo = {
|
|
|
574
591
|
export type TakerInfo = {
|
|
575
592
|
taker: PublicKey;
|
|
576
593
|
takerStats: PublicKey;
|
|
594
|
+
takerUserAccount: UserAccount;
|
|
577
595
|
order: Order;
|
|
578
596
|
};
|
|
579
597
|
|
|
598
|
+
export type ReferrerInfo = {
|
|
599
|
+
referrer: PublicKey;
|
|
600
|
+
referrerStats: PublicKey;
|
|
601
|
+
};
|
|
602
|
+
|
|
580
603
|
// # Misc Types
|
|
581
604
|
export interface IWallet {
|
|
582
605
|
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
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.getMarketAddress = void 0;
|
|
13
|
-
const pda_1 = require("./pda");
|
|
14
|
-
const CACHE = new Map();
|
|
15
|
-
function getMarketAddress(programId, marketIndex) {
|
|
16
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
-
const cacheKey = `${programId.toString()}-${marketIndex.toString()}`;
|
|
18
|
-
if (CACHE.has(cacheKey)) {
|
|
19
|
-
return CACHE.get(cacheKey);
|
|
20
|
-
}
|
|
21
|
-
const publicKey = yield pda_1.getMarketPublicKey(programId, marketIndex);
|
|
22
|
-
CACHE.set(cacheKey, publicKey);
|
|
23
|
-
return publicKey;
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
exports.getMarketAddress = getMarketAddress;
|
package/src/assert/assert.js
DELETED
package/src/constants/banks.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Banks = exports.MainnetBanks = exports.DevnetBanks = exports.WRAPPED_SOL_MINT = void 0;
|
|
4
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
-
const __1 = require("../");
|
|
6
|
-
exports.WRAPPED_SOL_MINT = new web3_js_1.PublicKey('So11111111111111111111111111111111111111112');
|
|
7
|
-
exports.DevnetBanks = [
|
|
8
|
-
{
|
|
9
|
-
symbol: 'USDC',
|
|
10
|
-
bankIndex: new __1.BN(0),
|
|
11
|
-
oracle: web3_js_1.PublicKey.default,
|
|
12
|
-
oracleSource: __1.OracleSource.QUOTE_ASSET,
|
|
13
|
-
mint: new web3_js_1.PublicKey('8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2'),
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
symbol: 'SOL',
|
|
17
|
-
bankIndex: new __1.BN(1),
|
|
18
|
-
oracle: new web3_js_1.PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
|
|
19
|
-
oracleSource: __1.OracleSource.PYTH,
|
|
20
|
-
mint: new web3_js_1.PublicKey(exports.WRAPPED_SOL_MINT),
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
symbol: 'BTC',
|
|
24
|
-
bankIndex: new __1.BN(2),
|
|
25
|
-
oracle: new web3_js_1.PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
|
|
26
|
-
oracleSource: __1.OracleSource.PYTH,
|
|
27
|
-
mint: new web3_js_1.PublicKey('3BZPwbcqB5kKScF3TEXxwNfx5ipV13kbRVDvfVp5c6fv'),
|
|
28
|
-
},
|
|
29
|
-
];
|
|
30
|
-
exports.MainnetBanks = [
|
|
31
|
-
{
|
|
32
|
-
symbol: 'USDC',
|
|
33
|
-
bankIndex: new __1.BN(0),
|
|
34
|
-
oracle: web3_js_1.PublicKey.default,
|
|
35
|
-
oracleSource: __1.OracleSource.QUOTE_ASSET,
|
|
36
|
-
mint: new web3_js_1.PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
|
|
37
|
-
},
|
|
38
|
-
];
|
|
39
|
-
exports.Banks = {
|
|
40
|
-
devnet: exports.DevnetBanks,
|
|
41
|
-
'mainnet-beta': exports.MainnetBanks,
|
|
42
|
-
};
|
package/src/constants/markets.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Markets = exports.MainnetMarkets = exports.DevnetMarkets = void 0;
|
|
4
|
-
const __1 = require("../");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
exports.DevnetMarkets = [
|
|
7
|
-
{
|
|
8
|
-
fullName: 'Solana',
|
|
9
|
-
category: ['L1', 'Infra'],
|
|
10
|
-
symbol: 'SOL-PERP',
|
|
11
|
-
baseAssetSymbol: 'SOL',
|
|
12
|
-
marketIndex: new __1.BN(0),
|
|
13
|
-
oracle: new web3_js_1.PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
|
|
14
|
-
launchTs: 1655751353000,
|
|
15
|
-
oracleSource: __1.OracleSource.PYTH,
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
fullName: 'Bitcoin',
|
|
19
|
-
category: ['L1', 'Payment'],
|
|
20
|
-
symbol: 'BTC-PERP',
|
|
21
|
-
baseAssetSymbol: 'BTC',
|
|
22
|
-
marketIndex: new __1.BN(1),
|
|
23
|
-
oracle: new web3_js_1.PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
|
|
24
|
-
launchTs: 1655751353000,
|
|
25
|
-
oracleSource: __1.OracleSource.PYTH,
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
fullName: 'Ethereum',
|
|
29
|
-
category: ['L1', 'Infra'],
|
|
30
|
-
symbol: 'ETH-PERP',
|
|
31
|
-
baseAssetSymbol: 'ETH',
|
|
32
|
-
marketIndex: new __1.BN(2),
|
|
33
|
-
oracle: new web3_js_1.PublicKey('EdVCmQ9FSPcVe5YySXDPCRmc8aDQLKJ9xvYBMZPie1Vw'),
|
|
34
|
-
launchTs: 1637691133472,
|
|
35
|
-
oracleSource: __1.OracleSource.PYTH,
|
|
36
|
-
},
|
|
37
|
-
];
|
|
38
|
-
exports.MainnetMarkets = [];
|
|
39
|
-
exports.Markets = {
|
|
40
|
-
devnet: exports.DevnetMarkets,
|
|
41
|
-
'mainnet-beta': [],
|
|
42
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.QUOTE_ASSET_BANK_INDEX = exports.ONE_YEAR = exports.BID_ASK_SPREAD_PRECISION = exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_DIV_PEG = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION_EXP = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.LIQUIDATION_FEE_PRECISION = exports.BANK_BALANCE_PRECISION = exports.BANK_BALANCE_PRECISION_EXP = exports.BANK_WEIGHT_PRECISION = exports.BANK_RATE_PRECISION = exports.BANK_UTILIZATION_PRECISION = exports.BANK_CUMULATIVE_INTEREST_PRECISION = exports.BANK_INTEREST_PRECISION = exports.AMM_RESERVE_PRECISION_EXP = exports.PEG_PRECISION_EXP = exports.FUNDING_RATE_PRECISION_EXP = exports.MARK_PRICE_PRECISION_EXP = exports.FUNDING_PAYMENT_PRECISION_EXP = exports.QUOTE_PRECISION_EXP = exports.MAX_LEVERAGE = exports.TEN_MILLION = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.TWO = exports.ONE = exports.ZERO = void 0;
|
|
4
|
-
const __1 = require("../");
|
|
5
|
-
exports.ZERO = new __1.BN(0);
|
|
6
|
-
exports.ONE = new __1.BN(1);
|
|
7
|
-
exports.TWO = new __1.BN(2);
|
|
8
|
-
exports.TEN = new __1.BN(10);
|
|
9
|
-
exports.TEN_THOUSAND = new __1.BN(10000);
|
|
10
|
-
exports.BN_MAX = new __1.BN(Number.MAX_SAFE_INTEGER);
|
|
11
|
-
exports.TEN_MILLION = exports.TEN_THOUSAND.mul(exports.TEN_THOUSAND);
|
|
12
|
-
exports.MAX_LEVERAGE = new __1.BN(5);
|
|
13
|
-
exports.QUOTE_PRECISION_EXP = new __1.BN(6);
|
|
14
|
-
exports.FUNDING_PAYMENT_PRECISION_EXP = new __1.BN(4);
|
|
15
|
-
exports.MARK_PRICE_PRECISION_EXP = new __1.BN(10);
|
|
16
|
-
exports.FUNDING_RATE_PRECISION_EXP = exports.MARK_PRICE_PRECISION_EXP.mul(exports.FUNDING_PAYMENT_PRECISION_EXP);
|
|
17
|
-
exports.PEG_PRECISION_EXP = new __1.BN(3);
|
|
18
|
-
exports.AMM_RESERVE_PRECISION_EXP = new __1.BN(13);
|
|
19
|
-
exports.BANK_INTEREST_PRECISION = new __1.BN(1000000);
|
|
20
|
-
exports.BANK_CUMULATIVE_INTEREST_PRECISION = new __1.BN(10000000000);
|
|
21
|
-
exports.BANK_UTILIZATION_PRECISION = new __1.BN(1000000);
|
|
22
|
-
exports.BANK_RATE_PRECISION = new __1.BN(1000000);
|
|
23
|
-
exports.BANK_WEIGHT_PRECISION = new __1.BN(100);
|
|
24
|
-
exports.BANK_BALANCE_PRECISION_EXP = new __1.BN(6);
|
|
25
|
-
exports.BANK_BALANCE_PRECISION = new __1.BN(10).pow(exports.BANK_BALANCE_PRECISION_EXP);
|
|
26
|
-
exports.LIQUIDATION_FEE_PRECISION = new __1.BN(1000000);
|
|
27
|
-
exports.QUOTE_PRECISION = new __1.BN(10).pow(exports.QUOTE_PRECISION_EXP);
|
|
28
|
-
exports.MARK_PRICE_PRECISION = new __1.BN(10).pow(exports.MARK_PRICE_PRECISION_EXP);
|
|
29
|
-
exports.FUNDING_PAYMENT_PRECISION = new __1.BN(10).pow(exports.FUNDING_PAYMENT_PRECISION_EXP);
|
|
30
|
-
exports.PEG_PRECISION = new __1.BN(10).pow(exports.PEG_PRECISION_EXP);
|
|
31
|
-
exports.AMM_RESERVE_PRECISION = new __1.BN(10).pow(exports.AMM_RESERVE_PRECISION_EXP);
|
|
32
|
-
exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION;
|
|
33
|
-
exports.BASE_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP;
|
|
34
|
-
exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.div(exports.QUOTE_PRECISION); // 10^7
|
|
35
|
-
exports.PRICE_DIV_PEG = exports.MARK_PRICE_PRECISION.div(exports.PEG_PRECISION); //10^7
|
|
36
|
-
exports.PRICE_TO_QUOTE_PRECISION = exports.MARK_PRICE_PRECISION.div(exports.QUOTE_PRECISION);
|
|
37
|
-
exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.mul(exports.PEG_PRECISION).div(exports.QUOTE_PRECISION); // 10^10
|
|
38
|
-
exports.MARGIN_PRECISION = exports.TEN_THOUSAND;
|
|
39
|
-
exports.BID_ASK_SPREAD_PRECISION = new __1.BN(1000000);
|
|
40
|
-
exports.ONE_YEAR = new __1.BN(31536000);
|
|
41
|
-
exports.QUOTE_ASSET_BANK_INDEX = new __1.BN(0);
|
package/src/events/eventList.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EventList = void 0;
|
|
4
|
-
class Node {
|
|
5
|
-
constructor(event, next, prev) {
|
|
6
|
-
this.event = event;
|
|
7
|
-
this.next = next;
|
|
8
|
-
this.prev = prev;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
class EventList {
|
|
12
|
-
constructor(eventType, maxSize, sortFn, orderDirection) {
|
|
13
|
-
this.eventType = eventType;
|
|
14
|
-
this.maxSize = maxSize;
|
|
15
|
-
this.sortFn = sortFn;
|
|
16
|
-
this.orderDirection = orderDirection;
|
|
17
|
-
this.size = 0;
|
|
18
|
-
}
|
|
19
|
-
insert(event) {
|
|
20
|
-
this.size++;
|
|
21
|
-
const newNode = new Node(event);
|
|
22
|
-
if (this.head === undefined) {
|
|
23
|
-
this.head = this.tail = newNode;
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
if (this.sortFn(this.head.event, newNode.event) ===
|
|
27
|
-
(this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
|
|
28
|
-
this.head.prev = newNode;
|
|
29
|
-
newNode.next = this.head;
|
|
30
|
-
this.head = newNode;
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
let currentNode = this.head;
|
|
34
|
-
while (currentNode.next !== undefined &&
|
|
35
|
-
this.sortFn(currentNode.next.event, newNode.event) !==
|
|
36
|
-
(this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
|
|
37
|
-
currentNode = currentNode.next;
|
|
38
|
-
}
|
|
39
|
-
newNode.next = currentNode.next;
|
|
40
|
-
if (currentNode.next !== undefined) {
|
|
41
|
-
newNode.next.prev = newNode;
|
|
42
|
-
}
|
|
43
|
-
currentNode.next = newNode;
|
|
44
|
-
newNode.prev = currentNode;
|
|
45
|
-
}
|
|
46
|
-
if (this.size > this.maxSize) {
|
|
47
|
-
this.detach();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
detach() {
|
|
51
|
-
const node = this.tail;
|
|
52
|
-
if (node.prev !== undefined) {
|
|
53
|
-
node.prev.next = node.next;
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
this.head = node.next;
|
|
57
|
-
}
|
|
58
|
-
if (node.next !== undefined) {
|
|
59
|
-
node.next.prev = node.prev;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
this.tail = node.prev;
|
|
63
|
-
}
|
|
64
|
-
this.size--;
|
|
65
|
-
}
|
|
66
|
-
toArray() {
|
|
67
|
-
return Array.from(this);
|
|
68
|
-
}
|
|
69
|
-
*[Symbol.iterator]() {
|
|
70
|
-
let node = this.head;
|
|
71
|
-
while (node) {
|
|
72
|
-
yield node.event;
|
|
73
|
-
node = node.next;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
exports.EventList = EventList;
|