Skip to content

Commit 5d77439

Browse files
authored
Merge pull request #25 from tokenhost/fix/faucet-noop-feedback
Faucet DX: fix no-op behavior and add explicit feedback
2 parents 1ba987d + a3dde5d commit 5d77439

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

packages/cli/src/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,28 @@ function startUiSiteServer(args: {
673673
return `0x${n.toString(16)}`;
674674
}
675675

676+
async function trySetLocalBalance(rpcUrl: string, addr: string, wei: bigint): Promise<{ ok: boolean; method?: string; error?: string }> {
677+
const qty = toHexQuantity(wei);
678+
const methods = ['anvil_setBalance', 'hardhat_setBalance'];
679+
680+
for (const method of methods) {
681+
try {
682+
await rpcRequest(rpcUrl, method, [addr, qty], 2000);
683+
return { ok: true, method };
684+
} catch (e: any) {
685+
const msg = String(e?.message ?? e ?? '');
686+
const unsupported =
687+
/method not found/i.test(msg) ||
688+
/unsupported/i.test(msg) ||
689+
/does not exist/i.test(msg) ||
690+
/-32601/.test(msg);
691+
if (!unsupported) return { ok: false, method, error: msg };
692+
}
693+
}
694+
695+
return { ok: false, error: 'No supported local balance RPC method found (anvil_setBalance, hardhat_setBalance).' };
696+
}
697+
676698
function readBody(req: nodeHttp.IncomingMessage, maxBytes = 1024 * 1024): Promise<string> {
677699
return new Promise((resolve, reject) => {
678700
let raw = '';
@@ -750,9 +772,14 @@ function startUiSiteServer(args: {
750772
const targetWei = faucet!.targetWei;
751773

752774
let didSet = false;
775+
let setMethod: string | null = null;
753776
if (oldWei < targetWei) {
754-
await rpcRequest(faucet!.rpcUrl, 'anvil_setBalance', [addr, toHexQuantity(targetWei)], 2000);
777+
const setResult = await trySetLocalBalance(faucet!.rpcUrl, addr, targetWei);
778+
if (!setResult.ok) {
779+
return sendJson(res, 400, { ok: false, error: setResult.error ?? 'Failed to set balance.' });
780+
}
755781
didSet = true;
782+
setMethod = setResult.method ?? null;
756783
}
757784

758785
const newHex = (await rpcRequest(faucet!.rpcUrl, 'eth_getBalance', [addr, 'latest'], 2000)) as string;
@@ -765,6 +792,7 @@ function startUiSiteServer(args: {
765792
targetWei: toHexQuantity(targetWei),
766793
oldBalanceWei: toHexQuantity(oldWei),
767794
newBalanceWei: toHexQuantity(newWei),
795+
method: setMethod,
768796
didSet
769797
});
770798
} catch (e: any) {

packages/templates/next-export-ui/src/components/FaucetButton.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ type FaucetStatus = {
1414
reason?: string | null;
1515
};
1616

17+
function formatWeiHexAsEth(weiHex: unknown): string | null {
18+
if (typeof weiHex !== 'string' || !weiHex.startsWith('0x')) return null;
19+
try {
20+
const wei = BigInt(weiHex);
21+
const whole = wei / 10n ** 18n;
22+
const frac4 = (wei % 10n ** 18n) / 10n ** 14n;
23+
if (frac4 === 0n) return `${whole.toString()} ETH`;
24+
return `${whole.toString()}.${frac4.toString().padStart(4, '0')} ETH`;
25+
} catch {
26+
return null;
27+
}
28+
}
29+
1730
async function tryFetchFaucetStatus(): Promise<FaucetStatus | null> {
1831
try {
1932
const res = await fetch('/__tokenhost/faucet', { cache: 'no-store' });
@@ -102,8 +115,16 @@ export default function FaucetButton() {
102115
const msg = String(json?.error ?? `Faucet failed (HTTP ${res.status}).`);
103116
throw new Error(msg);
104117
}
105-
106-
setTimedNote(`Funded to ~${targetEthRef.current} ETH`);
118+
const oldEth = formatWeiHexAsEth(json?.oldBalanceWei);
119+
const newEth = formatWeiHexAsEth(json?.newBalanceWei);
120+
if (json?.didSet) {
121+
const detail = oldEth && newEth ? ` (${oldEth} -> ${newEth})` : '';
122+
setTimedNote(`Faucet funded${detail}`);
123+
} else if (newEth) {
124+
setTimedNote(`Already funded (${newEth})`);
125+
} else {
126+
setTimedNote(`Already funded (~${targetEthRef.current} ETH)`);
127+
}
107128
} catch (e: any) {
108129
setTimedNote(String(e?.message ?? e));
109130
} finally {

0 commit comments

Comments
 (0)