@apibara/evm 2.0.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/.turbo/turbo-build.log +17 -0
- package/CHANGELOG.md +1 -0
- package/LICENSE.txt +202 -0
- package/README.md +27 -0
- package/buf.gen.yaml +14 -0
- package/build.config.ts +11 -0
- package/dist/index.cjs +2994 -0
- package/dist/index.d.cts +1900 -0
- package/dist/index.d.mts +1900 -0
- package/dist/index.d.ts +1900 -0
- package/dist/index.mjs +2953 -0
- package/package.json +49 -0
- package/proto/common.proto +37 -0
- package/proto/data.proto +192 -0
- package/proto/filter.proto +61 -0
- package/src/block.ts +167 -0
- package/src/common.test.ts +79 -0
- package/src/common.ts +117 -0
- package/src/filter.test.ts +191 -0
- package/src/filter.ts +117 -0
- package/src/index.ts +15 -0
- package/src/proto/common.ts +561 -0
- package/src/proto/data.ts +2111 -0
- package/src/proto/filter.ts +658 -0
- package/src/proto/google/protobuf/timestamp.ts +220 -0
- package/src/proto/index.ts +3 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { encodeEventTopics, pad, parseAbi } from "viem";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { Schema } from "@effect/schema";
|
|
5
|
+
import {
|
|
6
|
+
Filter,
|
|
7
|
+
LogFilter,
|
|
8
|
+
filterFromProto,
|
|
9
|
+
filterToProto,
|
|
10
|
+
mergeFilter,
|
|
11
|
+
} from "./filter";
|
|
12
|
+
|
|
13
|
+
const abi = parseAbi([
|
|
14
|
+
"event Transfer(address indexed from, address indexed to, uint256 value)",
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
describe("Filter", () => {
|
|
18
|
+
it("all filters are optional", () => {
|
|
19
|
+
const filter = Filter.make({});
|
|
20
|
+
|
|
21
|
+
const proto = filterToProto(filter);
|
|
22
|
+
const back = filterFromProto(proto);
|
|
23
|
+
expect(back).toEqual(filter);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("accepts logs filter", () => {
|
|
27
|
+
const filter = Filter.make({
|
|
28
|
+
logs: [
|
|
29
|
+
{
|
|
30
|
+
address: "0x123456789012",
|
|
31
|
+
strict: true,
|
|
32
|
+
topics: encodeEventTopics({
|
|
33
|
+
abi,
|
|
34
|
+
eventName: "Transfer",
|
|
35
|
+
args: { from: null, to: null },
|
|
36
|
+
}) as `0x${string}`[],
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
expect(filter.logs).toHaveLength(1);
|
|
42
|
+
|
|
43
|
+
const proto = filterToProto(filter);
|
|
44
|
+
const back = filterFromProto(proto);
|
|
45
|
+
|
|
46
|
+
expect(back).toBeDefined();
|
|
47
|
+
expect(back.logs).toHaveLength(1);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("LogFilter", () => {
|
|
52
|
+
const encode = Schema.encodeSync(LogFilter);
|
|
53
|
+
const decode = Schema.decodeSync(LogFilter);
|
|
54
|
+
|
|
55
|
+
it("can be empty", () => {
|
|
56
|
+
const filter = LogFilter.make({});
|
|
57
|
+
|
|
58
|
+
const proto = encode(filter);
|
|
59
|
+
const back = decode(proto);
|
|
60
|
+
expect(back).toEqual(filter);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("can have null topics", () => {
|
|
64
|
+
const filter = LogFilter.make({
|
|
65
|
+
topics: [null, pad("0x1"), null, pad("0x3")],
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const proto = encode(filter);
|
|
69
|
+
const back = decode(proto);
|
|
70
|
+
expect(back).toEqual(filter);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("can have all optional fields", () => {
|
|
74
|
+
const filter = LogFilter.make({
|
|
75
|
+
address: pad("0xa", { size: 20 }),
|
|
76
|
+
topics: [null, pad("0x1"), null, pad("0x3")],
|
|
77
|
+
includeTransaction: true,
|
|
78
|
+
includeReceipt: true,
|
|
79
|
+
strict: true,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const proto = encode(filter);
|
|
83
|
+
const back = decode(proto);
|
|
84
|
+
expect(back).toEqual(filter);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("mergeFilter", () => {
|
|
89
|
+
it("returns header.always if any has it", () => {
|
|
90
|
+
const fa = mergeFilter({}, { header: { always: true } });
|
|
91
|
+
expect(fa).toMatchInlineSnapshot(`
|
|
92
|
+
{
|
|
93
|
+
"header": {
|
|
94
|
+
"always": true,
|
|
95
|
+
},
|
|
96
|
+
"logs": [],
|
|
97
|
+
"transactions": [],
|
|
98
|
+
"withdrawals": [],
|
|
99
|
+
}
|
|
100
|
+
`);
|
|
101
|
+
const fb = mergeFilter({ header: { always: true } }, {});
|
|
102
|
+
expect(fb).toMatchInlineSnapshot(`
|
|
103
|
+
{
|
|
104
|
+
"header": {
|
|
105
|
+
"always": true,
|
|
106
|
+
},
|
|
107
|
+
"logs": [],
|
|
108
|
+
"transactions": [],
|
|
109
|
+
"withdrawals": [],
|
|
110
|
+
}
|
|
111
|
+
`);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("returns an empty header by default", () => {
|
|
115
|
+
const f = mergeFilter({}, {});
|
|
116
|
+
expect(f).toMatchInlineSnapshot(`
|
|
117
|
+
{
|
|
118
|
+
"header": undefined,
|
|
119
|
+
"logs": [],
|
|
120
|
+
"transactions": [],
|
|
121
|
+
"withdrawals": [],
|
|
122
|
+
}
|
|
123
|
+
`);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("concatenates logs", () => {
|
|
127
|
+
const f = mergeFilter(
|
|
128
|
+
{ logs: [{ address: "0xAAAAAAAAAAAAAAAAAAAAAA" }] },
|
|
129
|
+
{ logs: [{ address: "0xBBBBBBBBBBBBBBBBBBBBBB" }] },
|
|
130
|
+
);
|
|
131
|
+
expect(f).toMatchInlineSnapshot(`
|
|
132
|
+
{
|
|
133
|
+
"header": undefined,
|
|
134
|
+
"logs": [
|
|
135
|
+
{
|
|
136
|
+
"address": "0xAAAAAAAAAAAAAAAAAAAAAA",
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
"address": "0xBBBBBBBBBBBBBBBBBBBBBB",
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
"transactions": [],
|
|
143
|
+
"withdrawals": [],
|
|
144
|
+
}
|
|
145
|
+
`);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("concatenates transactions", () => {
|
|
149
|
+
const f = mergeFilter(
|
|
150
|
+
{ transactions: [{ from: "0xAAAAAAAAAAAAAAAAAAAAAA" }] },
|
|
151
|
+
{ transactions: [{ from: "0xBBBBBBBBBBBBBBBBBBBBBB" }] },
|
|
152
|
+
);
|
|
153
|
+
expect(f).toMatchInlineSnapshot(`
|
|
154
|
+
{
|
|
155
|
+
"header": undefined,
|
|
156
|
+
"logs": [],
|
|
157
|
+
"transactions": [
|
|
158
|
+
{
|
|
159
|
+
"from": "0xAAAAAAAAAAAAAAAAAAAAAA",
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
"from": "0xBBBBBBBBBBBBBBBBBBBBBB",
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
"withdrawals": [],
|
|
166
|
+
}
|
|
167
|
+
`);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("concatenates withdrawals", () => {
|
|
171
|
+
const f = mergeFilter(
|
|
172
|
+
{ withdrawals: [{ validatorIndex: 1n }] },
|
|
173
|
+
{ withdrawals: [{ validatorIndex: 100n }] },
|
|
174
|
+
);
|
|
175
|
+
expect(f).toMatchInlineSnapshot(`
|
|
176
|
+
{
|
|
177
|
+
"header": undefined,
|
|
178
|
+
"logs": [],
|
|
179
|
+
"transactions": [],
|
|
180
|
+
"withdrawals": [
|
|
181
|
+
{
|
|
182
|
+
"validatorIndex": 1n,
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
"validatorIndex": 100n,
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
}
|
|
189
|
+
`);
|
|
190
|
+
});
|
|
191
|
+
});
|
package/src/filter.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { Schema } from "@effect/schema";
|
|
2
|
+
|
|
3
|
+
import { Address, B256, b256FromProto, b256ToProto } 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
|
+
validatorIndex: Schema.optional(Schema.BigIntFromSelf),
|
|
18
|
+
address: Schema.optional(Address),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export type WithdrawalFilter = typeof WithdrawalFilter.Type;
|
|
22
|
+
|
|
23
|
+
// TODO: using the decoder inside decode/encode feels wrong.
|
|
24
|
+
export const Topic = Schema.transform(
|
|
25
|
+
Schema.Struct({ value: Schema.UndefinedOr(B256) }),
|
|
26
|
+
Schema.NullOr(B256),
|
|
27
|
+
{
|
|
28
|
+
strict: false,
|
|
29
|
+
decode({ value }) {
|
|
30
|
+
if (value === undefined) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return b256ToProto(value);
|
|
34
|
+
},
|
|
35
|
+
encode(value) {
|
|
36
|
+
if (value === null) {
|
|
37
|
+
return { value: undefined };
|
|
38
|
+
}
|
|
39
|
+
return { value: b256FromProto(value) };
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
export const LogFilter = Schema.Struct({
|
|
45
|
+
address: Schema.optional(Address),
|
|
46
|
+
topics: OptionalArray(Topic),
|
|
47
|
+
|
|
48
|
+
strict: Schema.optional(Schema.Boolean),
|
|
49
|
+
includeTransaction: Schema.optional(Schema.Boolean),
|
|
50
|
+
includeReceipt: Schema.optional(Schema.Boolean),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export type LogFilter = typeof LogFilter.Type;
|
|
54
|
+
|
|
55
|
+
export const TransactionFilter = Schema.Struct({
|
|
56
|
+
from: Schema.optional(Address),
|
|
57
|
+
to: Schema.optional(Address),
|
|
58
|
+
|
|
59
|
+
includeReceipt: Schema.optional(Schema.Boolean),
|
|
60
|
+
includeLogs: Schema.optional(Schema.Boolean),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export type TransactionFilter = typeof TransactionFilter.Type;
|
|
64
|
+
|
|
65
|
+
export const Filter = Schema.Struct({
|
|
66
|
+
header: Schema.optional(HeaderFilter),
|
|
67
|
+
withdrawals: OptionalArray(WithdrawalFilter),
|
|
68
|
+
logs: OptionalArray(LogFilter),
|
|
69
|
+
transactions: OptionalArray(TransactionFilter),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
export type Filter = typeof Filter.Type;
|
|
73
|
+
|
|
74
|
+
export const filterToProto = Schema.encodeSync(Filter);
|
|
75
|
+
export const filterFromProto = Schema.decodeSync(Filter);
|
|
76
|
+
|
|
77
|
+
export const FilterFromBytes = Schema.transform(
|
|
78
|
+
Schema.Uint8ArrayFromSelf,
|
|
79
|
+
Filter,
|
|
80
|
+
{
|
|
81
|
+
strict: false,
|
|
82
|
+
decode(value) {
|
|
83
|
+
return proto.filter.Filter.decode(value);
|
|
84
|
+
},
|
|
85
|
+
encode(value) {
|
|
86
|
+
return proto.filter.Filter.encode(value).finish();
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
export const filterToBytes = Schema.encodeSync(FilterFromBytes);
|
|
92
|
+
export const filterFromBytes = Schema.decodeSync(FilterFromBytes);
|
|
93
|
+
|
|
94
|
+
export function mergeFilter(a: Filter, b: Filter): Filter {
|
|
95
|
+
const header = mergeHeaderFilter(a.header, b.header);
|
|
96
|
+
return {
|
|
97
|
+
header,
|
|
98
|
+
withdrawals: [...(a.withdrawals ?? []), ...(b.withdrawals ?? [])],
|
|
99
|
+
logs: [...(a.logs ?? []), ...(b.logs ?? [])],
|
|
100
|
+
transactions: [...(a.transactions ?? []), ...(b.transactions ?? [])],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function mergeHeaderFilter(
|
|
105
|
+
a?: HeaderFilter,
|
|
106
|
+
b?: HeaderFilter,
|
|
107
|
+
): HeaderFilter | undefined {
|
|
108
|
+
if (a === undefined) {
|
|
109
|
+
return b;
|
|
110
|
+
}
|
|
111
|
+
if (b === undefined) {
|
|
112
|
+
return a;
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
always: a.always || b.always,
|
|
116
|
+
};
|
|
117
|
+
}
|
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
|
+
);
|