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,237 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExtensionType,
|
|
3
|
+
getExtensionData,
|
|
4
|
+
getExtensionTypes,
|
|
5
|
+
getMetadataPointerState,
|
|
6
|
+
TOKEN_2022_PROGRAM_ID,
|
|
7
|
+
TOKEN_PROGRAM_ID,
|
|
8
|
+
unpackAccount,
|
|
9
|
+
unpackMint,
|
|
10
|
+
} from "@solana/spl-token";
|
|
11
|
+
import { unpack as unpackTokenMetadata } from "@solana/spl-token-metadata";
|
|
12
|
+
import { type AccountInfo, PublicKey } from "@solana/web3.js";
|
|
13
|
+
import type { RpcMethodContext } from "../../../server/types";
|
|
14
|
+
|
|
15
|
+
export function parseSplTokenAccountOrMint(
|
|
16
|
+
pubkey: PublicKey,
|
|
17
|
+
ownerPk: PublicKey,
|
|
18
|
+
data: Uint8Array,
|
|
19
|
+
context: RpcMethodContext,
|
|
20
|
+
) {
|
|
21
|
+
const isTokenOwner =
|
|
22
|
+
ownerPk.equals(TOKEN_PROGRAM_ID) || ownerPk.equals(TOKEN_2022_PROGRAM_ID);
|
|
23
|
+
if (!isTokenOwner) return null;
|
|
24
|
+
const programPk = ownerPk.equals(TOKEN_2022_PROGRAM_ID)
|
|
25
|
+
? TOKEN_2022_PROGRAM_ID
|
|
26
|
+
: TOKEN_PROGRAM_ID;
|
|
27
|
+
const programLabel = ownerPk.equals(TOKEN_2022_PROGRAM_ID)
|
|
28
|
+
? "spl-token-2022"
|
|
29
|
+
: "spl-token";
|
|
30
|
+
const space = data.length;
|
|
31
|
+
|
|
32
|
+
// Try token account first
|
|
33
|
+
if (space >= 165) {
|
|
34
|
+
try {
|
|
35
|
+
const dec = unpackAccount(
|
|
36
|
+
pubkey,
|
|
37
|
+
toAccountInfo(
|
|
38
|
+
{
|
|
39
|
+
data,
|
|
40
|
+
lamports: 0,
|
|
41
|
+
owner: ownerPk,
|
|
42
|
+
executable: false,
|
|
43
|
+
rentEpoch: 0,
|
|
44
|
+
},
|
|
45
|
+
ownerPk,
|
|
46
|
+
),
|
|
47
|
+
programPk,
|
|
48
|
+
);
|
|
49
|
+
// fetch mint decimals
|
|
50
|
+
let decimals = 0;
|
|
51
|
+
try {
|
|
52
|
+
const mintAcc = context.svm.getAccount(dec.mint);
|
|
53
|
+
if (mintAcc) {
|
|
54
|
+
const mintOwner =
|
|
55
|
+
typeof (mintAcc as any).owner?.toBase58 === "function"
|
|
56
|
+
? ((mintAcc as any).owner as PublicKey)
|
|
57
|
+
: new PublicKey(mintAcc.owner);
|
|
58
|
+
const mintProg = mintOwner.equals(TOKEN_2022_PROGRAM_ID)
|
|
59
|
+
? TOKEN_2022_PROGRAM_ID
|
|
60
|
+
: TOKEN_PROGRAM_ID;
|
|
61
|
+
const mi = unpackMint(
|
|
62
|
+
dec.mint,
|
|
63
|
+
toAccountInfo(mintAcc, mintOwner),
|
|
64
|
+
mintProg,
|
|
65
|
+
);
|
|
66
|
+
decimals = mi?.decimals ?? 0;
|
|
67
|
+
}
|
|
68
|
+
} catch {}
|
|
69
|
+
const amount = BigInt(dec.amount?.toString?.() ?? dec.amount ?? 0);
|
|
70
|
+
const ui = Number.isFinite(decimals)
|
|
71
|
+
? Number(amount) / 10 ** decimals
|
|
72
|
+
: null;
|
|
73
|
+
const state = dec.isFrozen
|
|
74
|
+
? "frozen"
|
|
75
|
+
: dec.isInitialized
|
|
76
|
+
? "initialized"
|
|
77
|
+
: "uninitialized";
|
|
78
|
+
const delegated = BigInt(
|
|
79
|
+
dec.delegatedAmount?.toString?.() ?? dec.delegatedAmount ?? 0n,
|
|
80
|
+
);
|
|
81
|
+
const delegatedUi = Number.isFinite(decimals)
|
|
82
|
+
? Number(delegated) / 10 ** decimals
|
|
83
|
+
: null;
|
|
84
|
+
const rentExemptReserve = dec.isNative
|
|
85
|
+
? {
|
|
86
|
+
amount: BigInt(
|
|
87
|
+
dec.rentExemptReserve?.toString?.() ?? dec.rentExemptReserve ?? 0,
|
|
88
|
+
).toString(),
|
|
89
|
+
decimals: 9,
|
|
90
|
+
uiAmount: null,
|
|
91
|
+
uiAmountString: "0",
|
|
92
|
+
}
|
|
93
|
+
: null;
|
|
94
|
+
const extensions = buildAccountExtensions(dec);
|
|
95
|
+
return {
|
|
96
|
+
program: programLabel,
|
|
97
|
+
parsed: {
|
|
98
|
+
type: "account",
|
|
99
|
+
info: {
|
|
100
|
+
mint: dec.mint.toBase58(),
|
|
101
|
+
owner: dec.owner.toBase58(),
|
|
102
|
+
tokenAmount: {
|
|
103
|
+
amount: amount.toString(),
|
|
104
|
+
decimals,
|
|
105
|
+
uiAmount: ui,
|
|
106
|
+
uiAmountString: (ui ?? 0).toString(),
|
|
107
|
+
},
|
|
108
|
+
state,
|
|
109
|
+
isNative: !!dec.isNative,
|
|
110
|
+
delegatedAmount: {
|
|
111
|
+
amount: delegated.toString(),
|
|
112
|
+
decimals,
|
|
113
|
+
uiAmount: delegatedUi,
|
|
114
|
+
uiAmountString: (delegatedUi ?? 0).toString(),
|
|
115
|
+
},
|
|
116
|
+
delegate: dec.delegate ? dec.delegate.toBase58() : null,
|
|
117
|
+
rentExemptReserve,
|
|
118
|
+
closeAuthority: dec.closeAuthority
|
|
119
|
+
? dec.closeAuthority.toBase58()
|
|
120
|
+
: null,
|
|
121
|
+
extensions,
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
space,
|
|
125
|
+
};
|
|
126
|
+
} catch {}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Try mint
|
|
130
|
+
try {
|
|
131
|
+
const dec = unpackMint(
|
|
132
|
+
pubkey,
|
|
133
|
+
toAccountInfo(
|
|
134
|
+
{ data, lamports: 0, owner: ownerPk, executable: false, rentEpoch: 0 },
|
|
135
|
+
ownerPk,
|
|
136
|
+
),
|
|
137
|
+
programPk,
|
|
138
|
+
);
|
|
139
|
+
const supply = BigInt(dec.supply?.toString?.() ?? dec.supply ?? 0);
|
|
140
|
+
const mintAuthority = dec.mintAuthority
|
|
141
|
+
? dec.mintAuthority.toBase58()
|
|
142
|
+
: null;
|
|
143
|
+
const freezeAuthority = dec.freezeAuthority
|
|
144
|
+
? dec.freezeAuthority.toBase58()
|
|
145
|
+
: null;
|
|
146
|
+
const extensions = buildMintExtensions(dec);
|
|
147
|
+
return {
|
|
148
|
+
program: programLabel,
|
|
149
|
+
parsed: {
|
|
150
|
+
type: "mint",
|
|
151
|
+
info: {
|
|
152
|
+
mintAuthority,
|
|
153
|
+
supply: supply.toString(),
|
|
154
|
+
decimals: dec.decimals,
|
|
155
|
+
isInitialized: !!dec.isInitialized,
|
|
156
|
+
freezeAuthority,
|
|
157
|
+
extensions,
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
space,
|
|
161
|
+
};
|
|
162
|
+
} catch {}
|
|
163
|
+
|
|
164
|
+
// Fallback if not parsed
|
|
165
|
+
return { program: programLabel, parsed: null, space };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function buildAccountExtensions(account: {
|
|
169
|
+
tlvData: Buffer;
|
|
170
|
+
}): Array<{ type: string }> | undefined {
|
|
171
|
+
if (!account.tlvData || account.tlvData.length === 0) return undefined;
|
|
172
|
+
const types = getExtensionTypes(account.tlvData);
|
|
173
|
+
if (!types.length) return undefined;
|
|
174
|
+
return types.map((ext) => ({ type: ExtensionType[ext] ?? String(ext) }));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function buildMintExtensions(mint: {
|
|
178
|
+
tlvData: Buffer;
|
|
179
|
+
}): Array<Record<string, any>> | undefined {
|
|
180
|
+
if (!mint.tlvData || mint.tlvData.length === 0) return undefined;
|
|
181
|
+
const types = getExtensionTypes(mint.tlvData);
|
|
182
|
+
if (!types.length) return undefined;
|
|
183
|
+
const out: Array<Record<string, any>> = [];
|
|
184
|
+
for (const ext of types) {
|
|
185
|
+
const entry: Record<string, any> = {
|
|
186
|
+
type: ExtensionType[ext] ?? String(ext),
|
|
187
|
+
};
|
|
188
|
+
try {
|
|
189
|
+
if (ext === ExtensionType.MetadataPointer) {
|
|
190
|
+
const state = getMetadataPointerState(mint as any);
|
|
191
|
+
if (state) {
|
|
192
|
+
entry.info = {
|
|
193
|
+
authority: state.authority ? state.authority.toBase58() : null,
|
|
194
|
+
metadataAddress: state.metadataAddress
|
|
195
|
+
? state.metadataAddress.toBase58()
|
|
196
|
+
: null,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
} else if (ext === ExtensionType.TokenMetadata) {
|
|
200
|
+
const data = getExtensionData(ext, mint.tlvData);
|
|
201
|
+
if (data) {
|
|
202
|
+
const meta = unpackTokenMetadata(data);
|
|
203
|
+
entry.info = {
|
|
204
|
+
updateAuthority: meta.updateAuthority
|
|
205
|
+
? meta.updateAuthority.toBase58()
|
|
206
|
+
: null,
|
|
207
|
+
mint: meta.mint.toBase58(),
|
|
208
|
+
name: meta.name,
|
|
209
|
+
symbol: meta.symbol,
|
|
210
|
+
uri: meta.uri,
|
|
211
|
+
additionalMetadata: meta.additionalMetadata.map(([k, v]) => [k, v]),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
} catch (error) {
|
|
216
|
+
try {
|
|
217
|
+
console.warn("[rpc] decode mint extension failed", error);
|
|
218
|
+
} catch {}
|
|
219
|
+
}
|
|
220
|
+
out.push(entry);
|
|
221
|
+
}
|
|
222
|
+
return out.length ? out : undefined;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function toAccountInfo(raw: any, owner: PublicKey): AccountInfo<Buffer> {
|
|
226
|
+
const data =
|
|
227
|
+
raw.data instanceof Buffer ? raw.data : Buffer.from(raw.data ?? []);
|
|
228
|
+
return {
|
|
229
|
+
data,
|
|
230
|
+
executable: !!raw.executable,
|
|
231
|
+
lamports: Number(
|
|
232
|
+
typeof raw.lamports === "bigint" ? raw.lamports : (raw.lamports ?? 0),
|
|
233
|
+
),
|
|
234
|
+
owner,
|
|
235
|
+
rentEpoch: Number(raw.rentEpoch ?? 0),
|
|
236
|
+
} as AccountInfo<Buffer>;
|
|
237
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PublicKey,
|
|
3
|
+
SystemProgram,
|
|
4
|
+
TransactionInstruction,
|
|
5
|
+
TransactionMessage,
|
|
6
|
+
VersionedTransaction,
|
|
7
|
+
} from "@solana/web3.js";
|
|
8
|
+
import type { RpcMethodHandler } from "../../types";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Implements the requestAirdrop RPC method
|
|
12
|
+
* @see https://docs.solana.com/api/http#requestairdrop
|
|
13
|
+
*/
|
|
14
|
+
export const requestAirdrop: RpcMethodHandler = (id, params, context) => {
|
|
15
|
+
const [pubkeyStr, lamports, config] = params || [];
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const toPubkey = new PublicKey(pubkeyStr);
|
|
19
|
+
const faucet = context.getFaucet();
|
|
20
|
+
// Use SVM's latest blockhash; uniqueness ensured via memo nonce
|
|
21
|
+
let recentBlockhash: string | undefined;
|
|
22
|
+
try {
|
|
23
|
+
recentBlockhash = context.svm.latestBlockhash();
|
|
24
|
+
} catch {}
|
|
25
|
+
if (!recentBlockhash) {
|
|
26
|
+
const bh = new Uint8Array(32);
|
|
27
|
+
crypto.getRandomValues(bh);
|
|
28
|
+
recentBlockhash = context.encodeBase58(bh);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// No per-request top-up; faucet is funded heavily at startup
|
|
32
|
+
|
|
33
|
+
const ix = SystemProgram.transfer({
|
|
34
|
+
fromPubkey: faucet.publicKey,
|
|
35
|
+
toPubkey,
|
|
36
|
+
lamports: Number(BigInt(lamports)),
|
|
37
|
+
});
|
|
38
|
+
// Add a memo with random nonce to guarantee unique signatures
|
|
39
|
+
const memoProgramId = new PublicKey(
|
|
40
|
+
"MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr",
|
|
41
|
+
);
|
|
42
|
+
const nonce = new Uint8Array(8);
|
|
43
|
+
crypto.getRandomValues(nonce);
|
|
44
|
+
const memoIx = new TransactionInstruction({
|
|
45
|
+
keys: [],
|
|
46
|
+
programId: memoProgramId,
|
|
47
|
+
data: Buffer.from(`airdrop:${Buffer.from(nonce).toString("hex")}`),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const messageV0 = new TransactionMessage({
|
|
51
|
+
payerKey: faucet.publicKey,
|
|
52
|
+
recentBlockhash,
|
|
53
|
+
instructions: [ix, memoIx],
|
|
54
|
+
});
|
|
55
|
+
// Prefer legacy message for maximum LiteSVM compatibility
|
|
56
|
+
const compiled = messageV0.compileToLegacyMessage();
|
|
57
|
+
|
|
58
|
+
const tx = new VersionedTransaction(compiled);
|
|
59
|
+
tx.sign([faucet]);
|
|
60
|
+
|
|
61
|
+
// Compute pre balances for all static account keys
|
|
62
|
+
const txMsg: any = tx.message as any;
|
|
63
|
+
const rawKeys: any[] = Array.isArray(txMsg.staticAccountKeys)
|
|
64
|
+
? txMsg.staticAccountKeys
|
|
65
|
+
: Array.isArray(txMsg.accountKeys)
|
|
66
|
+
? txMsg.accountKeys
|
|
67
|
+
: [];
|
|
68
|
+
const staticKeys = rawKeys.map((k: any) => {
|
|
69
|
+
try {
|
|
70
|
+
return typeof k === "string" ? new PublicKey(k) : (k as PublicKey);
|
|
71
|
+
} catch {
|
|
72
|
+
return faucet.publicKey;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
const preBalances = staticKeys.map((pk) => {
|
|
76
|
+
try {
|
|
77
|
+
return Number(context.svm.getBalance(pk));
|
|
78
|
+
} catch {
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
const toIndex = staticKeys.findIndex((pk) => pk.equals(toPubkey));
|
|
83
|
+
const beforeTo =
|
|
84
|
+
toIndex >= 0
|
|
85
|
+
? preBalances[toIndex]
|
|
86
|
+
: (() => {
|
|
87
|
+
try {
|
|
88
|
+
return Number(context.svm.getBalance(toPubkey));
|
|
89
|
+
} catch {
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
})();
|
|
93
|
+
|
|
94
|
+
const sendResult = context.svm.sendTransaction(tx);
|
|
95
|
+
// Surface errors to aid debugging
|
|
96
|
+
try {
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
98
|
+
const rawErrFun = (sendResult as any).err;
|
|
99
|
+
const maybeErr =
|
|
100
|
+
typeof rawErrFun === "function" ? rawErrFun() : rawErrFun;
|
|
101
|
+
if (maybeErr) {
|
|
102
|
+
let logsForErr: string[] = [];
|
|
103
|
+
try {
|
|
104
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
105
|
+
const anyRes: any = sendResult;
|
|
106
|
+
if (typeof anyRes?.logs === "function") logsForErr = anyRes.logs();
|
|
107
|
+
else if (typeof anyRes?.meta === "function")
|
|
108
|
+
logsForErr = anyRes.meta()?.logs?.() ?? [];
|
|
109
|
+
} catch {}
|
|
110
|
+
console.warn(
|
|
111
|
+
"[requestAirdrop] transfer failed. err=",
|
|
112
|
+
maybeErr,
|
|
113
|
+
" logs=\n",
|
|
114
|
+
logsForErr.join("\n"),
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
} catch {}
|
|
118
|
+
|
|
119
|
+
let signature = tx.signatures[0]
|
|
120
|
+
? context.encodeBase58(tx.signatures[0])
|
|
121
|
+
: context.encodeBase58(new Uint8Array(64).fill(0));
|
|
122
|
+
context.notifySignature(signature);
|
|
123
|
+
// Compute post balances and capture logs if available for explorer detail view
|
|
124
|
+
let postBalances = staticKeys.map((pk) => {
|
|
125
|
+
try {
|
|
126
|
+
return Number(context.svm.getBalance(pk));
|
|
127
|
+
} catch {
|
|
128
|
+
return 0;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
let logs: string[] = [];
|
|
132
|
+
try {
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
134
|
+
const anyRes: any = sendResult;
|
|
135
|
+
if (typeof anyRes?.logs === "function") logs = anyRes.logs();
|
|
136
|
+
else if (typeof anyRes?.meta === "function")
|
|
137
|
+
logs = anyRes.meta()?.logs?.() ?? [];
|
|
138
|
+
} catch {}
|
|
139
|
+
// Verify recipient received lamports; retry once if not
|
|
140
|
+
const afterTo =
|
|
141
|
+
toIndex >= 0
|
|
142
|
+
? postBalances[toIndex]
|
|
143
|
+
: (() => {
|
|
144
|
+
try {
|
|
145
|
+
return Number(context.svm.getBalance(toPubkey));
|
|
146
|
+
} catch {
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
})();
|
|
150
|
+
const expectedDelta = Number(BigInt(lamports));
|
|
151
|
+
if (afterTo - beforeTo < expectedDelta) {
|
|
152
|
+
// Retry once with fresh blockhash + memo
|
|
153
|
+
try {
|
|
154
|
+
const bh2 = new Uint8Array(32);
|
|
155
|
+
crypto.getRandomValues(bh2);
|
|
156
|
+
const rb2 = context.encodeBase58(bh2);
|
|
157
|
+
const memoProgramId2 = new PublicKey(
|
|
158
|
+
"MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr",
|
|
159
|
+
);
|
|
160
|
+
const nonce2 = new Uint8Array(8);
|
|
161
|
+
crypto.getRandomValues(nonce2);
|
|
162
|
+
const memoIx2 = new TransactionInstruction({
|
|
163
|
+
keys: [],
|
|
164
|
+
programId: memoProgramId2,
|
|
165
|
+
data: Buffer.from(
|
|
166
|
+
`airdrop-retry:${Buffer.from(nonce2).toString("hex")}`,
|
|
167
|
+
),
|
|
168
|
+
});
|
|
169
|
+
const msg2 = new TransactionMessage({
|
|
170
|
+
payerKey: faucet.publicKey,
|
|
171
|
+
recentBlockhash: rb2,
|
|
172
|
+
instructions: [ix, memoIx2],
|
|
173
|
+
}).compileToV0Message();
|
|
174
|
+
const tx2 = new VersionedTransaction(msg2);
|
|
175
|
+
tx2.sign([faucet]);
|
|
176
|
+
const res2 = context.svm.sendTransaction(tx2);
|
|
177
|
+
try {
|
|
178
|
+
const e2 = (res2 as any).err?.() ?? (res2 as any).err;
|
|
179
|
+
if (e2) console.warn("[requestAirdrop] retry failed:", e2);
|
|
180
|
+
} catch {}
|
|
181
|
+
signature = tx2.signatures[0]
|
|
182
|
+
? context.encodeBase58(tx2.signatures[0])
|
|
183
|
+
: signature;
|
|
184
|
+
context.notifySignature(signature);
|
|
185
|
+
postBalances = staticKeys.map((pk) => {
|
|
186
|
+
try {
|
|
187
|
+
return Number(context.svm.getBalance(pk));
|
|
188
|
+
} catch {
|
|
189
|
+
return 0;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
} catch {}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Try to capture error again for accurate status reporting
|
|
196
|
+
let recErr: any = null;
|
|
197
|
+
try {
|
|
198
|
+
const rawErrFun = (sendResult as any).err;
|
|
199
|
+
recErr = typeof rawErrFun === "function" ? rawErrFun() : rawErrFun;
|
|
200
|
+
} catch {}
|
|
201
|
+
context.recordTransaction(signature, tx, {
|
|
202
|
+
logs,
|
|
203
|
+
fee: 5000,
|
|
204
|
+
blockTime: Math.floor(Date.now() / 1000),
|
|
205
|
+
preBalances,
|
|
206
|
+
postBalances,
|
|
207
|
+
err: recErr,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
return context.createSuccessResponse(id, signature);
|
|
211
|
+
} catch (error: any) {
|
|
212
|
+
return context.createErrorResponse(
|
|
213
|
+
id,
|
|
214
|
+
-32602,
|
|
215
|
+
"Invalid params",
|
|
216
|
+
error.message,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { MINT_SIZE, MintLayout } from "@solana/spl-token";
|
|
2
|
+
import { PublicKey } from "@solana/web3.js";
|
|
3
|
+
import type { RpcMethodHandler } from "../../types";
|
|
4
|
+
|
|
5
|
+
// Adopt faucet as mint authority for a given mint (LiteSVM-only, overwrites account data)
|
|
6
|
+
export const solforgeAdoptMintAuthority: RpcMethodHandler = async (
|
|
7
|
+
id,
|
|
8
|
+
params,
|
|
9
|
+
context,
|
|
10
|
+
) => {
|
|
11
|
+
try {
|
|
12
|
+
const [mintStr] = params as [string];
|
|
13
|
+
if (!mintStr)
|
|
14
|
+
return context.createErrorResponse(
|
|
15
|
+
id,
|
|
16
|
+
-32602,
|
|
17
|
+
"Invalid params: mint required",
|
|
18
|
+
);
|
|
19
|
+
const mint = new PublicKey(mintStr);
|
|
20
|
+
const acct = context.svm.getAccount(mint);
|
|
21
|
+
if (!acct)
|
|
22
|
+
return context.createErrorResponse(
|
|
23
|
+
id,
|
|
24
|
+
-32004,
|
|
25
|
+
"Mint not found in LiteSVM",
|
|
26
|
+
);
|
|
27
|
+
if (!acct.data || acct.data.length < MINT_SIZE)
|
|
28
|
+
return context.createErrorResponse(
|
|
29
|
+
id,
|
|
30
|
+
-32000,
|
|
31
|
+
"Account not a valid mint",
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const faucet = context.getFaucet();
|
|
35
|
+
const buf = Buffer.from(acct.data);
|
|
36
|
+
type MintStruct = Parameters<typeof MintLayout.encode>[0];
|
|
37
|
+
const mintDecoded = MintLayout.decode(
|
|
38
|
+
buf.slice(0, MINT_SIZE),
|
|
39
|
+
) as unknown as MintStruct;
|
|
40
|
+
// Update authority fields
|
|
41
|
+
(
|
|
42
|
+
mintDecoded as unknown as { mintAuthorityOption: number }
|
|
43
|
+
).mintAuthorityOption = 1;
|
|
44
|
+
(mintDecoded as unknown as { mintAuthority: PublicKey }).mintAuthority =
|
|
45
|
+
faucet.publicKey;
|
|
46
|
+
const out = Buffer.from(buf); // preserve any extensions beyond MintLayout
|
|
47
|
+
MintLayout.encode(mintDecoded as MintStruct, out);
|
|
48
|
+
|
|
49
|
+
const ownerBase58 =
|
|
50
|
+
typeof acct.owner === "string"
|
|
51
|
+
? new PublicKey(acct.owner).toBase58()
|
|
52
|
+
: (acct.owner as PublicKey).toBase58();
|
|
53
|
+
const ownerPk = new PublicKey(ownerBase58);
|
|
54
|
+
|
|
55
|
+
context.svm.setAccount(mint, {
|
|
56
|
+
lamports: Number(acct.lamports || 0n),
|
|
57
|
+
data: new Uint8Array(out),
|
|
58
|
+
owner: ownerPk,
|
|
59
|
+
executable: false,
|
|
60
|
+
rentEpoch: 0,
|
|
61
|
+
});
|
|
62
|
+
try {
|
|
63
|
+
context.registerMint?.(mint);
|
|
64
|
+
} catch {}
|
|
65
|
+
try {
|
|
66
|
+
await context.store?.upsertAccounts([
|
|
67
|
+
{
|
|
68
|
+
address: mint.toBase58(),
|
|
69
|
+
lamports: Number(acct.lamports || 0n),
|
|
70
|
+
ownerProgram: ownerBase58,
|
|
71
|
+
executable: false,
|
|
72
|
+
rentEpoch: 0,
|
|
73
|
+
dataLen: out.length,
|
|
74
|
+
dataBase64: undefined,
|
|
75
|
+
lastSlot: Number(context.slot),
|
|
76
|
+
},
|
|
77
|
+
]);
|
|
78
|
+
} catch {}
|
|
79
|
+
return context.createSuccessResponse(id, {
|
|
80
|
+
ok: true,
|
|
81
|
+
mint: mintStr,
|
|
82
|
+
authority: faucet.publicKey.toBase58(),
|
|
83
|
+
});
|
|
84
|
+
} catch (e) {
|
|
85
|
+
return context.createErrorResponse(
|
|
86
|
+
id,
|
|
87
|
+
-32603,
|
|
88
|
+
"Adopt mint authority failed",
|
|
89
|
+
(e as Error)?.message || String(e),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export type { RpcMethodHandler } from "../../types";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Connection, PublicKey } from "@solana/web3.js";
|
|
2
|
+
import type { RpcMethodHandler } from "../../types";
|
|
3
|
+
|
|
4
|
+
export const solforgeAdminCloneProgramAccounts: RpcMethodHandler = async (
|
|
5
|
+
id,
|
|
6
|
+
params,
|
|
7
|
+
context,
|
|
8
|
+
) => {
|
|
9
|
+
const [programId, options] = params as [
|
|
10
|
+
string,
|
|
11
|
+
{ endpoint?: string; limit?: number; filters?: unknown[] }?,
|
|
12
|
+
];
|
|
13
|
+
if (!programId)
|
|
14
|
+
return context.createErrorResponse(
|
|
15
|
+
id,
|
|
16
|
+
-32602,
|
|
17
|
+
"Invalid params: programId required",
|
|
18
|
+
);
|
|
19
|
+
const endpoint = options?.endpoint || "https://api.mainnet-beta.solana.com";
|
|
20
|
+
const limit = options?.limit
|
|
21
|
+
? Math.max(1, Math.min(10000, options.limit))
|
|
22
|
+
: undefined;
|
|
23
|
+
try {
|
|
24
|
+
const conn = new Connection(endpoint, "confirmed");
|
|
25
|
+
const pid = new PublicKey(programId);
|
|
26
|
+
const list = await conn.getProgramAccounts(pid, {
|
|
27
|
+
commitment: "confirmed",
|
|
28
|
+
// @ts-expect-error: filters type is loose
|
|
29
|
+
filters: Array.isArray(options?.filters) ? options?.filters : undefined,
|
|
30
|
+
});
|
|
31
|
+
let count = 0;
|
|
32
|
+
for (const { pubkey, account } of list.slice(0, limit ?? list.length)) {
|
|
33
|
+
try {
|
|
34
|
+
context.svm.setAccount(pubkey, {
|
|
35
|
+
data: new Uint8Array(account.data as Buffer),
|
|
36
|
+
executable: account.executable,
|
|
37
|
+
lamports: Number(account.lamports),
|
|
38
|
+
owner: account.owner,
|
|
39
|
+
rentEpoch: 0,
|
|
40
|
+
});
|
|
41
|
+
count++;
|
|
42
|
+
} catch {}
|
|
43
|
+
}
|
|
44
|
+
return context.createSuccessResponse(id, { ok: true, count });
|
|
45
|
+
} catch (e) {
|
|
46
|
+
return context.createErrorResponse(
|
|
47
|
+
id,
|
|
48
|
+
-32603,
|
|
49
|
+
"Clone program accounts failed",
|
|
50
|
+
(e as Error)?.message || String(e),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type { RpcMethodHandler } from "../../types";
|