@mysten/sui 1.25.0 → 1.26.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.
Files changed (67) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/cjs/client/client.d.ts +12 -2
  3. package/dist/cjs/client/client.js +16 -4
  4. package/dist/cjs/client/client.js.map +2 -2
  5. package/dist/cjs/experimental/client.d.ts +8 -0
  6. package/dist/cjs/experimental/client.js +43 -0
  7. package/dist/cjs/experimental/client.js.map +7 -0
  8. package/dist/cjs/experimental/core.d.ts +14 -0
  9. package/dist/cjs/experimental/core.js +31 -0
  10. package/dist/cjs/experimental/core.js.map +7 -0
  11. package/dist/cjs/experimental/errors.d.ts +8 -0
  12. package/dist/cjs/experimental/errors.js +54 -0
  13. package/dist/cjs/experimental/errors.js.map +7 -0
  14. package/dist/cjs/experimental/transports/jsonRPC.d.ts +62 -0
  15. package/dist/cjs/experimental/transports/jsonRPC.js +255 -0
  16. package/dist/cjs/experimental/transports/jsonRPC.js.map +7 -0
  17. package/dist/cjs/experimental/types.d.ts +211 -0
  18. package/dist/cjs/experimental/types.js +17 -0
  19. package/dist/cjs/experimental/types.js.map +7 -0
  20. package/dist/cjs/transactions/executor/parallel.d.ts +1 -1
  21. package/dist/cjs/transactions/executor/parallel.js +4 -4
  22. package/dist/cjs/transactions/executor/parallel.js.map +2 -2
  23. package/dist/cjs/transactions/executor/serial.d.ts +1 -1
  24. package/dist/cjs/transactions/executor/serial.js +2 -2
  25. package/dist/cjs/transactions/executor/serial.js.map +2 -2
  26. package/dist/cjs/version.d.ts +2 -2
  27. package/dist/cjs/version.js +2 -2
  28. package/dist/cjs/version.js.map +1 -1
  29. package/dist/esm/client/client.d.ts +12 -2
  30. package/dist/esm/client/client.js +16 -4
  31. package/dist/esm/client/client.js.map +2 -2
  32. package/dist/esm/experimental/client.d.ts +8 -0
  33. package/dist/esm/experimental/client.js +23 -0
  34. package/dist/esm/experimental/client.js.map +7 -0
  35. package/dist/esm/experimental/core.d.ts +14 -0
  36. package/dist/esm/experimental/core.js +11 -0
  37. package/dist/esm/experimental/core.js.map +7 -0
  38. package/dist/esm/experimental/errors.d.ts +8 -0
  39. package/dist/esm/experimental/errors.js +34 -0
  40. package/dist/esm/experimental/errors.js.map +7 -0
  41. package/dist/esm/experimental/transports/jsonRPC.d.ts +62 -0
  42. package/dist/esm/experimental/transports/jsonRPC.js +235 -0
  43. package/dist/esm/experimental/transports/jsonRPC.js.map +7 -0
  44. package/dist/esm/experimental/types.d.ts +211 -0
  45. package/dist/esm/experimental/types.js +1 -0
  46. package/dist/esm/experimental/types.js.map +7 -0
  47. package/dist/esm/transactions/executor/parallel.d.ts +1 -1
  48. package/dist/esm/transactions/executor/parallel.js +4 -4
  49. package/dist/esm/transactions/executor/parallel.js.map +2 -2
  50. package/dist/esm/transactions/executor/serial.d.ts +1 -1
  51. package/dist/esm/transactions/executor/serial.js +2 -2
  52. package/dist/esm/transactions/executor/serial.js.map +2 -2
  53. package/dist/esm/version.d.ts +2 -2
  54. package/dist/esm/version.js +2 -2
  55. package/dist/esm/version.js.map +1 -1
  56. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  57. package/dist/tsconfig.tsbuildinfo +1 -1
  58. package/package.json +1 -1
  59. package/src/client/client.ts +21 -2
  60. package/src/experimental/client.ts +61 -0
  61. package/src/experimental/core.ts +46 -0
  62. package/src/experimental/errors.ts +37 -0
  63. package/src/experimental/transports/jsonRPC.ts +263 -0
  64. package/src/experimental/types.ts +293 -0
  65. package/src/transactions/executor/parallel.ts +8 -3
  66. package/src/transactions/executor/serial.ts +2 -1
  67. package/src/version.ts +2 -2
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "Mysten Labs <build@mystenlabs.com>",
4
4
  "description": "Sui TypeScript API(Work in Progress)",
