@drift-labs/sdk 2.82.0-beta.15 → 2.82.0-beta.17

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.82.0-beta.15
1
+ 2.82.0-beta.17
@@ -0,0 +1,29 @@
1
+ /// <reference types="node" />
2
+ import { Commitment, Connection } from '@solana/web3.js';
3
+ import { EventEmitter } from 'events';
4
+ import StrictEventEmitter from 'strict-event-emitter-types/types/src';
5
+ type ClockSubscriberConfig = {
6
+ commitment: Commitment;
7
+ resubTimeoutMs?: number;
8
+ };
9
+ export interface ClockSubscriberEvent {
10
+ clockUpdate: (ts: number) => void;
11
+ }
12
+ export declare class ClockSubscriber {
13
+ private connection;
14
+ private latestSlot;
15
+ currentTs: number;
16
+ private subscriptionId;
17
+ commitment: Commitment;
18
+ eventEmitter: StrictEventEmitter<EventEmitter, ClockSubscriberEvent>;
19
+ private timeoutId?;
20
+ private resubTimeoutMs?;
21
+ private isUnsubscribing;
22
+ private receivingData;
23
+ constructor(connection: Connection, config?: ClockSubscriberConfig);
24
+ subscribe(): Promise<void>;
25
+ private setTimeout;
26
+ getUnixTs(): number;
27
+ unsubscribe(onResub?: boolean): Promise<void>;
28
+ }
29
+ export {};
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClockSubscriber = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ const events_1 = require("events");
6
+ const __1 = require("..");
7
+ class ClockSubscriber {
8
+ constructor(connection, config) {
9
+ this.connection = connection;
10
+ this.isUnsubscribing = false;
11
+ this.receivingData = false;
12
+ this.eventEmitter = new events_1.EventEmitter();
13
+ this.resubTimeoutMs = config === null || config === void 0 ? void 0 : config.resubTimeoutMs;
14
+ this.commitment = (config === null || config === void 0 ? void 0 : config.commitment) || 'confirmed';
15
+ if (this.resubTimeoutMs < 1000) {
16
+ console.log('resubTimeoutMs should be at least 1000ms to avoid spamming resub');
17
+ }
18
+ }
19
+ async subscribe() {
20
+ if (this.subscriptionId != null) {
21
+ return;
22
+ }
23
+ this.subscriptionId = this.connection.onAccountChange(web3_js_1.SYSVAR_CLOCK_PUBKEY, (acctInfo, context) => {
24
+ if (!this.latestSlot || this.latestSlot < context.slot) {
25
+ if (this.resubTimeoutMs && !this.isUnsubscribing) {
26
+ this.receivingData = true;
27
+ clearTimeout(this.timeoutId);
28
+ this.setTimeout();
29
+ }
30
+ this.latestSlot = context.slot;
31
+ this.currentTs = new __1.BN(acctInfo.data.subarray(32, 39), undefined, 'le').toNumber();
32
+ this.eventEmitter.emit('clockUpdate', this.currentTs);
33
+ }
34
+ }, this.commitment);
35
+ if (this.resubTimeoutMs) {
36
+ this.receivingData = true;
37
+ this.setTimeout();
38
+ }
39
+ }
40
+ setTimeout() {
41
+ this.timeoutId = setTimeout(async () => {
42
+ if (this.isUnsubscribing) {
43
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
44
+ return;
45
+ }
46
+ if (this.receivingData) {
47
+ console.log(`No new slot in ${this.resubTimeoutMs}ms, slot subscriber resubscribing`);
48
+ await this.unsubscribe(true);
49
+ this.receivingData = false;
50
+ await this.subscribe();
51
+ }
52
+ }, this.resubTimeoutMs);
53
+ }
54
+ getUnixTs() {
55
+ return this.currentTs;
56
+ }
57
+ async unsubscribe(onResub = false) {
58
+ if (!onResub) {
59
+ this.resubTimeoutMs = undefined;
60
+ }
61
+ this.isUnsubscribing = true;
62
+ clearTimeout(this.timeoutId);
63
+ this.timeoutId = undefined;
64
+ if (this.subscriptionId != null) {
65
+ await this.connection.removeAccountChangeListener(this.subscriptionId);
66
+ this.subscriptionId = undefined;
67
+ this.isUnsubscribing = false;
68
+ }
69
+ else {
70
+ this.isUnsubscribing = false;
71
+ }
72
+ }
73
+ }
74
+ exports.ClockSubscriber = ClockSubscriber;
package/lib/index.d.ts CHANGED
@@ -101,4 +101,5 @@ export * from './auctionSubscriber/types';
101
101
  export * from './memcmp';
102
102
  export * from './decode/user';
103
103
  export * from './blockhashSubscriber';
104
+ export * from './clock/clockSubscriber';
104
105
  export { BN, PublicKey, pyth };
package/lib/index.js CHANGED
@@ -124,3 +124,4 @@ __exportStar(require("./auctionSubscriber/types"), exports);
124
124
  __exportStar(require("./memcmp"), exports);
