@apibara/beaconchain 2.1.0-beta.8 → 2.1.1-beta.0

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/filter.ts CHANGED
@@ -1,5 +1,12 @@
1
- import { Schema } from "@effect/schema";
2
-
1
+ import {
2
+ ArrayCodec,
3
+ BooleanCodec,
4
+ type Codec,
5
+ type CodecType,
6
+ MessageCodec,
7
+ NumberCodec,
8
+ OptionalCodec,
9
+ } from "@apibara/protocol/codec";
3
10
  import { Address, ValidatorStatus } from "./common";
4
11
  import * as proto from "./proto";
5
12
 
@@ -9,37 +16,36 @@ import * as proto from "./proto";
9
16
  * - `on_data`: receive headers only if any other filter matches.
10
17
  * - `on_data_or_on_new_block`: receive headers only if any other filter matches and for "live" blocks.
11
18
  */
12
- export const HeaderFilter = Schema.transform(
13
- Schema.Enums(proto.filter.HeaderFilter),
14
- Schema.Literal("always", "on_data", "on_data_or_on_new_block", "unknown"),
15
- {
16
- decode(value) {
17
- const enumMap = {
18
- [proto.filter.HeaderFilter.ALWAYS]: "always",
19
- [proto.filter.HeaderFilter.ON_DATA]: "on_data",
20
- [proto.filter.HeaderFilter.ON_DATA_OR_ON_NEW_BLOCK]:
21
- "on_data_or_on_new_block",
22
- [proto.filter.HeaderFilter.UNSPECIFIED]: "unknown",
23
- [proto.filter.HeaderFilter.UNRECOGNIZED]: "unknown",
24
- } as const;
25
- return enumMap[value] ?? "unknown";
26
- },
27
- encode(value) {
28
- switch (value) {
29
- case "always":
30
- return proto.filter.HeaderFilter.ALWAYS;
31
- case "on_data":
32
- return proto.filter.HeaderFilter.ON_DATA;
33
- case "on_data_or_on_new_block":
34
- return proto.filter.HeaderFilter.ON_DATA_OR_ON_NEW_BLOCK;
35
- default:
36
- return proto.filter.HeaderFilter.UNSPECIFIED;
37
- }
38
- },
19
+ export const HeaderFilter: Codec<
20
+ "always" | "on_data" | "on_data_or_on_new_block" | "unknown",
21
+ proto.filter.HeaderFilter
22
+ > = {
23
+ encode(x) {
24
+ switch (x) {
25
+ case "always":
26
+ return proto.filter.HeaderFilter.ALWAYS;
27
+ case "on_data":
28
+ return proto.filter.HeaderFilter.ON_DATA;
29
+ case "on_data_or_on_new_block":
30
+ return proto.filter.HeaderFilter.ON_DATA_OR_ON_NEW_BLOCK;
31
+ default:
32
+ return proto.filter.HeaderFilter.UNSPECIFIED;
33
+ }
34
+ },
35
+ decode(p) {
36
+ const enumMap = {
37
+ [proto.filter.HeaderFilter.ALWAYS]: "always",
38
+ [proto.filter.HeaderFilter.ON_DATA]: "on_data",
39
+ [proto.filter.HeaderFilter.ON_DATA_OR_ON_NEW_BLOCK]:
40
+ "on_data_or_on_new_block",
41
+ [proto.filter.HeaderFilter.UNSPECIFIED]: "unknown",
42
+ [proto.filter.HeaderFilter.UNRECOGNIZED]: "unknown",
43
+ } as const;
44
+ return enumMap[p] ?? "unknown";
39
45
  },
40
- );
46
+ };
41
47
 
42
- export type HeaderFilter = typeof HeaderFilter.Type;
48
+ export type HeaderFilter = CodecType<typeof HeaderFilter>;
43
49
 
44
50
  /** Filter transactions.
45
51
  *
@@ -47,73 +53,64 @@ export type HeaderFilter = typeof HeaderFilter.Type;
47
53
  * @prop to Filter transactions by the target address.
48
54
  * @prop includeBlob Include any blob posted by the transaction..
49
55
  */
50
- export const TransactionFilter = Schema.Struct({
51
- id: Schema.optional(Schema.Number),
52
- from: Schema.optional(Address),
53
- to: Schema.optional(Address),
54
- create: Schema.optional(Schema.Boolean),
55
- includeBlob: Schema.optional(Schema.Boolean),
56
+ export const TransactionFilter = MessageCodec({
57
+ id: OptionalCodec(NumberCodec),
58
+ from: OptionalCodec(Address),
59
+ to: OptionalCodec(Address),
60
+ create: OptionalCodec(BooleanCodec),
61
+ includeBlob: OptionalCodec(BooleanCodec),
56
62
  });
57
63
 
58
- export type TransactionFilter = typeof TransactionFilter.Type;
64
+ export type TransactionFilter = CodecType<typeof TransactionFilter>;
59
65
 
60
66
  /** Filter validators.
61
67
  *
62
68
  * @prop validatorIndex Filter validators by their index.
63
69
  * @prop status Filter validators by their status.
64
70
  */
65
- export const ValidatorFilter = Schema.Struct({
66
- id: Schema.optional(Schema.Number),
67
- validatorIndex: Schema.optional(Schema.Number),
68
- status: Schema.optional(ValidatorStatus),
71
+ export const ValidatorFilter = MessageCodec({
72
+ id: OptionalCodec(NumberCodec),
73
+ validatorIndex: OptionalCodec(NumberCodec),
74
+ status: OptionalCodec(ValidatorStatus),
69
75
  });
70
76
 
71
- export type ValidatorFilter = typeof ValidatorFilter.Type;
77
+ export type ValidatorFilter = CodecType<typeof ValidatorFilter>;
72
78
 
73
79
  /** Filter blobs.
74
80
  *
75
81
  * @prop includeTransaction Include the transaction that posted the blob.
76
82
  */
77
- export const BlobFilter = Schema.Struct({
78
- id: Schema.optional(Schema.Number),
79
- includeTransaction: Schema.optional(Schema.Boolean),
83
+ export const BlobFilter = MessageCodec({
84
+ id: OptionalCodec(NumberCodec),
85
+ includeTransaction: OptionalCodec(BooleanCodec),
80
86
  });
81
87
 
82
- export type BlobFilter = typeof BlobFilter.Type;
88
+ export type BlobFilter = CodecType<typeof BlobFilter>;
83
89
 
84
90
  /** Filter block data.
85
91
  *
86
92
  * @prop header Change how block headers are returned.
87
93
  * @prop validators Filter validators.
88
94
  */
89
- export const Filter = Schema.Struct({
90
- header: Schema.optional(HeaderFilter),
91
- transactions: Schema.optional(Schema.Array(TransactionFilter)),
92
- validators: Schema.optional(Schema.Array(ValidatorFilter)),
93
- blobs: Schema.optional(Schema.Array(BlobFilter)),
95
+ export const Filter = MessageCodec({
96
+ header: OptionalCodec(HeaderFilter),
97
+ transactions: OptionalCodec(ArrayCodec(TransactionFilter)),
98
+ validators: OptionalCodec(ArrayCodec(ValidatorFilter)),
99
+ blobs: OptionalCodec(ArrayCodec(BlobFilter)),
94
100
  });
95
101
 
96
- export type Filter = typeof Filter.Type;
97
-
98
- export const filterToProto = Schema.encodeSync(Filter);
99
- export const filterFromProto = Schema.decodeSync(Filter);
100
-
101
- export const FilterFromBytes = Schema.transform(
102
- Schema.Uint8ArrayFromSelf,
103
- Filter,
104
- {
105
- strict: false,
106
- decode(value) {
107
- return proto.filter.Filter.decode(value);
108
- },
109
- encode(value) {
110
- return proto.filter.Filter.encode(value).finish();
111
- },
112
- },
113
- );
102
+ export type Filter = CodecType<typeof Filter>;
114
103
 
115
- export const filterToBytes = Schema.encodeSync(FilterFromBytes);
116
- export const filterFromBytes = Schema.decodeSync(FilterFromBytes);
104
+ export const FilterFromBytes: Codec<Filter, Uint8Array> = {
105
+ encode(value) {
106
+ const filter = Filter.encode(value);
107
+ return proto.filter.Filter.encode(filter).finish();
108
+ },
109
+ decode(value) {
110
+ const filter = proto.filter.Filter.decode(value);
111
+ return Filter.decode(filter);
112
+ },
113
+ };
117
114
 
118
115
  export function mergeFilter(a: Filter, b: Filter): Filter {
119
116
  const header = mergeHeaderFilter(a.header, b.header);
package/src/index.ts CHANGED
@@ -12,4 +12,5 @@ export const BeaconChainStream = new StreamConfig(
12
12
  FilterFromBytes,
13
13
  BlockFromBytes,
14
14
  mergeFilter,
15
+ "beaconchain",
15
16
  );
@@ -103,17 +103,18 @@ export const protobufPackage = "google.protobuf";
103
103
  */
104
104
  export interface Timestamp {
105
105
  /**
106
- * Represents seconds of UTC time since Unix epoch
107
- * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
108
- * 9999-12-31T23:59:59Z inclusive.
106
+ * Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must
107
+ * be between -315576000000 and 315576000000 inclusive (which corresponds to
108
+ * 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z).
109
109
  */
110
110
  readonly seconds?:
111
111
  | bigint
112
112
  | undefined;
113
113
  /**
114
- * Non-negative fractions of a second at nanosecond resolution. Negative
115
- * second values with fractions must still have non-negative nanos values
116
- * that count forward in time. Must be from 0 to 999,999,999
114
+ * Non-negative fractions of a second at nanosecond resolution. This field is
115
+ * the nanosecond portion of the duration, not an alternative to seconds.
116
+ * Negative second values with fractions must still have non-negative nanos
117
+ * values that count forward in time. Must be between 0 and 999,999,999
117
118
  * inclusive.
118
119
  */
119
120
  readonly nanos?: number | undefined;
@@ -1,21 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { Schema } from "@effect/schema";
4
- import { pad } from "viem";
5
-
6
- import { B384 } from "./common";
7
-
8
- describe("B384", () => {
9
- const encode = Schema.encodeSync(B384);
10
- const decode = Schema.decodeSync(B384);
11
-
12
- it("should convert from and to proto", () => {
13
- const value =
14
- "0x9df92d765b5aa041fd4bbe8d5878eb89290efa78e444c1a603eecfae2ea05fa4";
15
-
16
- const message = encode(value);
17
-
18
- const back = decode(message);
19
- expect(back).toEqual(pad(value, { size: 48 }));
20
- });
21
- });