@drift-labs/sdk 2.28.0-beta.2 → 2.28.0-beta.4

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/dlob_read.ts ADDED
@@ -0,0 +1,155 @@
1
+ import { Connection, Keypair, PublicKey } from '@solana/web3.js';
2
+ import {
3
+ BASE_PRECISION,
4
+ BulkAccountLoader,
5
+ configs,
6
+ convertToNumber,
7
+ DLOBSubscriber,
8
+ DriftClient,
9
+ getMarketsAndOraclesForSubscription,
10
+ MarketType,
11
+ PRICE_PRECISION,
12
+ SlotSubscriber,
13
+ UserMap,
14
+ Wallet,
15
+ } from './src/index';
16
+ import {
17
+ DLOBApiClient,
18
+ } from './src/dlob/DLOBApiClient';
19
+
20
+ async function main() {
21
+
22
+ const driftConfig = configs['mainnet-beta'];
23
+ const connection = new Connection('https://api.mainnet-beta.solana.com');
24
+
25
+ const accountLoader = new BulkAccountLoader(
26
+ connection,
27
+ 'confirmed',
28
+ 10000
29
+ );
30
+
31
+ const { perpMarketIndexes, spotMarketIndexes, oracleInfos } = getMarketsAndOraclesForSubscription('mainnet-beta');
32
+
33
+
34
+ const driftClient = new DriftClient({
35
+ connection: connection,
36
+ wallet: new Wallet(new Keypair()),
37
+ programID: new PublicKey(driftConfig.DRIFT_PROGRAM_ID),
38
+ accountSubscription: {
39
+ type: 'polling',
40
+ accountLoader: accountLoader,
41
+ },
42
+ perpMarketIndexes,
43
+ spotMarketIndexes,
44
+ oracleInfos,
45
+ userStats: true,
46
+ env: 'mainnet-beta',
47
+ });
48
+ console.log(`driftClientSubscribed: ${await driftClient.subscribe()}`);
49
+
50
+ const slotSubscriber = new SlotSubscriber(connection);
51
+ await slotSubscriber.subscribe();
52
+
53
+
54
+ // Alternatively, you can also use the UserMap class, which loads the DLOB via RPC calls.
55
+ // const userMap = new UserMap(driftClient, driftClient.userAccountSubscriptionConfig, false);
56
+
57
+ // This loads the DLOB from a server hosted by Drift.
58
+ const dlobAPI = new DLOBApiClient({
59
+ url: 'https://dlob.drift.trade/orders/idlWithSlot',
60
+ });
61
+
62
+ const dlobSubscriber = new DLOBSubscriber({
63
+ dlobSource: dlobAPI,
64
+ slotSource: slotSubscriber,
65
+ driftClient: driftClient,
66
+ updateFrequency: 10000,
67
+ });
68
+ await dlobSubscriber.subscribe();
69
+
70
+ const l2 = dlobSubscriber.getL2({
71
+ marketIndex: 0,
72
+ marketType: MarketType.PERP,
73
+ });
74
+ console.log("Level 2 order book:");
75
+
76
+ console.log("Asks:");
77
+ const asks = l2.asks.slice().reverse();
78
+ for (let i = 0; i < asks.length; i++) {
79
+ const ask = asks[i];
80
+ console.log(` [${asks.length - i - 1}] ${convertToNumber(ask.size, BASE_PRECISION)} @ $${convertToNumber(ask.price, PRICE_PRECISION)}`);
81
+ }
82
+
83
+ console.log("Bids:");
84
+ const bids = l2.bids;
85
+ for (let i = 0; i < bids.length; i++) {
86
+ const bid = bids[i];
87
+ console.log(` [${i}] ${convertToNumber(bid.size, BASE_PRECISION)} @ $${convertToNumber(bid.price, PRICE_PRECISION)}`);
88
+ }
89
+ console.log("");
90
+ console.log("");
91
+
92
+ /**
93
+ Level 2 order book:
94
+ Asks:
95
+ [9] 664.8 @ $21.3248
96
+ [8] 3.5 @ $21.288301
97
+ [7] 3.5 @ $21.287237
98
+ [6] 3.5 @ $21.283129
99
+ [5] 0.1 @ $21.2825
100
+ [4] 1937.6 @ $21.2748
101
+ [3] 1 @ $21.26
102
+ [2] 2015 @ $21.2589
103
+ [1] 503.7 @ $21.2483
104
+ [0] 377.8 @ $21.243
105
+ Bids:
106
+ [0] 1168.2 @ $21.2464
107
+ [1] 136.7 @ $21.2461
108
+ [2] 2570.2 @ $21.2426
109
+ [3] 0.1 @ $21.2419
110
+ [4] 47 @ $21.216188
111
+ [5] 327.8 @ $21.211
112
+ [6] 437 @ $21.2057
113
+ [7] 47 @ $21.205592
114
+ [8] 1748.3 @ $21.1951
115
+ [9] 187 @ $21.1792
116
+ */
117
+
118
+ const l3 = dlobSubscriber.getL3({
119
+ marketIndex: 0,
120
+ marketType: MarketType.PERP,
121
+ });
122
+
123
+ console.log("Level 3 order book:");
124
+ console.log("Asks:");
125
+ const l3asks = l3.asks.slice().reverse();
126
+ for (let i = 0; i < l3asks.length; i++) {
127
+ const ask = l3asks[i];
128
+ console.log(` [${l3asks.length - i - 1}] ${ask.maker.toBase58()} ${convertToNumber(ask.size, BASE_PRECISION)} @ $${convertToNumber(ask.price, PRICE_PRECISION)}`);
129
+ }
130
+
131
+ console.log("Bids:");
132
+ const l3bids = l3.bids;
133
+ for (let i = 0; i < l3bids.length; i++) {
134
+ const bid = l3bids[i];
135
+ console.log(` [${i}] ${bid.maker.toBase58()} ${convertToNumber(bid.size, BASE_PRECISION)} @ $${convertToNumber(bid.price, PRICE_PRECISION)}`);
136
+ }
137
+
138
+ /**
139
+ Level 3 order book:
140
+ Asks:
141
+ ...
142
+ [3] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 1 @ $21.26
143
+ [2] C13FZykQfLXKuMAMh2iuG7JxhQqd8otujNRAgVETU6id 2015 @ $21.2589
144
+ [1] C13FZykQfLXKuMAMh2iuG7JxhQqd8otujNRAgVETU6id 503.7 @ $21.2483
145
+ [0] C13FZykQfLXKuMAMh2iuG7JxhQqd8otujNRAgVETU6id 377.8 @ $21.243
146
+ Bids:
147
+ [0] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 1168.2 @ $21.2464
148
+ [1] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 2570.2 @ $21.2464
149
+ [2] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 136.7 @ $21.2461
150
+ [3] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 2570.2 @ $21.2426
151
+ ...
152
+ */
153
+ }
154
+
155
+ main().catch(console.error);
@@ -66,7 +66,7 @@ class DLOBSubscriber {
66
66
  const isPerp = (0, types_1.isVariant)(marketType, 'perp');
67
67
  if (isPerp) {
68
68
  const perpMarketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
69
- oraclePriceData = this.driftClient.getOraclePriceDataAndSlot(perpMarketAccount.amm.oracle);
69
+ oraclePriceData = this.driftClient.getOracleDataForPerpMarket(perpMarketAccount.marketIndex);
70
70
  fallbackBid = (0, market_1.calculateBidPrice)(perpMarketAccount, oraclePriceData);
71
71
  fallbackAsk = (0, market_1.calculateAskPrice)(perpMarketAccount, oraclePriceData);
72
72
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.28.0-beta.2",
2
+ "version": "2.28.0-beta.4",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/lib/index.d.ts CHANGED
@@ -67,6 +67,7 @@ export * from './dlob/DLOBNode';
67
67
  export * from './dlob/DLOBOrders';
68
68
  export * from './dlob/NodeList';
69
69
  export * from './dlob/DLOBSubscriber';
70
+ export * from './dlob/DLOBApiClient';
70
71
  export * from './dlob/types';
71
72
  export * from './dlob/orderBookLevels';
72
73
  export * from './userMap/userMap';
package/lib/index.js CHANGED
@@ -91,6 +91,7 @@ __exportStar(require("./dlob/DLOBNode"), exports);
91
91
  __exportStar(require("./dlob/DLOBOrders"), exports);
92
92
  __exportStar(require("./dlob/NodeList"), exports);
93
93
  __exportStar(require("./dlob/DLOBSubscriber"), exports);
94
+ __exportStar(require("./dlob/DLOBApiClient"), exports);
94
95
  __exportStar(require("./dlob/types"), exports);
95
96
  __exportStar(require("./dlob/orderBookLevels"), exports);
96
97
  __exportStar(require("./userMap/userMap"), exports);
@@ -1,6 +1,6 @@
1
1
  import { Connection, PublicKey } from '@solana/web3.js';
2
2
  import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
3
- import { MarketData, Client } from '@ellipsis-labs/phoenix-sdk';
3
+ import { Client, Market } from '@ellipsis-labs/phoenix-sdk';
4
4
  import { BN } from '@coral-xyz/anchor';
5
5
  import { L2Level, L2OrderBookGenerator } from '../dlob/orderBookLevels';
6
6
  export type PhoenixMarketSubscriberConfig = {
@@ -21,7 +21,7 @@ export declare class PhoenixSubscriber implements L2OrderBookGenerator {
21
21
  marketAddress: PublicKey;
22
22
  subscriptionType: 'polling' | 'websocket';
23
23
  accountLoader: BulkAccountLoader | undefined;
24
- market: MarketData;
24
+ market: Market;
25
25
  marketCallbackId: string | number;
26
26
  clockCallbackId: string | number;
27
27
  subscribed: boolean;
@@ -24,30 +24,54 @@ class PhoenixSubscriber {
24
24
  if (this.subscribed) {
25
25
  return;
26
26
  }
27
- this.market = (0, phoenix_sdk_1.deserializeMarketData)((await this.connection.getAccountInfo(this.marketAddress, 'confirmed'))
28
- .data);
27
+ this.market = await phoenix_sdk_1.Market.loadFromAddress({
28
+ connection: this.connection,
29
+ address: this.marketAddress,
30
+ });
29
31
  const clock = (0, phoenix_sdk_1.deserializeClockData)((await this.connection.getAccountInfo(web3_js_1.SYSVAR_CLOCK_PUBKEY, 'confirmed'))
30
32
  .data);
31
33
  this.lastUnixTimestamp = (0, phoenix_sdk_1.toNum)(clock.unixTimestamp);
32
34
  if (this.subscriptionType === 'websocket') {
33
35
  this.marketCallbackId = this.connection.onAccountChange(this.marketAddress, (accountInfo, _ctx) => {
34
- this.market = (0, phoenix_sdk_1.deserializeMarketData)(accountInfo.data);
36
+ try {
37
+ this.market = this.market.reload(accountInfo.data);
38
+ }
39
+ catch {
40
+ console.error('Failed to reload Phoenix market data');
41
+ }
35
42
  });
36
43
  this.clockCallbackId = this.connection.onAccountChange(web3_js_1.SYSVAR_CLOCK_PUBKEY, (accountInfo, ctx) => {
37
- this.lastSlot = ctx.slot;
38
- const clock = (0, phoenix_sdk_1.deserializeClockData)(accountInfo.data);
39
- this.lastUnixTimestamp = (0, phoenix_sdk_1.toNum)(clock.unixTimestamp);
44
+ try {
45
+ this.lastSlot = ctx.slot;
46
+ const clock = (0, phoenix_sdk_1.deserializeClockData)(accountInfo.data);
47
+ this.lastUnixTimestamp = (0, phoenix_sdk_1.toNum)(clock.unixTimestamp);
48
+ }
49
+ catch {
50
+ console.error('Failed to reload clock data');
51
+ }
40
52
  });
41
53
  }
42
54
  else {
43
55
  this.marketCallbackId = await this.accountLoader.addAccount(this.marketAddress, (buffer, slot) => {
44
- this.lastSlot = slot;
45
- this.market = (0, phoenix_sdk_1.deserializeMarketData)(buffer);
56
+ try {
57
+ this.lastSlot = slot;
58
+ if (buffer) {
59
+ this.market = this.market.reload(buffer);
60
+ }
61
+ }
62
+ catch {
63
+ console.error('Failed to reload Phoenix market data');
64
+ }
46
65
  });
47
66
  this.clockCallbackId = await this.accountLoader.addAccount(web3_js_1.SYSVAR_CLOCK_PUBKEY, (buffer, slot) => {
48
- this.lastSlot = slot;
49
- const clock = (0, phoenix_sdk_1.deserializeClockData)(buffer);
50
- this.lastUnixTimestamp = (0, phoenix_sdk_1.toNum)(clock.unixTimestamp);
67
+ try {
68
+ this.lastSlot = slot;
69
+ const clock = (0, phoenix_sdk_1.deserializeClockData)(buffer);
70
+ this.lastUnixTimestamp = (0, phoenix_sdk_1.toNum)(clock.unixTimestamp);
71
+ }
72
+ catch {
73
+ console.error('Failed to reload clock data');
74
+ }
51
75
  });
52
76
  }
53
77
  this.subscribed = true;
@@ -58,7 +82,7 @@ class PhoenixSubscriber {
58
82
  if (!bestBid) {
59
83
  return undefined;
60
84
  }
61
- return new anchor_1.BN(Math.floor(bestBid[0] * numericConstants_1.PRICE_PRECISION.toNumber()));
85
+ return new anchor_1.BN(Math.floor(bestBid.price * numericConstants_1.PRICE_PRECISION.toNumber()));
62
86
  }
63
87
  getBestAsk() {
64
88
  const ladder = (0, phoenix_sdk_1.getMarketUiLadder)(this.market, this.lastSlot, this.lastUnixTimestamp, 1);
@@ -66,7 +90,7 @@ class PhoenixSubscriber {
66
90
  if (!bestAsk) {
67
91
  return undefined;
68
92
  }
69
- return new anchor_1.BN(Math.floor(bestAsk[0] * numericConstants_1.PRICE_PRECISION.toNumber()));
93
+ return new anchor_1.BN(Math.floor(bestAsk.price * numericConstants_1.PRICE_PRECISION.toNumber()));
70
94
  }
71
95
  getL2Bids() {
72
96
  return this.getL2Levels('bids');
@@ -80,10 +104,10 @@ class PhoenixSubscriber {
80
104
  const pricePrecision = numericConstants_1.PRICE_PRECISION.toNumber();
81
105
  const ladder = (0, phoenix_sdk_1.getMarketUiLadder)(this.market, this.lastSlot, this.lastUnixTimestamp, 20);
82
106
  for (let i = 0; i < ladder[side].length; i++) {
83
- const [priceNum, sizeNum] = ladder[side][i];
84
- const size = new anchor_1.BN(Math.floor(sizeNum * basePrecision));
107
+ const { price, quantity } = ladder[side][i];
108
+ const size = new anchor_1.BN(Math.floor(quantity * basePrecision));
85
109
  yield {
86
- price: new anchor_1.BN(Math.floor(priceNum * pricePrecision)),
110
+ price: new anchor_1.BN(Math.floor(price * pricePrecision)),
87
111
  size,
88
112
  sources: {
89
113
  phoenix: size,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.28.0-beta.2",
3
+ "version": "2.28.0-beta.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@coral-xyz/anchor": "0.26.0",
37
- "@ellipsis-labs/phoenix-sdk": "^1.3.2",
37
+ "@ellipsis-labs/phoenix-sdk": "^1.4.2",
38
38
  "@project-serum/serum": "^0.13.38",
39
39
  "@pythnetwork/client": "2.5.3",
40
40
  "@solana/spl-token": "^0.1.6",
@@ -107,8 +107,8 @@ export class DLOBSubscriber {
107
107
  if (isPerp) {
108
108
  const perpMarketAccount =
109
109
  this.driftClient.getPerpMarketAccount(marketIndex);
110
- oraclePriceData = this.driftClient.getOraclePriceDataAndSlot(
111
- perpMarketAccount.amm.oracle
110
+ oraclePriceData = this.driftClient.getOracleDataForPerpMarket(
111
+ perpMarketAccount.marketIndex
112
112
  );
113
113
  fallbackBid = calculateBidPrice(perpMarketAccount, oraclePriceData);
114
114
  fallbackAsk = calculateAskPrice(perpMarketAccount, oraclePriceData);
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.28.0-beta.2",
2
+ "version": "2.28.0-beta.4",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -9212,4 +9212,4 @@
9212
9212
  "msg": "InvalidPhoenixMarket"
9213
9213
  }
9214
9214
  ]
9215
- }
9215
+ }
package/src/index.ts CHANGED
@@ -69,6 +69,7 @@ export * from './dlob/DLOBNode';
69
69
  export * from './dlob/DLOBOrders';
70
70
  export * from './dlob/NodeList';
71
71
  export * from './dlob/DLOBSubscriber';
72
+ export * from './dlob/DLOBApiClient';
72
73
  export * from './dlob/types';
73
74
  export * from './dlob/orderBookLevels';
74
75
  export * from './userMap/userMap';
@@ -1,12 +1,11 @@
1
1
  import { Connection, PublicKey, SYSVAR_CLOCK_PUBKEY } from '@solana/web3.js';
2
2
  import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
3
3
  import {
4
- MarketData,
5
4
  Client,
6
- deserializeMarketData,
7
5
  deserializeClockData,
8
6
  toNum,
9
7
  getMarketUiLadder,
8
+ Market,
10
9
  } from '@ellipsis-labs/phoenix-sdk';
11
10
  import { PRICE_PRECISION } from '../constants/numericConstants';
12
11
  import { BN } from '@coral-xyz/anchor';
@@ -34,7 +33,7 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
34
33
  marketAddress: PublicKey;
35
34
  subscriptionType: 'polling' | 'websocket';
36
35
  accountLoader: BulkAccountLoader | undefined;
37
- market: MarketData;
36
+ market: Market;
38
37
  marketCallbackId: string | number;
39
38
  clockCallbackId: string | number;
40
39
 
@@ -61,10 +60,10 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
61
60
  return;
62
61
  }
63
62
 
64
- this.market = deserializeMarketData(
65
- (await this.connection.getAccountInfo(this.marketAddress, 'confirmed'))
66
- .data
67
- );
63
+ this.market = await Market.loadFromAddress({
64
+ connection: this.connection,
65
+ address: this.marketAddress,
66
+ });
68
67
 
69
68
  const clock = deserializeClockData(
70
69
  (await this.connection.getAccountInfo(SYSVAR_CLOCK_PUBKEY, 'confirmed'))
@@ -76,31 +75,49 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
76
75
  this.marketCallbackId = this.connection.onAccountChange(
77
76
  this.marketAddress,
78
77
  (accountInfo, _ctx) => {
79
- this.market = deserializeMarketData(accountInfo.data);
78
+ try {
79
+ this.market = this.market.reload(accountInfo.data);
80
+ } catch {
81
+ console.error('Failed to reload Phoenix market data');
82
+ }
80
83
  }
81
84
  );
82
85
  this.clockCallbackId = this.connection.onAccountChange(
83
86
  SYSVAR_CLOCK_PUBKEY,
84
87
  (accountInfo, ctx) => {
85
- this.lastSlot = ctx.slot;
86
- const clock = deserializeClockData(accountInfo.data);
87
- this.lastUnixTimestamp = toNum(clock.unixTimestamp);
88
+ try {
89
+ this.lastSlot = ctx.slot;
90
+ const clock = deserializeClockData(accountInfo.data);
91
+ this.lastUnixTimestamp = toNum(clock.unixTimestamp);
92
+ } catch {
93
+ console.error('Failed to reload clock data');
94
+ }
88
95
  }
89
96
  );
90
97
  } else {
91
98
  this.marketCallbackId = await this.accountLoader.addAccount(
92
99
  this.marketAddress,
93
100
  (buffer, slot) => {
94
- this.lastSlot = slot;
95
- this.market = deserializeMarketData(buffer);
101
+ try {
102
+ this.lastSlot = slot;
103
+ if (buffer) {
104
+ this.market = this.market.reload(buffer);
105
+ }
106
+ } catch {
107
+ console.error('Failed to reload Phoenix market data');
108
+ }
96
109
  }
97
110
  );
98
111
  this.clockCallbackId = await this.accountLoader.addAccount(
99
112
  SYSVAR_CLOCK_PUBKEY,
100
113
  (buffer, slot) => {
101
- this.lastSlot = slot;
102
- const clock = deserializeClockData(buffer);
103
- this.lastUnixTimestamp = toNum(clock.unixTimestamp);
114
+ try {
115
+ this.lastSlot = slot;
116
+ const clock = deserializeClockData(buffer);
117
+ this.lastUnixTimestamp = toNum(clock.unixTimestamp);
118
+ } catch {
119
+ console.error('Failed to reload clock data');
120
+ }
104
121
  }
105
122
  );
106
123
  }
@@ -119,7 +136,7 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
119
136
  if (!bestBid) {
120
137
  return undefined;
121
138
  }
122
- return new BN(Math.floor(bestBid[0] * PRICE_PRECISION.toNumber()));
139
+ return new BN(Math.floor(bestBid.price * PRICE_PRECISION.toNumber()));
123
140
  }
124
141
 
125
142
  public getBestAsk(): BN | undefined {
@@ -134,7 +151,7 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
134
151
  if (!bestAsk) {
135
152
  return undefined;
136
153
  }
137
- return new BN(Math.floor(bestAsk[0] * PRICE_PRECISION.toNumber()));
154
+ return new BN(Math.floor(bestAsk.price * PRICE_PRECISION.toNumber()));
138
155
  }
139
156
 
140
157
  public getL2Bids(): Generator<L2Level> {
@@ -158,10 +175,10 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
158
175
  );
159
176
 
160
177
  for (let i = 0; i < ladder[side].length; i++) {
161
- const [priceNum, sizeNum] = ladder[side][i];
162
- const size = new BN(Math.floor(sizeNum * basePrecision));
178
+ const { price, quantity } = ladder[side][i];
179
+ const size = new BN(Math.floor(quantity * basePrecision));
163
180
  yield {
164
- price: new BN(Math.floor(priceNum * pricePrecision)),
181
+ price: new BN(Math.floor(price * pricePrecision)),
165
182
  size,
166
183
  sources: {
167
184
  phoenix: size,
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assert = void 0;
4
- function assert(condition, error) {
5
- if (!condition) {
6
- throw new Error(error || 'Unspecified AssertionError');
7
- }
8
- }
9
- exports.assert = assert;
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseTokenAccount = void 0;
4
- const spl_token_1 = require("@solana/spl-token");
5
- const web3_js_1 = require("@solana/web3.js");
6
- function parseTokenAccount(data) {
7
- const accountInfo = spl_token_1.AccountLayout.decode(data);
8
- accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
9
- accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
10
- accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
11
- if (accountInfo.delegateOption === 0) {
12
- accountInfo.delegate = null;
13
- // eslint-disable-next-line new-cap
14
- accountInfo.delegatedAmount = new spl_token_1.u64(0);
15
- }
16
- else {
17
- accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
18
- accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
19
- }
20
- accountInfo.isInitialized = accountInfo.state !== 0;
21
- accountInfo.isFrozen = accountInfo.state === 2;
22
- if (accountInfo.isNativeOption === 1) {
23
- accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
24
- accountInfo.isNative = true;
25
- }
26
- else {
27
- accountInfo.rentExemptReserve = null;
28
- accountInfo.isNative = false;
29
- }
30
- if (accountInfo.closeAuthorityOption === 0) {
31
- accountInfo.closeAuthority = null;
32
- }
33
- else {
34
- accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
35
- }
36
- return accountInfo;
37
- }
38
- exports.parseTokenAccount = parseTokenAccount;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.findComputeUnitConsumption = void 0;
13
- function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- const tx = yield connection.getTransaction(txSignature, { commitment });
16
- const computeUnits = [];
17
- const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
18
- tx.meta.logMessages.forEach((logMessage) => {
19
- const match = logMessage.match(regex);
20
- if (match && match[1]) {
21
- computeUnits.push(match[1]);
22
- }
23
- });
24
- return computeUnits;
25
- });
26
- }
27
- exports.findComputeUnitConsumption = findComputeUnitConsumption;
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTokenAddress = void 0;
4
- const spl_token_1 = require("@solana/spl-token");
5
- const web3_js_1 = require("@solana/web3.js");
6
- const getTokenAddress = (mintAddress, userPubKey) => {
7
- return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
8
- };
9
- exports.getTokenAddress = getTokenAddress;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.promiseTimeout = void 0;
4
- function promiseTimeout(promise, timeoutMs) {
5
- let timeoutId;
6
- const timeoutPromise = new Promise((resolve) => {
7
- timeoutId = setTimeout(() => resolve(null), timeoutMs);
8
- });
9
- return Promise.race([promise, timeoutPromise]).then((result) => {
10
- clearTimeout(timeoutId);
11
- return result;
12
- });
13
- }
14
- exports.promiseTimeout = promiseTimeout;
package/src/util/tps.js DELETED
@@ -1,27 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.estimateTps = void 0;
13
- function estimateTps(programId, connection, failed) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- let signatures = yield connection.getSignaturesForAddress(programId, undefined, 'finalized');
16
- if (failed) {
17
- signatures = signatures.filter((signature) => signature.err);
18
- }
19
- const numberOfSignatures = signatures.length;
20
- if (numberOfSignatures === 0) {
21
- return 0;
22
- }
23
- return (numberOfSignatures /
24
- (signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
25
- });
26
- }
27
- exports.estimateTps = estimateTps;