@0xobelisk/sui-client 1.1.6 → 1.1.8

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.
@@ -1,4 +1,5 @@
1
1
  import { WebSocketInstance } from './ws-adapter';
2
+ import { SubscribableType } from '../../types';
2
3
  export type FetchOptions = RequestInit & {
3
4
  next?: {
4
5
  revalidate?: boolean | number;
@@ -17,5 +18,5 @@ export declare class Http {
17
18
  query: string;
18
19
  variables?: any;
19
20
  }): Promise<T>;
20
- subscribe(names: string[], handleData: (data: any) => void): Promise<WebSocketInstance>;
21
+ subscribe(types: SubscribableType[], handleData: (data: any) => void): Promise<WebSocketInstance>;
21
22
  }
@@ -1,8 +1,13 @@
1
1
  import { Http } from '../http';
2
+ import { SubscribableType } from '../../types';
2
3
  export interface OrderDirection {
3
4
  ASC: 'ASC';
4
5
  DESC: 'DESC';
5
6
  }
7
+ export declare enum SubscriptionKind {
8
+ Event = "event",
9
+ Schema = "schema"
10
+ }
6
11
  export interface PageInfo {
7
12
  hasNextPage: boolean;
8
13
  endCursor?: string;
@@ -65,6 +70,7 @@ export declare class SuiIndexerClient {
65
70
  checkpoint?: number;
66
71
  orderBy?: string[];
67
72
  }): Promise<ConnectionResponse<IndexerTransaction>>;
73
+ getTransaction(digest: string): Promise<IndexerTransaction | undefined>;
68
74
  getSchemas(params?: {
69
75
  first?: number;
70
76
  after?: string;
@@ -107,5 +113,5 @@ export declare class SuiIndexerClient {
107
113
  last_update_digest?: string;
108
114
  value?: any;
109
115
  }): Promise<StorageItemResponse<IndexerSchema> | undefined>;
110
- subscribe(names: string[], handleData: (data: any) => void): Promise<WebSocket>;
116
+ subscribe(types: SubscribableType[], handleData: (data: any) => void): Promise<WebSocket>;
111
117
  }
