@apibara/starknet 2.1.0-beta.23 → 2.1.0-beta.25

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/src/block.ts CHANGED
@@ -1,7 +1,19 @@
1
- import { Schema } from "@effect/schema";
2
-
1
+ import {
2
+ ArrayCodec,
3
+ BigIntCodec,
4
+ type Codec,
5
+ type CodecType,
6
+ DateCodec,
7
+ type Evaluate,
8
+ MessageCodec,
9
+ NumberCodec,
10
+ OneOfCodec,
11
+ OptionalCodec,
12
+ RequiredCodec,
13
+ StringCodec,
14
+ Uint8ArrayCodec,
15
+ } from "@apibara/protocol/codec";
3
16
  import { FieldElement } from "./common";
4
- import { tag } from "./helpers";
5
17
  import * as proto from "./proto";
6
18
 
7
19
  /** Price of a unit of resource.
@@ -9,113 +21,154 @@ import * as proto from "./proto";
9
21
  * @prop priceInFri The price in Fri (1e-18 STRK).
10
22
  * @prop priceInWei The price in Wei (1e-18 ETH).
11
23
  */
12
- export const ResourcePrice = Schema.Struct({
13
- priceInFri: Schema.optional(FieldElement),
14
- priceInWei: Schema.optional(FieldElement),
24
+ export const ResourcePrice = MessageCodec({
25
+ priceInFri: OptionalCodec(FieldElement),
26
+ priceInWei: OptionalCodec(FieldElement),
15
27
  });
16
28
 
17
- export type ResourcePrice = typeof ResourcePrice.Type;
29
+ export type ResourcePrice = Readonly<CodecType<typeof ResourcePrice>>;
18
30
 
19
- /** How data is posted to L1. */
20
- export const L1DataAvailabilityMode = Schema.transform(
21
- Schema.Enums(proto.data.L1DataAvailabilityMode),
22
- Schema.Literal("blob", "calldata", "unknown"),
23
- {
24
- decode(value) {
25
- const enumMap = {
26
- [proto.data.L1DataAvailabilityMode.CALLDATA]: "calldata",
27
- [proto.data.L1DataAvailabilityMode.BLOB]: "blob",
28
- [proto.data.L1DataAvailabilityMode.UNSPECIFIED]: "unknown",
29
- [proto.data.L1DataAvailabilityMode.UNRECOGNIZED]: "unknown",
30
- } as const;
31
+ // -----------------------------------------------------
31
32
 
32
- return enumMap[value] ?? "unknown";
33
- },
34
- encode(value) {
35
- throw new Error("encode: not implemented");
36
- },
33
+ /** How data is posted to L1. */
34
+ export const L1DataAvailabilityMode: Codec<
35
+ "blob" | "calldata" | "unknown",
36
+ proto.data.L1DataAvailabilityMode
37
+ > = {
38
+ encode(x) {
39
+ switch (x) {
40
+ case "calldata":
41
+ return proto.data.L1DataAvailabilityMode.CALLDATA;
42
+ case "blob":
43
+ return proto.data.L1DataAvailabilityMode.BLOB;
44
+ case "unknown":
45
+ return proto.data.L1DataAvailabilityMode.UNSPECIFIED;
46
+ default:
47
+ return proto.data.L1DataAvailabilityMode.UNRECOGNIZED;
48
+ }
49
+ },
50
+ decode(p) {
51
+ const enumMap = {
52
+ [proto.data.L1DataAvailabilityMode.CALLDATA]: "calldata",
53
+ [proto.data.L1DataAvailabilityMode.BLOB]: "blob",
54
+ [proto.data.L1DataAvailabilityMode.UNSPECIFIED]: "unknown",
55
+ [proto.data.L1DataAvailabilityMode.UNRECOGNIZED]: "unknown",
56
+ } as const;
57
+
58
+ return enumMap[p] ?? "unknown";
37
59
  },
38
- );
60
+ };
61
+
62
+ export type L1DataAvailabilityMode = CodecType<typeof L1DataAvailabilityMode>;
63
+
64
+ // -----------------------------------------------------
65
+
66
+ /** Status of a transaction. */
67
+ export const TransactionStatus: Codec<
68
+ "unknown" | "succeeded" | "reverted",
69
+ proto.data.TransactionStatus
70
+ > = {
71
+ encode(x) {
72
+ switch (x) {
73
+ case "succeeded":
74
+ return proto.data.TransactionStatus.SUCCEEDED;
75
+ case "reverted":
76
+ return proto.data.TransactionStatus.REVERTED;
77
+ case "unknown":
78
+ return proto.data.TransactionStatus.UNSPECIFIED;
79
+ default:
80
+ return proto.data.TransactionStatus.UNRECOGNIZED;
81
+ }
82
+ },
83
+ decode(p) {
84
+ const enumMap = {
85
+ [proto.data.TransactionStatus.SUCCEEDED]: "succeeded",
86
+ [proto.data.TransactionStatus.REVERTED]: "reverted",
87
+ [proto.data.TransactionStatus.UNSPECIFIED]: "unknown",
88
+ [proto.data.TransactionStatus.UNRECOGNIZED]: "unknown",
89
+ } as const;
90
+
91
+ return enumMap[p] ?? "unknown";
92
+ },
93
+ };
39
94
 
40
- export type L1DataAvailabilityMode = typeof L1DataAvailabilityMode.Type;
95
+ export type TransactionStatus = CodecType<typeof TransactionStatus>;
41
96
 
42
- export const TransactionStatus = Schema.transform(
43
- Schema.Enums(proto.data.TransactionStatus),
44
- Schema.Literal("unknown", "succeeded", "reverted"),
45
- {
46
- decode(value) {
47
- const enumMap = {
48
- [proto.data.TransactionStatus.SUCCEEDED]: "succeeded",
49
- [proto.data.TransactionStatus.REVERTED]: "reverted",
50
- [proto.data.TransactionStatus.UNSPECIFIED]: "unknown",
51
- [proto.data.TransactionStatus.UNRECOGNIZED]: "unknown",
52
- } as const;
97
+ // -----------------------------------------------------
53
98
 
54
- return enumMap[value] ?? "unknown";
55
- },
56
- encode(value) {
57
- throw new Error("encode: not implemented");
58
- },
99
+ /** 128-bit unsigned integer. */
100
+ export const U128: Codec<bigint, proto.data.Uint128> = {
101
+ // TODO: double check if this is correct
102
+ encode(x) {
103
+ const low = x.toString(16).padStart(16, "0");
104
+ const high = (x >> 128n).toString(16).padStart(16, "0");
105
+ return { x0: BigInt(`0x${low}`), x1: BigInt(`0x${high}`) };
59
106
  },
60
- );
61
-
62
- export type TransactionStatus = typeof TransactionStatus.Type;
63
-
64
- export const U128 = Schema.transform(
65
- Schema.Struct({
66
- x0: Schema.BigIntFromSelf,
67
- x1: Schema.BigIntFromSelf,
68
- }),
69
- Schema.BigIntFromSelf,
70
- {
71
- decode(value) {
72
- const low = value.x0.toString(16).padStart(16, "0");
73
- const high = value.x1.toString(16).padStart(16, "0");
74
- return BigInt(`0x${low}${high}`);
75
- },
76
- encode(value) {
77
- throw new Error("encode: not implemented");
78
- },
107
+ decode(p) {
108
+ const low = (p.x0 ?? 0n).toString(16).padStart(16, "0");
109
+ const high = (p.x1 ?? 0n).toString(16).padStart(16, "0");
110
+ return BigInt(`0x${low}${high}`);
79
111
  },
80
- );
112
+ };
81
113
 
82
- export type U128 = typeof U128.Type;
114
+ export type U128 = CodecType<typeof U128>;
83
115
 
84
- export const ResourceBounds = Schema.Struct({
85
- maxAmount: Schema.BigIntFromSelf,
86
- maxPricePerUnit: U128,
87
- });
88
-
89
- export type ResourceBounds = typeof ResourceBounds.Type;
116
+ // -----------------------------------------------------
90
117
 
91
- export const ResourceBoundsMapping = Schema.Struct({
92
- l1Gas: ResourceBounds,
93
- l2Gas: ResourceBounds,
118
+ /** Resource bounds. */
119
+ export const ResourceBounds = MessageCodec({
120
+ maxAmount: RequiredCodec(BigIntCodec),
121
+ maxPricePerUnit: RequiredCodec(U128),
94
122
  });
95
123
 
96
- export type ResourceBoundsMapping = typeof ResourceBoundsMapping.Type;
124
+ export type ResourceBounds = Readonly<CodecType<typeof ResourceBounds>>;
97
125
 
98
- export const DataAvailabilityMode = Schema.transform(
99
- Schema.Enums(proto.data.DataAvailabilityMode),
100
- Schema.Literal("l1", "l2", "unknown"),
101
- {
102
- decode(value) {
103
- const enumMap = {
104
- [proto.data.DataAvailabilityMode.L1]: "l1",
105
- [proto.data.DataAvailabilityMode.L2]: "l2",
106
- [proto.data.DataAvailabilityMode.UNSPECIFIED]: "unknown",
107
- [proto.data.DataAvailabilityMode.UNRECOGNIZED]: "unknown",
108
- } as const;
126
+ // -----------------------------------------------------
109
127
 
110
- return enumMap[value] ?? "unknown";
111
- },
112
- encode(value) {
113
- throw new Error("encode: not implemented");
114
- },
128
+ /** Resource bounds mapping. */
129
+ export const ResourceBoundsMapping = MessageCodec({
130
+ l1Gas: RequiredCodec(ResourceBounds),
131
+ l2Gas: RequiredCodec(ResourceBounds),
132
+ });
133
+
134
+ export type ResourceBoundsMapping = Readonly<
135
+ CodecType<typeof ResourceBoundsMapping>
136
+ >;
137
+
138
+ // -----------------------------------------------------
139
+
140
+ /** Data availability mode. */
141
+ export const DataAvailabilityMode: Codec<
142
+ "l1" | "l2" | "unknown",
143
+ proto.data.DataAvailabilityMode
144
+ > = {
145
+ encode(x) {
146
+ switch (x) {
147
+ case "l1":
148
+ return proto.data.DataAvailabilityMode.L1;
149
+ case "l2":
150
+ return proto.data.DataAvailabilityMode.L2;
151
+ case "unknown":
152
+ return proto.data.DataAvailabilityMode.UNSPECIFIED;
153
+ default:
154
+ return proto.data.DataAvailabilityMode.UNRECOGNIZED;
155
+ }
115
156
  },
116
- );
157
+ decode(p) {
158
+ const enumMap = {
159
+ [proto.data.DataAvailabilityMode.L1]: "l1",
160
+ [proto.data.DataAvailabilityMode.L2]: "l2",
161
+ [proto.data.DataAvailabilityMode.UNSPECIFIED]: "unknown",
162
+ [proto.data.DataAvailabilityMode.UNRECOGNIZED]: "unknown",
163
+ } as const;
164
+
165
+ return enumMap[p] ?? "unknown";
166
+ },
167
+ };
168
+
169
+ export type DataAvailabilityMode = CodecType<typeof DataAvailabilityMode>;
117
170
 
118
- export type DataAvailabilityMode = typeof DataAvailabilityMode.Type;
171
+ // -----------------------------------------------------
119
172
 
120
173
  /** Starknet block header.
121
174
  *
@@ -130,20 +183,22 @@ export type DataAvailabilityMode = typeof DataAvailabilityMode.Type;
130
183
  * @prop l1DataGasPrice Blob gas price.
131
184
  * @prop l1DataAvailabilityMode How data is posted to L1.
132
185
  */
133
- export const BlockHeader = Schema.Struct({
134
- blockHash: Schema.optional(FieldElement),
135
- parentBlockHash: FieldElement,
136
- blockNumber: Schema.BigIntFromSelf,
137
- sequencerAddress: FieldElement,
138
- newRoot: Schema.optional(FieldElement),
139
- timestamp: Schema.DateFromSelf,
140
- starknetVersion: Schema.String,
141
- l1GasPrice: ResourcePrice,
142
- l1DataGasPrice: ResourcePrice,
143
- l1DataAvailabilityMode: L1DataAvailabilityMode,
186
+ export const BlockHeader = MessageCodec({
187
+ blockHash: RequiredCodec(FieldElement),
188
+ parentBlockHash: RequiredCodec(FieldElement),
189
+ blockNumber: RequiredCodec(BigIntCodec),
190
+ sequencerAddress: RequiredCodec(FieldElement),
191
+ newRoot: OptionalCodec(FieldElement),
192
+ timestamp: RequiredCodec(DateCodec),
193
+ starknetVersion: RequiredCodec(StringCodec),
194
+ l1GasPrice: RequiredCodec(ResourcePrice),
195
+ l1DataGasPrice: RequiredCodec(ResourcePrice),
196
+ l1DataAvailabilityMode: RequiredCodec(L1DataAvailabilityMode),
144
197
  });
145
198
 
146
- export type BlockHeader = typeof BlockHeader.Type;
199
+ export type BlockHeader = Readonly<CodecType<typeof BlockHeader>>;
200
+
201
+ // -----------------------------------------------------
147
202
 
148
203
  /** Transaction metadata.
149
204
  *
@@ -153,200 +208,327 @@ export type BlockHeader = typeof BlockHeader.Type;
153
208
  * @prop transactionHash The transaction hash.
154
209
  * @prop transactionStatus The transaction status.
155
210
  */
156
- export const TransactionMeta = Schema.Struct({
157
- transactionIndex: Schema.Number,
158
- transactionHash: FieldElement,
159
- transactionStatus: TransactionStatus,
160
- });
161
-
162
- export type TransactionMeta = typeof TransactionMeta.Type;
163
-
164
- export const InvokeTransactionV0 = Schema.Struct({
165
- _tag: tag("invokeV0"),
166
- invokeV0: Schema.Struct({
167
- maxFee: FieldElement,
168
- signature: Schema.Array(FieldElement),
169
- contractAddress: FieldElement,
170
- entryPointSelector: FieldElement,
171
- calldata: Schema.Array(FieldElement),
172
- }),
173
- });
174
-
175
- export type InvokeTransactionV0 = typeof InvokeTransactionV0.Type;
176
-
177
- export const InvokeTransactionV1 = Schema.Struct({
178
- _tag: tag("invokeV1"),
179
- invokeV1: Schema.Struct({
180
- senderAddress: FieldElement,
181
- calldata: Schema.Array(FieldElement),
182
- maxFee: FieldElement,
183
- signature: Schema.Array(FieldElement),
184
- nonce: FieldElement,
185
- }),
186
- });
187
-
188
- export type InvokeTransactionV1 = typeof InvokeTransactionV1.Type;
189
-
190
- export const InvokeTransactionV3 = Schema.Struct({
191
- _tag: tag("invokeV3"),
192
- invokeV3: Schema.Struct({
193
- senderAddress: FieldElement,
194
- calldata: Schema.Array(FieldElement),
195
- signature: Schema.Array(FieldElement),
196
- nonce: FieldElement,
197
- resourceBounds: ResourceBoundsMapping,
198
- tip: Schema.BigIntFromSelf,
199
- paymasterData: Schema.Array(FieldElement),
200
- accountDeploymentData: Schema.Array(FieldElement),
201
- nonceDataAvailabilityMode: DataAvailabilityMode,
202
- feeDataAvailabilityMode: DataAvailabilityMode,
203
- }),
204
- });
205
-
206
- export type InvokeTransactionV3 = typeof InvokeTransactionV3.Type;
207
-
208
- export const L1HandlerTransaction = Schema.Struct({
209
- _tag: tag("l1Handler"),
210
- l1Handler: Schema.Struct({
211
- nonce: Schema.BigIntFromSelf,
212
- contractAddress: FieldElement,
213
- entryPointSelector: FieldElement,
214
- calldata: Schema.Array(FieldElement),
215
- }),
216
- });
217
-
218
- export type L1HandlerTransaction = typeof L1HandlerTransaction.Type;
219
-
220
- export const DeployTransaction = Schema.Struct({
221
- _tag: tag("deploy"),
222
- deploy: Schema.Struct({
223
- contractAddressSalt: FieldElement,
224
- constructorCalldata: Schema.Array(FieldElement),
225
- classHash: FieldElement,
226
- }),
227
- });
228
-
229
- export type DeployTransaction = typeof DeployTransaction.Type;
230
-
231
- export const DeclareTransactionV0 = Schema.Struct({
232
- _tag: tag("declareV0"),
233
- declareV0: Schema.Struct({
234
- senderAddress: FieldElement,
235
- maxFee: FieldElement,
236
- signature: Schema.Array(FieldElement),
237
- classHash: FieldElement,
238
- }),
239
- });
240
-
241
- export type DeclareTransactionV0 = typeof DeclareTransactionV0.Type;
242
-
243
- export const DeclareTransactionV1 = Schema.Struct({
244
- _tag: tag("declareV1"),
245
- declareV1: Schema.Struct({
246
- senderAddress: FieldElement,
247
- maxFee: FieldElement,
248
- signature: Schema.Array(FieldElement),
249
- nonce: FieldElement,
250
- classHash: FieldElement,
251
- }),
252
- });
253
-
254
- export type DeclareTransactionV1 = typeof DeclareTransactionV1.Type;
255
-
256
- export const DeclareTransactionV2 = Schema.Struct({
257
- _tag: tag("declareV2"),
258
- declareV2: Schema.Struct({
259
- senderAddress: FieldElement,
260
- compiledClassHash: FieldElement,
261
- maxFee: FieldElement,
262
- signature: Schema.Array(FieldElement),
263
- nonce: FieldElement,
264
- classHash: FieldElement,
265
- }),
266
- });
267
-
268
- export type DeclareTransactionV2 = typeof DeclareTransactionV2.Type;
269
-
270
- export const DeclareTransactionV3 = Schema.Struct({
271
- _tag: tag("declareV3"),
272
- declareV3: Schema.Struct({
273
- senderAddress: FieldElement,
274
- compiledClassHash: FieldElement,
275
- signature: Schema.Array(FieldElement),
276
- nonce: FieldElement,
277
- classHash: FieldElement,
278
- resourceBounds: ResourceBoundsMapping,
279
- tip: Schema.BigIntFromSelf,
280
- paymasterData: Schema.Array(FieldElement),
281
- accountDeploymentData: Schema.Array(FieldElement),
282
- nonceDataAvailabilityMode: DataAvailabilityMode,
283
- feeDataAvailabilityMode: DataAvailabilityMode,
284
- }),
285
- });
286
-
287
- export type DeclareTransactionV3 = typeof DeclareTransactionV3.Type;
288
-
289
- export const DeployAccountTransactionV1 = Schema.Struct({
290
- _tag: tag("deployAccountV1"),
291
- deployAccountV1: Schema.Struct({
292
- maxFee: FieldElement,
293
- signature: Schema.Array(FieldElement),
294
- nonce: FieldElement,
295
- contractAddressSalt: FieldElement,
296
- constructorCalldata: Schema.Array(FieldElement),
297
- classHash: FieldElement,
298
- }),
299
- });
300
-
301
- export type DeployAccountTransactionV1 = typeof DeployAccountTransactionV1.Type;
302
-
303
- export const DeployAccountTransactionV3 = Schema.Struct({
304
- _tag: tag("deployAccountV3"),
305
- deployAccountV3: Schema.Struct({
306
- signature: Schema.Array(FieldElement),
307
- nonce: FieldElement,
308
- contractAddressSalt: FieldElement,
309
- constructorCalldata: Schema.Array(FieldElement),
310
- classHash: FieldElement,
311
- resourceBounds: ResourceBoundsMapping,
312
- tip: Schema.BigIntFromSelf,
313
- paymasterData: Schema.Array(FieldElement),
314
- nonceDataAvailabilityMode: DataAvailabilityMode,
315
- feeDataAvailabilityMode: DataAvailabilityMode,
316
- }),
317
- });
318
-
319
- export type DeployAccountTransactionV3 = typeof DeployAccountTransactionV3.Type;
211
+ export const TransactionMeta = MessageCodec({
212
+ transactionIndex: RequiredCodec(NumberCodec),
213
+ transactionHash: RequiredCodec(FieldElement),
214
+ transactionStatus: RequiredCodec(TransactionStatus),
215
+ });
216
+
217
+ export type TransactionMeta = Readonly<CodecType<typeof TransactionMeta>>;
218
+
219
+ // -----------------------------------------------------
220
+
221
+ /** Invoke transaction v0.
222
+ *
223
+ * @prop maxFee The maximum fee.
224
+ * @prop signature The signature.
225
+ * @prop contractAddress The contract address.
226
+ * @prop entryPointSelector The entry point selector.
227
+ * @prop calldata The calldata.
228
+ */
229
+ export const InvokeTransactionV0 = MessageCodec({
230
+ maxFee: RequiredCodec(FieldElement),
231
+ signature: ArrayCodec(FieldElement),
232
+ contractAddress: RequiredCodec(FieldElement),
233
+ entryPointSelector: RequiredCodec(FieldElement),
234
+ calldata: ArrayCodec(FieldElement),
235
+ });
236
+
237
+ export type InvokeTransactionV0 = Readonly<
238
+ CodecType<typeof InvokeTransactionV0>
239
+ >;
240
+
241
+ // -----------------------------------------------------
242
+
243
+ /** Invoke transaction v1.
244
+ *
245
+ * @prop senderAddress The sender address.
246
+ * @prop calldata The calldata.
247
+ * @prop maxFee The maximum fee.
248
+ * @prop signature The signature.
249
+ * @prop nonce The nonce.
250
+ */
251
+ export const InvokeTransactionV1 = MessageCodec({
252
+ senderAddress: RequiredCodec(FieldElement),
253
+ calldata: ArrayCodec(FieldElement),
254
+ maxFee: RequiredCodec(FieldElement),
255
+ signature: ArrayCodec(FieldElement),
256
+ nonce: RequiredCodec(FieldElement),
257
+ });
258
+
259
+ export type InvokeTransactionV1 = Readonly<
260
+ CodecType<typeof InvokeTransactionV1>
261
+ >;
262
+
263
+ // -----------------------------------------------------
264
+
265
+ /** Invoke transaction v3.
266
+ *
267
+ * @prop senderAddress The sender address.
268
+ * @prop calldata The calldata.
269
+ * @prop signature The signature.
270
+ * @prop nonce The nonce.
271
+ * @prop resourceBounds The resource bounds.
272
+ * @prop tip The tip.
273
+ * @prop paymasterData The paymaster data.
274
+ * @prop accountDeploymentData The account deployment data.
275
+ * @prop nonceDataAvailabilityMode How nonce data is posted to L1.
276
+ * @prop feeDataAvailabilityMode How fee data is posted to L1.
277
+ */
278
+ export const InvokeTransactionV3 = MessageCodec({
279
+ senderAddress: RequiredCodec(FieldElement),
280
+ calldata: ArrayCodec(FieldElement),
281
+ signature: ArrayCodec(FieldElement),
282
+ nonce: RequiredCodec(FieldElement),
283
+ resourceBounds: RequiredCodec(ResourceBoundsMapping),
284
+ tip: RequiredCodec(BigIntCodec),
285
+ paymasterData: ArrayCodec(FieldElement),
286
+ accountDeploymentData: ArrayCodec(FieldElement),
287
+ nonceDataAvailabilityMode: RequiredCodec(DataAvailabilityMode),
288
+ feeDataAvailabilityMode: RequiredCodec(DataAvailabilityMode),
289
+ });
290
+
291
+ export type InvokeTransactionV3 = Readonly<
292
+ CodecType<typeof InvokeTransactionV3>
293
+ >;
294
+
295
+ // -----------------------------------------------------
296
+
297
+ /** L1 handler transaction.
298
+ *
299
+ * @prop nonce The nonce.
300
+ * @prop contractAddress The contract address.
301
+ * @prop entryPointSelector The entry point selector.
302
+ * @prop calldata The calldata.
303
+ */
304
+ export const L1HandlerTransaction = MessageCodec({
305
+ nonce: RequiredCodec(BigIntCodec),
306
+ contractAddress: RequiredCodec(FieldElement),
307
+ entryPointSelector: RequiredCodec(FieldElement),
308
+ calldata: ArrayCodec(FieldElement),
309
+ });
310
+
311
+ export type L1HandlerTransaction = Readonly<
312
+ CodecType<typeof L1HandlerTransaction>
313
+ >;
314
+
315
+ // -----------------------------------------------------
316
+
317
+ /** Deploy transaction.
318
+ *
319
+ * @prop contractAddressSalt The contract address salt.
320
+ * @prop constructorCalldata The constructor calldata.
321
+ * @prop classHash The class hash.
322
+ */
323
+ export const DeployTransaction = MessageCodec({
324
+ contractAddressSalt: RequiredCodec(FieldElement),
325
+ constructorCalldata: ArrayCodec(FieldElement),
326
+ classHash: RequiredCodec(FieldElement),
327
+ });
328
+
329
+ export type DeployTransaction = Readonly<CodecType<typeof DeployTransaction>>;
330
+
331
+ // -----------------------------------------------------
332
+
333
+ /** Declare transaction v0.
334
+ *
335
+ * @prop senderAddress The sender address.
336
+ * @prop maxFee The maximum fee.
337
+ * @prop signature The signature.
338
+ * @prop classHash The class hash.
339
+ */
340
+ export const DeclareTransactionV0 = MessageCodec({
341
+ senderAddress: RequiredCodec(FieldElement),
342
+ maxFee: RequiredCodec(FieldElement),
343
+ signature: ArrayCodec(FieldElement),
344
+ classHash: RequiredCodec(FieldElement),
345
+ });
346
+
347
+ export type DeclareTransactionV0 = Readonly<
348
+ CodecType<typeof DeclareTransactionV0>
349
+ >;
350
+
351
+ // -----------------------------------------------------
352
+
353
+ /** Declare transaction v1.
354
+ *
355
+ * @prop senderAddress The sender address.
356
+ * @prop maxFee The maximum fee.
357
+ * @prop signature The signature.
358
+ * @prop nonce The nonce.
359
+ * @prop classHash The class hash.
360
+ */
361
+ export const DeclareTransactionV1 = MessageCodec({
362
+ senderAddress: RequiredCodec(FieldElement),
363
+ maxFee: RequiredCodec(FieldElement),
364
+ signature: ArrayCodec(FieldElement),
365
+ nonce: RequiredCodec(FieldElement),
366
+ classHash: RequiredCodec(FieldElement),
367
+ });
368
+
369
+ export type DeclareTransactionV1 = Readonly<
370
+ CodecType<typeof DeclareTransactionV1>
371
+ >;
372
+
373
+ // -----------------------------------------------------
374
+
375
+ /** Declare transaction v2.
376
+ *
377
+ * @prop senderAddress The sender address.
378
+ * @prop compiledClassHash The compiled class hash.
379
+ * @prop maxFee The maximum fee.
380
+ * @prop signature The signature.
381
+ * @prop nonce The nonce.
382
+ * @prop classHash The class hash.
383
+ */
384
+ export const DeclareTransactionV2 = MessageCodec({
385
+ senderAddress: RequiredCodec(FieldElement),
386
+ compiledClassHash: RequiredCodec(FieldElement),
387
+ maxFee: RequiredCodec(FieldElement),
388
+ signature: ArrayCodec(FieldElement),
389
+ nonce: RequiredCodec(FieldElement),
390
+ classHash: RequiredCodec(FieldElement),
391
+ });
392
+
393
+ export type DeclareTransactionV2 = Readonly<
394
+ CodecType<typeof DeclareTransactionV2>
395
+ >;
396
+
397
+ // -----------------------------------------------------
398
+
399
+ /** Declare transaction v3.
400
+ *
401
+ * @prop senderAddress The sender address.
402
+ * @prop compiledClassHash The compiled class hash.
403
+ * @prop signature The signature.
404
+ * @prop nonce The nonce.
405
+ * @prop classHash The class hash.
406
+ * @prop resourceBounds The resource bounds.
407
+ * @prop tip The tip.
408
+ * @prop paymasterData The paymaster data.
409
+ * @prop nonceDataAvailabilityMode How nonce data is posted to L1.
410
+ * @prop feeDataAvailabilityMode How fee data is posted to L1.
411
+ */
412
+ export const DeclareTransactionV3 = MessageCodec({
413
+ senderAddress: RequiredCodec(FieldElement),
414
+ compiledClassHash: RequiredCodec(FieldElement),
415
+ signature: ArrayCodec(FieldElement),
416
+ nonce: RequiredCodec(FieldElement),
417
+ classHash: RequiredCodec(FieldElement),
418
+ resourceBounds: RequiredCodec(ResourceBoundsMapping),
419
+ tip: RequiredCodec(BigIntCodec),
420
+ paymasterData: ArrayCodec(FieldElement),
421
+ accountDeploymentData: ArrayCodec(FieldElement),
422
+ nonceDataAvailabilityMode: RequiredCodec(DataAvailabilityMode),
423
+ feeDataAvailabilityMode: RequiredCodec(DataAvailabilityMode),
424
+ });
425
+
426
+ export type DeclareTransactionV3 = Readonly<
427
+ CodecType<typeof DeclareTransactionV3>
428
+ >;
429
+
430
+ // -----------------------------------------------------
431
+
432
+ /** Deploy account transaction v1.
433
+ *
434
+ * @prop maxFee The maximum fee.
435
+ * @prop signature The signature.
436
+ * @prop nonce The nonce.
437
+ * @prop contractAddressSalt The contract address salt.
438
+ * @prop constructorCalldata The constructor calldata.
439
+ * @prop classHash The class hash.
440
+ */
441
+ export const DeployAccountTransactionV1 = MessageCodec({
442
+ maxFee: RequiredCodec(FieldElement),
443
+ signature: ArrayCodec(FieldElement),
444
+ nonce: RequiredCodec(FieldElement),
445
+ contractAddressSalt: RequiredCodec(FieldElement),
446
+ constructorCalldata: ArrayCodec(FieldElement),
447
+ classHash: RequiredCodec(FieldElement),
448
+ });
449
+
450
+ export type DeployAccountTransactionV1 = Readonly<
451
+ CodecType<typeof DeployAccountTransactionV1>
452
+ >;
453
+
454
+ // -----------------------------------------------------
455
+
456
+ /** Deploy account transaction v3.
457
+ *
458
+ * @prop signature The signature.
459
+ * @prop nonce The nonce.
460
+ * @prop contractAddressSalt The contract address salt.
461
+ * @prop constructorCalldata The constructor calldata.
462
+ * @prop classHash The class hash.
463
+ * @prop resourceBounds The resource bounds.
464
+ * @prop tip The tip.
465
+ * @prop paymasterData The paymaster data.
466
+ * @prop nonceDataAvailabilityMode How nonce data is posted to L1.
467
+ * @prop feeDataAvailabilityMode How fee data is posted to L1.
468
+ */
469
+ export const DeployAccountTransactionV3 = MessageCodec({
470
+ signature: ArrayCodec(FieldElement),
471
+ nonce: RequiredCodec(FieldElement),
472
+ contractAddressSalt: RequiredCodec(FieldElement),
473
+ constructorCalldata: ArrayCodec(FieldElement),
474
+ classHash: RequiredCodec(FieldElement),
475
+ resourceBounds: RequiredCodec(ResourceBoundsMapping),
476
+ tip: RequiredCodec(BigIntCodec),
477
+ paymasterData: ArrayCodec(FieldElement),
478
+ nonceDataAvailabilityMode: RequiredCodec(DataAvailabilityMode),
479
+ feeDataAvailabilityMode: RequiredCodec(DataAvailabilityMode),
480
+ });
481
+
482
+ export type DeployAccountTransactionV3 = Readonly<
483
+ CodecType<typeof DeployAccountTransactionV3>
484
+ >;
485
+
486
+ // -----------------------------------------------------
320
487
 
321
488
  /** A transaction.
322
489
  *
323
- * @prop meta Transaction metadata.
324
- */
325
- export const Transaction = Schema.Struct({
326
- filterIds: Schema.Array(Schema.Number),
327
- meta: TransactionMeta,
328
- transaction: Schema.Union(
329
- InvokeTransactionV0,
330
- InvokeTransactionV1,
331
- InvokeTransactionV3,
332
- L1HandlerTransaction,
333
- DeployTransaction,
334
- DeclareTransactionV0,
335
- DeclareTransactionV1,
336
- DeclareTransactionV2,
337
- DeclareTransactionV3,
338
- DeployAccountTransactionV1,
339
- DeployAccountTransactionV3,
490
+ * @prop filterIds The filter IDs.
491
+ * @prop meta The transaction metadata.
492
+ */
493
+ export const Transaction = MessageCodec({
494
+ filterIds: ArrayCodec(NumberCodec),
495
+ meta: RequiredCodec(TransactionMeta),
496
+ transaction: RequiredCodec(
497
+ OneOfCodec({
498
+ invokeV0: InvokeTransactionV0,
499
+ invokeV1: InvokeTransactionV1,
500
+ invokeV3: InvokeTransactionV3,
501
+ l1Handler: L1HandlerTransaction,
502
+ deploy: DeployTransaction,
503
+ declareV0: DeclareTransactionV0,
504
+ declareV1: DeclareTransactionV1,
505
+ declareV2: DeclareTransactionV2,
506
+ declareV3: DeclareTransactionV3,
507
+ deployAccountV1: DeployAccountTransactionV1,
508
+ deployAccountV3: DeployAccountTransactionV3,
509
+ }),
340
510
  ),
341
511
  });
342
512
 
343
- export type Transaction = typeof Transaction.Type;
513
+ export type Transaction = Readonly<CodecType<typeof Transaction>>;
514
+
515
+ // -----------------------------------------------------
344
516
 
345
- export const PriceUnit = Schema.transform(
346
- Schema.Enums(proto.data.PriceUnit),
347
- Schema.Literal("wei", "fri", "unknown"),
517
+ export const PriceUnit: Codec<"wei" | "fri" | "unknown", proto.data.PriceUnit> =
348
518
  {
349
- decode(value) {
519
+ encode(x) {
520
+ switch (x) {
521
+ case "wei":
522
+ return proto.data.PriceUnit.WEI;
523
+ case "fri":
524
+ return proto.data.PriceUnit.FRI;
525
+ case "unknown":
526
+ return proto.data.PriceUnit.UNSPECIFIED;
527
+ default:
528
+ return proto.data.PriceUnit.UNRECOGNIZED;
529
+ }
530
+ },
531
+ decode(p) {
350
532
  const enumMap = {
351
533
  [proto.data.PriceUnit.WEI]: "wei",
352
534
  [proto.data.PriceUnit.FRI]: "fri",
@@ -354,140 +536,163 @@ export const PriceUnit = Schema.transform(
354
536
  [proto.data.PriceUnit.UNRECOGNIZED]: "unknown",
355
537
  } as const;
356
538
 
357
- return enumMap[value] ?? "unknown";
358
- },
359
- encode(value) {
360
- throw new Error("encode: not implemented");
539
+ return enumMap[p] ?? "unknown";
361
540
  },
362
- },
363
- );
541
+ };
364
542
 
365
- export type PriceUnit = typeof PriceUnit.Type;
543
+ export type PriceUnit = CodecType<typeof PriceUnit>;
366
544
 
367
- export const FeePayment = Schema.Struct({
368
- amount: FieldElement,
369
- unit: PriceUnit,
370
- });
545
+ // -----------------------------------------------------
371
546
 
372
- export type FeePayment = typeof FeePayment.Type;
547
+ export const FeePayment = MessageCodec({
548
+ amount: RequiredCodec(FieldElement),
549
+ unit: RequiredCodec(PriceUnit),
550
+ });
373
551
 
374
- export const ComputationResources = Schema.Struct({
375
- steps: Schema.BigIntFromSelf,
376
- memoryHoles: Schema.optional(Schema.BigIntFromSelf),
377
- rangeCheckBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
378
- pedersenBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
379
- poseidonBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
380
- ecOpBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
381
- ecdsaBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
382
- bitwiseBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
383
- keccakBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
384
- segmentArenaBuiltin: Schema.optional(Schema.BigIntFromSelf),
552
+ export type FeePayment = Readonly<CodecType<typeof FeePayment>>;
553
+
554
+ // -----------------------------------------------------
555
+
556
+ export const ComputationResources = MessageCodec({
557
+ steps: RequiredCodec(BigIntCodec),
558
+ memoryHoles: OptionalCodec(BigIntCodec),
559
+ rangeCheckBuiltinApplications: OptionalCodec(BigIntCodec),
560
+ pedersenBuiltinApplications: OptionalCodec(BigIntCodec),
561
+ poseidonBuiltinApplications: OptionalCodec(BigIntCodec),
562
+ ecOpBuiltinApplications: OptionalCodec(BigIntCodec),
563
+ ecdsaBuiltinApplications: OptionalCodec(BigIntCodec),
564
+ bitwiseBuiltinApplications: OptionalCodec(BigIntCodec),
565
+ keccakBuiltinApplications: OptionalCodec(BigIntCodec),
566
+ segmentArenaBuiltin: OptionalCodec(BigIntCodec),
385
567
  });
386
568
 
387
- export type ComputationResources = typeof ComputationResources.Type;
569
+ export type ComputationResources = Readonly<
570
+ CodecType<typeof ComputationResources>
571
+ >;
388
572
 
389
- export const DataAvailabilityResources = Schema.Struct({
390
- l1Gas: Schema.BigIntFromSelf,
391
- l1DataGas: Schema.BigIntFromSelf,
573
+ // -----------------------------------------------------
574
+
575
+ export const DataAvailabilityResources = MessageCodec({
576
+ l1Gas: RequiredCodec(BigIntCodec),
577
+ l1DataGas: RequiredCodec(BigIntCodec),
392
578
  });
393
579
 
394
- export type DataAvailabilityResources = typeof DataAvailabilityResources.Type;
580
+ export type DataAvailabilityResources = Readonly<
581
+ CodecType<typeof DataAvailabilityResources>
582
+ >;
583
+
584
+ // -----------------------------------------------------
395
585
 
396
- export const ExecutionResources = Schema.Struct({
397
- computation: ComputationResources,
398
- dataAvailability: DataAvailabilityResources,
586
+ export const ExecutionResources = MessageCodec({
587
+ computation: RequiredCodec(ComputationResources),
588
+ dataAvailability: RequiredCodec(DataAvailabilityResources),
399
589
  });
400
590
 
401
- export type ExecutionResources = typeof ExecutionResources.Type;
591
+ export type ExecutionResources = Readonly<CodecType<typeof ExecutionResources>>;
402
592
 
403
- export const ExecutionSucceeded = Schema.Struct({
404
- _tag: tag("succeeded"),
405
- succeeded: Schema.Struct({}),
406
- });
593
+ // -----------------------------------------------------
407
594
 
408
- export type ExecutionSucceeded = typeof ExecutionSucceeded.Type;
595
+ export const ExecutionSucceeded = MessageCodec({});
409
596
 
410
- export const ExecutionReverted = Schema.Struct({
411
- _tag: tag("reverted"),
412
- reverted: Schema.Struct({
413
- reason: Schema.optional(Schema.String),
414
- }),
415
- });
597
+ export type ExecutionSucceeded = Readonly<CodecType<typeof ExecutionSucceeded>>;
416
598
 
417
- export type ExecutionReverted = typeof ExecutionReverted.Type;
599
+ // -----------------------------------------------------
418
600
 
419
- /** Common fields for all transaction receipts. */
420
- export const TransactionReceiptMeta = Schema.Struct({
421
- transactionIndex: Schema.Number,
422
- transactionHash: FieldElement,
423
- actualFee: FeePayment,
424
- executionResources: ExecutionResources,
425
- executionResult: Schema.Union(ExecutionSucceeded, ExecutionReverted),
601
+ export const ExecutionReverted = MessageCodec({
602
+ reason: OptionalCodec(StringCodec),
426
603
  });
427
604
 
428
- export type TransactionReceiptMeta = typeof TransactionReceiptMeta.Type;
605
+ export type ExecutionReverted = Readonly<CodecType<typeof ExecutionReverted>>;
606
+
607
+ // -----------------------------------------------------
429
608
 
430
- export const InvokeTransactionReceipt = Schema.Struct({
431
- _tag: tag("invoke"),
432
- invoke: Schema.Struct({}),
609
+ export const TransactionReceiptMeta = MessageCodec({
610
+ transactionIndex: RequiredCodec(NumberCodec),
611
+ transactionHash: RequiredCodec(FieldElement),
612
+ actualFee: RequiredCodec(FeePayment),
613
+ executionResources: RequiredCodec(ExecutionResources),
614
+ executionResult: RequiredCodec(
615
+ OneOfCodec({
616
+ succeeded: ExecutionSucceeded,
617
+ reverted: ExecutionReverted,
618
+ }),
619
+ ),
433
620
  });
434
621
 
435
- export type InvokeTransactionReceipt = typeof InvokeTransactionReceipt.Type;
622
+ export type TransactionReceiptMeta = Readonly<
623
+ CodecType<typeof TransactionReceiptMeta>
624
+ >;
436
625
 
437
- export const L1HandlerTransactionReceipt = Schema.Struct({
438
- _tag: tag("l1Handler"),
439
- l1Handler: Schema.Struct({
440
- messageHash: Schema.Uint8ArrayFromSelf,
441
- }),
442
- });
626
+ // -----------------------------------------------------
627
+
628
+ export const InvokeTransactionReceipt = MessageCodec({});
629
+
630
+ export type InvokeTransactionReceipt = Readonly<
631
+ CodecType<typeof InvokeTransactionReceipt>
632
+ >;
443
633
 
444
- export type L1HandlerTransactionReceipt =
445
- typeof L1HandlerTransactionReceipt.Type;
634
+ // -----------------------------------------------------
446
635
 
447
- export const DeclareTransactionReceipt = Schema.Struct({
448
- _tag: tag("declare"),
449
- declare: Schema.Struct({}),
636
+ export const L1HandlerTransactionReceipt = MessageCodec({
637
+ messageHash: RequiredCodec(Uint8ArrayCodec),
450
638
  });
451
639
 
452
- export type DeclareTransactionReceipt = typeof DeclareTransactionReceipt.Type;
640
+ export type L1HandlerTransactionReceipt = Readonly<
641
+ CodecType<typeof L1HandlerTransactionReceipt>
642
+ >;
453
643
 
454
- export const DeployTransactionReceipt = Schema.Struct({
455
- _tag: tag("deploy"),
456
- deploy: Schema.Struct({
457
- contractAddress: FieldElement,
458
- }),
644
+ // -----------------------------------------------------
645
+
646
+ export const DeclareTransactionReceipt = MessageCodec({});
647
+
648
+ export type DeclareTransactionReceipt = Readonly<
649
+ CodecType<typeof DeclareTransactionReceipt>
650
+ >;
651
+
652
+ // -----------------------------------------------------
653
+
654
+ export const DeployTransactionReceipt = MessageCodec({
655
+ contractAddress: RequiredCodec(FieldElement),
459
656
  });
460
657
 
461
- export type DeployTransactionReceipt = typeof DeployTransactionReceipt.Type;
658
+ export type DeployTransactionReceipt = Readonly<
659
+ CodecType<typeof DeployTransactionReceipt>
660
+ >;
661
+
662
+ // -----------------------------------------------------
462
663
 
463
- export const DeployAccountTransactionReceipt = Schema.Struct({
464
- _tag: tag("deployAccount"),
465
- deployAccount: Schema.Struct({
466
- contractAddress: FieldElement,
467
- }),
664
+ export const DeployAccountTransactionReceipt = MessageCodec({
665
+ contractAddress: RequiredCodec(FieldElement),
468
666
  });
469
667
 
470
- export type DeployAccountTransactionReceipt =
471
- typeof DeployAccountTransactionReceipt.Type;
668
+ export type DeployAccountTransactionReceipt = Readonly<
669
+ CodecType<typeof DeployAccountTransactionReceipt>
670
+ >;
671
+
672
+ // -----------------------------------------------------
472
673
 
473
674
  /** A transaction receipt.
474
675
  *
475
676
  * @prop meta Transaction receipt metadata.
476
677
  * @prop receipt Transaction-specific receipt.
477
678
  */
478
- export const TransactionReceipt = Schema.Struct({
479
- filterIds: Schema.Array(Schema.Number),
480
- meta: TransactionReceiptMeta,
481
- receipt: Schema.Union(
482
- InvokeTransactionReceipt,
483
- L1HandlerTransactionReceipt,
484
- DeclareTransactionReceipt,
485
- DeployTransactionReceipt,
486
- DeployAccountTransactionReceipt,
679
+ export const TransactionReceipt = MessageCodec({
680
+ filterIds: ArrayCodec(NumberCodec),
681
+ meta: RequiredCodec(TransactionReceiptMeta),
682
+ receipt: RequiredCodec(
683
+ OneOfCodec({
684
+ invoke: InvokeTransactionReceipt,
685
+ l1Handler: L1HandlerTransactionReceipt,
686
+ declare: DeclareTransactionReceipt,
687
+ deploy: DeployTransactionReceipt,
688
+ deployAccount: DeployAccountTransactionReceipt,
689
+ }),
487
690
  ),
488
691
  });
489
692
 
490
- export type TransactionReceipt = typeof TransactionReceipt.Type;
693
+ export type TransactionReceipt = Readonly<CodecType<typeof TransactionReceipt>>;
694
+
695
+ // -----------------------------------------------------
491
696
 
492
697
  /** A transaction event.
493
698
  *
@@ -500,22 +705,25 @@ export type TransactionReceipt = typeof TransactionReceipt.Type;
500
705
  * @prop transactionStatus The transaction status.
501
706
  * @prop eventIndexInTransaction The event index in the transaction.
502
707
  */
503
- export const Event = Schema.Struct({
504
- filterIds: Schema.Array(Schema.Number),
505
- address: FieldElement,
506
- keys: Schema.Array(FieldElement),
507
- data: Schema.Array(FieldElement),
508
- eventIndex: Schema.Number,
509
- transactionIndex: Schema.Number,
510
- transactionHash: FieldElement,
511
- transactionStatus: TransactionStatus,
512
- eventIndexInTransaction: Schema.Number,
708
+ export const Event = MessageCodec({
709
+ filterIds: ArrayCodec(NumberCodec),
710
+ address: RequiredCodec(FieldElement),
711
+ keys: ArrayCodec(FieldElement),
712
+ data: ArrayCodec(FieldElement),
713
+ eventIndex: RequiredCodec(NumberCodec),
714
+ transactionIndex: RequiredCodec(NumberCodec),
715
+ transactionHash: RequiredCodec(FieldElement),
716
+ transactionStatus: RequiredCodec(TransactionStatus),
717
+ eventIndexInTransaction: RequiredCodec(NumberCodec),
513
718
  });
514
719
 
515
- export type Event = typeof Event.Type;
720
+ export type Event = Readonly<CodecType<typeof Event>>;
721
+
722
+ // -----------------------------------------------------
516
723
 
517
724
  /** A message from the L2 to the L1.
518
725
  *
726
+ * @prop filterIds The filter IDs.
519
727
  * @prop fromAddress The address on L2 that sent the message.
520
728
  * @prop toAddress The address on L1 that will receive the message.
521
729
  * @prop payload The message payload.
@@ -525,136 +733,183 @@ export type Event = typeof Event.Type;
525
733
  * @prop transactionStatus The transaction status.
526
734
  * @prop messageIndexInTransaction The message index in the transaction.
527
735
  */
528
- export const MessageToL1 = Schema.Struct({
529
- filterIds: Schema.Array(Schema.Number),
530
- fromAddress: FieldElement,
531
- toAddress: FieldElement,
532
- payload: Schema.Array(FieldElement),
533
- messageIndex: Schema.Number,
534
- transactionIndex: Schema.Number,
535
- transactionHash: FieldElement,
536
- transactionStatus: TransactionStatus,
537
- messageIndexInTransaction: Schema.Number,
736
+ export const MessageToL1 = MessageCodec({
737
+ filterIds: ArrayCodec(NumberCodec),
738
+ fromAddress: RequiredCodec(FieldElement),
739
+ toAddress: RequiredCodec(FieldElement),
740
+ payload: ArrayCodec(FieldElement),
741
+ messageIndex: RequiredCodec(NumberCodec),
742
+ transactionIndex: RequiredCodec(NumberCodec),
743
+ transactionHash: RequiredCodec(FieldElement),
744
+ transactionStatus: RequiredCodec(TransactionStatus),
745
+ messageIndexInTransaction: RequiredCodec(NumberCodec),
538
746
  });
539
747
 
540
- export type MessageToL1 = typeof MessageToL1.Type;
748
+ export type MessageToL1 = Readonly<CodecType<typeof MessageToL1>>;
749
+
750
+ // -----------------------------------------------------
541
751
 
542
752
  /** An entry in the storage diff.
543
753
  *
544
754
  * @prop key The storage location.
545
755
  * @prop value The new value at the storage location.
546
756
  */
547
- export const StorageEntry = Schema.Struct({
548
- key: FieldElement,
549
- value: FieldElement,
757
+ export const StorageEntry = MessageCodec({
758
+ key: RequiredCodec(FieldElement),
759
+ value: RequiredCodec(FieldElement),
550
760
  });
551
761
 
552
- export type StorageEntry = typeof StorageEntry.Type;
762
+ export type StorageEntry = Readonly<CodecType<typeof StorageEntry>>;
763
+
764
+ // -----------------------------------------------------
553
765
 
554
766
  /** Storage diff.
555
767
  *
556
768
  * @prop contractAddress The contract address.
557
769
  * @prop storageEntries The entries that changed.
558
770
  */
559
- export const StorageDiff = Schema.Struct({
560
- filterIds: Schema.Array(Schema.Number),
561
- contractAddress: FieldElement,
562
- storageEntries: Schema.Array(StorageEntry),
771
+ export const StorageDiff = MessageCodec({
772
+ filterIds: ArrayCodec(NumberCodec),
773
+ contractAddress: RequiredCodec(FieldElement),
774
+ storageEntries: ArrayCodec(StorageEntry),
563
775
  });
564
776
 
565
- export type StorageDiff = typeof StorageDiff.Type;
777
+ export type StorageDiff = Readonly<CodecType<typeof StorageDiff>>;
778
+
779
+ // -----------------------------------------------------
566
780
 
567
781
  /** A new class declared.
568
782
  *
569
783
  * @prop classHash The class hash.
570
784
  * @prop compiledClassHash The compiled class hash. If undefined, it's the result of a deprecated Cairo 0 declaration.
571
785
  */
572
- export const DeclaredClass = Schema.Struct({
573
- _tag: tag("declaredClass"),
574
- declaredClass: Schema.Struct({
575
- classHash: Schema.optional(FieldElement),
576
- compiledClassHash: Schema.optional(FieldElement),
577
- }),
786
+ export const DeclaredClass = MessageCodec({
787
+ classHash: OptionalCodec(FieldElement),
788
+ compiledClassHash: OptionalCodec(FieldElement),
578
789
  });
579
790
 
580
- export type DeclaredClass = typeof DeclaredClass.Type;
791
+ export type DeclaredClass = Readonly<CodecType<typeof DeclaredClass>>;
792
+
793
+ // -----------------------------------------------------
581
794
 
582
795
  /** A class replaced.
583
796
  *
584
797
  * @prop contractAddress The contract address.
585
798
  * @prop classHash The class new hash.
586
799
  */
587
- export const ReplacedClass = Schema.Struct({
588
- _tag: tag("replacedClass"),
589
- replacedClass: Schema.Struct({
590
- contractAddress: Schema.optional(FieldElement),
591
- classHash: Schema.optional(FieldElement),
592
- }),
800
+ export const ReplacedClass = MessageCodec({
801
+ contractAddress: OptionalCodec(FieldElement),
802
+ classHash: OptionalCodec(FieldElement),
593
803
  });
594
804
 
595
- export type ReplacedClass = typeof ReplacedClass.Type;
805
+ export type ReplacedClass = Readonly<CodecType<typeof ReplacedClass>>;
806
+
807
+ // -----------------------------------------------------
596
808
 
597
809
  /** A contract deployed.
598
810
  *
599
811
  * @prop contractAddress The contract address.
600
812
  * @prop classHash The class hash.
601
813
  */
602
- export const DeployedContract = Schema.Struct({
603
- _tag: tag("deployedContract"),
604
- deployedContract: Schema.Struct({
605
- contractAddress: Schema.optional(FieldElement),
606
- classHash: Schema.optional(FieldElement),
607
- }),
814
+ export const DeployedContract = MessageCodec({
815
+ contractAddress: OptionalCodec(FieldElement),
816
+ classHash: OptionalCodec(FieldElement),
608
817
  });
609
818
 
610
- export type DeployedContract = typeof DeployedContract.Type;
819
+ export type DeployedContract = Readonly<CodecType<typeof DeployedContract>>;
820
+
821
+ // -----------------------------------------------------
611
822
 
612
823
  /** A contract change.
613
824
  *
614
- * @prop contractAddress The contract address.
825
+ * @prop filterIds The filter IDs.
615
826
  * @prop change The change.
616
827
  */
617
- export const ContractChange = Schema.Struct({
618
- filterIds: Schema.Array(Schema.Number),
619
- change: Schema.Union(DeclaredClass, ReplacedClass, DeployedContract),
828
+ export const ContractChange = MessageCodec({
829
+ filterIds: ArrayCodec(NumberCodec),
830
+ change: RequiredCodec(
831
+ OneOfCodec({
832
+ declaredClass: DeclaredClass,
833
+ replacedClass: ReplacedClass,
834
+ deployedContract: DeployedContract,
835
+ }),
836
+ ),
620
837
  });
621
838
 
622
- export type ContractChange = typeof ContractChange.Type;
839
+ export type ContractChange = Readonly<CodecType<typeof ContractChange>>;
840
+
841
+ // -----------------------------------------------------
623
842
 
624
843
  /** A nonce update.
625
844
  *
626
845
  * @prop contractAddress The contract address.
627
846
  * @prop nonce The new nonce.
628
847
  */
629
- export const NonceUpdate = Schema.Struct({
630
- filterIds: Schema.Array(Schema.Number),
631
- contractAddress: FieldElement,
632
- nonce: FieldElement,
848
+ export const NonceUpdate = MessageCodec({
849
+ filterIds: ArrayCodec(NumberCodec),
850
+ contractAddress: RequiredCodec(FieldElement),
851
+ nonce: RequiredCodec(FieldElement),
633
852
  });
634
853
 
635
- export type NonceUpdate = typeof NonceUpdate.Type;
636
-
637
- /** Trace call type. */
638
- export const CallType = Schema.transform(
639
- Schema.Enums(proto.data.CallType),
640
- Schema.Literal("libraryCall", "call", "delegate", "unknown"),
641
- {
642
- decode(value) {
643
- const enumMap = {
644
- [proto.data.CallType.LIBRARY_CALL]: "libraryCall",
645
- [proto.data.CallType.CALL]: "call",
646
- [proto.data.CallType.DELEGATE]: "delegate",
647
- [proto.data.CallType.UNSPECIFIED]: "unknown",
648
- [proto.data.CallType.UNRECOGNIZED]: "unknown",
649
- } as const;
650
-
651
- return enumMap[value] ?? "unknown";
652
- },
653
- encode(value) {
654
- throw new Error("encode: not implemented");
655
- },
854
+ export type NonceUpdate = Readonly<CodecType<typeof NonceUpdate>>;
855
+
856
+ // -----------------------------------------------------
857
+
858
+ export const CallType: Codec<
859
+ "libraryCall" | "call" | "delegate" | "unknown",
860
+ proto.data.CallType
861
+ > = {
862
+ encode(x) {
863
+ switch (x) {
864
+ case "libraryCall":
865
+ return proto.data.CallType.LIBRARY_CALL;
866
+ case "call":
867
+ return proto.data.CallType.CALL;
868
+ case "delegate":
869
+ return proto.data.CallType.DELEGATE;
870
+ case "unknown":
871
+ return proto.data.CallType.UNSPECIFIED;
872
+ default:
873
+ return proto.data.CallType.UNRECOGNIZED;
874
+ }
875
+ },
876
+ decode(p) {
877
+ const enumMap = {
878
+ [proto.data.CallType.LIBRARY_CALL]: "libraryCall",
879
+ [proto.data.CallType.CALL]: "call",
880
+ [proto.data.CallType.DELEGATE]: "delegate",
881
+ [proto.data.CallType.UNSPECIFIED]: "unknown",
882
+ [proto.data.CallType.UNRECOGNIZED]: "unknown",
883
+ } as const;
884
+
885
+ return enumMap[p] ?? "unknown";
656
886
  },
657
- );
887
+ };
888
+
889
+ export type CallType = CodecType<typeof CallType>;
890
+
891
+ // -----------------------------------------------------
892
+
893
+ const _FunctionInvocationCodec = MessageCodec({
894
+ contractAddress: RequiredCodec(FieldElement),
895
+ entryPointSelector: RequiredCodec(FieldElement),
896
+ calldata: ArrayCodec(FieldElement),
897
+ callerAddress: RequiredCodec(FieldElement),
898
+ classHash: RequiredCodec(FieldElement),
899
+ callType: RequiredCodec(CallType),
900
+ result: ArrayCodec(FieldElement),
901
+ events: ArrayCodec(NumberCodec),
902
+ messages: ArrayCodec(NumberCodec),
903
+ });
904
+
905
+ /**
906
+ * @note This is a recursive type.
907
+ */
908
+ export type FunctionInvocation = Evaluate<
909
+ CodecType<typeof _FunctionInvocationCodec> & {
910
+ calls: FunctionInvocation[];
911
+ }
912
+ >;
658
913
 
659
914
  /** A function invocation.
660
915
  *
@@ -669,126 +924,139 @@ export const CallType = Schema.transform(
669
924
  * @prop events The events index in the current transaction.
670
925
  * @prop messages The messages index in the current transaction.
671
926
  */
672
- export class FunctionInvocation extends Schema.Class<FunctionInvocation>(
673
- "FunctionInvocation",
674
- )({
675
- contractAddress: FieldElement,
676
- entryPointSelector: FieldElement,
677
- calldata: Schema.Array(FieldElement),
678
- callerAddress: FieldElement,
679
- classHash: FieldElement,
680
- callType: CallType,
681
- result: Schema.Array(FieldElement),
682
- calls: Schema.suspend(
683
- // biome-ignore lint/suspicious/noExplicitAny: not possible otherwise
684
- (): Schema.Schema<any> => Schema.Array(FunctionInvocation),
685
- ),
686
- events: Schema.Array(Schema.Number),
687
- messages: Schema.Array(Schema.Number),
688
- }) {}
927
+ const FunctionInvocationCodec: Codec<
928
+ FunctionInvocation,
929
+ proto.data.FunctionInvocation
930
+ > = {
931
+ encode(x) {
932
+ const { calls, ...rest } = x;
933
+ const encodedCalls = calls.map(FunctionInvocationCodec.encode);
934
+ const encodedRest = _FunctionInvocationCodec.encode(rest);
935
+ return { calls: encodedCalls, ...encodedRest };
936
+ },
937
+ decode(p) {
938
+ const { calls = [], ...rest } = p;
939
+ const decodedCalls = calls.map(FunctionInvocationCodec.decode);
940
+ const decodedRest = _FunctionInvocationCodec.decode(rest);
941
+ return { ...decodedRest, calls: decodedCalls };
942
+ },
943
+ };
944
+
945
+ // -----------------------------------------------------
689
946
 
690
947
  /** A successful invocation of the __execute__ call.
691
948
  *
692
- * @prop success The call.
949
+ * The call.
693
950
  */
694
- export const ExecuteInvocationSuccess = Schema.Struct({
695
- _tag: tag("success"),
696
- success: FunctionInvocation,
697
- });
951
+ export const ExecuteInvocationSuccess = FunctionInvocationCodec;
952
+
953
+ // -----------------------------------------------------
698
954
 
699
955
  /** A failed invocation of the __execute__ call.
700
956
  *
701
957
  * @prop reason The reason for the failure.
702
958
  */
703
- export const ExecuteInvocationReverted = Schema.Struct({
704
- _tag: tag("reverted"),
705
- reverted: Schema.Struct({
706
- reason: Schema.optional(Schema.String),
707
- }),
959
+ export const ExecuteInvocationReverted = MessageCodec({
960
+ reason: OptionalCodec(StringCodec),
708
961
  });
709
962
 
963
+ // -----------------------------------------------------
964
+
710
965
  /** Trace for invoke transactions.
711
966
  *
712
967
  * @prop validateInvocation The __validate__ call.
713
968
  * @prop executeInvocation The __execute__ call.
714
969
  * @prop feeTransferInvocation The __fee_transfer__ call.
715
970
  */
716
- export const InvokeTransactionTrace = Schema.Struct({
717
- _tag: tag("invoke"),
718
- invoke: Schema.Struct({
719
- validateInvocation: Schema.optional(FunctionInvocation),
720
- executeInvocation: Schema.Union(
721
- ExecuteInvocationReverted,
722
- ExecuteInvocationSuccess,
723
- ),
724
- feeTransferInvocation: Schema.optional(FunctionInvocation),
725
- }),
971
+ export const InvokeTransactionTrace = MessageCodec({
972
+ validateInvocation: OptionalCodec(FunctionInvocationCodec),
973
+ executeInvocation: RequiredCodec(
974
+ OneOfCodec({
975
+ success: ExecuteInvocationSuccess,
976
+ reverted: ExecuteInvocationReverted,
977
+ }),
978
+ ),
979
+ feeTransferInvocation: OptionalCodec(FunctionInvocationCodec),
726
980
  });
727
981
 
728
- export type InvokeTransactionTrace = typeof InvokeTransactionTrace.Type;
982
+ export type InvokeTransactionTrace = Readonly<
983
+ CodecType<typeof InvokeTransactionTrace>
984
+ >;
985
+
986
+ // -----------------------------------------------------
729
987
 
730
988
  /** Trace for declare transactions.
731
989
  *
732
990
  * @prop validateInvocation The __validate__ call.
733
991
  * @prop feeTransferInvocation The __fee_transfer__ call.
734
992
  */
735
- export const DeclareTransactionTrace = Schema.Struct({
736
- _tag: tag("declare"),
737
- declare: Schema.Struct({
738
- validateInvocation: Schema.optional(FunctionInvocation),
739
- feeTransferInvocation: Schema.optional(FunctionInvocation),
740
- }),
993
+ export const DeclareTransactionTrace = MessageCodec({
994
+ validateInvocation: OptionalCodec(FunctionInvocationCodec),
995
+ feeTransferInvocation: OptionalCodec(FunctionInvocationCodec),
741
996
  });
742
997
 
743
- export type DeclareTransactionTrace = typeof DeclareTransactionTrace.Type;
998
+ export type DeclareTransactionTrace = Readonly<
999
+ CodecType<typeof DeclareTransactionTrace>
1000
+ >;
1001
+
1002
+ // -----------------------------------------------------
744
1003
 
745
1004
  /** Trace for deploy account transactions.
746
1005
  *
747
1006
  * @prop validateInvocation The __validate__ call.
748
- * @prop constructorInvocation The __constructor__ call.
1007
+ * @prop constructorInvocation The __constructor__ invocation.
749
1008
  * @prop feeTransferInvocation The __fee_transfer__ call.
750
1009
  */
751
- export const DeployAccountTransactionTrace = Schema.Struct({
752
- _tag: tag("deployAccount"),
753
- deployAccount: Schema.Struct({
754
- validateInvocation: Schema.optional(FunctionInvocation),
755
- constructorInvocation: Schema.optional(FunctionInvocation),
756
- feeTransferInvocation: Schema.optional(FunctionInvocation),
757
- }),
1010
+ export const DeployAccountTransactionTrace = MessageCodec({
1011
+ validateInvocation: OptionalCodec(FunctionInvocationCodec),
1012
+ constructorInvocation: OptionalCodec(FunctionInvocationCodec),
1013
+ feeTransferInvocation: OptionalCodec(FunctionInvocationCodec),
758
1014
  });
759
1015
 
760
- export type DeployAccountTransactionTrace =
761
- typeof DeployAccountTransactionTrace.Type;
1016
+ export type DeployAccountTransactionTrace = Readonly<
1017
+ CodecType<typeof DeployAccountTransactionTrace>
1018
+ >;
1019
+
1020
+ // -----------------------------------------------------
762
1021
 
763
1022
  /** Trace for L1 handler transactions.
764
1023
  *
765
1024
  * @prop functionInvocation The L1 handler function invocation.
766
1025
  */
767
- export const L1HandlerTransactionTrace = Schema.Struct({
768
- _tag: tag("l1Handler"),
769
- l1Handler: Schema.Struct({
770
- functionInvocation: Schema.optional(FunctionInvocation),
771
- }),
1026
+ export const L1HandlerTransactionTrace = MessageCodec({
1027
+ functionInvocation: OptionalCodec(FunctionInvocationCodec),
772
1028
  });
773
1029
 
1030
+ export type L1HandlerTransactionTrace = Readonly<
1031
+ CodecType<typeof L1HandlerTransactionTrace>
1032
+ >;
1033
+
1034
+ // -----------------------------------------------------
1035
+
774
1036
  /** A transaction trace.
775
1037
  *
776
- * @prop transactionHash The hash of the trace's transaction.
777
- * @prp traceRoot the trace root entry.
778
- */
779
- export const TransactionTrace = Schema.Struct({
780
- filterIds: Schema.Array(Schema.Number),
781
- transactionIndex: Schema.Number,
782
- transactionHash: FieldElement,
783
- traceRoot: Schema.Union(
784
- InvokeTransactionTrace,
785
- DeclareTransactionTrace,
786
- DeployAccountTransactionTrace,
787
- L1HandlerTransactionTrace,
1038
+ * @prop filterIds The filter IDs.
1039
+ * @prop transactionIndex The transaction index.
1040
+ * @prop transactionHash The transaction hash.
1041
+ * @prop traceRoot The trace root.
1042
+ */
1043
+ export const TransactionTrace = MessageCodec({
1044
+ filterIds: ArrayCodec(NumberCodec),
1045
+ transactionIndex: RequiredCodec(NumberCodec),
1046
+ transactionHash: RequiredCodec(FieldElement),
1047
+ traceRoot: RequiredCodec(
1048
+ OneOfCodec({
1049
+ invoke: InvokeTransactionTrace,
1050
+ declare: DeclareTransactionTrace,
1051
+ deployAccount: DeployAccountTransactionTrace,
1052
+ l1Handler: L1HandlerTransactionTrace,
1053
+ }),
788
1054
  ),
789
1055
  });
790
1056
 
791
- export type TransactionTrace = typeof TransactionTrace.Type;
1057
+ export type TransactionTrace = Readonly<CodecType<typeof TransactionTrace>>;
1058
+
1059
+ // -----------------------------------------------------
792
1060
 
793
1061
  /** A block.
794
1062
  *
@@ -800,37 +1068,31 @@ export type TransactionTrace = typeof TransactionTrace.Type;
800
1068
  * @prop traces The transaction traces.
801
1069
  * @prop storageDiffs The changes to the storage.
802
1070
  * @prop contractChanges The changes to contracts and classes.
1071
+ * @prop nonceUpdates The nonce updates.
803
1072
  */
804
- export const Block = Schema.Struct({
805
- header: BlockHeader,
806
- transactions: Schema.Array(Transaction),
807
- receipts: Schema.Array(TransactionReceipt),
808
- events: Schema.Array(Event),
809
- messages: Schema.Array(MessageToL1),
810
- traces: Schema.Array(TransactionTrace),
811
- storageDiffs: Schema.Array(StorageDiff),
812
- contractChanges: Schema.Array(ContractChange),
813
- nonceUpdates: Schema.Array(NonceUpdate),
1073
+ export const Block = MessageCodec({
1074
+ header: RequiredCodec(BlockHeader),
1075
+ transactions: ArrayCodec(Transaction),
1076
+ receipts: ArrayCodec(TransactionReceipt),
1077
+ events: ArrayCodec(Event),
1078
+ messages: ArrayCodec(MessageToL1),
1079
+ traces: ArrayCodec(TransactionTrace),
1080
+ storageDiffs: ArrayCodec(StorageDiff),
1081
+ contractChanges: ArrayCodec(ContractChange),
1082
+ nonceUpdates: ArrayCodec(NonceUpdate),
814
1083
  });
815
1084
 
816
- export type Block = typeof Block.Type;
1085
+ export type Block = Readonly<CodecType<typeof Block>>;
817
1086
 
818
- export const BlockFromBytes = Schema.transform(
819
- Schema.Uint8ArrayFromSelf,
820
- Schema.NullOr(Block),
821
- {
822
- strict: false,
823
- decode(value) {
824
- if (value.length === 0) {
825
- return null;
826
- }
827
- return proto.data.Block.decode(value);
828
- },
829
- encode(value) {
830
- if (value === null) {
831
- return new Uint8Array();
832
- }
833
- return proto.data.Block.encode(value).finish();
834
- },
1087
+ // -----------------------------------------------------
1088
+
1089
+ export const BlockFromBytes: Codec<Block, Uint8Array> = {
1090
+ encode(x) {
1091
+ const block = Block.encode(x);
1092
+ return proto.data.Block.encode(block).finish();
1093
+ },
1094
+ decode(p) {
1095
+ const block = proto.data.Block.decode(p);
1096
+ return Block.decode(block);
835
1097
  },
836
- );
1098
+ };