@apibara/beaconchain 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apibara/beaconchain",
3
- "version": "2.1.0-beta.23",
3
+ "version": "2.1.0-beta.25",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -35,8 +35,7 @@
35
35
  "vitest": "^1.6.0"
36
36
  },
37
37
  "dependencies": {
38
- "@apibara/evm": "2.1.0-beta.23",
39
- "@apibara/protocol": "2.1.0-beta.23",
38
+ "@apibara/protocol": "2.1.0-beta.25",
40
39
  "@effect/schema": "^0.67.15",
41
40
  "effect": "^3.2.6",
42
41
  "long": "^5.2.1",
package/src/block.ts CHANGED
@@ -1,127 +1,134 @@
1
- import { AccessListItem } from "@apibara/evm";
2
1
  import { BytesFromUint8Array } from "@apibara/protocol";
3
- import { Schema } from "@effect/schema";
4
-
2
+ import {
3
+ ArrayCodec,
4
+ BigIntCodec,
5
+ BooleanCodec,
6
+ type Codec,
7
+ type CodecType,
8
+ DateCodec,
9
+ MessageCodec,
10
+ NumberCodec,
11
+ OptionalCodec,
12
+ RequiredCodec,
13
+ } from "@apibara/protocol/codec";
5
14
  import { Address, B256, B384, U128, U256, ValidatorStatus } from "./common";
6
15
  import * as proto from "./proto";
7
16
 
8
- export const ExecutionPayload = Schema.Struct({
9
- parentHash: B256,
10
- feeRecipient: Address,
11
- stateRoot: B256,
12
- receiptsRoot: B256,
13
- logsBloom: BytesFromUint8Array,
14
- prevRandao: B256,
15
- blockNumber: Schema.BigIntFromSelf,
16
- timestamp: Schema.DateFromSelf,
17
+ export const ExecutionPayload = MessageCodec({
18
+ parentHash: RequiredCodec(B256),
19
+ feeRecipient: RequiredCodec(Address),
20
+ stateRoot: RequiredCodec(B256),
21
+ receiptsRoot: RequiredCodec(B256),
22
+ logsBloom: RequiredCodec(BytesFromUint8Array),
23
+ prevRandao: RequiredCodec(B256),
24
+ blockNumber: RequiredCodec(BigIntCodec),
25
+ timestamp: RequiredCodec(DateCodec),
26
+ });
27
+
28
+ export type ExecutionPayload = CodecType<typeof ExecutionPayload>;
29
+
30
+ export const BlockHeader = MessageCodec({
31
+ slot: RequiredCodec(BigIntCodec),
32
+ proposerIndex: RequiredCodec(NumberCodec),
33
+ parentRoot: RequiredCodec(B256),
34
+ stateRoot: RequiredCodec(B256),
35
+ randaoReveal: RequiredCodec(BytesFromUint8Array),
36
+ depositCount: RequiredCodec(BigIntCodec),
37
+ depositRoot: RequiredCodec(B256),
38
+ blockHash: RequiredCodec(B256),
39
+ graffiti: RequiredCodec(B256),
40
+ executionPayload: OptionalCodec(ExecutionPayload),
41
+ blobKzgCommitments: ArrayCodec(B384),
17
42
  });
18
43
 
19
- export type ExecutionPayload = typeof ExecutionPayload.Type;
20
-
21
- export const BlockHeader = Schema.Struct({
22
- slot: Schema.BigIntFromSelf,
23
- proposerIndex: Schema.Number,
24
- parentRoot: B256,
25
- stateRoot: B256,
26
- randaoReveal: BytesFromUint8Array,
27
- depositCount: Schema.BigIntFromSelf,
28
- depositRoot: B256,
29
- blockHash: B256,
30
- graffiti: B256,
31
- executionPayload: Schema.optional(ExecutionPayload),
32
- blobKzgCommitments: Schema.Array(B384),
44
+ export type BlockHeader = CodecType<typeof BlockHeader>;
45
+
46
+ export const Validator = MessageCodec({
47
+ filterIds: ArrayCodec(NumberCodec),
48
+ validatorIndex: RequiredCodec(NumberCodec),
49
+ balance: RequiredCodec(BigIntCodec),
50
+ status: RequiredCodec(ValidatorStatus),
51
+ pubkey: RequiredCodec(B384),
52
+ withdrawalCredentials: RequiredCodec(B256),
53
+ effectiveBalance: RequiredCodec(BigIntCodec),
54
+ slashed: RequiredCodec(BooleanCodec),
55
+ activationEligibilityEpoch: RequiredCodec(BigIntCodec),
56
+ activationEpoch: RequiredCodec(BigIntCodec),
57
+ exitEpoch: RequiredCodec(BigIntCodec),
58
+ withdrawableEpoch: RequiredCodec(BigIntCodec),
33
59
  });
34
60
 
35
- export type BlockHeader = typeof BlockHeader.Type;
36
-
37
- export const Validator = Schema.Struct({
38
- filterIds: Schema.Array(Schema.Number),
39
- validatorIndex: Schema.Number,
40
- balance: Schema.BigIntFromSelf,
41
- status: ValidatorStatus,
42
- pubkey: B384,
43
- withdrawalCredentials: B256,
44
- effectiveBalance: Schema.BigIntFromSelf,
45
- slashed: Schema.Boolean,
46
- activationEligibilityEpoch: Schema.BigIntFromSelf,
47
- activationEpoch: Schema.BigIntFromSelf,
48
- exitEpoch: Schema.BigIntFromSelf,
49
- withdrawableEpoch: Schema.BigIntFromSelf,
61
+ export type Validator = CodecType<typeof Validator>;
62
+
63
+ export const Blob = MessageCodec({
64
+ filterIds: ArrayCodec(NumberCodec),
65
+ blobIndex: RequiredCodec(NumberCodec),
66
+ blob: RequiredCodec(BytesFromUint8Array),
67
+ kzgCommitment: RequiredCodec(B384),
68
+ kzgProof: RequiredCodec(B384),
69
+ kzgCommitmentInclusionProof: ArrayCodec(B256),
70
+ blobHash: RequiredCodec(B256),
71
+ transactionIndex: RequiredCodec(NumberCodec),
72
+ transactionHash: RequiredCodec(B256),
50
73
  });
51
74
 
52
- export type Validator = typeof Validator.Type;
53
-
54
- export const Blob = Schema.Struct({
55
- filterIds: Schema.Array(Schema.Number),
56
- blobIndex: Schema.Number,
57
- blob: Schema.Uint8ArrayFromSelf,
58
- kzgCommitment: B384,
59
- kzgProof: B384,
60
- kzgCommitmentInclusionProof: Schema.Array(B256),
61
- blobHash: B256,
62
- transactionIndex: Schema.Number,
63
- transactionHash: B256,
75
+ export type Blob = CodecType<typeof Blob>;
76
+
77
+ export const Signature = MessageCodec({
78
+ r: OptionalCodec(U256),
79
+ s: OptionalCodec(U256),
80
+ v: OptionalCodec(U256),
81
+ YParity: OptionalCodec(BooleanCodec),
64
82
  });
65
83
 
66
- export type Blob = typeof Blob.Type;
84
+ export type Signature = CodecType<typeof Signature>;
67
85
 
68
- export const Signature = Schema.Struct({
69
- r: Schema.optional(U256),
70
- s: Schema.optional(U256),
71
- v: Schema.optional(U256),
72
- YParity: Schema.optional(Schema.Boolean),
86
+ export const AccessListItem = MessageCodec({
87
+ address: RequiredCodec(Address),
88
+ storageKeys: ArrayCodec(B256),
73
89
  });
74
90
 
75
- export type Signature = typeof Signature.Type;
76
-
77
- export const Transaction = Schema.Struct({
78
- filterIds: Schema.Array(Schema.Number),
79
- transactionHash: B256,
80
- nonce: Schema.BigIntFromSelf,
81
- transactionIndex: Schema.Number,
82
- from: Address,
83
- to: Schema.optional(Address),
84
- value: U256,
85
- gasPrice: Schema.optional(U128),
86
- gasLimit: Schema.optional(U128),
87
- maxFeePerGas: Schema.optional(U128),
88
- maxPriorityFeePerGas: Schema.optional(U128),
89
- input: Schema.Uint8ArrayFromSelf,
90
- signature: Schema.optional(Signature),
91
- chainId: Schema.optional(Schema.BigIntFromSelf),
92
- accessList: Schema.Array(AccessListItem),
93
- transactionType: Schema.BigIntFromSelf,
94
- maxFeePerBlobGas: Schema.optional(U128),
95
- blobVersionedHashes: Schema.Array(B256),
91
+ export type AccessListItem = CodecType<typeof AccessListItem>;
92
+
93
+ export const Transaction = MessageCodec({
94
+ filterIds: ArrayCodec(NumberCodec),
95
+ transactionHash: RequiredCodec(B256),
96
+ nonce: RequiredCodec(BigIntCodec),
97
+ transactionIndex: RequiredCodec(NumberCodec),
98
+ from: RequiredCodec(Address),
99
+ to: OptionalCodec(Address),
100
+ value: RequiredCodec(U256),
101
+ gasPrice: OptionalCodec(U128),
102
+ gasLimit: OptionalCodec(U128),
103
+ maxFeePerGas: OptionalCodec(U128),
104
+ maxPriorityFeePerGas: OptionalCodec(U128),
105
+ input: RequiredCodec(BytesFromUint8Array),
106
+ signature: OptionalCodec(Signature),
107
+ chainId: OptionalCodec(BigIntCodec),
108
+ accessList: ArrayCodec(AccessListItem),
109
+ transactionType: RequiredCodec(BigIntCodec),
110
+ maxFeePerBlobGas: OptionalCodec(U128),
111
+ blobVersionedHashes: ArrayCodec(B256),
96
112
  });
97
113
 
98
- export type Transaction = typeof Transaction.Type;
114
+ export type Transaction = CodecType<typeof Transaction>;
99
115
 
100
- export const Block = Schema.Struct({
101
- header: BlockHeader,
102
- validators: Schema.Array(Validator),
103
- blobs: Schema.Array(Blob),
104
- transactions: Schema.Array(Transaction),
116
+ export const Block = MessageCodec({
117
+ header: RequiredCodec(BlockHeader),
118
+ validators: ArrayCodec(Validator),
119
+ blobs: ArrayCodec(Blob),
120
+ transactions: ArrayCodec(Transaction),
105
121
  });
106
122
 
107
- export type Block = typeof Block.Type;
108
-
109
- export const BlockFromBytes = Schema.transform(
110
- Schema.Uint8ArrayFromSelf,
111
- Schema.NullOr(Block),
112
- {
113
- strict: false,
114
- decode(value) {
115
- if (value.length === 0) {
116
- return null;
117
- }
118
- return proto.data.Block.decode(value);
119
- },
120
- encode(value) {
121
- if (value === null) {
122
- return new Uint8Array();
123
- }
124
- return proto.data.Block.encode(value).finish();
125
- },
123
+ export type Block = CodecType<typeof Block>;
124
+
125
+ export const BlockFromBytes: Codec<Block, Uint8Array> = {
126
+ encode(x) {
127
+ const block = Block.encode(x);
128
+ return proto.data.Block.encode(block).finish();
129
+ },
130
+ decode(p) {
131
+ const block = proto.data.Block.decode(p);
132
+ return Block.decode(block);
126
133
  },
127
- );
134
+ };
package/src/common.ts CHANGED
@@ -1,97 +1,169 @@
1
- import { Schema } from "@effect/schema";
2
- import { hexToBytes, pad } from "viem";
3
-
1
+ import type { Codec, CodecType } from "@apibara/protocol/codec";
4
2
  import * as proto from "./proto";
5
3
 
6
- // Re-export common types
7
- export { Address, U128, U256, B256 } from "@apibara/evm";
8
-
9
- export const ValidatorStatus = Schema.transform(
10
- Schema.Enums(proto.common.ValidatorStatus),
11
- Schema.Literal(
12
- "pending_initialized",
13
- "pending_queued",
14
- "active_ongoing",
15
- "active_exiting",
16
- "active_slashed",
17
- "exited_unslashed",
18
- "exited_slashed",
19
- "withdrawal_possible",
20
- "withdrawal_done",
21
- "unknown",
22
- ),
23
- {
24
- decode(value) {
25
- const enumMap = {
26
- [proto.common.ValidatorStatus.PENDING_INITIALIZED]:
27
- "pending_initialized",
28
- [proto.common.ValidatorStatus.PENDING_QUEUED]: "pending_queued",
29
- [proto.common.ValidatorStatus.ACTIVE_ONGOING]: "active_ongoing",
30
- [proto.common.ValidatorStatus.ACTIVE_EXITING]: "active_exiting",
31
- [proto.common.ValidatorStatus.ACTIVE_SLASHED]: "active_slashed",
32
- [proto.common.ValidatorStatus.EXITED_UNSLASHED]: "exited_unslashed",
33
- [proto.common.ValidatorStatus.EXITED_SLASHED]: "exited_slashed",
34
- [proto.common.ValidatorStatus.WITHDRAWAL_POSSIBLE]:
35
- "withdrawal_possible",
36
- [proto.common.ValidatorStatus.WITHDRAWAL_DONE]: "withdrawal_done",
37
- [proto.common.ValidatorStatus.UNKNOWN]: "unknown",
38
- [proto.common.ValidatorStatus.UNRECOGNIZED]: "unknown",
39
- } as const;
40
-
41
- return enumMap[value] ?? "unknown";
42
- },
43
- encode(value) {
44
- const enumMap = {
45
- pending_initialized: proto.common.ValidatorStatus.PENDING_INITIALIZED,
46
- pending_queued: proto.common.ValidatorStatus.PENDING_QUEUED,
47
- active_ongoing: proto.common.ValidatorStatus.ACTIVE_ONGOING,
48
- active_exiting: proto.common.ValidatorStatus.ACTIVE_EXITING,
49
- active_slashed: proto.common.ValidatorStatus.ACTIVE_SLASHED,
50
- exited_unslashed: proto.common.ValidatorStatus.EXITED_UNSLASHED,
51
- exited_slashed: proto.common.ValidatorStatus.EXITED_SLASHED,
52
- withdrawal_possible: proto.common.ValidatorStatus.WITHDRAWAL_POSSIBLE,
53
- withdrawal_done: proto.common.ValidatorStatus.WITHDRAWAL_DONE,
54
- unknown: proto.common.ValidatorStatus.UNKNOWN,
55
- } as const;
56
-
57
- return enumMap[value] ?? proto.common.ValidatorStatus.UNKNOWN;
58
- },
4
+ const MAX_U64 = 0xffffffffffffffffn;
5
+ const MAX_U32 = 0xffffffffn;
6
+
7
+ /** An Ethereum address. */
8
+ export const Address: Codec<`0x${string}`, proto.common.Address> = {
9
+ encode(x) {
10
+ const bn = BigInt(x);
11
+ // Ethereum address is 20 bytes (160 bits)
12
+ // Splitting into two 64-bit chunks and one 32-bit chunk
13
+ const x2 = bn & MAX_U32;
14
+ const x1 = (bn >> 32n) & MAX_U64;
15
+ const x0 = (bn >> 96n) & MAX_U64;
16
+ return { x0, x1, x2: Number(x2) };
17
+ },
18
+ decode(p) {
19
+ const x0 = p.x0 ?? 0n;
20
+ const x1 = p.x1 ?? 0n;
21
+ const x2 = BigInt(p.x2 ?? 0);
22
+ const bn = x2 + (x1 << 32n) + (x0 << 96n);
23
+ return `0x${bn.toString(16).padStart(40, "0")}` as `0x${string}`;
24
+ },
25
+ };
26
+
27
+ export type Address = CodecType<typeof Address>;
28
+
29
+ /** Data with length 256 bits. */
30
+ export const B256: Codec<`0x${string}`, proto.common.B256> = {
31
+ encode(x) {
32
+ const bn = BigInt(x);
33
+ const x3 = bn & MAX_U64;
34
+ const x2 = (bn >> 64n) & MAX_U64;
35
+ const x1 = (bn >> 128n) & MAX_U64;
36
+ const x0 = (bn >> 192n) & MAX_U64;
37
+ return { x0, x1, x2, x3 };
38
+ },
39
+ decode(p) {
40
+ const x0 = p.x0 ?? 0n;
41
+ const x1 = p.x1 ?? 0n;
42
+ const x2 = p.x2 ?? 0n;
43
+ const x3 = p.x3 ?? 0n;
44
+ const bn = x3 + (x2 << 64n) + (x1 << 128n) + (x0 << 192n);
45
+ return `0x${bn.toString(16).padStart(64, "0")}` as `0x${string}`;
59
46
  },
60
- );
61
-
62
- export type ValidatorStatus = typeof ValidatorStatus.Type;
63
-
64
- const _B384 = Schema.TemplateLiteral(Schema.Literal("0x"), Schema.String);
65
- const B384Proto = Schema.Struct({
66
- x0: Schema.BigIntFromSelf,
67
- x1: Schema.BigIntFromSelf,
68
- x2: Schema.BigIntFromSelf,
69
- x3: Schema.BigIntFromSelf,
70
- x4: Schema.BigIntFromSelf,
71
- x5: Schema.BigIntFromSelf,
72
- });
73
-
74
- export const B384 = Schema.transform(B384Proto, _B384, {
75
- decode(value) {
76
- const x0 = value.x0.toString(16).padStart(16, "0");
77
- const x1 = value.x1.toString(16).padStart(16, "0");
78
- const x2 = value.x2.toString(16).padStart(16, "0");
79
- const x3 = value.x3.toString(16).padStart(16, "0");
80
- const x4 = value.x4.toString(16).padStart(16, "0");
81
- const x5 = value.x5.toString(16).padStart(16, "0");
82
- return `0x${x0}${x1}${x2}${x3}${x4}${x5}` as `0x${string}`;
47
+ };
48
+
49
+ export type B256 = CodecType<typeof B256>;
50
+
51
+ /** Data with length 256 bits. */
52
+ export const U256: Codec<bigint, proto.common.U256> = {
53
+ encode(x) {
54
+ const bn = BigInt(x);
55
+ const x3 = bn & MAX_U64;
56
+ const x2 = (bn >> 64n) & MAX_U64;
57
+ const x1 = (bn >> 128n) & MAX_U64;
58
+ const x0 = (bn >> 192n) & MAX_U64;
59
+ return { x0, x1, x2, x3 };
60
+ },
61
+ decode(p) {
62
+ const x0 = p.x0 ?? 0n;
63
+ const x1 = p.x1 ?? 0n;
64
+ const x2 = p.x2 ?? 0n;
65
+ const x3 = p.x3 ?? 0n;
66
+ return x3 + (x2 << 64n) + (x1 << 128n) + (x0 << 192n);
67
+ },
68
+ };
69
+
70
+ export type U256 = CodecType<typeof U256>;
71
+
72
+ /** Data with length 128 bits. */
73
+ export const U128: Codec<bigint, proto.common.U128> = {
74
+ encode(x) {
75
+ const x1 = x & MAX_U64;
76
+ const x0 = (x >> 64n) & MAX_U64;
77
+ return { x0, x1 };
83
78
  },
84
- encode(value) {
85
- const bytes = hexToBytes(pad(value, { size: 48, dir: "left" }));
86
- const dv = new DataView(bytes.buffer);
87
- const x0 = dv.getBigUint64(0);
88
- const x1 = dv.getBigUint64(8);
89
- const x2 = dv.getBigUint64(16);
90
- const x3 = dv.getBigUint64(24);
91
- const x4 = dv.getBigUint64(32);
92
- const x5 = dv.getBigUint64(40);
79
+ decode(p) {
80
+ const x0 = p.x0 ?? 0n;
81
+ const x1 = p.x1 ?? 0n;
82
+ return x1 + (x0 << 64n);
83
+ },
84
+ };
85
+
86
+ export type U128 = CodecType<typeof U128>;
87
+
88
+ export const B384: Codec<`0x${string}`, proto.common.B384> = {
89
+ encode(x) {
90
+ const bn = BigInt(x);
91
+ const x5 = bn & MAX_U64;
92
+ const x4 = (bn >> 64n) & MAX_U64;
93
+ const x3 = (bn >> 128n) & MAX_U64;
94
+ const x2 = (bn >> 192n) & MAX_U64;
95
+ const x1 = (bn >> 256n) & MAX_U64;
96
+ const x0 = (bn >> 320n) & MAX_U64;
93
97
  return { x0, x1, x2, x3, x4, x5 };
94
98
  },
95
- });
99
+ decode(p) {
100
+ const x0 = p.x0 ?? 0n;
101
+ const x1 = p.x1 ?? 0n;
102
+ const x2 = p.x2 ?? 0n;
103
+ const x3 = p.x3 ?? 0n;
104
+ const x4 = p.x4 ?? 0n;
105
+ const x5 = p.x5 ?? 0n;
106
+ const bn =
107
+ x5 +
108
+ (x4 << 64n) +
109
+ (x3 << 128n) +
110
+ (x2 << 192n) +
111
+ (x1 << 256n) +
112
+ (x0 << 320n);
113
+
114
+ return `0x${bn.toString(16).padStart(96, "0")}` as `0x${string}`;
115
+ },
116
+ };
117
+
118
+ export type B384 = CodecType<typeof B384>;
119
+
120
+ export const ValidatorStatus: Codec<
121
+ | "pending_initialized"
122
+ | "pending_queued"
123
+ | "active_ongoing"
124
+ | "active_exiting"
125
+ | "active_slashed"
126
+ | "exited_unslashed"
127
+ | "exited_slashed"
128
+ | "withdrawal_possible"
129
+ | "withdrawal_done"
130
+ | "unknown",
131
+ proto.common.ValidatorStatus
132
+ > = {
133
+ encode(x) {
134
+ const enumMap = {
135
+ pending_initialized: proto.common.ValidatorStatus.PENDING_INITIALIZED,
136
+ pending_queued: proto.common.ValidatorStatus.PENDING_QUEUED,
137
+ active_ongoing: proto.common.ValidatorStatus.ACTIVE_ONGOING,
138
+ active_exiting: proto.common.ValidatorStatus.ACTIVE_EXITING,
139
+ active_slashed: proto.common.ValidatorStatus.ACTIVE_SLASHED,
140
+ exited_unslashed: proto.common.ValidatorStatus.EXITED_UNSLASHED,
141
+ exited_slashed: proto.common.ValidatorStatus.EXITED_SLASHED,
142
+ withdrawal_possible: proto.common.ValidatorStatus.WITHDRAWAL_POSSIBLE,
143
+ withdrawal_done: proto.common.ValidatorStatus.WITHDRAWAL_DONE,
144
+ unknown: proto.common.ValidatorStatus.UNKNOWN,
145
+ } as const;
146
+
147
+ return enumMap[x] ?? proto.common.ValidatorStatus.UNKNOWN;
148
+ },
149
+
150
+ decode(p) {
151
+ const enumMap = {
152
+ [proto.common.ValidatorStatus.PENDING_INITIALIZED]: "pending_initialized",
153
+ [proto.common.ValidatorStatus.PENDING_QUEUED]: "pending_queued",
154
+ [proto.common.ValidatorStatus.ACTIVE_ONGOING]: "active_ongoing",
155
+ [proto.common.ValidatorStatus.ACTIVE_EXITING]: "active_exiting",
156
+ [proto.common.ValidatorStatus.ACTIVE_SLASHED]: "active_slashed",
157
+ [proto.common.ValidatorStatus.EXITED_UNSLASHED]: "exited_unslashed",
158
+ [proto.common.ValidatorStatus.EXITED_SLASHED]: "exited_slashed",
159
+ [proto.common.ValidatorStatus.WITHDRAWAL_POSSIBLE]: "withdrawal_possible",
160
+ [proto.common.ValidatorStatus.WITHDRAWAL_DONE]: "withdrawal_done",
161
+ [proto.common.ValidatorStatus.UNKNOWN]: "unknown",
162
+ [proto.common.ValidatorStatus.UNRECOGNIZED]: "unknown",
163
+ } as const;
164
+
165
+ return enumMap[p] ?? "unknown";
166
+ },
167
+ };
96
168
 
97
- export type B384 = typeof B384.Type;
169
+ export type ValidatorStatus = CodecType<typeof ValidatorStatus>;