@@ -6,6 +6,7 @@ import type { SuiMoveNormalizedModules, DevInspectResults, SuiTransactionBlockRe
6
6
  import { SuiTx } from '../libs/suiTxBuilder';
7
7
  import { SuiMoveMoudleFuncType } from '../libs/suiContractFactory/types';
8
8
  import { FetchOptions } from '../libs/http';
9
+ import { SubscriptionKind } from '../libs/suiIndexerClient';
9
10
  export declare const ObjectContentFields: import("superstruct").Struct<Record<string, any>, null>;
10
11
  export type ObjectContentFields = Infer<typeof ObjectContentFields>;
11
12
  export type DubheObjectData = {
@@ -206,4 +207,12 @@ export type ObjectContent = {
206
207
  dataType: string;
207
208
  };
208
209
  export type SuiDubheReturnType<T extends boolean> = T extends true ? SuiTransactionBlockResponse : SuiTx;
210
+ export type SubscribableType = {
211
+ kind: SubscriptionKind.Event;
212
+ name?: string;
213
+ sender?: string;
214
+ } | {
215
+ kind: SubscriptionKind.Schema;
216
+ name?: string;
217
+ };
209
218
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xobelisk/sui-client",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Tookit for interacting with move eps framework",
5
5
  "keywords": [
6
6
  "sui",
@@ -70,19 +70,19 @@
70
70
  "@typescript-eslint/eslint-plugin": "^6.8.0",
71
71
  "@typescript-eslint/parser": "^6.8.0",
72
72
  "dotenv": "^16.3.1",
73
- "eslint": "^8.52.0",
74
- "eslint-config-prettier": "^8.8.0",
75
73
  "eslint-plugin-prettier": "^5.0.1",
76
74
  "graphql-ws": "^6.0.2",
77
75
  "jest": "^29.7.0",
78
76
  "lint-staged": "^15.0.2",
79
- "prettier": "^2.8.8",
80
77
  "ts-node": "^10.9.1",
81
78
  "tsconfig-paths": "^4.2.0",
82
79
  "tsup": "^7.1.0",
83
80
  "typedoc": "^0.25.2",
84
81
  "typescript": "^5.2.2",
85
- "@types/ws": "^8.5.14"
82
+ "@types/ws": "^8.5.14",
83
+ "eslint": "^8.56.0",
84
+ "eslint-config-prettier": "^9.1.0",
85
+ "prettier": "3.3.3"
86
86
  },
87
87
  "lint-staged": {
88
88
  "**/*.ts": [
@@ -154,8 +154,9 @@
154
154
  "lint:fix": "eslint . --ignore-pattern dist --ext .ts --fix",
155
155
  "commit": "commit",
156
156
  "doc": "typedoc --out docs src/index.ts",
157
- "chalk": "^5.0.1",
158
- "prettier": "^2.8.4",
159
- "prettier-plugin-rust": "^0.1.9"
157
+ "format": "prettier --write .",
158
+ "format:check": "prettier --check .",
159
+ "type-check": "tsc --noEmit",
160
+ "validate": "pnpm format:check && pnpm type-check"
160
161
  }
161
162
  }
package/src/dubhe.ts CHANGED
@@ -28,6 +28,7 @@ import {
28
28
  SuiTxArg,
29
29
  SuiObjectArg,
30
30
  SuiVecTxArg,
31
+ SubscribableType,
31
32
  } from './types';
32
33
  import {
33
34
  convertHttpToWebSocket,
@@ -1142,6 +1143,55 @@ export class Dubhe {
1142
1143
  });
1143
1144
  }
1144
1145
 
1146
+ async getTransaction(
1147
+ digest: string
1148
+ ): Promise<IndexerTransaction | undefined> {
1149
+ return await this.suiIndexerClient.getTransaction(digest);
1150
+ }
1151
+
1152
+ async awaitIndexerTransaction(
1153
+ digest: string,
1154
+ options?: {
1155
+ checkInterval?: number;
1156
+ timeout?: number;
1157
+ maxRetries?: number;
1158
+ }
1159
+ ): Promise<IndexerTransaction | undefined> {
1160
+ const {
1161
+ checkInterval = 100,
1162
+ timeout = 30000, // 30 seconds default timeout
1163
+ maxRetries = 300, // 300 times default max retries
1164
+ } = options ?? {};
1165
+
1166
+ const startTime = Date.now();
1167
+ let retryCount = 0;
1168
+
1169
+ while (retryCount < maxRetries) {
1170
+ try {
1171
+ if (Date.now() - startTime > timeout) {
1172
+ throw new Error(`Timeout waiting for transaction ${digest}`);
1173
+ }
1174
+
1175
+ await new Promise((resolve) => setTimeout(resolve, checkInterval));
1176
+
1177
+ const transaction = await this.getTransaction(digest);
1178
+ if (transaction) {
1179
+ return transaction;
1180
+ }
1181
+
1182
+ retryCount++;
1183
+ } catch (error) {
1184
+ throw new Error(
1185
+ `Error while waiting for transaction ${digest}: ${error}`
1186
+ );
1187
+ }
1188
+ }
1189
+
1190
+ throw new Error(
1191
+ `Max retries (${maxRetries}) reached while waiting for transaction ${digest}`
1192
+ );
1193
+ }
1194
+
1145
1195
  async getEvents({
1146
1196
  first,
1147
1197
  after,
@@ -1274,10 +1324,10 @@ export class Dubhe {
1274
1324
  }
1275
1325
 
1276
1326
  async subscribe(
1277
- names: string[],
1327
+ types: SubscribableType[],
1278
1328
  handleData: (data: any) => void
1279
1329
  ): Promise<WebSocket> {
1280
- return this.suiIndexerClient.subscribe(names, handleData);
1330
+ return this.suiIndexerClient.subscribe(types, handleData);
1281
1331
  }
1282
1332
 
1283
1333
  #processKeyParameter(tx: Transaction, keyType: string, value: any) {
package/src/index.ts CHANGED
@@ -11,5 +11,6 @@ export { SuiAccountManager } from './libs/suiAccountManager';
11
11
  export { SuiTx } from './libs/suiTxBuilder';
12
12
  export { MultiSigClient } from './libs/multiSig';
13
13
  export { SuiContractFactory } from './libs/suiContractFactory';
14
+ export { SubscriptionKind } from './libs/suiIndexerClient';
14
15
  export { loadMetadata } from './metadata';
15
16
  export type * from './types';
@@ -1,5 +1,9 @@
1
1
  export class BaseError extends Error {
2
- constructor(message: string, public code: number, public type: string) {
2
+ constructor(
3
+ message: string,
4
+ public code: number,
5
+ public type: string
6
+ ) {
3
7
  super(message);
4
8
  this.name = this.constructor.name;
5
9
  }
@@ -1,5 +1,6 @@
1
1
  import { BaseError, HttpError, GraphQLError, ParseError } from './errors';
2
2
  import { createWebSocketClient, WebSocketInstance } from './ws-adapter';
3
+ import { SubscribableType } from '../../types';
3
4
 
4
5
  export type FetchOptions = RequestInit & {
5
6
  next?: {
@@ -122,17 +123,14 @@ export class Http {
122
123
  }
123
124
 
124
125
  async subscribe(
125
- names: string[],
126
+ types: SubscribableType[],
126
127
  handleData: (data: any) => void
127
128
  ): Promise<WebSocketInstance> {
128
129
  const ws = createWebSocketClient(this.wsEndpoint);
129
130
 
130
131
  ws.onopen = () => {
131
132
  console.log('Connected to the WebSocket server');
132
- const subscribeMessage = JSON.stringify({
133
- type: 'subscribe',
134
- names: names,
135
- });
133
+ const subscribeMessage = JSON.stringify(types);
136
134
  ws.send(subscribeMessage);
137
135
  };
138
136
 
@@ -1,11 +1,17 @@
1
1
  import { Http } from '../http';
2
2
  import { parseValue } from './utils';
3
+ import { SubscribableType } from '../../types';
3
4
 
4
5
  export interface OrderDirection {
5
6
  ASC: 'ASC';
6
7
  DESC: 'DESC';
7
8
  }
8
9
 
10
+ export enum SubscriptionKind {
11
+ Event = 'event',
12
+ Schema = 'schema',
13
+ }
14
+
9
15
  // export interface OrderBy {
10
16
  // field: string;
11
17
  // direction: OrderDirection['ASC'] | OrderDirection['DESC'];
@@ -115,6 +121,16 @@ export class SuiIndexerClient {
115
121
  return response.transactions;
116
122
  }
117
123
 
124
+ async getTransaction(
125
+ digest: string
126
+ ): Promise<IndexerTransaction | undefined> {
127
+ const response = await this.getTransactions({
128
+ first: 1,
129
+ digest,
130
+ });
131
+ return response.edges[0]?.node;
132
+ }
133
+
118
134
  async getSchemas(params?: {
119
135
  first?: number;
120
136
  after?: string;
@@ -305,9 +321,9 @@ export class SuiIndexerClient {
305
321
  }
306
322
 
307
323
  async subscribe(
308
- names: string[],
324
+ types: SubscribableType[],
309
325
  handleData: (data: any) => void
310
326
  ): Promise<WebSocket> {
311
- return this.http.subscribe(names, handleData);
327
+ return this.http.subscribe(types, handleData);
312
328
  }
313
329
  }
@@ -13,9 +13,8 @@ export async function loadMetadata(
13
13
  fullnodeUrls = fullnodeUrls || [getFullnodeUrl(networkType)];
14
14
  const suiInteractor = new SuiInteractor(fullnodeUrls);
15
15
  if (packageId !== undefined) {
16
- const jsonData = await suiInteractor.getNormalizedMoveModulesByPackage(
17
- packageId
18
- );
16
+ const jsonData =
17
+ await suiInteractor.getNormalizedMoveModulesByPackage(packageId);
19
18
 
20
19
  return jsonData as SuiMoveNormalizedModules;
21
20
  } else {
@@ -24,6 +24,7 @@ import { SuiTx } from '../libs/suiTxBuilder';
24
24
 
25
25
  import { SuiMoveMoudleFuncType } from '../libs/suiContractFactory/types';
26
26
  import { FetchOptions } from '../libs/http';
27
+ import { SubscriptionKind } from '../libs/suiIndexerClient';
27
28
 
28
29
  export const ObjectContentFields = record(string(), any());
29
30
  export type ObjectContentFields = Infer<typeof ObjectContentFields>;
@@ -306,3 +307,7 @@ export type ObjectContent = {
306
307
  export type SuiDubheReturnType<T extends boolean> = T extends true
307
308
  ? SuiTransactionBlockResponse
308
309
  : SuiTx;
310
+
311
+ export type SubscribableType =
312
+ | { kind: SubscriptionKind.Event; name?: string; sender?: string }
313
+ | { kind: SubscriptionKind.Schema; name?: string };