@apibara/starknet 2.1.0-beta.3 → 2.1.0-beta.30

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,101 +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
- /** How data is posted to L1. */
18
- export const L1DataAvailabilityMode = Schema.transform(
19
- Schema.Enums(proto.data.L1DataAvailabilityMode),
20
- Schema.Literal("blob", "calldata", "unknown"),
21
- {
22
- decode(value) {
23
- const enumMap = {
24
- [proto.data.L1DataAvailabilityMode.CALLDATA]: "calldata",
25
- [proto.data.L1DataAvailabilityMode.BLOB]: "blob",
26
- [proto.data.L1DataAvailabilityMode.UNSPECIFIED]: "unknown",
27
- [proto.data.L1DataAvailabilityMode.UNRECOGNIZED]: "unknown",
28
- } as const;
29
+ export type ResourcePrice = Readonly<CodecType<typeof ResourcePrice>>;
29
30
 
30
- return enumMap[value] ?? "unknown";
31
- },
32
- encode(value) {
33
- throw new Error("encode: not implemented");
34
- },
31
+ // -----------------------------------------------------
32
+
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";
59
+ },
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
+ }
35
82
  },
36
- );
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
+ };
37
94
 
38
- export const TransactionStatus = Schema.transform(
39
- Schema.Enums(proto.data.TransactionStatus),
40
- Schema.Literal("unknown", "succeeded", "reverted"),
41
- {
42
- decode(value) {
43
- const enumMap = {
44
- [proto.data.TransactionStatus.SUCCEEDED]: "succeeded",
45
- [proto.data.TransactionStatus.REVERTED]: "reverted",
46
- [proto.data.TransactionStatus.UNSPECIFIED]: "unknown",
47
- [proto.data.TransactionStatus.UNRECOGNIZED]: "unknown",
48
- } as const;
95
+ export type TransactionStatus = CodecType<typeof TransactionStatus>;
49
96
 
50
- return enumMap[value] ?? "unknown";
51
- },
52
- encode(value) {
53
- throw new Error("encode: not implemented");
54
- },
97
+ // -----------------------------------------------------
98
+
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}`) };
55
106
  },
56
- );
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}`);
111
+ },
112
+ };
57
113
 
58
- export type TransactionStatus = typeof TransactionStatus.Type;
114
+ export type U128 = CodecType<typeof U128>;
59
115
 
60
- export const U128 = Schema.transform(
61
- Schema.Struct({
62
- x0: Schema.BigIntFromSelf,
63
- x1: Schema.BigIntFromSelf,
64
- }),
65
- Schema.BigIntFromSelf,
66
- {
67
- decode(value) {
68
- const low = value.x0.toString(16).padStart(16, "0");
69
- const high = value.x1.toString(16).padStart(16, "0");
70
- return BigInt(`0x${low}${high}`);
71
- },
72
- encode(value) {
73
- throw new Error("encode: not implemented");
74
- },
75
- },
76
- );
116
+ // -----------------------------------------------------
77
117
 
78
- export const ResourceBounds = Schema.Struct({
79
- maxAmount: Schema.BigIntFromSelf,
80
- maxPricePerUnit: U128,
118
+ /** Resource bounds. */
119
+ export const ResourceBounds = MessageCodec({
120
+ maxAmount: RequiredCodec(BigIntCodec),
121
+ maxPricePerUnit: RequiredCodec(U128),
81
122
  });
82
123
 
83
- export const ResourceBoundsMapping = Schema.Struct({
84
- l1Gas: ResourceBounds,
85
- l2Gas: ResourceBounds,
86
- });
124
+ export type ResourceBounds = Readonly<CodecType<typeof ResourceBounds>>;
87
125
 
88
- export const DataAvailabilityMode = Schema.transform(
89
- Schema.Enums(proto.data.DataAvailabilityMode),
90
- Schema.Literal("l1", "l2", "unknown"),
91
- {
92
- decode(value) {
93
- const enumMap = {
94
- [proto.data.DataAvailabilityMode.L1]: "l1",
95
- [proto.data.DataAvailabilityMode.L2]: "l2",
96
- [proto.data.DataAvailabilityMode.UNSPECIFIED]: "unknown",
97
- [proto.data.DataAvailabilityMode.UNRECOGNIZED]: "unknown",
98
- } as const;
126
+ // -----------------------------------------------------
99
127
 
100
- return enumMap[value] ?? "unknown";
101
- },
102
- encode(value) {
103
- throw new Error("encode: not implemented");
104
- },
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
+ }
156
+ },
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";
105
166
  },
106
- );
167
+ };
168
+
169
+ export type DataAvailabilityMode = CodecType<typeof DataAvailabilityMode>;
170
+
171
+ // -----------------------------------------------------
107
172
 
