@effect-blockchain/solana 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/SolanaAddress.d.ts +21 -0
- package/dist/SolanaAddress.d.ts.map +1 -0
- package/dist/SolanaAddress.js +33 -0
- package/dist/SolanaAddress.js.map +1 -0
- package/dist/SolanaIndexer.d.ts +156 -0
- package/dist/SolanaIndexer.d.ts.map +1 -0
- package/dist/SolanaIndexer.js +227 -0
- package/dist/SolanaIndexer.js.map +1 -0
- package/dist/SolanaNetwork.d.ts +64 -0
- package/dist/SolanaNetwork.d.ts.map +1 -0
- package/dist/SolanaNetwork.js +84 -0
- package/dist/SolanaNetwork.js.map +1 -0
- package/dist/SolanaRpc.d.ts +258 -0
- package/dist/SolanaRpc.d.ts.map +1 -0
- package/dist/SolanaRpc.js +154 -0
- package/dist/SolanaRpc.js.map +1 -0
- package/dist/SolanaTransactionHash.d.ts +14 -0
- package/dist/SolanaTransactionHash.d.ts.map +1 -0
- package/dist/SolanaTransactionHash.js +34 -0
- package/dist/SolanaTransactionHash.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/bigIntJson.d.ts +2 -0
- package/dist/internal/bigIntJson.d.ts.map +1 -0
- package/dist/internal/bigIntJson.js +9 -0
- package/dist/internal/bigIntJson.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 +105 -0
- package/dist/internal/rpcMethods.js.map +1 -0
- package/package.json +66 -3
- package/src/SolanaAddress.ts +40 -0
- package/src/SolanaIndexer.ts +376 -0
- package/src/SolanaNetwork.ts +105 -0
- package/src/SolanaRpc.ts +387 -0
- package/src/SolanaTransactionHash.ts +41 -0
- package/src/index.ts +30 -0
- package/src/internal/bigIntJson.ts +18 -0
- package/src/internal/rpcMethods.ts +143 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Named Solana clusters and their genesis hashes.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.1.0
|
|
5
|
+
*/
|
|
6
|
+
import { isBlockhash } from "@solana/kit"
|
|
7
|
+
import * as Schema from "effect/Schema"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @since 0.1.0
|
|
11
|
+
* @category constants
|
|
12
|
+
*/
|
|
13
|
+
export const mainnets = ["Solana"] as const
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @since 0.1.0
|
|
17
|
+
* @category constants
|
|
18
|
+
*/
|
|
19
|
+
export const testnets = ["SolanaDevnet", "SolanaTestnet"] 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
|
+
* A named Solana cluster.
|
|
31
|
+
*
|
|
32
|
+
* @since 0.1.0
|
|
33
|
+
* @category schemas
|
|
34
|
+
*/
|
|
35
|
+
export const SolanaNetwork = Schema.Literals(networks).annotate({
|
|
36
|
+
identifier: "SolanaNetwork",
|
|
37
|
+
title: "Solana network",
|
|
38
|
+
description: "a named Solana cluster: Solana (mainnet-beta), SolanaDevnet or SolanaTestnet"
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @since 0.1.0
|
|
43
|
+
* @category models
|
|
44
|
+
*/
|
|
45
|
+
export type SolanaNetwork = typeof SolanaNetwork.Type
|
|
46
|
+
|
|
47
|
+
// like `SolanaAddress`: always 32 bytes once decoded
|
|
48
|
+
const solanaGenesisHashPattern = "^[2-9A-H][1-9A-HJ-NP-Za-km-z]{43}$"
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A Solana genesis hash: a base58-encoded 32-byte hash, validated with `@solana/kit`.
|
|
52
|
+
*
|
|
53
|
+
* @since 0.1.0
|
|
54
|
+
* @category schemas
|
|
55
|
+
*/
|
|
56
|
+
export const SolanaGenesisHash = Schema.String.annotate({
|
|
57
|
+
title: "Solana genesis hash",
|
|
58
|
+
description: "a valid Solana genesis hash (base58-encoded 32-byte hash)"
|
|
59
|
+
}).pipe(
|
|
60
|
+
Schema.check(
|
|
61
|
+
Schema.makeFilter((s: string) => isBlockhash(s) || `${s} is not a valid Solana genesis hash`, {
|
|
62
|
+
arbitraryConstraint: { patterns: [{ source: solanaGenesisHashPattern, flags: "" }] }
|
|
63
|
+
})
|
|
64
|
+
),
|
|
65
|
+
Schema.brand("SolanaGenesisHash"),
|
|
66
|
+
Schema.annotate({ identifier: "SolanaGenesisHash" })
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @since 0.1.0
|
|
71
|
+
* @category models
|
|
72
|
+
*/
|
|
73
|
+
export type SolanaGenesisHash = typeof SolanaGenesisHash.Type
|
|
74
|
+
|
|
75
|
+
// `--expected-genesis-hash` of each cluster, https://docs.anza.xyz/clusters/available
|
|
76
|
+
const genesisHashes: Record<SolanaNetwork, SolanaGenesisHash> = {
|
|
77
|
+
Solana: SolanaGenesisHash.make("5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d"),
|
|
78
|
+
SolanaDevnet: SolanaGenesisHash.make("EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG"),
|
|
79
|
+
SolanaTestnet: SolanaGenesisHash.make("4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY")
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The cluster's genesis hash, reported by `getGenesisHash`.
|
|
84
|
+
*
|
|
85
|
+
* @since 0.1.0
|
|
86
|
+
* @category conversions
|
|
87
|
+
*/
|
|
88
|
+
export const toGenesisHash = (network: SolanaNetwork): SolanaGenesisHash => genesisHashes[network]
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @since 0.1.0
|
|
92
|
+
* @category predicates
|
|
93
|
+
*/
|
|
94
|
+
export const isTestnet = (network: SolanaNetwork): boolean =>
|
|
95
|
+
(testnets as ReadonlyArray<SolanaNetwork>).includes(network)
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Default number of slots `SolanaIndexer` stays behind the head: 0 on every network. The indexer reads at `finalized`
|
|
99
|
+
* commitment, and a block is finalized once 31+ confirmed blocks are built on top of it
|
|
100
|
+
* (https://docs.anza.xyz/consensus/commitments), so the finalized slot needs no further margin.
|
|
101
|
+
*
|
|
102
|
+
* @since 0.1.0
|
|
103
|
+
* @category getters
|
|
104
|
+
*/
|
|
105
|
+
export const defaultConfirmations = (_network: SolanaNetwork): number => 0
|
package/src/SolanaRpc.ts
ADDED
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed, read-only Solana JSON-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 (`getGenesisHash`). Every method is a read, so retries and fallback
|
|
6
|
+
* are safe. Methods are named after the JSON-RPC method. Every JSON integer in a reply is read as an exact `bigint`,
|
|
7
|
+
* so lamport amounts above 2^53 are never rounded.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { SolanaAddress } from "@effect-blockchain/solana/SolanaAddress"
|
|
12
|
+
* import * as SolanaRpc from "@effect-blockchain/solana/SolanaRpc"
|
|
13
|
+
* import * as Effect from "effect/Effect"
|
|
14
|
+
*
|
|
15
|
+
* declare const address: SolanaAddress
|
|
16
|
+
*
|
|
17
|
+
* // the 10 latest finalized transactions touching `address`
|
|
18
|
+
* const latest = Effect.gen(function*() {
|
|
19
|
+
* const rpc = yield* SolanaRpc.SolanaRpc
|
|
20
|
+
* const signatures = yield* rpc.getSignaturesForAddress({ address, limit: 10, commitment: "finalized" })
|
|
21
|
+
* return yield* Effect.forEach(signatures, ({ signature }) =>
|
|
22
|
+
* rpc.getTransaction({ signature, commitment: "finalized" }))
|
|
23
|
+
* })
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @since 0.1.0
|
|
27
|
+
*/
|
|
28
|
+
import * as JsonRpc from "@effect-blockchain/core/JsonRpc"
|
|
29
|
+
import {
|
|
30
|
+
SOLANA_ERROR__JSON_RPC__INTERNAL_ERROR,
|
|
31
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_CLEANED_UP,
|
|
32
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_NOT_AVAILABLE,
|
|
33
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET,
|
|
34
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX,
|
|
35
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED,
|
|
36
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_LONG_TERM_STORAGE_UNREACHABLE,
|
|
37
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED,
|
|
38
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_NODE_UNHEALTHY,
|
|
39
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SLOT_SKIPPED,
|
|
40
|
+
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE
|
|
41
|
+
} from "@solana/kit"
|
|
42
|
+
import type * as Arr from "effect/Array"
|
|
43
|
+
import * as Config from "effect/Config"
|
|
44
|
+
import * as Context from "effect/Context"
|
|
45
|
+
import type * as Duration from "effect/Duration"
|
|
46
|
+
import * as Effect from "effect/Effect"
|
|
47
|
+
import type * as HttpClient from "effect/http/HttpClient"
|
|
48
|
+
import * as Layer from "effect/Layer"
|
|
49
|
+
import type * as Redacted from "effect/Redacted"
|
|
50
|
+
import * as Schema from "effect/Schema"
|
|
51
|
+
import * as BigIntJson from "./internal/bigIntJson.ts"
|
|
52
|
+
import * as RpcMethods from "./internal/rpcMethods.ts"
|
|
53
|
+
import type { SolanaAddress } from "./SolanaAddress.ts"
|
|
54
|
+
import { type SolanaGenesisHash, type SolanaNetwork, toGenesisHash } from "./SolanaNetwork.ts"
|
|
55
|
+
import type { SolanaTransactionHash } from "./SolanaTransactionHash.ts"
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A JSON-RPC endpoint. The `name` is shown in errors and spans, the `url` is never logged.
|
|
59
|
+
*
|
|
60
|
+
* @since 0.1.0
|
|
61
|
+
* @category models
|
|
62
|
+
*/
|
|
63
|
+
export interface SolanaRpcEndpoint {
|
|
64
|
+
readonly name: string
|
|
65
|
+
readonly url: Redacted.Redacted<string>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A failed call: its `reason` (a {@link JsonRpc.Reason}, matched with `Effect.catchReason`), the JSON-RPC error code or
|
|
70
|
+
* HTTP status as `code`, and a `cause` without the endpoint URL. After several endpoints, the error is the last one
|
|
71
|
+
* and `cause` is an `AggregateError` of each endpoint's error, in order.
|
|
72
|
+
*
|
|
73
|
+
* On Solana, `Unavailable` includes the codes 429, -32603, -32005 and -32019, `Lagging` a `minContextSlot` not reached
|
|
74
|
+
* or a block not available yet, and `Pruned` a cleaned-up or skipped slot or a `getTransaction` returning `null`.
|
|
75
|
+
*
|
|
76
|
+
* @since 0.1.0
|
|
77
|
+
* @category errors
|
|
78
|
+
*/
|
|
79
|
+
export class SolanaRpcError extends Schema.TaggedError<SolanaRpcError>()("SolanaRpcError", {
|
|
80
|
+
reason: JsonRpc.Reason,
|
|
81
|
+
method: Schema.String,
|
|
82
|
+
endpoint: Schema.String,
|
|
83
|
+
code: Schema.optional(Schema.Number),
|
|
84
|
+
cause: Schema.Defect()
|
|
85
|
+
}) {
|
|
86
|
+
/**
|
|
87
|
+
* @since 0.1.0
|
|
88
|
+
*/
|
|
89
|
+
override get message(): string {
|
|
90
|
+
return `${this.method} failed on ${this.endpoint}: ${this.reason._tag}${
|
|
91
|
+
this.code === undefined ? "" : ` (${this.code})`
|
|
92
|
+
}`
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The commitment a read waits for. `processed` is left out: a processed block can still be dropped.
|
|
98
|
+
*
|
|
99
|
+
* @since 0.1.0
|
|
100
|
+
* @category models
|
|
101
|
+
*/
|
|
102
|
+
export type SolanaCommitment = "confirmed" | "finalized"
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* One `getSignaturesForAddress` page: signatures touching `address`, newest first, older than `before` and newer than
|
|
106
|
+
* `until` when given, at most `limit` (the node's default and maximum is 1000).
|
|
107
|
+
*
|
|
108
|
+
* @since 0.1.0
|
|
109
|
+
* @category models
|
|
110
|
+
*/
|
|
111
|
+
export interface SolanaSignaturesQuery {
|
|
112
|
+
readonly address: SolanaAddress
|
|
113
|
+
readonly before?: SolanaTransactionHash | undefined
|
|
114
|
+
readonly until?: SolanaTransactionHash | undefined
|
|
115
|
+
readonly limit?: number | undefined
|
|
116
|
+
readonly commitment: SolanaCommitment
|
|
117
|
+
/**
|
|
118
|
+
* An endpoint that honours it answers `Lagging` until it has reached this slot. Some providers ignore it and answer
|
|
119
|
+
* from wherever they are, so a caller that needs the guarantee checks the head itself, e.g. with `getSlot`.
|
|
120
|
+
*/
|
|
121
|
+
readonly minContextSlot?: bigint | undefined
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* A `getSignaturesForAddress` entry. `blockTime` is a Unix timestamp in seconds, `null` when the node does not know
|
|
126
|
+
* it. `err` is the raw transaction error as JSON (integers as decimal strings), `null` when the transaction succeeded.
|
|
127
|
+
*
|
|
128
|
+
* @since 0.1.0
|
|
129
|
+
* @category models
|
|
130
|
+
*/
|
|
131
|
+
export interface SolanaSignatureInfo {
|
|
132
|
+
readonly signature: SolanaTransactionHash
|
|
133
|
+
readonly slot: bigint
|
|
134
|
+
readonly blockTime: bigint | null
|
|
135
|
+
readonly err: Schema.Json
|
|
136
|
+
readonly memo: string | null
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The `getTransaction` encodings. `base64` is the signed wire transaction, for the `@solana/kit` decoders (e.g.
|
|
141
|
+
* `getTransactionDecoder`) and for storing the exact bytes. `json` splits it into signatures, account keys and compiled
|
|
142
|
+
* instructions; `jsonParsed` is not offered, as its shape depends on the programs the node knows.
|
|
143
|
+
*
|
|
144
|
+
* @since 0.1.0
|
|
145
|
+
* @category models
|
|
146
|
+
*/
|
|
147
|
+
export type SolanaTransactionEncoding = "base64" | "json"
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A compiled instruction of a `json` transaction: `programIdIndex` and `accounts` index the message's account keys,
|
|
151
|
+
* followed by the addresses loaded from the lookup tables (writable then readonly), and `data` is base58.
|
|
152
|
+
*
|
|
153
|
+
* @since 0.1.0
|
|
154
|
+
* @category models
|
|
155
|
+
*/
|
|
156
|
+
export interface SolanaCompiledInstruction {
|
|
157
|
+
readonly programIdIndex: number
|
|
158
|
+
readonly accounts: ReadonlyArray<number>
|
|
159
|
+
readonly data: string
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* A `json` transaction. `addressTableLookups` is absent from legacy transactions.
|
|
164
|
+
*
|
|
165
|
+
* @since 0.1.0
|
|
166
|
+
* @category models
|
|
167
|
+
*/
|
|
168
|
+
export interface SolanaJsonTransaction {
|
|
169
|
+
readonly signatures: ReadonlyArray<SolanaTransactionHash>
|
|
170
|
+
readonly message: {
|
|
171
|
+
readonly accountKeys: ReadonlyArray<SolanaAddress>
|
|
172
|
+
readonly header: {
|
|
173
|
+
readonly numRequiredSignatures: number
|
|
174
|
+
readonly numReadonlySignedAccounts: number
|
|
175
|
+
readonly numReadonlyUnsignedAccounts: number
|
|
176
|
+
}
|
|
177
|
+
readonly recentBlockhash: string
|
|
178
|
+
readonly instructions: ReadonlyArray<SolanaCompiledInstruction>
|
|
179
|
+
readonly addressTableLookups?: ReadonlyArray<{
|
|
180
|
+
readonly accountKey: SolanaAddress
|
|
181
|
+
readonly writableIndexes: ReadonlyArray<number>
|
|
182
|
+
readonly readonlyIndexes: ReadonlyArray<number>
|
|
183
|
+
}>
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The `transaction` of a `getTransaction` result, by encoding: the base64 wire transaction or a
|
|
189
|
+
* {@link SolanaJsonTransaction}.
|
|
190
|
+
*
|
|
191
|
+
* @since 0.1.0
|
|
192
|
+
* @category models
|
|
193
|
+
*/
|
|
194
|
+
export interface SolanaEncodedTransaction {
|
|
195
|
+
readonly base64: string
|
|
196
|
+
readonly json: SolanaJsonTransaction
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* A `getTransaction` result. `transaction` follows the requested encoding (see {@link SolanaEncodedTransaction}).
|
|
201
|
+
* `meta` is the raw status meta (fee, balances, logs, inner instructions...) as JSON with every integer as a decimal
|
|
202
|
+
* string, so u64 values are exact and the value can be stored as is. It is `null` when the node does not have it.
|
|
203
|
+
*
|
|
204
|
+
* @since 0.1.0
|
|
205
|
+
* @category models
|
|
206
|
+
*/
|
|
207
|
+
export interface SolanaTransactionInfo<Encoding extends SolanaTransactionEncoding = "base64"> {
|
|
208
|
+
readonly slot: bigint
|
|
209
|
+
readonly blockTime: bigint | null
|
|
210
|
+
readonly transaction: SolanaEncodedTransaction[Encoding]
|
|
211
|
+
readonly meta: Schema.Json
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The `getTransaction` options.
|
|
216
|
+
*
|
|
217
|
+
* @since 0.1.0
|
|
218
|
+
* @category models
|
|
219
|
+
*/
|
|
220
|
+
export interface SolanaTransactionQuery {
|
|
221
|
+
readonly signature: SolanaTransactionHash
|
|
222
|
+
readonly commitment: SolanaCommitment
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @since 0.1.0
|
|
227
|
+
* @category services
|
|
228
|
+
*/
|
|
229
|
+
export class SolanaRpc extends Context.Service<SolanaRpc, {
|
|
230
|
+
readonly network: SolanaNetwork
|
|
231
|
+
/** `getGenesisHash`, verified against `network` once per endpoint then served from that check. */
|
|
232
|
+
readonly getGenesisHash: Effect.Effect<SolanaGenesisHash, SolanaRpcError>
|
|
233
|
+
/** `getSlot` */
|
|
234
|
+
readonly getSlot: (options: {
|
|
235
|
+
readonly commitment: SolanaCommitment
|
|
236
|
+
}) => Effect.Effect<bigint, SolanaRpcError>
|
|
237
|
+
/** `getSignaturesForAddress`, one page. */
|
|
238
|
+
readonly getSignaturesForAddress: (
|
|
239
|
+
query: SolanaSignaturesQuery
|
|
240
|
+
) => Effect.Effect<ReadonlyArray<SolanaSignatureInfo>, SolanaRpcError>
|
|
241
|
+
/**
|
|
242
|
+
* `getTransaction` in `encoding`, `base64` by default, versioned transactions included. A transaction the endpoint
|
|
243
|
+
* lacks fails `Pruned`.
|
|
244
|
+
*/
|
|
245
|
+
readonly getTransaction: {
|
|
246
|
+
(
|
|
247
|
+
options: SolanaTransactionQuery & { readonly encoding?: "base64" | undefined }
|
|
248
|
+
): Effect.Effect<SolanaTransactionInfo, SolanaRpcError>
|
|
249
|
+
<Encoding extends SolanaTransactionEncoding>(
|
|
250
|
+
options: SolanaTransactionQuery & { readonly encoding: Encoding }
|
|
251
|
+
): Effect.Effect<SolanaTransactionInfo<Encoding>, SolanaRpcError>
|
|
252
|
+
}
|
|
253
|
+
}>()("@effect-blockchain/solana/SolanaRpc") {}
|
|
254
|
+
|
|
255
|
+
// JSON-RPC codes from @solana/errors, any other code is `Rejected`
|
|
256
|
+
const codes: ReadonlyMap<number, JsonRpc.CodeReason> = new Map([
|
|
257
|
+
[429, "Unavailable"],
|
|
258
|
+
[SOLANA_ERROR__JSON_RPC__INTERNAL_ERROR, "Unavailable"],
|
|
259
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_NODE_UNHEALTHY, "Unavailable"],
|
|
260
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_LONG_TERM_STORAGE_UNREACHABLE, "Unavailable"],
|
|
261
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED, "Lagging"],
|
|
262
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_NOT_AVAILABLE, "Lagging"],
|
|
263
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET, "Lagging"],
|
|
264
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_CLEANED_UP, "Pruned"],
|
|
265
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SLOT_SKIPPED, "Pruned"],
|
|
266
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED, "Pruned"],
|
|
267
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE, "Pruned"],
|
|
268
|
+
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX, "Pruned"]
|
|
269
|
+
])
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Builds a {@link SolanaRpc} over `endpoints`, tried in order, sending requests with the provided `HttpClient`
|
|
273
|
+
* (e.g. `FetchHttpClient.layer`). `timeout` (default 30 seconds) bounds each attempt, `retries` (default 3) and
|
|
274
|
+
* `retryDelay` (default 500 millis) set the backoff before falling back: see {@link JsonRpc.Unavailable}.
|
|
275
|
+
*
|
|
276
|
+
* @since 0.1.0
|
|
277
|
+
* @category layers
|
|
278
|
+
*/
|
|
279
|
+
export const layer = (options: {
|
|
280
|
+
readonly network: SolanaNetwork
|
|
281
|
+
readonly endpoints: Arr.NonEmptyReadonlyArray<SolanaRpcEndpoint>
|
|
282
|
+
readonly timeout?: Duration.Input | undefined
|
|
283
|
+
readonly retries?: number | undefined
|
|
284
|
+
readonly retryDelay?: Duration.Input | undefined
|
|
285
|
+
}): Layer.Layer<SolanaRpc, never, HttpClient.HttpClient> =>
|
|
286
|
+
Layer.effect(
|
|
287
|
+
SolanaRpc,
|
|
288
|
+
Effect.gen(function*() {
|
|
289
|
+
const { network } = options
|
|
290
|
+
const expectedGenesisHash = toGenesisHash(network)
|
|
291
|
+
const rpc = yield* JsonRpc.make({
|
|
292
|
+
spanPrefix: "SolanaRpc",
|
|
293
|
+
network,
|
|
294
|
+
endpoints: options.endpoints,
|
|
295
|
+
timeout: options.timeout,
|
|
296
|
+
retries: options.retries,
|
|
297
|
+
retryDelay: options.retryDelay,
|
|
298
|
+
codes,
|
|
299
|
+
parseBody: BigIntJson.parse,
|
|
300
|
+
checkNetwork: (send, endpoint) =>
|
|
301
|
+
send(RpcMethods.getGenesisHash, []).pipe(
|
|
302
|
+
Effect.flatMap((genesisHash) =>
|
|
303
|
+
genesisHash === expectedGenesisHash
|
|
304
|
+
? Effect.succeed(expectedGenesisHash)
|
|
305
|
+
: Effect.fail(
|
|
306
|
+
new SolanaRpcError({
|
|
307
|
+
reason: new JsonRpc.WrongNetwork({ expected: expectedGenesisHash, actual: genesisHash }),
|
|
308
|
+
method: RpcMethods.getGenesisHash.name,
|
|
309
|
+
endpoint,
|
|
310
|
+
cause: new Error(`expected genesis hash ${expectedGenesisHash} for ${network}, got ${genesisHash}`)
|
|
311
|
+
})
|
|
312
|
+
)
|
|
313
|
+
)
|
|
314
|
+
),
|
|
315
|
+
makeError: (fields: JsonRpc.ErrorFields) => new SolanaRpcError(fields)
|
|
316
|
+
})
|
|
317
|
+
type Endpoint = JsonRpc.Endpoint<SolanaGenesisHash, SolanaRpcError>
|
|
318
|
+
|
|
319
|
+
const getTransactionOn = (
|
|
320
|
+
endpoint: Endpoint,
|
|
321
|
+
{ commitment, encoding, signature }: SolanaTransactionQuery & { readonly encoding: SolanaTransactionEncoding }
|
|
322
|
+
) =>
|
|
323
|
+
rpc.call(endpoint, RpcMethods.getTransaction(encoding), [signature, {
|
|
324
|
+
commitment,
|
|
325
|
+
encoding,
|
|
326
|
+
maxSupportedTransactionVersion: 0
|
|
327
|
+
}]).pipe(
|
|
328
|
+
Effect.flatMap((tx) =>
|
|
329
|
+
tx === null
|
|
330
|
+
? Effect.fail(
|
|
331
|
+
rpc.fail(
|
|
332
|
+
endpoint,
|
|
333
|
+
RpcMethods.getTransaction.name,
|
|
334
|
+
new JsonRpc.Pruned(),
|
|
335
|
+
new Error(`transaction ${signature} not found`)
|
|
336
|
+
)
|
|
337
|
+
)
|
|
338
|
+
: Effect.succeed(tx)
|
|
339
|
+
)
|
|
340
|
+
)
|
|
341
|
+
function getTransaction(
|
|
342
|
+
options: SolanaTransactionQuery & { readonly encoding?: "base64" | undefined }
|
|
343
|
+
): Effect.Effect<SolanaTransactionInfo, SolanaRpcError>
|
|
344
|
+
function getTransaction<Encoding extends SolanaTransactionEncoding>(
|
|
345
|
+
options: SolanaTransactionQuery & { readonly encoding: Encoding }
|
|
346
|
+
): Effect.Effect<SolanaTransactionInfo<Encoding>, SolanaRpcError>
|
|
347
|
+
function getTransaction(
|
|
348
|
+
{ encoding = "base64", ...options }: SolanaTransactionQuery & {
|
|
349
|
+
readonly encoding?: SolanaTransactionEncoding | undefined
|
|
350
|
+
}
|
|
351
|
+
): Effect.Effect<SolanaTransactionInfo<SolanaTransactionEncoding>, SolanaRpcError> {
|
|
352
|
+
return rpc.withFallback((endpoint) => getTransactionOn(endpoint, { ...options, encoding }))
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return SolanaRpc.of({
|
|
356
|
+
network,
|
|
357
|
+
getGenesisHash: rpc.withFallback(rpc.checkNetwork),
|
|
358
|
+
getSlot: ({ commitment }) =>
|
|
359
|
+
rpc.withFallback((endpoint) => rpc.call(endpoint, RpcMethods.getSlot, [{ commitment }])),
|
|
360
|
+
getSignaturesForAddress: ({ address, ...config }) =>
|
|
361
|
+
rpc.withFallback((endpoint) => rpc.call(endpoint, RpcMethods.getSignaturesForAddress, [address, config])),
|
|
362
|
+
getTransaction
|
|
363
|
+
})
|
|
364
|
+
})
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Builds an {@link SolanaRpc} like {@link layer}, reading the options from `Config`. Endpoint URLs stay `Redacted`.
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* ```ts
|
|
372
|
+
* import * as SolanaRpc from "@effect-blockchain/solana/SolanaRpc"
|
|
373
|
+
* import * as Config from "effect/Config"
|
|
374
|
+
*
|
|
375
|
+
* const RpcLive = SolanaRpc.layerConfig({
|
|
376
|
+
* network: Config.succeed("Solana"),
|
|
377
|
+
* endpoints: Config.all([Config.all({ name: Config.succeed("main"), url: Config.Redacted("SOLANA_RPC_URL") })])
|
|
378
|
+
* })
|
|
379
|
+
* ```
|
|
380
|
+
*
|
|
381
|
+
* @since 0.1.0
|
|
382
|
+
* @category layers
|
|
383
|
+
*/
|
|
384
|
+
export const layerConfig = (
|
|
385
|
+
options: Config.Wrap<Parameters<typeof layer>[0]>
|
|
386
|
+
): Layer.Layer<SolanaRpc, Config.ConfigError, HttpClient.HttpClient> =>
|
|
387
|
+
Layer.unwrap(Effect.map(Config.unwrap(options), layer))
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 0.1.0
|
|
3
|
+
*/
|
|
4
|
+
import { isSignature } from "@solana/kit"
|
|
5
|
+
import * as Schema from "effect/Schema"
|
|
6
|
+
|
|
7
|
+
const solanaTransactionHashPattern = "^[1-9A-HJ-NP-Za-km-z]{87}$"
|
|
8
|
+
|
|
9
|
+
// `isSignature` from @solana/kit throws on non-base58 characters instead of returning false.
|
|
10
|
+
const isSolanaSignature = (value: string): boolean => {
|
|
11
|
+
try {
|
|
12
|
+
return isSignature(value)
|
|
13
|
+
} catch {
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A Solana transaction hash: a base58-encoded Ed25519 signature, validated with `@solana/kit`.
|
|
20
|
+
*
|
|
21
|
+
* @since 0.1.0
|
|
22
|
+
* @category schemas
|
|
23
|
+
*/
|
|
24
|
+
export const SolanaTransactionHash = Schema.String.annotate({
|
|
25
|
+
title: "Solana transaction hash",
|
|
26
|
+
description: "a valid Solana transaction hash (base58-encoded Ed25519 signature)"
|
|
27
|
+
}).pipe(
|
|
28
|
+
Schema.check(
|
|
29
|
+
Schema.makeFilter((s: string) => isSolanaSignature(s) || `${s} is not a valid Solana transaction hash`, {
|
|
30
|
+
arbitraryConstraint: { patterns: [{ source: solanaTransactionHashPattern, flags: "" }] }
|
|
31
|
+
})
|
|
32
|
+
),
|
|
33
|
+
Schema.brand("SolanaTransactionHash"),
|
|
34
|
+
Schema.annotate({ identifier: "SolanaTransactionHash" })
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @since 0.1.0
|
|
39
|
+
* @category models
|
|
40
|
+
*/
|
|
41
|
+
export type SolanaTransactionHash = typeof SolanaTransactionHash.Type
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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 SolanaAddress from "./SolanaAddress.ts"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @since 0.1.0
|
|
14
|
+
*/
|
|
15
|
+
export * as SolanaIndexer from "./SolanaIndexer.ts"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @since 0.1.0
|
|
19
|
+
*/
|
|
20
|
+
export * as SolanaNetwork from "./SolanaNetwork.ts"
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @since 0.1.0
|
|
24
|
+
*/
|
|
25
|
+
export * as SolanaRpc from "./SolanaRpc.ts"
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @since 0.1.0
|
|
29
|
+
*/
|
|
30
|
+
export * as SolanaTransactionHash from "./SolanaTransactionHash.ts"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { RpcJson } from "./rpcMethods.ts"
|
|
2
|
+
|
|
3
|
+
const integer = /^-?\d+$/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parses JSON with every integer literal as an exact `bigint`, like `@solana/kit` does: u64 values above 2^53 are
|
|
7
|
+
* never rounded. Reads the literal's source text through the reviver's third argument (Node >= 24).
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export const parse = (text: string): RpcJson =>
|
|
12
|
+
JSON.parse(
|
|
13
|
+
text,
|
|
14
|
+
(_key, value: RpcJson, context?: { readonly source?: string }) =>
|
|
15
|
+
typeof value === "number" && context?.source !== undefined && integer.test(context.source)
|
|
16
|
+
? BigInt(context.source)
|
|
17
|
+
: value
|
|
18
|
+
)
|