Skip to content

Commit

Permalink
chore: basic toast for while funding
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jul 3, 2024
1 parent b513525 commit 9e802cb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
23 changes: 13 additions & 10 deletions static/src/funding/balance-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ export async function walletNeedsFunded(signer: Wallet) {
}

export async function fundWalletFromFaucet(signer: Wallet) {
const workerUrl = "https://ubq-gas-faucet.keyrxng7749.workers.dev"

const workerUrl = "https://ubq-gas-faucet.keyrxng7749.workers.dev/?address=" + signer.address;
let res: Response | null = null;
try {
const response = await fetch(workerUrl + "/?address=" + signer.address, {
res = await fetch(workerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});

if (response.status === 200) {
await response.json();
} else {
throw new Error("Failed to fund wallet");
if (res.status !== 200) {
throw new Error("Failed to fund wallet from faucet");
}


} catch (e) {
console.error(e)
throw new Error("Failed to fund wallet");
console.error(e);
}

if (res) {
return res.json();
}
}
3 changes: 2 additions & 1 deletion static/src/webauthn/rendering.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { formatUnits, Wallet } from "ethers";
import { BigNumberish, ethers, formatUnits, Wallet, ZeroAddress } from "ethers";
import { provider } from "../funding/balance-check";


export async function renderSafeUI(signer: Wallet) {
Expand Down
32 changes: 29 additions & 3 deletions static/src/webauthn/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,41 @@ export async function webAuthn(ghUser?: GitHubUser | null) {
const signer = await handleUser(user, userAuth, provider);

if (await walletNeedsFunded(signer)) {
console.log(`Funding wallet ${signer.address}`)
await fundWalletFromFaucet(signer);
const killToast = toastNotification("Funding wallet from faucet...");
const res = await fundWalletFromFaucet(signer);
console.log("res", res); // @todo: handle this better
killToast();
} else {
console.log("Wallet does not need funding")
toastNotification("Wallet already funded");
}

return signer;
}

function toastNotification(message: string) {
const toast = document.createElement("div");
toast.textContent = message;

const style = `
position: fixed;
bottom: 0;
right: 0;
background-color: #333;
color: white;
padding: 1rem;
border-radius: 0.5rem;
margin: 1rem;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
`
toast.style.cssText = style;

document.body.appendChild(toast);

return () => {
document.body.removeChild(toast);
}
}

function abortControlHandler() {
// TODO: implement this
return new AbortController();
Expand Down

0 comments on commit 9e802cb

Please sign in to comment.