@drift-labs/sdk 0.2.0-master.32 → 0.2.0-master.34
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.js +2 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +2 -2
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +8 -8
- package/lib/accounts/types.d.ts +1 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +2 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +7 -7
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +4 -4
- package/lib/addresses/pda.js +23 -22
- package/lib/admin.d.ts +30 -32
- package/lib/admin.js +111 -119
- package/lib/clearingHouse.d.ts +43 -34
- package/lib/clearingHouse.js +353 -193
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +26 -5
- package/lib/clearingHouseUser.js +151 -51
- package/lib/config.d.ts +2 -0
- package/lib/config.js +5 -1
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +3 -2
- package/lib/dlob/DLOB.d.ts +18 -11
- package/lib/dlob/DLOB.js +179 -107
- package/lib/dlob/DLOBNode.js +2 -10
- package/lib/dlob/NodeList.js +1 -1
- package/lib/events/eventSubscriber.d.ts +1 -0
- package/lib/events/eventSubscriber.js +11 -4
- package/lib/events/fetchLogs.d.ts +3 -1
- package/lib/events/fetchLogs.js +13 -5
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/events/types.d.ts +1 -1
- package/lib/events/webSocketLogProvider.js +1 -1
- package/lib/factory/bigNum.d.ts +5 -4
- package/lib/factory/bigNum.js +36 -6
- package/lib/idl/clearing_house.json +1540 -1246
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/math/amm.js +9 -9
- package/lib/math/exchangeStatus.d.ts +4 -0
- package/lib/math/exchangeStatus.js +18 -0
- package/lib/math/funding.js +10 -10
- package/lib/math/margin.js +6 -1
- package/lib/math/market.js +9 -9
- package/lib/math/orders.d.ts +7 -3
- package/lib/math/orders.js +39 -31
- package/lib/math/repeg.js +3 -3
- package/lib/math/spotBalance.js +3 -3
- package/lib/math/spotPosition.js +2 -2
- package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/serum/serumSubscriber.d.ts +4 -0
- package/lib/serum/serumSubscriber.js +16 -1
- package/lib/types.d.ts +60 -75
- package/lib/types.js +2 -1
- package/lib/userMap/userMap.d.ts +17 -1
- package/lib/userMap/userMap.js +12 -0
- package/lib/userName.d.ts +1 -0
- package/lib/userName.js +3 -2
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +5 -1
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +9 -9
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +8 -8
- package/src/addresses/marketAddresses.ts +2 -2
- package/src/addresses/pda.ts +20 -20
- package/src/admin.ts +246 -221
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +556 -236
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.ts +237 -87
- package/src/config.ts +8 -1
- package/src/constants/numericConstants.ts +5 -1
- package/src/dlob/DLOB.ts +257 -120
- package/src/dlob/DLOBNode.ts +2 -14
- package/src/dlob/NodeList.ts +1 -1
- package/src/events/eventList.js +77 -0
- package/src/events/eventSubscriber.ts +18 -4
- package/src/events/fetchLogs.ts +20 -5
- package/src/events/pollingLogProvider.ts +1 -1
- package/src/events/types.ts +2 -1
- package/src/events/webSocketLogProvider.ts +1 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/factory/bigNum.ts +59 -6
- package/src/idl/clearing_house.json +1540 -1246
- package/src/idl/pyth.json +98 -2
- package/src/index.ts +3 -0
- package/src/math/amm.ts +9 -9
- package/src/math/exchangeStatus.ts +31 -0
- package/src/math/funding.ts +20 -10
- package/src/math/margin.ts +7 -1
- package/src/math/market.ts +9 -9
- package/src/math/orders.ts +44 -29
- package/src/math/repeg.ts +3 -3
- package/src/math/spotBalance.ts +4 -4
- package/src/math/spotPosition.ts +2 -2
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +20 -1
- package/src/token/index.js +38 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/types.ts +65 -51
- package/src/userMap/userMap.ts +25 -1
- package/src/userName.ts +2 -1
- package/src/util/computeUnits.js +27 -0
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/tests/bn/test.ts +22 -1
- package/tests/dlob/helpers.ts +252 -81
- package/tests/dlob/test.ts +1040 -219
package/lib/types.d.ts
CHANGED
|
@@ -251,7 +251,7 @@ export declare type NewUserRecord = {
|
|
|
251
251
|
ts: BN;
|
|
252
252
|
userAuthority: PublicKey;
|
|
253
253
|
user: PublicKey;
|
|
254
|
-
|
|
254
|
+
subAccount: number;
|
|
255
255
|
name: number[];
|
|
256
256
|
referrer: PublicKey;
|
|
257
257
|
};
|
|
@@ -347,8 +347,8 @@ export declare type FundingRateRecord = {
|
|
|
347
347
|
oraclePriceTwap: BN;
|
|
348
348
|
markPriceTwap: BN;
|
|
349
349
|
periodRevenue: BN;
|
|
350
|
-
|
|
351
|
-
|
|
350
|
+
baseAssetAmountWithAmm: BN;
|
|
351
|
+
baseAssetAmountWithUnsettledLp: BN;
|
|
352
352
|
};
|
|
353
353
|
export declare type FundingPaymentRecord = {
|
|
354
354
|
ts: BN;
|
|
@@ -486,7 +486,6 @@ export declare type OrderActionRecord = {
|
|
|
486
486
|
makerOrderBaseAssetAmount: BN | null;
|
|
487
487
|
makerOrderCumulativeBaseAssetAmountFilled: BN | null;
|
|
488
488
|
makerOrderCumulativeQuoteAssetAmountFilled: BN | null;
|
|
489
|
-
makerOrderFee: BN | null;
|
|
490
489
|
oraclePrice: BN;
|
|
491
490
|
};
|
|
492
491
|
export declare type StateAccount = {
|
|
@@ -497,7 +496,6 @@ export declare type StateAccount = {
|
|
|
497
496
|
oracleGuardRails: OracleGuardRails;
|
|
498
497
|
numberOfMarkets: number;
|
|
499
498
|
numberOfSpotMarkets: number;
|
|
500
|
-
minOrderQuoteAssetAmount: BN;
|
|
501
499
|
minPerpAuctionDuration: number;
|
|
502
500
|
defaultMarketOrderTimeInForce: number;
|
|
503
501
|
defaultSpotAuctionDuration: number;
|
|
@@ -508,19 +506,18 @@ export declare type StateAccount = {
|
|
|
508
506
|
srmVault: PublicKey;
|
|
509
507
|
perpFeeStructure: FeeStructure;
|
|
510
508
|
spotFeeStructure: FeeStructure;
|
|
509
|
+
lpCooldownTime: BN;
|
|
511
510
|
};
|
|
512
511
|
export declare type PerpMarketAccount = {
|
|
513
512
|
status: MarketStatus;
|
|
514
513
|
contractType: ContractType;
|
|
515
514
|
expiryTs: BN;
|
|
516
|
-
|
|
515
|
+
expiryPrice: BN;
|
|
517
516
|
marketIndex: number;
|
|
518
517
|
pubkey: PublicKey;
|
|
518
|
+
name: number[];
|
|
519
519
|
amm: AMM;
|
|
520
|
-
|
|
521
|
-
baseAssetAmountLong: BN;
|
|
522
|
-
baseAssetAmountShort: BN;
|
|
523
|
-
openInterest: BN;
|
|
520
|
+
numberOfUsers: BN;
|
|
524
521
|
marginRatioInitial: number;
|
|
525
522
|
marginRatioMaintenance: number;
|
|
526
523
|
nextFillRecordId: BN;
|
|
@@ -528,15 +525,17 @@ export declare type PerpMarketAccount = {
|
|
|
528
525
|
liquidatorFee: BN;
|
|
529
526
|
ifLiquidationFee: BN;
|
|
530
527
|
imfFactor: BN;
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
528
|
+
unrealizedPnlImfFactor: BN;
|
|
529
|
+
unrealizedPnlMaxImbalance: BN;
|
|
530
|
+
unrealizedPnlInitialAssetWeight: number;
|
|
531
|
+
unrealizedPnlMaintenanceAssetWeight: number;
|
|
532
|
+
insuranceClaim: {
|
|
533
|
+
revenueWithdrawSinceLastSettle: BN;
|
|
534
|
+
maxRevenueWithdrawPerPeriod: BN;
|
|
535
|
+
lastRevenueWithdrawTs: BN;
|
|
536
|
+
quoteSettledInsurance: BN;
|
|
537
|
+
quoteMaxInsurance: BN;
|
|
538
|
+
};
|
|
540
539
|
};
|
|
541
540
|
export declare type HistoricalOracleData = {
|
|
542
541
|
lastOraclePrice: BN;
|
|
@@ -550,7 +549,7 @@ export declare type HistoricalIndexData = {
|
|
|
550
549
|
lastIndexBidPrice: BN;
|
|
551
550
|
lastIndexAskPrice: BN;
|
|
552
551
|
lastIndexPriceTwap: BN;
|
|
553
|
-
|
|
552
|
+
lastIndexPriceTwap5min: BN;
|
|
554
553
|
lastIndexPriceTwapTs: BN;
|
|
555
554
|
};
|
|
556
555
|
export declare type SpotMarketAccount = {
|
|
@@ -564,13 +563,18 @@ export declare type SpotMarketAccount = {
|
|
|
564
563
|
oracleSource: OracleSource;
|
|
565
564
|
historicalOracleData: HistoricalOracleData;
|
|
566
565
|
historicalIndexData: HistoricalIndexData;
|
|
567
|
-
|
|
568
|
-
|
|
566
|
+
insuranceFund: {
|
|
567
|
+
vault: PublicKey;
|
|
568
|
+
totalShares: BN;
|
|
569
|
+
userShares: BN;
|
|
570
|
+
sharesBase: BN;
|
|
571
|
+
unstakingPeriod: BN;
|
|
572
|
+
lastRevenueSettleTs: BN;
|
|
573
|
+
revenueSettlePeriod: BN;
|
|
574
|
+
totalFactor: number;
|
|
575
|
+
userFactor: number;
|
|
576
|
+
};
|
|
569
577
|
revenuePool: PoolBalance;
|
|
570
|
-
totalIfShares: BN;
|
|
571
|
-
userIfShares: BN;
|
|
572
|
-
userIfFactor: number;
|
|
573
|
-
totalIfFactor: number;
|
|
574
578
|
ifLiquidationFee: BN;
|
|
575
579
|
decimals: number;
|
|
576
580
|
optimalUtilization: number;
|
|
@@ -594,12 +598,13 @@ export declare type SpotMarketAccount = {
|
|
|
594
598
|
borrowTokenTwap: BN;
|
|
595
599
|
utilizationTwap: BN;
|
|
596
600
|
orderStepSize: BN;
|
|
601
|
+
orderTickSize: BN;
|
|
597
602
|
nextFillRecordId: BN;
|
|
598
603
|
spotFeePool: PoolBalance;
|
|
599
604
|
totalSpotFee: BN;
|
|
600
605
|
};
|
|
601
606
|
export declare type PoolBalance = {
|
|
602
|
-
|
|
607
|
+
scaledBalance: BN;
|
|
603
608
|
marketIndex: number;
|
|
604
609
|
};
|
|
605
610
|
export declare type AMM = {
|
|
@@ -629,14 +634,16 @@ export declare type AMM = {
|
|
|
629
634
|
cumulativeFeePerLp: BN;
|
|
630
635
|
cumulativeNetBaseAssetAmountPerLp: BN;
|
|
631
636
|
userLpShares: BN;
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
637
|
+
baseAssetAmountWithUnsettledLp: BN;
|
|
638
|
+
orderStepSize: BN;
|
|
639
|
+
orderTickSize: BN;
|
|
640
|
+
maxFillReserveFraction: number;
|
|
636
641
|
maxSlippageRatio: number;
|
|
637
642
|
baseSpread: number;
|
|
638
643
|
curveUpdateIntensity: number;
|
|
639
|
-
|
|
644
|
+
baseAssetAmountWithAmm: BN;
|
|
645
|
+
baseAssetAmountLong: BN;
|
|
646
|
+
baseAssetAmountShort: BN;
|
|
640
647
|
quoteAssetAmountLong: BN;
|
|
641
648
|
quoteAssetAmountShort: BN;
|
|
642
649
|
terminalQuoteAssetReserve: BN;
|
|
@@ -651,8 +658,8 @@ export declare type AMM = {
|
|
|
651
658
|
longSpread: BN;
|
|
652
659
|
shortSpread: BN;
|
|
653
660
|
maxSpread: number;
|
|
654
|
-
|
|
655
|
-
|
|
661
|
+
baseAssetAmountPerLp: BN;
|
|
662
|
+
quoteAssetAmountPerLp: BN;
|
|
656
663
|
ammJitIntensity: number;
|
|
657
664
|
maxBaseAssetReserve: BN;
|
|
658
665
|
minBaseAssetReserve: BN;
|
|
@@ -674,7 +681,7 @@ export declare type PerpPosition = {
|
|
|
674
681
|
lastNetQuoteAssetAmountPerLp: BN;
|
|
675
682
|
};
|
|
676
683
|
export declare type UserStatsAccount = {
|
|
677
|
-
|
|
684
|
+
numberOfSubAccounts: number;
|
|
678
685
|
makerVolume30D: BN;
|
|
679
686
|
takerVolume30D: BN;
|
|
680
687
|
fillerVolume30D: BN;
|
|
@@ -686,31 +693,36 @@ export declare type UserStatsAccount = {
|
|
|
686
693
|
totalFeeRebate: BN;
|
|
687
694
|
totalTokenDiscount: BN;
|
|
688
695
|
totalRefereeDiscount: BN;
|
|
696
|
+
totalReferrerReward: BN;
|
|
697
|
+
current_epoch_referrer_reward: BN;
|
|
689
698
|
};
|
|
690
699
|
referrer: PublicKey;
|
|
691
700
|
isReferrer: boolean;
|
|
692
|
-
totalReferrerReward: BN;
|
|
693
701
|
authority: PublicKey;
|
|
694
|
-
|
|
702
|
+
ifStakedQuoteAssetAmount: BN;
|
|
695
703
|
};
|
|
696
704
|
export declare type UserAccount = {
|
|
697
705
|
authority: PublicKey;
|
|
698
706
|
delegate: PublicKey;
|
|
699
707
|
name: number[];
|
|
700
|
-
|
|
708
|
+
subAccountId: number;
|
|
701
709
|
spotPositions: SpotPosition[];
|
|
702
710
|
perpPositions: PerpPosition[];
|
|
703
711
|
orders: Order[];
|
|
704
|
-
|
|
705
|
-
|
|
712
|
+
isBeingLiquidated: boolean;
|
|
713
|
+
isBankrupt: boolean;
|
|
706
714
|
nextLiquidationId: number;
|
|
707
715
|
nextOrderId: number;
|
|
708
|
-
|
|
716
|
+
maxMarginRatio: number;
|
|
717
|
+
lastAddPerpLpSharesTs: BN;
|
|
718
|
+
settledPerpPnl: BN;
|
|
719
|
+
totalDeposits: BN;
|
|
720
|
+
totalWithdraws: BN;
|
|
709
721
|
};
|
|
710
722
|
export declare type SpotPosition = {
|
|
711
723
|
marketIndex: number;
|
|
712
724
|
balanceType: SpotBalanceType;
|
|
713
|
-
|
|
725
|
+
scaledBalance: BN;
|
|
714
726
|
openOrders: number;
|
|
715
727
|
openBids: BN;
|
|
716
728
|
openAsks: BN;
|
|
@@ -720,7 +732,6 @@ export declare type Order = {
|
|
|
720
732
|
status: OrderStatus;
|
|
721
733
|
orderType: OrderType;
|
|
722
734
|
marketType: MarketType;
|
|
723
|
-
ts: BN;
|
|
724
735
|
slot: BN;
|
|
725
736
|
orderId: number;
|
|
726
737
|
userOrderId: number;
|
|
@@ -730,7 +741,6 @@ export declare type Order = {
|
|
|
730
741
|
baseAssetAmountFilled: BN;
|
|
731
742
|
quoteAssetAmount: BN;
|
|
732
743
|
quoteAssetAmountFilled: BN;
|
|
733
|
-
fee: BN;
|
|
734
744
|
direction: PositionDirection;
|
|
735
745
|
reduceOnly: boolean;
|
|
736
746
|
triggerPrice: BN;
|
|
@@ -739,11 +749,11 @@ export declare type Order = {
|
|
|
739
749
|
existingPositionDirection: PositionDirection;
|
|
740
750
|
postOnly: boolean;
|
|
741
751
|
immediateOrCancel: boolean;
|
|
742
|
-
oraclePriceOffset:
|
|
752
|
+
oraclePriceOffset: number;
|
|
743
753
|
auctionDuration: number;
|
|
744
754
|
auctionStartPrice: BN;
|
|
745
755
|
auctionEndPrice: BN;
|
|
746
|
-
|
|
756
|
+
maxTs: BN;
|
|
747
757
|
};
|
|
748
758
|
export declare type OrderParams = {
|
|
749
759
|
orderType: OrderType;
|
|
@@ -759,10 +769,11 @@ export declare type OrderParams = {
|
|
|
759
769
|
triggerPrice: BN | null;
|
|
760
770
|
triggerCondition: OrderTriggerCondition;
|
|
761
771
|
positionLimit: BN;
|
|
762
|
-
oraclePriceOffset:
|
|
772
|
+
oraclePriceOffset: number | null;
|
|
763
773
|
auctionDuration: number | null;
|
|
764
|
-
|
|
774
|
+
maxTs: BN | null;
|
|
765
775
|
auctionStartPrice: BN | null;
|
|
776
|
+
auctionEndPrice: BN | null;
|
|
766
777
|
};
|
|
767
778
|
export declare type NecessaryOrderParams = {
|
|
768
779
|
orderType: OrderType;
|
|
@@ -773,33 +784,7 @@ export declare type NecessaryOrderParams = {
|
|
|
773
784
|
export declare type OptionalOrderParams = {
|
|
774
785
|
[Property in keyof OrderParams]?: OrderParams[Property];
|
|
775
786
|
} & NecessaryOrderParams;
|
|
776
|
-
export declare const DefaultOrderParams:
|
|
777
|
-
orderType: {
|
|
778
|
-
market: {};
|
|
779
|
-
};
|
|
780
|
-
marketType: {
|
|
781
|
-
perp: {};
|
|
782
|
-
};
|
|
783
|
-
userOrderId: number;
|
|
784
|
-
direction: {
|
|
785
|
-
long: {};
|
|
786
|
-
};
|
|
787
|
-
baseAssetAmount: BN;
|
|
788
|
-
price: BN;
|
|
789
|
-
marketIndex: number;
|
|
790
|
-
reduceOnly: boolean;
|
|
791
|
-
postOnly: boolean;
|
|
792
|
-
immediateOrCancel: boolean;
|
|
793
|
-
triggerPrice: any;
|
|
794
|
-
triggerCondition: {
|
|
795
|
-
above: {};
|
|
796
|
-
};
|
|
797
|
-
positionLimit: BN;
|
|
798
|
-
oraclePriceOffset: any;
|
|
799
|
-
auctionDuration: any;
|
|
800
|
-
timeInForce: any;
|
|
801
|
-
auctionStartPrice: any;
|
|
802
|
-
};
|
|
787
|
+
export declare const DefaultOrderParams: OrderParams;
|
|
803
788
|
export declare type MakerInfo = {
|
|
804
789
|
maker: PublicKey;
|
|
805
790
|
makerStats: PublicKey;
|
package/lib/types.js
CHANGED
package/lib/userMap/userMap.d.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { ClearingHouseUser, ClearingHouse, OrderRecord, ClearingHouseUserAccountSubscriptionConfig } from '..';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
|
-
export
|
|
3
|
+
export interface UserMapInterface {
|
|
4
|
+
fetchAllUsers(): Promise<void>;
|
|
5
|
+
addPubkey(userAccountPublicKey: PublicKey): Promise<void>;
|
|
6
|
+
has(key: string): boolean;
|
|
7
|
+
get(key: string): ClearingHouseUser | undefined;
|
|
8
|
+
mustGet(key: string): Promise<ClearingHouseUser>;
|
|
9
|
+
getUserAuthority(key: string): PublicKey | undefined;
|
|
10
|
+
updateWithOrderRecord(record: OrderRecord): Promise<void>;
|
|
11
|
+
values(): IterableIterator<ClearingHouseUser>;
|
|
12
|
+
}
|
|
13
|
+
export declare class UserMap implements UserMapInterface {
|
|
4
14
|
private userMap;
|
|
5
15
|
private clearingHouse;
|
|
6
16
|
private accountSubscription;
|
|
@@ -20,6 +30,12 @@ export declare class UserMap {
|
|
|
20
30
|
* @returns ClearingHouseUser
|
|
21
31
|
*/
|
|
22
32
|
mustGet(key: string): Promise<ClearingHouseUser>;
|
|
33
|
+
/**
|
|
34
|
+
* gets the Authority for a particular userAccountPublicKey, if no ClearingHouseUser exists, undefined is returned
|
|
35
|
+
* @param key userAccountPublicKey to get ClearngHouseUserFor
|
|
36
|
+
* @returns authority PublicKey | undefined
|
|
37
|
+
*/
|
|
38
|
+
getUserAuthority(key: string): PublicKey | undefined;
|
|
23
39
|
updateWithOrderRecord(record: OrderRecord): Promise<void>;
|
|
24
40
|
values(): IterableIterator<ClearingHouseUser>;
|
|
25
41
|
}
|
package/lib/userMap/userMap.js
CHANGED
|
@@ -63,6 +63,18 @@ class UserMap {
|
|
|
63
63
|
await user.fetchAccounts();
|
|
64
64
|
return user;
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* gets the Authority for a particular userAccountPublicKey, if no ClearingHouseUser exists, undefined is returned
|
|
68
|
+
* @param key userAccountPublicKey to get ClearngHouseUserFor
|
|
69
|
+
* @returns authority PublicKey | undefined
|
|
70
|
+
*/
|
|
71
|
+
getUserAuthority(key) {
|
|
72
|
+
const chUser = this.userMap.get(key);
|
|
73
|
+
if (!chUser) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
return chUser.getUserAccount().authority;
|
|
77
|
+
}
|
|
66
78
|
async updateWithOrderRecord(record) {
|
|
67
79
|
await this.addPubkey(record.user);
|
|
68
80
|
}
|
package/lib/userName.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export declare const MAX_NAME_LENGTH = 32;
|
|
2
2
|
export declare const DEFAULT_USER_NAME = "Main Account";
|
|
3
|
+
export declare const DEFAULT_MARKET_NAME = "Default Market Name";
|
|
3
4
|
export declare function encodeName(name: string): number[];
|
|
4
5
|
export declare function decodeName(bytes: number[]): string;
|
package/lib/userName.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.decodeName = exports.encodeName = exports.DEFAULT_USER_NAME = exports.MAX_NAME_LENGTH = void 0;
|
|
3
|
+
exports.decodeName = exports.encodeName = exports.DEFAULT_MARKET_NAME = exports.DEFAULT_USER_NAME = exports.MAX_NAME_LENGTH = void 0;
|
|
4
4
|
exports.MAX_NAME_LENGTH = 32;
|
|
5
5
|
exports.DEFAULT_USER_NAME = 'Main Account';
|
|
6
|
+
exports.DEFAULT_MARKET_NAME = 'Default Market Name';
|
|
6
7
|
function encodeName(name) {
|
|
7
8
|
if (name.length > exports.MAX_NAME_LENGTH) {
|
|
8
|
-
throw Error(`
|
|
9
|
+
throw Error(`Name (${name}) longer than 32 characters`);
|
|
9
10
|
}
|
|
10
11
|
const buffer = Buffer.alloc(32);
|
|
11
12
|
buffer.fill(name);
|
package/package.json
CHANGED
|
@@ -222,7 +222,11 @@ export class BulkAccountLoader {
|
|
|
222
222
|
return;
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
-
|
|
225
|
+
if (this.pollingFrequency !== 0)
|
|
226
|
+
this.intervalId = setInterval(
|
|
227
|
+
this.load.bind(this),
|
|
228
|
+
this.pollingFrequency
|
|
229
|
+
);
|
|
226
230
|
}
|
|
227
231
|
|
|
228
232
|
public stopPolling(): void {
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
import {
|
|
19
19
|
getClearingHouseStateAccountPublicKey,
|
|
20
20
|
getSpotMarketPublicKey,
|
|
21
|
-
|
|
21
|
+
getPerpMarketPublicKey,
|
|
22
22
|
} from '../addresses/pda';
|
|
23
23
|
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
24
24
|
import { capitalize } from './utils';
|
|
@@ -121,26 +121,26 @@ export class PollingClearingHouseAccountSubscriber
|
|
|
121
121
|
eventType: 'stateAccountUpdate',
|
|
122
122
|
});
|
|
123
123
|
|
|
124
|
-
await this.
|
|
124
|
+
await this.updatePerpMarketAccountsToPoll();
|
|
125
125
|
await this.updateSpotMarketAccountsToPoll();
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
async
|
|
128
|
+
async updatePerpMarketAccountsToPoll(): Promise<boolean> {
|
|
129
129
|
for (const marketIndex of this.perpMarketIndexes) {
|
|
130
|
-
await this.
|
|
130
|
+
await this.addPerpMarketAccountToPoll(marketIndex);
|
|
131
131
|
}
|
|
132
132
|
return true;
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
async
|
|
136
|
-
const
|
|
135
|
+
async addPerpMarketAccountToPoll(marketIndex: number): Promise<boolean> {
|
|
136
|
+
const perpMarketPublicKey = await getPerpMarketPublicKey(
|
|
137
137
|
this.program.programId,
|
|
138
138
|
marketIndex
|
|
139
139
|
);
|
|
140
140
|
|
|
141
|
-
this.accountsToPoll.set(
|
|
141
|
+
this.accountsToPoll.set(perpMarketPublicKey.toString(), {
|
|
142
142
|
key: 'perpMarket',
|
|
143
|
-
publicKey:
|
|
143
|
+
publicKey: perpMarketPublicKey,
|
|
144
144
|
eventType: 'perpMarketAccountUpdate',
|
|
145
145
|
mapKey: marketIndex,
|
|
146
146
|
});
|
|
@@ -361,7 +361,7 @@ export class PollingClearingHouseAccountSubscriber
|
|
|
361
361
|
}
|
|
362
362
|
|
|
363
363
|
async addPerpMarket(marketIndex: number): Promise<boolean> {
|
|
364
|
-
await this.
|
|
364
|
+
await this.addPerpMarketAccountToPoll(marketIndex);
|
|
365
365
|
const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
|
|
366
366
|
this.addAccountToAccountLoader(accountToPoll);
|
|
367
367
|
return true;
|
|
@@ -11,7 +11,7 @@ import { EventEmitter } from 'events';
|
|
|
11
11
|
import {
|
|
12
12
|
getClearingHouseStateAccountPublicKey,
|
|
13
13
|
getSpotMarketPublicKey,
|
|
14
|
-
|
|
14
|
+
getPerpMarketPublicKey,
|
|
15
15
|
} from '../addresses/pda';
|
|
16
16
|
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
17
17
|
import { PublicKey } from '@solana/web3.js';
|
|
@@ -91,7 +91,7 @@ export class WebSocketClearingHouseAccountSubscriber
|
|
|
91
91
|
});
|
|
92
92
|
|
|
93
93
|
// subscribe to market accounts
|
|
94
|
-
await this.
|
|
94
|
+
await this.subscribeToPerpMarketAccounts();
|
|
95
95
|
|
|
96
96
|
// subscribe to spot market accounts
|
|
97
97
|
await this.subscribeToSpotMarketAccounts();
|
|
@@ -108,22 +108,22 @@ export class WebSocketClearingHouseAccountSubscriber
|
|
|
108
108
|
return true;
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
async
|
|
111
|
+
async subscribeToPerpMarketAccounts(): Promise<boolean> {
|
|
112
112
|
for (const marketIndex of this.perpMarketIndexes) {
|
|
113
|
-
await this.
|
|
113
|
+
await this.subscribeToPerpMarketAccount(marketIndex);
|
|
114
114
|
}
|
|
115
115
|
return true;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
async
|
|
119
|
-
const
|
|
118
|
+
async subscribeToPerpMarketAccount(marketIndex: number): Promise<boolean> {
|
|
119
|
+
const perpMarketPublicKey = await getPerpMarketPublicKey(
|
|
120
120
|
this.program.programId,
|
|
121
121
|
marketIndex
|
|
122
122
|
);
|
|
123
123
|
const accountSubscriber = new WebSocketAccountSubscriber<PerpMarketAccount>(
|
|
124
124
|
'perpMarket',
|
|
125
125
|
this.program,
|
|
126
|
-
|
|
126
|
+
perpMarketPublicKey
|
|
127
127
|
);
|
|
128
128
|
await accountSubscriber.subscribe((data: PerpMarketAccount) => {
|
|
129
129
|
this.eventEmitter.emit('perpMarketAccountUpdate', data);
|
|
@@ -257,7 +257,7 @@ export class WebSocketClearingHouseAccountSubscriber
|
|
|
257
257
|
if (this.perpMarketAccountSubscribers.has(marketIndex)) {
|
|
258
258
|
return true;
|
|
259
259
|
}
|
|
260
|
-
return this.
|
|
260
|
+
return this.subscribeToPerpMarketAccount(marketIndex);
|
|
261
261
|
}
|
|
262
262
|
|
|
263
263
|
async addOracle(oracleInfo: OracleInfo): Promise<boolean> {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PublicKey } from '@solana/web3.js';
|
|
2
|
-
import {
|
|
2
|
+
import { getPerpMarketPublicKey } from './pda';
|
|
3
3
|
|
|
4
4
|
const CACHE = new Map<string, PublicKey>();
|
|
5
5
|
export async function getMarketAddress(
|
|
@@ -11,7 +11,7 @@ export async function getMarketAddress(
|
|
|
11
11
|
return CACHE.get(cacheKey);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
const publicKey = await
|
|
14
|
+
const publicKey = await getPerpMarketPublicKey(programId, marketIndex);
|
|
15
15
|
CACHE.set(cacheKey, publicKey);
|
|
16
16
|
return publicKey;
|
|
17
17
|
}
|
package/src/addresses/pda.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { BN } from '@project-serum/anchor';
|
|
|
5
5
|
export async function getClearingHouseStateAccountPublicKeyAndNonce(
|
|
6
6
|
programId: PublicKey
|
|
7
7
|
): Promise<[PublicKey, number]> {
|
|
8
|
-
return
|
|
8
|
+
return PublicKey.findProgramAddress(
|
|
9
9
|
[Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house'))],
|
|
10
10
|
programId
|
|
11
11
|
);
|
|
@@ -20,13 +20,13 @@ export async function getClearingHouseStateAccountPublicKey(
|
|
|
20
20
|
export async function getUserAccountPublicKeyAndNonce(
|
|
21
21
|
programId: PublicKey,
|
|
22
22
|
authority: PublicKey,
|
|
23
|
-
|
|
23
|
+
subAccountId = 0
|
|
24
24
|
): Promise<[PublicKey, number]> {
|
|
25
|
-
return
|
|
25
|
+
return PublicKey.findProgramAddress(
|
|
26
26
|
[
|
|
27
27
|
Buffer.from(anchor.utils.bytes.utf8.encode('user')),
|
|
28
28
|
authority.toBuffer(),
|
|
29
|
-
Uint8Array.from([
|
|
29
|
+
Uint8Array.from([subAccountId]),
|
|
30
30
|
],
|
|
31
31
|
programId
|
|
32
32
|
);
|
|
@@ -35,23 +35,23 @@ export async function getUserAccountPublicKeyAndNonce(
|
|
|
35
35
|
export async function getUserAccountPublicKey(
|
|
36
36
|
programId: PublicKey,
|
|
37
37
|
authority: PublicKey,
|
|
38
|
-
|
|
38
|
+
subAccountId = 0
|
|
39
39
|
): Promise<PublicKey> {
|
|
40
40
|
return (
|
|
41
|
-
await getUserAccountPublicKeyAndNonce(programId, authority,
|
|
41
|
+
await getUserAccountPublicKeyAndNonce(programId, authority, subAccountId)
|
|
42
42
|
)[0];
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export function getUserAccountPublicKeySync(
|
|
46
46
|
programId: PublicKey,
|
|
47
47
|
authority: PublicKey,
|
|
48
|
-
|
|
48
|
+
subAccountId = 0
|
|
49
49
|
): PublicKey {
|
|
50
|
-
return
|
|
50
|
+
return PublicKey.findProgramAddressSync(
|
|
51
51
|
[
|
|
52
52
|
Buffer.from(anchor.utils.bytes.utf8.encode('user')),
|
|
53
53
|
authority.toBuffer(),
|
|
54
|
-
Uint8Array.from([
|
|
54
|
+
Uint8Array.from([subAccountId]),
|
|
55
55
|
],
|
|
56
56
|
programId
|
|
57
57
|
)[0];
|
|
@@ -61,7 +61,7 @@ export function getUserStatsAccountPublicKey(
|
|
|
61
61
|
programId: PublicKey,
|
|
62
62
|
authority: PublicKey
|
|
63
63
|
): PublicKey {
|
|
64
|
-
return
|
|
64
|
+
return PublicKey.findProgramAddressSync(
|
|
65
65
|
[
|
|
66
66
|
Buffer.from(anchor.utils.bytes.utf8.encode('user_stats')),
|
|
67
67
|
authority.toBuffer(),
|
|
@@ -70,14 +70,14 @@ export function getUserStatsAccountPublicKey(
|
|
|
70
70
|
)[0];
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
export async function
|
|
73
|
+
export async function getPerpMarketPublicKey(
|
|
74
74
|
programId: PublicKey,
|
|
75
75
|
marketIndex: number
|
|
76
76
|
): Promise<PublicKey> {
|
|
77
77
|
return (
|
|
78
|
-
await
|
|
78
|
+
await PublicKey.findProgramAddress(
|
|
79
79
|
[
|
|
80
|
-
Buffer.from(anchor.utils.bytes.utf8.encode('
|
|
80
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('perp_market')),
|
|
81
81
|
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
82
82
|
],
|
|
83
83
|
programId
|
|
@@ -90,7 +90,7 @@ export async function getSpotMarketPublicKey(
|
|
|
90
90
|
marketIndex: number
|
|
91
91
|
): Promise<PublicKey> {
|
|
92
92
|
return (
|
|
93
|
-
await
|
|
93
|
+
await PublicKey.findProgramAddress(
|
|
94
94
|
[
|
|
95
95
|
Buffer.from(anchor.utils.bytes.utf8.encode('spot_market')),
|
|
96
96
|
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
@@ -105,7 +105,7 @@ export async function getSpotMarketVaultPublicKey(
|
|
|
105
105
|
marketIndex: number
|
|
106
106
|
): Promise<PublicKey> {
|
|
107
107
|
return (
|
|
108
|
-
await
|
|
108
|
+
await PublicKey.findProgramAddress(
|
|
109
109
|
[
|
|
110
110
|
Buffer.from(anchor.utils.bytes.utf8.encode('spot_market_vault')),
|
|
111
111
|
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
@@ -120,7 +120,7 @@ export async function getInsuranceFundVaultPublicKey(
|
|
|
120
120
|
marketIndex: number
|
|
121
121
|
): Promise<PublicKey> {
|
|
122
122
|
return (
|
|
123
|
-
await
|
|
123
|
+
await PublicKey.findProgramAddress(
|
|
124
124
|
[
|
|
125
125
|
Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_vault')),
|
|
126
126
|
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
@@ -135,7 +135,7 @@ export function getInsuranceFundStakeAccountPublicKey(
|
|
|
135
135
|
authority: PublicKey,
|
|
136
136
|
marketIndex: number
|
|
137
137
|
): PublicKey {
|
|
138
|
-
return
|
|
138
|
+
return PublicKey.findProgramAddressSync(
|
|
139
139
|
[
|
|
140
140
|
Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_stake')),
|
|
141
141
|
authority.toBuffer(),
|
|
@@ -148,7 +148,7 @@ export function getInsuranceFundStakeAccountPublicKey(
|
|
|
148
148
|
export function getClearingHouseSignerPublicKey(
|
|
149
149
|
programId: PublicKey
|
|
150
150
|
): PublicKey {
|
|
151
|
-
return
|
|
151
|
+
return PublicKey.findProgramAddressSync(
|
|
152
152
|
[Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house_signer'))],
|
|
153
153
|
programId
|
|
154
154
|
)[0];
|
|
@@ -158,7 +158,7 @@ export function getSerumOpenOrdersPublicKey(
|
|
|
158
158
|
programId: PublicKey,
|
|
159
159
|
market: PublicKey
|
|
160
160
|
): PublicKey {
|
|
161
|
-
return
|
|
161
|
+
return PublicKey.findProgramAddressSync(
|
|
162
162
|
[
|
|
163
163
|
Buffer.from(anchor.utils.bytes.utf8.encode('serum_open_orders')),
|
|
164
164
|
market.toBuffer(),
|
|
@@ -182,7 +182,7 @@ export function getSerumFulfillmentConfigPublicKey(
|
|
|
182
182
|
programId: PublicKey,
|
|
183
183
|
market: PublicKey
|
|
184
184
|
): PublicKey {
|
|
185
|
-
return
|
|
185
|
+
return PublicKey.findProgramAddressSync(
|
|
186
186
|
[
|
|
187
187
|
Buffer.from(anchor.utils.bytes.utf8.encode('serum_fulfillment_config')),
|
|
188
188
|
market.toBuffer(),
|