@apibara/evm 2.0.0-beta.3

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 ADDED
@@ -0,0 +1,150 @@
1
+ import { Schema } from "@effect/schema";
2
+
3
+ import { Address, B256, B256Proto } from "./common";
4
+
5
+ import * as proto from "./proto";
6
+
7
+ const OptionalArray = <TSchema extends Schema.Schema.Any>(schema: TSchema) =>
8
+ Schema.optional(Schema.Array(schema));
9
+
10
+ export const HeaderFilter = Schema.Struct({
11
+ always: Schema.optional(Schema.Boolean),
12
+ });
13
+
14
+ export type HeaderFilter = typeof HeaderFilter.Type;
15
+
16
+ export const WithdrawalFilter = Schema.Struct({
17
+ id: Schema.optional(Schema.Number),
18
+ validatorIndex: Schema.optional(Schema.Number),
19
+ address: Schema.optional(Address),
20
+ });
21
+
22
+ export type WithdrawalFilter = typeof WithdrawalFilter.Type;
23
+
24
+ export const TransactionStatusFilter = Schema.transform(
25
+ Schema.Enums(proto.filter.TransactionStatusFilter),
26
+ Schema.Literal("succeeded", "reverted", "all", "unknown"),
27
+ {
28
+ decode(value) {
29
+ const enumMap = {
30
+ [proto.filter.TransactionStatusFilter.SUCCEEDED]: "succeeded",
31
+ [proto.filter.TransactionStatusFilter.REVERTED]: "reverted",
32
+ [proto.filter.TransactionStatusFilter.ALL]: "all",
33
+ [proto.filter.TransactionStatusFilter.UNSPECIFIED]: "unknown",
34
+ [proto.filter.TransactionStatusFilter.UNRECOGNIZED]: "unknown",
35
+ } as const;
36
+ return enumMap[value] ?? "unknown";
37
+ },
38
+ encode(value) {
39
+ switch (value) {
40
+ case "succeeded":
41
+ return proto.filter.TransactionStatusFilter.SUCCEEDED;
42
+ case "reverted":
43
+ return proto.filter.TransactionStatusFilter.REVERTED;
44
+ case "all":
45
+ return proto.filter.TransactionStatusFilter.ALL;
46
+ default:
47
+ return proto.filter.TransactionStatusFilter.UNSPECIFIED;
48
+ }
49
+ },
50
+ },
51
+ );
52
+
53
+ export type TransactionStatusFilter = typeof TransactionStatusFilter.Type;
54
+
55
+ export const Topic = Schema.transform(
56
+ Schema.Struct({ value: Schema.UndefinedOr(B256Proto) }),
57
+ Schema.NullOr(B256),
58
+ {
59
+ decode({ value }) {
60
+ if (value === undefined) {
61
+ return null;
62
+ }
63
+ return value;
64
+ },
65
+ encode(value) {
66
+ if (value === null) {
67
+ return { value: undefined };
68
+ }
69
+ return { value };
70
+ },
71
+ },
72
+ );
73
+
74
+ export const LogFilter = Schema.Struct({
75
+ id: Schema.optional(Schema.Number),
76
+ address: Schema.optional(Address),
77
+ topics: OptionalArray(Topic),
78
+ strict: Schema.optional(Schema.Boolean),
79
+ transactionStatus: Schema.optional(TransactionStatusFilter),
80
+ includeTransaction: Schema.optional(Schema.Boolean),
81
+ includeReceipt: Schema.optional(Schema.Boolean),
82
+ });
83
+
84
+ export type LogFilter = typeof LogFilter.Type;
85
+
86
+ export const TransactionFilter = Schema.Struct({
87
+ id: Schema.optional(Schema.Number),
88
+ from: Schema.optional(Address),
89
+ to: Schema.optional(Address),
90
+ create: Schema.optional(Schema.Boolean),
91
+ transactionStatus: Schema.optional(TransactionStatusFilter),
92
+ includeReceipt: Schema.optional(Schema.Boolean),
93
+ includeLogs: Schema.optional(Schema.Boolean),
94
+ });
95
+
96
+ export type TransactionFilter = typeof TransactionFilter.Type;
97
+
98
+ export const Filter = Schema.Struct({
99
+ header: Schema.optional(HeaderFilter),
100
+ withdrawals: OptionalArray(WithdrawalFilter),
101
+ transactions: OptionalArray(TransactionFilter),
102
+ logs: OptionalArray(LogFilter),
103
+ });
104
+
105
+ export type Filter = typeof Filter.Type;
106
+
107
+ export const filterToProto = Schema.encodeSync(Filter);
108
+ export const filterFromProto = Schema.decodeSync(Filter);
109
+
110
+ export const FilterFromBytes = Schema.transform(
111
+ Schema.Uint8ArrayFromSelf,
112
+ Filter,
113
+ {
114
+ strict: false,
115
+ decode(value) {
116
+ return proto.filter.Filter.decode(value);
117
+ },
118
+ encode(value) {
119
+ return proto.filter.Filter.encode(value).finish();
120
+ },
121
+ },
122
+ );
123
+
124
+ export const filterToBytes = Schema.encodeSync(FilterFromBytes);
125
+ export const filterFromBytes = Schema.decodeSync(FilterFromBytes);
126
+
127
+ export function mergeFilter(a: Filter, b: Filter): Filter {
128
+ const header = mergeHeaderFilter(a.header, b.header);
129
+ return {
130
+ header,
131
+ withdrawals: [...(a.withdrawals ?? []), ...(b.withdrawals ?? [])],
132
+ logs: [...(a.logs ?? []), ...(b.logs ?? [])],
133
+ transactions: [...(a.transactions ?? []), ...(b.transactions ?? [])],
134
+ };
135
+ }
136
+
137
+ function mergeHeaderFilter(
138
+ a?: HeaderFilter,
139
+ b?: HeaderFilter,
140
+ ): HeaderFilter | undefined {
141
+ if (a === undefined) {
142
+ return b;
143
+ }
144
+ if (b === undefined) {
145
+ return a;
146
+ }
147
+ return {
148
+ always: a.always || b.always,
149
+ };
150
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { StreamConfig } from "@apibara/protocol";
2
+ import { BlockFromBytes } from "./block";
3
+ import { FilterFromBytes, mergeFilter } from "./filter";
4
+
5
+ export * as proto from "./proto";
6
+
7
+ export * from "./common";
8
+ export * from "./filter";
9
+ export * from "./block";
10
+
11
+ export const EvmStream = new StreamConfig(
12
+ FilterFromBytes,
13
+ BlockFromBytes,
14
+ mergeFilter,
15
+ );