From 94b8ca9b5897dfbc3ec12ccc1e45bf82fc7471a1 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Thu, 28 Mar 2024 11:40:58 +0000 Subject: [PATCH] fix: use window.ethereum --- globals.d.ts | 4 +++- static/scripts/rewards/ButtonController.ts | 6 +++--- .../rewards/render-transaction/read-claim-data-from-url.ts | 4 ++-- .../rewards/render-transaction/render-transaction.ts | 2 +- static/scripts/rewards/web3/connect-wallet.ts | 2 +- static/scripts/rewards/web3/verify-current-network.ts | 6 +++--- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/globals.d.ts b/globals.d.ts index e19d1266..1a9ff4d3 100644 --- a/globals.d.ts +++ b/globals.d.ts @@ -1,5 +1,7 @@ import { Ethereum } from "ethereum-protocol"; declare global { - const ethereum: Ethereum; + interface Window { + ethereum: Ethereum; + } } diff --git a/static/scripts/rewards/ButtonController.ts b/static/scripts/rewards/ButtonController.ts index 8fea529d..3e487b6f 100644 --- a/static/scripts/rewards/ButtonController.ts +++ b/static/scripts/rewards/ButtonController.ts @@ -11,7 +11,7 @@ export class ButtonController { } public showLoader(): void { - if (ethereum) { + if (window.ethereum) { this._controls.setAttribute(LOADER, "true"); } else { throw new Error("Can not show loader without `ethereum`"); @@ -27,7 +27,7 @@ export class ButtonController { } public showMakeClaim(): void { - if (ethereum) { + if (window.ethereum) { this._controls.setAttribute(MAKE_CLAIM, "true"); } else { throw new Error("Can not show make claim button without `ethereum`"); @@ -47,7 +47,7 @@ export class ButtonController { } public showInvalidator(): void { - if (ethereum) { + if (window.ethereum) { this._controls.setAttribute(INVALIDATOR, "true"); } else { throw new Error("Can not show invalidator button without `ethereum`"); diff --git a/static/scripts/rewards/render-transaction/read-claim-data-from-url.ts b/static/scripts/rewards/render-transaction/read-claim-data-from-url.ts index fe0a5aef..95927e08 100644 --- a/static/scripts/rewards/render-transaction/read-claim-data-from-url.ts +++ b/static/scripts/rewards/render-transaction/read-claim-data-from-url.ts @@ -36,13 +36,13 @@ export async function readClaimDataFromUrl(app: AppState) { } catch (e) { toaster.create("error", `${e}`); } - if (ethereum) { + if (window.ethereum) { try { app.signer = await connectWallet(); } catch (error) { /* empty */ } - ethereum.on("accountsChanged", () => { + window.ethereum.on("accountsChanged", () => { checkRenderMakeClaimControl(app).catch(console.error); checkRenderInvalidatePermitAdminControl(app).catch(console.error); }); diff --git a/static/scripts/rewards/render-transaction/render-transaction.ts b/static/scripts/rewards/render-transaction/render-transaction.ts index 2bbc0346..b3a69a94 100644 --- a/static/scripts/rewards/render-transaction/render-transaction.ts +++ b/static/scripts/rewards/render-transaction/render-transaction.ts @@ -53,7 +53,7 @@ export async function renderTransaction(): Promise { if (app.claimTxs[app.reward.permit.nonce.toString()] !== undefined) { buttonController.showViewClaim(); viewClaimButton.addEventListener("click", () => window.open(`${app.currentExplorerUrl}/tx/${app.claimTxs[app.reward.permit.nonce.toString()]}`)); - } else if (ethereum) { + } else if (window.ethereum) { // requires wallet connection to claim buttonController.showMakeClaim(); getMakeClaimButton().addEventListener("click", claimErc20PermitHandlerWrapper(app)); diff --git a/static/scripts/rewards/web3/connect-wallet.ts b/static/scripts/rewards/web3/connect-wallet.ts index fcfae3df..81a29cb7 100644 --- a/static/scripts/rewards/web3/connect-wallet.ts +++ b/static/scripts/rewards/web3/connect-wallet.ts @@ -4,7 +4,7 @@ import { buttonController, toaster } from "../toaster"; export async function connectWallet(): Promise { try { - const wallet = new ethers.providers.Web3Provider(ethereum); + const wallet = new ethers.providers.Web3Provider(window.ethereum); await wallet.send("eth_requestAccounts", []); diff --git a/static/scripts/rewards/web3/verify-current-network.ts b/static/scripts/rewards/web3/verify-current-network.ts index 1f9b7a4e..8d1e530d 100644 --- a/static/scripts/rewards/web3/verify-current-network.ts +++ b/static/scripts/rewards/web3/verify-current-network.ts @@ -5,18 +5,18 @@ import { notOnCorrectNetwork } from "./not-on-correct-network"; // verifyCurrentNetwork checks if the user is on the correct network and displays an error if not export async function verifyCurrentNetwork(desiredNetworkId: number) { - if (!ethereum) { + if (!window.ethereum) { buttonController.hideAll(); return; } - const web3provider = new ethers.providers.Web3Provider(ethereum); + const web3provider = new ethers.providers.Web3Provider(window.ethereum); const network = await web3provider.getNetwork(); const currentNetworkId = network.chainId; // watch for network changes - ethereum.on("chainChanged", (newNetworkId: T | string) => handleIfOnCorrectNetwork(parseInt(newNetworkId as string, 16), desiredNetworkId)); + window.ethereum.on("chainChanged", (newNetworkId: T | string) => handleIfOnCorrectNetwork(parseInt(newNetworkId as string, 16), desiredNetworkId)); // if its not on ethereum mainnet, gnosis, or goerli, display error notOnCorrectNetwork(currentNetworkId, desiredNetworkId, web3provider);