5
5
  "homepage": "https://sdk.mystenlabs.com",
6
- "version": "1.25.0",
6
+ "version": "1.26.0",
7
7
  "license": "Apache-2.0",
8
8
  "sideEffects": false,
9
9
  "files": [
@@ -3,6 +3,12 @@
3
3
  import { fromBase58, toBase64, toHex } from '@mysten/bcs';
4
4
 
5
5
  import type { Signer } from '../cryptography/index.js';
6
+ import { Experimental_SuiClient } from '../experimental/client.js';
7
+ import { JSONRpcTransport } from '../experimental/transports/jsonRPC.js';
8
+ import type {
9
+ Experimental_SuiClientTypes,
10
+ SelfRegisteringClientExtension,
11
+ } from '../experimental/types.js';
6
12
  import type { Transaction } from '../transactions/index.js';
7
13
  import { isTransaction } from '../transactions/index.js';
8
14
  import {
@@ -106,7 +112,9 @@ export interface OrderArguments {
106
112
  * Configuration options for the SuiClient
107
113
  * You must provide either a `url` or a `transport`
108
114
  */
109
- export type SuiClientOptions = NetworkOrTransport;
115
+ export type SuiClientOptions = NetworkOrTransport & {
116
+ network?: Experimental_SuiClientTypes.Network;
117
+ };
110
118
 
111
119
  type NetworkOrTransport =
112
120
  | {
@@ -126,7 +134,8 @@ export function isSuiClient(client: unknown): client is SuiClient {
126
134
  );
127
135
  }
128
136
 
129
- export class SuiClient {
137
+ export class SuiClient extends Experimental_SuiClient implements SelfRegisteringClientExtension {
138
+ core: JSONRpcTransport = new JSONRpcTransport(this);
130
139
  protected transport: SuiTransport;
131
140
 
132
141
  get [SUI_CLIENT_BRAND]() {
@@ -139,6 +148,7 @@ export class SuiClient {
139
148
  * @param options configuration options for the API Client
140
149
  */
141
150
  constructor(options: SuiClientOptions) {
151
+ super({ network: options.network ?? 'unknown' });
142
152
  this.transport = options.transport ?? new SuiHTTPTransport({ url: options.url });
143
153
  }
144
154
 
@@ -826,4 +836,13 @@ export class SuiClient {
826
836
  // This should never happen, because the above case should always throw, but just adding it in the event that something goes horribly wrong.
827
837
  throw new Error('Unexpected error while waiting for transaction block.');
828
838
  }
839
+
840
+ experimental_asClientExtension(this: SuiClient) {
841
+ return {
842
+ name: 'jsonRPC',
843
+ register: () => {
844
+ return this;
845
+ },
846
+ } as const;
847
+ }
829
848
  }
@@ -0,0 +1,61 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /* eslint-disable @typescript-eslint/ban-types */
4
+
5
+ import type { Experimental_CoreClient } from './core.js';
6
+ import type {
7
+ ClientWithExtensions,
8
+ Experimental_SuiClientTypes,
9
+ Simplify,
10
+ SuiClientRegistration,
11
+ UnionToIntersection,
12
+ } from './types.js';
13
+
14
+ export abstract class Experimental_SuiClient {
15
+ network: Experimental_SuiClientTypes.Network;
16
+
17
+ constructor({ network }: Experimental_SuiClientTypes.SuiClientOptions) {
18
+ this.network = network;
19
+ }
20
+
21
+ abstract core: Experimental_CoreClient;
22
+
23
+ $extend<const Registrations extends SuiClientRegistration<this>[]>(
24
+ ...registrations: Registrations
25
+ ) {
26
+ return Object.create(
27
+ this,
28
+ Object.fromEntries(
29
+ registrations.map((registration) => {
30
+ if ('experimental_asClientExtension' in registration) {
31
+ const { name, register } = registration.experimental_asClientExtension();
32
+ return [name, { value: register(this) }];
33
+ }
34
+ return [registration.name, { value: registration.register(this) }];
35
+ }),
36
+ ),
37
+ ) as ClientWithExtensions<
38
+ Simplify<
39
+ Omit<
40
+ {
41
+ [K in keyof this]: this[K];
42
+ },
43
+ keyof Experimental_SuiClient
44
+ > &
45
+ UnionToIntersection<
46
+ {
47
+ [K in keyof Registrations]: Registrations[K] extends SuiClientRegistration<
48
+ this,
49
+ infer Name extends string,
50
+ infer Extension
51
+ >
52
+ ? {
53
+ [K2 in Name]: Extension;
54
+ }
55
+ : never;
56
+ }[number]
57
+ >
58
+ >
59
+ >;
60
+ }
61
+ }
@@ -0,0 +1,46 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Experimental_SuiClient } from './client.js';
5
+ import type { Experimental_SuiClientTypes } from './types.js';
6
+
7
+ export abstract class Experimental_CoreClient
8
+ extends Experimental_SuiClient
9
+ implements Experimental_SuiClientTypes.TransportMethods
10
+ {
11
+ core = this;
12
+
13
+ abstract getObjects(
14
+ options: Experimental_SuiClientTypes.GetObjectsOptions,
15
+ ): Promise<Experimental_SuiClientTypes.GetObjectsResponse>;
16
+
17
+ abstract getCoins(
18
+ options: Experimental_SuiClientTypes.GetCoinsOptions,
19
+ ): Promise<Experimental_SuiClientTypes.GetCoinsResponse>;
20
+
21
+ abstract getOwnedObjects(
22
+ options: Experimental_SuiClientTypes.GetOwnedObjectsOptions,
23
+ ): Promise<Experimental_SuiClientTypes.GetOwnedObjectsResponse>;
24
+
25
+ abstract getBalance(
26
+ options: Experimental_SuiClientTypes.GetBalanceOptions,
27
+ ): Promise<Experimental_SuiClientTypes.GetBalanceResponse>;
28
+
29
+ abstract getAllBalances(
30
+ options: Experimental_SuiClientTypes.GetAllBalancesOptions,
31
+ ): Promise<Experimental_SuiClientTypes.GetAllBalancesResponse>;
32
+
33
+ abstract getTransaction(
34
+ options: Experimental_SuiClientTypes.GetTransactionOptions,
35
+ ): Promise<Experimental_SuiClientTypes.GetTransactionResponse>;
36
+
37
+ abstract executeTransaction(
38
+ options: Experimental_SuiClientTypes.ExecuteTransactionOptions,
39
+ ): Promise<Experimental_SuiClientTypes.ExecuteTransactionResponse>;
40
+
41
+ abstract dryRunTransaction(
42
+ options: Experimental_SuiClientTypes.DryRunTransactionOptions,
43
+ ): Promise<Experimental_SuiClientTypes.DryRunTransactionResponse>;
44
+
45
+ abstract getReferenceGasPrice(): Promise<Experimental_SuiClientTypes.GetReferenceGasPriceResponse>;
46
+ }
@@ -0,0 +1,37 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { ObjectResponseError } from '../client/index.js';
5
+
6
+ export class SuiClientError extends Error {}
7
+
8
+ export class ObjectError extends SuiClientError {
9
+ code: string;
10
+
11
+ constructor(code: string, message: string) {
12
+ super(message);
13
+ this.code = code;
14
+ }
15
+
16
+ static fromResponse(response: ObjectResponseError, objectId?: string): ObjectError {
17
+ switch (response.code) {
18
+ case 'notExists':
19
+ return new ObjectError(response.code, `Object ${response.object_id} does not exist`);
20
+ case 'dynamicFieldNotFound':
21
+ return new ObjectError(
22
+ response.code,
23
+ `Dynamic field not found for object ${response.parent_object_id}`,
24
+ );
25
+ case 'deleted':
26
+ return new ObjectError(response.code, `Object ${response.object_id} has been deleted`);
27
+ case 'displayError':
28
+ return new ObjectError(response.code, `Display error: ${response.error}`);
29
+ case 'unknown':
30
+ default:
31
+ return new ObjectError(
32
+ response.code,
33
+ `Unknown error while loading object${objectId ? ` ${objectId}` : ''}`,
34
+ );
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,263 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { fromBase64 } from '@mysten/bcs';
5
+
6
+ import { bcs } from '../../bcs/index.js';
7
+ import type {
8
+ ObjectOwner,
9
+ SuiClient,
10
+ SuiObjectData,
11
+ SuiTransactionBlockResponse,
12
+ } from '../../client/index.js';
13
+ import { batch } from '../../transactions/plugins/utils.js';
14
+ import { Transaction } from '../../transactions/Transaction.js';
15
+ import { Experimental_CoreClient } from '../core.js';
16
+ import { ObjectError } from '../errors.js';
17
+ import type { Experimental_SuiClientTypes } from '../types.js';
18
+
19
+ export class JSONRpcTransport extends Experimental_CoreClient {
20
+ #jsonRpcClient: SuiClient;
21
+
22
+ constructor(jsonRpcClient: SuiClient) {
23
+ super({ network: jsonRpcClient.network });
24
+ this.#jsonRpcClient = jsonRpcClient;
25
+ }
26
+
27
+ async getObjects(options: Experimental_SuiClientTypes.GetObjectsOptions) {
28
+ const batches = batch(options.objectIds, 50);
29
+ const results: Experimental_SuiClientTypes.GetObjectsResponse['objects'] = [];
30
+
31
+ for (const batch of batches) {
32
+ const objects = await this.#jsonRpcClient.multiGetObjects({
33
+ ids: batch,
34
+ options: {
35
+ showOwner: true,
36
+ showType: true,
37
+ },
38
+ });
39
+
40
+ for (const [idx, object] of objects.entries()) {
41
+ if (object.error) {
42
+ results.push(ObjectError.fromResponse(object.error, batch[idx]));
43
+ } else {
44
+ results.push(parseObject(object.data!));
45
+ }
46
+ }
47
+ }
48
+
49
+ return {
50
+ objects: results,
51
+ };
52
+ }
53
+ async getOwnedObjects(options: Experimental_SuiClientTypes.GetOwnedObjectsOptions) {
54
+ const objects = await this.#jsonRpcClient.getOwnedObjects({
55
+ owner: options.address,
56
+ limit: options.limit,
57
+ cursor: options.cursor,
58
+ });
59
+
60
+ return {
61
+ objects: objects.data.map((result) => {
62
+ if (result.error) {
63
+ throw ObjectError.fromResponse(result.error);
64
+ }
65
+
66
+ return parseObject(result.data!);
67
+ }),
68
+ hasNextPage: objects.hasNextPage,
69
+ cursor: objects.nextCursor ?? null,
70
+ };
71
+ }
72
+
73
+ async getCoins(options: Experimental_SuiClientTypes.GetCoinsOptions) {
74
+ const coins = await this.#jsonRpcClient.getCoins({
75
+ owner: options.address,
76
+ coinType: options.coinType,
77
+ });
78
+
79
+ return {
80
+ objects: coins.data.map((coin) => {
81
+ return {
82
+ id: coin.coinObjectId,
83
+ version: coin.version,
84
+ digest: coin.digest,
85
+ balance: BigInt(coin.balance),
86
+ type: `0x2::coin::Coin<${coin.coinType}>`,
87
+ content: Coin.serialize({
88
+ id: coin.coinObjectId,
89
+ balance: {
90
+ value: coin.balance,
91
+ },
92
+ }).toBytes(),
93
+ owner: {
94
+ $kind: 'ObjectOwner' as const,
95
+ ObjectOwner: options.address,
96
+ },
97
+ };
98
+ }),
99
+ hasNextPage: coins.hasNextPage,
100
+ cursor: coins.nextCursor ?? null,
101
+ };
102
+ }
103
+
104
+ async getBalance(options: Experimental_SuiClientTypes.GetBalanceOptions) {
105
+ const balance = await this.#jsonRpcClient.getBalance({
106
+ owner: options.address,
107
+ coinType: options.coinType,
108
+ });
109
+
110
+ return {
111
+ balance: {
112
+ coinType: balance.coinType,
113
+ balance: BigInt(balance.totalBalance),
114
+ },
115
+ };
116
+ }
117
+ async getAllBalances(options: Experimental_SuiClientTypes.GetAllBalancesOptions) {
118
+ const balances = await this.#jsonRpcClient.getAllBalances({
119
+ owner: options.address,
120
+ });
121
+
122
+ return {
123
+ balances: balances.map((balance) => ({
124
+ coinType: balance.coinType,
125
+ balance: BigInt(balance.totalBalance),
126
+ })),
127
+ hasNextPage: false,
128
+ cursor: null,
129
+ };
130
+ }
131
+ async getTransaction(options: Experimental_SuiClientTypes.GetTransactionOptions) {
132
+ const transaction = await this.#jsonRpcClient.getTransactionBlock({
133
+ digest: options.digest,
134
+ options: {
135
+ showRawInput: true,
136
+ showObjectChanges: true,
137
+ showRawEffects: true,
138
+ showEvents: true,
139
+ },
140
+ });
141
+
142
+ return {
143
+ transaction: parseTransaction(transaction),
144
+ };
145
+ }
146
+ async executeTransaction(options: Experimental_SuiClientTypes.ExecuteTransactionOptions) {
147
+ const transaction = await this.#jsonRpcClient.executeTransactionBlock({
148
+ transactionBlock: options.transaction,
149
+ signature: options.signatures,
150
+ options: {
151
+ showEffects: true,
152
+ showEvents: true,
153
+ },
154
+ });
155
+
156
+ return {
157
+ transaction: parseTransaction(transaction),
158
+ };
159
+ }
160
+ async dryRunTransaction(options: Experimental_SuiClientTypes.DryRunTransactionOptions) {
161
+ const tx = Transaction.from(options.transaction);
162
+ const result = await this.#jsonRpcClient.dryRunTransactionBlock({
163
+ transactionBlock: options.transaction,
164
+ });
165
+
166
+ return {
167
+ transaction: {
168
+ digest: await tx.getDigest(),
169
+ // TODO: Effects aren't returned as bcs from dryRun, once we define structured effects we can return those instead
170
+ effects: result.effects as never,
171
+ signatures: [],
172
+ bcs: options.transaction,
173
+ },
174
+ };
175
+ }
176
+ async getReferenceGasPrice() {
177
+ const referenceGasPrice = await this.#jsonRpcClient.getReferenceGasPrice();
178
+ return {
179
+ referenceGasPrice,
180
+ };
181
+ }
182
+ }
183
+
184
+ function parseObject(object: SuiObjectData): Experimental_SuiClientTypes.ObjectResponse {
185
+ return {
186
+ id: object.objectId,
187
+ version: object.version,
188
+ digest: object.digest,
189
+ type: object.type!,
190
+ content:
191
+ object.bcs?.dataType === 'moveObject' ? fromBase64(object.bcs.bcsBytes) : new Uint8Array(),
192
+ owner: parseOwner(object.owner!),
193
+ };
194
+ }
195
+
196
+ function parseOwner(owner: ObjectOwner): Experimental_SuiClientTypes.ObjectOwner {
197
+ if (owner === 'Immutable') {
198
+ return {
199
+ $kind: 'Immutable',
200
+ Immutable: true,
201
+ };
202
+ }
203
+
204
+ if ('ConsensusV2' in owner) {
205
+ return {
206
+ $kind: 'ConsensusV2',
207
+ ConsensusV2Owner: {
208
+ authenticator: {
209
+ $kind: 'SingleOwner',
210
+ SingleOwner: owner.ConsensusV2.authenticator.SingleOwner,
211
+ },
212
+ startVersion: owner.ConsensusV2.start_version,
213
+ },
214
+ };
215
+ }
216
+
217
+ if ('AddressOwner' in owner) {
218
+ return {
219
+ $kind: 'AddressOwner',
220
+ AddressOwner: owner.AddressOwner,
221
+ };
222
+ }
223
+
224
+ if ('ObjectOwner' in owner) {
225
+ return {
226
+ $kind: 'ObjectOwner',
227
+ ObjectOwner: owner.ObjectOwner,
228
+ };
229
+ }
230
+
231
+ if ('Shared' in owner) {
232
+ return {
233
+ $kind: 'Shared',
234
+ Shared: {
235
+ initialSharedVersion: owner.Shared.initial_shared_version,
236
+ },
237
+ };
238
+ }
239
+
240
+ throw new Error(`Unknown owner type: ${JSON.stringify(owner)}`);
241
+ }
242
+
243
+ function parseTransaction(
244
+ transaction: SuiTransactionBlockResponse,
245
+ ): Experimental_SuiClientTypes.TransactionResponse {
246
+ const parsedTx = bcs.SenderSignedData.parse(fromBase64(transaction.rawTransaction!))[0];
247
+
248
+ return {
249
+ digest: transaction.digest,
250
+ effects: new Uint8Array(transaction.rawEffects!),
251
+ bcs: bcs.TransactionData.serialize(parsedTx.intentMessage.value).toBytes(),
252
+ signatures: parsedTx.txSignatures,
253
+ };
254
+ }
255
+
256
+ const Balance = bcs.struct('Balance', {
257
+ value: bcs.u64(),
258
+ });
259
+
260
+ const Coin = bcs.struct('Coin', {
261
+ id: bcs.Address,
262
+ balance: Balance,
263
+ });