@apibara/starknet 0.3.0 → 0.3.1
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/cursor.d.ts +2 -2
- package/dist/cursor.js +1 -1
- package/dist/cursor.js.map +1 -1
- package/dist/felt.d.ts +1 -1
- package/dist/felt.js +5 -5
- package/dist/felt.js.map +1 -1
- package/dist/felt.test.js +9 -9
- package/dist/felt.test.js.map +1 -1
- package/dist/filter.d.ts +13 -1
- package/dist/filter.js +22 -1
- package/dist/filter.js.map +1 -1
- package/dist/filter.test.js +46 -40
- package/dist/filter.test.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js.map +1 -1
- package/dist/proto/filter.proto +6 -0
- package/dist/proto/generated.d.ts +27 -0
- package/dist/proto/generated.js +111 -0
- package/dist/proto/generated.js.map +1 -1
- package/dist/proto/index.d.ts +1 -1
- package/dist/proto/index.js.map +1 -1
- package/package.json +4 -8
- package/src/cursor.ts +14 -8
- package/src/felt.test.ts +21 -19
- package/src/felt.ts +22 -22
- package/src/filter.test.ts +129 -107
- package/src/filter.ts +251 -174
- package/src/index.ts +4 -4
- package/src/proto/filter.proto +6 -0
- package/src/proto/generated.d.ts +27 -0
- package/src/proto/generated.js +118 -0
- package/src/proto/index.ts +2 -2
package/dist/proto/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { apibara } from
|
|
1
|
+
import { apibara } from "./generated";
|
|
2
2
|
export import v1alpha2 = apibara.starknet.v1alpha2;
|
package/dist/proto/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/proto/index.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/proto/index.ts"],"names":[],"mappings":";;;AAAA,2CAAsC;AAEtC,6DAA6D;AAC/C,QAAA,QAAQ,GAAG,mBAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apibara/starknet",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,17 +19,13 @@
|
|
|
19
19
|
"@types/node": "^18.11.9",
|
|
20
20
|
"protobufjs-cli": "^1.0.2"
|
|
21
21
|
},
|
|
22
|
-
"eslintConfig": {
|
|
23
|
-
"ignorePatterns": [
|
|
24
|
-
"src/proto/generated.d.ts"
|
|
25
|
-
]
|
|
26
|
-
},
|
|
27
22
|
"scripts": {
|
|
28
23
|
"prebuild": "pnpm run protobuf && pnpm run gen-proto",
|
|
29
24
|
"build": "tsc && cp src/proto/generated.d.ts dist/proto/generated.d.ts",
|
|
30
25
|
"test": "jest src",
|
|
31
|
-
"lint": "
|
|
32
|
-
"lint:fix": "
|
|
26
|
+
"lint": "biome check .",
|
|
27
|
+
"lint:fix": "pnpm lint --apply",
|
|
28
|
+
"format": "biome format . --write",
|
|
33
29
|
"protobuf": "mkdir -p dist/proto && cp src/proto/*.proto dist/proto",
|
|
34
30
|
"gen-module": "pbjs -t static-module -w commonjs -o src/proto/generated.js src/proto/*.proto",
|
|
35
31
|
"gen-types": "pbts -o src/proto/generated.d.ts src/proto/generated.js",
|
package/src/cursor.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Long from
|
|
2
|
-
import { FieldElement } from
|
|
3
|
-
import { v1alpha2 } from
|
|
1
|
+
import Long from "long";
|
|
2
|
+
import { FieldElement } from "./felt";
|
|
3
|
+
import { v1alpha2 } from "./proto";
|
|
4
4
|
|
|
5
5
|
export const StarkNetCursor = {
|
|
6
6
|
/**
|
|
@@ -10,17 +10,23 @@ export const StarkNetCursor = {
|
|
|
10
10
|
return {
|
|
11
11
|
orderKey: Long.fromValue(number),
|
|
12
12
|
uniqueKey: new Uint8Array(),
|
|
13
|
-
}
|
|
13
|
+
};
|
|
14
14
|
},
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Creates a cursor pointing at the block with the given height and hash.
|
|
18
18
|
*/
|
|
19
|
-
createWithBlockNumberAndHash: (
|
|
20
|
-
|
|
19
|
+
createWithBlockNumberAndHash: (
|
|
20
|
+
number: string | number | Long,
|
|
21
|
+
hash: v1alpha2.IFieldElement,
|
|
22
|
+
) => {
|
|
23
|
+
const uniqueKey = Buffer.from(
|
|
24
|
+
FieldElement.toHex(hash).replace("0x", ""),
|
|
25
|
+
"hex",
|
|
26
|
+
);
|
|
21
27
|
return {
|
|
22
28
|
orderKey: Long.fromValue(number),
|
|
23
29
|
uniqueKey,
|
|
24
|
-
}
|
|
30
|
+
};
|
|
25
31
|
},
|
|
26
|
-
}
|
|
32
|
+
};
|
package/src/felt.test.ts
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
import { FieldElement } from
|
|
1
|
+
import { FieldElement } from "./felt";
|
|
2
2
|
|
|
3
|
-
describe(
|
|
4
|
-
describe(
|
|
5
|
-
it(
|
|
6
|
-
const original = BigInt(
|
|
3
|
+
describe("FieldElement", () => {
|
|
4
|
+
describe("encoding", () => {
|
|
5
|
+
it("can be encoded to and from the wire format", () => {
|
|
6
|
+
const original = BigInt(
|
|
7
|
+
"0x00da114221cb83fa859dbdb4c44beeaa0bb37c7537ad5ae66fe5e0efd20e6eb3",
|
|
8
|
+
);
|
|
7
9
|
|
|
8
|
-
const encoded = FieldElement.fromBigInt(original)
|
|
10
|
+
const encoded = FieldElement.fromBigInt(original);
|
|
9
11
|
|
|
10
|
-
const back = FieldElement.toBigInt(encoded)
|
|
12
|
+
const back = FieldElement.toBigInt(encoded);
|
|
11
13
|
|
|
12
|
-
expect(back).toBe(original)
|
|
13
|
-
})
|
|
14
|
+
expect(back).toBe(original);
|
|
15
|
+
});
|
|
14
16
|
|
|
15
|
-
it(
|
|
16
|
-
const prime = 2n ** 251n + 17n * 2n ** 192n
|
|
17
|
-
const encoded = FieldElement.fromBigInt(prime)
|
|
18
|
-
expect(encoded.hiHi?.toString()).toEqual(
|
|
19
|
-
expect(encoded.hiLo?.toString()).toEqual(
|
|
20
|
-
expect(encoded.loHi?.toString()).toEqual(
|
|
21
|
-
expect(encoded.loLo?.toString()).toEqual(
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
})
|
|
17
|
+
it("encodes the value as big endian", () => {
|
|
18
|
+
const prime = 2n ** 251n + 17n * 2n ** 192n;
|
|
19
|
+
const encoded = FieldElement.fromBigInt(prime);
|
|
20
|
+
expect(encoded.hiHi?.toString()).toEqual("0");
|
|
21
|
+
expect(encoded.hiLo?.toString()).toEqual("0");
|
|
22
|
+
expect(encoded.loHi?.toString()).toEqual("0");
|
|
23
|
+
expect(encoded.loLo?.toString()).toEqual("576460752303423505");
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
});
|
package/src/felt.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import Long from
|
|
2
|
-
import { v1alpha2 } from
|
|
1
|
+
import Long from "long";
|
|
2
|
+
import { v1alpha2 } from "./proto";
|
|
3
3
|
|
|
4
|
-
const MAX_FELT = 2n ** 251n + 17n * 2n ** 192n
|
|
4
|
+
const MAX_FELT = 2n ** 251n + 17n * 2n ** 192n;
|
|
5
5
|
|
|
6
6
|
export const FieldElement = {
|
|
7
7
|
encode: v1alpha2.FieldElement.encode,
|
|
@@ -13,11 +13,11 @@ export const FieldElement = {
|
|
|
13
13
|
* Converts from the wire representation to a bigint.
|
|
14
14
|
*/
|
|
15
15
|
toBigInt(message: v1alpha2.IFieldElement): bigint {
|
|
16
|
-
const hiHi = hexEncodedU64(message.hiHi)
|
|
17
|
-
const hiLo = hexEncodedU64(message.hiLo)
|
|
18
|
-
const loHi = hexEncodedU64(message.loHi)
|
|
19
|
-
const loLo = hexEncodedU64(message.loLo)
|
|
20
|
-
return BigInt(`0x${loLo}${loHi}${hiLo}${hiHi}`)
|
|
16
|
+
const hiHi = hexEncodedU64(message.hiHi);
|
|
17
|
+
const hiLo = hexEncodedU64(message.hiLo);
|
|
18
|
+
const loHi = hexEncodedU64(message.loHi);
|
|
19
|
+
const loLo = hexEncodedU64(message.loLo);
|
|
20
|
+
return BigInt(`0x${loLo}${loHi}${hiLo}${hiHi}`);
|
|
21
21
|
},
|
|
22
22
|
|
|
23
23
|
/**
|
|
@@ -25,37 +25,37 @@ export const FieldElement = {
|
|
|
25
25
|
*/
|
|
26
26
|
fromBigInt(number: string | number | bigint): v1alpha2.IFieldElement {
|
|
27
27
|
if (number < 0 || number > MAX_FELT) {
|
|
28
|
-
throw new Error(
|
|
28
|
+
throw new Error("FieldElement outside of range");
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
const bn = BigInt(number)
|
|
31
|
+
const bn = BigInt(number);
|
|
32
32
|
// bit-shifting of a big int doesn't make much sense.
|
|
33
33
|
// convert to hex and from there breakup in pieces
|
|
34
|
-
const hex = bn.toString(16).padStart(64,
|
|
35
|
-
const s = hex.length
|
|
36
|
-
const hiHi = Long.fromString(hex.slice(s - 16, s), true, 16)
|
|
37
|
-
const hiLo = Long.fromString(hex.slice(s - 32, s - 16), true, 16)
|
|
38
|
-
const loHi = Long.fromString(hex.slice(s - 48, s - 32), true, 16)
|
|
39
|
-
const loLo = Long.fromString(hex.slice(s - 64, s - 48), true, 16)
|
|
34
|
+
const hex = bn.toString(16).padStart(64, "0");
|
|
35
|
+
const s = hex.length;
|
|
36
|
+
const hiHi = Long.fromString(hex.slice(s - 16, s), true, 16);
|
|
37
|
+
const hiLo = Long.fromString(hex.slice(s - 32, s - 16), true, 16);
|
|
38
|
+
const loHi = Long.fromString(hex.slice(s - 48, s - 32), true, 16);
|
|
39
|
+
const loLo = Long.fromString(hex.slice(s - 64, s - 48), true, 16);
|
|
40
40
|
|
|
41
41
|
return {
|
|
42
42
|
hiHi,
|
|
43
43
|
hiLo,
|
|
44
44
|
loHi,
|
|
45
45
|
loLo,
|
|
46
|
-
}
|
|
46
|
+
};
|
|
47
47
|
},
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
* Returns the hex value of the field element.
|
|
51
51
|
*/
|
|
52
52
|
toHex(message: v1alpha2.IFieldElement): string {
|
|
53
|
-
const num = this.toBigInt(message)
|
|
54
|
-
return `0x${num.toString(16).padStart(64,
|
|
53
|
+
const num = this.toBigInt(message);
|
|
54
|
+
return `0x${num.toString(16).padStart(64, "0")}`;
|
|
55
55
|
},
|
|
56
|
-
}
|
|
56
|
+
};
|
|
57
57
|
|
|
58
58
|
function hexEncodedU64(n: Long | number | null | undefined): string {
|
|
59
|
-
const s = n?.toString(16) ??
|
|
60
|
-
return s.padStart(16,
|
|
59
|
+
const s = n?.toString(16) ?? "";
|
|
60
|
+
return s.padStart(16, "0");
|
|
61
61
|
}
|
package/src/filter.test.ts
CHANGED
|
@@ -1,117 +1,139 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
describe(
|
|
5
|
-
describe(
|
|
6
|
-
it(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
expect(filter.header).toEqual({})
|
|
10
|
-
})
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
describe(
|
|
14
|
-
it(
|
|
15
|
-
|
|
16
|
-
.addTransaction(
|
|
1
|
+
import { FieldElement } from "./felt";
|
|
2
|
+
import { Filter } from "./filter";
|
|
3
|
+
|
|
4
|
+
describe("Filter", () => {
|
|
5
|
+
describe("addHeader", () => {
|
|
6
|
+
it("sets the header field", () => {
|
|
7
|
+
const filter = Filter.create().withHeader().toObject();
|
|
8
|
+
|
|
9
|
+
expect(filter.header).toEqual({});
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe("addTransaction", () => {
|
|
14
|
+
it("accepts a builder or callback function", () => {
|
|
15
|
+
const filter = Filter.create()
|
|
16
|
+
.addTransaction(
|
|
17
|
+
Filter.transaction()
|
|
18
|
+
.declare()
|
|
19
|
+
.withClassHash(FieldElement.fromBigInt(123)),
|
|
20
|
+
)
|
|
17
21
|
.addTransaction((tx) =>
|
|
18
|
-
tx
|
|
22
|
+
tx
|
|
23
|
+
.deployAccount()
|
|
24
|
+
.withConstructorCalldata([FieldElement.fromBigInt(0)]),
|
|
25
|
+
)
|
|
26
|
+
.toObject();
|
|
27
|
+
|
|
28
|
+
expect(filter.transactions).toHaveLength(2);
|
|
29
|
+
expect(filter.transactions?.[0].declare).toBeDefined();
|
|
30
|
+
expect(filter.transactions?.[1].deployAccount).toBeDefined();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("addEvent", () => {
|
|
35
|
+
it("accepts a builder or callback function", () => {
|
|
36
|
+
const filter = Filter.create()
|
|
37
|
+
.addEvent(
|
|
38
|
+
Filter.event().withFromAddress(FieldElement.fromBigInt("0x0")),
|
|
19
39
|
)
|
|
20
|
-
.toObject()
|
|
21
|
-
|
|
22
|
-
expect(filter.transactions).toHaveLength(2)
|
|
23
|
-
expect(filter.transactions?.[0].declare).toBeDefined()
|
|
24
|
-
expect(filter.transactions?.[1].deployAccount).toBeDefined()
|
|
25
|
-
})
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
describe('addEvent', () => {
|
|
29
|
-
it('accepts a builder or callback function', () => {
|
|
30
|
-
let filter = Filter.create()
|
|
31
|
-
.addEvent(Filter.event().withFromAddress(FieldElement.fromBigInt('0x0')))
|
|
32
40
|
.addEvent((ev) => ev.withData([FieldElement.fromBigInt(0)]))
|
|
33
|
-
.toObject()
|
|
41
|
+
.toObject();
|
|
34
42
|
|
|
35
|
-
expect(filter.events).toHaveLength(2)
|
|
36
|
-
})
|
|
37
|
-
})
|
|
43
|
+
expect(filter.events).toHaveLength(2);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
38
46
|
|
|
39
|
-
describe(
|
|
40
|
-
it(
|
|
41
|
-
|
|
47
|
+
describe("addMessage", () => {
|
|
48
|
+
it("accepts a builder or callback function", () => {
|
|
49
|
+
const filter = Filter.create()
|
|
42
50
|
.addMessage(Filter.message().withToAddress(FieldElement.fromBigInt(0)))
|
|
43
51
|
.addMessage((msg) => msg.withPayload([FieldElement.fromBigInt(0)]))
|
|
44
|
-
.toObject()
|
|
52
|
+
.toObject();
|
|
45
53
|
|
|
46
|
-
expect(filter.messages).toHaveLength(2)
|
|
47
|
-
})
|
|
48
|
-
})
|
|
54
|
+
expect(filter.messages).toHaveLength(2);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
49
57
|
|
|
50
|
-
describe(
|
|
51
|
-
it(
|
|
52
|
-
|
|
58
|
+
describe("withStateUpdate", () => {
|
|
59
|
+
it("accepts a callback function", () => {
|
|
60
|
+
const filter = Filter.create()
|
|
53
61
|
.withStateUpdate((s) => s)
|
|
54
|
-
.toObject()
|
|
55
|
-
expect(filter.stateUpdate).toBeDefined()
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
})
|
|
62
|
+
.toObject();
|
|
63
|
+
expect(filter.stateUpdate).toBeDefined();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("accepts a builder", () => {
|
|
67
|
+
const filter = Filter.create()
|
|
68
|
+
.withStateUpdate(Filter.stateUpdate())
|
|
69
|
+
.toObject();
|
|
70
|
+
expect(filter.stateUpdate).toBeDefined();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("addStorageDiff", () => {
|
|
74
|
+
it("accepts a callback function", () => {
|
|
75
|
+
const filter = Filter.create()
|
|
76
|
+
.withStateUpdate((s) =>
|
|
77
|
+
s.addStorageDiff((d) => d).addStorageDiff((d) => d),
|
|
78
|
+
)
|
|
79
|
+
.toObject();
|
|
80
|
+
expect(filter.stateUpdate?.storageDiffs).toHaveLength(2);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("addDeclaredContract", () => {
|
|
85
|
+
it("accepts a callback function", () => {
|
|
86
|
+
const filter = Filter.create()
|
|
87
|
+
.withStateUpdate((s) =>
|
|
88
|
+
s.addDeclaredContract((d) => d).addDeclaredContract((d) => d),
|
|
89
|
+
)
|
|
90
|
+
.toObject();
|
|
91
|
+
expect(filter.stateUpdate?.declaredContracts).toHaveLength(2);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("addDeployedContract", () => {
|
|
96
|
+
it("accepts a callback function", () => {
|
|
97
|
+
const filter = Filter.create()
|
|
98
|
+
.withStateUpdate((s) =>
|
|
99
|
+
s.addDeployedContract((d) => d).addDeployedContract((d) => d),
|
|
100
|
+
)
|
|
101
|
+
.toObject();
|
|
102
|
+
expect(filter.stateUpdate?.deployedContracts).toHaveLength(2);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe("addDeclaredClass", () => {
|
|
107
|
+
it("accepts a callback function", () => {
|
|
108
|
+
const filter = Filter.create()
|
|
109
|
+
.withStateUpdate((s) =>
|
|
110
|
+
s.addDeclaredClass((d) => d).addDeclaredClass((d) => d),
|
|
111
|
+
)
|
|
112
|
+
.toObject();
|
|
113
|
+
expect(filter.stateUpdate?.declaredClasses).toHaveLength(2);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("addReplacedClass", () => {
|
|
118
|
+
it("accepts a callback function", () => {
|
|
119
|
+
const filter = Filter.create()
|
|
120
|
+
.withStateUpdate((s) =>
|
|
121
|
+
s.addReplacedClass((d) => d).addReplacedClass((d) => d),
|
|
122
|
+
)
|
|
123
|
+
.toObject();
|
|
124
|
+
expect(filter.stateUpdate?.replacedClasses).toHaveLength(2);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("addNonceUpdated", () => {
|
|
129
|
+
it("accepts a callback function", () => {
|
|
130
|
+
const filter = Filter.create()
|
|
131
|
+
.withStateUpdate((s) =>
|
|
132
|
+
s.addNonceUpdate((d) => d).addNonceUpdate((d) => d),
|
|
133
|
+
)
|
|
134
|
+
.toObject();
|
|
135
|
+
expect(filter.stateUpdate?.nonces).toHaveLength(2);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|