@apibara/beaconchain 2.1.0-beta.3 → 2.1.0-beta.31

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.3",
3
+ "version": "2.1.0-beta.31",
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.3",
39
- "@apibara/protocol": "2.1.0-beta.3",
38
+ "@apibara/protocol": "2.1.0-beta.31",
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,123 +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),
17
26
  });
18
27
 
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),
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),
33
42
  });
34
43
 
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,
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),
50
59
  });
51
60
 
52
- export const Blob = Schema.Struct({
53
- filterIds: Schema.Array(Schema.Number),
54
- blobIndex: Schema.Number,
55
- blob: Schema.Uint8ArrayFromSelf,
56
- kzgCommitment: B384,
57
- kzgProof: B384,
58
- kzgCommitmentInclusionProof: Schema.Array(B256),
59
- blobHash: B256,
60
- transactionIndex: Schema.Number,
61
- transactionHash: B256,
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),
62
73
  });
63
74
 
64
- export const Signature = Schema.Struct({
65
- r: Schema.optional(U256),
66
- s: Schema.optional(U256),
67
- v: Schema.optional(U256),
68
- YParity: Schema.optional(Schema.Boolean),
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),
69
82
  });
70
83
 
71
- export type Signature = typeof Signature.Type;
72
-
73
- export const Transaction = Schema.Struct({
74
- filterIds: Schema.Array(Schema.Number),
75
- transactionHash: B256,
76
- nonce: Schema.BigIntFromSelf,
77
- transactionIndex: Schema.Number,
78
- from: Address,
79
- to: Schema.optional(Address),
80
- value: U256,
81
- gasPrice: Schema.optional(U128),
82
- gasLimit: Schema.optional(U128),
83
- maxFeePerGas: Schema.optional(U128),
84
- maxPriorityFeePerGas: Schema.optional(U128),
85
- input: Schema.Uint8ArrayFromSelf,
86
- signature: Schema.optional(Signature),
87
- chainId: Schema.optional(Schema.BigIntFromSelf),
88
- accessList: Schema.Array(AccessListItem),
89
- transactionType: Schema.BigIntFromSelf,
90
- maxFeePerBlobGas: Schema.optional(U128),
91
- blobVersionedHashes: Schema.Array(B256),
84
+ export type Signature = CodecType<typeof Signature>;
85
+
86
+ export const AccessListItem = MessageCodec({
87
+ address: RequiredCodec(Address),
88
+ storageKeys: ArrayCodec(B256),
92
89
  });
93
90
 
94
- export type Transaction = typeof Transaction.Type;
91
+ export type AccessListItem = CodecType<typeof AccessListItem>;
95
92
 
96
- export const Block = Schema.Struct({
97
- header: BlockHeader,
98
- validators: Schema.Array(Validator),
99
- blobs: Schema.Array(Blob),
100
- transactions: Schema.Array(Transaction),
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),
101
112
  });
102
113
 
103
- export type Block = typeof Block.Type;
104
-
105
- export const BlockFromBytes = Schema.transform(
106
- Schema.Uint8ArrayFromSelf,
107
- Schema.NullOr(Block),
108
- {
109
- strict: false,
110
- decode(value) {
111
- if (value.length === 0) {
112
- return null;
113
- }
114
- return proto.data.Block.decode(value);
115
- },
116
- encode(value) {
117
- if (value === null) {
118
- return new Uint8Array();
119
- }
120
- return proto.data.Block.encode(value).finish();
121
- },
114
+ export type Transaction = CodecType<typeof Transaction>;
115
+
116
+ export const Block = MessageCodec({
117
+ header: RequiredCodec(BlockHeader),
118
+ validators: ArrayCodec(Validator),
119
+ blobs: ArrayCodec(Blob),
120
+ transactions: ArrayCodec(Transaction),
121
+ });
122
+
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);
122
133
  },
123
- );
134
+ };
package/src/common.ts CHANGED
@@ -1,95 +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
- const _B384 = Schema.TemplateLiteral(Schema.Literal("0x"), Schema.String);
63
- const B384Proto = Schema.Struct({
64
- x0: Schema.BigIntFromSelf,
65
- x1: Schema.BigIntFromSelf,
66
- x2: Schema.BigIntFromSelf,
67
- x3: Schema.BigIntFromSelf,
68
- x4: Schema.BigIntFromSelf,
69
- x5: Schema.BigIntFromSelf,
70
- });
71
-
72
- export const B384 = Schema.transform(B384Proto, _B384, {
73
- decode(value) {
74
- const x0 = value.x0.toString(16).padStart(16, "0");
75
- const x1 = value.x1.toString(16).padStart(16, "0");
76
- const x2 = value.x2.toString(16).padStart(16, "0");
77
- const x3 = value.x3.toString(16).padStart(16, "0");
78
- const x4 = value.x4.toString(16).padStart(16, "0");
79
- const x5 = value.x5.toString(16).padStart(16, "0");
80
- 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 };
81
78
  },
82
- encode(value) {
83
- const bytes = hexToBytes(pad(value, { size: 48, dir: "left" }));
84
- const dv = new DataView(bytes.buffer);
85
- const x0 = dv.getBigUint64(0);
86
- const x1 = dv.getBigUint64(8);
87
- const x2 = dv.getBigUint64(16);
88
- const x3 = dv.getBigUint64(24);
89
- const x4 = dv.getBigUint64(32);
90
- 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;
91
97
  return { x0, x1, x2, x3, x4, x5 };
92
98
  },
93
- });
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
+ };
94
168
 
95
- export type B384 = typeof B384.Type;
169
+ export type ValidatorStatus = CodecType<typeof ValidatorStatus>;