@0xobelisk/sui-client 1.0.6 → 1.0.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.
@@ -0,0 +1,36 @@
1
+ export type WebSocketInstance = WebSocket;
2
+
3
+ export interface WebSocketConstructor {
4
+ new (url: string): WebSocket;
5
+ }
6
+
7
+ export function createWebSocketClient(url: string): WebSocketInstance {
8
+ if (typeof window !== 'undefined') {
9
+ // Browser Environment
10
+ return new WebSocket(url);
11
+ } else {
12
+ // Node.js Environment
13
+ try {
14
+ require.resolve('ws');
15
+ const WebSocket = require('ws');
16
+ return new WebSocket(url);
17
+ } catch (e) {
18
+ console.error('Failed to load WebSocket implementation:', e);
19
+ throw new Error(
20
+ 'WebSocket implementation not available. Please install the "ws" package.'
21
+ );
22
+ }
23
+ }
24
+ }
25
+
26
+ export function isWebSocketSupported(): boolean {
27
+ if (typeof window !== 'undefined') {
28
+ return typeof WebSocket !== 'undefined';
29
+ }
30
+ try {
31
+ require.resolve('ws');
32
+ return true;
33
+ } catch {
34
+ return false;
35
+ }
36
+ }
@@ -0,0 +1,232 @@
1
+ import { Http } from '../http';
2
+
3
+ export interface OrderDirection {
4
+ ASC: 'ASC';
5
+ DESC: 'DESC';
6
+ }
7
+
8
+ export interface OrderBy {
9
+ field: string;
10
+ direction: OrderDirection['ASC'] | OrderDirection['DESC'];
11
+ }
12
+
13
+ export interface PageInfo {
14
+ hasNextPage: boolean;
15
+ hasPreviousPage: boolean;
16
+ startCursor?: string;
17
+ endCursor?: string;
18
+ }
19
+
20
+ export interface Transaction {
21
+ id: number;
22
+ checkpoint: number;
23
+ digest: string;
24
+ created_at: string;
25
+ }
26
+
27
+ export interface Schema {
28
+ id: number;
29
+ name: string;
30
+ key1?: string;
31
+ key2?: string;
32
+ value: string;
33
+ last_update_checkpoint: string;
34
+ last_update_digest: string;
35
+ is_removed: boolean;
36
+ created_at: string;
37
+ updated_at: string;
38
+ }
39
+
40
+ export interface Event {
41
+ id: number;
42
+ checkpoint: string;
43
+ digest: string;
44
+ name: string;
45
+ value: string;
46
+ created_at: string;
47
+ }
48
+
49
+ export interface ConnectionResponse<T> {
50
+ edges: Array<{
51
+ cursor: string;
52
+ node: T;
53
+ }>;
54
+ pageInfo: PageInfo;
55
+ }
56
+
57
+ export class SuiIndexerClient {
58
+ private http: Http;
59
+
60
+ constructor(http: Http) {
61
+ this.http = http;
62
+ }
63
+
64
+ private async fetchGraphql<T>(query: string, variables?: any): Promise<T> {
65
+ return this.http.fetchGraphql({ query, variables });
66
+ }
67
+
68
+ async getTransactions(params?: {
69
+ first?: number;
70
+ after?: string;
71
+ last?: number;
72
+ before?: string;
73
+ checkpoint?: number;
74
+ orderBy?: OrderBy;
75
+ distinct?: boolean;
76
+ }) {
77
+ const query = `
78
+ query GetTransactions($first: Int, $after: String, $last: Int, $before: String, $checkpoint: Int, $orderBy: TransactionOrderBy, $distinct: Boolean) {
79
+ transactions(first: $first, after: $after, last: $last, before: $before, checkpoint: $checkpoint, orderBy: $orderBy, distinct: $distinct) {
80
+ edges {
81
+ cursor
82
+ node {
83
+ id
84
+ checkpoint
85
+ digest
86
+ created_at
87
+ }
88
+ }
89
+ pageInfo {
90
+ hasNextPage
91
+ hasPreviousPage
92
+ startCursor
93
+ endCursor
94
+ }
95
+ }
96
+ }
97
+ `;
98
+
99
+ const response = await this.fetchGraphql<{
100
+ transactions: ConnectionResponse<Transaction>;
101
+ }>(query, params);
102
+ return response.transactions;
103
+ }
104
+
105
+ async getSchemas(params?: {
106
+ first?: number;
107
+ after?: string;
108
+ last?: number;
109
+ before?: string;
110
+ name?: string;
111
+ key1?: string;
112
+ key2?: string;
113
+ orderBy?: OrderBy;
114
+ distinct?: boolean;
115
+ }): Promise<ConnectionResponse<Schema>> {
116
+ const query = `
117
+ query GetSchemas($first: Int, $after: String, $last: Int, $before: String, $name: String, $key1: String, $key2: String, $orderBy: SchemaOrderBy, $distinct: Boolean) {
118
+ schemas(first: $first, after: $after, last: $last, before: $before, name: $name, key1: $key1, key2: $key2, orderBy: $orderBy, distinct: $distinct) {
119
+ edges {
120
+ cursor
121
+ node {
122
+ id
123
+ name
124
+ key1
125
+ key2
126
+ value
127
+ last_update_checkpoint
128
+ last_update_digest
129
+ is_removed
130
+ created_at
131
+ updated_at
132
+ }
133
+ }
134
+ pageInfo {
135
+ hasNextPage
136
+ hasPreviousPage
137
+ startCursor
138
+ endCursor
139
+ }
140
+ }
141
+ }
142
+ `;
143
+
144
+ const response = await this.fetchGraphql<{
145
+ schemas: ConnectionResponse<Schema>;
146
+ }>(query, params);
147
+ return response.schemas;
148
+ }
149
+
150
+ async getEvents(params?: {
151
+ first?: number;
152
+ after?: string;
153
+ last?: number;
154
+ before?: string;
155
+ name?: string;
156
+ checkpoint?: string;
157
+ orderBy?: OrderBy;
158
+ distinct?: boolean;
159
+ }) {
160
+ const query = `
161
+ query GetEvents($first: Int, $after: String, $last: Int, $before: String, $name: String, $checkpoint: String, $orderBy: EventOrderBy, $distinct: Boolean) {
162
+ events(first: $first, after: $after, last: $last, before: $before, name: $name, checkpoint: $checkpoint, orderBy: $orderBy, distinct: $distinct) {
163
+ edges {
164
+ cursor
165
+ node {
166
+ id
167
+ checkpoint
168
+ digest
169
+ name
170
+ value
171
+ created_at
172
+ }
173
+ }
174
+ pageInfo {
175
+ hasNextPage
176
+ hasPreviousPage
177
+ startCursor
178
+ endCursor
179
+ }
180
+ }
181
+ }
182
+ `;
183
+
184
+ const response = await this.fetchGraphql<{
185
+ events: ConnectionResponse<Event>;
186
+ }>(query, params);
187
+ return response.events;
188
+ }
189
+
190
+ async getStorage({
191
+ name,
192
+ key1,
193
+ key2,
194
+ first,
195
+ after,
196
+ last,
197
+ before,
198
+ orderBy,
199
+ distinct,
200
+ }: {
201
+ name: string;
202
+ key1?: string;
203
+ key2?: string;
204
+ first?: number;
205
+ after?: string;
206
+ last?: number;
207
+ before?: string;
208
+ orderBy?: OrderBy;
209
+ distinct?: boolean;
210
+ }): Promise<ConnectionResponse<Schema>> {
211
+ const schemas = await this.getSchemas({
212
+ name,
213
+ key1,
214
+ key2,
215
+ first,
216
+ after,
217
+ last,
218
+ before,
219
+ orderBy,
220
+ distinct,
221
+ });
222
+
223
+ return schemas;
224
+ }
225
+
226
+ async subscribe(
227
+ names: string[],
228
+ handleData: (data: any) => void
229
+ ): Promise<WebSocket> {
230
+ return this.http.subscribe(names, handleData);
231
+ }
232
+ }
File without changes
@@ -7,6 +7,7 @@ export interface NetworkConfig {
7
7
  txExplorer: string;
8
8
  accountExplorer: string;
9
9
  explorer: string;
10
+ indexerUrl: string;
10
11
  }
