@effect-blockchain/solana 0.1.0 → 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/dist/SolanaIndexer.d.ts +45 -3
- package/dist/SolanaIndexer.d.ts.map +1 -1
- package/dist/SolanaIndexer.js +138 -21
- package/dist/SolanaIndexer.js.map +1 -1
- package/dist/SolanaRelayer.d.ts +207 -0
- package/dist/SolanaRelayer.d.ts.map +1 -0
- package/dist/SolanaRelayer.js +377 -0
- package/dist/SolanaRelayer.js.map +1 -0
- package/dist/SolanaRpc.d.ts +176 -7
- package/dist/SolanaRpc.d.ts.map +1 -1
- package/dist/SolanaRpc.js +68 -6
- package/dist/SolanaRpc.js.map +1 -1
- package/dist/SolanaSigner.d.ts +49 -0
- package/dist/SolanaSigner.d.ts.map +1 -0
- package/dist/SolanaSigner.js +82 -0
- package/dist/SolanaSigner.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/confirmations.d.ts +2 -0
- package/dist/internal/confirmations.d.ts.map +1 -0
- package/dist/internal/confirmations.js +15 -0
- package/dist/internal/confirmations.js.map +1 -0
- package/dist/internal/rpcMethods.js +119 -1
- package/dist/internal/rpcMethods.js.map +1 -1
- package/package.json +1 -1
- package/src/SolanaIndexer.ts +210 -34
- package/src/SolanaRelayer.ts +493 -0
- package/src/SolanaRpc.ts +217 -9
- package/src/SolanaSigner.ts +98 -0
- package/src/index.ts +10 -0
- package/src/internal/confirmations.ts +17 -0
- package/src/internal/rpcMethods.ts +118 -1
package/src/SolanaRpc.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Typed
|
|
2
|
+
* Typed Solana JSON-RPC methods over an ordered list of endpoints with fallback.
|
|
3
3
|
*
|
|
4
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,
|
|
6
|
-
* are safe. Methods are named after the JSON-RPC
|
|
7
|
-
* so lamport amounts above 2^53 are never rounded.
|
|
5
|
+
* is checked once to serve the configured network (`getGenesisHash`). Every method but `sendTransaction` is a read,
|
|
6
|
+
* so retries and fallback are safe; `sendTransaction` documents its own case. Methods are named after the JSON-RPC
|
|
7
|
+
* method. Every JSON integer in a reply is read as an exact `bigint`, so lamport amounts above 2^53 are never rounded.
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* ```ts
|
|
@@ -196,10 +196,28 @@ export interface SolanaEncodedTransaction {
|
|
|
196
196
|
readonly json: SolanaJsonTransaction
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
+
/**
|
|
200
|
+
* The status meta of a transaction, reduced to its outcome and lamport balances. `err` is the raw transaction error as
|
|
201
|
+
* JSON (integers as decimal strings), `null` when it succeeded. `preBalances` and `postBalances` follow the account
|
|
202
|
+
* keys, then `loadedAddresses` (absent from transactions older than versioned transactions).
|
|
203
|
+
*
|
|
204
|
+
* @since 0.2.0
|
|
205
|
+
* @category models
|
|
206
|
+
*/
|
|
207
|
+
export interface SolanaTransactionMeta {
|
|
208
|
+
readonly err: Schema.Json
|
|
209
|
+
readonly fee: bigint
|
|
210
|
+
readonly preBalances: ReadonlyArray<bigint>
|
|
211
|
+
readonly postBalances: ReadonlyArray<bigint>
|
|
212
|
+
readonly loadedAddresses?: {
|
|
213
|
+
readonly writable: ReadonlyArray<SolanaAddress>
|
|
214
|
+
readonly readonly: ReadonlyArray<SolanaAddress>
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
199
218
|
/**
|
|
200
219
|
* A `getTransaction` result. `transaction` follows the requested encoding (see {@link SolanaEncodedTransaction}).
|
|
201
|
-
* `meta` is
|
|
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.
|
|
220
|
+
* `meta` is `null` when the node does not have it.
|
|
203
221
|
*
|
|
204
222
|
* @since 0.1.0
|
|
205
223
|
* @category models
|
|
@@ -208,7 +226,108 @@ export interface SolanaTransactionInfo<Encoding extends SolanaTransactionEncodin
|
|
|
208
226
|
readonly slot: bigint
|
|
209
227
|
readonly blockTime: bigint | null
|
|
210
228
|
readonly transaction: SolanaEncodedTransaction[Encoding]
|
|
211
|
-
readonly meta:
|
|
229
|
+
readonly meta: SolanaTransactionMeta | null
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* A `getBalance` result: the lamports of the account (`value`) at the context `slot`.
|
|
234
|
+
*
|
|
235
|
+
* @since 0.2.0
|
|
236
|
+
* @category models
|
|
237
|
+
*/
|
|
238
|
+
export interface SolanaBalance {
|
|
239
|
+
readonly slot: bigint
|
|
240
|
+
readonly value: bigint
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Why an instruction failed, as the node returns it: the variant's name (`"ProgramFailedToComplete"`), the program's
|
|
245
|
+
* own error code (`{ Custom: 6001 }`), or any other variant carrying data as raw JSON (integers as decimal strings).
|
|
246
|
+
*
|
|
247
|
+
* @since 0.2.0
|
|
248
|
+
* @category models
|
|
249
|
+
*/
|
|
250
|
+
export type SolanaInstructionError =
|
|
251
|
+
| string
|
|
252
|
+
| { readonly Custom: number }
|
|
253
|
+
| { readonly [variant: string]: Schema.Json }
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Why a transaction failed, as the node returns it: the variant's name (`"BlockhashNotFound"`), the failing
|
|
257
|
+
* instruction's index and error (`{ InstructionError: [2, { Custom: 6001 }] }`), or any other variant carrying data
|
|
258
|
+
* as raw JSON (integers as decimal strings).
|
|
259
|
+
*
|
|
260
|
+
* @since 0.2.0
|
|
261
|
+
* @category models
|
|
262
|
+
*/
|
|
263
|
+
export type SolanaTransactionError =
|
|
264
|
+
| string
|
|
265
|
+
| { readonly InstructionError: readonly [index: number, error: SolanaInstructionError] }
|
|
266
|
+
| { readonly [variant: string]: Schema.Json }
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* A `getSignatureStatuses` entry: the `slot` holding the transaction, its `err` (`null` when it succeeded) and the
|
|
270
|
+
* commitment its block has reached. `confirmations` counts the blocks built on it, `null` once it is finalized.
|
|
271
|
+
*
|
|
272
|
+
* @since 0.2.0
|
|
273
|
+
* @category models
|
|
274
|
+
*/
|
|
275
|
+
export interface SolanaSignatureStatus {
|
|
276
|
+
readonly slot: bigint
|
|
277
|
+
readonly confirmations: number | null
|
|
278
|
+
readonly err: SolanaTransactionError | null
|
|
279
|
+
readonly confirmationStatus: "processed" | "confirmed" | "finalized" | null
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* A `getSignatureStatuses` result: one entry of `value` per signature, in order, `null` for a signature the endpoint
|
|
284
|
+
* does not know, as seen at the context `slot`.
|
|
285
|
+
*
|
|
286
|
+
* @since 0.2.0
|
|
287
|
+
* @category models
|
|
288
|
+
*/
|
|
289
|
+
export interface SolanaSignatureStatuses {
|
|
290
|
+
readonly slot: bigint
|
|
291
|
+
readonly value: ReadonlyArray<SolanaSignatureStatus | null>
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* A `getLatestBlockhash` result at the context `slot`: a transaction signed with `blockhash` can land while the block
|
|
296
|
+
* height is at most `lastValidBlockHeight`.
|
|
297
|
+
*
|
|
298
|
+
* @since 0.2.0
|
|
299
|
+
* @category models
|
|
300
|
+
*/
|
|
301
|
+
export interface SolanaLatestBlockhash {
|
|
302
|
+
readonly slot: bigint
|
|
303
|
+
readonly blockhash: string
|
|
304
|
+
readonly lastValidBlockHeight: bigint
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* A `simulateTransaction` result at the context `slot`: `err` is `null` when the transaction would succeed, `logs` the
|
|
309
|
+
* program logs and `unitsConsumed` the compute units used, both `null` when the simulation did not run.
|
|
310
|
+
*
|
|
311
|
+
* @since 0.2.0
|
|
312
|
+
* @category models
|
|
313
|
+
*/
|
|
314
|
+
export interface SolanaSimulation {
|
|
315
|
+
readonly slot: bigint
|
|
316
|
+
readonly err: SolanaTransactionError | null
|
|
317
|
+
readonly logs: ReadonlyArray<string> | null
|
|
318
|
+
readonly unitsConsumed: bigint | null
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* A `getRecentPrioritizationFees` entry: the lowest compute unit price, in micro-lamports, of a transaction that
|
|
323
|
+
* landed in `slot` and locked the queried accounts. It is 0 for a slot where such a transaction paid none.
|
|
324
|
+
*
|
|
325
|
+
* @since 0.2.0
|
|
326
|
+
* @category models
|
|
327
|
+
*/
|
|
328
|
+
export interface SolanaPrioritizationFee {
|
|
329
|
+
readonly slot: bigint
|
|
330
|
+
readonly prioritizationFee: bigint
|
|
212
331
|
}
|
|
213
332
|
|
|
214
333
|
/**
|
|
@@ -234,6 +353,23 @@ export class SolanaRpc extends Context.Service<SolanaRpc, {
|
|
|
234
353
|
readonly getSlot: (options: {
|
|
235
354
|
readonly commitment: SolanaCommitment
|
|
236
355
|
}) => Effect.Effect<bigint, SolanaRpcError>
|
|
356
|
+
/** `getBlockHeight`: the number of blocks below the head, lower than its slot as skipped slots hold no block. */
|
|
357
|
+
readonly getBlockHeight: (options: {
|
|
358
|
+
readonly commitment: SolanaCommitment
|
|
359
|
+
}) => Effect.Effect<bigint, SolanaRpcError>
|
|
360
|
+
/**
|
|
361
|
+
* `getSignatureStatuses` of at most 256 `signatures`, read from the blocks the node has processed, whatever their
|
|
362
|
+
* commitment. Without `searchTransactionHistory`, only the node's cache of recent slots is searched: an older
|
|
363
|
+
* transaction is `null`.
|
|
364
|
+
*/
|
|
365
|
+
readonly getSignatureStatuses: (options: {
|
|
366
|
+
readonly signatures: ReadonlyArray<SolanaTransactionHash>
|
|
367
|
+
readonly searchTransactionHistory?: boolean | undefined
|
|
368
|
+
}) => Effect.Effect<SolanaSignatureStatuses, SolanaRpcError>
|
|
369
|
+
/** `getBalance` at `finalized` commitment. */
|
|
370
|
+
readonly getBalance: (options: {
|
|
371
|
+
readonly address: SolanaAddress
|
|
372
|
+
}) => Effect.Effect<SolanaBalance, SolanaRpcError>
|
|
237
373
|
/** `getSignaturesForAddress`, one page. */
|
|
238
374
|
readonly getSignaturesForAddress: (
|
|
239
375
|
query: SolanaSignaturesQuery
|
|
@@ -250,6 +386,43 @@ export class SolanaRpc extends Context.Service<SolanaRpc, {
|
|
|
250
386
|
options: SolanaTransactionQuery & { readonly encoding: Encoding }
|
|
251
387
|
): Effect.Effect<SolanaTransactionInfo<Encoding>, SolanaRpcError>
|
|
252
388
|
}
|
|
389
|
+
/** `getLatestBlockhash` */
|
|
390
|
+
readonly getLatestBlockhash: (options: {
|
|
391
|
+
readonly commitment: SolanaCommitment
|
|
392
|
+
}) => Effect.Effect<SolanaLatestBlockhash, SolanaRpcError>
|
|
393
|
+
/**
|
|
394
|
+
* `simulateTransaction` of a base64 wire `transaction`, run against the bank at `commitment` without being sent. A
|
|
395
|
+
* transaction that would fail is a success whose `err` says why. `sigVerify` checks the signatures, and
|
|
396
|
+
* `replaceRecentBlockhash` swaps the blockhash for the latest one, which unsigned transactions need: the node
|
|
397
|
+
* refuses both together.
|
|
398
|
+
*/
|
|
399
|
+
readonly simulateTransaction: (options: {
|
|
400
|
+
readonly transaction: string
|
|
401
|
+
readonly commitment: SolanaCommitment
|
|
402
|
+
readonly sigVerify?: boolean | undefined
|
|
403
|
+
readonly replaceRecentBlockhash?: boolean | undefined
|
|
404
|
+
}) => Effect.Effect<SolanaSimulation, SolanaRpcError>
|
|
405
|
+
/**
|
|
406
|
+
* `getRecentPrioritizationFees` over the node's cache of recent slots (up to 150) for the transactions locking every
|
|
407
|
+
* one of `addresses` as writable, at most 128 of them. Without `addresses`, any transaction counts.
|
|
408
|
+
*/
|
|
409
|
+
readonly getRecentPrioritizationFees: (options: {
|
|
410
|
+
readonly addresses: ReadonlyArray<SolanaAddress>
|
|
411
|
+
}) => Effect.Effect<ReadonlyArray<SolanaPrioritizationFee>, SolanaRpcError>
|
|
412
|
+
/**
|
|
413
|
+
* `sendTransaction`: submits a signed base64 wire `transaction` and returns its first signature. The node simulates
|
|
414
|
+
* it first at `preflightCommitment` (`finalized` by default) unless `skipPreflight`, then forwards it to the leader
|
|
415
|
+
* and rebroadcasts it `maxRetries` times, or until its blockhash expires when left out. A retry or a fallback sends
|
|
416
|
+
* the same bytes, so it never creates a second transaction, but a node that has processed them refuses the second
|
|
417
|
+
* copy. A refusal (a failed preflight, `BlockhashNotFound`, `AlreadyProcessed`...) is `Rejected`, with the node's
|
|
418
|
+
* message in `cause`.
|
|
419
|
+
*/
|
|
420
|
+
readonly sendTransaction: (options: {
|
|
421
|
+
readonly transaction: string
|
|
422
|
+
readonly skipPreflight?: boolean | undefined
|
|
423
|
+
readonly preflightCommitment?: SolanaCommitment | undefined
|
|
424
|
+
readonly maxRetries?: number | undefined
|
|
425
|
+
}) => Effect.Effect<SolanaTransactionHash, SolanaRpcError>
|
|
253
426
|
}>()("@effect-blockchain/solana/SolanaRpc") {}
|
|
254
427
|
|
|
255
428
|
// JSON-RPC codes from @solana/errors, any other code is `Rejected`
|
|
@@ -272,12 +445,15 @@ const codes: ReadonlyMap<number, JsonRpc.CodeReason> = new Map([
|
|
|
272
445
|
* Builds a {@link SolanaRpc} over `endpoints`, tried in order, sending requests with the provided `HttpClient`
|
|
273
446
|
* (e.g. `FetchHttpClient.layer`). `timeout` (default 30 seconds) bounds each attempt, `retries` (default 3) and
|
|
274
447
|
* `retryDelay` (default 500 millis) set the backoff before falling back: see {@link JsonRpc.Unavailable}.
|
|
448
|
+
* `genesisHash` replaces the genesis hash of `network`, for a local validator or a private cluster, which have their
|
|
449
|
+
* own.
|
|
275
450
|
*
|
|
276
451
|
* @since 0.1.0
|
|
277
452
|
* @category layers
|
|
278
453
|
*/
|
|
279
454
|
export const layer = (options: {
|
|
280
455
|
readonly network: SolanaNetwork
|
|
456
|
+
readonly genesisHash?: SolanaGenesisHash | undefined
|
|
281
457
|
readonly endpoints: Arr.NonEmptyReadonlyArray<SolanaRpcEndpoint>
|
|
282
458
|
readonly timeout?: Duration.Input | undefined
|
|
283
459
|
readonly retries?: number | undefined
|
|
@@ -287,7 +463,7 @@ export const layer = (options: {
|
|
|
287
463
|
SolanaRpc,
|
|
288
464
|
Effect.gen(function*() {
|
|
289
465
|
const { network } = options
|
|
290
|
-
const expectedGenesisHash = toGenesisHash(network)
|
|
466
|
+
const expectedGenesisHash = options.genesisHash ?? toGenesisHash(network)
|
|
291
467
|
const rpc = yield* JsonRpc.make({
|
|
292
468
|
spanPrefix: "SolanaRpc",
|
|
293
469
|
network,
|
|
@@ -357,9 +533,41 @@ export const layer = (options: {
|
|
|
357
533
|
getGenesisHash: rpc.withFallback(rpc.checkNetwork),
|
|
358
534
|
getSlot: ({ commitment }) =>
|
|
359
535
|
rpc.withFallback((endpoint) => rpc.call(endpoint, RpcMethods.getSlot, [{ commitment }])),
|
|
536
|
+
getBlockHeight: ({ commitment }) =>
|
|
537
|
+
rpc.withFallback((endpoint) => rpc.call(endpoint, RpcMethods.getBlockHeight, [{ commitment }])),
|
|
538
|
+
getSignatureStatuses: ({ signatures, ...config }) =>
|
|
539
|
+
rpc.withFallback((endpoint) =>
|
|
540
|
+
rpc.call(endpoint, RpcMethods.getSignatureStatuses, [signatures, config]).pipe(
|
|
541
|
+
Effect.map(({ context, value }) => ({ slot: context.slot, value }))
|
|
542
|
+
)
|
|
543
|
+
),
|
|
544
|
+
getBalance: ({ address }) =>
|
|
545
|
+
rpc.withFallback((endpoint) =>
|
|
546
|
+
rpc.call(endpoint, RpcMethods.getBalance, [address, { commitment: "finalized" }]).pipe(
|
|
547
|
+
Effect.map(({ context, value }) => ({ slot: context.slot, value }))
|
|
548
|
+
)
|
|
549
|
+
),
|
|
360
550
|
getSignaturesForAddress: ({ address, ...config }) =>
|
|
361
551
|
rpc.withFallback((endpoint) => rpc.call(endpoint, RpcMethods.getSignaturesForAddress, [address, config])),
|
|
362
|
-
getTransaction
|
|
552
|
+
getTransaction,
|
|
553
|
+
getLatestBlockhash: ({ commitment }) =>
|
|
554
|
+
rpc.withFallback((endpoint) =>
|
|
555
|
+
rpc.call(endpoint, RpcMethods.getLatestBlockhash, [{ commitment }]).pipe(
|
|
556
|
+
Effect.map(({ context, value }) => ({ slot: context.slot, ...value }))
|
|
557
|
+
)
|
|
558
|
+
),
|
|
559
|
+
simulateTransaction: ({ transaction, ...config }) =>
|
|
560
|
+
rpc.withFallback((endpoint) =>
|
|
561
|
+
rpc.call(endpoint, RpcMethods.simulateTransaction, [transaction, { encoding: "base64", ...config }]).pipe(
|
|
562
|
+
Effect.map(({ context, value }) => ({ slot: context.slot, ...value }))
|
|
563
|
+
)
|
|
564
|
+
),
|
|
565
|
+
getRecentPrioritizationFees: ({ addresses }) =>
|
|
566
|
+
rpc.withFallback((endpoint) => rpc.call(endpoint, RpcMethods.getRecentPrioritizationFees, [addresses])),
|
|
567
|
+
sendTransaction: ({ transaction, ...config }) =>
|
|
568
|
+
rpc.withFallback((endpoint) =>
|
|
569
|
+
rpc.call(endpoint, RpcMethods.sendTransaction, [transaction, { encoding: "base64", ...config }])
|
|
570
|
+
)
|
|
363
571
|
})
|
|
364
572
|
})
|
|
365
573
|
)
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signers of Solana transactions: a local private key, or a custody API that signs raw bytes.
|
|
3
|
+
*
|
|
4
|
+
* A signer is a value and receives the whole serialized transaction message. Solana signs these bytes themselves,
|
|
5
|
+
* without hashing them first, so what is being authorised can always be decoded from them. A transaction carries one
|
|
6
|
+
* signature per signer account of its message, the fee payer's first.
|
|
7
|
+
*
|
|
8
|
+
* @since 0.2.0
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
address,
|
|
12
|
+
assertIsSignatureBytes,
|
|
13
|
+
createKeyPairFromBytes,
|
|
14
|
+
getAddressFromPublicKey,
|
|
15
|
+
getBase58Encoder,
|
|
16
|
+
getPublicKeyFromAddress,
|
|
17
|
+
signBytes,
|
|
18
|
+
verifySignature
|
|
19
|
+
} from "@solana/kit"
|
|
20
|
+
import * as Effect from "effect/Effect"
|
|
21
|
+
import * as Redacted from "effect/Redacted"
|
|
22
|
+
import * as Schema from "effect/Schema"
|
|
23
|
+
import { SolanaAddress } from "./SolanaAddress.ts"
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A signature could not be produced: `cause` is the signer's own failure, or why its signature was refused. It never
|
|
27
|
+
* holds a private key.
|
|
28
|
+
*
|
|
29
|
+
* @since 0.2.0
|
|
30
|
+
* @category errors
|
|
31
|
+
*/
|
|
32
|
+
export class SolanaSignerError extends Schema.TaggedError<SolanaSignerError>()("SolanaSignerError", {
|
|
33
|
+
cause: Schema.Defect()
|
|
34
|
+
}) {}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Signs transactions as `address`. `signTransactionMessage` receives the serialized transaction message and returns
|
|
38
|
+
* the 64-byte ed25519 signature of these bytes.
|
|
39
|
+
*
|
|
40
|
+
* @since 0.2.0
|
|
41
|
+
* @category models
|
|
42
|
+
*/
|
|
43
|
+
export interface SolanaSigner {
|
|
44
|
+
readonly address: SolanaAddress
|
|
45
|
+
readonly signTransactionMessage: (message: Uint8Array) => Effect.Effect<Uint8Array, SolanaSignerError>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const toError = (cause: unknown) => new SolanaSignerError({ cause })
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A signer holding an ed25519 key in memory: the 64-byte secret key (the private key followed by its public key) in
|
|
52
|
+
* base58, as wallets export it. An invalid key fails with a {@link SolanaSignerError} that does not repeat it.
|
|
53
|
+
*
|
|
54
|
+
* @since 0.2.0
|
|
55
|
+
* @category constructors
|
|
56
|
+
*/
|
|
57
|
+
export const fromPrivateKey = (key: Redacted.Redacted<string>): Effect.Effect<SolanaSigner, SolanaSignerError> =>
|
|
58
|
+
Effect.tryPromise({
|
|
59
|
+
try: async () => {
|
|
60
|
+
const keyPair = await createKeyPairFromBytes(getBase58Encoder().encode(Redacted.value(key)))
|
|
61
|
+
return { keyPair, address: SolanaAddress.make(await getAddressFromPublicKey(keyPair.publicKey)) }
|
|
62
|
+
},
|
|
63
|
+
// the thrown error can quote the key
|
|
64
|
+
catch: () => new SolanaSignerError({ cause: new Error("invalid private key") })
|
|
65
|
+
}).pipe(Effect.map(({ address, keyPair }) => ({
|
|
66
|
+
address,
|
|
67
|
+
signTransactionMessage: (message) =>
|
|
68
|
+
Effect.tryPromise({ try: () => signBytes(keyPair.privateKey, message), catch: toError })
|
|
69
|
+
})))
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A signer for a key held elsewhere (a custody API, an HSM) that signs raw bytes. `signMessage` receives the
|
|
73
|
+
* serialized transaction message and returns its 64-byte ed25519 signature. A failure of `signMessage`, a malformed
|
|
74
|
+
* signature or one that `address` did not make fails with a {@link SolanaSignerError}.
|
|
75
|
+
*
|
|
76
|
+
* @since 0.2.0
|
|
77
|
+
* @category constructors
|
|
78
|
+
*/
|
|
79
|
+
export const fromMessageSigner = <E>(options: {
|
|
80
|
+
readonly address: SolanaAddress
|
|
81
|
+
readonly signMessage: (message: Uint8Array) => Effect.Effect<Uint8Array, E>
|
|
82
|
+
}): SolanaSigner => ({
|
|
83
|
+
address: options.address,
|
|
84
|
+
signTransactionMessage: Effect.fnUntraced(function*(message) {
|
|
85
|
+
const signature = yield* Effect.mapError(options.signMessage(message), toError)
|
|
86
|
+
return yield* Effect.tryPromise({
|
|
87
|
+
try: async () => {
|
|
88
|
+
assertIsSignatureBytes(signature)
|
|
89
|
+
const publicKey = await getPublicKeyFromAddress(address(options.address))
|
|
90
|
+
if (!(await verifySignature(publicKey, signature, message))) {
|
|
91
|
+
throw new Error(`not a signature of ${options.address}`)
|
|
92
|
+
}
|
|
93
|
+
return signature
|
|
94
|
+
},
|
|
95
|
+
catch: toError
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
})
|
package/src/index.ts
CHANGED
|
@@ -19,11 +19,21 @@ export * as SolanaIndexer from "./SolanaIndexer.ts"
|
|
|
19
19
|
*/
|
|
20
20
|
export * as SolanaNetwork from "./SolanaNetwork.ts"
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* @since 0.2.0
|
|
24
|
+
*/
|
|
25
|
+
export * as SolanaRelayer from "./SolanaRelayer.ts"
|
|
26
|
+
|
|
22
27
|
/**
|
|
23
28
|
* @since 0.1.0
|
|
24
29
|
*/
|
|
25
30
|
export * as SolanaRpc from "./SolanaRpc.ts"
|
|
26
31
|
|
|
32
|
+
/**
|
|
33
|
+
* @since 0.2.0
|
|
34
|
+
*/
|
|
35
|
+
export * as SolanaSigner from "./SolanaSigner.ts"
|
|
36
|
+
|
|
27
37
|
/**
|
|
28
38
|
* @since 0.1.0
|
|
29
39
|
*/
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as BigInt_ from "effect/BigInt"
|
|
2
|
+
import * as Effect from "effect/Effect"
|
|
3
|
+
import * as SolanaNetwork from "../SolanaNetwork.ts"
|
|
4
|
+
import type { SolanaRpc, SolanaRpcError } from "../SolanaRpc.ts"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The `finalized` slot minus `confirmations` (the network's default when unset), never below 0.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export const lastConfirmedBlock = (
|
|
12
|
+
rpc: SolanaRpc["Service"],
|
|
13
|
+
confirmations: number | undefined
|
|
14
|
+
): Effect.Effect<bigint, SolanaRpcError> => {
|
|
15
|
+
const depth = BigInt(confirmations ?? SolanaNetwork.defaultConfirmations(rpc.network))
|
|
16
|
+
return Effect.map(rpc.getSlot({ commitment: "finalized" }), (slot) => BigInt_.max(slot - depth, BigInt(0)))
|
|
17
|
+
}
|
|
@@ -85,6 +85,36 @@ const transactions = {
|
|
|
85
85
|
})
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
const Lamports = Schema.Array(Schema.BigInt)
|
|
89
|
+
|
|
90
|
+
// the fields the balance helpers read, any other field of the status meta dropped
|
|
91
|
+
const Meta = Schema.Struct({
|
|
92
|
+
err: JsonFromRpc,
|
|
93
|
+
fee: Schema.BigInt,
|
|
94
|
+
preBalances: Lamports,
|
|
95
|
+
postBalances: Lamports,
|
|
96
|
+
// absent from transactions older than versioned transactions
|
|
97
|
+
loadedAddresses: Schema.optionalKey(Schema.Struct({
|
|
98
|
+
writable: Schema.Array(SolanaAddress),
|
|
99
|
+
readonly: Schema.Array(SolanaAddress)
|
|
100
|
+
}))
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
// the node's `InstructionError` and `TransactionError` as serde writes them: a variant without data is its name, one
|
|
104
|
+
// with data an object keyed by its name. Only the variants below are decoded, any other one is kept as raw JSON
|
|
105
|
+
const InstructionError = Schema.Union([
|
|
106
|
+
Schema.String,
|
|
107
|
+
Schema.Struct({ Custom: NumberFromBigInt }),
|
|
108
|
+
Schema.Record(Schema.String, JsonFromRpc)
|
|
109
|
+
])
|
|
110
|
+
|
|
111
|
+
/** @internal */
|
|
112
|
+
export const TransactionError = Schema.Union([
|
|
113
|
+
Schema.String,
|
|
114
|
+
Schema.Struct({ InstructionError: Schema.Tuple([NumberFromBigInt, InstructionError]) }),
|
|
115
|
+
Schema.Record(Schema.String, JsonFromRpc)
|
|
116
|
+
])
|
|
117
|
+
|
|
88
118
|
const NoParams = Schema.Tuple([])
|
|
89
119
|
|
|
90
120
|
const Commitment = Schema.Literals(["confirmed", "finalized"])
|
|
@@ -99,6 +129,39 @@ export const getSlot = make({
|
|
|
99
129
|
result: Schema.BigInt
|
|
100
130
|
})
|
|
101
131
|
|
|
132
|
+
/** @internal */
|
|
133
|
+
export const getBlockHeight = make({
|
|
134
|
+
name: "getBlockHeight",
|
|
135
|
+
params: Schema.Tuple([Schema.Struct({ commitment: Commitment })]),
|
|
136
|
+
result: Schema.BigInt
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
/** @internal */
|
|
140
|
+
export const getSignatureStatuses = make({
|
|
141
|
+
name: "getSignatureStatuses",
|
|
142
|
+
params: Schema.Tuple([
|
|
143
|
+
Schema.Array(SolanaTransactionHash),
|
|
144
|
+
Schema.Struct({ searchTransactionHistory: JsonRpc.optionalOmitted(Schema.Boolean) })
|
|
145
|
+
]),
|
|
146
|
+
result: Schema.Struct({
|
|
147
|
+
context: Schema.Struct({ slot: Schema.BigInt }),
|
|
148
|
+
// `null` for a signature the endpoint does not know
|
|
149
|
+
value: Schema.Array(Schema.NullOr(Schema.Struct({
|
|
150
|
+
slot: Schema.BigInt,
|
|
151
|
+
confirmations: Schema.NullOr(NumberFromBigInt),
|
|
152
|
+
err: Schema.NullOr(TransactionError),
|
|
153
|
+
confirmationStatus: Schema.NullOr(Schema.Literals(["processed", "confirmed", "finalized"]))
|
|
154
|
+
})))
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
/** @internal */
|
|
159
|
+
export const getBalance = make({
|
|
160
|
+
name: "getBalance",
|
|
161
|
+
params: Schema.Tuple([SolanaAddress, Schema.Struct({ commitment: Commitment })]),
|
|
162
|
+
result: Schema.Struct({ context: Schema.Struct({ slot: Schema.BigInt }), value: Schema.BigInt })
|
|
163
|
+
})
|
|
164
|
+
|
|
102
165
|
/** @internal */
|
|
103
166
|
export const getSignaturesForAddress = make({
|
|
104
167
|
name: "getSignaturesForAddress",
|
|
@@ -138,6 +201,60 @@ export const getTransaction = (encoding: keyof typeof transactions) =>
|
|
|
138
201
|
slot: Schema.BigInt,
|
|
139
202
|
blockTime: Schema.NullOr(Schema.BigInt),
|
|
140
203
|
transaction: transactions[encoding],
|
|
141
|
-
meta:
|
|
204
|
+
meta: Schema.NullOr(Meta)
|
|
142
205
|
}))
|
|
143
206
|
})
|
|
207
|
+
|
|
208
|
+
/** @internal */
|
|
209
|
+
export const getLatestBlockhash = make({
|
|
210
|
+
name: "getLatestBlockhash",
|
|
211
|
+
params: Schema.Tuple([Schema.Struct({ commitment: Commitment })]),
|
|
212
|
+
result: Schema.Struct({
|
|
213
|
+
context: Schema.Struct({ slot: Schema.BigInt }),
|
|
214
|
+
value: Schema.Struct({ blockhash: Schema.String, lastValidBlockHeight: Schema.BigInt })
|
|
215
|
+
})
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
/** @internal */
|
|
219
|
+
export const simulateTransaction = make({
|
|
220
|
+
name: "simulateTransaction",
|
|
221
|
+
params: Schema.Tuple([
|
|
222
|
+
Schema.String,
|
|
223
|
+
Schema.Struct({
|
|
224
|
+
encoding: Schema.Literal("base64"),
|
|
225
|
+
commitment: Commitment,
|
|
226
|
+
sigVerify: JsonRpc.optionalOmitted(Schema.Boolean),
|
|
227
|
+
replaceRecentBlockhash: JsonRpc.optionalOmitted(Schema.Boolean)
|
|
228
|
+
})
|
|
229
|
+
]),
|
|
230
|
+
result: Schema.Struct({
|
|
231
|
+
context: Schema.Struct({ slot: Schema.BigInt }),
|
|
232
|
+
value: Schema.Struct({
|
|
233
|
+
err: Schema.NullOr(TransactionError),
|
|
234
|
+
logs: Schema.NullOr(Schema.Array(Schema.String)),
|
|
235
|
+
unitsConsumed: Schema.NullOr(Schema.BigInt)
|
|
236
|
+
})
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
/** @internal */
|
|
241
|
+
export const getRecentPrioritizationFees = make({
|
|
242
|
+
name: "getRecentPrioritizationFees",
|
|
243
|
+
params: Schema.Tuple([Schema.Array(SolanaAddress)]),
|
|
244
|
+
result: Schema.Array(Schema.Struct({ slot: Schema.BigInt, prioritizationFee: Schema.BigInt }))
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
/** @internal */
|
|
248
|
+
export const sendTransaction = make({
|
|
249
|
+
name: "sendTransaction",
|
|
250
|
+
params: Schema.Tuple([
|
|
251
|
+
Schema.String,
|
|
252
|
+
Schema.Struct({
|
|
253
|
+
encoding: Schema.Literal("base64"),
|
|
254
|
+
skipPreflight: JsonRpc.optionalOmitted(Schema.Boolean),
|
|
255
|
+
preflightCommitment: JsonRpc.optionalOmitted(Commitment),
|
|
256
|
+
maxRetries: JsonRpc.optionalOmitted(Schema.Int)
|
|
257
|
+
})
|
|
258
|
+
]),
|
|
259
|
+
result: SolanaTransactionHash
|
|
260
|
+
})
|