-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup Web3Context with web3modal and ethers.js
Sets up the logic and components for connecting to Ethereum wallets using Web3Modal with support for both MetaMask and WalletConnect. UI to actually login will be implemented in upcoming PRs. Related: #2975 Test Plan: Ensure app runs without crashing
- Loading branch information
1 parent
f44ae63
commit 0722e53
Showing
5 changed files
with
1,647 additions
and
52 deletions.
There are no files selected for viewing
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,33 @@ | ||
// flow-typed signature: bb27aa0ed14d1e118ca352d4a01c6376 | ||
// flow-typed version: <<STUB>>/ethers_v5.x.x/flow_v0.135.0 | ||
|
||
/** | ||
* This is an autogenerated libdef stub for: | ||
* | ||
* 'ethers' | ||
* | ||
* Fill this stub out by replacing all the `any` types. | ||
* | ||
* Once filled out, we encourage you to share your work with the | ||
* community by sending a pull request to: | ||
* https://github.com/flowtype/flow-typed | ||
*/ | ||
|
||
declare module 'ethers' { | ||
declare class Signer { | ||
getAddress(): Promise<string> | ||
} | ||
|
||
declare class Web3Provider { | ||
constructor(provider: any): void; | ||
|
||
getSigner(): Signer; | ||
} | ||
|
||
|
||
declare module.exports: { | ||
providers: { | ||
Web3Provider: typeof Web3Provider, | ||
} | ||
}; | ||
} |
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
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
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,106 @@ | ||
// @flow | ||
|
||
import WalletConnectProvider from "@walletconnect/web3-provider"; | ||
import {providers} from "ethers"; | ||
import * as React from "react"; | ||
import Web3Modal from "web3modal"; | ||
|
||
const INFURA_ID = "a60f8c4d3d4a40a49c4568570a7546b7"; | ||
|
||
export type Web3ContextType = {| | ||
provider: providers.Web3Provider | null, | ||
connectWeb3: () => Promise<void>, | ||
disconnect: () => void, | ||
isConnecting: boolean, | ||
isConnected: boolean, | ||
address: string | null, | ||
|}; | ||
|
||
export const Web3Context: React$Context<Web3ContextType> = React.createContext({ | ||
provider: null, | ||
connectWeb3: async () => {}, | ||
disconnect: () => undefined, | ||
isConnecting: false, | ||
isConnected: false, | ||
address: null, | ||
}); | ||
|
||
const providerOptions = { | ||
walletconnect: { | ||
package: WalletConnectProvider, | ||
options: { | ||
infuraId: INFURA_ID, | ||
}, | ||
}, | ||
}; | ||
|
||
const web3Modal = | ||
typeof window !== "undefined" && | ||
new Web3Modal({ | ||
network: "mainnet", | ||
cacheProvider: true, | ||
providerOptions, | ||
}); | ||
|
||
type Web3ContextProviderProps = {| | ||
+children: React.Node, | ||
|}; | ||
|
||
export const useWeb3 = (): Web3ContextType => React.useContext(Web3Context); | ||
|
||
export const Web3ContextProvider = ({ | ||
children, | ||
}: Web3ContextProviderProps): React.Node => { | ||
const [provider, setProvider] = React.useState<providers.Web3Provider | null>( | ||
null | ||
); | ||
const [isConnected, setIsConnected] = React.useState<boolean>(false); | ||
const [isConnecting, setIsConnecting] = React.useState<boolean>(false); | ||
const [address, setAddress] = React.useState<string | null>(null); | ||
|
||
const disconnect = React.useCallback(() => { | ||
if (web3Modal === false) return; | ||
|
||
web3Modal.clearCachedProvider(); | ||
setAddress(null); | ||
setProvider(null); | ||
setIsConnecting(false); | ||
setIsConnected(false); | ||
}, []); | ||
|
||
const connectWeb3 = React.useCallback(async () => { | ||
if (web3Modal === false) return; | ||
setIsConnecting(true); | ||
|
||
try { | ||
const web3Provider = await web3Modal.connect(); | ||
const ethersProvider = new providers.Web3Provider(web3Provider); | ||
|
||
const ethAddress = await ethersProvider.getSigner().getAddress(); | ||
|
||
setAddress(ethAddress); | ||
setProvider(ethersProvider); | ||
setIsConnecting(false); | ||
setIsConnected(true); | ||
} catch (error) { | ||
console.log(error); | ||
setIsConnecting(false); | ||
disconnect(); | ||
} | ||
}, [disconnect]); | ||
|
||
return ( | ||
<Web3Context.Provider | ||
value={{ | ||
provider, | ||
connectWeb3, | ||
disconnect, | ||
isConnected, | ||
isConnecting, | ||
address, | ||
}} | ||
> | ||
{children} | ||
</Web3Context.Provider> | ||
); | ||
}; |
Oops, something went wrong.