@apibara/starknet 2.0.0-beta.9 → 2.1.0-beta.2
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 +353 -169
- package/dist/index.d.cts +1297 -1263
- package/dist/index.d.mts +1297 -1263
- package/dist/index.d.ts +1297 -1263
- package/dist/index.mjs +349 -170
- package/dist/parser.cjs +133 -0
- package/dist/parser.d.cts +72 -0
- package/dist/parser.d.mts +72 -0
- package/dist/parser.d.ts +72 -0
- package/dist/parser.mjs +109 -0
- package/dist/shared/starknet.2b19268a.d.cts +32 -0
- package/dist/shared/starknet.2b19268a.d.mts +32 -0
- package/dist/shared/starknet.2b19268a.d.ts +32 -0
- package/package.json +10 -2
- package/src/abi.ts +79 -0
- package/src/access.ts +6 -2
- package/src/block.ts +151 -155
- package/src/common.ts +2 -0
- package/src/event.ts +204 -0
- package/src/index.ts +11 -0
- package/src/parser.test.ts +169 -0
- package/src/parser.ts +170 -0
- package/src/proto/data.ts +56 -12
package/dist/parser.cjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class ParseError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "ParseError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function assertInBounds(data, offset) {
|
|
10
|
+
if (offset >= data.length) {
|
|
11
|
+
throw new ParseError(
|
|
12
|
+
`Offset out of bounds. Data length ${data.length}, offset ${offset}`
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function parseBool(data, offset) {
|
|
17
|
+
assertInBounds(data, offset);
|
|
18
|
+
return { out: BigInt(data[offset]) > 0n, offset: offset + 1 };
|
|
19
|
+
}
|
|
20
|
+
function parseAsBigInt(data, offset) {
|
|
21
|
+
assertInBounds(data, offset);
|
|
22
|
+
return { out: BigInt(data[offset]), offset: offset + 1 };
|
|
23
|
+
}
|
|
24
|
+
const parseU8 = parseAsBigInt;
|
|
25
|
+
const parseU16 = parseAsBigInt;
|
|
26
|
+
const parseU32 = parseAsBigInt;
|
|
27
|
+
const parseU64 = parseAsBigInt;
|
|
28
|
+
const parseU128 = parseAsBigInt;
|
|
29
|
+
const parseUsize = parseAsBigInt;
|
|
30
|
+
function parseU256(data, offset) {
|
|
31
|
+
assertInBounds(data, offset + 1);
|
|
32
|
+
return {
|
|
33
|
+
out: BigInt(data[offset]) + (BigInt(data[offset + 1]) << 128n),
|
|
34
|
+
offset: offset + 2
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function parseAsHex(data, offset) {
|
|
38
|
+
assertInBounds(data, offset);
|
|
39
|
+
return {
|
|
40
|
+
out: String(data[offset]),
|
|
41
|
+
offset: offset + 1
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const parseContractAddress = parseAsHex;
|
|
45
|
+
const parseEthAddress = parseAsHex;
|
|
46
|
+
const parseStorageAddress = parseAsHex;
|
|
47
|
+
const parseClassHash = parseAsHex;
|
|
48
|
+
const parseBytes31 = parseAsHex;
|
|
49
|
+
function parseFelt252(data, offset) {
|
|
50
|
+
assertInBounds(data, offset);
|
|
51
|
+
return {
|
|
52
|
+
out: BigInt(data[offset]),
|
|
53
|
+
offset: offset + 1
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function parseEmpty(data, offset) {
|
|
57
|
+
return { out: null, offset };
|
|
58
|
+
}
|
|
59
|
+
function parseArray(type) {
|
|
60
|
+
return (data, startingOffset) => {
|
|
61
|
+
let offset = startingOffset;
|
|
62
|
+
const length = BigInt(data[offset]);
|
|
63
|
+
offset++;
|
|
64
|
+
const out = [];
|
|
65
|
+
for (let i = 0; i < length; i++) {
|
|
66
|
+
const { out: item, offset: newOffset } = type(data, offset);
|
|
67
|
+
out.push(item);
|
|
68
|
+
offset = newOffset;
|
|
69
|
+
}
|
|
70
|
+
return { out, offset };
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const parseSpan = parseArray;
|
|
74
|
+
function parseOption(type) {
|
|
75
|
+
return (data, offset) => {
|
|
76
|
+
const hasValue = BigInt(data[offset]) === 1n;
|
|
77
|
+
if (hasValue) {
|
|
78
|
+
return type(data, offset + 1);
|
|
79
|
+
}
|
|
80
|
+
return { out: null, offset: offset + 1 };
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function parseStruct(parsers) {
|
|
84
|
+
const sortedParsers = Object.entries(parsers).sort(
|
|
85
|
+
(a, b) => a[1].index - b[1].index
|
|
86
|
+
);
|
|
87
|
+
return (data, startingOffset) => {
|
|
88
|
+
let offset = startingOffset;
|
|
89
|
+
const out = {};
|
|
90
|
+
for (const [key, { parser }] of sortedParsers) {
|
|
91
|
+
const { out: value, offset: newOffset } = parser(data, offset);
|
|
92
|
+
out[key] = value;
|
|
93
|
+
offset = newOffset;
|
|
94
|
+
}
|
|
95
|
+
return { out, offset };
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function parseTuple(...parsers) {
|
|
99
|
+
return (data, startingOffset) => {
|
|
100
|
+
let offset = startingOffset;
|
|
101
|
+
const out = [];
|
|
102
|
+
for (const parser of parsers) {
|
|
103
|
+
const { out: value, offset: newOffset } = parser(data, offset);
|
|
104
|
+
out.push(value);
|
|
105
|
+
offset = newOffset;
|
|
106
|
+
}
|
|
107
|
+
return { out, offset };
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
exports.ParseError = ParseError;
|
|
112
|
+
exports.parseArray = parseArray;
|
|
113
|
+
exports.parseAsBigInt = parseAsBigInt;
|
|
114
|
+
exports.parseAsHex = parseAsHex;
|
|
115
|
+
exports.parseBool = parseBool;
|
|
116
|
+
exports.parseBytes31 = parseBytes31;
|
|
117
|
+
exports.parseClassHash = parseClassHash;
|
|
118
|
+
exports.parseContractAddress = parseContractAddress;
|
|
119
|
+
exports.parseEmpty = parseEmpty;
|
|
120
|
+
exports.parseEthAddress = parseEthAddress;
|
|
121
|
+
exports.parseFelt252 = parseFelt252;
|
|
122
|
+
exports.parseOption = parseOption;
|
|
123
|
+
exports.parseSpan = parseSpan;
|
|
124
|
+
exports.parseStorageAddress = parseStorageAddress;
|
|
125
|
+
exports.parseStruct = parseStruct;
|
|
126
|
+
exports.parseTuple = parseTuple;
|
|
127
|
+
exports.parseU128 = parseU128;
|
|
128
|
+
exports.parseU16 = parseU16;
|
|
129
|
+
exports.parseU256 = parseU256;
|
|
130
|
+
exports.parseU32 = parseU32;
|
|
131
|
+
exports.parseU64 = parseU64;
|
|
132
|
+
exports.parseU8 = parseU8;
|
|
133
|
+
exports.parseUsize = parseUsize;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { F as FieldElement } from './shared/starknet.2b19268a.cjs';
|
|
2
|
+
import '@effect/schema/AST';
|
|
3
|
+
import '@effect/schema';
|
|
4
|
+
|
|
5
|
+
type Parser<TOut> = (data: readonly FieldElement[], offset: number) => {
|
|
6
|
+
out: TOut;
|
|
7
|
+
offset: number;
|
|
8
|
+
};
|
|
9
|
+
declare class ParseError extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
declare function parseBool(data: readonly FieldElement[], offset: number): {
|
|
13
|
+
out: boolean;
|
|
14
|
+
offset: number;
|
|
15
|
+
};
|
|
16
|
+
declare function parseAsBigInt(data: readonly FieldElement[], offset: number): {
|
|
17
|
+
out: bigint;
|
|
18
|
+
offset: number;
|
|
19
|
+
};
|
|
20
|
+
declare const parseU8: typeof parseAsBigInt;
|
|
21
|
+
declare const parseU16: typeof parseAsBigInt;
|
|
22
|
+
declare const parseU32: typeof parseAsBigInt;
|
|
23
|
+
declare const parseU64: typeof parseAsBigInt;
|
|
24
|
+
declare const parseU128: typeof parseAsBigInt;
|
|
25
|
+
declare const parseUsize: typeof parseAsBigInt;
|
|
26
|
+
declare function parseU256(data: readonly FieldElement[], offset: number): {
|
|
27
|
+
out: bigint;
|
|
28
|
+
offset: number;
|
|
29
|
+
};
|
|
30
|
+
declare function parseAsHex(data: readonly FieldElement[], offset: number): {
|
|
31
|
+
out: string;
|
|
32
|
+
offset: number;
|
|
33
|
+
};
|
|
34
|
+
declare const parseContractAddress: typeof parseAsHex;
|
|
35
|
+
declare const parseEthAddress: typeof parseAsHex;
|
|
36
|
+
declare const parseStorageAddress: typeof parseAsHex;
|
|
37
|
+
declare const parseClassHash: typeof parseAsHex;
|
|
38
|
+
declare const parseBytes31: typeof parseAsHex;
|
|
39
|
+
declare function parseFelt252(data: readonly FieldElement[], offset: number): {
|
|
40
|
+
out: bigint;
|
|
41
|
+
offset: number;
|
|
42
|
+
};
|
|
43
|
+
declare function parseEmpty(data: readonly FieldElement[], offset: number): {
|
|
44
|
+
out: null;
|
|
45
|
+
offset: number;
|
|
46
|
+
};
|
|
47
|
+
declare function parseArray<T>(type: Parser<T>): Parser<T[]>;
|
|
48
|
+
declare const parseSpan: typeof parseArray;
|
|
49
|
+
declare function parseOption<T>(type: Parser<T>): (data: readonly FieldElement[], offset: number) => {
|
|
50
|
+
out: T;
|
|
51
|
+
offset: number;
|
|
52
|
+
} | {
|
|
53
|
+
out: null;
|
|
54
|
+
offset: number;
|
|
55
|
+
};
|
|
56
|
+
declare function parseStruct<T extends {
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}>(parsers: {
|
|
59
|
+
[K in keyof T]: {
|
|
60
|
+
index: number;
|
|
61
|
+
parser: Parser<T[K]>;
|
|
62
|
+
};
|
|
63
|
+
}): (data: readonly FieldElement[], startingOffset: number) => {
|
|
64
|
+
out: Record<string, unknown>;
|
|
65
|
+
offset: number;
|
|
66
|
+
};
|
|
67
|
+
declare function parseTuple<T extends Parser<unknown>[]>(...parsers: T): Parser<UnwrapParsers<T>>;
|
|
68
|
+
type UnwrapParsers<TP> = {
|
|
69
|
+
[Index in keyof TP]: TP[Index] extends Parser<infer U> ? U : never;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { ParseError, type Parser, parseArray, parseAsBigInt, parseAsHex, parseBool, parseBytes31, parseClassHash, parseContractAddress, parseEmpty, parseEthAddress, parseFelt252, parseOption, parseSpan, parseStorageAddress, parseStruct, parseTuple, parseU128, parseU16, parseU256, parseU32, parseU64, parseU8, parseUsize };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { F as FieldElement } from './shared/starknet.2b19268a.mjs';
|
|
2
|
+
import '@effect/schema/AST';
|
|
3
|
+
import '@effect/schema';
|
|
4
|
+
|
|
5
|
+
type Parser<TOut> = (data: readonly FieldElement[], offset: number) => {
|
|
6
|
+
out: TOut;
|
|
7
|
+
offset: number;
|
|
8
|
+
};
|
|
9
|
+
declare class ParseError extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
declare function parseBool(data: readonly FieldElement[], offset: number): {
|
|
13
|
+
out: boolean;
|
|
14
|
+
offset: number;
|
|
15
|
+
};
|
|
16
|
+
declare function parseAsBigInt(data: readonly FieldElement[], offset: number): {
|
|
17
|
+
out: bigint;
|
|
18
|
+
offset: number;
|
|
19
|
+
};
|
|
20
|
+
declare const parseU8: typeof parseAsBigInt;
|
|
21
|
+
declare const parseU16: typeof parseAsBigInt;
|
|
22
|
+
declare const parseU32: typeof parseAsBigInt;
|
|
23
|
+
declare const parseU64: typeof parseAsBigInt;
|
|
24
|
+
declare const parseU128: typeof parseAsBigInt;
|
|
25
|
+
declare const parseUsize: typeof parseAsBigInt;
|
|
26
|
+
declare function parseU256(data: readonly FieldElement[], offset: number): {
|
|
27
|
+
out: bigint;
|
|
28
|
+
offset: number;
|
|
29
|
+
};
|
|
30
|
+
declare function parseAsHex(data: readonly FieldElement[], offset: number): {
|
|
31
|
+
out: string;
|
|
32
|
+
offset: number;
|
|
33
|
+
};
|
|
34
|
+
declare const parseContractAddress: typeof parseAsHex;
|
|
35
|
+
declare const parseEthAddress: typeof parseAsHex;
|
|
36
|
+
declare const parseStorageAddress: typeof parseAsHex;
|
|
37
|
+
declare const parseClassHash: typeof parseAsHex;
|
|
38
|
+
declare const parseBytes31: typeof parseAsHex;
|
|
39
|
+
declare function parseFelt252(data: readonly FieldElement[], offset: number): {
|
|
40
|
+
out: bigint;
|
|
41
|
+
offset: number;
|
|
42
|
+
};
|
|
43
|
+
declare function parseEmpty(data: readonly FieldElement[], offset: number): {
|
|
44
|
+
out: null;
|
|
45
|
+
offset: number;
|
|
46
|
+
};
|
|
47
|
+
declare function parseArray<T>(type: Parser<T>): Parser<T[]>;
|
|
48
|
+
declare const parseSpan: typeof parseArray;
|
|
49
|
+
declare function parseOption<T>(type: Parser<T>): (data: readonly FieldElement[], offset: number) => {
|
|
50
|
+
out: T;
|
|
51
|
+
offset: number;
|
|
52
|
+
} | {
|
|
53
|
+
out: null;
|
|
54
|
+
offset: number;
|
|
55
|
+
};
|
|
56
|
+
declare function parseStruct<T extends {
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}>(parsers: {
|
|
59
|
+
[K in keyof T]: {
|
|
60
|
+
index: number;
|
|
61
|
+
parser: Parser<T[K]>;
|
|
62
|
+
};
|
|
63
|
+
}): (data: readonly FieldElement[], startingOffset: number) => {
|
|
64
|
+
out: Record<string, unknown>;
|
|
65
|
+
offset: number;
|
|
66
|
+
};
|
|
67
|
+
declare function parseTuple<T extends Parser<unknown>[]>(...parsers: T): Parser<UnwrapParsers<T>>;
|
|
68
|
+
type UnwrapParsers<TP> = {
|
|
69
|
+
[Index in keyof TP]: TP[Index] extends Parser<infer U> ? U : never;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { ParseError, type Parser, parseArray, parseAsBigInt, parseAsHex, parseBool, parseBytes31, parseClassHash, parseContractAddress, parseEmpty, parseEthAddress, parseFelt252, parseOption, parseSpan, parseStorageAddress, parseStruct, parseTuple, parseU128, parseU16, parseU256, parseU32, parseU64, parseU8, parseUsize };
|
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { F as FieldElement } from './shared/starknet.2b19268a.js';
|
|
2
|
+
import '@effect/schema/AST';
|
|
3
|
+
import '@effect/schema';
|
|
4
|
+
|
|
5
|
+
type Parser<TOut> = (data: readonly FieldElement[], offset: number) => {
|
|
6
|
+
out: TOut;
|
|
7
|
+
offset: number;
|
|
8
|
+
};
|
|
9
|
+
declare class ParseError extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
declare function parseBool(data: readonly FieldElement[], offset: number): {
|
|
13
|
+
out: boolean;
|
|
14
|
+
offset: number;
|
|
15
|
+
};
|
|
16
|
+
declare function parseAsBigInt(data: readonly FieldElement[], offset: number): {
|
|
17
|
+
out: bigint;
|
|
18
|
+
offset: number;
|
|
19
|
+
};
|
|
20
|
+
declare const parseU8: typeof parseAsBigInt;
|
|
21
|
+
declare const parseU16: typeof parseAsBigInt;
|
|
22
|
+
declare const parseU32: typeof parseAsBigInt;
|
|
23
|
+
declare const parseU64: typeof parseAsBigInt;
|
|
24
|
+
declare const parseU128: typeof parseAsBigInt;
|
|
25
|
+
declare const parseUsize: typeof parseAsBigInt;
|
|
26
|
+
declare function parseU256(data: readonly FieldElement[], offset: number): {
|
|
27
|
+
out: bigint;
|
|
28
|
+
offset: number;
|
|
29
|
+
};
|
|
30
|
+
declare function parseAsHex(data: readonly FieldElement[], offset: number): {
|
|
31
|
+
out: string;
|
|
32
|
+
offset: number;
|
|
33
|
+
};
|
|
34
|
+
declare const parseContractAddress: typeof parseAsHex;
|
|
35
|
+
declare const parseEthAddress: typeof parseAsHex;
|
|
36
|
+
declare const parseStorageAddress: typeof parseAsHex;
|
|
37
|
+
declare const parseClassHash: typeof parseAsHex;
|
|
38
|
+
declare const parseBytes31: typeof parseAsHex;
|
|
39
|
+
declare function parseFelt252(data: readonly FieldElement[], offset: number): {
|
|
40
|
+
out: bigint;
|
|
41
|
+
offset: number;
|
|
42
|
+
};
|
|
43
|
+
declare function parseEmpty(data: readonly FieldElement[], offset: number): {
|
|
44
|
+
out: null;
|
|
45
|
+
offset: number;
|
|
46
|
+
};
|
|
47
|
+
declare function parseArray<T>(type: Parser<T>): Parser<T[]>;
|
|
48
|
+
declare const parseSpan: typeof parseArray;
|
|
49
|
+
declare function parseOption<T>(type: Parser<T>): (data: readonly FieldElement[], offset: number) => {
|
|
50
|
+
out: T;
|
|
51
|
+
offset: number;
|
|
52
|
+
} | {
|
|
53
|
+
out: null;
|
|
54
|
+
offset: number;
|
|
55
|
+
};
|
|
56
|
+
declare function parseStruct<T extends {
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}>(parsers: {
|
|
59
|
+
[K in keyof T]: {
|
|
60
|
+
index: number;
|
|
61
|
+
parser: Parser<T[K]>;
|
|
62
|
+
};
|
|
63
|
+
}): (data: readonly FieldElement[], startingOffset: number) => {
|
|
64
|
+
out: Record<string, unknown>;
|
|
65
|
+
offset: number;
|
|
66
|
+
};
|
|
67
|
+
declare function parseTuple<T extends Parser<unknown>[]>(...parsers: T): Parser<UnwrapParsers<T>>;
|
|
68
|
+
type UnwrapParsers<TP> = {
|
|
69
|
+
[Index in keyof TP]: TP[Index] extends Parser<infer U> ? U : never;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { ParseError, type Parser, parseArray, parseAsBigInt, parseAsHex, parseBool, parseBytes31, parseClassHash, parseContractAddress, parseEmpty, parseEthAddress, parseFelt252, parseOption, parseSpan, parseStorageAddress, parseStruct, parseTuple, parseU128, parseU16, parseU256, parseU32, parseU64, parseU8, parseUsize };
|
package/dist/parser.mjs
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
class ParseError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "ParseError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
function assertInBounds(data, offset) {
|
|
8
|
+
if (offset >= data.length) {
|
|
9
|
+
throw new ParseError(
|
|
10
|
+
`Offset out of bounds. Data length ${data.length}, offset ${offset}`
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function parseBool(data, offset) {
|
|
15
|
+
assertInBounds(data, offset);
|
|
16
|
+
return { out: BigInt(data[offset]) > 0n, offset: offset + 1 };
|
|
17
|
+
}
|
|
18
|
+
function parseAsBigInt(data, offset) {
|
|
19
|
+
assertInBounds(data, offset);
|
|
20
|
+
return { out: BigInt(data[offset]), offset: offset + 1 };
|
|
21
|
+
}
|
|
22
|
+
const parseU8 = parseAsBigInt;
|
|
23
|
+
const parseU16 = parseAsBigInt;
|
|
24
|
+
const parseU32 = parseAsBigInt;
|
|
25
|
+
const parseU64 = parseAsBigInt;
|
|
26
|
+
const parseU128 = parseAsBigInt;
|
|
27
|
+
const parseUsize = parseAsBigInt;
|
|
28
|
+
function parseU256(data, offset) {
|
|
29
|
+
assertInBounds(data, offset + 1);
|
|
30
|
+
return {
|
|
31
|
+
out: BigInt(data[offset]) + (BigInt(data[offset + 1]) << 128n),
|
|
32
|
+
offset: offset + 2
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function parseAsHex(data, offset) {
|
|
36
|
+
assertInBounds(data, offset);
|
|
37
|
+
return {
|
|
38
|
+
out: String(data[offset]),
|
|
39
|
+
offset: offset + 1
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const parseContractAddress = parseAsHex;
|
|
43
|
+
const parseEthAddress = parseAsHex;
|
|
44
|
+
const parseStorageAddress = parseAsHex;
|
|
45
|
+
const parseClassHash = parseAsHex;
|
|
46
|
+
const parseBytes31 = parseAsHex;
|
|
47
|
+
function parseFelt252(data, offset) {
|
|
48
|
+
assertInBounds(data, offset);
|
|
49
|
+
return {
|
|
50
|
+
out: BigInt(data[offset]),
|
|
51
|
+
offset: offset + 1
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function parseEmpty(data, offset) {
|
|
55
|
+
return { out: null, offset };
|
|
56
|
+
}
|
|
57
|
+
function parseArray(type) {
|
|
58
|
+
return (data, startingOffset) => {
|
|
59
|
+
let offset = startingOffset;
|
|
60
|
+
const length = BigInt(data[offset]);
|
|
61
|
+
offset++;
|
|
62
|
+
const out = [];
|
|
63
|
+
for (let i = 0; i < length; i++) {
|
|
64
|
+
const { out: item, offset: newOffset } = type(data, offset);
|
|
65
|
+
out.push(item);
|
|
66
|
+
offset = newOffset;
|
|
67
|
+
}
|
|
68
|
+
return { out, offset };
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const parseSpan = parseArray;
|
|
72
|
+
function parseOption(type) {
|
|
73
|
+
return (data, offset) => {
|
|
74
|
+
const hasValue = BigInt(data[offset]) === 1n;
|
|
75
|
+
if (hasValue) {
|
|
76
|
+
return type(data, offset + 1);
|
|
77
|
+
}
|
|
78
|
+
return { out: null, offset: offset + 1 };
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function parseStruct(parsers) {
|
|
82
|
+
const sortedParsers = Object.entries(parsers).sort(
|
|
83
|
+
(a, b) => a[1].index - b[1].index
|
|
84
|
+
);
|
|
85
|
+
return (data, startingOffset) => {
|
|
86
|
+
let offset = startingOffset;
|
|
87
|
+
const out = {};
|
|
88
|
+
for (const [key, { parser }] of sortedParsers) {
|
|
89
|
+
const { out: value, offset: newOffset } = parser(data, offset);
|
|
90
|
+
out[key] = value;
|
|
91
|
+
offset = newOffset;
|
|
92
|
+
}
|
|
93
|
+
return { out, offset };
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function parseTuple(...parsers) {
|
|
97
|
+
return (data, startingOffset) => {
|
|
98
|
+
let offset = startingOffset;
|
|
99
|
+
const out = [];
|
|
100
|
+
for (const parser of parsers) {
|
|
101
|
+
const { out: value, offset: newOffset } = parser(data, offset);
|
|
102
|
+
out.push(value);
|
|
103
|
+
offset = newOffset;
|
|
104
|
+
}
|
|
105
|
+
return { out, offset };
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { ParseError, parseArray, parseAsBigInt, parseAsHex, parseBool, parseBytes31, parseClassHash, parseContractAddress, parseEmpty, parseEthAddress, parseFelt252, parseOption, parseSpan, parseStorageAddress, parseStruct, parseTuple, parseU128, parseU16, parseU256, parseU32, parseU64, parseU8, parseUsize };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as _effect_schema_AST from '@effect/schema/AST';
|
|
2
|
+
import { Schema } from '@effect/schema';
|
|
3
|
+
|
|
4
|
+
/** Wire representation of `FieldElement`. */
|
|
5
|
+
declare const FieldElementProto: Schema.Struct<{
|
|
6
|
+
x0: typeof Schema.BigIntFromSelf;
|
|
7
|
+
x1: typeof Schema.BigIntFromSelf;
|
|
8
|
+
x2: typeof Schema.BigIntFromSelf;
|
|
9
|
+
x3: typeof Schema.BigIntFromSelf;
|
|
10
|
+
}>;
|
|
11
|
+
/** Field element. */
|
|
12
|
+
declare const FieldElement: Schema.transform<Schema.Struct<{
|
|
13
|
+
x0: typeof Schema.BigIntFromSelf;
|
|
14
|
+
x1: typeof Schema.BigIntFromSelf;
|
|
15
|
+
x2: typeof Schema.BigIntFromSelf;
|
|
16
|
+
x3: typeof Schema.BigIntFromSelf;
|
|
17
|
+
}>, Schema.SchemaClass<`0x${string}`, `0x${string}`, never>>;
|
|
18
|
+
type FieldElement = Schema.Schema.Type<typeof FieldElement>;
|
|
19
|
+
declare const feltToProto: (a: `0x${string}`, overrideOptions?: _effect_schema_AST.ParseOptions) => {
|
|
20
|
+
readonly x0: bigint;
|
|
21
|
+
readonly x1: bigint;
|
|
22
|
+
readonly x2: bigint;
|
|
23
|
+
readonly x3: bigint;
|
|
24
|
+
};
|
|
25
|
+
declare const feltFromProto: (i: {
|
|
26
|
+
readonly x0: bigint;
|
|
27
|
+
readonly x1: bigint;
|
|
28
|
+
readonly x2: bigint;
|
|
29
|
+
readonly x3: bigint;
|
|
30
|
+
}, overrideOptions?: _effect_schema_AST.ParseOptions) => `0x${string}`;
|
|
31
|
+
|
|
32
|
+
export { FieldElement as F, FieldElementProto as a, feltFromProto as b, feltToProto as f };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as _effect_schema_AST from '@effect/schema/AST';
|
|
2
|
+
import { Schema } from '@effect/schema';
|
|
3
|
+
|
|
4
|
+
/** Wire representation of `FieldElement`. */
|
|
5
|
+
declare const FieldElementProto: Schema.Struct<{
|
|
6
|
+
x0: typeof Schema.BigIntFromSelf;
|
|
7
|
+
x1: typeof Schema.BigIntFromSelf;
|
|
8
|
+
x2: typeof Schema.BigIntFromSelf;
|
|
9
|
+
x3: typeof Schema.BigIntFromSelf;
|
|
10
|
+
}>;
|
|
11
|
+
/** Field element. */
|
|
12
|
+
declare const FieldElement: Schema.transform<Schema.Struct<{
|
|
13
|
+
x0: typeof Schema.BigIntFromSelf;
|
|
14
|
+
x1: typeof Schema.BigIntFromSelf;
|
|
15
|
+
x2: typeof Schema.BigIntFromSelf;
|
|
16
|
+
x3: typeof Schema.BigIntFromSelf;
|
|
17
|
+
}>, Schema.SchemaClass<`0x${string}`, `0x${string}`, never>>;
|
|
18
|
+
type FieldElement = Schema.Schema.Type<typeof FieldElement>;
|
|
19
|
+
declare const feltToProto: (a: `0x${string}`, overrideOptions?: _effect_schema_AST.ParseOptions) => {
|
|
20
|
+
readonly x0: bigint;
|
|
21
|
+
readonly x1: bigint;
|
|
22
|
+
readonly x2: bigint;
|
|
23
|
+
readonly x3: bigint;
|
|
24
|
+
};
|
|
25
|
+
declare const feltFromProto: (i: {
|
|
26
|
+
readonly x0: bigint;
|
|
27
|
+
readonly x1: bigint;
|
|
28
|
+
readonly x2: bigint;
|
|
29
|
+
readonly x3: bigint;
|
|
30
|
+
}, overrideOptions?: _effect_schema_AST.ParseOptions) => `0x${string}`;
|
|
31
|
+
|
|
32
|
+
export { FieldElement as F, FieldElementProto as a, feltFromProto as b, feltToProto as f };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as _effect_schema_AST from '@effect/schema/AST';
|
|
2
|
+
import { Schema } from '@effect/schema';
|
|
3
|
+
|
|
4
|
+
/** Wire representation of `FieldElement`. */
|
|
5
|
+
declare const FieldElementProto: Schema.Struct<{
|
|
6
|
+
x0: typeof Schema.BigIntFromSelf;
|
|
7
|
+
x1: typeof Schema.BigIntFromSelf;
|
|
8
|
+
x2: typeof Schema.BigIntFromSelf;
|
|
9
|
+
x3: typeof Schema.BigIntFromSelf;
|
|
10
|
+
}>;
|
|
11
|
+
/** Field element. */
|
|
12
|
+
declare const FieldElement: Schema.transform<Schema.Struct<{
|
|
13
|
+
x0: typeof Schema.BigIntFromSelf;
|
|
14
|
+
x1: typeof Schema.BigIntFromSelf;
|
|
15
|
+
x2: typeof Schema.BigIntFromSelf;
|
|
16
|
+
x3: typeof Schema.BigIntFromSelf;
|
|
17
|
+
}>, Schema.SchemaClass<`0x${string}`, `0x${string}`, never>>;
|
|
18
|
+
type FieldElement = Schema.Schema.Type<typeof FieldElement>;
|
|
19
|
+
declare const feltToProto: (a: `0x${string}`, overrideOptions?: _effect_schema_AST.ParseOptions) => {
|
|
20
|
+
readonly x0: bigint;
|
|
21
|
+
readonly x1: bigint;
|
|
22
|
+
readonly x2: bigint;
|
|
23
|
+
readonly x3: bigint;
|
|
24
|
+
};
|
|
25
|
+
declare const feltFromProto: (i: {
|
|
26
|
+
readonly x0: bigint;
|
|
27
|
+
readonly x1: bigint;
|
|
28
|
+
readonly x2: bigint;
|
|
29
|
+
readonly x3: bigint;
|
|
30
|
+
}, overrideOptions?: _effect_schema_AST.ParseOptions) => `0x${string}`;
|
|
31
|
+
|
|
32
|
+
export { FieldElement as F, FieldElementProto as a, feltFromProto as b, feltToProto as f };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apibara/starknet",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0-beta.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -15,6 +15,12 @@
|
|
|
15
15
|
"import": "./dist/index.mjs",
|
|
16
16
|
"require": "./dist/index.cjs",
|
|
17
17
|
"default": "./dist/index.mjs"
|
|
18
|
+
},
|
|
19
|
+
"./parser": {
|
|
20
|
+
"types": "./dist/parser.d.ts",
|
|
21
|
+
"import": "./dist/parser.mjs",
|
|
22
|
+
"require": "./dist/parser.cjs",
|
|
23
|
+
"default": "./dist/parser.mjs"
|
|
18
24
|
}
|
|
19
25
|
},
|
|
20
26
|
"scripts": {
|
|
@@ -36,8 +42,10 @@
|
|
|
36
42
|
"vitest": "^1.6.0"
|
|
37
43
|
},
|
|
38
44
|
"dependencies": {
|
|
39
|
-
"@apibara/protocol": "2.
|
|
45
|
+
"@apibara/protocol": "2.1.0-beta.2",
|
|
40
46
|
"@effect/schema": "^0.67.15",
|
|
47
|
+
"@scure/starknet": "^1.1.0",
|
|
48
|
+
"abi-wan-kanabi": "^2.2.4",
|
|
41
49
|
"effect": "^3.2.6",
|
|
42
50
|
"long": "^5.2.1",
|
|
43
51
|
"nice-grpc-common": "^2.0.2",
|
package/src/abi.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { keccak } from "@scure/starknet";
|
|
2
|
+
|
|
3
|
+
import type { FieldElement } from "./common";
|
|
4
|
+
import {
|
|
5
|
+
parseBool,
|
|
6
|
+
parseContractAddress,
|
|
7
|
+
parseFelt252,
|
|
8
|
+
parseU8,
|
|
9
|
+
parseU16,
|
|
10
|
+
parseU32,
|
|
11
|
+
parseU64,
|
|
12
|
+
parseU128,
|
|
13
|
+
parseU256,
|
|
14
|
+
} from "./parser";
|
|
15
|
+
|
|
16
|
+
/** Returns the selector of the provided `name` as a bigint. */
|
|
17
|
+
export function getBigIntSelector(name: string): bigint {
|
|
18
|
+
const asBytes = new TextEncoder().encode(name);
|
|
19
|
+
return keccak(asBytes);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Returns the selector of the provided `name` as a FieldElement. */
|
|
23
|
+
export function getSelector(name: string): FieldElement {
|
|
24
|
+
const bn = getBigIntSelector(name);
|
|
25
|
+
return `0x${bn.toString(16).padStart(64, "0")}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Returns the selector of the provided event with `name` as a FieldElement.
|
|
29
|
+
*
|
|
30
|
+
* If the name is fully qualified, only the last part is used to compute the selector.
|
|
31
|
+
*/
|
|
32
|
+
export function getEventSelector(name: string): FieldElement {
|
|
33
|
+
const parts = name.split("::");
|
|
34
|
+
return getSelector(parts[parts.length - 1]);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const PrimitiveTypeParsers = {
|
|
38
|
+
"core::bool": parseBool,
|
|
39
|
+
"core::felt252": parseFelt252,
|
|
40
|
+
"core::integer::u8": parseU8,
|
|
41
|
+
"core::integer::u16": parseU16,
|
|
42
|
+
"core::integer::u32": parseU32,
|
|
43
|
+
"core::integer::u64": parseU64,
|
|
44
|
+
"core::integer::u128": parseU128,
|
|
45
|
+
"core::integer::u256": parseU256,
|
|
46
|
+
"core::starknet::contract_address::ContractAddress": parseContractAddress,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export function isPrimitiveType(type: string) {
|
|
50
|
+
return type in PrimitiveTypeParsers;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function isArrayType(type: string) {
|
|
54
|
+
return type.startsWith("core::array::Array::<") && type.endsWith(">");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function getArrayElementType(type: string) {
|
|
58
|
+
return type.slice("core::array::Array::<".length, -1);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function isSpanType(type: string) {
|
|
62
|
+
return type.startsWith("core::array::Span::<") && type.endsWith(">");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function getSpanType(type: string) {
|
|
66
|
+
return type.slice("core::array::Span::<".length, -1);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function isOptionType(type: string) {
|
|
70
|
+
return type.startsWith("core::option::Option::<") && type.endsWith(">");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function getOptionType(type: string) {
|
|
74
|
+
return type.slice("core::option::Option::<".length, -1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function isEmptyType(type: string) {
|
|
78
|
+
return type === "()";
|
|
79
|
+
}
|
package/src/access.ts
CHANGED
|
@@ -3,8 +3,11 @@ import type { Transaction, TransactionReceipt } from "./block";
|
|
|
3
3
|
/** Returns the transaction receipt for the given transaction index. */
|
|
4
4
|
export function getReceipt(
|
|
5
5
|
transactionIndex: number,
|
|
6
|
-
|
|
6
|
+
params:
|
|
7
|
+
| { receipts: readonly TransactionReceipt[] }
|
|
8
|
+
| readonly TransactionReceipt[],
|
|
7
9
|
): TransactionReceipt | undefined {
|
|
10
|
+
const receipts = "receipts" in params ? params.receipts : params;
|
|
8
11
|
return binarySearch(
|
|
9
12
|
transactionIndex,
|
|
10
13
|
receipts,
|
|
@@ -15,8 +18,9 @@ export function getReceipt(
|
|
|
15
18
|
/** Returns the transaction for the given transaction index. */
|
|
16
19
|
export function getTransaction(
|
|
17
20
|
transactionIndex: number,
|
|
18
|
-
transactions: readonly Transaction[],
|
|
21
|
+
params: { transactions: readonly Transaction[] } | readonly Transaction[],
|
|
19
22
|
): Transaction | undefined {
|
|
23
|
+
const transactions = "transactions" in params ? params.transactions : params;
|
|
20
24
|
return binarySearch(
|
|
21
25
|
transactionIndex,
|
|
22
26
|
transactions,
|