nansen-cli 2.2.0 → 2.4.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/CHANGELOG.md +108 -0
- package/README.md +109 -33
- package/docs/auth.md +56 -0
- package/docs/browser-login.md +101 -7
- package/docs/error-codes.md +249 -0
- package/package.json +10 -4
- package/skills/nansen-agent-guide/SKILL.md +54 -1
- package/skills/nansen-alerts-webhook-listener/SKILL.md +1 -1
- package/skills/nansen-dca-tracker/SKILL.md +1 -1
- package/skills/nansen-defi-positions/SKILL.md +1 -1
- package/skills/nansen-exit-signals/SKILL.md +2 -2
- package/skills/nansen-fund-tracker/SKILL.md +1 -1
- package/skills/nansen-general-search/SKILL.md +1 -1
- package/skills/nansen-holder-analysis/SKILL.md +2 -2
- package/skills/nansen-limit-orders/SKILL.md +1 -1
- package/skills/nansen-mpp-payment/SKILL.md +2 -2
- package/skills/nansen-perp-screener/SKILL.md +1 -1
- package/skills/nansen-perp-trader-profile/SKILL.md +1 -1
- package/skills/nansen-polymarket-deep-dive/SKILL.md +1 -1
- package/skills/nansen-polymarket-insider-scan/SKILL.md +1 -1
- package/skills/nansen-polymarket-trader-profile/SKILL.md +1 -1
- package/skills/nansen-portfolio-tracker/SKILL.md +1 -1
- package/skills/nansen-prediction-markets/SKILL.md +1 -1
- package/skills/nansen-sm-cross-chain-flows/SKILL.md +1 -1
- package/skills/nansen-smart-alerts/SKILL.md +2 -1
- package/skills/nansen-smart-money-alpha/SKILL.md +1 -1
- package/skills/nansen-smart-money-tracker/SKILL.md +8 -7
- package/skills/nansen-smart-money-trend/SKILL.md +1 -1
- package/skills/nansen-token-research/SKILL.md +1 -1
- package/skills/nansen-token-screener/SKILL.md +1 -1
- package/skills/nansen-token-transfer-analysis/SKILL.md +1 -1
- package/skills/nansen-trading/SKILL.md +1 -1
- package/skills/nansen-wallet-batch/SKILL.md +1 -1
- package/skills/nansen-wallet-clustering/SKILL.md +1 -1
- package/skills/nansen-wallet-deep-dive/SKILL.md +1 -1
- package/skills/nansen-wallet-keychain-migration/SKILL.md +1 -1
- package/skills/nansen-wallet-manager/SKILL.md +1 -1
- package/skills/nansen-wallet-profiler/SKILL.md +1 -1
- package/skills/nansen-web-fetcher/SKILL.md +1 -1
- package/skills/nansen-web-searcher/SKILL.md +1 -1
- package/src/api-endpoints.json +1605 -0
- package/src/api.js +336 -121
- package/src/auth-credentials.js +1 -0
- package/src/auth-state.js +2 -2
- package/src/auth-store-native.js +6 -1
- package/src/auth-store-worker.js +1 -1
- package/src/auth-store.js +9 -4
- package/src/bridge.js +19 -10
- package/src/cli.js +258 -107
- package/src/commands/agent.js +2 -3
- package/src/commands/alerts.js +277 -9
- package/src/commands/completion.js +6 -1
- package/src/commands/feedback.js +413 -0
- package/src/commands/mcp.js +1 -1
- package/src/commands/research.js +14 -37
- package/src/cost-cache.js +33 -7
- package/src/doctor.js +14 -5
- package/src/error-codes.js +226 -0
- package/src/feedback-journal.js +86 -0
- package/src/input-validation.js +551 -0
- package/src/limit-order.js +12 -3
- package/src/perp.js +2 -1
- package/src/privy.js +144 -48
- package/src/query-options.js +30 -7
- package/src/rpc-urls.js +3 -1
- package/src/schema.json +1254 -150
- package/src/solana-tx.js +4 -0
- package/src/telemetry.js +5 -1
- package/src/trade-validation.js +190 -21
- package/src/trading.js +229 -129
- package/src/transfer.js +7 -1
- package/src/update-check.js +32 -5
- package/src/wallet.js +220 -57
- package/src/walletconnect-x402.js +152 -52
- package/src/x402-audit.js +191 -0
- package/src/x402-payment-balance.js +90 -0
- package/src/x402-policy.js +151 -4
- package/src/x402.js +51 -22
package/src/solana-tx.js
CHANGED
|
@@ -28,6 +28,8 @@ export function readCompactU16(buf, offset) {
|
|
|
28
28
|
* [compact-u16 sigCount][sigCount * 64-byte signatures][message]) into its
|
|
29
29
|
* header, static account keys, instructions, and (v0 only) address-table
|
|
30
30
|
* lookups. `data` on instructions is returned undecoded (raw Buffer).
|
|
31
|
+
* `messageBase64` is the serialized message (everything after the
|
|
32
|
+
* signatures), the form RPC getFeeForMessage takes.
|
|
31
33
|
*/
|
|
32
34
|
export function parseTransactionMessage(base64) {
|
|
33
35
|
const bytes = Buffer.from(base64, 'base64');
|
|
@@ -41,6 +43,7 @@ export function parseTransactionMessage(base64) {
|
|
|
41
43
|
};
|
|
42
44
|
const { value: numSignatures, size: sigCountSize } = readCompactU16(bytes, 0);
|
|
43
45
|
let offset = sigCountSize + numSignatures * 64;
|
|
46
|
+
const messageStart = offset;
|
|
44
47
|
|
|
45
48
|
requireBytes(offset, 1);
|
|
46
49
|
const first = bytes[offset];
|
|
@@ -136,6 +139,7 @@ export function parseTransactionMessage(base64) {
|
|
|
136
139
|
recentBlockhash,
|
|
137
140
|
instructions,
|
|
138
141
|
addressTableLookups,
|
|
142
|
+
messageBase64: bytes.subarray(messageStart).toString('base64'),
|
|
139
143
|
};
|
|
140
144
|
}
|
|
141
145
|
|
package/src/telemetry.js
CHANGED
|
@@ -232,12 +232,16 @@ function perpOutcomeEventId({ wallet_address, submission_id, leg_index, outcome
|
|
|
232
232
|
|
|
233
233
|
/**
|
|
234
234
|
* Convert a command string like "smart-money netflow" to a path like "/smart-money/netflow".
|
|
235
|
-
*
|
|
235
|
+
* Free text and malformed feedback arguments must never become path segments.
|
|
236
236
|
*/
|
|
237
237
|
function commandToPath(command) {
|
|
238
238
|
if (typeof command === 'string' && /^agent(?:\s+|$)/.test(command.trimStart())) {
|
|
239
239
|
return '/agent';
|
|
240
240
|
}
|
|
241
|
+
if (typeof command === 'string' && /^feedback(?:\s+|$)/.test(command.trimStart())) {
|
|
242
|
+
const subcommand = command.trim().split(/\s+/)[1];
|
|
243
|
+
return ['submit', 'get'].includes(subcommand) ? `/feedback/${subcommand}` : '/feedback';
|
|
244
|
+
}
|
|
241
245
|
return '/' + String(command).replace(/\s+/g, '/');
|
|
242
246
|
}
|
|
243
247
|
|
package/src/trade-validation.js
CHANGED
|
@@ -41,8 +41,27 @@ const COMPUTE_BUDGET_PROGRAM = 'ComputeBudget111111111111111111111111111111';
|
|
|
41
41
|
const COMPUTE_BUDGET_SET_UNIT_LIMIT = 2;
|
|
42
42
|
const COMPUTE_BUDGET_SET_UNIT_PRICE = 3;
|
|
43
43
|
const SOLANA_MAX_COMPUTE_UNITS = 1_400_000; // Solana's per-transaction compute-unit ceiling — the
|
|
44
|
-
// worst-case bound
|
|
45
|
-
// explicit limit instruction.
|
|
44
|
+
// worst-case bound the priority-fee cap assumes when a
|
|
45
|
+
// price is set with no explicit limit instruction. The
|
|
46
|
+
// runtime clamps any larger requested limit to it.
|
|
47
|
+
// The runtime's default limit when none is set: 200,000 CU per instruction,
|
|
48
|
+
// or 3,000 for a builtin program's instruction (Agave's builtin cost map).
|
|
49
|
+
// Vote is mid-migration to on-chain BPF; counting it as a builtin can only
|
|
50
|
+
// understate the fee, and an unpayable fee is dropped by the leader unsigned
|
|
51
|
+
// and uncharged, whereas overstating it falsely rejects the transaction.
|
|
52
|
+
const SOLANA_DEFAULT_INSTRUCTION_COMPUTE_UNITS = 200_000;
|
|
53
|
+
const SOLANA_DEFAULT_BUILTIN_COMPUTE_UNITS = 3_000;
|
|
54
|
+
const SOLANA_BUILTIN_PROGRAMS = new Set([
|
|
55
|
+
SYSTEM_PROGRAM,
|
|
56
|
+
COMPUTE_BUDGET_PROGRAM,
|
|
57
|
+
'BPFLoader1111111111111111111111111111111111',
|
|
58
|
+
'BPFLoader2111111111111111111111111111111111',
|
|
59
|
+
'BPFLoaderUpgradeab1e11111111111111111111111',
|
|
60
|
+
'LoaderV411111111111111111111111111111111111',
|
|
61
|
+
'KeccakSecp256k11111111111111111111111111111',
|
|
62
|
+
'Ed25519SigVerify111111111111111111111111111',
|
|
63
|
+
'Vote111111111111111111111111111111111111111',
|
|
64
|
+
]);
|
|
46
65
|
const MAX_PRIORITY_FEE_LAMPORTS = 10_000_000n; // 0.01 SOL sanity ceiling on the priority fee a
|
|
47
66
|
// single trade can be made to pay.
|
|
48
67
|
|
|
@@ -202,15 +221,27 @@ const USDC_ADDRESSES = {
|
|
|
202
221
|
// Native token symbols for error messages.
|
|
203
222
|
const NATIVE_SYMBOLS = { solana: 'SOL', base: 'ETH' };
|
|
204
223
|
|
|
205
|
-
|
|
224
|
+
// The floor a wallet must clear to be allowed to quote. Kept in step with
|
|
225
|
+
// FEE_BUFFER below: that is what the CLI itself holds back when selling the
|
|
226
|
+
// maximum amount of a native token, so a wallet sitting on exactly that reserve
|
|
227
|
+
// must not then be refused a quote. Solana's real cost is a 5000-lamport base
|
|
228
|
+
// fee plus at most ~0.00204 SOL of ATA rent, so 0.005 is still a wide margin.
|
|
229
|
+
//
|
|
230
|
+
// Deliberately NOT sized against MAX_PRIORITY_FEE_LAMPORTS (0.01 SOL). That is
|
|
231
|
+
// an anti-abuse ceiling we refuse to exceed, not a fee we expect to pay, and the
|
|
232
|
+
// worst spend it permits (~0.012044 SOL with base fee and rent) was never
|
|
233
|
+
// covered by this floor at any value it has held — 0.01 did not cover it either.
|
|
234
|
+
// Sizing a minimum-balance gate off that ceiling would block real wallets to
|
|
235
|
+
// defend against a fee level we only tolerate. This check is a best-effort
|
|
236
|
+
// "can you afford a normal trade" gate that also passes on RPC failure; the
|
|
237
|
+
// authority on "can you afford THIS trade" is the execute-time Solana
|
|
238
|
+
// simulation (verifySolanaSwapOutcome), which reads live balances and skips the
|
|
239
|
+
// quote rather than broadcasting an unpayable transaction.
|
|
240
|
+
const MIN_GAS_AMOUNTS = { solana: 0.005, base: 0.000024 };
|
|
206
241
|
const FEE_BUFFER = { solana: 0.005, base: 0.00004 };
|
|
207
242
|
const HIGH_PERCENTAGE_THRESHOLD = 95;
|
|
208
243
|
const AUTO_ADJUST_THRESHOLD_PERCENT = 2;
|
|
209
244
|
|
|
210
|
-
// Trades at or above this USD value can use gasless/solver-paid routes (e.g. Relay),
|
|
211
|
-
// so the native gas pre-check is skipped for them.
|
|
212
|
-
export const GASLESS_MIN_TRADE_USD = 10;
|
|
213
|
-
|
|
214
245
|
/**
|
|
215
246
|
* Check if an address is USDC or the native token for a chain (case-insensitive for EVM).
|
|
216
247
|
*/
|
|
@@ -382,18 +413,16 @@ export async function resolvePercentAmount({ chain, from, walletAddress, percent
|
|
|
382
413
|
* Returns { hasSufficientNative } or throws on validation failure.
|
|
383
414
|
* Best-effort: if RPC fails, returns passing result.
|
|
384
415
|
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
416
|
+
* Applies to every trade regardless of value. A $10 bypass used to live here for
|
|
417
|
+
* solver-paid routes, but it never looked at whether such a route was in play —
|
|
418
|
+
* it waved through any trade at or above the threshold on value alone, so a
|
|
419
|
+
* wallet with no native token passed validation and then failed at broadcast.
|
|
387
420
|
*/
|
|
388
|
-
export async function validateGasBalance({ chain, walletAddress
|
|
421
|
+
export async function validateGasBalance({ chain, walletAddress }) {
|
|
389
422
|
const normalizedChain = chain.toLowerCase();
|
|
390
423
|
const minGas = MIN_GAS_AMOUNTS[normalizedChain];
|
|
391
424
|
if (minGas === undefined) return { hasSufficientNative: true };
|
|
392
425
|
|
|
393
|
-
// High-value trades can use gasless/solver-paid routes — skip the check.
|
|
394
|
-
const tradeUsd = parseFloat(tradeValueUsd) || 0;
|
|
395
|
-
if (tradeUsd >= GASLESS_MIN_TRADE_USD) return { hasSufficientNative: true };
|
|
396
|
-
|
|
397
426
|
const balance = await fetchNativeBalance(normalizedChain, walletAddress);
|
|
398
427
|
|
|
399
428
|
// RPC failure — proceed without validation.
|
|
@@ -405,7 +434,7 @@ export async function validateGasBalance({ chain, walletAddress, tradeValueUsd }
|
|
|
405
434
|
|
|
406
435
|
const symbol = NATIVE_SYMBOLS[normalizedChain] || 'native token';
|
|
407
436
|
throw new Error(
|
|
408
|
-
`Insufficient ${symbol} for gas fees. Wallet has ${balance} ${symbol} but needs at least ${minGas} ${symbol}.
|
|
437
|
+
`Insufficient ${symbol} for gas fees. Wallet has ${balance} ${symbol} but needs at least ${minGas} ${symbol}. Fund the wallet with ${symbol} before trading.`
|
|
409
438
|
);
|
|
410
439
|
}
|
|
411
440
|
|
|
@@ -1790,7 +1819,11 @@ export function assertLimitOrderCancelOutcome(sim, { inputMint, amount } = {}) {
|
|
|
1790
1819
|
* against SPL_TOKEN_PROGRAMS/COMPUTE_BUDGET_PROGRAM, so it's rejected outright
|
|
1791
1820
|
* rather than silently skipped.
|
|
1792
1821
|
*
|
|
1793
|
-
* Throws on any of those patterns.
|
|
1822
|
+
* Throws on any of those patterns. Otherwise returns the parsed transaction
|
|
1823
|
+
* plus `priorityFeeLamports` (0n when no compute-unit price is set), which
|
|
1824
|
+
* checkSolanaFeeBalance compares against the wallet's balance. With no limit
|
|
1825
|
+
* instruction it is priced at the runtime's default limit, not the 1.4M
|
|
1826
|
+
* ceiling the priority-fee cap assumes, so it is not a worst-case bound.
|
|
1794
1827
|
*/
|
|
1795
1828
|
export function assertSolanaInstructionsSafe(txBase64, { walletAddress } = {}) {
|
|
1796
1829
|
// Fail closed on a missing wallet address: every authority check below
|
|
@@ -1822,6 +1855,7 @@ export function assertSolanaInstructionsSafe(txBase64, { walletAddress } = {}) {
|
|
|
1822
1855
|
|
|
1823
1856
|
let computeUnitLimit = null;
|
|
1824
1857
|
let computeUnitPriceMicroLamports = null;
|
|
1858
|
+
let builtinInstructions = 0;
|
|
1825
1859
|
|
|
1826
1860
|
for (const ix of parsed.instructions) {
|
|
1827
1861
|
const programId = resolveStaticAccount(parsed, ix.programIdIndex);
|
|
@@ -1837,6 +1871,7 @@ export function assertSolanaInstructionsSafe(txBase64, { walletAddress } = {}) {
|
|
|
1837
1871
|
'Solana transaction invokes a program only resolvable via an address-lookup-table entry, which cannot be safety-classified. Refusing to sign.',
|
|
1838
1872
|
);
|
|
1839
1873
|
}
|
|
1874
|
+
if (SOLANA_BUILTIN_PROGRAMS.has(programId)) builtinInstructions++;
|
|
1840
1875
|
|
|
1841
1876
|
if (SPL_TOKEN_PROGRAMS.has(programId)) {
|
|
1842
1877
|
// No discriminator byte — not a valid SPL Token instruction (the runtime
|
|
@@ -1883,17 +1918,151 @@ export function assertSolanaInstructionsSafe(txBase64, { walletAddress } = {}) {
|
|
|
1883
1918
|
}
|
|
1884
1919
|
}
|
|
1885
1920
|
|
|
1921
|
+
let priorityFeeLamports = 0n;
|
|
1886
1922
|
if (computeUnitPriceMicroLamports != null) {
|
|
1887
|
-
|
|
1888
|
-
const
|
|
1889
|
-
|
|
1923
|
+
// The runtime rounds the priority fee up to a whole lamport.
|
|
1924
|
+
const feeAt = (units) =>
|
|
1925
|
+
(computeUnitPriceMicroLamports * BigInt(Math.min(units, SOLANA_MAX_COMPUTE_UNITS)) + 999_999n) / 1_000_000n;
|
|
1926
|
+
// Returned for checkSolanaFeeBalance's fallback. With no limit instruction
|
|
1927
|
+
// it prices the runtime's default limit, so it doesn't reject a transaction
|
|
1928
|
+
// the wallet can afford.
|
|
1929
|
+
const defaultLimit =
|
|
1930
|
+
SOLANA_DEFAULT_INSTRUCTION_COMPUTE_UNITS * (parsed.instructions.length - builtinInstructions) +
|
|
1931
|
+
SOLANA_DEFAULT_BUILTIN_COMPUTE_UNITS * builtinInstructions;
|
|
1932
|
+
priorityFeeLamports = feeAt(computeUnitLimit ?? defaultLimit);
|
|
1933
|
+
// The cap guards against a hostile transaction, so with no limit
|
|
1934
|
+
// instruction it assumes the 1.4M ceiling instead.
|
|
1935
|
+
const worstCaseFeeLamports = computeUnitLimit != null ? priorityFeeLamports : feeAt(SOLANA_MAX_COMPUTE_UNITS);
|
|
1936
|
+
if (worstCaseFeeLamports > MAX_PRIORITY_FEE_LAMPORTS) {
|
|
1890
1937
|
throw new Error(
|
|
1891
|
-
`Solana transaction sets an excessive priority fee (~${
|
|
1938
|
+
`Solana transaction sets an excessive priority fee (~${worstCaseFeeLamports} lamports, cap ${MAX_PRIORITY_FEE_LAMPORTS}). Refusing to sign.`,
|
|
1892
1939
|
);
|
|
1893
1940
|
}
|
|
1894
1941
|
}
|
|
1895
1942
|
|
|
1896
|
-
return parsed;
|
|
1943
|
+
return { ...parsed, priorityFeeLamports };
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
// Fallback only: the protocol's per-signature base fee, used when the cluster
|
|
1947
|
+
// can't price the message (getFeeForMessage failed or returned null because
|
|
1948
|
+
// the blockhash is unknown to that node). The live figure comes from the
|
|
1949
|
+
// cluster below.
|
|
1950
|
+
const SOLANA_LAMPORTS_PER_SIGNATURE = 5_000n;
|
|
1951
|
+
// Signature-verification precompiles. Each signature they verify is charged
|
|
1952
|
+
// like a transaction signature; the count is the instruction's first data byte.
|
|
1953
|
+
const SOLANA_SIGNATURE_PRECOMPILES = new Set([
|
|
1954
|
+
'Ed25519SigVerify111111111111111111111111111',
|
|
1955
|
+
'KeccakSecp256k11111111111111111111111111111',
|
|
1956
|
+
'Secp256r1SigVerify1111111111111111111111111',
|
|
1957
|
+
]);
|
|
1958
|
+
// A hung read must not hold the transaction until its blockhash expires.
|
|
1959
|
+
const SOLANA_FEE_CHECK_RPC_TIMEOUT_MS = 10_000;
|
|
1960
|
+
// Account state the check reads. Finalized state lags ~13s, so SOL spent in
|
|
1961
|
+
// that window would still show.
|
|
1962
|
+
const SOLANA_FEE_CHECK_COMMITMENT = 'confirmed';
|
|
1963
|
+
|
|
1964
|
+
function formatLamports(lamports) {
|
|
1965
|
+
const whole = lamports / 1_000_000_000n;
|
|
1966
|
+
const frac = (lamports % 1_000_000_000n).toString().padStart(9, '0').replace(/0+$/, '');
|
|
1967
|
+
return frac ? `${whole}.${frac}` : `${whole}`;
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
async function solanaRpc(rpcUrl, method, params) {
|
|
1971
|
+
const res = await fetch(rpcUrl, {
|
|
1972
|
+
method: 'POST',
|
|
1973
|
+
headers: { 'Content-Type': 'application/json' },
|
|
1974
|
+
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }),
|
|
1975
|
+
signal: AbortSignal.timeout(SOLANA_FEE_CHECK_RPC_TIMEOUT_MS),
|
|
1976
|
+
});
|
|
1977
|
+
if (!res.ok) throw new Error(`${method} failed: HTTP ${res.status}`);
|
|
1978
|
+
const body = await res.json();
|
|
1979
|
+
if (body.error || body.result === undefined) {
|
|
1980
|
+
throw new Error(`${method} failed: ${body.error?.message || 'no result'}`);
|
|
1981
|
+
}
|
|
1982
|
+
return body.result;
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1985
|
+
/**
|
|
1986
|
+
* Check, before signing, that the wallet holds enough SOL to pay THIS
|
|
1987
|
+
* transaction's network fee, when the wallet is the fee payer.
|
|
1988
|
+
*
|
|
1989
|
+
* The fee comes from the cluster (getFeeForMessage), so it follows the current
|
|
1990
|
+
* fee schedule and includes the priority fee and precompile signatures. When
|
|
1991
|
+
* the cluster can't price the message, it falls back to the protocol base fee
|
|
1992
|
+
* (5,000 lamports per transaction and precompile signature) plus the priority
|
|
1993
|
+
* fee assertSolanaInstructionsSafe parsed, rounded up.
|
|
1994
|
+
*
|
|
1995
|
+
* Only a missing Solana RPC or a failed balance read skips the check (warns
|
|
1996
|
+
* and returns { proceed: true }).
|
|
1997
|
+
*
|
|
1998
|
+
* Limits, left to simulation (verifySolanaSwapOutcome): SOL the transaction
|
|
1999
|
+
* takes from the wallet beyond the fee (account rent, the swap's SOL input,
|
|
2000
|
+
* transfers) is not counted.
|
|
2001
|
+
*
|
|
2002
|
+
* The quote-time gate (validateGasBalance) can only apply a fixed floor because
|
|
2003
|
+
* the transaction does not exist yet, and validateBalance holds back a fee
|
|
2004
|
+
* buffer only when selling 95% or more of the SOL balance. Simulation also
|
|
2005
|
+
* catches an unpayable transaction, but it is skipped by --no-verify-outcome
|
|
2006
|
+
* and degrades when no simulation RPC is reachable. This check runs on every
|
|
2007
|
+
* path regardless of that flag.
|
|
2008
|
+
*
|
|
2009
|
+
* REQUIRES `parsed` to be the return value of assertSolanaInstructionsSafe.
|
|
2010
|
+
*
|
|
2011
|
+
* @param {object} opts
|
|
2012
|
+
* @param {object} opts.parsed
|
|
2013
|
+
* @param {string} opts.walletAddress
|
|
2014
|
+
* @returns {Promise<{ proceed: boolean, reason?: string }>}
|
|
2015
|
+
*/
|
|
2016
|
+
export async function checkSolanaFeeBalance({ parsed, walletAddress, log = () => {} }) {
|
|
2017
|
+
// The fee payer is the first static account. When another account pays (a
|
|
2018
|
+
// sponsored transaction), the fee is not the wallet's to cover.
|
|
2019
|
+
if (parsed.staticAccountKeys[0] !== walletAddress) return { proceed: true };
|
|
2020
|
+
|
|
2021
|
+
const rpcUrl = CHAIN_RPCS.solana;
|
|
2022
|
+
if (!rpcUrl) {
|
|
2023
|
+
log(' ⚠ SOL fee balance check could not run (no Solana RPC endpoint); proceeding without it.');
|
|
2024
|
+
return { proceed: true };
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
let balance;
|
|
2028
|
+
let feeResult;
|
|
2029
|
+
try {
|
|
2030
|
+
[balance, feeResult] = await Promise.all([
|
|
2031
|
+
solanaRpc(rpcUrl, 'getBalance', [walletAddress, { commitment: SOLANA_FEE_CHECK_COMMITMENT }])
|
|
2032
|
+
.then(r => BigInt(r.value)),
|
|
2033
|
+
// A failed fee lookup falls back to the static estimate.
|
|
2034
|
+
solanaRpc(rpcUrl, 'getFeeForMessage', [parsed.messageBase64, { commitment: 'processed' }]).catch(() => null),
|
|
2035
|
+
]);
|
|
2036
|
+
} catch (e) {
|
|
2037
|
+
log(` ⚠ SOL fee balance check could not run (${e.message}); proceeding without it.`);
|
|
2038
|
+
return { proceed: true };
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
const clusterFee = feeResult?.value;
|
|
2042
|
+
const clusterPriced = Number.isSafeInteger(clusterFee) && clusterFee >= 0;
|
|
2043
|
+
let feeLamports;
|
|
2044
|
+
if (clusterPriced) {
|
|
2045
|
+
feeLamports = BigInt(clusterFee);
|
|
2046
|
+
} else {
|
|
2047
|
+
let precompileSignatures = 0n;
|
|
2048
|
+
for (const ix of parsed.instructions) {
|
|
2049
|
+
if (SOLANA_SIGNATURE_PRECOMPILES.has(resolveStaticAccount(parsed, ix.programIdIndex)) && ix.data.length > 0) {
|
|
2050
|
+
precompileSignatures += BigInt(ix.data[0]);
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
feeLamports = SOLANA_LAMPORTS_PER_SIGNATURE * (BigInt(parsed.header.numRequiredSignatures) + precompileSignatures) +
|
|
2054
|
+
parsed.priorityFeeLamports;
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
if (balance < feeLamports) {
|
|
2058
|
+
return {
|
|
2059
|
+
proceed: false,
|
|
2060
|
+
reason: `wallet has ${formatLamports(balance)} SOL but this transaction needs at least ${formatLamports(feeLamports)} SOL ` +
|
|
2061
|
+
`for its ${clusterPriced ? '' : 'estimated '}network fee. Fund ${walletAddress} with SOL and retry.`,
|
|
2062
|
+
};
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
return { proceed: true };
|
|
1897
2066
|
}
|
|
1898
2067
|
|
|
1899
2068
|
/**
|