@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.
@@ -1,2 +1,2 @@
1
- import { apibara } from './generated';
1
+ import { apibara } from "./generated";
2
2
  export import v1alpha2 = apibara.starknet.v1alpha2;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/proto/index.ts"],"names":[],"mappings":";;;AAAA,2CAAqC;AAErC,6DAA6D;AAC/C,QAAA,QAAQ,GAAG,mBAAO,CAAC,QAAQ,CAAC,QAAQ,CAAA"}
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.0",
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": "eslint src --ext .ts",
32
- "lint:fix": "eslint --fix src --ext .ts",
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 'long'
2
- import { FieldElement } from './felt'
3
- import { v1alpha2 } from './proto'
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: (number: string | number | Long, hash: v1alpha2.IFieldElement) => {
20
- const uniqueKey = Buffer.from(FieldElement.toHex(hash).replace('0x', ''), 'hex')
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 './felt'
1
+ import { FieldElement } from "./felt";
2
2
 
3
- describe('FieldElement', () => {
4
- describe('encoding', () => {
5
- it('can be encoded to and from the wire format', () => {
6
- const original = BigInt('0x00da114221cb83fa859dbdb4c44beeaa0bb37c7537ad5ae66fe5e0efd20e6eb3')
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('encodes the value as big endian', () => {
16
- const prime = 2n ** 251n + 17n * 2n ** 192n
17
- const encoded = FieldElement.fromBigInt(prime)
18
- expect(encoded.hiHi?.toString()).toEqual('0')
19
- expect(encoded.hiLo?.toString()).toEqual('0')
20
- expect(encoded.loHi?.toString()).toEqual('0')
21
- expect(encoded.loLo?.toString()).toEqual('576460752303423505')
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 'long'
2
- import { v1alpha2 } from './proto'
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('FieldElement outside of range')
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, '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)
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, '0')}`
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, '0')
59
+ const s = n?.toString(16) ?? "";
60
+ return s.padStart(16, "0");
61
61
  }
@@ -1,117 +1,139 @@
1
- import { Filter } from './filter'
2
- import { FieldElement } from './felt'
3
-
4
- describe('Filter', () => {
5
- describe('addHeader', () => {
6
- it('sets the header field', () => {
7
- let 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
- let filter = Filter.create()
16
- .addTransaction(Filter.transaction().declare().withClassHash(FieldElement.fromBigInt(123)))
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.deployAccount().withConstructorCalldata([FieldElement.fromBigInt(0)])
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('addMessage', () => {
40
- it('accepts a builder or callback function', () => {
41
- let filter = Filter.create()
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('withStateUpdate', () => {
51
- it('accepts a callback function', () => {
52
- let filter = Filter.create()
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('accepts a builder', () => {
59
- let filter = Filter.create().withStateUpdate(Filter.stateUpdate()).toObject()
60
- expect(filter.stateUpdate).toBeDefined()
61
- })
62
-
63
- describe('addStorageDiff', () => {
64
- it('accepts a callback function', () => {
65
- let filter = Filter.create()
66
- .withStateUpdate((s) => s.addStorageDiff((d) => d).addStorageDiff((d) => d))
67
- .toObject()
68
- expect(filter.stateUpdate?.storageDiffs).toHaveLength(2)
69
- })
70
- })
71
-
72
- describe('addDeclaredContract', () => {
73
- it('accepts a callback function', () => {
74
- let filter = Filter.create()
75
- .withStateUpdate((s) => s.addDeclaredContract((d) => d).addDeclaredContract((d) => d))
76
- .toObject()
77
- expect(filter.stateUpdate?.declaredContracts).toHaveLength(2)
78
- })
79
- })
80
-
81
- describe('addDeployedContract', () => {
82
- it('accepts a callback function', () => {
83
- let filter = Filter.create()
84
- .withStateUpdate((s) => s.addDeployedContract((d) => d).addDeployedContract((d) => d))
85
- .toObject()
86
- expect(filter.stateUpdate?.deployedContracts).toHaveLength(2)
87
- })
88
- })
89
-
90
- describe('addDeclaredClass', () => {
91
- it('accepts a callback function', () => {
92
- let filter = Filter.create()
93
- .withStateUpdate((s) => s.addDeclaredClass((d) => d).addDeclaredClass((d) => d))
94
- .toObject()
95
- expect(filter.stateUpdate?.declaredClasses).toHaveLength(2)
96
- })
97
- })
98
-
99
- describe('addReplacedClass', () => {
100
- it('accepts a callback function', () => {
101
- let filter = Filter.create()
102
- .withStateUpdate((s) => s.addReplacedClass((d) => d).addReplacedClass((d) => d))
103
- .toObject()
104
- expect(filter.stateUpdate?.replacedClasses).toHaveLength(2)
105
- })
106
- })
107
-
108
- describe('addNonceUpdated', () => {
109
- it('accepts a callback function', () => {
110
- let filter = Filter.create()
111
- .withStateUpdate((s) => s.addNonceUpdate((d) => d).addNonceUpdate((d) => d))
112
- .toObject()
113
- expect(filter.stateUpdate?.nonces).toHaveLength(2)
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
+ });