@effect-blockchain/stellar 0.0.0-stage → 0.1.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/StellarAddress.d.ts +48 -0
- package/dist/StellarAddress.d.ts.map +1 -0
- package/dist/StellarAddress.js +68 -0
- package/dist/StellarAddress.js.map +1 -0
- package/dist/StellarContractEvent.d.ts +41 -0
- package/dist/StellarContractEvent.d.ts.map +1 -0
- package/dist/StellarContractEvent.js +49 -0
- package/dist/StellarContractEvent.js.map +1 -0
- package/dist/StellarIndexer.d.ts +121 -0
- package/dist/StellarIndexer.d.ts.map +1 -0
- package/dist/StellarIndexer.js +186 -0
- package/dist/StellarIndexer.js.map +1 -0
- package/dist/StellarNetwork.d.ts +56 -0
- package/dist/StellarNetwork.d.ts.map +1 -0
- package/dist/StellarNetwork.js +67 -0
- package/dist/StellarNetwork.js.map +1 -0
- package/dist/StellarRpc.d.ts +198 -0
- package/dist/StellarRpc.d.ts.map +1 -0
- package/dist/StellarRpc.js +244 -0
- package/dist/StellarRpc.js.map +1 -0
- package/dist/StellarTransactionHash.d.ts +17 -0
- package/dist/StellarTransactionHash.d.ts.map +1 -0
- package/dist/StellarTransactionHash.js +29 -0
- package/dist/StellarTransactionHash.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/LedgerFromNumber.d.ts +2 -0
- package/dist/internal/LedgerFromNumber.d.ts.map +1 -0
- package/dist/internal/LedgerFromNumber.js +12 -0
- package/dist/internal/LedgerFromNumber.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 +64 -0
- package/dist/internal/rpcMethods.js.map +1 -0
- package/package.json +68 -3
- package/src/StellarAddress.ts +114 -0
- package/src/StellarContractEvent.ts +54 -0
- package/src/StellarIndexer.ts +260 -0
- package/src/StellarNetwork.ts +82 -0
- package/src/StellarRpc.ts +371 -0
- package/src/StellarTransactionHash.ts +41 -0
- package/src/index.ts +35 -0
- package/src/internal/LedgerFromNumber.ts +11 -0
- package/src/internal/rpcMethods.ts +54 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed, read-only Stellar RPC methods over an ordered list of endpoints with fallback.
|
|
3
|
+
*
|
|
4
|
+
* A call starts at the first endpoint and moves to the next one on failure, except on `WrongNetwork`. Each endpoint
|
|
5
|
+
* is checked once to serve the configured network (`getNetwork` passphrase). Every method is a read, so retries and
|
|
6
|
+
* fallback are safe. Methods are named after the RPC method. A `getEvents` page cursor is only sent to the endpoint
|
|
7
|
+
* that issued it, with no fallback.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { StellarContractAddress } from "@effect-blockchain/stellar/StellarAddress"
|
|
12
|
+
* import * as StellarRpc from "@effect-blockchain/stellar/StellarRpc"
|
|
13
|
+
* import * as Effect from "effect/Effect"
|
|
14
|
+
*
|
|
15
|
+
* declare const contract: StellarContractAddress
|
|
16
|
+
*
|
|
17
|
+
* // the first page of events of `contract` over the last 100 ledgers
|
|
18
|
+
* const recent = Effect.gen(function*() {
|
|
19
|
+
* const rpc = yield* StellarRpc.StellarRpc
|
|
20
|
+
* const { sequence } = yield* rpc.getLatestLedger
|
|
21
|
+
* return yield* rpc.getEvents({ contractIds: [contract], fromBlock: sequence - BigInt(100), toBlock: sequence })
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @since 0.1.0
|
|
26
|
+
*/
|
|
27
|
+
import * as JsonRpc from "@effect-blockchain/core/JsonRpc"
|
|
28
|
+
import * as Arr from "effect/Array"
|
|
29
|
+
import * as BigInt_ from "effect/BigInt"
|
|
30
|
+
import * as Config from "effect/Config"
|
|
31
|
+
import * as Context from "effect/Context"
|
|
32
|
+
import type * as Duration from "effect/Duration"
|
|
33
|
+
import * as Effect from "effect/Effect"
|
|
34
|
+
import type * as HttpClient from "effect/http/HttpClient"
|
|
35
|
+
import * as Layer from "effect/Layer"
|
|
36
|
+
import * as Order from "effect/Order"
|
|
37
|
+
import type * as Redacted from "effect/Redacted"
|
|
38
|
+
import * as Ref from "effect/Ref"
|
|
39
|
+
import * as Schema from "effect/Schema"
|
|
40
|
+
import * as RpcMethods from "./internal/rpcMethods.ts"
|
|
41
|
+
import type { StellarContractAddress } from "./StellarAddress.ts"
|
|
42
|
+
import type { StellarContractEvent } from "./StellarContractEvent.ts"
|
|
43
|
+
import * as StellarNetwork from "./StellarNetwork.ts"
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A Stellar RPC endpoint. The `name` is shown in errors and spans and routes cursors, so it must be unique; the `url`
|
|
47
|
+
* is never logged.
|
|
48
|
+
*
|
|
49
|
+
* @since 0.1.0
|
|
50
|
+
* @category models
|
|
51
|
+
*/
|
|
52
|
+
export interface StellarRpcEndpoint {
|
|
53
|
+
readonly name: string
|
|
54
|
+
readonly url: Redacted.Redacted<string>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A failed call: its `reason` (a {@link JsonRpc.Reason}, matched with `Effect.catchReason`), the JSON-RPC error code or
|
|
59
|
+
* HTTP status as `code`, and a `cause` without the endpoint URL. After several endpoints, the error is the last one
|
|
60
|
+
* and `cause` is an `AggregateError` of each endpoint's error, in order.
|
|
61
|
+
*
|
|
62
|
+
* On Stellar, `Unavailable` includes the code -32603, `InvalidResponse` an event outside the `getEvents` filter,
|
|
63
|
+
* `Lagging` a latest ledger below `toBlock`, and `Pruned` an oldest retained ledger above `fromBlock` (or the cursor).
|
|
64
|
+
*
|
|
65
|
+
* @since 0.1.0
|
|
66
|
+
* @category errors
|
|
67
|
+
*/
|
|
68
|
+
export class StellarRpcError extends Schema.TaggedError<StellarRpcError>()("StellarRpcError", {
|
|
69
|
+
reason: JsonRpc.Reason,
|
|
70
|
+
method: Schema.String,
|
|
71
|
+
endpoint: Schema.String,
|
|
72
|
+
code: Schema.optional(Schema.Number),
|
|
73
|
+
cause: Schema.Defect()
|
|
74
|
+
}) {
|
|
75
|
+
/**
|
|
76
|
+
* @since 0.1.0
|
|
77
|
+
*/
|
|
78
|
+
override get message(): string {
|
|
79
|
+
return `${this.method} failed on ${this.endpoint}: ${this.reason._tag}${
|
|
80
|
+
this.code === undefined ? "" : ` (${this.code})`
|
|
81
|
+
}`
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Where a `getEvents` page stopped: the RPC paging cursor and the endpoint that issued it. Opaque: only pass it back to
|
|
87
|
+
* `getEvents` with the same filter.
|
|
88
|
+
*
|
|
89
|
+
* @since 0.1.0
|
|
90
|
+
* @category schemas
|
|
91
|
+
*/
|
|
92
|
+
export const StellarEventsCursor = Schema.Struct({
|
|
93
|
+
endpoint: Schema.String,
|
|
94
|
+
cursor: Schema.String
|
|
95
|
+
}).pipe(Schema.brand("StellarEventsCursor"))
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @since 0.1.0
|
|
99
|
+
* @category models
|
|
100
|
+
*/
|
|
101
|
+
export type StellarEventsCursor = typeof StellarEventsCursor.Type
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A `getEvents` filter over the inclusive ledger range `[fromBlock, toBlock]`.
|
|
105
|
+
*
|
|
106
|
+
* - `contractIds`: 1 to 5 contracts (the RPC limit).
|
|
107
|
+
* - `topics`: RPC topic filters, each a list of 1 to 4 segments: a base64 XDR `ScVal`, `"*"` (any one topic) or a
|
|
108
|
+
* final `"**"` (any number of topics). An event matches when it matches any filter.
|
|
109
|
+
* - `limit`: page size, default 1000, at most 10000.
|
|
110
|
+
* - `cursor`: the cursor of the previous page, to continue from it on the same endpoint.
|
|
111
|
+
*
|
|
112
|
+
* @since 0.1.0
|
|
113
|
+
* @category models
|
|
114
|
+
*/
|
|
115
|
+
export interface StellarEventsFilter {
|
|
116
|
+
readonly contractIds: Arr.NonEmptyReadonlyArray<StellarContractAddress>
|
|
117
|
+
readonly fromBlock: bigint
|
|
118
|
+
readonly toBlock: bigint
|
|
119
|
+
readonly topics?: ReadonlyArray<ReadonlyArray<string>> | undefined
|
|
120
|
+
readonly limit?: number | undefined
|
|
121
|
+
readonly cursor?: StellarEventsCursor | undefined
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* One `getEvents` page, in chain order (sorted by event id). `cursor` is `undefined` once the range is done.
|
|
126
|
+
*
|
|
127
|
+
* @since 0.1.0
|
|
128
|
+
* @category models
|
|
129
|
+
*/
|
|
130
|
+
export interface StellarEventsPage {
|
|
131
|
+
readonly events: ReadonlyArray<StellarContractEvent>
|
|
132
|
+
readonly cursor: StellarEventsCursor | undefined
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @since 0.1.0
|
|
137
|
+
* @category models
|
|
138
|
+
*/
|
|
139
|
+
export interface StellarNetworkInfo {
|
|
140
|
+
readonly passphrase: string
|
|
141
|
+
readonly protocolVersion: number
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* @since 0.1.0
|
|
146
|
+
* @category models
|
|
147
|
+
*/
|
|
148
|
+
export interface StellarLatestLedger {
|
|
149
|
+
readonly sequence: bigint
|
|
150
|
+
/** Hex hash of the ledger. */
|
|
151
|
+
readonly id: string
|
|
152
|
+
readonly protocolVersion: number
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @since 0.1.0
|
|
157
|
+
* @category services
|
|
158
|
+
*/
|
|
159
|
+
export class StellarRpc extends Context.Service<StellarRpc, {
|
|
160
|
+
readonly network: StellarNetwork.StellarNetwork
|
|
161
|
+
/**
|
|
162
|
+
* `getNetwork`, verified against `network` once per endpoint then served from that check: `protocolVersion` is the
|
|
163
|
+
* one seen at the check, `getLatestLedger` has the current one.
|
|
164
|
+
*/
|
|
165
|
+
readonly getNetwork: Effect.Effect<StellarNetworkInfo, StellarRpcError>
|
|
166
|
+
/** `getLatestLedger` */
|
|
167
|
+
readonly getLatestLedger: Effect.Effect<StellarLatestLedger, StellarRpcError>
|
|
168
|
+
/**
|
|
169
|
+
* `getEvents` for contract events, one page in chain order (by event id), only answered by an endpoint whose latest
|
|
170
|
+
* ledger has reached `toBlock` and that still retains `fromBlock`. Events above `toBlock` are dropped, and an event of
|
|
171
|
+
* another type, of another contract or below `fromBlock` fails with `InvalidResponse`.
|
|
172
|
+
*
|
|
173
|
+
* `cursor` is `undefined` once the RPC cursor passes `toBlock`. A short page is not the end of the range: the RPC
|
|
174
|
+
* scans at most 10000 ledgers per request. A short page stopping at a latest ledger below `toBlock` fails `Lagging`.
|
|
175
|
+
*/
|
|
176
|
+
readonly getEvents: (filter: StellarEventsFilter) => Effect.Effect<StellarEventsPage, StellarRpcError>
|
|
177
|
+
}>()("@effect-blockchain/stellar/StellarRpc") {}
|
|
178
|
+
|
|
179
|
+
// JSON-RPC internal error: stellar-rpc returns it for its own database or I/O failures, retry then fall back
|
|
180
|
+
const codes: ReadonlyMap<number, JsonRpc.CodeReason> = new Map([[-32603, "Unavailable"]])
|
|
181
|
+
|
|
182
|
+
// stellar-rpc's out-of-range start, current ("startLedger must be within the ledger range: X - Y") and older
|
|
183
|
+
// ("startLedger must be between the oldest ledger: X and the latest ledger: Y ...")
|
|
184
|
+
const ledgerRangeError = /startLedger must be \D*(\d+)\D+(\d+)/u
|
|
185
|
+
|
|
186
|
+
// the ledger of a TOID cursor: ledger << 32 | transaction << 12 | operation
|
|
187
|
+
const cursorLedger = (cursor: string) => BigInt(cursor.slice(0, 19)) >> BigInt(32)
|
|
188
|
+
|
|
189
|
+
const byId = Order.mapInput(Order.String, (event: StellarContractEvent) => event.id)
|
|
190
|
+
|
|
191
|
+
type Endpoint = JsonRpc.Endpoint<StellarNetworkInfo, StellarRpcError>
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Builds a {@link StellarRpc} over `endpoints`, tried in order, sending requests with the provided `HttpClient`
|
|
195
|
+
* (e.g. `FetchHttpClient.layer`). `timeout` (default 30 seconds) bounds each attempt, `retries` (default 3) and
|
|
196
|
+
* `retryDelay` (default 500 millis) set the backoff before falling back: see {@link JsonRpc.Unavailable}.
|
|
197
|
+
*
|
|
198
|
+
* @since 0.1.0
|
|
199
|
+
* @category layers
|
|
200
|
+
*/
|
|
201
|
+
export const layer = (options: {
|
|
202
|
+
readonly network: StellarNetwork.StellarNetwork
|
|
203
|
+
readonly endpoints: Arr.NonEmptyReadonlyArray<StellarRpcEndpoint>
|
|
204
|
+
readonly timeout?: Duration.Input | undefined
|
|
205
|
+
readonly retries?: number | undefined
|
|
206
|
+
readonly retryDelay?: Duration.Input | undefined
|
|
207
|
+
}): Layer.Layer<StellarRpc, never, HttpClient.HttpClient> =>
|
|
208
|
+
Layer.effect(
|
|
209
|
+
StellarRpc,
|
|
210
|
+
Effect.gen(function*() {
|
|
211
|
+
const { network } = options
|
|
212
|
+
const passphrase = StellarNetwork.toPassphrase(network)
|
|
213
|
+
const rpc = yield* JsonRpc.make({
|
|
214
|
+
spanPrefix: "StellarRpc",
|
|
215
|
+
network,
|
|
216
|
+
endpoints: options.endpoints,
|
|
217
|
+
timeout: options.timeout,
|
|
218
|
+
retries: options.retries,
|
|
219
|
+
retryDelay: options.retryDelay,
|
|
220
|
+
codes,
|
|
221
|
+
checkNetwork: (send, endpoint) =>
|
|
222
|
+
send(RpcMethods.getNetwork, undefined).pipe(
|
|
223
|
+
Effect.flatMap((info) =>
|
|
224
|
+
info.passphrase === passphrase ? Effect.succeed(info) : Effect.fail(
|
|
225
|
+
new StellarRpcError({
|
|
226
|
+
reason: new JsonRpc.WrongNetwork({ expected: passphrase, actual: info.passphrase }),
|
|
227
|
+
method: RpcMethods.getNetwork.name,
|
|
228
|
+
endpoint,
|
|
229
|
+
cause: new Error(`expected passphrase "${passphrase}" for ${network}, got "${info.passphrase}"`)
|
|
230
|
+
})
|
|
231
|
+
)
|
|
232
|
+
)
|
|
233
|
+
),
|
|
234
|
+
makeError: (fields: JsonRpc.ErrorFields) => new StellarRpcError(fields)
|
|
235
|
+
})
|
|
236
|
+
const { call, fail, withFallback } = rpc
|
|
237
|
+
|
|
238
|
+
// retention is not cached (the oldest ledger moves): getHealth checks it with the head, and the RPC's own
|
|
239
|
+
// out-of-range error on getEvents is mapped back to Pruned or Lagging
|
|
240
|
+
const guard = Effect.fnUntraced(function*(endpoint: Endpoint, fromBlock: bigint, toBlock: bigint) {
|
|
241
|
+
if ((yield* Ref.get(endpoint.head)) >= toBlock) return
|
|
242
|
+
const health = yield* call(endpoint, RpcMethods.getHealth, undefined)
|
|
243
|
+
yield* Ref.update(endpoint.head, BigInt_.max(health.latestLedger))
|
|
244
|
+
const method = RpcMethods.getEvents.name
|
|
245
|
+
if (health.latestLedger < toBlock) {
|
|
246
|
+
return yield* fail(
|
|
247
|
+
endpoint,
|
|
248
|
+
method,
|
|
249
|
+
new JsonRpc.Lagging({ head: health.latestLedger, toBlock }),
|
|
250
|
+
new Error(`latest ledger ${health.latestLedger} is below toBlock ${toBlock}`)
|
|
251
|
+
)
|
|
252
|
+
}
|
|
253
|
+
if (health.oldestLedger > fromBlock) {
|
|
254
|
+
return yield* fail(
|
|
255
|
+
endpoint,
|
|
256
|
+
method,
|
|
257
|
+
new JsonRpc.Pruned(),
|
|
258
|
+
new Error(`oldest ledger ${health.oldestLedger} is above fromBlock ${fromBlock}`)
|
|
259
|
+
)
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
// `start` is lazy: a cursor that failed to encode is not a TOID
|
|
264
|
+
const outOfRange = (error: StellarRpcError, start: () => bigint): StellarRpcError => {
|
|
265
|
+
const match = error.reason._tag === "Rejected" && error.cause instanceof Error
|
|
266
|
+
? ledgerRangeError.exec(error.cause.message)
|
|
267
|
+
: null
|
|
268
|
+
if (match === null) return error
|
|
269
|
+
return new StellarRpcError({
|
|
270
|
+
reason: start() < BigInt(match[1])
|
|
271
|
+
? new JsonRpc.Pruned()
|
|
272
|
+
: new JsonRpc.Lagging({ head: BigInt(match[2]), toBlock: start() }),
|
|
273
|
+
method: error.method,
|
|
274
|
+
endpoint: error.endpoint,
|
|
275
|
+
code: error.code,
|
|
276
|
+
cause: error.cause
|
|
277
|
+
})
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const getEventsOn = Effect.fnUntraced(function*(endpoint: Endpoint, filter: StellarEventsFilter) {
|
|
281
|
+
const { cursor, fromBlock, toBlock } = filter
|
|
282
|
+
const method = RpcMethods.getEvents.name
|
|
283
|
+
const limit = filter.limit ?? 1000
|
|
284
|
+
if (cursor === undefined) yield* guard(endpoint, fromBlock, toBlock)
|
|
285
|
+
const page = yield* call(endpoint, RpcMethods.getEvents, {
|
|
286
|
+
...(cursor === undefined ? { startLedger: fromBlock, endLedger: toBlock + BigInt(1) } : {}),
|
|
287
|
+
filters: [{
|
|
288
|
+
type: "contract",
|
|
289
|
+
contractIds: filter.contractIds,
|
|
290
|
+
...(filter.topics === undefined ? {} : { topics: filter.topics })
|
|
291
|
+
}],
|
|
292
|
+
pagination: { limit, ...(cursor === undefined ? {} : { cursor: cursor.cursor }) }
|
|
293
|
+
}).pipe(
|
|
294
|
+
Effect.mapError((error) =>
|
|
295
|
+
outOfRange(error, () => cursor === undefined ? fromBlock : cursorLedger(cursor.cursor))
|
|
296
|
+
)
|
|
297
|
+
)
|
|
298
|
+
yield* Ref.update(endpoint.head, BigInt_.max(page.latestLedger))
|
|
299
|
+
|
|
300
|
+
const contracts = new Set<string>(filter.contractIds)
|
|
301
|
+
const invalid = page.events.find((event) =>
|
|
302
|
+
event.type !== "contract" || !contracts.has(event.contractId) || event.blockNumber < fromBlock
|
|
303
|
+
)
|
|
304
|
+
if (invalid !== undefined) {
|
|
305
|
+
return yield* fail(
|
|
306
|
+
endpoint,
|
|
307
|
+
method,
|
|
308
|
+
new JsonRpc.InvalidResponse(),
|
|
309
|
+
new Error(`event ${invalid.id} is outside the filter`)
|
|
310
|
+
)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const last = cursorLedger(page.cursor)
|
|
314
|
+
const full = page.events.length >= limit
|
|
315
|
+
if (!full && last < toBlock && last >= page.latestLedger) {
|
|
316
|
+
return yield* fail(
|
|
317
|
+
endpoint,
|
|
318
|
+
method,
|
|
319
|
+
new JsonRpc.Lagging({ head: page.latestLedger, toBlock }),
|
|
320
|
+
new Error(`latest ledger ${page.latestLedger} is below toBlock ${toBlock}`)
|
|
321
|
+
)
|
|
322
|
+
}
|
|
323
|
+
const done = last > toBlock || (!full && last === toBlock)
|
|
324
|
+
return {
|
|
325
|
+
events: Arr.sort(page.events.filter((event) => event.blockNumber <= toBlock), byId),
|
|
326
|
+
cursor: done ? undefined : StellarEventsCursor.make({ endpoint: endpoint.name, cursor: page.cursor })
|
|
327
|
+
}
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
return StellarRpc.of({
|
|
331
|
+
network,
|
|
332
|
+
getNetwork: withFallback(rpc.checkNetwork),
|
|
333
|
+
getLatestLedger: withFallback((endpoint) =>
|
|
334
|
+
call(endpoint, RpcMethods.getLatestLedger, undefined).pipe(
|
|
335
|
+
Effect.tap((ledger) => Ref.update(endpoint.head, BigInt_.max(ledger.sequence)))
|
|
336
|
+
)
|
|
337
|
+
),
|
|
338
|
+
getEvents: (filter) => {
|
|
339
|
+
const { cursor } = filter
|
|
340
|
+
if (filter.fromBlock > filter.toBlock) return Effect.succeed({ events: [], cursor: undefined })
|
|
341
|
+
if (cursor === undefined) return withFallback((endpoint) => getEventsOn(endpoint, filter))
|
|
342
|
+
// a cursor is only valid on the endpoint that issued it
|
|
343
|
+
return rpc.endpointOf(RpcMethods.getEvents.name, cursor).pipe(
|
|
344
|
+
Effect.flatMap((endpoint) => getEventsOn(endpoint, filter))
|
|
345
|
+
)
|
|
346
|
+
}
|
|
347
|
+
})
|
|
348
|
+
})
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Builds an {@link StellarRpc} like {@link layer}, reading the options from `Config`. Endpoint URLs stay `Redacted`.
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```ts
|
|
356
|
+
* import * as StellarRpc from "@effect-blockchain/stellar/StellarRpc"
|
|
357
|
+
* import * as Config from "effect/Config"
|
|
358
|
+
*
|
|
359
|
+
* const RpcLive = StellarRpc.layerConfig({
|
|
360
|
+
* network: Config.succeed("Stellar"),
|
|
361
|
+
* endpoints: Config.all([Config.all({ name: Config.succeed("main"), url: Config.Redacted("STELLAR_RPC_URL") })])
|
|
362
|
+
* })
|
|
363
|
+
* ```
|
|
364
|
+
*
|
|
365
|
+
* @since 0.1.0
|
|
366
|
+
* @category layers
|
|
367
|
+
*/
|
|
368
|
+
export const layerConfig = (
|
|
369
|
+
options: Config.Wrap<Parameters<typeof layer>[0]>
|
|
370
|
+
): Layer.Layer<StellarRpc, Config.ConfigError, HttpClient.HttpClient> =>
|
|
371
|
+
Layer.unwrap(Effect.map(Config.unwrap(options), layer))
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stellar transaction hashes.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.1.0
|
|
5
|
+
*/
|
|
6
|
+
import { identity, pipe } from "effect/Function"
|
|
7
|
+
import * as Schema from "effect/Schema"
|
|
8
|
+
import * as SchemaTransformation from "effect/SchemaTransformation"
|
|
9
|
+
|
|
10
|
+
const stellarTransactionHashRegex = /^0x[a-f0-9]{64}$/u
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A Stellar transaction hash: 32 bytes as lowercase hex.
|
|
14
|
+
*
|
|
15
|
+
* Decoding lower-cases the input and adds a `0x` prefix when missing, so the raw 64-hex form Horizon and RPC return
|
|
16
|
+
* is accepted and normalized to `0x…`. Encoding keeps the `0x` prefix: strip it before querying Stellar APIs.
|
|
17
|
+
*
|
|
18
|
+
* @since 0.1.0
|
|
19
|
+
* @category schemas
|
|
20
|
+
*/
|
|
21
|
+
export const StellarTransactionHash = pipe(
|
|
22
|
+
Schema.String,
|
|
23
|
+
Schema.decodeTo(Schema.String.check(Schema.isLowercased()), SchemaTransformation.toLowerCase()),
|
|
24
|
+
Schema.decodeTo(
|
|
25
|
+
Schema.String,
|
|
26
|
+
SchemaTransformation.transform({ decode: (s: string) => (s.startsWith("0x") ? s : `0x${s}`), encode: identity })
|
|
27
|
+
),
|
|
28
|
+
Schema.check(Schema.isPattern(stellarTransactionHashRegex, { message: "Not a valid Stellar transaction hash" })),
|
|
29
|
+
Schema.brand("StellarTransactionHash"),
|
|
30
|
+
Schema.annotate({
|
|
31
|
+
identifier: "StellarTransactionHash",
|
|
32
|
+
title: "Stellar transaction hash",
|
|
33
|
+
description: "a valid Stellar transaction hash, 0x followed by 64 lowercase hexadecimal characters"
|
|
34
|
+
})
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @since 0.1.0
|
|
39
|
+
* @category models
|
|
40
|
+
*/
|
|
41
|
+
export type StellarTransactionHash = typeof StellarTransactionHash.Type
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 0.1.0
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// @barrel: Auto-generated exports. Do not edit manually.
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @since 0.1.0
|
|
9
|
+
*/
|
|
10
|
+
export * as StellarAddress from "./StellarAddress.ts"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @since 0.1.0
|
|
14
|
+
*/
|
|
15
|
+
export * as StellarContractEvent from "./StellarContractEvent.ts"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @since 0.1.0
|
|
19
|
+
*/
|
|
20
|
+
export * as StellarIndexer from "./StellarIndexer.ts"
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @since 0.1.0
|
|
24
|
+
*/
|
|
25
|
+
export * as StellarNetwork from "./StellarNetwork.ts"
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @since 0.1.0
|
|
29
|
+
*/
|
|
30
|
+
export * as StellarRpc from "./StellarRpc.ts"
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @since 0.1.0
|
|
34
|
+
*/
|
|
35
|
+
export * as StellarTransactionHash from "./StellarTransactionHash.ts"
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as Schema from "effect/Schema"
|
|
2
|
+
import * as SchemaTransformation from "effect/SchemaTransformation"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A ledger sequence, a JSON number on the wire, decoded to a `bigint` like EVM block numbers.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export const LedgerFromNumber = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)).pipe(
|
|
10
|
+
Schema.decodeTo(Schema.BigInt, SchemaTransformation.transform({ decode: BigInt, encode: Number }))
|
|
11
|
+
)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type * as JsonRpc from "@effect-blockchain/core/JsonRpc"
|
|
2
|
+
import * as Schema from "effect/Schema"
|
|
3
|
+
import { StellarContractAddress } from "../StellarAddress.ts"
|
|
4
|
+
import { StellarContractEvent } from "../StellarContractEvent.ts"
|
|
5
|
+
import { LedgerFromNumber } from "./LedgerFromNumber.ts"
|
|
6
|
+
|
|
7
|
+
const make = <Params, A>(method: JsonRpc.Method<Params, A>): JsonRpc.Method<Params, A> => method
|
|
8
|
+
|
|
9
|
+
const NoParams = Schema.Undefined
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* An RPC paging cursor or event id: a 19-digit TOID and a 10-digit event index.
|
|
13
|
+
*
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export const Cursor = Schema.String.check(Schema.isPattern(/^\d{19}-\d{10}$/u))
|
|
17
|
+
|
|
18
|
+
/** @internal */
|
|
19
|
+
export const getNetwork = make({
|
|
20
|
+
name: "getNetwork",
|
|
21
|
+
params: NoParams,
|
|
22
|
+
result: Schema.Struct({ passphrase: Schema.String, protocolVersion: Schema.Int })
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
/** @internal */
|
|
26
|
+
export const getLatestLedger = make({
|
|
27
|
+
name: "getLatestLedger",
|
|
28
|
+
params: NoParams,
|
|
29
|
+
result: Schema.Struct({ id: Schema.String, protocolVersion: Schema.Int, sequence: LedgerFromNumber })
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
/** @internal */
|
|
33
|
+
export const getHealth = make({
|
|
34
|
+
name: "getHealth",
|
|
35
|
+
params: NoParams,
|
|
36
|
+
result: Schema.Struct({ latestLedger: LedgerFromNumber, oldestLedger: LedgerFromNumber })
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
/** @internal */
|
|
40
|
+
export const getEvents = make({
|
|
41
|
+
name: "getEvents",
|
|
42
|
+
params: Schema.Struct({
|
|
43
|
+
// `endLedger` is exclusive, and both bounds must be left out when paging with a cursor
|
|
44
|
+
startLedger: Schema.optionalKey(LedgerFromNumber),
|
|
45
|
+
endLedger: Schema.optionalKey(LedgerFromNumber),
|
|
46
|
+
filters: Schema.Array(Schema.Struct({
|
|
47
|
+
type: Schema.Literal("contract"),
|
|
48
|
+
contractIds: Schema.Array(StellarContractAddress),
|
|
49
|
+
topics: Schema.optionalKey(Schema.Array(Schema.Array(Schema.String)))
|
|
50
|
+
})),
|
|
51
|
+
pagination: Schema.Struct({ cursor: Schema.optionalKey(Cursor), limit: Schema.Int })
|
|
52
|
+
}),
|
|
53
|
+
result: Schema.Struct({ events: Schema.Array(StellarContractEvent), cursor: Cursor, latestLedger: LedgerFromNumber })
|
|
54
|
+
})
|