125
125
  __exportStar(require("./decode/user"), exports);
126
126
  __exportStar(require("./blockhashSubscriber"), exports);
127
+ __exportStar(require("./clock/clockSubscriber"), exports);
@@ -1,6 +1,6 @@
1
- import { PerpMarketAccount, PerpOperation, SpotMarketAccount, SpotOperation, StateAccount } from '../types';
1
+ import { PerpMarketAccount, PerpOperation, SpotMarketAccount, SpotOperation, StateAccount, InsuranceFundOperation } from '../types';
2
2
  export declare function exchangePaused(state: StateAccount): boolean;
3
3
  export declare function fillPaused(state: StateAccount, market: PerpMarketAccount | SpotMarketAccount): boolean;
4
4
  export declare function ammPaused(state: StateAccount, market: PerpMarketAccount | SpotMarketAccount): boolean;
5
- export declare function isOperationPaused(pausedOperations: number, operation: PerpOperation | SpotOperation): boolean;
5
+ export declare function isOperationPaused(pausedOperations: number, operation: PerpOperation | SpotOperation | InsuranceFundOperation): boolean;
6
6
  export declare function isAmmDrawdownPause(market: PerpMarketAccount): boolean;
package/lib/types.d.ts CHANGED
@@ -55,6 +55,12 @@ export declare enum SpotOperation {
55
55
  WITHDRAW = 4,
56
56
  LIQUIDATION = 8
57
57
  }
58
+ export declare enum InsuranceFundOperation {
59
+ INIT = 1,
60
+ ADD = 2,
61
+ REQUEST_REMOVE = 4,
62
+ REMOVE = 8
63
+ }
58
64
  export declare enum UserStatus {
59
65
  BEING_LIQUIDATED = 1,
60
66
  BANKRUPT = 2,
@@ -780,6 +786,7 @@ export type SpotMarketAccount = {
780
786
  flashLoanInitialTokenAmount: BN;
781
787
  ordersEnabled: boolean;
782
788
  pausedOperations: number;
789
+ ifPausedOperations: number;
783
790
  };
784
791
  export type PoolBalance = {
785
792
  scaledBalance: BN;
package/lib/types.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SwapReduceOnly = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.UserStatus = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.ExchangeStatus = void 0;
3
+ exports.SwapReduceOnly = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.UserStatus = exports.InsuranceFundOperation = exports.SpotOperation = exports.PerpOperation = exports.MarketStatus = exports.ExchangeStatus = void 0;
4
4
  const _1 = require(".");
5
5
  // # Utility Types / Enums / Constants
6
6
  var ExchangeStatus;
@@ -43,6 +43,13 @@ var SpotOperation;
43
43
  SpotOperation[SpotOperation["WITHDRAW"] = 4] = "WITHDRAW";
44
44
  SpotOperation[SpotOperation["LIQUIDATION"] = 8] = "LIQUIDATION";
45
45
  })(SpotOperation = exports.SpotOperation || (exports.SpotOperation = {}));
46
+ var InsuranceFundOperation;
47
+ (function (InsuranceFundOperation) {
48
+ InsuranceFundOperation[InsuranceFundOperation["INIT"] = 1] = "INIT";
49
+ InsuranceFundOperation[InsuranceFundOperation["ADD"] = 2] = "ADD";
50
+ InsuranceFundOperation[InsuranceFundOperation["REQUEST_REMOVE"] = 4] = "REQUEST_REMOVE";
51
+ InsuranceFundOperation[InsuranceFundOperation["REMOVE"] = 8] = "REMOVE";
52
+ })(InsuranceFundOperation = exports.InsuranceFundOperation || (exports.InsuranceFundOperation = {}));
46
53
  var UserStatus;
