@drift-labs/sdk 0.2.0-master.20 → 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 +2 -2
- package/lib/clearingHouse.js +25 -19
- package/lib/clearingHouseUser.d.ts +9 -1
- package/lib/clearingHouseUser.js +57 -20
- package/lib/math/orders.d.ts +2 -1
- package/lib/math/orders.js +14 -5
- package/lib/types.d.ts +3 -0
- 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 +27 -23
- package/src/clearingHouseUser.ts +67 -42
- package/src/math/orders.ts +26 -9
- package/src/types.ts +3 -0
- 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/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/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/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
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClearingHouse,
|
|
3
|
+
UserAccount,
|
|
4
|
+
} from '..';
|
|
5
|
+
|
|
6
|
+
export function printUserStats(chUserAccount: UserAccount, marketIndexToSymbol: Map<number, string>) {
|
|
7
|
+
|
|
8
|
+
console.log("")
|
|
9
|
+
console.log(`${new Date(Date.now())}`)
|
|
10
|
+
console.log("Open positions:");
|
|
11
|
+
let openPositions = 0;
|
|
12
|
+
for (const p of chUserAccount!.positions) {
|
|
13
|
+
if (p.baseAssetAmount.isZero()) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
console.log(` [${marketIndexToSymbol.get(p.marketIndex.toNumber())}]`);
|
|
17
|
+
console.log(` . baseAssetAmount: ${p.baseAssetAmount.toString()}`);
|
|
18
|
+
console.log(` . quoteAssetAmount: ${p.quoteAssetAmount.toString()}`);
|
|
19
|
+
console.log(` . quoteEntryAmount: ${p.quoteEntryAmount.toString()}`);
|
|
20
|
+
console.log(` . lastCumulativeFundingRate: ${p.lastCumulativeFundingRate.toString()}`);
|
|
21
|
+
console.log(` . openOrders: ${p.openOrders}`);
|
|
22
|
+
console.log(` . openBids: ${p.openBids.toString()}`);
|
|
23
|
+
console.log(` . openAsks: ${p.openAsks.toString()}`);
|
|
24
|
+
openPositions++;
|
|
25
|
+
}
|
|
26
|
+
console.log(`count: ${openPositions}`);
|
|
27
|
+
|
|
28
|
+
let openOrders = 0
|
|
29
|
+
console.log("")
|
|
30
|
+
console.log("Open orders:");
|
|
31
|
+
for (const o of chUserAccount!.orders) {
|
|
32
|
+
if (o.baseAssetAmount.isZero()) {
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
console.log(` . [OrderId: ${o.orderId.toString()}, ${JSON.stringify(o.status)} ${JSON.stringify(o.direction)}, mktIdx: ${o.marketIndex.toString()}]: ${o.baseAssetAmountFilled.toString()}/${o.baseAssetAmount.toString()}`)
|
|
36
|
+
openOrders++;
|
|
37
|
+
}
|
|
38
|
+
console.log(`count: ${openOrders}`);
|
|
39
|
+
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function cancelOpenOrders(clearingHouse: ClearingHouse, chUserAccount: UserAccount) {
|
|
44
|
+
for (const o of chUserAccount!.orders) {
|
|
45
|
+
if (o.baseAssetAmount.isZero()) {
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const tx = await clearingHouse.cancelOrder(o.orderId);
|
|
50
|
+
console.log(`Canceled orderID ${o.orderId.toString()} on market ${o.marketIndex.toString()}, tx: ${tx}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[149,94,114,124,219,221,15,219,156,59,219,91,53,1,121,158,237,212,164,155,68,189,58,136,87,97,154,140,23,246,148,112,14,18,108,149,240,175,10,38,108,111,73,206,93,83,134,205,168,159,203,151,219,125,105,254,244,61,190,29,211,107,13,19]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[240,167,34,21,160,221,50,92,208,80,210,26,131,127,167,209,55,203,154,72,195,156,103,167,91,95,76,29,235,92,57,167,14,18,117,219,39,14,43,171,96,125,33,203,219,16,219,60,194,217,59,116,35,116,255,1,179,89,243,9,41,181,81,103]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[5,60,137,129,213,3,6,50,181,118,108,215,24,4,33,79,191,86,110,99,115,149,212,216,240,15,229,159,228,144,75,215,14,18,120,40,242,92,142,39,104,193,163,8,30,13,91,89,147,117,144,82,156,127,20,66,10,222,188,128,65,29,15,139]
|
package/package.json
CHANGED
package/src/clearingHouse.ts
CHANGED
|
@@ -224,11 +224,13 @@ export class ClearingHouse {
|
|
|
224
224
|
* Forces the accountSubscriber to fetch account updates from rpc
|
|
225
225
|
*/
|
|
226
226
|
public async fetchAccounts(): Promise<void> {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
227
|
+
const promises = [...this.users.values()]
|
|
228
|
+
.map((user) => user.fetchAccounts())
|
|
229
|
+
.concat(this.accountSubscriber.fetch());
|
|
230
|
+
if (this.userStats) {
|
|
231
|
+
promises.concat(this.userStats.fetchAccounts());
|
|
232
|
+
}
|
|
233
|
+
await Promise.all(promises);
|
|
232
234
|
}
|
|
233
235
|
|
|
234
236
|
public async unsubscribe(): Promise<void> {
|
|
@@ -1764,7 +1766,9 @@ export class ClearingHouse {
|
|
|
1764
1766
|
const userStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
1765
1767
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1766
1768
|
|
|
1767
|
-
|
|
1769
|
+
// todo merge this with getRemainingAccounts
|
|
1770
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
1771
|
+
counterPartyUserAccount: takerInfo.takerUserAccount,
|
|
1768
1772
|
writableMarketIndex: orderParams.marketIndex,
|
|
1769
1773
|
});
|
|
1770
1774
|
|
|
@@ -1985,9 +1989,9 @@ export class ClearingHouse {
|
|
|
1985
1989
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
1986
1990
|
const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
1987
1991
|
|
|
1988
|
-
const remainingAccounts = this.
|
|
1992
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
1989
1993
|
writableMarketIndex: marketIndex,
|
|
1990
|
-
userAccount,
|
|
1994
|
+
counterPartyUserAccount: userAccount,
|
|
1991
1995
|
});
|
|
1992
1996
|
|
|
1993
1997
|
return await this.program.instruction.liquidatePerp(
|
|
@@ -2039,8 +2043,8 @@ export class ClearingHouse {
|
|
|
2039
2043
|
): Promise<TransactionInstruction> {
|
|
2040
2044
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
2041
2045
|
|
|
2042
|
-
const remainingAccounts = this.
|
|
2043
|
-
userAccount,
|
|
2046
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2047
|
+
counterPartyUserAccount: userAccount,
|
|
2044
2048
|
writableBankIndexes: [liabilityBankIndex, assetBankIndex],
|
|
2045
2049
|
});
|
|
2046
2050
|
|
|
@@ -2092,8 +2096,8 @@ export class ClearingHouse {
|
|
|
2092
2096
|
): Promise<TransactionInstruction> {
|
|
2093
2097
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
2094
2098
|
|
|
2095
|
-
const remainingAccounts = this.
|
|
2096
|
-
userAccount,
|
|
2099
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2100
|
+
counterPartyUserAccount: userAccount,
|
|
2097
2101
|
writableMarketIndex: perpMarketIndex,
|
|
2098
2102
|
writableBankIndexes: [liabilityBankIndex],
|
|
2099
2103
|
});
|
|
@@ -2146,8 +2150,8 @@ export class ClearingHouse {
|
|
|
2146
2150
|
): Promise<TransactionInstruction> {
|
|
2147
2151
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
2148
2152
|
|
|
2149
|
-
const remainingAccounts = this.
|
|
2150
|
-
userAccount,
|
|
2153
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2154
|
+
counterPartyUserAccount: userAccount,
|
|
2151
2155
|
writableMarketIndex: perpMarketIndex,
|
|
2152
2156
|
writableBankIndexes: [assetBankIndex],
|
|
2153
2157
|
});
|
|
@@ -2194,9 +2198,9 @@ export class ClearingHouse {
|
|
|
2194
2198
|
): Promise<TransactionInstruction> {
|
|
2195
2199
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
2196
2200
|
|
|
2197
|
-
const remainingAccounts = this.
|
|
2201
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2198
2202
|
writableMarketIndex: marketIndex,
|
|
2199
|
-
userAccount,
|
|
2203
|
+
counterPartyUserAccount: userAccount,
|
|
2200
2204
|
});
|
|
2201
2205
|
|
|
2202
2206
|
return await this.program.instruction.resolvePerpBankruptcy(marketIndex, {
|
|
@@ -2236,9 +2240,9 @@ export class ClearingHouse {
|
|
|
2236
2240
|
): Promise<TransactionInstruction> {
|
|
2237
2241
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
2238
2242
|
|
|
2239
|
-
const remainingAccounts = this.
|
|
2243
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
2240
2244
|
writableBankIndexes: [bankIndex],
|
|
2241
|
-
userAccount,
|
|
2245
|
+
counterPartyUserAccount: userAccount,
|
|
2242
2246
|
});
|
|
2243
2247
|
|
|
2244
2248
|
return await this.program.instruction.resolveBorrowBankruptcy(bankIndex, {
|
|
@@ -2252,17 +2256,17 @@ export class ClearingHouse {
|
|
|
2252
2256
|
});
|
|
2253
2257
|
}
|
|
2254
2258
|
|
|
2255
|
-
|
|
2256
|
-
|
|
2259
|
+
getRemainingAccountsWithCounterparty(params: {
|
|
2260
|
+
counterPartyUserAccount: UserAccount;
|
|
2257
2261
|
writableMarketIndex?: BN;
|
|
2258
2262
|
writableBankIndexes?: BN[];
|
|
2259
2263
|
}): AccountMeta[] {
|
|
2260
|
-
const
|
|
2264
|
+
const counterPartyUserAccount = params.counterPartyUserAccount;
|
|
2261
2265
|
|
|
2262
2266
|
const oracleAccountMap = new Map<string, AccountMeta>();
|
|
2263
2267
|
const bankAccountMap = new Map<number, AccountMeta>();
|
|
2264
2268
|
const marketAccountMap = new Map<number, AccountMeta>();
|
|
2265
|
-
for (const bankBalance of
|
|
2269
|
+
for (const bankBalance of counterPartyUserAccount.bankBalances) {
|
|
2266
2270
|
if (!bankBalance.balance.eq(ZERO)) {
|
|
2267
2271
|
const bankAccount = this.getBankAccount(bankBalance.bankIndex);
|
|
2268
2272
|
bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
|
|
@@ -2280,7 +2284,7 @@ export class ClearingHouse {
|
|
|
2280
2284
|
}
|
|
2281
2285
|
}
|
|
2282
2286
|
}
|
|
2283
|
-
for (const position of
|
|
2287
|
+
for (const position of counterPartyUserAccount.positions) {
|
|
2284
2288
|
if (!positionIsAvailable(position)) {
|
|
2285
2289
|
const market = this.getMarketAccount(position.marketIndex);
|
|
2286
2290
|
marketAccountMap.set(position.marketIndex.toNumber(), {
|
package/src/clearingHouseUser.ts
CHANGED
|
@@ -144,6 +144,11 @@ export class ClearingHouseUser {
|
|
|
144
144
|
};
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
public getClonedPosition(position: UserPosition): UserPosition {
|
|
148
|
+
const clonedPosition = Object.assign({}, position);
|
|
149
|
+
return clonedPosition;
|
|
150
|
+
}
|
|
151
|
+
|
|
147
152
|
/**
|
|
148
153
|
* @param orderId
|
|
149
154
|
* @returns Order
|
|
@@ -180,8 +185,10 @@ export class ClearingHouseUser {
|
|
|
180
185
|
* calculates the market position if the lp position was settled
|
|
181
186
|
* @returns : userPosition
|
|
182
187
|
*/
|
|
183
|
-
public getSettledLPPosition(marketIndex: BN): [UserPosition, BN] {
|
|
184
|
-
const
|
|
188
|
+
public getSettledLPPosition(marketIndex: BN): [UserPosition, BN, BN] {
|
|
189
|
+
const _position = this.getUserPosition(marketIndex);
|
|
190
|
+
const position = this.getClonedPosition(_position);
|
|
191
|
+
|
|
185
192
|
const market = this.clearingHouse.getMarketAccount(position.marketIndex);
|
|
186
193
|
const nShares = position.lpShares;
|
|
187
194
|
|
|
@@ -265,7 +272,7 @@ export class ClearingHouseUser {
|
|
|
265
272
|
position.lastCumulativeFundingRate = ZERO;
|
|
266
273
|
}
|
|
267
274
|
|
|
268
|
-
return [position, pnl];
|
|
275
|
+
return [position, remainderBaa, pnl];
|
|
269
276
|
}
|
|
270
277
|
|
|
271
278
|
/**
|
|
@@ -289,51 +296,55 @@ export class ClearingHouseUser {
|
|
|
289
296
|
return freeCollateral.gte(ZERO) ? freeCollateral : ZERO;
|
|
290
297
|
}
|
|
291
298
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
299
|
+
/**
|
|
300
|
+
* @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
|
|
301
|
+
*/
|
|
302
|
+
public getMarginRequirement(type: MarginCategory): BN {
|
|
303
|
+
return this.getUserAccount()
|
|
304
|
+
.positions.reduce((marginRequirement, marketPosition) => {
|
|
295
305
|
const market = this.clearingHouse.getMarketAccount(
|
|
296
306
|
marketPosition.marketIndex
|
|
297
307
|
);
|
|
298
|
-
const worstCaseBaseAssetAmount =
|
|
299
|
-
calculateWorstCaseBaseAssetAmount(marketPosition);
|
|
300
308
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
.
|
|
309
|
+
if (marketPosition.lpShares.gt(ZERO)) {
|
|
310
|
+
// is an lp
|
|
311
|
+
// clone so we dont mutate the position
|
|
312
|
+
marketPosition = this.getClonedPosition(marketPosition);
|
|
305
313
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
market
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
314
|
+
// settle position
|
|
315
|
+
const [settledPosition, dustBaa, _] = this.getSettledLPPosition(
|
|
316
|
+
market.marketIndex
|
|
317
|
+
);
|
|
318
|
+
marketPosition.baseAssetAmount =
|
|
319
|
+
settledPosition.baseAssetAmount.add(dustBaa);
|
|
320
|
+
marketPosition.quoteAssetAmount = settledPosition.quoteAssetAmount;
|
|
321
|
+
|
|
322
|
+
// open orders
|
|
323
|
+
let openAsks;
|
|
324
|
+
if (market.amm.maxBaseAssetReserve > market.amm.baseAssetReserve) {
|
|
325
|
+
openAsks = market.amm.maxBaseAssetReserve
|
|
326
|
+
.sub(market.amm.baseAssetReserve)
|
|
327
|
+
.mul(marketPosition.lpShares)
|
|
328
|
+
.div(market.amm.sqrtK)
|
|
329
|
+
.mul(new BN(-1));
|
|
330
|
+
} else {
|
|
331
|
+
openAsks = ZERO;
|
|
332
|
+
}
|
|
319
333
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
334
|
+
let openBids;
|
|
335
|
+
if (market.amm.minBaseAssetReserve < market.amm.baseAssetReserve) {
|
|
336
|
+
openBids = market.amm.baseAssetReserve
|
|
337
|
+
.sub(market.amm.minBaseAssetReserve)
|
|
338
|
+
.mul(marketPosition.lpShares)
|
|
339
|
+
.div(market.amm.sqrtK);
|
|
340
|
+
} else {
|
|
341
|
+
openBids = ZERO;
|
|
342
|
+
}
|
|
324
343
|
|
|
325
|
-
|
|
326
|
-
|
|
344
|
+
marketPosition.openAsks = marketPosition.openAsks.add(openAsks);
|
|
345
|
+
marketPosition.openBids = marketPosition.openBids.add(openBids);
|
|
346
|
+
}
|
|
327
347
|
|
|
328
|
-
/**
|
|
329
|
-
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
330
|
-
*/
|
|
331
|
-
public getMaintenanceMarginRequirement(): BN {
|
|
332
|
-
return this.getUserAccount()
|
|
333
|
-
.positions.reduce((marginRequirement, marketPosition) => {
|
|
334
|
-
const market = this.clearingHouse.getMarketAccount(
|
|
335
|
-
marketPosition.marketIndex
|
|
336
|
-
);
|
|
337
348
|
const worstCaseBaseAssetAmount =
|
|
338
349
|
calculateWorstCaseBaseAssetAmount(marketPosition);
|
|
339
350
|
|
|
@@ -349,14 +360,28 @@ export class ClearingHouseUser {
|
|
|
349
360
|
calculateMarketMarginRatio(
|
|
350
361
|
market,
|
|
351
362
|
worstCaseBaseAssetAmount.abs(),
|
|
352
|
-
|
|
363
|
+
type
|
|
353
364
|
)
|
|
354
365
|
)
|
|
355
366
|
)
|
|
356
367
|
.div(MARGIN_PRECISION)
|
|
357
368
|
);
|
|
358
369
|
}, ZERO)
|
|
359
|
-
.add(this.getBankLiabilityValue(undefined,
|
|
370
|
+
.add(this.getBankLiabilityValue(undefined, type));
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
375
|
+
*/
|
|
376
|
+
public getInitialMarginRequirement(): BN {
|
|
377
|
+
return this.getMarginRequirement('Initial');
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
382
|
+
*/
|
|
383
|
+
public getMaintenanceMarginRequirement(): BN {
|
|
384
|
+
return this.getMarginRequirement('Maintenance');
|
|
360
385
|
}
|
|
361
386
|
|
|
362
387
|
/**
|
package/src/math/orders.ts
CHANGED
|
@@ -139,7 +139,7 @@ export function getLimitPrice(
|
|
|
139
139
|
if (!order.oraclePriceOffset.eq(ZERO)) {
|
|
140
140
|
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
141
141
|
} else if (isOneOfVariant(order.orderType, ['market', 'triggerMarket'])) {
|
|
142
|
-
if (isAuctionComplete(order, slot)) {
|
|
142
|
+
if (!isAuctionComplete(order, slot)) {
|
|
143
143
|
limitPrice = getAuctionPrice(order, slot);
|
|
144
144
|
} else if (!order.price.eq(ZERO)) {
|
|
145
145
|
limitPrice = order.price;
|
|
@@ -163,16 +163,18 @@ export function isFillableByVAMM(
|
|
|
163
163
|
order: Order,
|
|
164
164
|
market: MarketAccount,
|
|
165
165
|
oraclePriceData: OraclePriceData,
|
|
166
|
-
slot: number
|
|
166
|
+
slot: number,
|
|
167
|
+
maxAuctionDuration: number
|
|
167
168
|
): boolean {
|
|
168
169
|
return (
|
|
169
|
-
isAuctionComplete(order, slot) &&
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
170
|
+
(isAuctionComplete(order, slot) &&
|
|
171
|
+
!calculateBaseAssetAmountForAmmToFulfill(
|
|
172
|
+
order,
|
|
173
|
+
market,
|
|
174
|
+
oraclePriceData,
|
|
175
|
+
slot
|
|
176
|
+
).eq(ZERO)) ||
|
|
177
|
+
isOrderExpired(order, slot, maxAuctionDuration)
|
|
176
178
|
);
|
|
177
179
|
}
|
|
178
180
|
|
|
@@ -243,3 +245,18 @@ function isSameDirection(
|
|
|
243
245
|
(isVariant(firstDirection, 'short') && isVariant(secondDirection, 'short'))
|
|
244
246
|
);
|
|
245
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
|
@@ -326,6 +326,8 @@ export type StateAccount = {
|
|
|
326
326
|
numberOfMarkets: BN;
|
|
327
327
|
numberOfBanks: BN;
|
|
328
328
|
minOrderQuoteAssetAmount: BN;
|
|
329
|
+
maxAuctionDuration: number;
|
|
330
|
+
minAuctionDuration: number;
|
|
329
331
|
};
|
|
330
332
|
|
|
331
333
|
export type MarketAccount = {
|
|
@@ -589,6 +591,7 @@ export type MakerInfo = {
|
|
|
589
591
|
export type TakerInfo = {
|
|
590
592
|
taker: PublicKey;
|
|
591
593
|
takerStats: PublicKey;
|
|
594
|
+
takerUserAccount: UserAccount;
|
|
592
595
|
order: Order;
|
|
593
596
|
};
|
|
594
597
|
|
|
@@ -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;
|