108
173
  /** Starknet block header.
109
174
  *
@@ -118,20 +183,22 @@ export const DataAvailabilityMode = Schema.transform(
118
183
  * @prop l1DataGasPrice Blob gas price.
119
184
  * @prop l1DataAvailabilityMode How data is posted to L1.
120
185
  */
121
- export const BlockHeader = Schema.Struct({
122
- blockHash: Schema.optional(FieldElement),
123
- parentBlockHash: FieldElement,
124
- blockNumber: Schema.BigIntFromSelf,
125
- sequencerAddress: FieldElement,
126
- newRoot: Schema.optional(FieldElement),
127
- timestamp: Schema.DateFromSelf,
128
- starknetVersion: Schema.String,
129
- l1GasPrice: ResourcePrice,
130
- l1DataGasPrice: ResourcePrice,
131
- 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),
132
197
  });
133
198
 
134
- export type BlockHeader = typeof BlockHeader.Type;
199
+ export type BlockHeader = Readonly<CodecType<typeof BlockHeader>>;
200
+
201
+ // -----------------------------------------------------
135
202
 
136
203
  /** Transaction metadata.
137
204
  *
@@ -141,178 +208,327 @@ export type BlockHeader = typeof BlockHeader.Type;
141
208
  * @prop transactionHash The transaction hash.
142
209
  * @prop transactionStatus The transaction status.
143
210
  */
