From c2b8a0bdfc108a9a578818b259517f7b62aed75c Mon Sep 17 00:00:00 2001 From: masih Date: Mon, 23 Nov 2026 18:00:16 +0000 Subject: [PATCH 1/2] Show network details and switch wallets to the faucet chain The faucet page only showed the chain ID and offered a bare wallet_addEthereumChain call that failed silently. It now shows the RPC and currency, asks the wallet to switch first and adds the chain only when the wallet does not know it (4902), reports the outcome, and hides the button when there is no injected wallet. --- internal/api/web/app.js | 32 ++++++++++++++++++++++++-------- internal/api/web/index.html | 2 ++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/internal/api/web/app.js b/internal/api/web/app.js index 5004fdb..e6fed38 100644 --- a/internal/api/web/app.js +++ b/internal/api/web/app.js @@ -2,6 +2,7 @@ const $ = (id) => document.getElementById(id); const ADDRESS = /^0x[0-9a-fA-F]{40}$/; const STEPS = ["queued", "sent", "confirmed"]; +const CURRENCY = { name: "SEI", symbol: "SEI", decimals: 18 }; let info, widget, token = "", polling = 0; const sei = (wei) => (Number(BigInt(wei) / 10n ** 14n) / 1e4).toLocaleString("en-US", { maximumFractionDigits: 4 }); @@ -67,10 +68,13 @@ async function load() { $("cooldown").textContent = `1 per ${info.addressCooldownSeconds / 3600}h`; $("balance").textContent = info.balanceWei ? `${Math.floor(Number(BigInt(info.balanceWei) / 10n ** 18n)).toLocaleString("en-US")} SEI` : "–"; $("chain").textContent = info.chainId; + $("rpc").textContent = info.publicRpcUrl; + $("rpc-row").hidden = !info.publicRpcUrl; + $("currency").textContent = `${CURRENCY.symbol}, ${CURRENCY.decimals} decimals`; $("faucet").textContent = info.faucetAddress; $("details").hidden = false; $("add").textContent = `Add ${info.chainName} to wallet`; - $("add").hidden = !info.publicRpcUrl; + $("add").hidden = !info.publicRpcUrl || !window.ethereum; $("connect").hidden = !window.ethereum; const src = { turnstile: "https://challenges.cloudflare.com/turnstile/v0/api.js", @@ -163,14 +167,26 @@ $("connect").onclick = async () => { } }; +// EIP-3326 switch first; 4902 means the wallet lacks the chain, so add it (EIP-3085). +// MetaMask Mobile nests the code under data.originalError. $("add").onclick = async () => { - if (!window.ethereum) { show("No browser wallet found", "error"); return; } - await window.ethereum.request({ method: "wallet_addEthereumChain", params: [{ - chainId: "0x" + Number(info.chainId).toString(16), - chainName: info.chainName, - nativeCurrency: { name: "SEI", symbol: "SEI", decimals: 18 }, - rpcUrls: [info.publicRpcUrl], - }] }); + const chainId = "0x" + Number(info.chainId).toString(16); + try { + try { + await window.ethereum.request({ method: "wallet_switchEthereumChain", params: [{ chainId }] }); + } catch (e) { + if (e?.code !== 4902 && e?.data?.originalError?.code !== 4902) throw e; + await window.ethereum.request({ method: "wallet_addEthereumChain", params: [{ + chainId, + chainName: info.chainName, + nativeCurrency: CURRENCY, + rpcUrls: [info.publicRpcUrl], + }] }); + } + show(`${info.chainName} is ready in your wallet`, "ok"); + } catch (e) { + show(e?.code === 4001 ? "Wallet request was rejected" : `Wallet could not switch to ${info.chainName}`, "error"); + } }; load(); diff --git a/internal/api/web/index.html b/internal/api/web/index.html index 8a3bd74..8aebe78 100644 --- a/internal/api/web/index.html +++ b/internal/api/web/index.html @@ -49,6 +49,8 @@

Fund your wallet

From 8c9053daac138819e4dea763f4c0b35e0eda14fd Mon Sep 17 00:00:00 2001 From: masih Date: Mon, 23 Nov 2026 18:02:28 +0000 Subject: [PATCH 2/2] Read wallet rejections nested by MetaMask Mobile --- internal/api/web/app.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/api/web/app.js b/internal/api/web/app.js index e6fed38..b8146d7 100644 --- a/internal/api/web/app.js +++ b/internal/api/web/app.js @@ -167,15 +167,17 @@ $("connect").onclick = async () => { } }; +// MetaMask Mobile nests the provider error code under data.originalError. +const walletError = (e) => e?.data?.originalError?.code ?? e?.code; + // EIP-3326 switch first; 4902 means the wallet lacks the chain, so add it (EIP-3085). -// MetaMask Mobile nests the code under data.originalError. $("add").onclick = async () => { const chainId = "0x" + Number(info.chainId).toString(16); try { try { await window.ethereum.request({ method: "wallet_switchEthereumChain", params: [{ chainId }] }); } catch (e) { - if (e?.code !== 4902 && e?.data?.originalError?.code !== 4902) throw e; + if (walletError(e) !== 4902) throw e; await window.ethereum.request({ method: "wallet_addEthereumChain", params: [{ chainId, chainName: info.chainName, @@ -185,7 +187,7 @@ $("add").onclick = async () => { } show(`${info.chainName} is ready in your wallet`, "ok"); } catch (e) { - show(e?.code === 4001 ? "Wallet request was rejected" : `Wallet could not switch to ${info.chainName}`, "error"); + show(walletError(e) === 4001 ? "Wallet request was rejected" : `Wallet could not switch to ${info.chainName}`, "error"); } };