@apibara/evm 2.1.0-beta.3 → 2.1.0-beta.30
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/dist/index.cjs +1553 -418
- package/dist/index.d.cts +1287 -1416
- package/dist/index.d.mts +1287 -1416
- package/dist/index.d.ts +1287 -1416
- package/dist/index.mjs +1544 -409
- package/package.json +2 -4
- package/src/block.ts +358 -167
- package/src/common.ts +64 -95
- package/src/filter.ts +128 -134
- package/src/index.ts +1 -0
- package/src/proto/data.ts +1310 -3
- package/src/proto/filter.ts +46 -2
- package/src/common.test.ts +0 -79
- package/src/filter.test.ts +0 -187
package/src/common.ts
CHANGED
|
@@ -1,117 +1,86 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import type { Codec, CodecType } from "@apibara/protocol/codec";
|
|
2
|
+
import type * as proto from "./proto";
|
|
3
3
|
|
|
4
4
|
const MAX_U64 = 0xffffffffffffffffn;
|
|
5
|
-
|
|
6
|
-
const _Address = Schema.TemplateLiteral(Schema.Literal("0x"), Schema.String);
|
|
7
|
-
|
|
8
|
-
/** Wire representation of `Address`. */
|
|
9
|
-
const AddressProto = Schema.Struct({
|
|
10
|
-
x0: Schema.BigIntFromSelf,
|
|
11
|
-
x1: Schema.BigIntFromSelf,
|
|
12
|
-
x2: Schema.Number,
|
|
13
|
-
});
|
|
5
|
+
const MAX_U32 = 0xffffffffn;
|
|
14
6
|
|
|
15
7
|
/** An Ethereum address. */
|
|
16
|
-
export const Address
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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) };
|
|
22
17
|
},
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
return { x0, x1, x2 };
|
|
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}`;
|
|
30
24
|
},
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type Address = typeof Address.Type;
|
|
25
|
+
};
|
|
34
26
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
/** Wire representation of `B256`. */
|
|
38
|
-
export const B256Proto = Schema.Struct({
|
|
39
|
-
x0: Schema.BigIntFromSelf,
|
|
40
|
-
x1: Schema.BigIntFromSelf,
|
|
41
|
-
x2: Schema.BigIntFromSelf,
|
|
42
|
-
x3: Schema.BigIntFromSelf,
|
|
43
|
-
});
|
|
27
|
+
export type Address = CodecType<typeof Address>;
|
|
44
28
|
|
|
45
29
|
/** Data with length 256 bits. */
|
|
46
|
-
export const B256
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const x2 =
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
},
|
|
54
|
-
encode(value) {
|
|
55
|
-
const bytes = hexToBytes(pad(value, { size: 32, dir: "left" }));
|
|
56
|
-
const dv = new DataView(bytes.buffer);
|
|
57
|
-
const x0 = dv.getBigUint64(0);
|
|
58
|
-
const x1 = dv.getBigUint64(8);
|
|
59
|
-
const x2 = dv.getBigUint64(16);
|
|
60
|
-
const x3 = dv.getBigUint64(24);
|
|
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;
|
|
61
37
|
return { x0, x1, x2, x3 };
|
|
62
38
|
},
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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}`;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
67
48
|
|
|
68
|
-
|
|
69
|
-
const U256Proto = Schema.Struct({
|
|
70
|
-
x0: Schema.BigIntFromSelf,
|
|
71
|
-
x1: Schema.BigIntFromSelf,
|
|
72
|
-
x2: Schema.BigIntFromSelf,
|
|
73
|
-
x3: Schema.BigIntFromSelf,
|
|
74
|
-
});
|
|
49
|
+
export type B256 = CodecType<typeof B256>;
|
|
75
50
|
|
|
76
51
|
/** Data with length 256 bits. */
|
|
77
|
-
export const U256
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
);
|
|
85
|
-
},
|
|
86
|
-
encode(value) {
|
|
87
|
-
const x0 = (value >> (8n * 24n)) & MAX_U64;
|
|
88
|
-
const x1 = (value >> (8n * 16n)) & MAX_U64;
|
|
89
|
-
const x2 = (value >> (8n * 8n)) & MAX_U64;
|
|
90
|
-
const x3 = value & MAX_U64;
|
|
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;
|
|
91
59
|
return { x0, x1, x2, x3 };
|
|
92
60
|
},
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
+
};
|
|
97
69
|
|
|
98
|
-
|
|
99
|
-
const U128Proto = Schema.Struct({
|
|
100
|
-
x0: Schema.BigIntFromSelf,
|
|
101
|
-
x1: Schema.BigIntFromSelf,
|
|
102
|
-
});
|
|
70
|
+
export type U256 = CodecType<typeof U256>;
|
|
103
71
|
|
|
104
72
|
/** Data with length 128 bits. */
|
|
105
|
-
export const U128
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
encode(value) {
|
|
110
|
-
const x0 = (value >> (8n * 8n)) & MAX_U64;
|
|
111
|
-
const x1 = value & MAX_U64;
|
|
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;
|
|
112
77
|
return { x0, x1 };
|
|
113
78
|
},
|
|
114
|
-
|
|
79
|
+
decode(p) {
|
|
80
|
+
const x0 = p.x0 ?? 0n;
|
|
81
|
+
const x1 = p.x1 ?? 0n;
|
|
82
|
+
return x1 + (x0 << 64n);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
115
85
|
|
|
116
|
-
export
|
|
117
|
-
export const u128FromProto = Schema.decodeSync(U128);
|
|
86
|
+
export type U128 = CodecType<typeof U128>;
|
package/src/filter.ts
CHANGED
|
@@ -1,160 +1,154 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { Address, B256 } from "./common";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ArrayCodec,
|
|
5
|
+
BooleanCodec,
|
|
6
|
+
type Codec,
|
|
7
|
+
type CodecType,
|
|
8
|
+
MessageCodec,
|
|
9
|
+
NumberCodec,
|
|
10
|
+
OptionalCodec,
|
|
11
|
+
} from "@apibara/protocol/codec";
|
|
5
12
|
import * as proto from "./proto";
|
|
6
13
|
|
|
7
|
-
const OptionalArray = <TSchema extends Schema.Schema.Any>(schema: TSchema) =>
|
|
8
|
-
Schema.optional(Schema.Array(schema));
|
|
9
|
-
|
|
10
14
|
/** Header options.
|
|
11
15
|
*
|
|
12
16
|
* - `always`: receive all block headers.
|
|
13
17
|
* - `on_data`: receive headers only if any other filter matches.
|
|
14
18
|
* - `on_data_or_on_new_block`: receive headers only if any other filter matches and for "live" blocks.
|
|
15
19
|
*/
|
|
16
|
-
export const HeaderFilter
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
encode(value) {
|
|
32
|
-
switch (value) {
|
|
33
|
-
case "always":
|
|
34
|
-
return proto.filter.HeaderFilter.ALWAYS;
|
|
35
|
-
case "on_data":
|
|
36
|
-
return proto.filter.HeaderFilter.ON_DATA;
|
|
37
|
-
case "on_data_or_on_new_block":
|
|
38
|
-
return proto.filter.HeaderFilter.ON_DATA_OR_ON_NEW_BLOCK;
|
|
39
|
-
default:
|
|
40
|
-
return proto.filter.HeaderFilter.UNSPECIFIED;
|
|
41
|
-
}
|
|
42
|
-
},
|
|
20
|
+
export const HeaderFilter: Codec<
|
|
21
|
+
"always" | "on_data" | "on_data_or_on_new_block" | "unknown",
|
|
22
|
+
proto.filter.HeaderFilter
|
|
23
|
+
> = {
|
|
24
|
+
encode(x) {
|
|
25
|
+
switch (x) {
|
|
26
|
+
case "always":
|
|
27
|
+
return proto.filter.HeaderFilter.ALWAYS;
|
|
28
|
+
case "on_data":
|
|
29
|
+
return proto.filter.HeaderFilter.ON_DATA;
|
|
30
|
+
case "on_data_or_on_new_block":
|
|
31
|
+
return proto.filter.HeaderFilter.ON_DATA_OR_ON_NEW_BLOCK;
|
|
32
|
+
default:
|
|
33
|
+
return proto.filter.HeaderFilter.UNSPECIFIED;
|
|
34
|
+
}
|
|
43
35
|
},
|
|
44
|
-
)
|
|
36
|
+
decode(p) {
|
|
37
|
+
const enumMap = {
|
|
38
|
+
[proto.filter.HeaderFilter.ALWAYS]: "always",
|
|
39
|
+
[proto.filter.HeaderFilter.ON_DATA]: "on_data",
|
|
40
|
+
[proto.filter.HeaderFilter.ON_DATA_OR_ON_NEW_BLOCK]:
|
|
41
|
+
"on_data_or_on_new_block",
|
|
42
|
+
[proto.filter.HeaderFilter.UNSPECIFIED]: "unknown",
|
|
43
|
+
[proto.filter.HeaderFilter.UNRECOGNIZED]: "unknown",
|
|
44
|
+
} as const;
|
|
45
|
+
return enumMap[p] ?? "unknown";
|
|
46
|
+
},
|
|
47
|
+
};
|
|
45
48
|
|
|
46
|
-
export type HeaderFilter = typeof HeaderFilter
|
|
49
|
+
export type HeaderFilter = CodecType<typeof HeaderFilter>;
|
|
47
50
|
|
|
48
|
-
export const WithdrawalFilter =
|
|
49
|
-
id:
|
|
50
|
-
validatorIndex:
|
|
51
|
-
address:
|
|
51
|
+
export const WithdrawalFilter = MessageCodec({
|
|
52
|
+
id: OptionalCodec(NumberCodec),
|
|
53
|
+
validatorIndex: OptionalCodec(NumberCodec),
|
|
54
|
+
address: OptionalCodec(Address),
|
|
52
55
|
});
|
|
53
56
|
|
|
54
|
-
export type WithdrawalFilter = typeof WithdrawalFilter
|
|
55
|
-
|
|
56
|
-
export const TransactionStatusFilter
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
},
|
|
57
|
+
export type WithdrawalFilter = CodecType<typeof WithdrawalFilter>;
|
|
58
|
+
|
|
59
|
+
export const TransactionStatusFilter: Codec<
|
|
60
|
+
"succeeded" | "reverted" | "all" | "unknown",
|
|
61
|
+
proto.filter.TransactionStatusFilter
|
|
62
|
+
> = {
|
|
63
|
+
decode(value) {
|
|
64
|
+
const enumMap = {
|
|
65
|
+
[proto.filter.TransactionStatusFilter.SUCCEEDED]: "succeeded",
|
|
66
|
+
[proto.filter.TransactionStatusFilter.REVERTED]: "reverted",
|
|
67
|
+
[proto.filter.TransactionStatusFilter.ALL]: "all",
|
|
68
|
+
[proto.filter.TransactionStatusFilter.UNSPECIFIED]: "unknown",
|
|
69
|
+
[proto.filter.TransactionStatusFilter.UNRECOGNIZED]: "unknown",
|
|
70
|
+
} as const;
|
|
71
|
+
return enumMap[value] ?? "unknown";
|
|
72
|
+
},
|
|
73
|
+
encode(value) {
|
|
74
|
+
switch (value) {
|
|
75
|
+
case "succeeded":
|
|
76
|
+
return proto.filter.TransactionStatusFilter.SUCCEEDED;
|
|
77
|
+
case "reverted":
|
|
78
|
+
return proto.filter.TransactionStatusFilter.REVERTED;
|
|
79
|
+
case "all":
|
|
80
|
+
return proto.filter.TransactionStatusFilter.ALL;
|
|
81
|
+
default:
|
|
82
|
+
return proto.filter.TransactionStatusFilter.UNSPECIFIED;
|
|
83
|
+
}
|
|
82
84
|
},
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
export type TransactionStatusFilter = typeof TransactionStatusFilter
|
|
86
|
-
|
|
87
|
-
export const Topic
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
},
|
|
97
|
-
encode(value) {
|
|
98
|
-
if (value === null) {
|
|
99
|
-
return { value: undefined };
|
|
100
|
-
}
|
|
101
|
-
return { value };
|
|
102
|
-
},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export type TransactionStatusFilter = CodecType<typeof TransactionStatusFilter>;
|
|
88
|
+
|
|
89
|
+
export const Topic: Codec<
|
|
90
|
+
B256 | null,
|
|
91
|
+
{ value?: proto.common.B256 | undefined }
|
|
92
|
+
> = {
|
|
93
|
+
encode(x) {
|
|
94
|
+
if (x === null) {
|
|
95
|
+
return { value: undefined };
|
|
96
|
+
}
|
|
97
|
+
return { value: B256.encode(x) };
|
|
103
98
|
},
|
|
104
|
-
)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
99
|
+
decode({ value }) {
|
|
100
|
+
if (value === undefined) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
return B256.decode(value);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export type Topic = CodecType<typeof Topic>;
|
|
108
|
+
|
|
109
|
+
export const LogFilter = MessageCodec({
|
|
110
|
+
id: OptionalCodec(NumberCodec),
|
|
111
|
+
address: OptionalCodec(Address),
|
|
112
|
+
topics: OptionalCodec(ArrayCodec(Topic)),
|
|
113
|
+
strict: OptionalCodec(BooleanCodec),
|
|
114
|
+
transactionStatus: OptionalCodec(TransactionStatusFilter),
|
|
115
|
+
includeTransaction: OptionalCodec(BooleanCodec),
|
|
116
|
+
includeReceipt: OptionalCodec(BooleanCodec),
|
|
117
|
+
includeTransactionTrace: OptionalCodec(BooleanCodec),
|
|
114
118
|
});
|
|
115
119
|
|
|
116
|
-
export type LogFilter = typeof LogFilter
|
|
117
|
-
|
|
118
|
-
export const TransactionFilter =
|
|
119
|
-
id:
|
|
120
|
-
from:
|
|
121
|
-
to:
|
|
122
|
-
create:
|
|
123
|
-
transactionStatus:
|
|
124
|
-
includeReceipt:
|
|
125
|
-
includeLogs:
|
|
120
|
+
export type LogFilter = Readonly<CodecType<typeof LogFilter>>;
|
|
121
|
+
|
|
122
|
+
export const TransactionFilter = MessageCodec({
|
|
123
|
+
id: OptionalCodec(NumberCodec),
|
|
124
|
+
from: OptionalCodec(Address),
|
|
125
|
+
to: OptionalCodec(Address),
|
|
126
|
+
create: OptionalCodec(BooleanCodec),
|
|
127
|
+
transactionStatus: OptionalCodec(TransactionStatusFilter),
|
|
128
|
+
includeReceipt: OptionalCodec(BooleanCodec),
|
|
129
|
+
includeLogs: OptionalCodec(BooleanCodec),
|
|
130
|
+
includeTransactionTrace: OptionalCodec(BooleanCodec),
|
|
126
131
|
});
|
|
127
132
|
|
|
128
|
-
export type TransactionFilter = typeof TransactionFilter
|
|
133
|
+
export type TransactionFilter = Readonly<CodecType<typeof TransactionFilter>>;
|
|
129
134
|
|
|
130
|
-
export const Filter =
|
|
131
|
-
header:
|
|
132
|
-
withdrawals:
|
|
133
|
-
transactions:
|
|
134
|
-
logs:
|
|
135
|
+
export const Filter = MessageCodec({
|
|
136
|
+
header: OptionalCodec(HeaderFilter),
|
|
137
|
+
withdrawals: OptionalCodec(ArrayCodec(WithdrawalFilter)),
|
|
138
|
+
transactions: OptionalCodec(ArrayCodec(TransactionFilter)),
|
|
139
|
+
logs: OptionalCodec(ArrayCodec(LogFilter)),
|
|
135
140
|
});
|
|
136
141
|
|
|
137
|
-
export type Filter = typeof Filter
|
|
138
|
-
|
|
139
|
-
export const filterToProto = Schema.encodeSync(Filter);
|
|
140
|
-
export const filterFromProto = Schema.decodeSync(Filter);
|
|
141
|
-
|
|
142
|
-
export const FilterFromBytes = Schema.transform(
|
|
143
|
-
Schema.Uint8ArrayFromSelf,
|
|
144
|
-
Filter,
|
|
145
|
-
{
|
|
146
|
-
strict: false,
|
|
147
|
-
decode(value) {
|
|
148
|
-
return proto.filter.Filter.decode(value);
|
|
149
|
-
},
|
|
150
|
-
encode(value) {
|
|
151
|
-
return proto.filter.Filter.encode(value).finish();
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
|
-
);
|
|
142
|
+
export type Filter = Readonly<CodecType<typeof Filter>>;
|
|
155
143
|
|
|
156
|
-
export const
|
|
157
|
-
|
|
144
|
+
export const FilterFromBytes: Codec<Filter, Uint8Array> = {
|
|
145
|
+
encode(x) {
|
|
146
|
+
return proto.filter.Filter.encode(Filter.encode(x)).finish();
|
|
147
|
+
},
|
|
148
|
+
decode(p) {
|
|
149
|
+
return Filter.decode(proto.filter.Filter.decode(p));
|
|
150
|
+
},
|
|
151
|
+
};
|
|
158
152
|
|
|
159
153
|
export function mergeFilter(a: Filter, b: Filter): Filter {
|
|
160
154
|
const header = mergeHeaderFilter(a.header, b.header);
|