@drift-labs/sdk 2.96.0-beta.2 → 2.96.0-beta.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.
Files changed (110) hide show
  1. package/README.md +1 -0
  2. package/VERSION +1 -1
  3. package/bun.lockb +0 -0
  4. package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +5 -3
  5. package/lib/accounts/pollingDriftClientAccountSubscriber.js +24 -1
  6. package/lib/accounts/types.d.ts +5 -8
  7. package/lib/accounts/types.js +7 -1
  8. package/lib/accounts/utils.d.ts +7 -0
  9. package/lib/accounts/utils.js +33 -1
  10. package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
  11. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +8 -7
  12. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +24 -1
  13. package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
  14. package/lib/config.d.ts +5 -1
  15. package/lib/config.js +9 -1
  16. package/lib/constants/perpMarkets.js +21 -0
  17. package/lib/constants/spotMarkets.js +12 -1
  18. package/lib/driftClient.d.ts +44 -9
  19. package/lib/driftClient.js +181 -61
  20. package/lib/driftClientConfig.d.ts +2 -6
  21. package/lib/events/eventSubscriber.js +9 -8
  22. package/lib/events/types.js +1 -5
  23. package/lib/idl/drift.json +169 -1
  24. package/lib/index.d.ts +1 -0
  25. package/lib/index.js +1 -0
  26. package/lib/math/margin.d.ts +16 -1
  27. package/lib/math/margin.js +67 -1
  28. package/lib/orderParams.js +8 -8
  29. package/lib/orderSubscriber/OrderSubscriber.d.ts +1 -2
  30. package/lib/orderSubscriber/OrderSubscriber.js +4 -19
  31. package/lib/orderSubscriber/types.d.ts +0 -9
  32. package/lib/tokenFaucet.js +2 -1
  33. package/lib/tx/baseTxSender.js +2 -2
  34. package/lib/tx/fastSingleTxSender.js +2 -2
  35. package/lib/tx/forwardOnlyTxSender.js +2 -2
  36. package/lib/tx/retryTxSender.js +2 -2
  37. package/lib/tx/txHandler.js +10 -7
  38. package/lib/tx/whileValidTxSender.d.ts +2 -4
  39. package/lib/tx/whileValidTxSender.js +16 -17
  40. package/lib/types.d.ts +21 -1
  41. package/lib/types.js +6 -1
  42. package/lib/user.d.ts +4 -1
  43. package/lib/user.js +13 -13
  44. package/lib/userConfig.d.ts +1 -6
  45. package/lib/userMap/userMap.js +0 -14
  46. package/lib/userMap/userMapConfig.d.ts +0 -7
  47. package/lib/userStatsConfig.d.ts +0 -6
  48. package/lib/util/TransactionConfirmationManager.d.ts +14 -0
  49. package/lib/util/TransactionConfirmationManager.js +96 -0
  50. package/package.json +4 -5
  51. package/src/accounts/pollingDriftClientAccountSubscriber.ts +41 -5
  52. package/src/accounts/types.ts +6 -9
  53. package/src/accounts/utils.ts +42 -0
  54. package/src/accounts/webSocketAccountSubscriber.ts +1 -1
  55. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +43 -8
  56. package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
  57. package/src/config.ts +15 -1
  58. package/src/constants/perpMarkets.ts +22 -0
  59. package/src/constants/spotMarkets.ts +14 -1
  60. package/src/driftClient.ts +423 -91
  61. package/src/driftClientConfig.ts +2 -7
  62. package/src/events/eventSubscriber.ts +18 -11
  63. package/src/events/types.ts +1 -5
  64. package/src/idl/drift.json +169 -1
  65. package/src/index.ts +1 -0
  66. package/src/math/margin.ts +137 -1
  67. package/src/orderParams.ts +20 -12
  68. package/src/orderSubscriber/OrderSubscriber.ts +1 -15
  69. package/src/orderSubscriber/types.ts +0 -10
  70. package/src/tokenFaucet.ts +2 -2
  71. package/src/tx/baseTxSender.ts +2 -2
  72. package/src/tx/fastSingleTxSender.ts +2 -2
  73. package/src/tx/forwardOnlyTxSender.ts +2 -2
  74. package/src/tx/retryTxSender.ts +2 -2
  75. package/src/tx/txHandler.ts +8 -2
  76. package/src/tx/whileValidTxSender.ts +23 -26
  77. package/src/types.ts +30 -1
  78. package/src/user.ts +35 -13
  79. package/src/userConfig.ts +1 -7
  80. package/src/userMap/userMap.ts +1 -17
  81. package/src/userMap/userMapConfig.ts +0 -8
  82. package/src/userStatsConfig.ts +0 -7
  83. package/src/util/TransactionConfirmationManager.ts +155 -0
  84. package/tests/ci/idl.ts +12 -3
  85. package/tests/ci/verifyConstants.ts +13 -0
  86. package/tests/tx/TransactionConfirmationManager.test.ts +286 -0
  87. package/lib/accounts/grpcAccountSubscriber.d.ts +0 -16
  88. package/lib/accounts/grpcAccountSubscriber.js +0 -155
  89. package/lib/accounts/grpcDriftClientAccountSubscriber.d.ts +0 -13
  90. package/lib/accounts/grpcDriftClientAccountSubscriber.js +0 -96
  91. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +0 -10
  92. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.js +0 -30
  93. package/lib/accounts/grpcProgramAccountSubscriber.d.ts +0 -19
  94. package/lib/accounts/grpcProgramAccountSubscriber.js +0 -161
  95. package/lib/accounts/grpcUserAccountSubscriber.d.ts +0 -10
  96. package/lib/accounts/grpcUserAccountSubscriber.js +0 -28
  97. package/lib/accounts/grpcUserStatsAccountSubscriber.d.ts +0 -10
  98. package/lib/accounts/grpcUserStatsAccountSubscriber.js +0 -28
  99. package/lib/orderSubscriber/grpcSubscription.d.ts +0 -25
  100. package/lib/orderSubscriber/grpcSubscription.js +0 -68
  101. package/lib/userMap/grpcSubscription.d.ts +0 -26
  102. package/lib/userMap/grpcSubscription.js +0 -42
  103. package/src/accounts/grpcAccountSubscriber.ts +0 -158
  104. package/src/accounts/grpcDriftClientAccountSubscriber.ts +0 -196
  105. package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +0 -62
  106. package/src/accounts/grpcProgramAccountSubscriber.ts +0 -181
  107. package/src/accounts/grpcUserAccountSubscriber.ts +0 -48
  108. package/src/accounts/grpcUserStatsAccountSubscriber.ts +0 -51
  109. package/src/orderSubscriber/grpcSubscription.ts +0 -126
  110. package/src/userMap/grpcSubscription.ts +0 -83
