@effect-blockchain/starknet 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +12 -2
- package/dist/StarknetAddress.d.ts +29 -0
- package/dist/StarknetAddress.d.ts.map +1 -0
- package/dist/StarknetAddress.js +29 -0
- package/dist/StarknetAddress.js.map +1 -0
- package/dist/StarknetBlock.d.ts +49 -0
- package/dist/StarknetBlock.d.ts.map +1 -0
- package/dist/StarknetBlock.js +52 -0
- package/dist/StarknetBlock.js.map +1 -0
- package/dist/StarknetEvent.d.ts +34 -0
- package/dist/StarknetEvent.d.ts.map +1 -0
- package/dist/StarknetEvent.js +39 -0
- package/dist/StarknetEvent.js.map +1 -0
- package/dist/StarknetIndexer.d.ts +157 -0
- package/dist/StarknetIndexer.d.ts.map +1 -0
- package/dist/StarknetIndexer.js +245 -0
- package/dist/StarknetIndexer.js.map +1 -0
- package/dist/StarknetNetwork.d.ts +72 -0
- package/dist/StarknetNetwork.d.ts.map +1 -0
- package/dist/StarknetNetwork.js +83 -0
- package/dist/StarknetNetwork.js.map +1 -0
- package/dist/StarknetRelayer.d.ts +273 -0
- package/dist/StarknetRelayer.d.ts.map +1 -0
- package/dist/StarknetRelayer.js +352 -0
- package/dist/StarknetRelayer.js.map +1 -0
- package/dist/StarknetRpc.d.ts +306 -0
- package/dist/StarknetRpc.d.ts.map +1 -0
- package/dist/StarknetRpc.js +281 -0
- package/dist/StarknetRpc.js.map +1 -0
- package/dist/StarknetSigner.d.ts +75 -0
- package/dist/StarknetSigner.d.ts.map +1 -0
- package/dist/StarknetSigner.js +84 -0
- package/dist/StarknetSigner.js.map +1 -0
- package/dist/StarknetTransaction.d.ts +74 -0
- package/dist/StarknetTransaction.d.ts.map +1 -0
- package/dist/StarknetTransaction.js +2 -0
- package/dist/StarknetTransaction.js.map +1 -0
- package/dist/StarknetTransactionHash.d.ts +22 -0
- package/dist/StarknetTransactionHash.d.ts.map +1 -0
- package/dist/StarknetTransactionHash.js +22 -0
- package/dist/StarknetTransactionHash.js.map +1 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/PaddedFelt.d.ts +2 -0
- package/dist/internal/PaddedFelt.d.ts.map +1 -0
- package/dist/internal/PaddedFelt.js +15 -0
- package/dist/internal/PaddedFelt.js.map +1 -0
- package/dist/internal/confirmations.d.ts +2 -0
- package/dist/internal/confirmations.d.ts.map +1 -0
- package/dist/internal/confirmations.js +13 -0
- package/dist/internal/confirmations.js.map +1 -0
- package/dist/internal/rpcMethods.d.ts +2 -0
- package/dist/internal/rpcMethods.d.ts.map +1 -0
- package/dist/internal/rpcMethods.js +225 -0
- package/dist/internal/rpcMethods.js.map +1 -0
- package/dist/internal/transactionHash.d.ts +2 -0
- package/dist/internal/transactionHash.d.ts.map +1 -0
- package/dist/internal/transactionHash.js +60 -0
- package/dist/internal/transactionHash.js.map +1 -0
- package/package.json +66 -3
- package/src/StarknetAddress.ts +42 -0
- package/src/StarknetBlock.ts +54 -0
- package/src/StarknetEvent.ts +45 -0
- package/src/StarknetIndexer.ts +363 -0
- package/src/StarknetNetwork.ts +107 -0
- package/src/StarknetRelayer.ts +437 -0
- package/src/StarknetRpc.ts +524 -0
- package/src/StarknetSigner.ts +115 -0
- package/src/StarknetTransaction.ts +68 -0
- package/src/StarknetTransactionHash.ts +31 -0
- package/src/index.ts +55 -0
- package/src/internal/PaddedFelt.ts +18 -0
- package/src/internal/confirmations.ts +17 -0
- package/src/internal/rpcMethods.ts +224 -0
- package/src/internal/transactionHash.ts +64 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starknet event indexing on top of {@link StarknetRpc}: the last confirmed block, the events of a contract over a
|
|
3
|
+
* block range, and a stream of them following the confirmed head.
|
|
4
|
+
*
|
|
5
|
+
* A range is split into ranges of at most `maxBlockRange` blocks, each paged with `starknet_getEvents` (`chunkSize`
|
|
6
|
+
* events per page) until its continuation token is absent. A cursor page is pinned to the endpoint that issued it, so
|
|
7
|
+
* when it fails the range restarts once from its first page, through the fallback again. Any other failure fails the whole call and drops the
|
|
8
|
+
* events already read: the caller retries the same range.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import type { StarknetIndexerFilter } from "@effect-blockchain/starknet/StarknetIndexer"
|
|
13
|
+
* import { StarknetIndexer } from "@effect-blockchain/starknet/StarknetIndexer"
|
|
14
|
+
* import * as Effect from "effect/Effect"
|
|
15
|
+
* import * as Schedule from "effect/Schedule"
|
|
16
|
+
* import * as Stream from "effect/Stream"
|
|
17
|
+
*
|
|
18
|
+
* declare const filter: Omit<StarknetIndexerFilter, "fromBlock" | "toBlock">
|
|
19
|
+
* declare const cursor: bigint
|
|
20
|
+
* declare const save: (block: bigint) => Effect.Effect<void>
|
|
21
|
+
*
|
|
22
|
+
* const program = Effect.gen(function*() {
|
|
23
|
+
* const indexer = yield* StarknetIndexer
|
|
24
|
+
* // once caught up, read the head after 1 s, 2 s, 4 s... at most every minute
|
|
25
|
+
* const poll = Schedule.min([Schedule.exponential("1 second"), Schedule.spaced("1 minute")])
|
|
26
|
+
* yield* indexer.streamEvents({ filter, fromBlock: cursor + BigInt(1), poll }).pipe(
|
|
27
|
+
* Stream.runForEach((batch) => save(batch.toBlock))
|
|
28
|
+
* )
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* The STRK balance of an address, in fri, is read at one block or sampled every `step` blocks:
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import type { StarknetAddress } from "@effect-blockchain/starknet/StarknetAddress"
|
|
37
|
+
* import { StarknetIndexer } from "@effect-blockchain/starknet/StarknetIndexer"
|
|
38
|
+
* import * as Effect from "effect/Effect"
|
|
39
|
+
* import * as Stream from "effect/Stream"
|
|
40
|
+
*
|
|
41
|
+
* declare const address: StarknetAddress
|
|
42
|
+
*
|
|
43
|
+
* // the balance every 1000 blocks from block 1000000 to 1010000
|
|
44
|
+
* const balances = Effect.gen(function*() {
|
|
45
|
+
* const indexer = yield* StarknetIndexer
|
|
46
|
+
* return yield* indexer.streamNativeBalance({
|
|
47
|
+
* address,
|
|
48
|
+
* fromBlock: BigInt(1_000_000),
|
|
49
|
+
* toBlock: BigInt(1_010_000),
|
|
50
|
+
* step: 1000
|
|
51
|
+
* }).pipe(Stream.runCollect)
|
|
52
|
+
* })
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @since 0.1.0
|
|
56
|
+
*/
|
|
57
|
+
import * as JsonRpc from "@effect-blockchain/core/JsonRpc"
|
|
58
|
+
import * as Arr from "effect/Array"
|
|
59
|
+
import * as BigInt_ from "effect/BigInt"
|
|
60
|
+
import * as Config from "effect/Config"
|
|
61
|
+
import * as Context from "effect/Context"
|
|
62
|
+
import * as Duration from "effect/Duration"
|
|
63
|
+
import * as Effect from "effect/Effect"
|
|
64
|
+
import * as Layer from "effect/Layer"
|
|
65
|
+
import * as Option from "effect/Option"
|
|
66
|
+
import * as Schedule from "effect/Schedule"
|
|
67
|
+
import * as Stream from "effect/Stream"
|
|
68
|
+
import * as Confirmations from "./internal/confirmations.ts"
|
|
69
|
+
import type { StarknetAddress } from "./StarknetAddress.ts"
|
|
70
|
+
import type { StarknetEvent } from "./StarknetEvent.ts"
|
|
71
|
+
import * as StarknetNetwork from "./StarknetNetwork.ts"
|
|
72
|
+
import type { StarknetEventsCursor, StarknetEventsFilter } from "./StarknetRpc.ts"
|
|
73
|
+
import { StarknetRpc, StarknetRpcError } from "./StarknetRpc.ts"
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A {@link StarknetIndexer} `fetchEvents` filter: a `starknet_getEvents` filter without its paging fields, over the
|
|
77
|
+
* inclusive block range `[fromBlock, toBlock]`.
|
|
78
|
+
*
|
|
79
|
+
* @since 0.1.0
|
|
80
|
+
* @category models
|
|
81
|
+
*/
|
|
82
|
+
export type StarknetIndexerFilter = Omit<StarknetEventsFilter, "chunkSize" | "cursor">
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The events of `[fromBlock, toBlock]` (both inclusive), in chain order, emitted by
|
|
86
|
+
* {@link StarknetIndexer.Service.streamEvents}.
|
|
87
|
+
*
|
|
88
|
+
* A batch is emitted for every range, even without events. Delivery is at-least-once: save `batch.toBlock` once the
|
|
89
|
+
* batch is handled and resume from `toBlock + 1`.
|
|
90
|
+
*
|
|
91
|
+
* @since 0.1.0
|
|
92
|
+
* @category models
|
|
93
|
+
*/
|
|
94
|
+
export interface StarknetIndexerBatch {
|
|
95
|
+
readonly fromBlock: bigint
|
|
96
|
+
readonly toBlock: bigint
|
|
97
|
+
readonly events: ReadonlyArray<StarknetEvent>
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The default `maxBlockRange`. Starknet nodes do not limit the block range of `starknet_getEvents` (they cap the page
|
|
102
|
+
* size), so a range only bounds the work a restart redoes: 10000 blocks is a few hours of mainnet.
|
|
103
|
+
*
|
|
104
|
+
* @since 0.1.0
|
|
105
|
+
* @category constants
|
|
106
|
+
*/
|
|
107
|
+
export const DEFAULT_MAX_BLOCK_RANGE = 10_000
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The {@link StarknetIndexer.Service.streamEvents} options.
|
|
111
|
+
*
|
|
112
|
+
* @since 0.1.0
|
|
113
|
+
* @category models
|
|
114
|
+
*/
|
|
115
|
+
export interface StarknetIndexerStreamOptions {
|
|
116
|
+
readonly filter: Omit<StarknetIndexerFilter, "fromBlock" | "toBlock">
|
|
117
|
+
readonly fromBlock: bigint
|
|
118
|
+
/** The stream ends after the batch reaching it, and is empty when `fromBlock > toBlock`. */
|
|
119
|
+
readonly toBlock?: bigint | undefined
|
|
120
|
+
/**
|
|
121
|
+
* When to read the head again once caught up, `Schedule.spaced(DEFAULT_POLL_INTERVAL)` by default. It restarts after
|
|
122
|
+
* each batch, and its end ends the stream: `Schedule.recurs(0)` drains to the head and stops.
|
|
123
|
+
*/
|
|
124
|
+
readonly poll?: Schedule.Schedule<unknown> | undefined
|
|
125
|
+
/** Retries a step failing with a transient reason ({@link JsonRpc.isTransient}) at the same block. Off by default. */
|
|
126
|
+
readonly retry?: Schedule.Schedule<unknown, StarknetRpcError> | undefined
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The STRK balance of an address in fri at the end of `block`, emitted by
|
|
131
|
+
* {@link StarknetIndexer.Service.streamNativeBalance}.
|
|
132
|
+
*
|
|
133
|
+
* @since 0.2.0
|
|
134
|
+
* @category models
|
|
135
|
+
*/
|
|
136
|
+
export interface StarknetNativeBalance {
|
|
137
|
+
readonly block: bigint
|
|
138
|
+
readonly balance: bigint
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The {@link StarknetIndexer.Service.streamNativeBalance} options: one sample every `step` blocks from `fromBlock`.
|
|
143
|
+
* `step` is a positive integer, `poll` and `retry` behave as in {@link StarknetIndexerStreamOptions}.
|
|
144
|
+
*
|
|
145
|
+
* @since 0.2.0
|
|
146
|
+
* @category models
|
|
147
|
+
*/
|
|
148
|
+
export interface StarknetIndexerNativeBalanceOptions {
|
|
149
|
+
readonly address: StarknetAddress
|
|
150
|
+
readonly fromBlock: bigint
|
|
151
|
+
/** The stream ends after the last sample at or below it, and is empty when `fromBlock > toBlock`. */
|
|
152
|
+
readonly toBlock?: bigint | undefined
|
|
153
|
+
readonly step: number
|
|
154
|
+
readonly poll?: Schedule.Schedule<unknown> | undefined
|
|
155
|
+
readonly retry?: Schedule.Schedule<unknown, StarknetRpcError> | undefined
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @since 0.1.0
|
|
160
|
+
* @category services
|
|
161
|
+
*/
|
|
162
|
+
export class StarknetIndexer extends Context.Service<StarknetIndexer, {
|
|
163
|
+
/** The latest block with at least `confirmations` blocks on top of it, never below 0. */
|
|
164
|
+
readonly lastConfirmedBlock: Effect.Effect<bigint, StarknetRpcError>
|
|
165
|
+
/**
|
|
166
|
+
* Every event matching `filter` in `[fromBlock, toBlock]`, in chain order. `fromBlock > toBlock` returns no events.
|
|
167
|
+
* A `toBlock` above `lastConfirmedBlock` is accepted.
|
|
168
|
+
*/
|
|
169
|
+
readonly fetchEvents: (filter: StarknetIndexerFilter) => Effect.Effect<ReadonlyArray<StarknetEvent>, StarknetRpcError>
|
|
170
|
+
/**
|
|
171
|
+
* The events matching `filter` from `fromBlock` on, in batches of at most `maxBlockRange` confirmed blocks, following
|
|
172
|
+
* the confirmed head.
|
|
173
|
+
*/
|
|
174
|
+
readonly streamEvents: (
|
|
175
|
+
options: StarknetIndexerStreamOptions
|
|
176
|
+
) => Stream.Stream<StarknetIndexerBatch, StarknetRpcError>
|
|
177
|
+
/** The STRK balance of `address` in fri at the end of `block`. A `block` above `lastConfirmedBlock` is accepted. */
|
|
178
|
+
readonly fetchNativeBalance: (
|
|
179
|
+
options: { readonly address: StarknetAddress; readonly block: bigint }
|
|
180
|
+
) => Effect.Effect<bigint, StarknetRpcError>
|
|
181
|
+
/**
|
|
182
|
+
* The STRK balance of `address` at `fromBlock`, `fromBlock + step`, `fromBlock + 2 * step`... one sample per step
|
|
183
|
+
* even when unchanged, following the confirmed head.
|
|
184
|
+
*/
|
|
185
|
+
readonly streamNativeBalance: (
|
|
186
|
+
options: StarknetIndexerNativeBalanceOptions
|
|
187
|
+
) => Stream.Stream<StarknetNativeBalance, StarknetRpcError>
|
|
188
|
+
}>()("@effect-blockchain/starknet/StarknetIndexer") {}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The default `poll` spacing of {@link StarknetIndexer.Service.streamEvents}: 10 seconds.
|
|
192
|
+
*
|
|
193
|
+
* @since 0.1.0
|
|
194
|
+
* @category constants
|
|
195
|
+
*/
|
|
196
|
+
export const DEFAULT_POLL_INTERVAL: Duration.Duration = Duration.seconds(10)
|
|
197
|
+
|
|
198
|
+
// sn_keccak("balanceOf"), the ERC-20 entry point selector
|
|
199
|
+
const BALANCE_OF = "0x02e4263afad30923c891518314c3c95dbe830a16874e8abc5777a9a20b54c76e"
|
|
200
|
+
|
|
201
|
+
// the confirmed blocks [next, head] not indexed yet, empty once the stream reaches the head it last read
|
|
202
|
+
interface StreamPosition {
|
|
203
|
+
readonly next: bigint
|
|
204
|
+
readonly head: bigint
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Builds a {@link StarknetIndexer} over the provided {@link StarknetRpc}.
|
|
209
|
+
*
|
|
210
|
+
* - `confirmations` defaults to {@link StarknetNetwork.defaultConfirmations} for the RPC's network.
|
|
211
|
+
* - `maxBlockRange` is the largest range in blocks, a positive integer, default {@link DEFAULT_MAX_BLOCK_RANGE}.
|
|
212
|
+
* - `chunkSize` is the `starknet_getEvents` page size, default 1000: lower it for a provider with a smaller cap.
|
|
213
|
+
*
|
|
214
|
+
* @since 0.1.0
|
|
215
|
+
* @category layers
|
|
216
|
+
*/
|
|
217
|
+
export const layer = (options: {
|
|
218
|
+
readonly confirmations?: number | undefined
|
|
219
|
+
readonly maxBlockRange?: number | undefined
|
|
220
|
+
readonly chunkSize?: number | undefined
|
|
221
|
+
} = {}): Layer.Layer<StarknetIndexer, never, StarknetRpc> =>
|
|
222
|
+
Layer.effect(
|
|
223
|
+
StarknetIndexer,
|
|
224
|
+
Effect.gen(function*() {
|
|
225
|
+
const rpc = yield* StarknetRpc
|
|
226
|
+
const maxBlockRange = options.maxBlockRange ?? DEFAULT_MAX_BLOCK_RANGE
|
|
227
|
+
if (!Number.isSafeInteger(maxBlockRange) || maxBlockRange < 1) {
|
|
228
|
+
return yield* Effect.die(new RangeError(`maxBlockRange must be a positive integer, got ${maxBlockRange}`))
|
|
229
|
+
}
|
|
230
|
+
const size = BigInt(maxBlockRange)
|
|
231
|
+
const lastConfirmedBlock = Confirmations.lastConfirmedBlock(rpc, options.confirmations).pipe(
|
|
232
|
+
Effect.withSpan("StarknetIndexer.lastConfirmedBlock")
|
|
233
|
+
)
|
|
234
|
+
const fetchEvents = Effect.fn("StarknetIndexer.fetchEvents")((filter: StarknetIndexerFilter) =>
|
|
235
|
+
Effect.forEach(
|
|
236
|
+
filter.fromBlock > filter.toBlock ? [] : Arr.makeBy(
|
|
237
|
+
Number((filter.toBlock - filter.fromBlock) / size) + 1,
|
|
238
|
+
(i) => filter.fromBlock + BigInt(i) * size
|
|
239
|
+
),
|
|
240
|
+
(fromBlock) => {
|
|
241
|
+
const toBlock = BigInt_.min(fromBlock + size - BigInt(1), filter.toBlock)
|
|
242
|
+
const range = { ...filter, fromBlock, toBlock, chunkSize: options.chunkSize }
|
|
243
|
+
// a cursor page has no fallback: when it fails, the range restarts once from its first page
|
|
244
|
+
return Stream.paginate(
|
|
245
|
+
Option.none<StarknetEventsCursor>(),
|
|
246
|
+
(cursor) =>
|
|
247
|
+
rpc.getEvents({ ...range, cursor: Option.getOrUndefined(cursor) }).pipe(Effect.mapBoth({
|
|
248
|
+
onFailure: (error) => Option.isNone(cursor) ? error : { cursorPage: error },
|
|
249
|
+
onSuccess: (page) =>
|
|
250
|
+
[page.events, Option.map(Option.fromUndefinedOr(page.cursor), Option.some)] as const
|
|
251
|
+
}))
|
|
252
|
+
).pipe(
|
|
253
|
+
Stream.runCollect,
|
|
254
|
+
Effect.retry({ times: 1, while: (error) => "cursorPage" in error }),
|
|
255
|
+
Effect.mapError((error) => "cursorPage" in error ? error.cursorPage : error)
|
|
256
|
+
)
|
|
257
|
+
}
|
|
258
|
+
).pipe(Effect.map(Arr.flatten))
|
|
259
|
+
)
|
|
260
|
+
// the head last read is reused while it covers `next` (the confirmed head only moves forward), else polled
|
|
261
|
+
const confirmedFor = (next: bigint, head: bigint, poll: Schedule.Schedule<unknown>) =>
|
|
262
|
+
next <= head
|
|
263
|
+
? Effect.succeed(head)
|
|
264
|
+
: lastConfirmedBlock.pipe(Effect.repeat({ schedule: poll, until: (confirmed) => confirmed >= next }))
|
|
265
|
+
const retrying = <A, S>(
|
|
266
|
+
step: (state: S) => Effect.Effect<A, StarknetRpcError>,
|
|
267
|
+
retry: Schedule.Schedule<unknown, StarknetRpcError> | undefined
|
|
268
|
+
) =>
|
|
269
|
+
retry === undefined
|
|
270
|
+
? step
|
|
271
|
+
: (state: S) =>
|
|
272
|
+
Effect.retry(step(state), { schedule: retry, while: (error) => JsonRpc.isTransient(error.reason) })
|
|
273
|
+
const fetchNativeBalance = Effect.fn("StarknetIndexer.fetchNativeBalance")(
|
|
274
|
+
({ address, block }: { readonly address: StarknetAddress; readonly block: bigint }) =>
|
|
275
|
+
rpc.call({
|
|
276
|
+
contractAddress: StarknetNetwork.STRK_TOKEN_ADDRESS,
|
|
277
|
+
entryPointSelector: BALANCE_OF,
|
|
278
|
+
calldata: [address],
|
|
279
|
+
blockNumber: block
|
|
280
|
+
}).pipe(Effect.flatMap((felts) =>
|
|
281
|
+
felts.length === 2
|
|
282
|
+
? Effect.succeed(BigInt(felts[0]) + (BigInt(felts[1]) << BigInt(128)))
|
|
283
|
+
: Effect.fail(
|
|
284
|
+
new StarknetRpcError({
|
|
285
|
+
reason: new JsonRpc.InvalidResponse(),
|
|
286
|
+
method: "starknet_call",
|
|
287
|
+
endpoint: "StarknetIndexer",
|
|
288
|
+
cause: new Error(`balanceOf returned ${felts.length} felts, expected a u256 (low, high)`)
|
|
289
|
+
})
|
|
290
|
+
)
|
|
291
|
+
))
|
|
292
|
+
)
|
|
293
|
+
return StarknetIndexer.of({
|
|
294
|
+
lastConfirmedBlock,
|
|
295
|
+
fetchEvents,
|
|
296
|
+
fetchNativeBalance,
|
|
297
|
+
streamNativeBalance: (
|
|
298
|
+
{ address, fromBlock, poll = Schedule.spaced(DEFAULT_POLL_INTERVAL), retry, step, toBlock }
|
|
299
|
+
) => {
|
|
300
|
+
if (!Number.isSafeInteger(step) || step < 1) {
|
|
301
|
+
return Stream.die(new RangeError(`step must be a positive integer, got ${step}`))
|
|
302
|
+
}
|
|
303
|
+
if (toBlock !== undefined && fromBlock > toBlock) return Stream.empty
|
|
304
|
+
const size = BigInt(step)
|
|
305
|
+
const sample = (
|
|
306
|
+
{ head, next }: StreamPosition
|
|
307
|
+
): Effect.Effect<
|
|
308
|
+
readonly [ReadonlyArray<StarknetNativeBalance>, Option.Option<StreamPosition>],
|
|
309
|
+
StarknetRpcError
|
|
310
|
+
> =>
|
|
311
|
+
confirmedFor(next, head, poll).pipe(Effect.flatMap((confirmed) => {
|
|
312
|
+
if (next > confirmed) return Effect.succeed([[], Option.none()])
|
|
313
|
+
return fetchNativeBalance({ address, block: next }).pipe(Effect.map((balance) => [
|
|
314
|
+
[{ block: next, balance }],
|
|
315
|
+
toBlock !== undefined && next + size > toBlock
|
|
316
|
+
? Option.none()
|
|
317
|
+
: Option.some({ next: next + size, head: confirmed })
|
|
318
|
+
]))
|
|
319
|
+
}))
|
|
320
|
+
return Stream.paginate({ next: fromBlock, head: fromBlock - BigInt(1) }, retrying(sample, retry))
|
|
321
|
+
},
|
|
322
|
+
streamEvents: ({ filter, fromBlock, poll = Schedule.spaced(DEFAULT_POLL_INTERVAL), retry, toBlock }) => {
|
|
323
|
+
if (toBlock !== undefined && fromBlock > toBlock) return Stream.empty
|
|
324
|
+
const step = (
|
|
325
|
+
{ head, next }: StreamPosition
|
|
326
|
+
): Effect.Effect<
|
|
327
|
+
readonly [ReadonlyArray<StarknetIndexerBatch>, Option.Option<StreamPosition>],
|
|
328
|
+
StarknetRpcError
|
|
329
|
+
> =>
|
|
330
|
+
confirmedFor(next, head, poll).pipe(Effect.flatMap((confirmed) => {
|
|
331
|
+
const cap = toBlock === undefined ? confirmed : BigInt_.min(confirmed, toBlock)
|
|
332
|
+
if (next > cap) return Effect.succeed([[], Option.none()])
|
|
333
|
+
const end = BigInt_.min(next + size - BigInt(1), cap)
|
|
334
|
+
return fetchEvents({ ...filter, fromBlock: next, toBlock: end }).pipe(Effect.map((events) => [
|
|
335
|
+
[{ fromBlock: next, toBlock: end, events }],
|
|
336
|
+
end === toBlock ? Option.none() : Option.some({ next: end + BigInt(1), head: confirmed })
|
|
337
|
+
]))
|
|
338
|
+
}))
|
|
339
|
+
// a head below `fromBlock`: the first step reads it
|
|
340
|
+
return Stream.paginate({ next: fromBlock, head: fromBlock - BigInt(1) }, retrying(step, retry))
|
|
341
|
+
}
|
|
342
|
+
})
|
|
343
|
+
})
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Builds an {@link StarknetIndexer} like {@link layer}, reading the options from `Config`.
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
* ```ts
|
|
351
|
+
* import * as StarknetIndexer from "@effect-blockchain/starknet/StarknetIndexer"
|
|
352
|
+
* import * as Config from "effect/Config"
|
|
353
|
+
*
|
|
354
|
+
* const IndexerLive = StarknetIndexer.layerConfig({ confirmations: Config.Int("STARKNET_CONFIRMATIONS") })
|
|
355
|
+
* ```
|
|
356
|
+
*
|
|
357
|
+
* @since 0.1.0
|
|
358
|
+
* @category layers
|
|
359
|
+
*/
|
|
360
|
+
export const layerConfig = (
|
|
361
|
+
options: Config.Wrap<NonNullable<Parameters<typeof layer>[0]>>
|
|
362
|
+
): Layer.Layer<StarknetIndexer, Config.ConfigError, StarknetRpc> =>
|
|
363
|
+
Layer.unwrap(Effect.map(Config.unwrap(options), layer))
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Named Starknet networks and their chain IDs.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.1.0
|
|
5
|
+
*/
|
|
6
|
+
import * as Schema from "effect/Schema"
|
|
7
|
+
import { StarknetAddress } from "./StarknetAddress.ts"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @since 0.1.0
|
|
11
|
+
* @category constants
|
|
12
|
+
*/
|
|
13
|
+
export const mainnets = ["Starknet"] as const
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @since 0.1.0
|
|
17
|
+
* @category constants
|
|
18
|
+
*/
|
|
19
|
+
export const testnets = ["StarknetSepolia"] as const
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Every supported network, mainnets first.
|
|
23
|
+
*
|
|
24
|
+
* @since 0.1.0
|
|
25
|
+
* @category constants
|
|
26
|
+
*/
|
|
27
|
+
export const networks = [...mainnets, ...testnets] as const
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @since 0.1.0
|
|
31
|
+
* @category schemas
|
|
32
|
+
*/
|
|
33
|
+
export const StarknetNetwork = Schema.Literals(networks).annotate({
|
|
34
|
+
identifier: "StarknetNetwork",
|
|
35
|
+
title: "Starknet network",
|
|
36
|
+
description: "a Starknet network name: \"Starknet\" (mainnet) or \"StarknetSepolia\" (testnet)"
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @since 0.1.0
|
|
41
|
+
* @category models
|
|
42
|
+
*/
|
|
43
|
+
export type StarknetNetwork = typeof StarknetNetwork.Type
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A Starknet chain ID, the short string `starknet_chainId` returns.
|
|
47
|
+
*
|
|
48
|
+
* @since 0.1.0
|
|
49
|
+
* @category schemas
|
|
50
|
+
*/
|
|
51
|
+
export const StarknetChainId = Schema.Literals(["SN_MAIN", "SN_SEPOLIA"]).annotate({
|
|
52
|
+
identifier: "StarknetChainId",
|
|
53
|
+
title: "Starknet chain ID",
|
|
54
|
+
description: "a Starknet chain ID as a short string: \"SN_MAIN\" or \"SN_SEPOLIA\""
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @since 0.1.0
|
|
59
|
+
* @category models
|
|
60
|
+
*/
|
|
61
|
+
export type StarknetChainId = typeof StarknetChainId.Type
|
|
62
|
+
|
|
63
|
+
const chainIds: Record<StarknetNetwork, StarknetChainId> = {
|
|
64
|
+
Starknet: "SN_MAIN",
|
|
65
|
+
StarknetSepolia: "SN_SEPOLIA"
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @since 0.1.0
|
|
70
|
+
* @category conversions
|
|
71
|
+
*/
|
|
72
|
+
export const toChainId = (network: StarknetNetwork): StarknetChainId => chainIds[network]
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Default number of confirmations an indexer waits for on each network, on top of the latest accepted block.
|
|
76
|
+
*
|
|
77
|
+
* @since 0.1.0
|
|
78
|
+
* @category getters
|
|
79
|
+
*/
|
|
80
|
+
export const defaultConfirmations = (network: StarknetNetwork): number => {
|
|
81
|
+
switch (network) {
|
|
82
|
+
// ACCEPTED_ON_L2 blocks are "finalized by the consensus protocol": https://docs.starknet.io/learn/protocol/transactions
|
|
83
|
+
case "Starknet":
|
|
84
|
+
return 0
|
|
85
|
+
// same protocol and finality rules as mainnet: https://docs.starknet.io/learn/protocol/transactions
|
|
86
|
+
case "StarknetSepolia":
|
|
87
|
+
return 0
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The STRK token contract, the token Starknet fees are paid in, counted in fri (10^-18 STRK). The same address on
|
|
93
|
+
* mainnet and Sepolia: https://docs.starknet.io/resources/chain-info
|
|
94
|
+
*
|
|
95
|
+
* @since 0.2.0
|
|
96
|
+
* @category constants
|
|
97
|
+
*/
|
|
98
|
+
export const STRK_TOKEN_ADDRESS: StarknetAddress = StarknetAddress.make(
|
|
99
|
+
"0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @since 0.1.0
|
|
104
|
+
* @category predicates
|
|
105
|
+
*/
|
|
106
|
+
export const isTestnet = (network: StarknetNetwork): boolean =>
|
|
107
|
+
(testnets as ReadonlyArray<StarknetNetwork>).includes(network)
|