solforge 0.1.6 → 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/.agi/agi.sqlite +0 -0
- package/.claude/settings.local.json +9 -0
- package/.github/workflows/release-binaries.yml +133 -0
- package/.tmp/.787ebcdbf7b8fde8-00000000.hm +0 -0
- package/.tmp/.bffe6efebdf8aedc-00000000.hm +0 -0
- package/AGENTS.md +271 -0
- package/CLAUDE.md +106 -0
- package/PROJECT_STRUCTURE.md +124 -0
- package/README.md +367 -393
- package/SOLANA_KIT_GUIDE.md +251 -0
- package/SOLFORGE.md +119 -0
- package/biome.json +34 -0
- package/bun.lock +743 -0
- package/docs/bun-single-file-executable.md +585 -0
- package/docs/cli-plan.md +154 -0
- package/docs/data-indexing-plan.md +214 -0
- package/docs/gui-roadmap.md +202 -0
- package/drizzle/0000_friendly_millenium_guard.sql +53 -0
- package/drizzle/0001_stale_sentinels.sql +2 -0
- package/drizzle/meta/0000_snapshot.json +329 -0
- package/drizzle/meta/0001_snapshot.json +345 -0
- package/drizzle/meta/_journal.json +20 -0
- package/drizzle.config.ts +12 -0
- package/index.ts +21 -0
- package/mint.sh +47 -0
- package/package.json +45 -69
- package/postcss.config.js +6 -0
- package/rpc-server.ts.backup +519 -0
- package/server/index.ts +5 -0
- package/server/lib/base58.ts +33 -0
- package/server/lib/faucet.ts +110 -0
- package/server/lib/spl-token.ts +57 -0
- package/server/methods/TEMPLATE.md +117 -0
- package/server/methods/account/get-account-info.ts +90 -0
- package/server/methods/account/get-balance.ts +27 -0
- package/server/methods/account/get-multiple-accounts.ts +83 -0
- package/server/methods/account/get-parsed-account-info.ts +21 -0
- package/server/methods/account/index.ts +12 -0
- package/server/methods/account/parsers/index.ts +52 -0
- package/server/methods/account/parsers/loader-upgradeable.ts +66 -0
- package/server/methods/account/parsers/spl-token.ts +237 -0
- package/server/methods/account/parsers/system.ts +4 -0
- package/server/methods/account/request-airdrop.ts +219 -0
- package/server/methods/admin/adopt-mint-authority.ts +94 -0
- package/server/methods/admin/clone-program-accounts.ts +55 -0
- package/server/methods/admin/clone-program.ts +152 -0
- package/server/methods/admin/clone-token-accounts.ts +117 -0
- package/server/methods/admin/clone-token-mint.ts +82 -0
- package/server/methods/admin/create-mint.ts +114 -0
- package/server/methods/admin/create-token-account.ts +137 -0
- package/server/methods/admin/helpers.ts +70 -0
- package/server/methods/admin/index.ts +10 -0
- package/server/methods/admin/list-mints.ts +21 -0
- package/server/methods/admin/load-program.ts +52 -0
- package/server/methods/admin/mint-to.ts +278 -0
- package/server/methods/block/get-block-height.ts +5 -0
- package/server/methods/block/get-block.ts +35 -0
- package/server/methods/block/get-blocks-with-limit.ts +23 -0
- package/server/methods/block/get-latest-blockhash.ts +12 -0
- package/server/methods/block/get-slot.ts +5 -0
- package/server/methods/block/index.ts +6 -0
- package/server/methods/block/is-blockhash-valid.ts +23 -0
- package/server/methods/epoch/get-cluster-nodes.ts +17 -0
- package/server/methods/epoch/get-epoch-info.ts +16 -0
- package/server/methods/epoch/get-epoch-schedule.ts +15 -0
- package/server/methods/epoch/get-highest-snapshot-slot.ts +9 -0
- package/server/methods/epoch/get-leader-schedule.ts +8 -0
- package/server/methods/epoch/get-max-retransmit-slot.ts +9 -0
- package/server/methods/epoch/get-max-shred-insert-slot.ts +9 -0
- package/server/methods/epoch/get-slot-leader.ts +6 -0
- package/server/methods/epoch/get-slot-leaders.ts +9 -0
- package/server/methods/epoch/get-stake-activation.ts +9 -0
- package/server/methods/epoch/get-stake-minimum-delegation.ts +9 -0
- package/server/methods/epoch/get-vote-accounts.ts +19 -0
- package/server/methods/epoch/index.ts +13 -0
- package/server/methods/epoch/minimum-ledger-slot.ts +5 -0
- package/server/methods/fee/get-fee-calculator-for-blockhash.ts +12 -0
- package/server/methods/fee/get-fee-for-message.ts +8 -0
- package/server/methods/fee/get-fee-rate-governor.ts +16 -0
- package/server/methods/fee/get-fees.ts +14 -0
- package/server/methods/fee/get-recent-prioritization-fees.ts +22 -0
- package/server/methods/fee/index.ts +5 -0
- package/server/methods/get-address-lookup-table.ts +31 -0
- package/server/methods/index.ts +265 -0
- package/server/methods/performance/get-recent-performance-samples.ts +25 -0
- package/server/methods/performance/get-transaction-count.ts +5 -0
- package/server/methods/performance/index.ts +2 -0
- package/server/methods/program/get-block-commitment.ts +9 -0
- package/server/methods/program/get-block-production.ts +14 -0
- package/server/methods/program/get-block-time.ts +21 -0
- package/server/methods/program/get-blocks.ts +11 -0
- package/server/methods/program/get-first-available-block.ts +9 -0
- package/server/methods/program/get-genesis-hash.ts +6 -0
- package/server/methods/program/get-identity.ts +6 -0
- package/server/methods/program/get-inflation-governor.ts +15 -0
- package/server/methods/program/get-inflation-rate.ts +10 -0
- package/server/methods/program/get-inflation-reward.ts +12 -0
- package/server/methods/program/get-largest-accounts.ts +8 -0
- package/server/methods/program/get-parsed-program-accounts.ts +12 -0
- package/server/methods/program/get-parsed-token-accounts-by-delegate.ts +12 -0
- package/server/methods/program/get-parsed-token-accounts-by-owner.ts +12 -0
- package/server/methods/program/get-program-accounts.ts +221 -0
- package/server/methods/program/get-supply.ts +13 -0
- package/server/methods/program/get-token-account-balance.ts +64 -0
- package/server/methods/program/get-token-accounts-by-delegate.ts +81 -0
- package/server/methods/program/get-token-accounts-by-owner.ts +390 -0
- package/server/methods/program/get-token-largest-accounts.ts +80 -0
- package/server/methods/program/get-token-supply.ts +38 -0
- package/server/methods/program/index.ts +21 -0
- package/server/methods/solforge/index.ts +155 -0
- package/server/methods/system/get-health.ts +5 -0
- package/server/methods/system/get-minimum-balance-for-rent-exemption.ts +13 -0
- package/server/methods/system/get-version.ts +9 -0
- package/server/methods/system/index.ts +3 -0
- package/server/methods/transaction/get-confirmed-transaction.ts +11 -0
- package/server/methods/transaction/get-parsed-transaction.ts +21 -0
- package/server/methods/transaction/get-signature-statuses.ts +72 -0
- package/server/methods/transaction/get-signatures-for-address.ts +45 -0
- package/server/methods/transaction/get-transaction.ts +428 -0
- package/server/methods/transaction/index.ts +7 -0
- package/server/methods/transaction/send-transaction.ts +232 -0
- package/server/methods/transaction/simulate-transaction.ts +56 -0
- package/server/rpc-server.ts +474 -0
- package/server/types.ts +74 -0
- package/server/ws-server.ts +171 -0
- package/sf.config.json +38 -0
- package/src/cli/bootstrap.ts +67 -0
- package/src/cli/commands/airdrop.ts +37 -0
- package/src/cli/commands/config.ts +39 -0
- package/src/cli/commands/mint.ts +187 -0
- package/src/cli/commands/program-clone.ts +124 -0
- package/src/cli/commands/program-load.ts +64 -0
- package/src/cli/commands/rpc-start.ts +46 -0
- package/src/cli/commands/token-adopt-authority.ts +37 -0
- package/src/cli/commands/token-clone.ts +113 -0
- package/src/cli/commands/token-create.ts +81 -0
- package/src/cli/main.ts +130 -0
- package/src/cli/run-solforge.ts +98 -0
- package/src/cli/setup-utils.ts +54 -0
- package/src/cli/setup-wizard.ts +256 -0
- package/src/cli/utils/args.ts +15 -0
- package/src/config/index.ts +130 -0
- package/src/db/index.ts +83 -0
- package/src/db/schema/accounts.ts +23 -0
- package/src/db/schema/address-signatures.ts +31 -0
- package/src/db/schema/index.ts +5 -0
- package/src/db/schema/meta-kv.ts +9 -0
- package/src/db/schema/transactions.ts +29 -0
- package/src/db/schema/tx-accounts.ts +33 -0
- package/src/db/tx-store.ts +229 -0
- package/src/gui/public/app.css +1 -0
- package/src/gui/public/index.html +19 -0
- package/src/gui/server.ts +297 -0
- package/src/gui/src/api.ts +127 -0
- package/src/gui/src/app.tsx +390 -0
- package/src/gui/src/components/airdrop-mint-form.tsx +216 -0
- package/src/gui/src/components/clone-program-modal.tsx +183 -0
- package/src/gui/src/components/clone-token-modal.tsx +211 -0
- package/src/gui/src/components/modal.tsx +127 -0
- package/src/gui/src/components/programs-panel.tsx +112 -0
- package/src/gui/src/components/status-panel.tsx +122 -0
- package/src/gui/src/components/tokens-panel.tsx +116 -0
- package/src/gui/src/hooks/use-interval.ts +17 -0
- package/src/gui/src/index.css +529 -0
- package/src/gui/src/main.tsx +17 -0
- package/src/migrations-bundled.ts +17 -0
- package/src/rpc/start.ts +44 -0
- package/tailwind.config.js +27 -0
- package/test-client.ts +120 -0
- package/tmp/inspect-html.ts +4 -0
- package/tmp/response-test.ts +5 -0
- package/tmp/test-html.ts +5 -0
- package/tmp/test-server.ts +13 -0
- package/tsconfig.json +24 -23
- package/LICENSE +0 -21
- package/scripts/postinstall.cjs +0 -103
- package/src/api-server-entry.ts +0 -109
- package/src/commands/add-program.ts +0 -337
- package/src/commands/init.ts +0 -122
- package/src/commands/list.ts +0 -136
- package/src/commands/mint.ts +0 -336
- package/src/commands/start.ts +0 -878
- package/src/commands/status.ts +0 -99
- package/src/commands/stop.ts +0 -406
- package/src/config/manager.ts +0 -157
- package/src/index.ts +0 -188
- package/src/services/api-server.ts +0 -532
- package/src/services/port-manager.ts +0 -177
- package/src/services/process-registry.ts +0 -154
- package/src/services/program-cloner.ts +0 -317
- package/src/services/token-cloner.ts +0 -809
- package/src/services/validator.ts +0 -295
- package/src/types/config.ts +0 -110
- package/src/utils/shell.ts +0 -110
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Keypair,
|
|
3
|
+
PublicKey,
|
|
4
|
+
type VersionedTransaction,
|
|
5
|
+
} from "@solana/web3.js";
|
|
6
|
+
import { LiteSVM } from "litesvm";
|
|
7
|
+
import { sqlite } from "../src/db/index";
|
|
8
|
+
import { TxStore } from "../src/db/tx-store";
|
|
9
|
+
import { decodeBase58, encodeBase58 } from "./lib/base58";
|
|
10
|
+
import { fundFaucetIfNeeded, loadOrCreateFaucet } from "./lib/faucet";
|
|
11
|
+
import { rpcMethods } from "./methods";
|
|
12
|
+
import type {
|
|
13
|
+
JsonRpcRequest,
|
|
14
|
+
JsonRpcResponse,
|
|
15
|
+
RpcMethodContext,
|
|
16
|
+
} from "./types";
|
|
17
|
+
|
|
18
|
+
export class LiteSVMRpcServer {
|
|
19
|
+
private svm: LiteSVM;
|
|
20
|
+
private slot: bigint = 1n;
|
|
21
|
+
private blockHeight: bigint = 1n;
|
|
22
|
+
private txCount: bigint = 0n;
|
|
23
|
+
private signatureListeners: Set<(sig: string) => void> = new Set();
|
|
24
|
+
private knownMints: Set<string> = new Set();
|
|
25
|
+
private knownPrograms: Set<string> = new Set();
|
|
26
|
+
private faucet: Keypair;
|
|
27
|
+
private txRecords: Map<
|
|
28
|
+
string,
|
|
29
|
+
{
|
|
30
|
+
tx: VersionedTransaction;
|
|
31
|
+
logs: string[];
|
|
32
|
+
err: unknown;
|
|
33
|
+
fee: number;
|
|
34
|
+
slot: number;
|
|
35
|
+
blockTime?: number;
|
|
36
|
+
preBalances?: number[];
|
|
37
|
+
postBalances?: number[];
|
|
38
|
+
preTokenBalances?: any[];
|
|
39
|
+
postTokenBalances?: any[];
|
|
40
|
+
}
|
|
41
|
+
> = new Map();
|
|
42
|
+
private store: TxStore;
|
|
43
|
+
|
|
44
|
+
constructor() {
|
|
45
|
+
this.svm = new LiteSVM()
|
|
46
|
+
.withSysvars()
|
|
47
|
+
.withBuiltins()
|
|
48
|
+
.withDefaultPrograms()
|
|
49
|
+
// Mint 1,000,000 SOL (1e15 lamports) for local dev
|
|
50
|
+
.withLamports(1_000_000_000_000_000n)
|
|
51
|
+
.withBlockhashCheck(true)
|
|
52
|
+
// keep some tx history so getTransaction/getSignatureStatuses can work
|
|
53
|
+
.withTransactionHistory(1000n)
|
|
54
|
+
.withSigverify(false);
|
|
55
|
+
this.store = new TxStore();
|
|
56
|
+
// Seed slot/blockHeight/txCount from DB if available for continuity
|
|
57
|
+
try {
|
|
58
|
+
const maxRow = sqlite
|
|
59
|
+
.prepare("SELECT MAX(slot) as m FROM transactions")
|
|
60
|
+
.get() as { m?: number } | undefined;
|
|
61
|
+
const cntRow = sqlite
|
|
62
|
+
.prepare("SELECT COUNT(1) as c FROM transactions")
|
|
63
|
+
.get() as { c?: number } | undefined;
|
|
64
|
+
const maxSlot = (maxRow?.m ?? 0) as number;
|
|
65
|
+
const txc = (cntRow?.c ?? 0) as number;
|
|
66
|
+
if (maxSlot > 0) {
|
|
67
|
+
this.slot = BigInt(maxSlot + 1);
|
|
68
|
+
this.blockHeight = BigInt(maxSlot + 1);
|
|
69
|
+
}
|
|
70
|
+
if (txc > 0) {
|
|
71
|
+
this.txCount = BigInt(txc);
|
|
72
|
+
}
|
|
73
|
+
} catch {}
|
|
74
|
+
|
|
75
|
+
// Load or create faucet; fund once at startup
|
|
76
|
+
this.faucet = loadOrCreateFaucet();
|
|
77
|
+
try {
|
|
78
|
+
const bal = fundFaucetIfNeeded(this.svm, this.faucet);
|
|
79
|
+
console.log(
|
|
80
|
+
`💧 Faucet loaded: ${this.faucet.publicKey.toBase58()} with ${(Number(bal) / 1_000_000_000).toFixed(0)} SOL`,
|
|
81
|
+
);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
console.warn("⚠️ Faucet funding failed:", e);
|
|
84
|
+
try {
|
|
85
|
+
const bal =
|
|
86
|
+
this.svm.getBalance(this.faucet.publicKey as PublicKey) || 0n;
|
|
87
|
+
console.log(
|
|
88
|
+
`💧 Faucet balance: ${(Number(bal) / 1_000_000_000).toFixed(9)} SOL`,
|
|
89
|
+
);
|
|
90
|
+
} catch {}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// base58 helpers moved to server/lib/base58
|
|
95
|
+
|
|
96
|
+
private createSuccessResponse(
|
|
97
|
+
id: string | number,
|
|
98
|
+
result: any,
|
|
99
|
+
): JsonRpcResponse {
|
|
100
|
+
return {
|
|
101
|
+
jsonrpc: "2.0",
|
|
102
|
+
id,
|
|
103
|
+
result,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private createErrorResponse(
|
|
108
|
+
id: string | number,
|
|
109
|
+
code: number,
|
|
110
|
+
message: string,
|
|
111
|
+
data?: any,
|
|
112
|
+
): JsonRpcResponse {
|
|
113
|
+
return {
|
|
114
|
+
jsonrpc: "2.0",
|
|
115
|
+
id,
|
|
116
|
+
error: { code, message, data },
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private getContext(): RpcMethodContext {
|
|
121
|
+
return {
|
|
122
|
+
svm: this.svm,
|
|
123
|
+
slot: this.slot,
|
|
124
|
+
blockHeight: this.blockHeight,
|
|
125
|
+
store: this.store,
|
|
126
|
+
encodeBase58,
|
|
127
|
+
decodeBase58,
|
|
128
|
+
createSuccessResponse: this.createSuccessResponse.bind(this),
|
|
129
|
+
createErrorResponse: this.createErrorResponse.bind(this),
|
|
130
|
+
notifySignature: (signature: string) => {
|
|
131
|
+
for (const cb of this.signatureListeners) cb(signature);
|
|
132
|
+
},
|
|
133
|
+
getFaucet: () => this.faucet,
|
|
134
|
+
getTxCount: () => this.txCount,
|
|
135
|
+
registerMint: (mint: any) => {
|
|
136
|
+
try {
|
|
137
|
+
const pk =
|
|
138
|
+
typeof mint === "string" ? mint : new PublicKey(mint).toBase58();
|
|
139
|
+
this.knownMints.add(pk);
|
|
140
|
+
} catch {}
|
|
141
|
+
},
|
|
142
|
+
listMints: () => Array.from(this.knownMints),
|
|
143
|
+
registerProgram: (program: any) => {
|
|
144
|
+
try {
|
|
145
|
+
const pk =
|
|
146
|
+
typeof program === "string"
|
|
147
|
+
? program
|
|
148
|
+
: new PublicKey(program).toBase58();
|
|
149
|
+
this.knownPrograms.add(pk);
|
|
150
|
+
} catch {}
|
|
151
|
+
},
|
|
152
|
+
listPrograms: () => Array.from(this.knownPrograms),
|
|
153
|
+
recordTransaction: (signature, tx, meta) => {
|
|
154
|
+
this.txRecords.set(signature, {
|
|
155
|
+
tx,
|
|
156
|
+
logs: meta?.logs || [],
|
|
157
|
+
err: meta?.err ?? null,
|
|
158
|
+
fee: meta?.fee ?? 5000,
|
|
159
|
+
slot: Number(this.slot),
|
|
160
|
+
blockTime: meta?.blockTime,
|
|
161
|
+
preBalances: meta?.preBalances,
|
|
162
|
+
postBalances: meta?.postBalances,
|
|
163
|
+
preTokenBalances: (meta as any)?.preTokenBalances,
|
|
164
|
+
postTokenBalances: (meta as any)?.postTokenBalances,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// Persist to SQLite for durability and history queries
|
|
168
|
+
try {
|
|
169
|
+
const msg: any = tx.message as any;
|
|
170
|
+
const rawKeys: any[] = Array.isArray(msg.staticAccountKeys)
|
|
171
|
+
? msg.staticAccountKeys
|
|
172
|
+
: Array.isArray(msg.accountKeys)
|
|
173
|
+
? msg.accountKeys
|
|
174
|
+
: [];
|
|
175
|
+
const keys: string[] = rawKeys.map((k: any) => {
|
|
176
|
+
try {
|
|
177
|
+
return typeof k === "string" ? k : (k as PublicKey).toBase58();
|
|
178
|
+
} catch {
|
|
179
|
+
return String(k);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
const header = msg.header || {
|
|
183
|
+
numRequiredSignatures: (tx.signatures || []).length,
|
|
184
|
+
numReadonlySignedAccounts: 0,
|
|
185
|
+
numReadonlyUnsignedAccounts: 0,
|
|
186
|
+
};
|
|
187
|
+
const numReq = Number(header.numRequiredSignatures || 0);
|
|
188
|
+
const numRoSigned = Number(header.numReadonlySignedAccounts || 0);
|
|
189
|
+
const numRoUnsigned = Number(header.numReadonlyUnsignedAccounts || 0);
|
|
190
|
+
const total = keys.length;
|
|
191
|
+
const unsignedCount = Math.max(0, total - numReq);
|
|
192
|
+
const writableSignedCutoff = Math.max(0, numReq - numRoSigned);
|
|
193
|
+
const writableUnsignedCount = Math.max(
|
|
194
|
+
0,
|
|
195
|
+
unsignedCount - numRoUnsigned,
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
const accounts = keys.map((addr, i) => {
|
|
199
|
+
const signer =
|
|
200
|
+
typeof msg.isAccountSigner === "function"
|
|
201
|
+
? !!msg.isAccountSigner(i)
|
|
202
|
+
: i < numReq;
|
|
203
|
+
let writable = true;
|
|
204
|
+
if (typeof msg.isAccountWritable === "function")
|
|
205
|
+
writable = !!msg.isAccountWritable(i);
|
|
206
|
+
else {
|
|
207
|
+
if (i < numReq) writable = i < writableSignedCutoff;
|
|
208
|
+
else writable = i - numReq < writableUnsignedCount;
|
|
209
|
+
}
|
|
210
|
+
return { address: addr, index: i, signer, writable };
|
|
211
|
+
});
|
|
212
|
+
const version: 0 | "legacy" =
|
|
213
|
+
typeof msg.version === "number"
|
|
214
|
+
? msg.version === 0
|
|
215
|
+
? 0
|
|
216
|
+
: "legacy"
|
|
217
|
+
: 0;
|
|
218
|
+
const rawBase64 = Buffer.from(tx.serialize()).toString("base64");
|
|
219
|
+
this.store
|
|
220
|
+
.insertTransactionBundle({
|
|
221
|
+
signature,
|
|
222
|
+
slot: Number(this.slot),
|
|
223
|
+
blockTime: meta?.blockTime,
|
|
224
|
+
version,
|
|
225
|
+
fee: Number(meta?.fee ?? 5000),
|
|
226
|
+
err: meta?.err ?? null,
|
|
227
|
+
rawBase64,
|
|
228
|
+
preBalances: Array.isArray(meta?.preBalances)
|
|
229
|
+
? meta!.preBalances!
|
|
230
|
+
: [],
|
|
231
|
+
postBalances: Array.isArray(meta?.postBalances)
|
|
232
|
+
? meta!.postBalances!
|
|
233
|
+
: [],
|
|
234
|
+
logs: Array.isArray(meta?.logs) ? meta!.logs! : [],
|
|
235
|
+
preTokenBalances: Array.isArray((meta as any)?.preTokenBalances)
|
|
236
|
+
? (meta as any).preTokenBalances
|
|
237
|
+
: [],
|
|
238
|
+
postTokenBalances: Array.isArray((meta as any)?.postTokenBalances)
|
|
239
|
+
? (meta as any).postTokenBalances
|
|
240
|
+
: [],
|
|
241
|
+
accounts,
|
|
242
|
+
})
|
|
243
|
+
.catch(() => {});
|
|
244
|
+
|
|
245
|
+
// Upsert account snapshots for static keys
|
|
246
|
+
const snapshots = keys
|
|
247
|
+
.map((addr) => {
|
|
248
|
+
try {
|
|
249
|
+
const acc = this.svm.getAccount(new PublicKey(addr));
|
|
250
|
+
if (!acc) return null;
|
|
251
|
+
return {
|
|
252
|
+
address: addr,
|
|
253
|
+
lamports: Number(acc.lamports || 0n),
|
|
254
|
+
ownerProgram: new PublicKey(acc.owner).toBase58(),
|
|
255
|
+
executable: !!acc.executable,
|
|
256
|
+
rentEpoch: Number(acc.rentEpoch || 0),
|
|
257
|
+
dataLen: acc.data?.length ?? 0,
|
|
258
|
+
dataBase64: undefined,
|
|
259
|
+
lastSlot: Number(this.slot),
|
|
260
|
+
};
|
|
261
|
+
} catch {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
})
|
|
265
|
+
.filter(Boolean) as any[];
|
|
266
|
+
if (snapshots.length > 0)
|
|
267
|
+
this.store.upsertAccounts(snapshots).catch(() => {});
|
|
268
|
+
} catch {}
|
|
269
|
+
},
|
|
270
|
+
getRecordedTransaction: (signature) => this.txRecords.get(signature),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
onSignatureRecorded(cb: (sig: string) => void) {
|
|
275
|
+
this.signatureListeners.add(cb);
|
|
276
|
+
return () => this.signatureListeners.delete(cb);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
getFaucetAddress(): string {
|
|
280
|
+
try {
|
|
281
|
+
return this.faucet.publicKey.toBase58();
|
|
282
|
+
} catch {
|
|
283
|
+
return "";
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
getFaucetBalance(): bigint {
|
|
288
|
+
try {
|
|
289
|
+
return this.svm.getBalance(this.faucet.publicKey as PublicKey) ?? 0n;
|
|
290
|
+
} catch {
|
|
291
|
+
return 0n;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
listKnownMints(): string[] {
|
|
296
|
+
return Array.from(this.knownMints);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
listKnownPrograms(): string[] {
|
|
300
|
+
return Array.from(this.knownPrograms);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
getSignatureStatus(
|
|
304
|
+
signature: string,
|
|
305
|
+
): { slot: number; err: any | null } | null {
|
|
306
|
+
// Prefer local record for reliability
|
|
307
|
+
const rec = this.txRecords.get(signature);
|
|
308
|
+
if (rec) {
|
|
309
|
+
return { slot: rec.slot, err: rec.err ?? null };
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
const sigBytes = decodeBase58(signature);
|
|
313
|
+
const tx = this.svm.getTransaction(sigBytes);
|
|
314
|
+
if (!tx) return null;
|
|
315
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
316
|
+
let errVal: any = null;
|
|
317
|
+
try {
|
|
318
|
+
errVal = "err" in tx ? (tx as any).err() : null;
|
|
319
|
+
} catch {
|
|
320
|
+
errVal = null;
|
|
321
|
+
}
|
|
322
|
+
return { slot: Number(this.slot), err: errVal };
|
|
323
|
+
} catch {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async handleRequest(request: JsonRpcRequest): Promise<JsonRpcResponse> {
|
|
329
|
+
const { method, params, id } = request;
|
|
330
|
+
|
|
331
|
+
try {
|
|
332
|
+
const methodHandler = rpcMethods[method];
|
|
333
|
+
|
|
334
|
+
if (!methodHandler) {
|
|
335
|
+
return this.createErrorResponse(
|
|
336
|
+
id,
|
|
337
|
+
-32601,
|
|
338
|
+
`Method not found: ${method}`,
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const context = this.getContext();
|
|
343
|
+
const result = await methodHandler(id, params, context);
|
|
344
|
+
|
|
345
|
+
// Update slot and blockHeight for methods that modify state
|
|
346
|
+
if (["sendTransaction", "requestAirdrop"].includes(method)) {
|
|
347
|
+
this.slot += 1n;
|
|
348
|
+
this.blockHeight += 1n;
|
|
349
|
+
this.txCount += 1n;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return result;
|
|
353
|
+
} catch (error: any) {
|
|
354
|
+
return this.createErrorResponse(
|
|
355
|
+
id,
|
|
356
|
+
-32603,
|
|
357
|
+
"Internal error",
|
|
358
|
+
error.message,
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export function createLiteSVMRpcServer(port: number = 8899, host?: string) {
|
|
365
|
+
const server = new LiteSVMRpcServer();
|
|
366
|
+
|
|
367
|
+
const bunServer = Bun.serve({
|
|
368
|
+
port,
|
|
369
|
+
hostname: host || process.env.RPC_HOST || "127.0.0.1",
|
|
370
|
+
async fetch(req) {
|
|
371
|
+
const DEBUG = process.env.DEBUG_RPC_LOG === "1";
|
|
372
|
+
const acrh = req.headers.get("Access-Control-Request-Headers");
|
|
373
|
+
const allowHeaders =
|
|
374
|
+
acrh && acrh.length > 0
|
|
375
|
+
? acrh
|
|
376
|
+
: "Content-Type, Accept, Origin, solana-client";
|
|
377
|
+
const corsHeaders = {
|
|
378
|
+
"Access-Control-Allow-Origin": "*",
|
|
379
|
+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS, HEAD",
|
|
380
|
+
"Access-Control-Allow-Headers": allowHeaders,
|
|
381
|
+
"Access-Control-Max-Age": "600",
|
|
382
|
+
// Help Chrome when accessing local network/localhost from secure context
|
|
383
|
+
"Access-Control-Allow-Private-Network": "true",
|
|
384
|
+
} as const;
|
|
385
|
+
|
|
386
|
+
if (req.method === "GET") {
|
|
387
|
+
const url = new URL(req.url);
|
|
388
|
+
if (url.pathname === "/" || url.pathname === "") {
|
|
389
|
+
return new Response("ok", {
|
|
390
|
+
headers: { "Content-Type": "text/plain", ...corsHeaders },
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
if (url.pathname === "/health") {
|
|
394
|
+
return new Response("ok", {
|
|
395
|
+
headers: { "Content-Type": "text/plain", ...corsHeaders },
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
return new Response("Not found", { status: 404, headers: corsHeaders });
|
|
399
|
+
}
|
|
400
|
+
if (req.method === "POST") {
|
|
401
|
+
try {
|
|
402
|
+
const body = await req.json();
|
|
403
|
+
|
|
404
|
+
if (Array.isArray(body)) {
|
|
405
|
+
if (DEBUG) {
|
|
406
|
+
try {
|
|
407
|
+
console.log(
|
|
408
|
+
"RPC batch:",
|
|
409
|
+
body.map((b: any) => b.method),
|
|
410
|
+
);
|
|
411
|
+
} catch {}
|
|
412
|
+
}
|
|
413
|
+
const responses = await Promise.all(
|
|
414
|
+
body.map((request) => server.handleRequest(request)),
|
|
415
|
+
);
|
|
416
|
+
return new Response(JSON.stringify(responses), {
|
|
417
|
+
headers: { "Content-Type": "application/json", ...corsHeaders },
|
|
418
|
+
});
|
|
419
|
+
} else {
|
|
420
|
+
const reqObj = body as JsonRpcRequest;
|
|
421
|
+
if (DEBUG) {
|
|
422
|
+
try {
|
|
423
|
+
console.log(
|
|
424
|
+
"RPC:",
|
|
425
|
+
reqObj.method,
|
|
426
|
+
JSON.stringify(reqObj.params),
|
|
427
|
+
);
|
|
428
|
+
} catch {}
|
|
429
|
+
}
|
|
430
|
+
const response = await server.handleRequest(reqObj);
|
|
431
|
+
return new Response(JSON.stringify(response), {
|
|
432
|
+
headers: { "Content-Type": "application/json", ...corsHeaders },
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
} catch (error) {
|
|
436
|
+
return new Response(
|
|
437
|
+
JSON.stringify({
|
|
438
|
+
jsonrpc: "2.0",
|
|
439
|
+
id: null,
|
|
440
|
+
error: {
|
|
441
|
+
code: -32700,
|
|
442
|
+
message: "Parse error",
|
|
443
|
+
},
|
|
444
|
+
}),
|
|
445
|
+
{ headers: { "Content-Type": "application/json", ...corsHeaders } },
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (req.method === "OPTIONS") {
|
|
451
|
+
return new Response(null, { headers: corsHeaders });
|
|
452
|
+
}
|
|
453
|
+
if (req.method === "HEAD") {
|
|
454
|
+
return new Response(null, { headers: corsHeaders });
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return new Response("Method not allowed", {
|
|
458
|
+
status: 405,
|
|
459
|
+
headers: corsHeaders,
|
|
460
|
+
});
|
|
461
|
+
},
|
|
462
|
+
error(error) {
|
|
463
|
+
console.error("Server error:", error);
|
|
464
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
465
|
+
},
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
const hostname = (host || process.env.RPC_HOST || "127.0.0.1").toString();
|
|
469
|
+
console.log(`🚀 LiteSVM RPC Server running on http://${hostname}:${port}`);
|
|
470
|
+
console.log(` Compatible with Solana RPC API`);
|
|
471
|
+
console.log(` Use with: solana config set -u http://${hostname}:${port}`);
|
|
472
|
+
|
|
473
|
+
return { httpServer: bunServer, rpcServer: server };
|
|
474
|
+
}
|
package/server/types.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Keypair, PublicKey, VersionedTransaction } from "@solana/web3.js";
|
|
2
|
+
import type { LiteSVM } from "litesvm";
|
|
3
|
+
import type { TxStore } from "../src/db/tx-store";
|
|
4
|
+
|
|
5
|
+
export interface JsonRpcRequest {
|
|
6
|
+
jsonrpc: "2.0";
|
|
7
|
+
id: string | number;
|
|
8
|
+
method: string;
|
|
9
|
+
params?: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface JsonRpcResponse {
|
|
13
|
+
jsonrpc: "2.0";
|
|
14
|
+
id: string | number;
|
|
15
|
+
result?: any;
|
|
16
|
+
error?: {
|
|
17
|
+
code: number;
|
|
18
|
+
message: string;
|
|
19
|
+
data?: any;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface RpcMethodContext {
|
|
24
|
+
svm: LiteSVM;
|
|
25
|
+
slot: bigint;
|
|
26
|
+
blockHeight: bigint;
|
|
27
|
+
store?: TxStore;
|
|
28
|
+
encodeBase58: (bytes: Uint8Array) => string;
|
|
29
|
+
decodeBase58: (str: string) => Uint8Array;
|
|
30
|
+
createSuccessResponse: (id: string | number, result: any) => JsonRpcResponse;
|
|
31
|
+
createErrorResponse: (
|
|
32
|
+
id: string | number,
|
|
33
|
+
code: number,
|
|
34
|
+
message: string,
|
|
35
|
+
data?: any,
|
|
36
|
+
) => JsonRpcResponse;
|
|
37
|
+
notifySignature: (signature: string) => void;
|
|
38
|
+
getFaucet: () => Keypair;
|
|
39
|
+
getTxCount: () => bigint;
|
|
40
|
+
registerMint?: (mint: PublicKey | string) => void;
|
|
41
|
+
listMints?: () => string[];
|
|
42
|
+
registerProgram?: (program: PublicKey | string) => void;
|
|
43
|
+
listPrograms?: () => string[];
|
|
44
|
+
recordTransaction: (
|
|
45
|
+
signature: string,
|
|
46
|
+
tx: VersionedTransaction,
|
|
47
|
+
meta?: {
|
|
48
|
+
logs?: string[];
|
|
49
|
+
err?: unknown;
|
|
50
|
+
fee?: number;
|
|
51
|
+
blockTime?: number;
|
|
52
|
+
preBalances?: number[];
|
|
53
|
+
postBalances?: number[];
|
|
54
|
+
},
|
|
55
|
+
) => void;
|
|
56
|
+
getRecordedTransaction: (signature: string) =>
|
|
57
|
+
| {
|
|
58
|
+
tx: VersionedTransaction;
|
|
59
|
+
logs: string[];
|
|
60
|
+
err: unknown;
|
|
61
|
+
fee: number;
|
|
62
|
+
slot: number;
|
|
63
|
+
blockTime?: number;
|
|
64
|
+
preBalances?: number[];
|
|
65
|
+
postBalances?: number[];
|
|
66
|
+
}
|
|
67
|
+
| undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type RpcMethodHandler = (
|
|
71
|
+
id: string | number,
|
|
72
|
+
params: any,
|
|
73
|
+
context: RpcMethodContext,
|
|
74
|
+
) => JsonRpcResponse | Promise<JsonRpcResponse>;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type { Server } from "bun";
|
|
2
|
+
import type { LiteSVMRpcServer } from "./rpc-server";
|
|
3
|
+
|
|
4
|
+
type Sub = { id: number; type: "signature"; signature: string };
|
|
5
|
+
|
|
6
|
+
export function createLiteSVMWebSocketServer(
|
|
7
|
+
rpcServer: LiteSVMRpcServer,
|
|
8
|
+
port: number = 8900,
|
|
9
|
+
) {
|
|
10
|
+
let nextSubId = 1;
|
|
11
|
+
const subs = new Map<number, Sub>();
|
|
12
|
+
|
|
13
|
+
const sockets = new Set<WebSocket>();
|
|
14
|
+
const pendingChecks = new Map<string, number>();
|
|
15
|
+
|
|
16
|
+
const sendSignatureNotification = (sig: string, slot: number, err: any) => {
|
|
17
|
+
const payload = {
|
|
18
|
+
jsonrpc: "2.0",
|
|
19
|
+
method: "signatureNotification",
|
|
20
|
+
params: {
|
|
21
|
+
result: { context: { slot }, value: { err } },
|
|
22
|
+
},
|
|
23
|
+
} as const;
|
|
24
|
+
for (const [id, sub] of subs.entries()) {
|
|
25
|
+
if (sub.type === "signature" && sub.signature === sig) {
|
|
26
|
+
try {
|
|
27
|
+
sockets.forEach((s) =>
|
|
28
|
+
s.send(
|
|
29
|
+
JSON.stringify({
|
|
30
|
+
...payload,
|
|
31
|
+
params: { ...payload.params, subscription: id },
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
);
|
|
35
|
+
} catch {}
|
|
36
|
+
subs.delete(id);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const scheduleSignatureCheck = (sig: string) => {
|
|
42
|
+
if (pendingChecks.has(sig)) return;
|
|
43
|
+
pendingChecks.set(sig, 0);
|
|
44
|
+
const tick = () => {
|
|
45
|
+
const tries = (pendingChecks.get(sig) ?? 0) + 1;
|
|
46
|
+
pendingChecks.set(sig, tries);
|
|
47
|
+
const status = rpcServer.getSignatureStatus(sig);
|
|
48
|
+
if (status) {
|
|
49
|
+
pendingChecks.delete(sig);
|
|
50
|
+
sendSignatureNotification(sig, status.slot, status.err);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (tries < 60) {
|
|
54
|
+
setTimeout(tick, 25);
|
|
55
|
+
} else {
|
|
56
|
+
pendingChecks.delete(sig);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
setTimeout(tick, 10);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const notifySignature = (sig: string) => {
|
|
63
|
+
scheduleSignatureCheck(sig);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const unsubscribe = rpcServer.onSignatureRecorded(notifySignature);
|
|
67
|
+
|
|
68
|
+
const server: Server = Bun.serve({
|
|
69
|
+
port,
|
|
70
|
+
fetch(req, srv) {
|
|
71
|
+
if (srv.upgrade(req)) return undefined as any;
|
|
72
|
+
return new Response("Not a websocket", { status: 400 });
|
|
73
|
+
},
|
|
74
|
+
websocket: {
|
|
75
|
+
open(ws) {
|
|
76
|
+
sockets.add(ws);
|
|
77
|
+
},
|
|
78
|
+
close(ws) {
|
|
79
|
+
sockets.delete(ws);
|
|
80
|
+
},
|
|
81
|
+
message(ws, data) {
|
|
82
|
+
try {
|
|
83
|
+
const msg = JSON.parse(
|
|
84
|
+
typeof data === "string"
|
|
85
|
+
? data
|
|
86
|
+
: Buffer.from(data as ArrayBuffer).toString("utf8"),
|
|
87
|
+
);
|
|
88
|
+
const {
|
|
89
|
+
id,
|
|
90
|
+
method,
|
|
91
|
+
params = [],
|
|
92
|
+
} = msg as { id: number; method: string; params?: any[] };
|
|
93
|
+
if (method === "signatureSubscribe") {
|
|
94
|
+
const [signature] = params;
|
|
95
|
+
const subId = nextSubId++;
|
|
96
|
+
subs.set(subId, { id: subId, type: "signature", signature });
|
|
97
|
+
// Respond with subscription id
|
|
98
|
+
ws.send(JSON.stringify({ jsonrpc: "2.0", id, result: subId }));
|
|
99
|
+
// If already have a status, notify immediately
|
|
100
|
+
const status = rpcServer.getSignatureStatus(signature);
|
|
101
|
+
if (status) {
|
|
102
|
+
ws.send(
|
|
103
|
+
JSON.stringify({
|
|
104
|
+
jsonrpc: "2.0",
|
|
105
|
+
method: "signatureNotification",
|
|
106
|
+
params: {
|
|
107
|
+
result: {
|
|
108
|
+
context: { slot: status.slot },
|
|
109
|
+
value: { err: status.err },
|
|
110
|
+
},
|
|
111
|
+
subscription: subId,
|
|
112
|
+
},
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
subs.delete(subId);
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (method === "signatureUnsubscribe") {
|
|
120
|
+
const [subId] = params;
|
|
121
|
+
subs.delete(subId);
|
|
122
|
+
ws.send(JSON.stringify({ jsonrpc: "2.0", id, result: true }));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// Stub other subs to succeed without notifications
|
|
126
|
+
if (
|
|
127
|
+
method === "logsSubscribe" ||
|
|
128
|
+
method === "slotSubscribe" ||
|
|
129
|
+
method === "programSubscribe" ||
|
|
130
|
+
method === "blockSubscribe"
|
|
131
|
+
) {
|
|
132
|
+
const subId = nextSubId++;
|
|
133
|
+
ws.send(JSON.stringify({ jsonrpc: "2.0", id, result: subId }));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (method === "ping") {
|
|
137
|
+
ws.send(JSON.stringify({ jsonrpc: "2.0", id, result: null }));
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
// Method not found (ws)
|
|
141
|
+
ws.send(
|
|
142
|
+
JSON.stringify({
|
|
143
|
+
jsonrpc: "2.0",
|
|
144
|
+
id,
|
|
145
|
+
error: { code: -32601, message: `Method not found: ${method}` },
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
try {
|
|
150
|
+
ws.send(
|
|
151
|
+
JSON.stringify({
|
|
152
|
+
jsonrpc: "2.0",
|
|
153
|
+
id: null,
|
|
154
|
+
error: { code: -32700, message: "Parse error" },
|
|
155
|
+
}),
|
|
156
|
+
);
|
|
157
|
+
} catch {}
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
console.log(`📣 LiteSVM RPC PubSub running on ws://localhost:${port}`);
|
|
164
|
+
return {
|
|
165
|
+
wsServer: server,
|
|
166
|
+
stop: () => {
|
|
167
|
+
unsubscribe();
|
|
168
|
+
server.stop(true);
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|