-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility functions for wallet and event
handling
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
contracts/src/bridge/frontend/src/lib/utils/walletEvents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ethers } from "ethers"; | ||
|
||
export const setupEventListeners = ( | ||
provider: any, | ||
setAddress: (address: string) => void | ||
) => { | ||
const handleAccountsChange = (accounts: string[]) => { | ||
setAddress(accounts[0]); | ||
localStorage.setItem("walletAddress", accounts[0]); | ||
}; | ||
|
||
const handleChainChange = () => { | ||
window.location.reload(); | ||
}; | ||
|
||
provider.on("accountsChanged", handleAccountsChange); | ||
provider.on("chainChanged", handleChainChange); | ||
|
||
return () => { | ||
provider.removeListener("accountsChanged", handleAccountsChange); | ||
provider.removeListener("chainChanged", handleChainChange); | ||
}; | ||
}; | ||
|
||
export const initializeSigner = (provider: any) => { | ||
return new ethers.providers.Web3Provider(provider).getSigner(); | ||
}; |
37 changes: 37 additions & 0 deletions
37
contracts/src/bridge/frontend/src/lib/utils/walletUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import detectEthereumProvider from "@metamask/detect-provider"; | ||
import { ToastType, WalletNetwork } from "../../types"; | ||
import { requestMethods } from "../../routes"; | ||
import { toast } from "@/src/components/ui/use-toast"; | ||
|
||
export const getEthereumProvider = async () => { | ||
const provider = await detectEthereumProvider(); | ||
console.log("🚀 ~ getEthereumProvider ~ provider:", provider); | ||
if (!provider) { | ||
throw new Error("No Ethereum provider detected"); | ||
} | ||
return provider; | ||
}; | ||
|
||
export const switchNetwork = async ( | ||
provider: any, | ||
desiredNetwork: WalletNetwork | ||
) => { | ||
if (!provider) { | ||
toast({ | ||
title: "Error", | ||
description: "Please connect to wallet first", | ||
variant: ToastType.DESTRUCTIVE, | ||
}); | ||
return; | ||
} | ||
await provider.request({ | ||
method: requestMethods.switchNetwork, | ||
params: [{ chainId: desiredNetwork }], | ||
}); | ||
}; | ||
|
||
export const handleStorage = { | ||
save: (key: string, value: string) => localStorage.setItem(key, value), | ||
get: (key: string) => localStorage.getItem(key), | ||
remove: (key: string) => localStorage.removeItem(key), | ||
}; |