@drift-labs/sdk 0.1.18 → 0.1.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/accounts/bulkAccountLoader.d.ts +32 -0
- package/lib/accounts/bulkAccountLoader.d.ts.map +1 -0
- package/lib/accounts/bulkAccountLoader.js +166 -0
- package/lib/accounts/bulkUserSubscription.d.ts +8 -0
- package/lib/accounts/bulkUserSubscription.d.ts.map +1 -0
- package/lib/accounts/bulkUserSubscription.js +28 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +46 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +206 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +26 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +31 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingUserAccountSubscriber.js +123 -0
- package/lib/accounts/types.d.ts +30 -0
- package/lib/accounts/types.d.ts.map +1 -1
- package/lib/accounts/utils.d.ts +2 -0
- package/lib/accounts/utils.d.ts.map +1 -0
- package/lib/accounts/utils.js +7 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -2
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/webSocketAccountSubscriber.js +43 -12
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +4 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +4 -3
- package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +4 -2
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +4 -3
- package/lib/admin.d.ts +1 -0
- package/lib/admin.d.ts.map +1 -1
- package/lib/admin.js +20 -11
- package/lib/clearingHouse.d.ts +12 -2
- package/lib/clearingHouse.d.ts.map +1 -1
- package/lib/clearingHouse.js +19 -8
- package/lib/clearingHouseUser.d.ts +11 -3
- package/lib/clearingHouseUser.d.ts.map +1 -1
- package/lib/clearingHouseUser.js +33 -23
- package/lib/constants/markets.d.ts +2 -1
- package/lib/constants/markets.d.ts.map +1 -1
- package/lib/constants/markets.js +20 -15
- package/lib/constants/numericConstants.d.ts +3 -1
- package/lib/constants/numericConstants.d.ts.map +1 -1
- package/lib/constants/numericConstants.js +15 -17
- package/lib/examples/makeTradeExample.js +1 -1
- package/lib/factory/clearingHouse.d.ts +26 -0
- package/lib/factory/clearingHouse.d.ts.map +1 -0
- package/lib/factory/clearingHouse.js +64 -0
- package/lib/factory/clearingHouseUser.d.ts +20 -0
- package/lib/factory/clearingHouseUser.d.ts.map +1 -0
- package/lib/factory/clearingHouseUser.js +34 -0
- package/lib/index.d.ts +8 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +9 -2
- package/lib/math/conversion.d.ts +1 -1
- package/lib/math/conversion.d.ts.map +1 -1
- package/lib/math/insuranceFund.d.ts +2 -1
- package/lib/math/insuranceFund.d.ts.map +1 -1
- package/lib/math/insuranceFund.js +3 -6
- package/lib/math/position.d.ts +2 -1
- package/lib/math/position.d.ts.map +1 -1
- package/lib/math/position.js +2 -5
- package/lib/math/utils.d.ts +2 -1
- package/lib/math/utils.d.ts.map +1 -1
- package/lib/math/utils.js +3 -3
- package/lib/mockUSDCFaucet.d.ts +2 -1
- package/lib/mockUSDCFaucet.d.ts.map +1 -1
- package/lib/token/index.d.ts +4 -0
- package/lib/token/index.d.ts.map +1 -0
- package/lib/token/index.js +38 -0
- package/lib/types.d.ts +3 -1
- package/lib/types.d.ts.map +1 -1
- package/package.json +11 -3
- package/src/accounts/bulkAccountLoader.ts +207 -0
- package/src/accounts/bulkUserSubscription.ts +28 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +293 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +166 -0
- package/src/accounts/types.ts +38 -0
- package/src/accounts/utils.ts +3 -0
- package/src/accounts/webSocketAccountSubscriber.ts +67 -17
- package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +4 -1
- package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +4 -1
- package/src/admin.ts +27 -18
- package/src/clearingHouse.ts +25 -17
- package/src/clearingHouseUser.ts +26 -6
- package/src/constants/markets.ts +9 -1
- package/src/constants/numericConstants.ts +2 -1
- package/src/examples/makeTradeExample.ts +4 -1
- package/src/factory/clearingHouse.ts +125 -0
- package/src/factory/clearingHouseUser.ts +73 -0
- package/src/index.ts +8 -2
- package/src/math/conversion.ts +1 -1
- package/src/math/insuranceFund.ts +1 -1
- package/src/math/position.ts +1 -1
- package/src/math/utils.ts +1 -1
- package/src/mockUSDCFaucet.ts +1 -1
- package/src/token/index.ts +37 -0
- package/src/types.ts +2 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="bn.js" />
|
|
2
3
|
import { PublicKey } from '@solana/web3.js';
|
|
3
|
-
import BN from 'bn.js';
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
5
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
6
6
|
import { ClearingHouse } from './clearingHouse';
|
|
7
7
|
import { UserAccount, UserPosition, UserPositionsAccount } from './types';
|
|
8
8
|
import { UserAccountSubscriber, UserAccountEvents } from './accounts/types';
|
|
9
|
-
import { PositionDirection } from '.';
|
|
9
|
+
import { PositionDirection, BN } from '.';
|
|
10
10
|
export declare class ClearingHouseUser {
|
|
11
11
|
clearingHouse: ClearingHouse;
|
|
12
12
|
authority: PublicKey;
|
|
13
13
|
accountSubscriber: UserAccountSubscriber;
|
|
14
14
|
userAccountPublicKey?: PublicKey;
|
|
15
|
-
|
|
15
|
+
_isSubscribed: boolean;
|
|
16
16
|
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
17
|
+
get isSubscribed(): boolean;
|
|
18
|
+
set isSubscribed(val: boolean);
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated You should use getClearingHouseUser factory method instead
|
|
21
|
+
* @param clearingHouse
|
|
22
|
+
* @param authority
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
17
25
|
static from(clearingHouse: ClearingHouse, authority: PublicKey): ClearingHouseUser;
|
|
18
26
|
constructor(clearingHouse: ClearingHouse, authority: PublicKey, accountSubscriber: UserAccountSubscriber);
|
|
19
27
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clearingHouseUser.d.ts","sourceRoot":"","sources":["../src/clearingHouseUser.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"clearingHouseUser.d.ts","sourceRoot":"","sources":["../src/clearingHouseUser.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAc1E,OAAO,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC5E,OAAO,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,EACpB,SAAS,EAAE,iBAAiB,GAC1B,EAAE;IAuCL;;;;OAIG;IACI,0BAA0B,CAAC,WAAW,EAAE,EAAE,GAAG,EAAE;IAQtD;;;;OAIG;IACH,OAAO,CAAC,oCAAoC;CAY5C"}
|
package/lib/clearingHouseUser.js
CHANGED
|
@@ -8,28 +8,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
exports.ClearingHouseUser = void 0;
|
|
16
|
-
const bn_js_1 = __importDefault(require("bn.js"));
|
|
17
13
|
const position_1 = require("./math/position");
|
|
18
14
|
const numericConstants_1 = require("./constants/numericConstants");
|
|
19
|
-
const defaultUserAccountSubscriber_1 = require("./accounts/defaultUserAccountSubscriber");
|
|
20
15
|
const _1 = require(".");
|
|
21
16
|
const addresses_1 = require("./addresses");
|
|
17
|
+
const clearingHouseUser_1 = require("./factory/clearingHouseUser");
|
|
22
18
|
class ClearingHouseUser {
|
|
23
19
|
constructor(clearingHouse, authority, accountSubscriber) {
|
|
24
|
-
this.
|
|
20
|
+
this._isSubscribed = false;
|
|
25
21
|
this.clearingHouse = clearingHouse;
|
|
26
22
|
this.authority = authority;
|
|
27
23
|
this.accountSubscriber = accountSubscriber;
|
|
28
24
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
29
25
|
}
|
|
26
|
+
get isSubscribed() {
|
|
27
|
+
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
28
|
+
}
|
|
29
|
+
set isSubscribed(val) {
|
|
30
|
+
this._isSubscribed = val;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @deprecated You should use getClearingHouseUser factory method instead
|
|
34
|
+
* @param clearingHouse
|
|
35
|
+
* @param authority
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
30
38
|
static from(clearingHouse, authority) {
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
if (clearingHouse.accountSubscriber.type !== 'websocket')
|
|
40
|
+
throw 'This method only works for clearing houses with a websocket account listener. Try using the getClearingHouseUser factory method to initialize a ClearingHouseUser instead';
|
|
41
|
+
const config = clearingHouseUser_1.getWebSocketClearingHouseUserConfig(clearingHouse, authority);
|
|
42
|
+
return clearingHouseUser_1.getClearingHouseUser(config);
|
|
33
43
|
}
|
|
34
44
|
/**
|
|
35
45
|
* Subscribe to ClearingHouseUser state accounts
|
|
@@ -143,7 +153,7 @@ class ClearingHouseUser {
|
|
|
143
153
|
*/
|
|
144
154
|
getTotalCollateral() {
|
|
145
155
|
var _a;
|
|
146
|
-
return ((_a = this.getUserAccount().collateral.add(this.getUnrealizedPNL(true))) !== null && _a !== void 0 ? _a : new
|
|
156
|
+
return ((_a = this.getUserAccount().collateral.add(this.getUnrealizedPNL(true))) !== null && _a !== void 0 ? _a : new _1.BN(0));
|
|
147
157
|
}
|
|
148
158
|
/**
|
|
149
159
|
* calculates sum of position value across all positions
|
|
@@ -311,8 +321,8 @@ class ClearingHouseUser {
|
|
|
311
321
|
const proposedMarketPosition = {
|
|
312
322
|
marketIndex: targetMarket.marketIndex,
|
|
313
323
|
baseAssetAmount: currentMarketPositionBaseSize.add(positionBaseSizeChange),
|
|
314
|
-
lastCumulativeFundingRate: new
|
|
315
|
-
quoteAssetAmount: new
|
|
324
|
+
lastCumulativeFundingRate: new _1.BN(0),
|
|
325
|
+
quoteAssetAmount: new _1.BN(0),
|
|
316
326
|
};
|
|
317
327
|
const market = this.clearingHouse.getMarket(proposedMarketPosition.marketIndex);
|
|
318
328
|
const proposedMarketPositionValueUSDC = _1.calculateBaseAssetValue(market, proposedMarketPosition);
|
|
@@ -329,7 +339,7 @@ class ClearingHouseUser {
|
|
|
329
339
|
// if the position value after the trade is less than total collateral, there is no liq price
|
|
330
340
|
if (targetTotalPositionValueUSDC.lte(totalFreeCollateralUSDC) &&
|
|
331
341
|
proposedMarketPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
332
|
-
return new
|
|
342
|
+
return new _1.BN(-1);
|
|
333
343
|
}
|
|
334
344
|
// get current margin ratio based on current collateral and proposed total position value
|
|
335
345
|
let marginRatio;
|
|
@@ -356,7 +366,7 @@ class ClearingHouseUser {
|
|
|
356
366
|
if (numericConstants_1.TEN_THOUSAND.lte(pctChange)) {
|
|
357
367
|
// no liquidation price, position is a fully/over collateralized long
|
|
358
368
|
// handle as NaN on UI
|
|
359
|
-
return new
|
|
369
|
+
return new _1.BN(-1);
|
|
360
370
|
}
|
|
361
371
|
pctChange = numericConstants_1.TEN_THOUSAND.sub(pctChange);
|
|
362
372
|
}
|
|
@@ -385,7 +395,7 @@ class ClearingHouseUser {
|
|
|
385
395
|
const tpv = this.getTotalPositionValue();
|
|
386
396
|
const partialLev = 16;
|
|
387
397
|
const maintLev = 20;
|
|
388
|
-
const thisLev = partial ? new
|
|
398
|
+
const thisLev = partial ? new _1.BN(partialLev) : new _1.BN(maintLev);
|
|
389
399
|
// calculate the total position value ignoring any value from the target market of the trade
|
|
390
400
|
const totalCurrentPositionValueIgnoringTargetUSDC = this.getTotalPositionValueExcludingMarket(targetMarket.marketIndex);
|
|
391
401
|
const currentMarketPosition = this.getUserPosition(targetMarket.marketIndex) ||
|
|
@@ -397,7 +407,7 @@ class ClearingHouseUser {
|
|
|
397
407
|
marketIndex: targetMarket.marketIndex,
|
|
398
408
|
baseAssetAmount: proposedBaseAssetAmount,
|
|
399
409
|
lastCumulativeFundingRate: currentMarketPosition.lastCumulativeFundingRate,
|
|
400
|
-
quoteAssetAmount: new
|
|
410
|
+
quoteAssetAmount: new _1.BN(0),
|
|
401
411
|
};
|
|
402
412
|
const market = this.clearingHouse.getMarket(proposedMarketPosition.marketIndex);
|
|
403
413
|
const proposedMarketPositionValueUSDC = _1.calculateBaseAssetValue(market, proposedMarketPosition);
|
|
@@ -417,14 +427,14 @@ class ClearingHouseUser {
|
|
|
417
427
|
.mul(thisLev)
|
|
418
428
|
.sub(tpv)
|
|
419
429
|
.mul(numericConstants_1.PRICE_TO_QUOTE_PRECISION)
|
|
420
|
-
.div(thisLev.add(new
|
|
430
|
+
.div(thisLev.add(new _1.BN(1)));
|
|
421
431
|
}
|
|
422
432
|
else {
|
|
423
433
|
priceDelt = tc
|
|
424
434
|
.mul(thisLev)
|
|
425
435
|
.sub(tpv)
|
|
426
436
|
.mul(numericConstants_1.PRICE_TO_QUOTE_PRECISION)
|
|
427
|
-
.div(thisLev.sub(new
|
|
437
|
+
.div(thisLev.sub(new _1.BN(1)));
|
|
428
438
|
}
|
|
429
439
|
let currentPrice;
|
|
430
440
|
if (positionBaseSizeChange.eq(numericConstants_1.ZERO)) {
|
|
@@ -439,15 +449,15 @@ class ClearingHouseUser {
|
|
|
439
449
|
// if the position value after the trade is less than total collateral, there is no liq price
|
|
440
450
|
if (targetTotalPositionValueUSDC.lte(totalFreeCollateralUSDC) &&
|
|
441
451
|
proposedMarketPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
442
|
-
return new
|
|
452
|
+
return new _1.BN(-1);
|
|
443
453
|
}
|
|
444
454
|
if (proposedBaseAssetAmount.eq(numericConstants_1.ZERO))
|
|
445
|
-
return new
|
|
455
|
+
return new _1.BN(-1);
|
|
446
456
|
const eatMargin2 = priceDelt
|
|
447
457
|
.mul(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
448
458
|
.div(proposedBaseAssetAmount);
|
|
449
459
|
if (eatMargin2.gt(currentPrice)) {
|
|
450
|
-
return new
|
|
460
|
+
return new _1.BN(-1);
|
|
451
461
|
}
|
|
452
462
|
const liqPrice = currentPrice.sub(eatMargin2);
|
|
453
463
|
return liqPrice;
|
|
@@ -510,7 +520,7 @@ class ClearingHouseUser {
|
|
|
510
520
|
: this.getPositionValue(targetMarketIndex);
|
|
511
521
|
// get current leverage
|
|
512
522
|
const currentLeverage = this.getLeverage();
|
|
513
|
-
const remainingLeverage =
|
|
523
|
+
const remainingLeverage = _1.BN.max(userMaxLeverageSetting.sub(currentLeverage), numericConstants_1.ZERO);
|
|
514
524
|
// get total collateral
|
|
515
525
|
const totalCollateral = this.getTotalCollateral();
|
|
516
526
|
// position side allowed based purely on current leverage
|
|
@@ -524,7 +534,7 @@ class ClearingHouseUser {
|
|
|
524
534
|
}
|
|
525
535
|
else {
|
|
526
536
|
// case 2 : trade where current total position less than max, but need to account for flipping the current position over to the other side
|
|
527
|
-
maxPositionSize = maxPositionSize.add(oppositeSizeValueUSDC.mul(new
|
|
537
|
+
maxPositionSize = maxPositionSize.add(oppositeSizeValueUSDC.mul(new _1.BN(2)));
|
|
528
538
|
}
|
|
529
539
|
}
|
|
530
540
|
else {
|
|
@@ -592,7 +602,7 @@ class ClearingHouseUser {
|
|
|
592
602
|
return newLeverage;
|
|
593
603
|
}
|
|
594
604
|
else {
|
|
595
|
-
return new
|
|
605
|
+
return new _1.BN(0);
|
|
596
606
|
}
|
|
597
607
|
}
|
|
598
608
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markets.d.ts","sourceRoot":"","sources":["../../src/constants/markets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"markets.d.ts","sourceRoot":"","sources":["../../src/constants/markets.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AAEzB,aAAK,MAAM,GAAG;IACb,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,EAAE,CAAC;IAChB,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,QAAQ,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,OAAO,EAAE,MAAM,EAyG3B,CAAC"}
|
package/lib/constants/markets.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Markets = void 0;
|
|
7
|
-
const
|
|
4
|
+
const __1 = require("../");
|
|
8
5
|
exports.Markets = [
|
|
9
6
|
{
|
|
10
7
|
symbol: 'SOL-PERP',
|
|
11
8
|
baseAssetSymbol: 'SOL',
|
|
12
|
-
marketIndex: new
|
|
9
|
+
marketIndex: new __1.BN(0),
|
|
13
10
|
devnetPythOracle: 'J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix',
|
|
14
11
|
mainnetPythOracle: 'H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG',
|
|
15
12
|
launchTs: 1635209696886,
|
|
@@ -17,7 +14,7 @@ exports.Markets = [
|
|
|
17
14
|
{
|
|
18
15
|
symbol: 'BTC-PERP',
|
|
19
16
|
baseAssetSymbol: 'BTC',
|
|
20
|
-
marketIndex: new
|
|
17
|
+
marketIndex: new __1.BN(1),
|
|
21
18
|
devnetPythOracle: 'HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J',
|
|
22
19
|
mainnetPythOracle: 'GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU',
|
|
23
20
|
launchTs: 1637691088868,
|
|
@@ -25,7 +22,7 @@ exports.Markets = [
|
|
|
25
22
|
{
|
|
26
23
|
symbol: 'ETH-PERP',
|
|
27
24
|
baseAssetSymbol: 'ETH',
|
|
28
|
-
marketIndex: new
|
|
25
|
+
marketIndex: new __1.BN(2),
|
|
29
26
|
devnetPythOracle: 'EdVCmQ9FSPcVe5YySXDPCRmc8aDQLKJ9xvYBMZPie1Vw',
|
|
30
27
|
mainnetPythOracle: 'JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB',
|
|
31
28
|
launchTs: 1637691133472,
|
|
@@ -33,7 +30,7 @@ exports.Markets = [
|
|
|
33
30
|
{
|
|
34
31
|
symbol: 'LUNA-PERP',
|
|
35
32
|
baseAssetSymbol: 'LUNA',
|
|
36
|
-
marketIndex: new
|
|
33
|
+
marketIndex: new __1.BN(3),
|
|
37
34
|
devnetPythOracle: '8PugCXTAHLM9kfLSQWe2njE5pzAgUdpPk3Nx5zSm7BD3',
|
|
38
35
|
mainnetPythOracle: '5bmWuR1dgP4avtGYMNKLuxumZTVKGgoN2BCMXWDNL9nY',
|
|
39
36
|
launchTs: 1638821738525,
|
|
@@ -41,7 +38,7 @@ exports.Markets = [
|
|
|
41
38
|
{
|
|
42
39
|
symbol: 'AVAX-PERP',
|
|
43
40
|
baseAssetSymbol: 'AVAX',
|
|
44
|
-
marketIndex: new
|
|
41
|
+
marketIndex: new __1.BN(4),
|
|
45
42
|
devnetPythOracle: 'FVb5h1VmHPfVb1RfqZckchq18GxRv4iKt8T4eVTQAqdz',
|
|
46
43
|
mainnetPythOracle: 'Ax9ujW5B9oqcv59N8m6f1BpTBq2rGeGaBcpKjC5UYsXU',
|
|
47
44
|
launchTs: 1639092501080,
|
|
@@ -49,7 +46,7 @@ exports.Markets = [
|
|
|
49
46
|
{
|
|
50
47
|
symbol: 'BNB-PERP',
|
|
51
48
|
baseAssetSymbol: 'BNB',
|
|
52
|
-
marketIndex: new
|
|
49
|
+
marketIndex: new __1.BN(5),
|
|
53
50
|
devnetPythOracle: 'GwzBgrXb4PG59zjce24SF2b9JXbLEjJJTBkmytuEZj1b',
|
|
54
51
|
mainnetPythOracle: '4CkQJBxhU8EZ2UjhigbtdaPbpTe6mqf811fipYBFbSYN',
|
|
55
52
|
launchTs: 1639523193012,
|
|
@@ -57,7 +54,7 @@ exports.Markets = [
|
|
|
57
54
|
{
|
|
58
55
|
symbol: 'MATIC-PERP',
|
|
59
56
|
baseAssetSymbol: 'MATIC',
|
|
60
|
-
marketIndex: new
|
|
57
|
+
marketIndex: new __1.BN(6),
|
|
61
58
|
devnetPythOracle: 'FBirwuDFuRAu4iSGc7RGxN5koHB7EJM1wbCmyPuQoGur',
|
|
62
59
|
mainnetPythOracle: '7KVswB9vkCgeM3SHP7aGDijvdRAHK8P5wi9JXViCrtYh',
|
|
63
60
|
launchTs: 1641488603564,
|
|
@@ -65,7 +62,7 @@ exports.Markets = [
|
|
|
65
62
|
{
|
|
66
63
|
symbol: 'ATOM-PERP',
|
|
67
64
|
baseAssetSymbol: 'ATOM',
|
|
68
|
-
marketIndex: new
|
|
65
|
+
marketIndex: new __1.BN(7),
|
|
69
66
|
devnetPythOracle: '7YAze8qFUMkBnyLVdKT4TFUUFui99EwS5gfRArMcrvFk',
|
|
70
67
|
mainnetPythOracle: 'CrCpTerNqtZvqLcKqz1k13oVeXV9WkMD2zA9hBKXrsbN',
|
|
71
68
|
launchTs: 1641920238195,
|
|
@@ -73,7 +70,7 @@ exports.Markets = [
|
|
|
73
70
|
{
|
|
74
71
|
symbol: 'DOT-PERP',
|
|
75
72
|
baseAssetSymbol: 'DOT',
|
|
76
|
-
marketIndex: new
|
|
73
|
+
marketIndex: new __1.BN(8),
|
|
77
74
|
devnetPythOracle: '4dqq5VBpN4EwYb7wyywjjfknvMKu7m78j9mKZRXTj462',
|
|
78
75
|
mainnetPythOracle: 'EcV1X1gY2yb4KXxjVQtTHTbioum2gvmPnFk4zYAt7zne',
|
|
79
76
|
launchTs: 1642629253786,
|
|
@@ -81,7 +78,7 @@ exports.Markets = [
|
|
|
81
78
|
{
|
|
82
79
|
symbol: 'ADA-PERP',
|
|
83
80
|
baseAssetSymbol: 'ADA',
|
|
84
|
-
marketIndex: new
|
|
81
|
+
marketIndex: new __1.BN(9),
|
|
85
82
|
devnetPythOracle: '8oGTURNmSQkrBS1AQ5NjB2p8qY34UVmMA9ojrw8vnHus',
|
|
86
83
|
mainnetPythOracle: '3pyn4svBbxJ9Wnn3RVeafyLWfzie6yC5eTig2S62v9SC',
|
|
87
84
|
launchTs: 1643084413000,
|
|
@@ -89,11 +86,19 @@ exports.Markets = [
|
|
|
89
86
|
{
|
|
90
87
|
symbol: 'ALGO-PERP',
|
|
91
88
|
baseAssetSymbol: 'ALGO',
|
|
92
|
-
marketIndex: new
|
|
89
|
+
marketIndex: new __1.BN(10),
|
|
93
90
|
devnetPythOracle: 'c1A946dY5NHuVda77C8XXtXytyR3wK1SCP3eA9VRfC3',
|
|
94
91
|
mainnetPythOracle: 'HqFyq1wh1xKvL7KDqqT7NJeSPdAqsDqnmBisUC2XdXAX',
|
|
95
92
|
launchTs: 1643686767000,
|
|
96
93
|
},
|
|
94
|
+
{
|
|
95
|
+
symbol: 'FTT-PERP',
|
|
96
|
+
baseAssetSymbol: 'FTT',
|
|
97
|
+
marketIndex: new __1.BN(11),
|
|
98
|
+
devnetPythOracle: '6vivTRs5ZPeeXbjo7dfburfaYDWoXjBtdtuYgQRuGfu',
|
|
99
|
+
mainnetPythOracle: '8JPJJkmDScpcNmBRKGZuPuG2GYAveQgP3t5gFuMymwvF',
|
|
100
|
+
launchTs: 1644382122000,
|
|
101
|
+
},
|
|
97
102
|
// {
|
|
98
103
|
// symbol: 'mSOL-PERP',
|
|
99
104
|
// baseAssetSymbol: 'mSOL',
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { BN } from '../';
|
|
2
3
|
export declare const ZERO: BN;
|
|
3
4
|
export declare const ONE: BN;
|
|
4
5
|
export declare const TEN_THOUSAND: BN;
|
|
@@ -11,6 +12,7 @@ export declare const MARK_PRICE_PRECISION: BN;
|
|
|
11
12
|
export declare const FUNDING_PAYMENT_PRECISION: BN;
|
|
12
13
|
export declare const PEG_PRECISION: BN;
|
|
13
14
|
export declare const AMM_RESERVE_PRECISION: BN;
|
|
15
|
+
export declare const BASE_PRECISION: BN;
|
|
14
16
|
export declare const AMM_TO_QUOTE_PRECISION_RATIO: BN;
|
|
15
17
|
export declare const PRICE_TO_QUOTE_PRECISION: BN;
|
|
16
18
|
export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: BN;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numericConstants.d.ts","sourceRoot":"","sources":["../../src/constants/numericConstants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"numericConstants.d.ts","sourceRoot":"","sources":["../../src/constants/numericConstants.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AAEzB,eAAO,MAAM,IAAI,IAAY,CAAC;AAC9B,eAAO,MAAM,GAAG,IAAY,CAAC;AAC7B,eAAO,MAAM,YAAY,IAAgB,CAAC;AAC1C,eAAO,MAAM,MAAM,IAAkC,CAAC;AAEtD,eAAO,MAAM,YAAY,IAAY,CAAC;AACtC,eAAO,MAAM,sBAAsB,IAAc,CAAC;AAClD,eAAO,MAAM,yBAAyB,IAAc,CAAC;AAErD,eAAO,MAAM,eAAe,IAAkB,CAAC;AAC/C,eAAO,MAAM,oBAAoB,IAAmB,CAAC;AACrD,eAAO,MAAM,yBAAyB,IAAgB,CAAC;AACvD,eAAO,MAAM,aAAa,IAAe,CAAC;AAE1C,eAAO,MAAM,qBAAqB,IAAmB,CAAC;AACtD,eAAO,MAAM,cAAc,IAAwB,CAAC;AACpD,eAAO,MAAM,4BAA4B,IACE,CAAC;AAC5C,eAAO,MAAM,wBAAwB,IACK,CAAC;AAC3C,eAAO,MAAM,sCAAsC,IACW,CAAC"}
|
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.PARTIAL_LIQUIDATION_RATIO = exports.FULL_LIQUIDATION_RATIO = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = exports.ONE = exports.ZERO = void 0;
|
|
7
|
-
const
|
|
8
|
-
exports.ZERO = new
|
|
9
|
-
exports.ONE = new
|
|
10
|
-
exports.TEN_THOUSAND = new
|
|
11
|
-
exports.BN_MAX = new
|
|
12
|
-
exports.MAX_LEVERAGE = new
|
|
13
|
-
exports.FULL_LIQUIDATION_RATIO = new
|
|
14
|
-
exports.PARTIAL_LIQUIDATION_RATIO = new
|
|
15
|
-
exports.QUOTE_PRECISION = new
|
|
16
|
-
exports.MARK_PRICE_PRECISION = new
|
|
17
|
-
exports.FUNDING_PAYMENT_PRECISION = new
|
|
18
|
-
exports.PEG_PRECISION = new
|
|
19
|
-
exports.AMM_RESERVE_PRECISION = new
|
|
3
|
+
exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.PARTIAL_LIQUIDATION_RATIO = exports.FULL_LIQUIDATION_RATIO = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = 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.TEN_THOUSAND = new __1.BN(10000);
|
|
8
|
+
exports.BN_MAX = new __1.BN(Number.MAX_SAFE_INTEGER);
|
|
9
|
+
exports.MAX_LEVERAGE = new __1.BN(5);
|
|
10
|
+
exports.FULL_LIQUIDATION_RATIO = new __1.BN(500);
|
|
11
|
+
exports.PARTIAL_LIQUIDATION_RATIO = new __1.BN(625);
|
|
12
|
+
exports.QUOTE_PRECISION = new __1.BN(Math.pow(10, 6));
|
|
13
|
+
exports.MARK_PRICE_PRECISION = new __1.BN(Math.pow(10, 10));
|
|
14
|
+
exports.FUNDING_PAYMENT_PRECISION = new __1.BN(10000);
|
|
15
|
+
exports.PEG_PRECISION = new __1.BN(1000);
|
|
16
|
+
exports.AMM_RESERVE_PRECISION = new __1.BN(Math.pow(10, 13));
|
|
17
|
+
exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION;
|
|
20
18
|
exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.div(exports.QUOTE_PRECISION); // 10^7
|
|
21
19
|
exports.PRICE_TO_QUOTE_PRECISION = exports.MARK_PRICE_PRECISION.div(exports.QUOTE_PRECISION);
|
|
22
20
|
exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.mul(exports.PEG_PRECISION).div(exports.QUOTE_PRECISION); // 10^10
|
|
@@ -53,7 +53,7 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
53
53
|
// Get current price
|
|
54
54
|
const solMarketInfo = __2.Markets.find((market) => market.baseAssetSymbol === 'SOL');
|
|
55
55
|
const currentMarketPrice = __2.calculateMarkPrice(clearingHouse.getMarket(solMarketInfo.marketIndex));
|
|
56
|
-
const formattedPrice = __2.convertToNumber(currentMarketPrice, __2.
|
|
56
|
+
const formattedPrice = __2.convertToNumber(currentMarketPrice, __2.MARK_PRICE_PRECISION);
|
|
57
57
|
console.log(`Current Market Price is $${formattedPrice}`);
|
|
58
58
|
// Estimate the slippage for a $5000 LONG trade
|
|
59
59
|
const solMarketAccount = clearingHouse.getMarket(solMarketInfo.marketIndex);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ConfirmOptions, Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { IWallet } from '../types';
|
|
3
|
+
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
4
|
+
import { TxSender } from '../tx/types';
|
|
5
|
+
import { ClearingHouse } from '../clearingHouse';
|
|
6
|
+
import { Admin } from '../admin';
|
|
7
|
+
export declare type ClearingHouseConfigType = 'websocket' | 'polling' | 'custom';
|
|
8
|
+
declare type BaseClearingHouseConfig = {
|
|
9
|
+
type: ClearingHouseConfigType;
|
|
10
|
+
connection: Connection;
|
|
11
|
+
wallet: IWallet;
|
|
12
|
+
programID: PublicKey;
|
|
13
|
+
opts?: ConfirmOptions;
|
|
14
|
+
txSender?: TxSender;
|
|
15
|
+
};
|
|
16
|
+
declare type WebSocketClearingHouseConfiguration = BaseClearingHouseConfig;
|
|
17
|
+
declare type PollingClearingHouseConfiguration = BaseClearingHouseConfig & {
|
|
18
|
+
accountLoader: BulkAccountLoader;
|
|
19
|
+
};
|
|
20
|
+
declare type ClearingHouseConfig = PollingClearingHouseConfiguration | WebSocketClearingHouseConfiguration;
|
|
21
|
+
export declare function getWebSocketClearingHouseConfig(connection: Connection, wallet: IWallet, programID: PublicKey, opts?: ConfirmOptions, txSender?: TxSender): WebSocketClearingHouseConfiguration;
|
|
22
|
+
export declare function getPollingClearingHouseConfig(connection: Connection, wallet: IWallet, programID: PublicKey, accountLoader: BulkAccountLoader, opts?: ConfirmOptions, txSender?: TxSender): PollingClearingHouseConfiguration;
|
|
23
|
+
export declare function getClearingHouse(config: ClearingHouseConfig): ClearingHouse;
|
|
24
|
+
export declare function getAdmin(config: ClearingHouseConfig): Admin;
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=clearingHouse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
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AC,EAAE,QAAQ,GACjB,iCAAiC,CAUnC;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,mBAAmB,GAAG,aAAa,CA0B3E;AAED,wBAAgB,QAAQ,CAAC,MAAM,EAAE,mBAAmB,GAAG,KAAK,CA0B3D"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getAdmin = exports.getClearingHouse = exports.getPollingClearingHouseConfig = exports.getWebSocketClearingHouseConfig = void 0;
|
|
7
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
8
|
+
const clearingHouse_1 = require("../clearingHouse");
|
|
9
|
+
const clearing_house_json_1 = __importDefault(require("../idl/clearing_house.json"));
|
|
10
|
+
const webSocketClearingHouseAccountSubscriber_1 = require("../accounts/webSocketClearingHouseAccountSubscriber");
|
|
11
|
+
const defaultTxSender_1 = require("../tx/defaultTxSender");
|
|
12
|
+
const pollingClearingHouseAccountSubscriber_1 = require("../accounts/pollingClearingHouseAccountSubscriber");
|
|
13
|
+
const admin_1 = require("../admin");
|
|
14
|
+
function getWebSocketClearingHouseConfig(connection, wallet, programID, opts = anchor_1.Provider.defaultOptions(), txSender) {
|
|
15
|
+
return {
|
|
16
|
+
type: 'websocket',
|
|
17
|
+
connection,
|
|
18
|
+
wallet,
|
|
19
|
+
programID,
|
|
20
|
+
opts,
|
|
21
|
+
txSender,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
exports.getWebSocketClearingHouseConfig = getWebSocketClearingHouseConfig;
|
|
25
|
+
function getPollingClearingHouseConfig(connection, wallet, programID, accountLoader, opts = anchor_1.Provider.defaultOptions(), txSender) {
|
|
26
|
+
return {
|
|
27
|
+
type: 'polling',
|
|
28
|
+
connection,
|
|
29
|
+
wallet,
|
|
30
|
+
programID,
|
|
31
|
+
accountLoader,
|
|
32
|
+
opts,
|
|
33
|
+
txSender,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
exports.getPollingClearingHouseConfig = getPollingClearingHouseConfig;
|
|
37
|
+
function getClearingHouse(config) {
|
|
38
|
+
const provider = new anchor_1.Provider(config.connection, config.wallet, config.opts);
|
|
39
|
+
const program = new anchor_1.Program(clearing_house_json_1.default, config.programID, provider);
|
|
40
|
+
let accountSubscriber;
|
|
41
|
+
if (config.type === 'websocket') {
|
|
42
|
+
accountSubscriber = new webSocketClearingHouseAccountSubscriber_1.WebSocketClearingHouseAccountSubscriber(program);
|
|
43
|
+
}
|
|
44
|
+
else if (config.type === 'polling') {
|
|
45
|
+
accountSubscriber = new pollingClearingHouseAccountSubscriber_1.PollingClearingHouseAccountSubscriber(program, config.accountLoader);
|
|
46
|
+
}
|
|
47
|
+
const txSender = config.txSender || new defaultTxSender_1.DefaultTxSender(provider);
|
|
48
|
+
return new clearingHouse_1.ClearingHouse(config.connection, config.wallet, program, accountSubscriber, txSender, config.opts);
|
|
49
|
+
}
|
|
50
|
+
exports.getClearingHouse = getClearingHouse;
|
|
51
|
+
function getAdmin(config) {
|
|
52
|
+
const provider = new anchor_1.Provider(config.connection, config.wallet, config.opts);
|
|
53
|
+
const program = new anchor_1.Program(clearing_house_json_1.default, config.programID, provider);
|
|
54
|
+
let accountSubscriber;
|
|
55
|
+
if (config.type === 'websocket') {
|
|
56
|
+
accountSubscriber = new webSocketClearingHouseAccountSubscriber_1.WebSocketClearingHouseAccountSubscriber(program);
|
|
57
|
+
}
|
|
58
|
+
else if (config.type === 'polling') {
|
|
59
|
+
accountSubscriber = new pollingClearingHouseAccountSubscriber_1.PollingClearingHouseAccountSubscriber(program, config.accountLoader);
|
|
60
|
+
}
|
|
61
|
+
const txSender = config.txSender || new defaultTxSender_1.DefaultTxSender(provider);
|
|
62
|
+
return new admin_1.Admin(config.connection, config.wallet, program, accountSubscriber, txSender, config.opts);
|
|
63
|
+
}
|
|
64
|
+
exports.getAdmin = getAdmin;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { ClearingHouse } from '../clearingHouse';
|
|
3
|
+
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
4
|
+
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
5
|
+
export declare type ClearingHouseUserConfigType = 'websocket' | 'polling' | 'custom';
|
|
6
|
+
declare type BaseClearingHouseUserConfig = {
|
|
7
|
+
type: ClearingHouseUserConfigType;
|
|
8
|
+
clearingHouse: ClearingHouse;
|
|
9
|
+
authority: PublicKey;
|
|
10
|
+
};
|
|
11
|
+
declare type WebSocketClearingHouseUserConfig = BaseClearingHouseUserConfig;
|
|
12
|
+
declare type PollingClearingHouseUserConfig = BaseClearingHouseUserConfig & {
|
|
13
|
+
accountLoader: BulkAccountLoader;
|
|
14
|
+
};
|
|
15
|
+
declare type ClearingHouseUserConfig = PollingClearingHouseUserConfig | WebSocketClearingHouseUserConfig;
|
|
16
|
+
export declare function getWebSocketClearingHouseUserConfig(clearingHouse: ClearingHouse, authority: PublicKey): WebSocketClearingHouseUserConfig;
|
|
17
|
+
export declare function getPollingClearingHouseUserConfig(clearingHouse: ClearingHouse, authority: PublicKey, accountLoader: BulkAccountLoader): PollingClearingHouseUserConfig;
|
|
18
|
+
export declare function getClearingHouseUser(config: ClearingHouseUserConfig): ClearingHouseUser;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=clearingHouseUser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clearingHouseUser.d.ts","sourceRoot":"","sources":["../../src/factory/clearingHouseUser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAKzD,oBAAY,2BAA2B,GAAG,WAAW,GAAG,SAAS,GAAG,QAAQ,CAAC;AAE7E,aAAK,2BAA2B,GAAG;IAClC,IAAI,EAAE,2BAA2B,CAAC;IAClC,aAAa,EAAE,aAAa,CAAC;IAC7B,SAAS,EAAE,SAAS,CAAC;CACrB,CAAC;AAEF,aAAK,gCAAgC,GAAG,2BAA2B,CAAC;AAEpE,aAAK,8BAA8B,GAAG,2BAA2B,GAAG;IACnE,aAAa,EAAE,iBAAiB,CAAC;CACjC,CAAC;AAEF,aAAK,uBAAuB,GACzB,8BAA8B,GAC9B,gCAAgC,CAAC;AAEpC,wBAAgB,mCAAmC,CAClD,aAAa,EAAE,aAAa,EAC5B,SAAS,EAAE,SAAS,GAClB,gCAAgC,CAMlC;AAED,wBAAgB,iCAAiC,CAChD,aAAa,EAAE,aAAa,EAC5B,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,iBAAiB,GAC9B,8BAA8B,CAOhC;AAED,wBAAgB,oBAAoB,CACnC,MAAM,EAAE,uBAAuB,GAC7B,iBAAiB,CAoBnB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getClearingHouseUser = exports.getPollingClearingHouseUserConfig = exports.getWebSocketClearingHouseUserConfig = void 0;
|
|
4
|
+
const clearingHouseUser_1 = require("../clearingHouseUser");
|
|
5
|
+
const webSocketUserAccountSubscriber_1 = require("../accounts/webSocketUserAccountSubscriber");
|
|
6
|
+
const pollingUserAccountSubscriber_1 = require("../accounts/pollingUserAccountSubscriber");
|
|
7
|
+
function getWebSocketClearingHouseUserConfig(clearingHouse, authority) {
|
|
8
|
+
return {
|
|
9
|
+
type: 'websocket',
|
|
10
|
+
clearingHouse,
|
|
11
|
+
authority,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.getWebSocketClearingHouseUserConfig = getWebSocketClearingHouseUserConfig;
|
|
15
|
+
function getPollingClearingHouseUserConfig(clearingHouse, authority, accountLoader) {
|
|
16
|
+
return {
|
|
17
|
+
type: 'polling',
|
|
18
|
+
clearingHouse,
|
|
19
|
+
authority,
|
|
20
|
+
accountLoader,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.getPollingClearingHouseUserConfig = getPollingClearingHouseUserConfig;
|
|
24
|
+
function getClearingHouseUser(config) {
|
|
25
|
+
let accountSubscriber;
|
|
26
|
+
if (config.type === 'websocket') {
|
|
27
|
+
accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(config.clearingHouse.program, config.authority);
|
|
28
|
+
}
|
|
29
|
+
else if (config.type === 'polling') {
|
|
30
|
+
accountSubscriber = new pollingUserAccountSubscriber_1.PollingUserAccountSubscriber(config.clearingHouse.program, config.authority, config.accountLoader);
|
|
31
|
+
}
|
|
32
|
+
return new clearingHouseUser_1.ClearingHouseUser(config.clearingHouse, config.authority, accountSubscriber);
|
|
33
|
+
}
|
|
34
|
+
exports.getClearingHouseUser = getClearingHouseUser;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
3
|
export * from './mockUSDCFaucet';
|
|
3
4
|
export * from './pythClient';
|
|
4
5
|
export * from './types';
|
|
5
6
|
export * from './constants/markets';
|
|
6
|
-
export * from './accounts/
|
|
7
|
+
export * from './accounts/webSocketClearingHouseAccountSubscriber';
|
|
8
|
+
export * from './accounts/bulkAccountLoader';
|
|
9
|
+
export * from './accounts/pollingClearingHouseAccountSubscriber';
|
|
10
|
+
export * from './accounts/pollingTokenAccountSubscriber';
|
|
7
11
|
export * from './accounts/types';
|
|
8
12
|
export * from './addresses';
|
|
9
13
|
export * from './admin';
|
|
10
14
|
export * from './clearingHouseUser';
|
|
11
15
|
export * from './clearingHouse';
|
|
16
|
+
export * from './factory/clearingHouse';
|
|
17
|
+
export * from './factory/clearingHouseUser';
|
|
12
18
|
export * from './math/conversion';
|
|
13
19
|
export * from './math/funding';
|
|
14
20
|
export * from './math/insuranceFund';
|
|
@@ -23,5 +29,5 @@ export * from './config';
|
|
|
23
29
|
export * from './constants/numericConstants';
|
|
24
30
|
export * from './util/computeUnits';
|
|
25
31
|
export * from './util/tps';
|
|
26
|
-
export { BN };
|
|
32
|
+
export { BN, PublicKey };
|
|
27
33
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,uBAAuB,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC;AACxB,cAAc,qBAAqB,CAAC;AACpC,cAAc,oDAAoD,CAAC;AACnE,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kDAAkD,CAAC;AACjE,cAAc,0CAA0C,CAAC;AACzD,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAE3B,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -10,19 +10,26 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.BN = void 0;
|
|
13
|
+
exports.PublicKey = exports.BN = void 0;
|
|
14
14
|
const anchor_1 = require("@project-serum/anchor");
|
|
15
15
|
Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return anchor_1.BN; } });
|
|
16
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
+
Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return web3_js_1.PublicKey; } });
|
|
16
18
|
__exportStar(require("./mockUSDCFaucet"), exports);
|
|
17
19
|
__exportStar(require("./pythClient"), exports);
|
|
18
20
|
__exportStar(require("./types"), exports);
|
|
19
21
|
__exportStar(require("./constants/markets"), exports);
|
|
20
|
-
__exportStar(require("./accounts/
|
|
22
|
+
__exportStar(require("./accounts/webSocketClearingHouseAccountSubscriber"), exports);
|
|
23
|
+
__exportStar(require("./accounts/bulkAccountLoader"), exports);
|
|
24
|
+
__exportStar(require("./accounts/pollingClearingHouseAccountSubscriber"), exports);
|
|
25
|
+
__exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
|
|
21
26
|
__exportStar(require("./accounts/types"), exports);
|
|
22
27
|
__exportStar(require("./addresses"), exports);
|
|
23
28
|
__exportStar(require("./admin"), exports);
|
|
24
29
|
__exportStar(require("./clearingHouseUser"), exports);
|
|
25
30
|
__exportStar(require("./clearingHouse"), exports);
|
|
31
|
+
__exportStar(require("./factory/clearingHouse"), exports);
|
|
32
|
+
__exportStar(require("./factory/clearingHouseUser"), exports);
|
|
26
33
|
__exportStar(require("./math/conversion"), exports);
|
|
27
34
|
__exportStar(require("./math/funding"), exports);
|
|
28
35
|
__exportStar(require("./math/insuranceFund"), exports);
|