144
- export const TransactionMeta = Schema.Struct({
145
- transactionIndex: Schema.Number,
146
- transactionHash: FieldElement,
147
- transactionStatus: TransactionStatus,
148
- });
149
-
150
- export type TransactionMeta = typeof TransactionMeta.Type;
151
-
152
- export const InvokeTransactionV0 = Schema.Struct({
153
- _tag: tag("invokeV0"),
154
- invokeV0: Schema.Struct({
155
- maxFee: FieldElement,
156
- signature: Schema.Array(FieldElement),
157
- contractAddress: FieldElement,
158
- entryPointSelector: FieldElement,
159
- calldata: Schema.Array(FieldElement),
160
- }),
161
- });
162
-
163
- export const InvokeTransactionV1 = Schema.Struct({
164
- _tag: tag("invokeV1"),
165
- invokeV1: Schema.Struct({
166
- senderAddress: FieldElement,
167
- calldata: Schema.Array(FieldElement),
168
- maxFee: FieldElement,
169
- signature: Schema.Array(FieldElement),
170
- nonce: FieldElement,
171
- }),
172
- });
173
-
174
- export const InvokeTransactionV3 = Schema.Struct({
175
- _tag: tag("invokeV3"),
176
- invokeV3: Schema.Struct({
177
- senderAddress: FieldElement,
178
- calldata: Schema.Array(FieldElement),
179
- signature: Schema.Array(FieldElement),
180
- nonce: FieldElement,
181
- resourceBounds: ResourceBoundsMapping,
182
- tip: Schema.BigIntFromSelf,
183
- paymasterData: Schema.Array(FieldElement),
184
- accountDeploymentData: Schema.Array(FieldElement),
185
- nonceDataAvailabilityMode: DataAvailabilityMode,
186
- feeDataAvailabilityMode: DataAvailabilityMode,
187
- }),
188
- });
189
-
190
- export const L1HandlerTransaction = Schema.Struct({
191
- _tag: tag("l1Handler"),
192
- l1Handler: Schema.Struct({
193
- nonce: Schema.BigIntFromSelf,
194
- contractAddress: FieldElement,
195
- entryPointSelector: FieldElement,
196
- calldata: Schema.Array(FieldElement),
197
- }),
198
- });
199
-
200
- export const DeployTransaction = Schema.Struct({
201
- _tag: tag("deploy"),
202
- deploy: Schema.Struct({
203
- contractAddressSalt: FieldElement,
204
- constructorCalldata: Schema.Array(FieldElement),
205
- classHash: FieldElement,
206
- }),
207
- });
208
-
209
- export const DeclareTransactionV0 = Schema.Struct({
210
- _tag: tag("declareV0"),
211
- declareV0: Schema.Struct({
212
- senderAddress: FieldElement,
213
- maxFee: FieldElement,
214
- signature: Schema.Array(FieldElement),
215
- classHash: FieldElement,
216
- }),
217
- });
218
-
219
- export const DeclareTransactionV1 = Schema.Struct({
220
- _tag: tag("declareV1"),
221
- declareV1: Schema.Struct({
222
- senderAddress: FieldElement,
223
- maxFee: FieldElement,
224
- signature: Schema.Array(FieldElement),
225
- nonce: FieldElement,
226
- classHash: FieldElement,
227
- }),
228
- });
229
-
230
- export const DeclareTransactionV2 = Schema.Struct({
231
- _tag: tag("declareV2"),
232
- declareV2: Schema.Struct({
233
- senderAddress: FieldElement,
234
- compiledClassHash: FieldElement,
235
- maxFee: FieldElement,
236
- signature: Schema.Array(FieldElement),
237
- nonce: FieldElement,
238
- classHash: FieldElement,
239
- }),
240
- });
241
-
242
- export const DeclareTransactionV3 = Schema.Struct({
243
- _tag: tag("declareV3"),
244
- declareV3: Schema.Struct({
245
- senderAddress: FieldElement,
246
- compiledClassHash: FieldElement,
247
- signature: Schema.Array(FieldElement),
248
- nonce: FieldElement,
249
- classHash: FieldElement,
250
- resourceBounds: ResourceBoundsMapping,
251
- tip: Schema.BigIntFromSelf,
252
- paymasterData: Schema.Array(FieldElement),
253
- accountDeploymentData: Schema.Array(FieldElement),
254
- nonceDataAvailabilityMode: DataAvailabilityMode,
255
- feeDataAvailabilityMode: DataAvailabilityMode,
256
- }),
257
- });
258
-
259
- export const DeployAccountTransactionV1 = Schema.Struct({
260
- _tag: tag("deployAccountV1"),
261
- deployAccountV1: Schema.Struct({
262
- maxFee: FieldElement,
263
- signature: Schema.Array(FieldElement),
264
- nonce: FieldElement,
265
- contractAddressSalt: FieldElement,
266
- constructorCalldata: Schema.Array(FieldElement),
267
- classHash: FieldElement,
268
- }),
269
- });
270
-
271
- export const DeployAccountTransactionV3 = Schema.Struct({
272
- _tag: tag("deployAccountV3"),
273
- deployAccountV3: Schema.Struct({
274
- signature: Schema.Array(FieldElement),
275
- nonce: FieldElement,
276
- contractAddressSalt: FieldElement,
277
- constructorCalldata: Schema.Array(FieldElement),
278
- classHash: FieldElement,
279
- resourceBounds: ResourceBoundsMapping,
280
- tip: Schema.BigIntFromSelf,
281
- paymasterData: Schema.Array(FieldElement),
282
- nonceDataAvailabilityMode: DataAvailabilityMode,
283
- feeDataAvailabilityMode: DataAvailabilityMode,
284
- }),
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),
285
480
  });
286
481
 
482
+ export type DeployAccountTransactionV3 = Readonly<
483
+ CodecType<typeof DeployAccountTransactionV3>
484
+ >;
485
+
486
+ // -----------------------------------------------------
487
+
287
488
  /** A transaction.
288
489
  *
289
- * @prop meta Transaction metadata.
290
- */
291
- export const Transaction = Schema.Struct({
292
- filterIds: Schema.Array(Schema.Number),
293
- meta: TransactionMeta,
294
- transaction: Schema.Union(
295
- InvokeTransactionV0,
296
- InvokeTransactionV1,
297
- InvokeTransactionV3,
298
- L1HandlerTransaction,
299
- DeployTransaction,
300
- DeclareTransactionV0,
301
- DeclareTransactionV1,
302
- DeclareTransactionV2,
303
- DeclareTransactionV3,
304
- DeployAccountTransactionV1,
305
- 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
+ }),
306
510
  ),
307
511
  });
308
512
 
309
- export type Transaction = typeof Transaction.Type;
513
+ export type Transaction = Readonly<CodecType<typeof Transaction>>;
514
+
515
+ // -----------------------------------------------------
310
516
 
