@mysten/sui 2.10.0 → 2.12.0
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/CHANGELOG.md +16 -0
- package/dist/bcs/bcs.d.mts +6 -6
- package/dist/bcs/index.d.mts +20 -20
- package/dist/client/types.d.mts +8 -0
- package/dist/client/types.d.mts.map +1 -1
- package/dist/graphql/core.d.mts.map +1 -1
- package/dist/graphql/core.mjs +2 -1
- package/dist/graphql/core.mjs.map +1 -1
- package/dist/grpc/core.d.mts.map +1 -1
- package/dist/grpc/core.mjs +3 -1
- package/dist/grpc/core.mjs.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/ledger_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/move_package_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/transaction.d.mts.map +1 -1
- package/dist/grpc/proto/sui/rpc/v2/transaction_execution_service.client.d.mts +4 -4
- package/dist/jsonRpc/core.d.mts.map +1 -1
- package/dist/jsonRpc/core.mjs +30 -20
- package/dist/jsonRpc/core.mjs.map +1 -1
- package/dist/jsonRpc/http-transport.d.mts +1 -17
- package/dist/jsonRpc/http-transport.d.mts.map +1 -1
- package/dist/jsonRpc/http-transport.mjs +0 -23
- package/dist/jsonRpc/http-transport.mjs.map +1 -1
- package/dist/jsonRpc/index.d.mts +3 -3
- package/dist/jsonRpc/types/common.d.mts +1 -2
- package/dist/jsonRpc/types/common.d.mts.map +1 -1
- package/dist/jsonRpc/types/index.d.mts +1 -1
- package/dist/transactions/Transaction.d.mts +9 -9
- package/dist/transactions/Transaction.d.mts.map +1 -1
- package/dist/transactions/data/v1.d.mts +220 -220
- package/dist/transactions/data/v1.d.mts.map +1 -1
- package/dist/transactions/data/v2.d.mts +16 -16
- package/dist/transactions/data/v2.d.mts.map +1 -1
- package/dist/version.mjs +1 -1
- package/dist/version.mjs.map +1 -1
- package/dist/zklogin/bcs.d.mts +14 -14
- package/dist/zklogin/bcs.d.mts.map +1 -1
- package/docs/clients/core.md +15 -0
- package/docs/migrations/sui-2.0/json-rpc-migration.md +17 -1
- package/docs/utils/derived_objects.md +21 -0
- package/package.json +2 -4
- package/src/client/types.ts +8 -0
- package/src/graphql/core.ts +1 -0
- package/src/grpc/core.ts +5 -0
- package/src/jsonRpc/core.ts +50 -29
- package/src/jsonRpc/http-transport.ts +0 -52
- package/src/jsonRpc/index.ts +0 -1
- package/src/jsonRpc/types/common.ts +0 -1
- package/src/version.ts +1 -1
- package/dist/jsonRpc/rpc-websocket-client.d.mts +0 -26
- package/dist/jsonRpc/rpc-websocket-client.d.mts.map +0 -1
- package/dist/jsonRpc/rpc-websocket-client.mjs +0 -135
- package/dist/jsonRpc/rpc-websocket-client.mjs.map +0 -1
- package/src/jsonRpc/rpc-websocket-client.ts +0 -241
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import { JsonRpcError } from './errors.js';
|
|
5
|
-
|
|
6
|
-
function getWebsocketUrl(httpUrl: string): string {
|
|
7
|
-
const url = new URL(httpUrl);
|
|
8
|
-
url.protocol = url.protocol.replace('http', 'ws');
|
|
9
|
-
return url.toString();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
type JsonRpcMessage =
|
|
13
|
-
| {
|
|
14
|
-
id: number;
|
|
15
|
-
result: never;
|
|
16
|
-
error: {
|
|
17
|
-
code: number;
|
|
18
|
-
message: string;
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
| {
|
|
22
|
-
id: number;
|
|
23
|
-
result: unknown;
|
|
24
|
-
error: never;
|
|
25
|
-
}
|
|
26
|
-
| {
|
|
27
|
-
method: string;
|
|
28
|
-
params: NotificationMessageParams;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
type NotificationMessageParams = {
|
|
32
|
-
subscription?: number;
|
|
33
|
-
result: object;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
type SubscriptionRequest<T = any> = {
|
|
37
|
-
method: string;
|
|
38
|
-
unsubscribe: string;
|
|
39
|
-
params: any[];
|
|
40
|
-
onMessage: (event: T) => void;
|
|
41
|
-
signal?: AbortSignal;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Configuration options for the websocket connection
|
|
46
|
-
*/
|
|
47
|
-
export type WebsocketClientOptions = {
|
|
48
|
-
/**
|
|
49
|
-
* Custom WebSocket class to use. Defaults to the global WebSocket class, if available.
|
|
50
|
-
*/
|
|
51
|
-
WebSocketConstructor?: typeof WebSocket;
|
|
52
|
-
/**
|
|
53
|
-
* Milliseconds before timing out while calling an RPC method
|
|
54
|
-
*/
|
|
55
|
-
callTimeout?: number;
|
|
56
|
-
/**
|
|
57
|
-
* Milliseconds between attempts to connect
|
|
58
|
-
*/
|
|
59
|
-
reconnectTimeout?: number;
|
|
60
|
-
/**
|
|
61
|
-
* Maximum number of times to try connecting before giving up
|
|
62
|
-
*/
|
|
63
|
-
maxReconnects?: number;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
export const DEFAULT_CLIENT_OPTIONS = {
|
|
67
|
-
// We fudge the typing because we also check for undefined in the constructor:
|
|
68
|
-
WebSocketConstructor: (typeof WebSocket !== 'undefined'
|
|
69
|
-
? WebSocket
|
|
70
|
-
: undefined) as typeof WebSocket,
|
|
71
|
-
callTimeout: 30000,
|
|
72
|
-
reconnectTimeout: 3000,
|
|
73
|
-
maxReconnects: 5,
|
|
74
|
-
} satisfies WebsocketClientOptions;
|
|
75
|
-
|
|
76
|
-
export class WebsocketClient {
|
|
77
|
-
endpoint: string;
|
|
78
|
-
options: Required<WebsocketClientOptions>;
|
|
79
|
-
#requestId = 0;
|
|
80
|
-
#disconnects = 0;
|
|
81
|
-
#webSocket: WebSocket | null = null;
|
|
82
|
-
#connectionPromise: Promise<WebSocket> | null = null;
|
|
83
|
-
#subscriptions = new Set<RpcSubscription>();
|
|
84
|
-
#pendingRequests = new Map<
|
|
85
|
-
number,
|
|
86
|
-
{
|
|
87
|
-
resolve: (result: Extract<JsonRpcMessage, { id: number }>) => void;
|
|
88
|
-
reject: (reason: unknown) => void;
|
|
89
|
-
timeout: ReturnType<typeof setTimeout>;
|
|
90
|
-
}
|
|
91
|
-
>();
|
|
92
|
-
|
|
93
|
-
constructor(endpoint: string, options: WebsocketClientOptions = {}) {
|
|
94
|
-
this.endpoint = endpoint;
|
|
95
|
-
this.options = { ...DEFAULT_CLIENT_OPTIONS, ...options };
|
|
96
|
-
|
|
97
|
-
if (!this.options.WebSocketConstructor) {
|
|
98
|
-
throw new Error('Missing WebSocket constructor');
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (this.endpoint.startsWith('http')) {
|
|
102
|
-
this.endpoint = getWebsocketUrl(this.endpoint);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async makeRequest<T>(method: string, params: any[], signal?: AbortSignal): Promise<T> {
|
|
107
|
-
const webSocket = await this.#setupWebSocket();
|
|
108
|
-
|
|
109
|
-
return new Promise<Extract<JsonRpcMessage, { id: number }>>((resolve, reject) => {
|
|
110
|
-
this.#requestId += 1;
|
|
111
|
-
this.#pendingRequests.set(this.#requestId, {
|
|
112
|
-
resolve: resolve,
|
|
113
|
-
reject,
|
|
114
|
-
timeout: setTimeout(() => {
|
|
115
|
-
this.#pendingRequests.delete(this.#requestId);
|
|
116
|
-
reject(new Error(`Request timeout: ${method}`));
|
|
117
|
-
}, this.options.callTimeout),
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
signal?.addEventListener('abort', () => {
|
|
121
|
-
this.#pendingRequests.delete(this.#requestId);
|
|
122
|
-
reject(signal.reason);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
webSocket.send(JSON.stringify({ jsonrpc: '2.0', id: this.#requestId, method, params }));
|
|
126
|
-
}).then(({ error, result }) => {
|
|
127
|
-
if (error) {
|
|
128
|
-
throw new JsonRpcError(error.message, error.code);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return result as T;
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
#setupWebSocket() {
|
|
136
|
-
if (this.#connectionPromise) {
|
|
137
|
-
return this.#connectionPromise;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
this.#connectionPromise = new Promise<WebSocket>((resolve) => {
|
|
141
|
-
this.#webSocket?.close();
|
|
142
|
-
this.#webSocket = new this.options.WebSocketConstructor(this.endpoint);
|
|
143
|
-
|
|
144
|
-
this.#webSocket.addEventListener('open', () => {
|
|
145
|
-
this.#disconnects = 0;
|
|
146
|
-
resolve(this.#webSocket!);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
this.#webSocket.addEventListener('close', () => {
|
|
150
|
-
this.#disconnects++;
|
|
151
|
-
if (this.#disconnects <= this.options.maxReconnects) {
|
|
152
|
-
setTimeout(() => {
|
|
153
|
-
this.#reconnect();
|
|
154
|
-
}, this.options.reconnectTimeout);
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
this.#webSocket.addEventListener('message', ({ data }: { data: string }) => {
|
|
159
|
-
let json: JsonRpcMessage;
|
|
160
|
-
try {
|
|
161
|
-
json = JSON.parse(data) as JsonRpcMessage;
|
|
162
|
-
} catch (error) {
|
|
163
|
-
console.error(new Error(`Failed to parse RPC message: ${data}`, { cause: error }));
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if ('id' in json && json.id != null && this.#pendingRequests.has(json.id)) {
|
|
168
|
-
const { resolve, timeout } = this.#pendingRequests.get(json.id)!;
|
|
169
|
-
|
|
170
|
-
clearTimeout(timeout);
|
|
171
|
-
resolve(json);
|
|
172
|
-
} else if ('params' in json) {
|
|
173
|
-
const { params } = json;
|
|
174
|
-
this.#subscriptions.forEach((subscription) => {
|
|
175
|
-
if (subscription.subscriptionId === params.subscription)
|
|
176
|
-
if (params.subscription === subscription.subscriptionId) {
|
|
177
|
-
subscription.onMessage(params.result);
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
return this.#connectionPromise;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async #reconnect() {
|
|
188
|
-
this.#webSocket?.close();
|
|
189
|
-
this.#connectionPromise = null;
|
|
190
|
-
|
|
191
|
-
return Promise.allSettled(
|
|
192
|
-
[...this.#subscriptions].map((subscription) => subscription.subscribe(this)),
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
async subscribe<T>(input: SubscriptionRequest<T>) {
|
|
197
|
-
const subscription = new RpcSubscription(input);
|
|
198
|
-
this.#subscriptions.add(subscription);
|
|
199
|
-
await subscription.subscribe(this);
|
|
200
|
-
return () => subscription.unsubscribe(this);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
class RpcSubscription {
|
|
205
|
-
subscriptionId: number | null = null;
|
|
206
|
-
input: SubscriptionRequest<any>;
|
|
207
|
-
subscribed = false;
|
|
208
|
-
|
|
209
|
-
constructor(input: SubscriptionRequest) {
|
|
210
|
-
this.input = input;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
onMessage(message: unknown) {
|
|
214
|
-
if (this.subscribed) {
|
|
215
|
-
this.input.onMessage(message);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
async unsubscribe(client: WebsocketClient) {
|
|
220
|
-
const { subscriptionId } = this;
|
|
221
|
-
this.subscribed = false;
|
|
222
|
-
if (subscriptionId == null) return false;
|
|
223
|
-
this.subscriptionId = null;
|
|
224
|
-
|
|
225
|
-
return client.makeRequest(this.input.unsubscribe, [subscriptionId]);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async subscribe(client: WebsocketClient) {
|
|
229
|
-
this.subscriptionId = null;
|
|
230
|
-
this.subscribed = true;
|
|
231
|
-
const newSubscriptionId = await client.makeRequest<number>(
|
|
232
|
-
this.input.method,
|
|
233
|
-
this.input.params,
|
|
234
|
-
this.input.signal,
|
|
235
|
-
);
|
|
236
|
-
|
|
237
|
-
if (this.subscribed) {
|
|
238
|
-
this.subscriptionId = newSubscriptionId;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|