Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions internal/api/web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -163,14 +167,28 @@ $("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).
$("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 (walletError(e) !== 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(walletError(e) === 4001 ? "Wallet request was rejected" : `Wallet could not switch to ${info.chainName}`, "error");
}
};

load();
2 changes: 2 additions & 0 deletions internal/api/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ <h2 id="title">Fund your wallet</h2>
<div id="status" role="status" aria-live="polite"></div>
<dl class="details" id="details" hidden>
<div><dt>Chain ID</dt><dd id="chain"></dd></div>
<div id="rpc-row"><dt>RPC</dt><dd class="mono" id="rpc"></dd></div>
<div><dt>Currency</dt><dd id="currency"></dd></div>
<div><dt>Faucet</dt><dd class="mono" id="faucet"></dd></div>
</dl>
<button class="secondary" id="add" type="button" hidden>Add network to wallet</button>
Expand Down