@effect-blockchain/solana 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.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +12 -2
  3. package/dist/SolanaAddress.d.ts +21 -0
  4. package/dist/SolanaAddress.d.ts.map +1 -0
  5. package/dist/SolanaAddress.js +33 -0
  6. package/dist/SolanaAddress.js.map +1 -0
  7. package/dist/SolanaIndexer.d.ts +198 -0
  8. package/dist/SolanaIndexer.d.ts.map +1 -0
  9. package/dist/SolanaIndexer.js +344 -0
  10. package/dist/SolanaIndexer.js.map +1 -0
  11. package/dist/SolanaNetwork.d.ts +64 -0
  12. package/dist/SolanaNetwork.d.ts.map +1 -0
  13. package/dist/SolanaNetwork.js +84 -0
  14. package/dist/SolanaNetwork.js.map +1 -0
  15. package/dist/SolanaRelayer.d.ts +207 -0
  16. package/dist/SolanaRelayer.d.ts.map +1 -0
  17. package/dist/SolanaRelayer.js +377 -0
  18. package/dist/SolanaRelayer.js.map +1 -0
  19. package/dist/SolanaRpc.d.ts +427 -0
  20. package/dist/SolanaRpc.d.ts.map +1 -0
  21. package/dist/SolanaRpc.js +216 -0
  22. package/dist/SolanaRpc.js.map +1 -0
  23. package/dist/SolanaSigner.d.ts +49 -0
  24. package/dist/SolanaSigner.d.ts.map +1 -0
  25. package/dist/SolanaSigner.js +82 -0
  26. package/dist/SolanaSigner.js.map +1 -0
  27. package/dist/SolanaTransactionHash.d.ts +14 -0
  28. package/dist/SolanaTransactionHash.d.ts.map +1 -0
  29. package/dist/SolanaTransactionHash.js +34 -0
  30. package/dist/SolanaTransactionHash.js.map +1 -0
  31. package/dist/index.d.ts +32 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +33 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/internal/bigIntJson.d.ts +2 -0
  36. package/dist/internal/bigIntJson.d.ts.map +1 -0
  37. package/dist/internal/bigIntJson.js +9 -0
  38. package/dist/internal/bigIntJson.js.map +1 -0
  39. package/dist/internal/confirmations.d.ts +2 -0
  40. package/dist/internal/confirmations.d.ts.map +1 -0
  41. package/dist/internal/confirmations.js +15 -0
  42. package/dist/internal/confirmations.js.map +1 -0
  43. package/dist/internal/rpcMethods.d.ts +2 -0
  44. package/dist/internal/rpcMethods.d.ts.map +1 -0
  45. package/dist/internal/rpcMethods.js +223 -0
  46. package/dist/internal/rpcMethods.js.map +1 -0
  47. package/package.json +66 -3
  48. package/src/SolanaAddress.ts +40 -0
  49. package/src/SolanaIndexer.ts +552 -0
  50. package/src/SolanaNetwork.ts +105 -0
  51. package/src/SolanaRelayer.ts +493 -0
  52. package/src/SolanaRpc.ts +595 -0
  53. package/src/SolanaSigner.ts +98 -0
  54. package/src/SolanaTransactionHash.ts +41 -0
  55. package/src/index.ts +40 -0
  56. package/src/internal/bigIntJson.ts +18 -0
  57. package/src/internal/confirmations.ts +17 -0
  58. package/src/internal/rpcMethods.ts +260 -0