311
- export const PriceUnit = Schema.transform(
312
- Schema.Enums(proto.data.PriceUnit),
313
- Schema.Literal("wei", "fri", "unknown"),
517
+ export const PriceUnit: Codec<"wei" | "fri" | "unknown", proto.data.PriceUnit> =
314
518
  {
315
- 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) {
316
532
  const enumMap = {
317
533
  [proto.data.PriceUnit.WEI]: "wei",
318
534
  [proto.data.PriceUnit.FRI]: "fri",
@@ -320,112 +536,163 @@ export const PriceUnit = Schema.transform(
320
536
  [proto.data.PriceUnit.UNRECOGNIZED]: "unknown",
321
537
  } as const;
322
538
 
323
- return enumMap[value] ?? "unknown";
324
- },
325
- encode(value) {
326
- throw new Error("encode: not implemented");
539
+ return enumMap[p] ?? "unknown";
327
540
  },
328
- },
329
- );
541
+ };
330
542
 
331
- export const FeePayment = Schema.Struct({
332
- amount: FieldElement,
333
- unit: PriceUnit,
334
- });
543
+ export type PriceUnit = CodecType<typeof PriceUnit>;
335
544
 
336
- export const ComputationResources = Schema.Struct({
337
- steps: Schema.BigIntFromSelf,
338
- memoryHoles: Schema.optional(Schema.BigIntFromSelf),
339
- rangeCheckBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
340
- pedersenBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
341
- poseidonBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
342
- ecOpBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
343
- ecdsaBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
344
- bitwiseBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
345
- keccakBuiltinApplications: Schema.optional(Schema.BigIntFromSelf),
346
- segmentArenaBuiltin: Schema.optional(Schema.BigIntFromSelf),
347
- });
545
+ // -----------------------------------------------------
348
546
 
