@sovereign-sdk/web3 0.0.12 → 0.0.14

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/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # @sovereign-sdk/web3
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@sovereign-sdk/web3.svg)](https://www.npmjs.com/package/@sovereign-sdk/web3)
4
+ [![CI](https://github.com/Sovereign-Labs/sovereign-sdk-web3-js/actions/workflows/ci.yaml/badge.svg)](https://github.com/Sovereign-Labs/sovereign-sdk-web3-js/actions/workflows/ci.yaml)
5
+
6
+ A TypeScript client library for interacting with Sovereign SDK rollups.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @sovereign-sdk/web3
12
+ ```
13
+
14
+ ## Features
15
+
16
+ - 🔄 Type-safe transaction submission and signing
17
+ - 🔍 Runtime call simulation for gas estimation
18
+ - 📦 Borsh serialization with schema validation
19
+ - 🎯 Strongly typed rollup interactions with customizable type specifications
20
+ - 🌐 Full access to ledger, sequencer, and rollup APIs
21
+
22
+ ## Usage
23
+
24
+ ### Basic Setup
25
+
26
+ ```typescript
27
+ import { StandardRollup } from '@sovereign-sdk/web3';
28
+ import { newMetaMaskSnapSigner } from '@sovereign-sdk/signers';
29
+
30
+ // Initialize the rollup client
31
+ const rollup = new StandardRollup({
32
+ url: 'https://your-rollup-node.com',
33
+ schema: yourSchema,
34
+ defaultTxDetails: {
35
+ max_priority_fee_bips: 1000,
36
+ max_fee: 1000000,
37
+ chain_id: 1,
38
+ },
39
+ });
40
+
41
+ // Initialize a signer
42
+ const signer = newMetaMaskSnapSigner({
43
+ curve: 'ed25519',
44
+ schema: yourSchema,
45
+ snapId: 'npm:@sovereign-sdk/metamask-snap',
46
+ });
47
+ ```
48
+
49
+ ### Submitting Transactions
50
+
51
+ ```typescript
52
+ // Submit a runtime call
53
+ const result = await rollup.call(
54
+ {
55
+ value_setter: {
56
+ set_value: 100,
57
+ },
58
+ },
59
+ { signer }
60
+ );
61
+ ```
62
+
63
+ ### Type Safety
64
+
65
+ ```typescript
66
+ type MyRollupTypes = RollupTypeSpec<{
67
+ RuntimeCall: {
68
+ value_setter: {
69
+ set_value: number;
70
+ };
71
+ };
72
+ // Add other type specifications as needed
73
+ }>;
74
+
75
+ const typedRollup = new StandardRollup<MyRollupTypes>({
76
+ // ... config
77
+ });
78
+
79
+ // Now your calls will be type-checked!
80
+ const result = await typedRollup.call(
81
+ {
82
+ value_setter: {
83
+ set_value: 100, // Type checked and automatically serialized to Borsh bytes
84
+ },
85
+ },
86
+ { signer }
87
+ );
88
+ ```
89
+
90
+ ### Simulation
91
+
92
+ ```typescript
93
+ // Simulate a call to estimate gas
94
+ const simulation = await rollup.simulate(
95
+ {
96
+ value_setter: {
97
+ set_value: 100,
98
+ },
99
+ },
100
+ {
101
+ signer,
102
+ txDetails: {
103
+ max_priority_fee_bips: 1000,
104
+ max_fee: 1000000,
105
+ chain_id: 1,
106
+ },
107
+ }
108
+ );
109
+ ```
110
+
111
+ ## API Reference
112
+
113
+ The package exports the following main components:
114
+
115
+ - `StandardRollup`: The main client class for interacting with rollups
116
+ - `RollupTypeSpec`: Type utility for creating type-safe rollup interactions
117
+ - `createSerializer`: Function to create a Borsh serializer for your rollup schema
118
+
119
+ For detailed API documentation, please refer to the inline TypeScript documentation in the source code.
package/dist/index.d.mts CHANGED
@@ -2,68 +2,372 @@ import SovereignClient__default from '@sovereign-sdk/client';
2
2
  import * as SovereignClient from '@sovereign-sdk/client';
3
3
  export { SovereignClient };
4
4
  import { Signer } from '@sovereign-sdk/signers';
5
+ export { Schema } from '@sovereign-sdk/universal-wallet-wasm';
5
6
 
7
+ /**
8
+ * A rollup schema is a description of the types that are utilized in the rollup.
9
+ * It is used to serialize and deserialize the types.
10
+ * As well as display them in a human-readable way that is verified by the rollup.
11
+ */
6
12
  type RollupSchema = object;
13
+ /**
14
+ * A serializer is used to serialize rollup types to Borsh bytes.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const serializer = createSerializer(yourSchema);
19
+ * const runtimeCall = {
20
+ * value_setter: {
21
+ * set_value: 100,
22
+ * },
23
+ * };
24
+ * const borshBytes = serializer.serializeRuntimeCall(runtimeCall);
25
+ * ```
26
+ */
7
27
  type RollupSerializer = {
28
+ /**
29
+ * Serialize an input to Borsh bytes.
30
+ *
31
+ * Treats `Uint8Array` as a plain array of numbers as this is the format used by universal-wallet-wasm
32
+ * to represent Borsh bytes.
33
+ *
34
+ * @param input - The input to serialize.
35
+ * @param index - The index of the type in the schema to serialize.
36
+ * @returns The serialized Borsh bytes.
37
+ */
8
38
  serialize(input: unknown, index: number): Uint8Array;
39
+ /**
40
+ * Serialize a runtime call to Borsh bytes.
41
+ *
42
+ * @param input - The runtime call to serialize.
43
+ * @returns The serialized Borsh bytes.
44
+ */
9
45
  serializeRuntimeCall(input: unknown): Uint8Array;
46
+ /**
47
+ * Serialize an unsigned transaction to Borsh bytes.
48
+ *
49
+ * @param input - The unsigned transaction to serialize.
50
+ * @returns The serialized Borsh bytes.
51
+ */
10
52
  serializeUnsignedTx(input: unknown): Uint8Array;
53
+ /**
54
+ * Serialize a transaction to Borsh bytes.
55
+ *
56
+ * @param input - The transaction to serialize.
57
+ * @returns The serialized Borsh bytes.
58
+ */
11
59
  serializeTx(input: unknown): Uint8Array;
12
60
  };
61
+ /**
62
+ * Create a serializer for a rollup schema.
63
+ *
64
+ * @throws {@link SovereignError} - If the schema is invalid.
65
+ * @param schemaObject - The rollup schema.
66
+ * @returns The serializer.
67
+ */
13
68
  declare function createSerializer(schemaObject: RollupSchema): RollupSerializer;
14
69
 
70
+ /**
71
+ * The base type specification for a rollup.
72
+ *
73
+ * Defines a centralised set of types that are used in the rollup.
74
+ * This allows for a single source of truth for the types used in the rollup.
75
+ */
15
76
  type BaseTypeSpec = {
77
+ /**
78
+ * The type of transaction used in the rollup.
79
+ */
16
80
  Transaction: any;
81
+ /**
82
+ * The type of unsigned transaction used in the rollup.
83
+ */
17
84
  UnsignedTransaction: any;
85
+ /**
86
+ * The type of runtime call used in the rollup.
87
+ */
18
88
  RuntimeCall: any;
89
+ /**
90
+ * The type of the dedup data used to deduplicate transactions.
91
+ */
92
+ Dedup: any;
19
93
  };
20
94
  type OverrideType<Base, Override> = {
21
95
  [K in keyof Base]: K extends keyof Override ? Override[K] : Base[K];
22
96
  };
97
+ /**
98
+ * The type specification for a rollup.
99
+ *
100
+ * Extends the base type specification with concrete types.
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * type TxDetails = {
105
+ * maxPriorityFeeBips: number;
106
+ * maxFee: number;
107
+ * gasLimit?: bigint;
108
+ * chainId: number;
109
+ * };
110
+ *
111
+ * type UnsignedTransaction = {
112
+ * runtimeMsg: Uint8Array;
113
+ * nonce: number;
114
+ * details: TxDetails;
115
+ * };
116
+ *
117
+ * type Transaction = {
118
+ * pubKey: string;
119
+ * signature: string;
120
+ * } & UnsignedTransaction;
121
+ *
122
+ * type MyTypeSpec = RollupTypeSpec<{
123
+ * UnsignedTransaction: UnsignedTransaction,
124
+ * Transaction: Transaction,
125
+ * // ...
126
+ * }>;
127
+ *
128
+ * // The typespec can now be used with the rollup class to provide type safety
129
+ * // for the transaction types.
130
+ * const rollup = new StandardRollup<MyTypeSpec>({
131
+ * schema: yourSchema,
132
+ * // ...
133
+ * });
134
+ * const result = await rollup.signAndSubmitTransaction({
135
+ * // ...
136
+ * });
137
+ * const tx = result.transaction; // Strongly typed as `Transaction` on your RollupTypeSpec.
138
+ * ```
139
+ *
140
+ * @param T - The type specification to extend the base type specification with.
141
+ */
23
142
  type RollupTypeSpec<T extends Partial<BaseTypeSpec>> = OverrideType<BaseTypeSpec, T>;
24
143
 
25
- type TransactionResult<Tx> = {
26
- response: SovereignClient__default.Sequencer.TxCreateResponse;
27
- transaction: Tx;
144
+ type DeepPartial<T> = T extends unknown[] ? T : T extends Record<string, unknown> ? {
145
+ [P in keyof T]?: DeepPartial<T[P]>;
146
+ } : T;
147
+
148
+ type UnsignedTransactionContext<S extends BaseTypeSpec, C extends RollupContext> = {
149
+ runtimeCall: S["RuntimeCall"];
150
+ sender: Uint8Array;
151
+ overrides: DeepPartial<S["UnsignedTransaction"]>;
152
+ rollup: Rollup<S, C>;
153
+ };
154
+ type TransactionContext<S extends BaseTypeSpec, C extends RollupContext> = {
155
+ unsignedTx: S["UnsignedTransaction"];
156
+ sender: Uint8Array;
157
+ signature: Uint8Array;
158
+ rollup: Rollup<S, C>;
28
159
  };
29
- type RollupConfig = {
160
+ type TypeBuilder<S extends BaseTypeSpec, C extends RollupContext> = {
161
+ unsignedTransaction: (context: UnsignedTransactionContext<S, C>) => Promise<S["UnsignedTransaction"]>;
162
+ transaction: (context: TransactionContext<S, C>) => Promise<S["Transaction"]>;
163
+ };
164
+ /**
165
+ * Arbitrary context that is associated with the rollup.
166
+ */
167
+ type RollupContext = Record<string, unknown>;
168
+ /**
169
+ * The configuration for a rollup client.
170
+ */
171
+ type RollupConfig<C extends RollupContext> = {
172
+ /**
173
+ * The base URL of the rollup full node API.
174
+ */
30
175
  url?: string;
31
- schema: RollupSchema;
32
- defaultTxDetails: TxDetails;
176
+ /**
177
+ * The Sovereign SDK client to use for the rollup.
178
+ * If not provided, the default client will be used using {@link RollupConfig.url}.
179
+ */
180
+ client?: SovereignClient__default;
181
+ /**
182
+ * The schema of the rollup.
183
+ */
184
+ schema?: RollupSchema;
185
+ /**
186
+ * The serializer to use for the rollup.
187
+ * If not provided, a serializer will be created using the provided {@link RollupConfig.schema}.
188
+ */
189
+ serializer?: RollupSerializer;
190
+ /**
191
+ * Arbitrary context that is associated with the rollup.
192
+ */
193
+ context: C;
33
194
  };
34
- type TxDetails = {
35
- max_priority_fee_bips: number;
36
- max_fee: number;
37
- gas_limit?: number[];
38
- chain_id: number;
195
+ /**
196
+ * The result of signing and submitting a transaction.
197
+ *
198
+ * @template Tx The type of the transaction
199
+ */
200
+ type TransactionResult<Tx> = {
201
+ /**
202
+ * The response from the sequencer.
203
+ */
204
+ response: SovereignClient__default.Sequencer.TxCreateResponse;
205
+ /**
206
+ * The transaction that was submitted.
207
+ */
208
+ transaction: Tx;
39
209
  };
210
+ /**
211
+ * The parameters for signing and submitting a transaction.
212
+ */
40
213
  type SignerParams = {
214
+ /**
215
+ * The signer to use for signing the transaction.
216
+ */
41
217
  signer: Signer;
42
218
  };
43
- type CallParams = {
44
- txDetails?: TxDetails;
45
- } & SignerParams;
46
- type SimulateParams = {
47
- txDetails: TxDetails;
219
+ /**
220
+ * The parameters for calling executing a runtime call transaction.
221
+ */
222
+ type CallParams<S extends BaseTypeSpec> = {
223
+ overrides?: DeepPartial<S["UnsignedTransaction"]>;
48
224
  } & SignerParams;
49
- declare class StandardRollup<T extends BaseTypeSpec = BaseTypeSpec> {
225
+ /**
226
+ * A generic rollup client.
227
+ *
228
+ * @template S - The type specification for the rollup.
229
+ * If not provided, the base type specification will be used which uses `any` for all types.
230
+ * @template C - The context for the rollup.
231
+ */
232
+ declare class Rollup<S extends BaseTypeSpec, C extends RollupContext> {
50
233
  private readonly _config;
51
234
  private readonly _client;
52
235
  private readonly _serializer;
53
- constructor(config: RollupConfig);
54
- submitTransaction(transaction: T["Transaction"]): Promise<SovereignClient__default.Sequencer.TxCreateResponse>;
55
- signAndSubmitTransaction(unsignedTx: T["UnsignedTransaction"], { signer }: SignerParams): Promise<TransactionResult<T["Transaction"]>>;
56
- call(runtimeMessage: T["RuntimeCall"], { signer, txDetails }: CallParams): Promise<TransactionResult<T["Transaction"]>>;
57
- simulate(runtimeMessage: T["RuntimeCall"], { signer, txDetails }: SimulateParams): Promise<SovereignClient__default.Rollup.SimulateExecutionResponse>;
236
+ private readonly _typeBuilder;
237
+ /**
238
+ * Creates a new rollup client.
239
+ *
240
+ * @throws {@link InvalidRollupConfigError} - If no schema or serializer is provided.
241
+ *
242
+ * @param config - The configuration for the rollup client.
243
+ * @param typeBuilder - The type builder for the rollup.
244
+ */
245
+ constructor(config: RollupConfig<C>, typeBuilder: TypeBuilder<S, C>);
246
+ /**
247
+ * Retrieve dedup information about the provided address.
248
+ *
249
+ * @param address - The public key to dedup.
250
+ */
251
+ dedup(address: Uint8Array): Promise<S["Dedup"]>;
252
+ /**
253
+ * Submits a batch to the rollup.
254
+ *
255
+ * @param batch - The batch of transactions to submit.
256
+ */
257
+ submitBatch(batch: S["Transaction"][]): Promise<SovereignClient__default.Sequencer.BatchCreateResponse>;
258
+ /**
259
+ * Submits a transaction to the rollup.
260
+ *
261
+ * @param transaction - The transaction to submit.
262
+ */
263
+ submitTransaction(transaction: S["Transaction"]): Promise<SovereignClient__default.Sequencer.TxCreateResponse>;
264
+ /**
265
+ * Signs and submits a transaction to the rollup.
266
+ * Utilizes the provided signer to sign the transaction.
267
+ *
268
+ * @param unsignedTx - The unsigned transaction to sign and submit.
269
+ * @param {SignerParams} params - The params for signing and submitting the transaction.
270
+ */
271
+ signAndSubmitTransaction(unsignedTx: S["UnsignedTransaction"], { signer }: SignerParams): Promise<TransactionResult<S["Transaction"]>>;
272
+ /**
273
+ * Performs a runtime call transaction.
274
+ * Utilizes the provided signer to sign the transaction.
275
+ *
276
+ * @param runtimeCall - The runtime message to call.
277
+ * @param {CallParams<S>} params - The params for submitting a runtime call transaction.
278
+ */
279
+ call(runtimeCall: S["RuntimeCall"], { signer, overrides }: CallParams<S>): Promise<TransactionResult<S["Transaction"]>>;
280
+ /**
281
+ * A ledger client that can be used to query the ledger.
282
+ */
58
283
  get ledger(): SovereignClient__default.Ledger;
284
+ /**
285
+ * A sequencer client that can be used to submit transactions & batches to the rollup.
286
+ */
59
287
  get sequencer(): SovereignClient__default.Sequencer;
288
+ /**
289
+ * A rollup client that can be used to perform operations on the rollup.
290
+ */
60
291
  get rollup(): SovereignClient__default.Rollup;
292
+ /**
293
+ * A client that can be used to perform HTTP requests to the Sovereign API.
294
+ * This can be used as an escape hatch to execute arbitrary requests to the Sovereign API
295
+ * if the operation is not supported by `ledger`, `sequencer`, or `rollup` clients.
296
+ */
61
297
  get http(): SovereignClient__default;
298
+ /**
299
+ * The serializer for the rollup.
300
+ * Can be used to serialize transactions, runtime calls, etc.
301
+ * See {@link RollupSerializer} for more information.
302
+ */
62
303
  get serializer(): RollupSerializer;
304
+ /**
305
+ * The context for the rollup.
306
+ */
307
+ get context(): C;
63
308
  }
64
309
 
310
+ type TxDetails = {
311
+ max_priority_fee_bips: number;
312
+ max_fee: number;
313
+ gas_limit?: number[];
314
+ chain_id: number;
315
+ };
316
+ type UnsignedTransaction = {
317
+ runtime_msg: Uint8Array;
318
+ nonce: number;
319
+ details: TxDetails;
320
+ };
321
+ type Transaction = {
322
+ pub_key: {
323
+ pub_key: Uint8Array;
324
+ };
325
+ signature: {
326
+ msg_sig: Uint8Array;
327
+ };
328
+ } & UnsignedTransaction;
329
+ type Dedup = {
330
+ nonce: number;
331
+ };
332
+ type StandardRollupContext = {
333
+ defaultTxDetails: TxDetails;
334
+ };
335
+ type StandardRollupSpec<RuntimeCall> = {
336
+ UnsignedTransaction: UnsignedTransaction;
337
+ Transaction: Transaction;
338
+ RuntimeCall: RuntimeCall;
339
+ Dedup: Dedup;
340
+ };
341
+ declare function standardTypeBuilder<S extends StandardRollupSpec<unknown>>(): TypeBuilder<S, StandardRollupContext>;
342
+ /**
343
+ * The parameters for simulating a runtime call transaction.
344
+ */
345
+ type SimulateParams = {
346
+ /**
347
+ * The transaction details to use for the simulation.
348
+ */
349
+ txDetails: TxDetails;
350
+ nonce?: number;
351
+ } & SignerParams;
352
+ declare class StandardRollup<RuntimeCall> extends Rollup<StandardRollupSpec<RuntimeCall>, StandardRollupContext> {
353
+ /**
354
+ * Simulates a runtime call transaction.
355
+ *
356
+ * This method can be useful to estimate the gas cost of a runtime call transaction.
357
+ *
358
+ * @param runtimeMessage - The runtime message to call.
359
+ */
360
+ simulate(runtimeMessage: StandardRollupSpec<RuntimeCall>["RuntimeCall"], { signer, txDetails, nonce: overrideNonce }: SimulateParams): Promise<SovereignClient__default.Rollup.SimulateExecutionResponse>;
361
+ }
362
+ declare function createStandardRollup<RuntimeCall, C extends StandardRollupContext = StandardRollupContext>(config: RollupConfig<C>, typeBuilderOverrides?: Partial<TypeBuilder<StandardRollupSpec<RuntimeCall>, C>>): StandardRollup<RuntimeCall>;
363
+
364
+ /**
365
+ * Base error class for Sovereign SDK Web3.
366
+ */
65
367
  declare class SovereignError extends Error {
66
368
  constructor(message: string);
67
369
  }
370
+ declare class InvalidRollupConfigError extends SovereignError {
371
+ }
68
372
 
69
- export { type BaseTypeSpec, type RollupConfig, type RollupSchema, type RollupSerializer, type RollupTypeSpec, SovereignError, StandardRollup, type TransactionResult, createSerializer };
373
+ export { type BaseTypeSpec, type CallParams, type Dedup, InvalidRollupConfigError, Rollup, type RollupConfig, type RollupContext, type RollupSchema, type RollupSerializer, type RollupTypeSpec, type SignerParams, type SimulateParams, SovereignError, StandardRollup, type StandardRollupContext, type StandardRollupSpec, type Transaction, type TransactionContext, type TransactionResult, type TxDetails, type TypeBuilder, type UnsignedTransaction, type UnsignedTransactionContext, createSerializer, createStandardRollup, standardTypeBuilder };