Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hot-fix: use window.ethereum #213

Merged
merged 1 commit into from
Mar 29, 2024
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
4 changes: 3 additions & 1 deletion globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Ethereum } from "ethereum-protocol";

declare global {
const ethereum: Ethereum;
interface Window {
ethereum: Ethereum;
}
}
6 changes: 3 additions & 3 deletions static/scripts/rewards/ButtonController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`");
Expand All @@ -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`");
Expand All @@ -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`");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function renderTransaction(): Promise<Success> {
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));
Expand Down
2 changes: 1 addition & 1 deletion static/scripts/rewards/web3/connect-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { buttonController, toaster } from "../toaster";

export async function connectWallet(): Promise<JsonRpcSigner | null> {
try {
const wallet = new ethers.providers.Web3Provider(ethereum);
const wallet = new ethers.providers.Web3Provider(window.ethereum);

await wallet.send("eth_requestAccounts", []);

Expand Down
6 changes: 3 additions & 3 deletions static/scripts/rewards/web3/verify-current-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", <T>(newNetworkId: T | string) => handleIfOnCorrectNetwork(parseInt(newNetworkId as string, 16), desiredNetworkId));
window.ethereum.on("chainChanged", <T>(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);
Expand Down
Loading