@@ -1,161 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.grpcProgramAccountSubscriber = void 0;
27
- const web3_js_1 = require("@solana/web3.js");
28
- const Buffer = __importStar(require("buffer"));
29
- const webSocketProgramAccountSubscriber_1 = require("./webSocketProgramAccountSubscriber");
30
- const yellowstone_grpc_1 = __importStar(require("@triton-one/yellowstone-grpc"));
31
- class grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber {
32
- constructor(grpcConfigs, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
33
- filters: [],
34
- }, resubOpts) {
35
- var _a, _b;
36
- super(subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
37
- this.client = new yellowstone_grpc_1.default(grpcConfigs.endpoint, grpcConfigs.token, (_a = grpcConfigs.channelOptions) !== null && _a !== void 0 ? _a : {});
38
- this.commitmentLevel =
39
- (_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : yellowstone_grpc_1.CommitmentLevel.CONFIRMED;
40
- }
41
- async subscribe(onChange) {
42
- if (this.listenerId != null || this.isUnsubscribing) {
43
- return;
44
- }
45
- this.onChange = onChange;
46
- // Subscribe with grpc
47
- this.stream = await this.client.subscribe();
48
- const request = {
49
- slots: {
50
- slots: {},
51
- },
52
- accounts: {
53
- drift: {
54
- account: [],
55
- owner: [this.program.programId.toBase58()],
56
- // @ts-ignore
57
- filters: this.options.filters,
58
- },
59
- },
60
- transactions: {},
61
- blocks: {},
62
- blocksMeta: {},
63
- accountsDataSlice: [],
64
- commitment: this.commitmentLevel,
65
- entry: {},
66
- transactionsStatus: {},
67
- };
68
- this.stream.on('data', (chunk) => {
69
- var _a;
70
- if (!chunk.account) {
71
- return;
72
- }
73
- const slot = Number(chunk.account.slot);
74
- const accountInfo = {
75
- owner: new web3_js_1.PublicKey(chunk.account.account.owner),
76
- lamports: Number(chunk.account.account.lamports),
77
- data: Buffer.Buffer.from(chunk.account.account.data),
78
- executable: chunk.account.account.executable,
79
- rentEpoch: Number(chunk.account.account.rentEpoch),
80
- };
81
- if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
82
- this.receivingData = true;
83
- clearTimeout(this.timeoutId);
84
- this.handleRpcResponse({
85
- slot,
86
- }, {
87
- accountId: new web3_js_1.PublicKey(chunk.account.account.pubkey),
88
- accountInfo,
89
- });
90
- this.setTimeout();
91
- }
92
- else {
93
- this.handleRpcResponse({
94
- slot,
95
- }, {
96
- accountId: new web3_js_1.PublicKey(chunk.account.account.pubkey),
97
- accountInfo,
98
- });
99
- }
100
- });
101
- return new Promise((resolve, reject) => {
102
- this.stream.write(request, (err) => {
103
- var _a;
104
- if (err === null || err === undefined) {
105
- this.listenerId = 1;
106
- if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
107
- this.receivingData = true;
108
- this.setTimeout();
109
- }
110
- resolve();
111
- }
112
- else {
113
- reject(err);
114
- }
115
- });
116
- }).catch((reason) => {
117
- console.error(reason);
118
- throw reason;
119
- });
120
- }
121
- async unsubscribe(onResub = false) {
122
- if (!onResub && this.resubOpts) {
123
- this.resubOpts.resubTimeoutMs = undefined;
124
- }
125
- this.isUnsubscribing = true;
126
- clearTimeout(this.timeoutId);
127
- this.timeoutId = undefined;
128
- if (this.listenerId != null) {
129
- const promise = new Promise((resolve, reject) => {
130
- const request = {
131
- slots: {},
132
- accounts: {},
133
- transactions: {},
134
- blocks: {},
135
- blocksMeta: {},
136
- accountsDataSlice: [],
137
- entry: {},
138
- transactionsStatus: {},
139
- };
140
- this.stream.write(request, (err) => {
141
- if (err === null || err === undefined) {
142
- this.listenerId = undefined;
143
- this.isUnsubscribing = false;
144
- resolve();
145
- }
146
- else {
147
- reject(err);
148
- }
149
- });
150
- }).catch((reason) => {
151
- console.error(reason);
152
- throw reason;
153
- });
154
- return promise;
155
- }
156
- else {
157
- this.isUnsubscribing = false;
158
- }
159
- }
160
- }
161
- exports.grpcProgramAccountSubscriber = grpcProgramAccountSubscriber;
@@ -1,10 +0,0 @@
1
- import { ResubOpts, GrpcConfigs } from './types';
2
- import { Program } from '@coral-xyz/anchor';
3
- import { PublicKey } from '@solana/web3.js';
4
- import { UserAccount } from '../types';
5
- import { WebSocketUserAccountSubscriber } from './webSocketUserAccountSubscriber';
6
- export declare class grpcUserAccountSubscriber extends WebSocketUserAccountSubscriber {
7
- private grpcConfigs;
8
- constructor(grpcConfigs: GrpcConfigs, program: Program, userAccountPublicKey: PublicKey, resubOpts?: ResubOpts);
9
- subscribe(userAccount?: UserAccount): Promise<boolean>;
10
- }
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.grpcUserAccountSubscriber = void 0;
4
- const webSocketUserAccountSubscriber_1 = require("./webSocketUserAccountSubscriber");
5
- const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
6
- class grpcUserAccountSubscriber extends webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber {
7
- constructor(grpcConfigs, program, userAccountPublicKey, resubOpts) {
8
- super(program, userAccountPublicKey, resubOpts);
9
- this.grpcConfigs = grpcConfigs;
10
- }
11
- async subscribe(userAccount) {
12
- if (this.isSubscribed) {
13
- return true;
14
- }
15
- this.userDataAccountSubscriber = new grpcAccountSubscriber_1.grpcAccountSubscriber(this.grpcConfigs, 'user', this.program, this.userAccountPublicKey, undefined, this.resubOpts);
16
- if (userAccount) {
17
- this.userDataAccountSubscriber.setData(userAccount);
18
- }
19
- await this.userDataAccountSubscriber.subscribe((data) => {
20
- this.eventEmitter.emit('userAccountUpdate', data);
21
- this.eventEmitter.emit('update');
22
- });
23
- this.eventEmitter.emit('update');
24
- this.isSubscribed = true;
25
- return true;
26
- }
27
- }
28
- exports.grpcUserAccountSubscriber = grpcUserAccountSubscriber;
@@ -1,10 +0,0 @@
1
- import { ResubOpts, GrpcConfigs } from './types';
2
- import { Program } from '@coral-xyz/anchor';
3
- import { Commitment, PublicKey } from '@solana/web3.js';
4
- import { UserStatsAccount } from '../types';
5
- import { WebSocketUserStatsAccountSubscriber } from './webSocketUserStatsAccountSubsriber';
6
- export declare class grpcUserStatsAccountSubscriber extends WebSocketUserStatsAccountSubscriber {
7
- private grpcConfigs;
8
- constructor(grpcConfigs: GrpcConfigs, program: Program, userStatsAccountPublicKey: PublicKey, resubOpts?: ResubOpts, commitment?: Commitment);
9
- subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
10
- }
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.grpcUserStatsAccountSubscriber = void 0;
4
- const webSocketUserStatsAccountSubsriber_1 = require("./webSocketUserStatsAccountSubsriber");
5
- const grpcAccountSubscriber_1 = require("./grpcAccountSubscriber");
6
- class grpcUserStatsAccountSubscriber extends webSocketUserStatsAccountSubsriber_1.WebSocketUserStatsAccountSubscriber {
7
- constructor(grpcConfigs, program, userStatsAccountPublicKey, resubOpts, commitment) {
8
- super(program, userStatsAccountPublicKey, resubOpts, commitment);
9
- this.grpcConfigs = grpcConfigs;
10
- }
11
- async subscribe(userStatsAccount) {
12
- if (this.isSubscribed) {
13
- return true;
14
- }
15
- this.userStatsAccountSubscriber = new grpcAccountSubscriber_1.grpcAccountSubscriber(this.grpcConfigs, 'userStats', this.program, this.userStatsAccountPublicKey, undefined, this.resubOpts);
16
- if (userStatsAccount) {
17
- this.userStatsAccountSubscriber.setData(userStatsAccount);
18
- }
19
- await this.userStatsAccountSubscriber.subscribe((data) => {
20
- this.eventEmitter.emit('userStatsAccountUpdate', data);
21
- this.eventEmitter.emit('update');
22
- });
23
- this.eventEmitter.emit('update');
24
- this.isSubscribed = true;
25
- return true;
26
- }
27
- }
28
- exports.grpcUserStatsAccountSubscriber = grpcUserStatsAccountSubscriber;
@@ -1,25 +0,0 @@
1
- import { Commitment } from '@solana/web3.js';
2
- import { OrderSubscriber } from './OrderSubscriber';
3
- import { GrpcConfigs, ResubOpts } from '../accounts/types';
4
- export declare class grpcSubscription {
5
- private orderSubscriber;
6
- private commitment;
7
- private skipInitialLoad;
8
- private resubOpts?;
9
- private resyncIntervalMs?;
10
- private subscriber?;
11
- private resyncTimeoutId?;
12
- private decoded?;
13
- private grpcConfigs;
14
- constructor({ grpcConfigs, orderSubscriber, commitment, skipInitialLoad, resubOpts, resyncIntervalMs, decoded, }: {
15
- grpcConfigs: GrpcConfigs;
16
- orderSubscriber: OrderSubscriber;
17
- commitment: Commitment;
18
- skipInitialLoad?: boolean;
19
- resubOpts?: ResubOpts;
20
- resyncIntervalMs?: number;
21
- decoded?: boolean;
22
- });
23
- subscribe(): Promise<void>;
24
- unsubscribe(): Promise<void>;
25
- }
@@ -1,68 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.grpcSubscription = void 0;
4
- const grpcProgramAccountSubscriber_1 = require("../accounts/grpcProgramAccountSubscriber");
5
- const memcmp_1 = require("../memcmp");
6
- class grpcSubscription {
7
- constructor({ grpcConfigs, orderSubscriber, commitment, skipInitialLoad = false, resubOpts, resyncIntervalMs, decoded = true, }) {
8
- this.orderSubscriber = orderSubscriber;
9
- this.commitment = commitment;
10
- this.skipInitialLoad = skipInitialLoad;
11
- this.resubOpts = resubOpts;
12
- this.resyncIntervalMs = resyncIntervalMs;
13
- this.decoded = decoded;
14
- this.grpcConfigs = grpcConfigs;
15
- }
16
- async subscribe() {
17
- if (this.subscriber) {
18
- return;
19
- }
20
- this.subscriber = new grpcProgramAccountSubscriber_1.grpcProgramAccountSubscriber(this.grpcConfigs, 'OrderSubscriber', 'User', this.orderSubscriber.driftClient.program, this.orderSubscriber.decodeFn, {
21
- filters: [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getNonIdleUserFilter)()],
22
- commitment: this.commitment,
23
- }, this.resubOpts);
24
- await this.subscriber.subscribe((accountId, account, context, buffer) => {
25
- var _a;
26
- const userKey = accountId.toBase58();
27
- if ((_a = this.decoded) !== null && _a !== void 0 ? _a : true) {
28
- this.orderSubscriber.tryUpdateUserAccount(userKey, 'decoded', account, context.slot);
29
- }
30
- else {
31
- this.orderSubscriber.tryUpdateUserAccount(userKey, 'buffer', buffer, context.slot);
32
- }
33
- });
34
- if (!this.skipInitialLoad) {
35
- await this.orderSubscriber.fetch();
36
- }
37
- if (this.resyncIntervalMs) {
38
- const recursiveResync = () => {
39
- this.resyncTimeoutId = setTimeout(() => {
40
- this.orderSubscriber
41
- .fetch()
42
- .catch((e) => {
43
- console.error('Failed to resync in OrderSubscriber');
44
- console.log(e);
45
- })
46
- .finally(() => {
47
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
48
- if (!this.resyncTimeoutId)
49
- return;
50
- recursiveResync();
51
- });
52
- }, this.resyncIntervalMs);
53
- };
54
- recursiveResync();
55
- }
56
- }
57
- async unsubscribe() {
58
- if (!this.subscriber)
59
- return;
60
- await this.subscriber.unsubscribe();
61
- this.subscriber = undefined;
62
- if (this.resyncTimeoutId !== undefined) {
63
- clearTimeout(this.resyncTimeoutId);
64
- this.resyncTimeoutId = undefined;
65
- }
66
- }
67
- }
68
- exports.grpcSubscription = grpcSubscription;
@@ -1,26 +0,0 @@
1
- /// <reference types="node" />
2
- import { UserMap } from './userMap';
3
- import { UserAccount } from '../types';
4
- import { Commitment } from '@solana/web3.js';
5
- import { GrpcConfigs, ResubOpts } from '../accounts/types';
6
- export declare class grpcSubscription {
7
- private configs;
8
- private userMap;
9
- private commitment;
10
- private skipInitialLoad;
11
- private resubOpts?;
12
- private includeIdle?;
13
- private decodeFn;
14
- private subscriber;
15
- constructor({ configs, userMap, commitment, skipInitialLoad, resubOpts, includeIdle, decodeFn, }: {
16
- configs: GrpcConfigs;
17
- userMap: UserMap;
18
- commitment: Commitment;
19
- skipInitialLoad?: boolean;
20
- resubOpts?: ResubOpts;
21
- includeIdle?: boolean;
22
- decodeFn: (name: string, data: Buffer) => UserAccount;
23
- });
24
- subscribe(): Promise<void>;
25
- unsubscribe(): Promise<void>;
26
- }
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.grpcSubscription = void 0;
4
- const memcmp_1 = require("../memcmp");
5
- const grpcProgramAccountSubscriber_1 = require("../accounts/grpcProgramAccountSubscriber");
6
- class grpcSubscription {
7
- constructor({ configs, userMap, commitment, skipInitialLoad = false, resubOpts, includeIdle = false, decodeFn, }) {
8
- this.userMap = userMap;
9
- this.commitment = commitment;
10
- this.skipInitialLoad = skipInitialLoad;
11
- this.resubOpts = resubOpts;
12
- this.includeIdle = includeIdle || false;
13
- this.decodeFn = decodeFn;
14
- this.configs = configs;
15
- }
16
- async subscribe() {
17
- if (!this.subscriber) {
18
- const filters = [(0, memcmp_1.getUserFilter)()];
19
- if (!this.includeIdle) {
20
- filters.push((0, memcmp_1.getNonIdleUserFilter)());
21
- }
22
- this.subscriber = new grpcProgramAccountSubscriber_1.grpcProgramAccountSubscriber(this.configs, 'UserMap', 'User', this.userMap.driftClient.program, this.decodeFn, {
23
- filters,
24
- commitment: this.commitment,
25
- }, this.resubOpts);
26
- }
27
- await this.subscriber.subscribe((accountId, account, context) => {
28
- const userKey = accountId.toBase58();
29
- this.userMap.updateUserAccount(userKey, account, context.slot);
30
- });
31
- if (!this.skipInitialLoad) {
32
- await this.userMap.sync();
33
- }
34
- }
35
- async unsubscribe() {
36
- if (!this.subscriber)
37
- return;
38
- await this.subscriber.unsubscribe();
39
- this.subscriber = undefined;
40
- }
41
- }
42
- exports.grpcSubscription = grpcSubscription;
@@ -1,158 +0,0 @@
1
- import { ResubOpts, GrpcConfigs } from './types';
2
- import { Program } from '@coral-xyz/anchor';
3
- import { PublicKey } from '@solana/web3.js';
4
- import * as Buffer from 'buffer';
5
- import { ClientDuplexStream } from '@grpc/grpc-js';
6
- import Client, {
7
- CommitmentLevel,
8
- SubscribeRequest,
9
- SubscribeUpdate,
10
- } from '@triton-one/yellowstone-grpc';
11
- import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
12
-
13
- export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
14
- client: Client;
15
- stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
16
- commitmentLevel: CommitmentLevel;
17
- listenerId?: number;
18
-
19
- public constructor(
20
- grpcConfigs: GrpcConfigs,
21
- accountName: string,
22
- program: Program,
23
- accountPublicKey: PublicKey,
24
- decodeBuffer?: (buffer: Buffer) => T,
25
- resubOpts?: ResubOpts
26
- ) {
27
- super(accountName, program, accountPublicKey, decodeBuffer, resubOpts);
28
- this.client = new Client(
29
- grpcConfigs.endpoint,
30
- grpcConfigs.token,
31
- grpcConfigs.channelOptions ?? {}
32
- );
33
- this.commitmentLevel =
34
- grpcConfigs.commitmentLevel ?? CommitmentLevel.CONFIRMED;
35
- }
36
-
37
- override async subscribe(onChange: (data: T) => void): Promise<void> {
38
- if (this.listenerId != null || this.isUnsubscribing) {
39
- return;
40
- }
41
-
42
- this.onChange = onChange;
43
- if (!this.dataAndSlot) {
44
- await this.fetch();
45
- }
46
-
47
- // Subscribe with grpc
48
- this.stream = await this.client.subscribe();
49
- const request: SubscribeRequest = {
50
- slots: {
51
- slots: {},
52
- },
53
- accounts: {
54
- account: {
55
- account: [this.accountPublicKey.toString()],
56
- owner: [],
57
- filters: [],
58
- },
59
- },
60
- transactions: {},
61
- blocks: {},
62
- blocksMeta: {},
63
- accountsDataSlice: [],
64
- commitment: this.commitmentLevel,
65
- entry: {},
66
- transactionsStatus: {},
67
- };
68
- this.stream.on('data', (chunk: SubscribeUpdate) => {
69
- if (!chunk.account) {
70
- return;
71
- }
72
- const slot = Number(chunk.account.slot);
73
- const accountInfo = {
74
- owner: new PublicKey(chunk.account.account.owner),
75
- lamports: Number(chunk.account.account.lamports),
76
- data: Buffer.Buffer.from(chunk.account.account.data),
77
- executable: chunk.account.account.executable,
78
- rentEpoch: Number(chunk.account.account.rentEpoch),
79
- };
80
-
81
- if (this.resubOpts?.resubTimeoutMs) {
82
- this.receivingData = true;
83
- clearTimeout(this.timeoutId);
84
- this.handleRpcResponse(
85
- {
86
- slot,
87
- },
88
- accountInfo
89
- );
90
- this.setTimeout();
91
- } else {
92
- this.handleRpcResponse(
93
- {
94
- slot,
95
- },
96
- accountInfo
97
- );
98
- }
99
- });
100
-
101
- return new Promise<void>((resolve, reject) => {
102
- this.stream.write(request, (err) => {
103
- if (err === null || err === undefined) {
104
- this.listenerId = 1;
105
- if (this.resubOpts?.resubTimeoutMs) {
106
- this.receivingData = true;
107
- this.setTimeout();
108
- }
109
- resolve();
110
- } else {
111
- reject(err);
112
- }
113
- });
114
- }).catch((reason) => {
115
- console.error(reason);
116
- throw reason;
117
- });
118
- }
119
-
120
- override async unsubscribe(onResub = false): Promise<void> {
121
- if (!onResub && this.resubOpts) {
122
- this.resubOpts.resubTimeoutMs = undefined;
123
- }
124
- this.isUnsubscribing = true;
125
- clearTimeout(this.timeoutId);
126
- this.timeoutId = undefined;
127
-
128
- if (this.listenerId != null) {
129
- const promise = new Promise<void>((resolve, reject) => {
130
- const request: SubscribeRequest = {
131
- slots: {},
132
- accounts: {},
133
- transactions: {},
134
- blocks: {},
135
- blocksMeta: {},
136
- accountsDataSlice: [],
137
- entry: {},
138
- transactionsStatus: {},
139
- };
140
- this.stream.write(request, (err) => {
141
- if (err === null || err === undefined) {
142
- this.listenerId = undefined;
143
- this.isUnsubscribing = false;
144
- resolve();
145
- } else {
146
- reject(err);
147
- }
148
- });
149
- }).catch((reason) => {
150
- console.error(reason);
151
- throw reason;
152
- });
153
- return promise;
154
- } else {
155
- this.isUnsubscribing = false;
156
- }
157
- }
158
- }