349
- export const DataAvailabilityResources = Schema.Struct({
350
- l1Gas: Schema.BigIntFromSelf,
351
- l1DataGas: Schema.BigIntFromSelf,
547
+ export const FeePayment = MessageCodec({
548
+ amount: RequiredCodec(FieldElement),
549
+ unit: RequiredCodec(PriceUnit),
352
550
  });
353
551
 
354
- export const ExecutionResources = Schema.Struct({
355
- computation: ComputationResources,
356
- dataAvailability: DataAvailabilityResources,
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),
357
567
  });
358
568
 
359
- export const ExecutionSucceeded = Schema.Struct({
360
- _tag: tag("succeeded"),
361
- succeeded: Schema.Struct({}),
362
- });
569
+ export type ComputationResources = Readonly<
570
+ CodecType<typeof ComputationResources>
571
+ >;
363
572
 
364
- export const ExecutionReverted = Schema.Struct({
365
- _tag: tag("reverted"),
366
- reverted: Schema.Struct({
367
- reason: Schema.optional(Schema.String),
368
- }),
573
+ // -----------------------------------------------------
574
+
575
+ export const DataAvailabilityResources = MessageCodec({
576
+ l1Gas: RequiredCodec(BigIntCodec),
577
+ l1DataGas: RequiredCodec(BigIntCodec),
369
578
  });
370
579
 
371
- /** Common fields for all transaction receipts. */
372
- export const TransactionReceiptMeta = Schema.Struct({
373
- transactionIndex: Schema.Number,
374
- transactionHash: FieldElement,
375
- actualFee: FeePayment,
376
- executionResources: ExecutionResources,
377
- executionResult: Schema.Union(ExecutionSucceeded, ExecutionReverted),
580
+ export type DataAvailabilityResources = Readonly<
581
+ CodecType<typeof DataAvailabilityResources>
582
+ >;
583
+
584
+ // -----------------------------------------------------
585
+
586
+ export const ExecutionResources = MessageCodec({
587
+ computation: RequiredCodec(ComputationResources),
588
+ dataAvailability: RequiredCodec(DataAvailabilityResources),
378
589
  });
379
590
 
380
- export const InvokeTransactionReceipt = Schema.Struct({
381
- _tag: tag("invoke"),
382
- invoke: Schema.Struct({}),
591
+ export type ExecutionResources = Readonly<CodecType<typeof ExecutionResources>>;
592
+
593
+ // -----------------------------------------------------
594
+
595
+ export const ExecutionSucceeded = MessageCodec({});
596
+
597
+ export type ExecutionSucceeded = Readonly<CodecType<typeof ExecutionSucceeded>>;
598
+
599
+ // -----------------------------------------------------
600
+
601
+ export const ExecutionReverted = MessageCodec({
602
+ reason: OptionalCodec(StringCodec),
383
603
  });
384
604
 
385
- export const L1HandlerTransactionReceipt = Schema.Struct({
386
- _tag: tag("l1Handler"),
387
- l1Handler: Schema.Struct({
388
- messageHash: Schema.Uint8ArrayFromSelf,
389
- }),
605
+ export type ExecutionReverted = Readonly<CodecType<typeof ExecutionReverted>>;
606
+
607
+ // -----------------------------------------------------
608
+
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
+ ),
390
620
  });
391
621
 
392
- export const DeclareTransactionReceipt = Schema.Struct({
393
- _tag: tag("declare"),
394
- declare: Schema.Struct({}),
622
+ export type TransactionReceiptMeta = Readonly<
623
+ CodecType<typeof TransactionReceiptMeta>
624
+ >;
625
+
626
+ // -----------------------------------------------------
627
+
628
+ export const InvokeTransactionReceipt = MessageCodec({});
629
+
630
+ export type InvokeTransactionReceipt = Readonly<
631
+ CodecType<typeof InvokeTransactionReceipt>
632
+ >;
633
+
634
+ // -----------------------------------------------------
635
+
636
+ export const L1HandlerTransactionReceipt = MessageCodec({
637
+ messageHash: RequiredCodec(Uint8ArrayCodec),
395
638
  });
396
639
 
397
- export const DeployTransactionReceipt = Schema.Struct({
398
- _tag: tag("deploy"),
399
- deploy: Schema.Struct({
400
- contractAddress: FieldElement,
401
- }),
640
+ export type L1HandlerTransactionReceipt = Readonly<
641
+ CodecType<typeof L1HandlerTransactionReceipt>
642
+ >;
643
+
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),
402
656
  });
403
657
 
404
- export const DeployAccountTransactionReceipt = Schema.Struct({
405
- _tag: tag("deployAccount"),
406
- deployAccount: Schema.Struct({
407
- contractAddress: FieldElement,
408
- }),
658
+ export type DeployTransactionReceipt = Readonly<
659
+ CodecType<typeof DeployTransactionReceipt>
660
+ >;
661
+
662
+ // -----------------------------------------------------
663
+
664
+ export const DeployAccountTransactionReceipt = MessageCodec({
665
+ contractAddress: RequiredCodec(FieldElement),
409
666
  });
410
667
 
668
+ export type DeployAccountTransactionReceipt = Readonly<
669
+ CodecType<typeof DeployAccountTransactionReceipt>
670
+ >;
671
+
672
+ // -----------------------------------------------------
673
+
411
674
  /** A transaction receipt.
412
675
  *
413
676
  * @prop meta Transaction receipt metadata.
414
677
  * @prop receipt Transaction-specific receipt.
415
678
  */
416
- export const TransactionReceipt = Schema.Struct({
417
- filterIds: Schema.Array(Schema.Number),
418
- meta: TransactionReceiptMeta,
419
- receipt: Schema.Union(
420
- InvokeTransactionReceipt,
421
- L1HandlerTransactionReceipt,
422
- DeclareTransactionReceipt,
423
- DeployTransactionReceipt,
424
- 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
+ }),
425
690
  ),
426
691
  });
427
692
 
428
- export type TransactionReceipt = typeof TransactionReceipt.Type;
693
+ export type TransactionReceipt = Readonly<CodecType<typeof TransactionReceipt>>;
694
+
695
+ // -----------------------------------------------------
429
696
 
430
697
  /** A transaction event.
431
698
  *
@@ -438,22 +705,25 @@ export type TransactionReceipt = typeof TransactionReceipt.Type;
438
705
  * @prop transactionStatus The transaction status.
439
706
  * @prop eventIndexInTransaction The event index in the transaction.
440
707
  */
441
- export const Event = Schema.Struct({
442
- filterIds: Schema.Array(Schema.Number),
443
- address: FieldElement,
444
- keys: Schema.Array(FieldElement),
445
- data: Schema.Array(FieldElement),
446
- eventIndex: Schema.Number,
447
- transactionIndex: Schema.Number,
448
- transactionHash: FieldElement,
449
- transactionStatus: TransactionStatus,
450
- 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),
451
718
  });
452
719
 
453
- export type Event = typeof Event.Type;
720
+ export type Event = Readonly<CodecType<typeof Event>>;
721
+
722
+ // -----------------------------------------------------
454
723
 
455
724
  /** A message from the L2 to the L1.
456
725
  *
726
+ * @prop filterIds The filter IDs.
457
727
  * @prop fromAddress The address on L2 that sent the message.
458
728
  * @prop toAddress The address on L1 that will receive the message.
459
729
  * @prop payload The message payload.
@@ -463,114 +733,330 @@ export type Event = typeof Event.Type;
463
733
  * @prop transactionStatus The transaction status.
464
734
  * @prop messageIndexInTransaction The message index in the transaction.
465
735
  */
466
- export const MessageToL1 = Schema.Struct({
467
- filterIds: Schema.Array(Schema.Number),
468
- fromAddress: FieldElement,
469
- toAddress: FieldElement,
470
- payload: Schema.Array(FieldElement),
471
- messageIndex: Schema.Number,
472
- transactionIndex: Schema.Number,
473
- transactionHash: FieldElement,
474
- transactionStatus: TransactionStatus,
475
- 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),
476
746
  });
477
747
 
478
- export type MessageToL1 = typeof MessageToL1.Type;
748
+ export type MessageToL1 = Readonly<CodecType<typeof MessageToL1>>;
749
+
750
+ // -----------------------------------------------------
479
751
 
480
752
  /** An entry in the storage diff.
481
753
  *
482
754
  * @prop key The storage location.
483
755
  * @prop value The new value at the storage location.
484
756
  */
485
- export const StorageEntry = Schema.Struct({
486
- key: FieldElement,
487
- value: FieldElement,
757
+ export const StorageEntry = MessageCodec({
758
+ key: RequiredCodec(FieldElement),
759
+ value: RequiredCodec(FieldElement),
488
760
  });
489
761
 
490
- export type StorageEntry = typeof StorageEntry.Type;
762
+ export type StorageEntry = Readonly<CodecType<typeof StorageEntry>>;
763
+
764
+ // -----------------------------------------------------
491
765
 
492
766
  /** Storage diff.
493
767
  *
494
768
  * @prop contractAddress The contract address.
495
769
  * @prop storageEntries The entries that changed.
496
770
  */
497
- export const StorageDiff = Schema.Struct({
498
- filterIds: Schema.Array(Schema.Number),
499
- contractAddress: FieldElement,
500
- storageEntries: Schema.Array(StorageEntry),
771
+ export const StorageDiff = MessageCodec({
772
+ filterIds: ArrayCodec(NumberCodec),
773
+ contractAddress: RequiredCodec(FieldElement),
774
+ storageEntries: ArrayCodec(StorageEntry),
501
775
  });
502
776
 
503
- export type StorageDiff = typeof StorageDiff.Type;
777
+ export type StorageDiff = Readonly<CodecType<typeof StorageDiff>>;
778
+
779
+ // -----------------------------------------------------
504
780
 
505
781
  /** A new class declared.
506
782
  *
507
783
  * @prop classHash The class hash.
508
784
  * @prop compiledClassHash The compiled class hash. If undefined, it's the result of a deprecated Cairo 0 declaration.
509
785
  */
510
- export const DeclaredClass = Schema.Struct({
511
- _tag: tag("declaredClass"),
512
- declaredClass: Schema.Struct({
513
- classHash: Schema.optional(FieldElement),
514
- compiledClassHash: Schema.optional(FieldElement),
515
- }),
786
+ export const DeclaredClass = MessageCodec({
787
+ classHash: OptionalCodec(FieldElement),
788
+ compiledClassHash: OptionalCodec(FieldElement),
516
789
  });
517
790
 
518
- export type DeclaredClass = typeof DeclaredClass.Type;
791
+ export type DeclaredClass = Readonly<CodecType<typeof DeclaredClass>>;
792
+
793
+ // -----------------------------------------------------
519
794
 
520
795
  /** A class replaced.
521
796
  *
522
797
  * @prop contractAddress The contract address.
523
798
  * @prop classHash The class new hash.
524
799
  */
525
- export const ReplacedClass = Schema.Struct({
526
- _tag: tag("replacedClass"),
527
- replacedClass: Schema.Struct({
528
- contractAddress: Schema.optional(FieldElement),
529
- classHash: Schema.optional(FieldElement),
530
- }),
800
+ export const ReplacedClass = MessageCodec({
801
+ contractAddress: OptionalCodec(FieldElement),
802
+ classHash: OptionalCodec(FieldElement),
531
803
  });
532
804
 
533
- export type ReplacedClass = typeof ReplacedClass.Type;
805
+ export type ReplacedClass = Readonly<CodecType<typeof ReplacedClass>>;
806
+
807
+ // -----------------------------------------------------
534
808
 
535
809
  /** A contract deployed.
536
810
  *
537
811
  * @prop contractAddress The contract address.
538
812
  * @prop classHash The class hash.
539
813
  */
540
- export const DeployedContract = Schema.Struct({
541
- _tag: tag("deployedContract"),
542
- deployedContract: Schema.Struct({
543
- contractAddress: Schema.optional(FieldElement),
544
- classHash: Schema.optional(FieldElement),
545
- }),
814
+ export const DeployedContract = MessageCodec({
815
+ contractAddress: OptionalCodec(FieldElement),
816
+ classHash: OptionalCodec(FieldElement),
546
817
  });
547
818
 
548
- export type DeployedContract = typeof DeployedContract.Type;
819
+ export type DeployedContract = Readonly<CodecType<typeof DeployedContract>>;
820
+
821
+ // -----------------------------------------------------
549
822
 
550
823
  /** A contract change.
551
824
  *
552
- * @prop contractAddress The contract address.
825
+ * @prop filterIds The filter IDs.
553
826
  * @prop change The change.
554
827
  */
555
- export const ContractChange = Schema.Struct({
556
- filterIds: Schema.Array(Schema.Number),
557
- 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
+ ),
558
837
  });
559
838
 
560
- export type ContractChange = typeof ContractChange.Type;
839
+ export type ContractChange = Readonly<CodecType<typeof ContractChange>>;
840
+
841
+ // -----------------------------------------------------
561
842
 
562
843
  /** A nonce update.
563
844
  *
564
845
  * @prop contractAddress The contract address.
565
846
  * @prop nonce The new nonce.
566
847
  */
567
- export const NonceUpdate = Schema.Struct({
568
- filterIds: Schema.Array(Schema.Number),
569
- contractAddress: FieldElement,
570
- nonce: FieldElement,
848
+ export const NonceUpdate = MessageCodec({
849
+ filterIds: ArrayCodec(NumberCodec),
850
+ contractAddress: RequiredCodec(FieldElement),
851
+ nonce: RequiredCodec(FieldElement),
571
852
  });
572
853
 
573
- export type NonceUpdate = typeof NonceUpdate.Type;
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";
886
+ },
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
+ >;
913
+
914
+ /** A function invocation.
915
+ *
916
+ * @prop contractAddress The contract address.
917
+ * @prop entryPointSelector The entry point selector.
918
+ * @prop calldata The calldata.
919
+ * @prop callerAddress The caller address.
920
+ * @prop classHash The class hash.
921
+ * @prop callType The call type.
922
+ * @prop result The function invocation result.
923
+ * @prop calls The nested function invocations.
924
+ * @prop events The events index in the current transaction.
925
+ * @prop messages The messages index in the current transaction.
926
+ */
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
+ // -----------------------------------------------------
946
+
947
+ /** A successful invocation of the __execute__ call.
948
+ *
949
+ * The call.
950
+ */
951
+ export const ExecuteInvocationSuccess = FunctionInvocationCodec;
952
+
953
+ // -----------------------------------------------------
954
+
955
+ /** A failed invocation of the __execute__ call.
956
+ *
957
+ * @prop reason The reason for the failure.
958
+ */
959
+ export const ExecuteInvocationReverted = MessageCodec({
960
+ reason: OptionalCodec(StringCodec),
961
+ });
962
+
963
+ // -----------------------------------------------------
964
+
965
+ /** Trace for invoke transactions.
966
+ *
967
+ * @prop validateInvocation The __validate__ call.
968
+ * @prop executeInvocation The __execute__ call.
969
+ * @prop feeTransferInvocation The __fee_transfer__ call.
970
+ */
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),
980
+ });
981
+
982
+ export type InvokeTransactionTrace = Readonly<
983
+ CodecType<typeof InvokeTransactionTrace>
984
+ >;
985
+
986
+ // -----------------------------------------------------
987
+
988
+ /** Trace for declare transactions.
989
+ *
990
+ * @prop validateInvocation The __validate__ call.
991
+ * @prop feeTransferInvocation The __fee_transfer__ call.
992
+ */
993
+ export const DeclareTransactionTrace = MessageCodec({
994
+ validateInvocation: OptionalCodec(FunctionInvocationCodec),
995
+ feeTransferInvocation: OptionalCodec(FunctionInvocationCodec),
996
+ });
997
+
998
+ export type DeclareTransactionTrace = Readonly<
999
+ CodecType<typeof DeclareTransactionTrace>
1000
+ >;
1001
+
1002
+ // -----------------------------------------------------
1003
+
1004
+ /** Trace for deploy account transactions.
1005
+ *
1006
+ * @prop validateInvocation The __validate__ call.
1007
+ * @prop constructorInvocation The __constructor__ invocation.
1008
+ * @prop feeTransferInvocation The __fee_transfer__ call.
1009
+ */
1010
+ export const DeployAccountTransactionTrace = MessageCodec({
1011
+ validateInvocation: OptionalCodec(FunctionInvocationCodec),
1012
+ constructorInvocation: OptionalCodec(FunctionInvocationCodec),
1013
+ feeTransferInvocation: OptionalCodec(FunctionInvocationCodec),
1014
+ });
1015
+
1016
+ export type DeployAccountTransactionTrace = Readonly<
1017
+ CodecType<typeof DeployAccountTransactionTrace>
1018
+ >;
1019
+
1020
+ // -----------------------------------------------------
1021
+
1022
+ /** Trace for L1 handler transactions.
1023
+ *
1024
+ * @prop functionInvocation The L1 handler function invocation.
1025
+ */
1026
+ export const L1HandlerTransactionTrace = MessageCodec({
1027
+ functionInvocation: OptionalCodec(FunctionInvocationCodec),
1028
+ });
1029
+
1030
+ export type L1HandlerTransactionTrace = Readonly<
1031
+ CodecType<typeof L1HandlerTransactionTrace>
1032
+ >;
1033
+
1034
+ // -----------------------------------------------------
1035
+
1036
+ /** A transaction trace.
1037
+ *
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
+ }),
1054
+ ),
1055
+ });
1056
+
1057
+ export type TransactionTrace = Readonly<CodecType<typeof TransactionTrace>>;
1058
+
1059
+ // -----------------------------------------------------
574
1060
 
575
1061
  /** A block.
576
1062
  *
@@ -579,38 +1065,34 @@ export type NonceUpdate = typeof NonceUpdate.Type;
579
1065
  * @prop receipts The receipts of the transactions.
580
1066
  * @prop events The events emitted by the transactions.
581
1067
  * @prop messages The messages sent to L1 by the transactions.
1068
+ * @prop traces The transaction traces.
582
1069
  * @prop storageDiffs The changes to the storage.
583
1070
  * @prop contractChanges The changes to contracts and classes.
1071
+ * @prop nonceUpdates The nonce updates.
584
1072
  */
585
- export const Block = Schema.Struct({
586
- header: BlockHeader,
587
- transactions: Schema.Array(Transaction),
588
- receipts: Schema.Array(TransactionReceipt),
589
- events: Schema.Array(Event),
590
- messages: Schema.Array(MessageToL1),
591
- storageDiffs: Schema.Array(StorageDiff),
592
- contractChanges: Schema.Array(ContractChange),
593
- 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),
594
1083
  });
595
1084
 
596
- export type Block = typeof Block.Type;
1085
+ export type Block = Readonly<CodecType<typeof Block>>;
597
1086
 
598
- export const BlockFromBytes = Schema.transform(
599
- Schema.Uint8ArrayFromSelf,
600
- Schema.NullOr(Block),
601
- {
602
- strict: false,
603
- decode(value) {
604
- if (value.length === 0) {
605
- return null;
606
- }
607
- return proto.data.Block.decode(value);
608
- },
609
- encode(value) {
610
- if (value === null) {
611
- return new Uint8Array();
612
- }
613
- return proto.data.Block.encode(value).finish();
614
- },
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);
615
1097
  },
616
- );
1098
+ };