47
54
  (function (UserStatus) {
48
55
  UserStatus[UserStatus["BEING_LIQUIDATED"] = 1] = "BEING_LIQUIDATED";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.82.0-beta.15",
3
+ "version": "2.82.0-beta.17",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -0,0 +1,113 @@
1
+ import { Commitment, Connection, SYSVAR_CLOCK_PUBKEY } from '@solana/web3.js';
2
+ import { EventEmitter } from 'events';
3
+ import StrictEventEmitter from 'strict-event-emitter-types/types/src';
4
+ import { BN } from '..';
5
+
6
+ // eslint-disable-next-line @typescript-eslint/ban-types
7
+ type ClockSubscriberConfig = {
8
+ commitment: Commitment;
9
+ resubTimeoutMs?: number;
10
+ };
11
+
12
+ export interface ClockSubscriberEvent {
13
+ clockUpdate: (ts: number) => void;
14
+ }
15
+
16
+ export class ClockSubscriber {
17
+ private latestSlot: number;
18
+ currentTs: number;
19
+ private subscriptionId: number;
20
+ commitment: Commitment;
21
+ eventEmitter: StrictEventEmitter<EventEmitter, ClockSubscriberEvent>;
22
+
23
+ // Reconnection
24
+ private timeoutId?: NodeJS.Timeout;
25
+ private resubTimeoutMs?: number;
26
+ private isUnsubscribing = false;
27
+ private receivingData = false;
28
+
29
+ public constructor(
30
+ private connection: Connection,
31
+ config?: ClockSubscriberConfig
32
+ ) {
33
+ this.eventEmitter = new EventEmitter();
34
+ this.resubTimeoutMs = config?.resubTimeoutMs;
35
+ this.commitment = config?.commitment || 'confirmed';
36
+ if (this.resubTimeoutMs < 1000) {
37
+ console.log(
38
+ 'resubTimeoutMs should be at least 1000ms to avoid spamming resub'
39
+ );
40
+ }
41
+ }
42
+
43
+ public async subscribe(): Promise<void> {
44
+ if (this.subscriptionId != null) {
45
+ return;
46
+ }
47
+
48
+ this.subscriptionId = this.connection.onAccountChange(
49
+ SYSVAR_CLOCK_PUBKEY,
50
+ (acctInfo, context) => {
51
+ if (!this.latestSlot || this.latestSlot < context.slot) {
52
+ if (this.resubTimeoutMs && !this.isUnsubscribing) {
53
+ this.receivingData = true;
54
+ clearTimeout(this.timeoutId);
55
+ this.setTimeout();
56
+ }
57
+ this.latestSlot = context.slot;
58
+ this.currentTs = new BN(
59
+ acctInfo.data.subarray(32, 39),
60
+ undefined,
61
+ 'le'
62
+ ).toNumber();
63
+ this.eventEmitter.emit('clockUpdate', this.currentTs);
64
+ }
65
+ },
66
+ this.commitment
67
+ );
68
+
69
+ if (this.resubTimeoutMs) {
70
+ this.receivingData = true;
71
+ this.setTimeout();
72
+ }
73
+ }
74
+
75
+ private setTimeout(): void {
76
+ this.timeoutId = setTimeout(async () => {
77
+ if (this.isUnsubscribing) {
78
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
79
+ return;
80
+ }
81
+
82
+ if (this.receivingData) {
83
+ console.log(
84
+ `No new slot in ${this.resubTimeoutMs}ms, slot subscriber resubscribing`
85
+ );
86
+ await this.unsubscribe(true);
87
+ this.receivingData = false;
88
+ await this.subscribe();
89
+ }
90
+ }, this.resubTimeoutMs);
91
+ }
92
+
93
+ public getUnixTs(): number {
94
+ return this.currentTs;
95
+ }
96
+
97
+ public async unsubscribe(onResub = false): Promise<void> {
98
+ if (!onResub) {
99
+ this.resubTimeoutMs = undefined;
100
+ }
101
+ this.isUnsubscribing = true;
102
+ clearTimeout(this.timeoutId);
103
+ this.timeoutId = undefined;
104
+
105
+ if (this.subscriptionId != null) {
106
+ await this.connection.removeAccountChangeListener(this.subscriptionId);
107
+ this.subscriptionId = undefined;
108
+ this.isUnsubscribing = false;
109
+ } else {
110
+ this.isUnsubscribing = false;
111
+ }
112
+ }
113
+ }
package/src/index.ts CHANGED
@@ -102,5 +102,6 @@ export * from './auctionSubscriber/types';
102
102
  export * from './memcmp';
103
103
  export * from './decode/user';
104
104
  export * from './blockhashSubscriber';
105
+ export * from './clock/clockSubscriber';
105
106
 
106
107
  export { BN, PublicKey, pyth };
@@ -11,6 +11,7 @@ import {
11
11
  SpotOperation,
12
12
  StateAccount,
13
13
  isVariant,
14
+ InsuranceFundOperation,
14
15
  } from '../types';
15
16
  import { BN } from '@coral-xyz/anchor';
16
17
 
@@ -65,7 +66,7 @@ export function ammPaused(
65
66
 
66
67
  export function isOperationPaused(
67
68
  pausedOperations: number,
68
- operation: PerpOperation | SpotOperation
69
+ operation: PerpOperation | SpotOperation | InsuranceFundOperation
69
70
  ): boolean {
70
71
  return (pausedOperations & operation) > 0;
71
72
  }
package/src/types.ts CHANGED
@@ -43,6 +43,13 @@ export enum SpotOperation {
43
43
  LIQUIDATION = 8,
44
44
  }
45
45
 
46
+ export enum InsuranceFundOperation {
47
+ INIT = 1,
48
+ ADD = 2,
49
+ REQUEST_REMOVE = 4,
50
+ REMOVE = 8,
51
+ }
52
+
46
53
  export enum UserStatus {
47
54
  BEING_LIQUIDATED = 1,
48
55
  BANKRUPT = 2,
@@ -708,6 +715,8 @@ export type SpotMarketAccount = {
708
715
  ordersEnabled: boolean;
709
716
 
710
717
  pausedOperations: number;
718
+
719
+ ifPausedOperations: number;
711
720
  };
712
721
 
713
722
  export type PoolBalance = {