11
12
 
12
13
  export const getDefaultURL = (
@@ -23,6 +24,7 @@ export const getDefaultURL = (
23
24
  accountExplorer:
24
25
  'https://explorer.polymedia.app/address/:address?network=local',
25
26
  explorer: 'https://explorer.polymedia.app?network=local',
27
+ indexerUrl: 'http://127.0.0.1:3001',
26
28
  };
27
29
  case 'devnet':
28
30
  return {
@@ -31,6 +33,7 @@ export const getDefaultURL = (
31
33
  txExplorer: 'https://suiscan.xyz/devnet/tx/:txHash',
32
34
  accountExplorer: 'https://suiscan.xyz/devnet/address/:address',
33
35
  explorer: 'https://suiscan.xyz/devnet',
36
+ indexerUrl: 'http://127.0.0.1:3001',
34
37
  };
35
38
  case 'testnet':
36
39
  return {
@@ -40,6 +43,7 @@ export const getDefaultURL = (
40
43
  txExplorer: 'https://suiscan.xyz/testnet/tx/:txHash',
41
44
  accountExplorer: 'https://suiscan.xyz/testnet/address/:address',
42
45
  explorer: 'https://suiscan.xyz/testnet',
46
+ indexerUrl: 'http://127.0.0.1:3001',
43
47
  };
44
48
  case 'mainnet':
45
49
  return {
@@ -49,6 +53,7 @@ export const getDefaultURL = (
49
53
  txExplorer: 'https://suiscan.xyz/mainnet/tx/:txHash',
50
54
  accountExplorer: 'https://suiscan.xyz/mainnet/address/:address',
51
55
  explorer: 'https://suiscan.xyz/mainnet',
56
+ indexerUrl: 'http://127.0.0.1:3001',
52
57
  };
53
58
  default:
54
59
  return {
@@ -58,6 +63,7 @@ export const getDefaultURL = (
58
63
  txExplorer: 'https://suiscan.xyz/testnet/tx/:txHash',
59
64
  accountExplorer: 'https://suiscan.xyz/testnet/address/:address',
60
65
  explorer: 'https://suiscan.xyz/testnet',
66
+ indexerUrl: 'http://127.0.0.1:3001',
61
67
  };
62
68
  }
63
69
  };
@@ -23,6 +23,7 @@ import type {
23
23
  import { SuiTx } from '../libs/suiTxBuilder';
24
24
 
25
25
  import { SuiMoveMoudleFuncType } from '../libs/suiContractFactory/types';
26
+ import { FetchOptions } from '../libs/http';
26
27
 
27
28
  export const ObjectContentFields = record(string(), any());
28
29
  export type ObjectContentFields = Infer<typeof ObjectContentFields>;
@@ -50,6 +51,10 @@ export type DubheParams = {
50
51
  networkType?: NetworkType;
51
52
  packageId?: string;
52
53
  metadata?: SuiMoveNormalizedModules;
54
+ customFetch?: typeof fetch;
55
+ defaultOptions?: FetchOptions;
56
+ indexerUrl?: string;
57
+ indexerWsUrl?: string;
53
58
  };
54
59
 
55
60
  export type SchemaFieldType = {
@@ -28,3 +28,12 @@ export function normalizePackageId(input: string): string {
28
28
  const normalized = withoutPrefix.replace(/^0+/, '');
29
29
  return '0x' + normalized;
30
30
  }
31
+
32
+ export function convertHttpToWebSocket(url: string): string {
33
+ if (url.startsWith('https://')) {
34
+ return url.replace('https://', 'wss://');
35
+ } else if (url.startsWith('http://')) {
36
+ return url.replace('http://', 'ws://');
37
+ }
38
+ return url;
39
+ }