@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.
Files changed (43) 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 +156 -0
  8. package/dist/SolanaIndexer.d.ts.map +1 -0
  9. package/dist/SolanaIndexer.js +227 -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/SolanaRpc.d.ts +258 -0
  16. package/dist/SolanaRpc.d.ts.map +1 -0
  17. package/dist/SolanaRpc.js +154 -0
  18. package/dist/SolanaRpc.js.map +1 -0
  19. package/dist/SolanaTransactionHash.d.ts +14 -0
  20. package/dist/SolanaTransactionHash.d.ts.map +1 -0
  21. package/dist/SolanaTransactionHash.js +34 -0
  22. package/dist/SolanaTransactionHash.js.map +1 -0
  23. package/dist/index.d.ts +24 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +25 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/internal/bigIntJson.d.ts +2 -0
  28. package/dist/internal/bigIntJson.d.ts.map +1 -0
  29. package/dist/internal/bigIntJson.js +9 -0
  30. package/dist/internal/bigIntJson.js.map +1 -0
  31. package/dist/internal/rpcMethods.d.ts +2 -0
  32. package/dist/internal/rpcMethods.d.ts.map +1 -0
  33. package/dist/internal/rpcMethods.js +105 -0
  34. package/dist/internal/rpcMethods.js.map +1 -0
  35. package/package.json +66 -3
  36. package/src/SolanaAddress.ts +40 -0
  37. package/src/SolanaIndexer.ts +376 -0
  38. package/src/SolanaNetwork.ts +105 -0
  39. package/src/SolanaRpc.ts +387 -0
  40. package/src/SolanaTransactionHash.ts +41 -0
  41. package/src/index.ts +30 -0
  42. package/src/internal/bigIntJson.ts +18 -0
  43. package/src/internal/rpcMethods.ts +143 -0
@@ -0,0 +1,143 @@
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 NoParams = Schema.Tuple([])
89
+
90
+ const Commitment = Schema.Literals(["confirmed", "finalized"])
91
+
92
+ /** @internal */
93
+ export const getGenesisHash = make({ name: "getGenesisHash", params: NoParams, result: Schema.String })
94
+
95
+ /** @internal */
96
+ export const getSlot = make({
97
+ name: "getSlot",
98
+ params: Schema.Tuple([Schema.Struct({ commitment: Commitment })]),
99
+ result: Schema.BigInt
100
+ })
101
+
102
+ /** @internal */
103
+ export const getSignaturesForAddress = make({
104
+ name: "getSignaturesForAddress",
105
+ params: Schema.Tuple([
106
+ SolanaAddress,
107
+ Schema.Struct({
108
+ before: JsonRpc.optionalOmitted(SolanaTransactionHash),
109
+ until: JsonRpc.optionalOmitted(SolanaTransactionHash),
110
+ limit: JsonRpc.optionalOmitted(Schema.Int),
111
+ commitment: Commitment,
112
+ minContextSlot: JsonRpc.optionalOmitted(JsonRpc.BigIntFromInt)
113
+ })
114
+ ]),
115
+ result: Schema.Array(Schema.Struct({
116
+ signature: SolanaTransactionHash,
117
+ slot: Schema.BigInt,
118
+ blockTime: Schema.NullOr(Schema.BigInt),
119
+ err: JsonFromRpc,
120
+ memo: Schema.NullOr(Schema.String)
121
+ }))
122
+ })
123
+
124
+ /** @internal */
125
+ export const getTransaction = (encoding: keyof typeof transactions) =>
126
+ make({
127
+ name: "getTransaction",
128
+ params: Schema.Tuple([
129
+ SolanaTransactionHash,
130
+ Schema.Struct({
131
+ commitment: Commitment,
132
+ encoding: Schema.Literal(encoding),
133
+ maxSupportedTransactionVersion: Schema.Literal(0)
134
+ })
135
+ ]),
136
+ // `null` when the endpoint does not have the transaction
137
+ result: Schema.NullOr(Schema.Struct({
138
+ slot: Schema.BigInt,
139
+ blockTime: Schema.NullOr(Schema.BigInt),
140
+ transaction: transactions[encoding],
141
+ meta: JsonFromRpc
142
+ }))
143
+ })