@@ -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,40 @@
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.2.0
24
+ */
25
+ export * as SolanaRelayer from "./SolanaRelayer.ts"
26
+
27
+ /**
28
+ * @since 0.1.0
29
+ */
30
+ export * as SolanaRpc from "./SolanaRpc.ts"
31
+
32
+ /**
33
+ * @since 0.2.0
34
+ */
35
+ export * as SolanaSigner from "./SolanaSigner.ts"
36
+
37
+ /**
38
+ * @since 0.1.0
39
+ */
40
+ 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
+ )
@@ -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
+ }
@@ -0,0 +1,260 @@
1
+ import * as JsonRpc from "@effect-blockchain/core/JsonRpc"
2
+ import * as Schema from "effect/Schema"
3
+ import * as SchemaTransformation from "effect/SchemaTransformation"
4
+ import { SolanaAddress } from "../SolanaAddress.ts"
5
+ import { SolanaTransactionHash } from "../SolanaTransactionHash.ts"
6
+
7
+ /**
8
+ * JSON as `internal/bigIntJson.ts` reads it: every integer is a `bigint`, so u64 values stay exact.
9
+ *
10
+ * @internal
11
+ */
12
+ export type RpcJson =
13
+ | null
14
+ | boolean
15
+ | string
16
+ | number
17
+ | bigint
18
+ | ReadonlyArray<RpcJson>
19
+ | { readonly [key: string]: RpcJson }
20
+
21
+ const make = <Params, Result>(method: JsonRpc.Method<Params, Result, RpcJson>) => method
22
+
23
+ const RpcJson: Schema.Codec<RpcJson> = Schema.Union([
24
+ Schema.Null,
25
+ Schema.Boolean,
26
+ Schema.String,
27
+ Schema.Number,
28
+ Schema.BigInt,
29
+ Schema.Array(Schema.suspend(() => RpcJson)),
30
+ Schema.Record(Schema.String, Schema.suspend(() => RpcJson))
31
+ ])
32
+
33
+ const toJson = (value: RpcJson): Schema.Json => {
34
+ if (typeof value === "bigint") return value.toString()
35
+ if (value === null || typeof value !== "object") return value
36
+ if (Array.isArray(value)) return value.map(toJson)
37
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, toJson(item)]))
38
+ }
39
+
40
+ // raw RPC data kept as JSON, every integer as a decimal string: exact, and storable as is
41
+ const JsonFromRpc = RpcJson.pipe(
42
+ Schema.decodeTo(
43
+ Schema.Json,
44
+ SchemaTransformation.transform<Schema.Json, RpcJson>({ decode: toJson, encode: (json) => json })
45
+ )
46
+ )
47
+
48
+ // a small JSON integer read as a `number`, e.g. an account index
49
+ const NumberFromBigInt = Schema.BigInt.pipe(
50
+ Schema.decodeTo(Schema.Int, SchemaTransformation.transform({ decode: Number, encode: BigInt }))
51
+ )
52
+
53
+ const Indexes = Schema.Array(NumberFromBigInt)
54
+
55
+ // the `transaction` codec by encoding, checked against `SolanaEncodedTransaction` where `SolanaRpc.ts` uses it
56
+ const transactions = {
57
+ base64: Schema.Tuple([Schema.String, Schema.Literal("base64")]).pipe(
58
+ Schema.decodeTo(
59
+ Schema.String,
60
+ SchemaTransformation.transform({ decode: ([data]) => data, encode: (data) => [data, "base64"] as const })
61
+ )
62
+ ),
63
+ json: Schema.Struct({
64
+ signatures: Schema.Array(SolanaTransactionHash),
65
+ message: Schema.Struct({
66
+ accountKeys: Schema.Array(SolanaAddress),
67
+ header: Schema.Struct({
68
+ numRequiredSignatures: NumberFromBigInt,
69
+ numReadonlySignedAccounts: NumberFromBigInt,
70
+ numReadonlyUnsignedAccounts: NumberFromBigInt
71
+ }),
72
+ recentBlockhash: Schema.String,
73
+ instructions: Schema.Array(Schema.Struct({
74
+ programIdIndex: NumberFromBigInt,
75
+ accounts: Indexes,
76
+ data: Schema.String
77
+ })),
78
+ // absent from legacy transactions
79
+ addressTableLookups: Schema.optionalKey(Schema.Array(Schema.Struct({
80
+ accountKey: SolanaAddress,
81
+ writableIndexes: Indexes,
82
+ readonlyIndexes: Indexes
83
+ })))
84
+ })
85
+ })
86
+ }
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
+
118
+ const NoParams = Schema.Tuple([])
119
+
120
+ const Commitment = Schema.Literals(["confirmed", "finalized"])
121
+
122
+ /** @internal */
123
+ export const getGenesisHash = make({ name: "getGenesisHash", params: NoParams, result: Schema.String })
124
+
125
+ /** @internal */
126
+ export const getSlot = make({
127
+ name: "getSlot",
128
+ params: Schema.Tuple([Schema.Struct({ commitment: Commitment })]),
129
+ result: Schema.BigInt
130
+ })
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
+
165
+ /** @internal */
166
+ export const getSignaturesForAddress = make({
167
+ name: "getSignaturesForAddress",
168
+ params: Schema.Tuple([
169
+ SolanaAddress,
170
+ Schema.Struct({
171
+ before: JsonRpc.optionalOmitted(SolanaTransactionHash),
172
+ until: JsonRpc.optionalOmitted(SolanaTransactionHash),
173
+ limit: JsonRpc.optionalOmitted(Schema.Int),
174
+ commitment: Commitment,
175
+ minContextSlot: JsonRpc.optionalOmitted(JsonRpc.BigIntFromInt)
176
+ })
177
+ ]),
178
+ result: Schema.Array(Schema.Struct({
179
+ signature: SolanaTransactionHash,
180
+ slot: Schema.BigInt,
181
+ blockTime: Schema.NullOr(Schema.BigInt),
182
+ err: JsonFromRpc,
183
+ memo: Schema.NullOr(Schema.String)
184
+ }))
185
+ })
186
+
187
+ /** @internal */
188
+ export const getTransaction = (encoding: keyof typeof transactions) =>
189
+ make({
190
+ name: "getTransaction",
191
+ params: Schema.Tuple([
192
+ SolanaTransactionHash,
193
+ Schema.Struct({
194
+ commitment: Commitment,
195
+ encoding: Schema.Literal(encoding),
196
+ maxSupportedTransactionVersion: Schema.Literal(0)
197
+ })
198
+ ]),
199
+ // `null` when the endpoint does not have the transaction
200
+ result: Schema.NullOr(Schema.Struct({
201
+ slot: Schema.BigInt,
202
+ blockTime: Schema.NullOr(Schema.BigInt),
203
+ transaction: transactions[encoding],
204
+ meta: Schema.NullOr(Meta)
205
+ }))
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
+ })