apow-cli 0.11.2 → 0.11.4
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/dist/errors.js +8 -0
- package/dist/index.js +10 -3
- package/dist/miner.js +70 -45
- package/dist/x402.js +2 -0
- package/package.json +1 -1
package/dist/errors.js
CHANGED
|
@@ -93,6 +93,14 @@ const patterns = [
|
|
|
93
93
|
recovery: "Retrying with a fresh payment session...",
|
|
94
94
|
}),
|
|
95
95
|
},
|
|
96
|
+
{
|
|
97
|
+
test: (m) => m.includes("x402 payment failed — insufficient USDC balance on Base"),
|
|
98
|
+
classify: () => ({
|
|
99
|
+
category: "setup",
|
|
100
|
+
userMessage: "Insufficient USDC for QuickNode x402 RPC",
|
|
101
|
+
recovery: "Send USDC to your wallet on Base, or set RPC_URL in .env to use your own RPC",
|
|
102
|
+
}),
|
|
103
|
+
},
|
|
96
104
|
{
|
|
97
105
|
test: (m) => /\b402\b/.test(m) || m.includes("QuickNode x402") || (m.toLowerCase().includes("insufficient") && m.toLowerCase().includes("usdc")),
|
|
98
106
|
classify: () => ({
|
package/dist/index.js
CHANGED
|
@@ -470,10 +470,17 @@ async function main() {
|
|
|
470
470
|
.option("--token <token>", "Source token: sol, usdc, eth")
|
|
471
471
|
.option("--amount <eth>", "Target ETH amount (default: 0.005)")
|
|
472
472
|
.option("--no-swap", "Skip auto-split after bridging")
|
|
473
|
-
.hook("preAction", async () => {
|
|
474
|
-
await (0, preflight_1.runPreflight)("readonly");
|
|
475
|
-
})
|
|
476
473
|
.action(async (opts) => {
|
|
474
|
+
if (!config_1.config.privateKey || !wallet_1.account) {
|
|
475
|
+
ui.warn("No wallet configured — launching setup first.");
|
|
476
|
+
await setupWizard();
|
|
477
|
+
(0, config_1.reloadConfig)();
|
|
478
|
+
(0, wallet_1.reinitClients)();
|
|
479
|
+
if (!wallet_1.account) {
|
|
480
|
+
ui.error("Wallet configuration did not complete.");
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
477
484
|
await (0, fund_1.runFundFlow)(opts);
|
|
478
485
|
});
|
|
479
486
|
program
|
package/dist/miner.js
CHANGED
|
@@ -56,8 +56,8 @@ const wallet_1 = require("./wallet");
|
|
|
56
56
|
const agentCoinAbi = AgentCoin_json_1.default;
|
|
57
57
|
const miningAgentAbi = MiningAgent_json_1.default;
|
|
58
58
|
const MAX_CONSECUTIVE_FAILURES = 10;
|
|
59
|
-
const
|
|
60
|
-
const
|
|
59
|
+
const RETRY_DELAY_MS = 2_000;
|
|
60
|
+
const RETRY_JITTER_MS = 500;
|
|
61
61
|
const BASE_REWARD = 3n * 10n ** 18n;
|
|
62
62
|
const REWARD_DECAY_NUM = 90n;
|
|
63
63
|
const REWARD_DECAY_DEN = 100n;
|
|
@@ -68,6 +68,20 @@ function elapsedSeconds(start) {
|
|
|
68
68
|
function sleep(ms) {
|
|
69
69
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
70
70
|
}
|
|
71
|
+
function withTimeout(promise, ms, label) {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
const timer = setTimeout(() => {
|
|
74
|
+
reject(new Error(`${label} timed out after ${ms / 1000}s`));
|
|
75
|
+
}, ms);
|
|
76
|
+
promise.then((value) => {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
resolve(value);
|
|
79
|
+
}, (error) => {
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
reject(error);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
71
85
|
function rejectOnAbort(signal) {
|
|
72
86
|
if (signal.aborted) {
|
|
73
87
|
const err = new Error("Aborted");
|
|
@@ -82,10 +96,8 @@ function rejectOnAbort(signal) {
|
|
|
82
96
|
}, { once: true });
|
|
83
97
|
});
|
|
84
98
|
}
|
|
85
|
-
function
|
|
86
|
-
|
|
87
|
-
const jitter = Math.random() * base * 0.3;
|
|
88
|
-
return base + jitter;
|
|
99
|
+
function retryDelayMs() {
|
|
100
|
+
return RETRY_DELAY_MS + Math.random() * RETRY_JITTER_MS;
|
|
89
101
|
}
|
|
90
102
|
function estimateReward(totalMines, eraInterval, hashpower) {
|
|
91
103
|
const era = totalMines / eraInterval;
|
|
@@ -163,6 +175,13 @@ function formatTime(seconds) {
|
|
|
163
175
|
return `${(seconds / 60).toFixed(1)} min`;
|
|
164
176
|
return `${(seconds / 3600).toFixed(1)} hr`;
|
|
165
177
|
}
|
|
178
|
+
async function readMiningChallenge(timeoutMs = 8_000) {
|
|
179
|
+
return withTimeout(wallet_1.publicClient.readContract({
|
|
180
|
+
address: config_1.config.agentCoinAddress,
|
|
181
|
+
abi: agentCoinAbi,
|
|
182
|
+
functionName: "getMiningChallenge",
|
|
183
|
+
}), timeoutMs, "Mining challenge refresh");
|
|
184
|
+
}
|
|
166
185
|
function readCliVersion() {
|
|
167
186
|
try {
|
|
168
187
|
const pkg = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(__dirname, "..", "package.json"), "utf8"));
|
|
@@ -317,11 +336,7 @@ async function startMining(tokenId) {
|
|
|
317
336
|
await showStartupBanner(tokenId);
|
|
318
337
|
console.log(` Grinder: ${ui.bold(modeLabel)}`);
|
|
319
338
|
// Difficulty-aware preflight gate
|
|
320
|
-
const preflight =
|
|
321
|
-
address: config_1.config.agentCoinAddress,
|
|
322
|
-
abi: agentCoinAbi,
|
|
323
|
-
functionName: "getMiningChallenge",
|
|
324
|
-
}));
|
|
339
|
+
const preflight = await readMiningChallenge();
|
|
325
340
|
const preflightTarget = preflight[1];
|
|
326
341
|
const expectedHashes = preflightTarget > 0n ? (2n ** 256n) / preflightTarget : 0n;
|
|
327
342
|
const estHashrate = estimateHashrate(grinderInfo, useNative, useHttpGrind);
|
|
@@ -420,11 +435,7 @@ async function startMining(tokenId) {
|
|
|
420
435
|
}
|
|
421
436
|
mineCount++;
|
|
422
437
|
console.log(` ${ui.bold(`[Mine #${mineCount}]`)}`);
|
|
423
|
-
const miningChallenge =
|
|
424
|
-
address: config_1.config.agentCoinAddress,
|
|
425
|
-
abi: agentCoinAbi,
|
|
426
|
-
functionName: "getMiningChallenge",
|
|
427
|
-
}));
|
|
438
|
+
const miningChallenge = await readMiningChallenge();
|
|
428
439
|
let [challengeNumber, target] = [miningChallenge[0], miningChallenge[1]];
|
|
429
440
|
const smhl = (0, smhl_1.normalizeSmhlChallenge)(miningChallenge[2]);
|
|
430
441
|
// Solve SMHL algorithmically (sub-millisecond)
|
|
@@ -449,23 +460,45 @@ async function startMining(tokenId) {
|
|
|
449
460
|
const abortController = new AbortController();
|
|
450
461
|
const nonceSpinner = ui.spinner(`Grinding nonce (${modeLabel})...`);
|
|
451
462
|
// Background staleness checker
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
staleRestarts++;
|
|
461
|
-
nonceSpinner.stop(`Challenge changed — restarting grind (stale #${staleRestarts})`);
|
|
462
|
-
abortController.abort();
|
|
463
|
-
}
|
|
463
|
+
let staleTimer = null;
|
|
464
|
+
let staleCheckStopped = false;
|
|
465
|
+
let staleCheckInFlight = false;
|
|
466
|
+
const stopStaleChecks = () => {
|
|
467
|
+
staleCheckStopped = true;
|
|
468
|
+
if (staleTimer) {
|
|
469
|
+
clearTimeout(staleTimer);
|
|
470
|
+
staleTimer = null;
|
|
464
471
|
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
472
|
+
};
|
|
473
|
+
const scheduleStaleCheck = () => {
|
|
474
|
+
if (staleCheckStopped || abortController.signal.aborted)
|
|
475
|
+
return;
|
|
476
|
+
staleTimer = setTimeout(async () => {
|
|
477
|
+
if (staleCheckStopped || abortController.signal.aborted || staleCheckInFlight) {
|
|
478
|
+
scheduleStaleCheck();
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
staleCheckInFlight = true;
|
|
482
|
+
try {
|
|
483
|
+
const fresh = await readMiningChallenge(Math.min(staleCheckMs, 5_000));
|
|
484
|
+
if (fresh[0] !== challengeNumber && !abortController.signal.aborted) {
|
|
485
|
+
staleRestarts++;
|
|
486
|
+
nonceSpinner.stop(`Challenge changed — restarting grind (stale #${staleRestarts})`);
|
|
487
|
+
abortController.abort();
|
|
488
|
+
stopStaleChecks();
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
catch {
|
|
493
|
+
// RPC hiccup — don't abort, just skip this check
|
|
494
|
+
}
|
|
495
|
+
finally {
|
|
496
|
+
staleCheckInFlight = false;
|
|
497
|
+
}
|
|
498
|
+
scheduleStaleCheck();
|
|
499
|
+
}, staleCheckMs);
|
|
500
|
+
};
|
|
501
|
+
scheduleStaleCheck();
|
|
469
502
|
try {
|
|
470
503
|
// Race all available grinders — first valid nonce wins
|
|
471
504
|
const grinders = [];
|
|
@@ -546,11 +579,7 @@ async function startMining(tokenId) {
|
|
|
546
579
|
nonceSpinner.stop(`Nonce found (${grind.elapsed.toFixed(1)}s${grind.hashrate > 0 ? `, ${khs}k H/s` : ""})`);
|
|
547
580
|
// Final stale check: a challenge can change between the periodic
|
|
548
581
|
// poll and transaction submission, which would make the SMHL stale.
|
|
549
|
-
const latestChallenge =
|
|
550
|
-
address: config_1.config.agentCoinAddress,
|
|
551
|
-
abi: agentCoinAbi,
|
|
552
|
-
functionName: "getMiningChallenge",
|
|
553
|
-
}));
|
|
582
|
+
const latestChallenge = await readMiningChallenge();
|
|
554
583
|
if (latestChallenge[0] !== challengeNumber) {
|
|
555
584
|
staleRestarts++;
|
|
556
585
|
[challengeNumber, target] = [latestChallenge[0], latestChallenge[1]];
|
|
@@ -562,14 +591,10 @@ async function startMining(tokenId) {
|
|
|
562
591
|
}
|
|
563
592
|
}
|
|
564
593
|
catch (err) {
|
|
565
|
-
|
|
594
|
+
stopStaleChecks();
|
|
566
595
|
if (abortController.signal.aborted) {
|
|
567
596
|
// Challenge went stale — re-fetch and retry
|
|
568
|
-
const freshChallenge =
|
|
569
|
-
address: config_1.config.agentCoinAddress,
|
|
570
|
-
abi: agentCoinAbi,
|
|
571
|
-
functionName: "getMiningChallenge",
|
|
572
|
-
}));
|
|
597
|
+
const freshChallenge = await readMiningChallenge();
|
|
573
598
|
[challengeNumber, target] = [freshChallenge[0], freshChallenge[1]];
|
|
574
599
|
const freshSmhl = (0, smhl_1.normalizeSmhlChallenge)(freshChallenge[2]);
|
|
575
600
|
smhlSolution = (0, smhl_1.solveSmhlAlgorithmic)(freshSmhl);
|
|
@@ -585,7 +610,7 @@ async function startMining(tokenId) {
|
|
|
585
610
|
throw err; // non-abort error — propagate
|
|
586
611
|
}
|
|
587
612
|
finally {
|
|
588
|
-
|
|
613
|
+
stopStaleChecks();
|
|
589
614
|
}
|
|
590
615
|
}
|
|
591
616
|
// Submit transaction with spinner
|
|
@@ -655,7 +680,7 @@ async function startMining(tokenId) {
|
|
|
655
680
|
ui.error(`${MAX_CONSECUTIVE_FAILURES} consecutive failures. Last: ${classified.userMessage}`);
|
|
656
681
|
return;
|
|
657
682
|
}
|
|
658
|
-
const delay =
|
|
683
|
+
const delay = retryDelayMs();
|
|
659
684
|
ui.error(`${classified.userMessage} (${consecutiveFailures}/${MAX_CONSECUTIVE_FAILURES})`);
|
|
660
685
|
if (classified.recovery)
|
|
661
686
|
ui.hint(classified.recovery);
|
package/dist/x402.js
CHANGED
|
@@ -38,6 +38,7 @@ exports.resetX402 = resetX402;
|
|
|
38
38
|
const viem_1 = require("viem");
|
|
39
39
|
const QUICKNODE_BASE = "https://x402.quicknode.com";
|
|
40
40
|
const BASE_MAINNET = "eip155:8453";
|
|
41
|
+
const X402_RPC_TIMEOUT_MS = 10_000;
|
|
41
42
|
let _client = null;
|
|
42
43
|
async function getClient(privateKey) {
|
|
43
44
|
if (_client && !_client.isTokenExpired())
|
|
@@ -60,6 +61,7 @@ function createX402Transport(privateKey) {
|
|
|
60
61
|
method: "POST",
|
|
61
62
|
headers: { "Content-Type": "application/json" },
|
|
62
63
|
body: JSON.stringify({ jsonrpc: "2.0", method, params, id: 1 }),
|
|
64
|
+
signal: AbortSignal.timeout(X402_RPC_TIMEOUT_MS),
|
|
63
65
|
});
|
|
64
66
|
if (response.status === 402) {
|
|
65
67
|
throw new Error("x402 payment failed — insufficient USDC balance on Base");
|