Skip to content

Commit

Permalink
chore: improve funding for demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jul 6, 2024
1 parent d6cdb84 commit 7e9f4fa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 18 deletions.
41 changes: 30 additions & 11 deletions static/src/funding/balance-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,42 @@ export async function walletNeedsFunded(signer: Wallet) {
export async function fundWalletFromFaucet(signer: Wallet) {
const workerUrl = "https://ubq-gas-faucet.keyrxng7749.workers.dev/?address=" + signer.address;
let res: Response | null = null;
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
try {
res = await fetch(workerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
res = await fetch(workerUrl, options);
} catch (e) {
await retryWrapper(async () => {
res = await fetch(workerUrl, options);
});

if (res.status !== 200) {
throw new Error("Failed to fund wallet from faucet");
if (!res || !res.ok) {
throw new Error("Faucet work has likely exceeded limits, try again shortly.")
}
}

} catch (e) {
console.error(e);
if (!res || !res.ok) {
return null;
}

if (res) {
return res.json();
return res.json();
}

async function retryWrapper(fn: () => Promise<any>, retries = 3) {
let res;
let backoff = 7500;
for (let i = 0; i < retries; i++) {
try {
res = await fn();
break;
} catch (e) {
console.error(e);
}
await new Promise((resolve) => setTimeout(resolve, backoff * (i + 1)));
}
return res;
}
8 changes: 6 additions & 2 deletions static/src/webauthn/rendering.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { BigNumberish, ethers, formatUnits, Wallet, ZeroAddress } from "ethers";
import { provider } from "../funding/balance-check";


export async function renderSafeUI(signer: Wallet) {
export async function renderSafeUI(signer?: Wallet) {
const container = document.createElement("div")
if (!signer) {
container.innerHTML = "No signer available"
return container
}

const { address, privateKey: signerPk } = signer
const signerInfo = `
<div style="display: flex; flex-direction: column; margin-left: 6px">
Expand Down
41 changes: 36 additions & 5 deletions static/src/webauthn/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,39 @@ export async function webAuthn(ghUser?: GitHubUser | null) {
const signer = await handleUser(user, userAuth, provider);

if (await walletNeedsFunded(signer)) {
const killToast = toastNotification("Funding wallet from faucet...");
toastNotification("Funding wallet from faucet...", 5000);
const res = await fundWalletFromFaucet(signer);
console.log("res", res); // @todo: handle this better
killToast();
if (!res || !res?.txHash) {
toastNotification("Failed to fund wallet from faucet", 5000);
return;
}
console.log("Faucet response", res);

const txHash = res.txHash;

console.log("Waiting for transaction to be mined", txHash)

const waitingToBeMinedKill = toastNotification("Waiting for transaction to be mined...")
await provider.waitForTransaction(txHash);

const receipt = await provider.getTransaction(txHash);
console.log("Transaction mined", receipt);

waitingToBeMinedKill();

if (!receipt) {
toastNotification("Failed to fund wallet from faucet", 5000);
} else {
toastNotification("Wallet successfully funded", 5000)
}
} else {
toastNotification("Wallet already funded");
}

return signer;
}

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

Expand All @@ -62,9 +83,19 @@ function toastNotification(message: string) {

document.body.appendChild(toast);

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

if (!timeout) {
return killToast;
}

setTimeout(() => {
killToast();
}, timeout);

return killToast;
}

function abortControlHandler() {
Expand Down

0 comments on commit 7e9f4fa

Please sign in to comment.