-
Notifications
You must be signed in to change notification settings - Fork 1
Propose Recoil for Global State #1
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
Open
MorganIsBatman
wants to merge
1
commit into
master
Choose a base branch
from
proposal/recoil-for-global-state
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -21,3 +21,5 @@ | |
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| .idea/ | ||
This file contains hidden or 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 hidden or 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,30 @@ | ||
| import { useEffect, useState } from "react"; | ||
| import {ContractInterface, ethers} from "ethers"; | ||
| import {useRecoilValue} from "recoil"; | ||
| import {isMetamaskConnectedTypeGuard, metamaskState} from "../useMetamask/types"; | ||
|
|
||
| /** | ||
| * A hook to engage with any contract (can be used outright, or as the base to compose other contract-specific hooks) | ||
| * - returned value will be undefined until the user connects their wallet via metamask | ||
| * @param address the blockchain address of the contract | ||
| * @param abi the contract abi (can be partial) | ||
| * | ||
| */ | ||
| export const useContract = (address: string, abi: ContractInterface) => { | ||
| const [contract, setContract] = useState<ethers.Contract | undefined>( | ||
| undefined | ||
| ); | ||
|
|
||
| const metamaskState = useRecoilValue(metamaskState) | ||
|
|
||
| useEffect(() => { | ||
| if (isMetamaskConnectedTypeGuard(metamaskState)) { | ||
| const {provider, signer} = metamaskState; | ||
| const c = new ethers.Contract(address, abi, provider); | ||
| const contractWithSigner = c.connect(signer!); | ||
| setContract(contractWithSigner); | ||
| } | ||
| }, [metamaskState]); | ||
|
|
||
| return contract | ||
| }; | ||
This file contains hidden or 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,28 @@ | ||
| import {atom} from 'recoil'; | ||
| import {ethers} from 'ethers'; | ||
| import {AllMandatory} from "../../utilities/types"; | ||
|
|
||
| export type MetamaskState = { | ||
| provider?: ethers.providers.Web3Provider; | ||
| signer?: ethers.providers.JsonRpcSigner; | ||
| account?: string; | ||
| }; | ||
|
|
||
| type MetamaskIsConnected = AllMandatory<MetamaskState>; // the state when user is connected | ||
|
|
||
| /** | ||
| * A Typeguard to allow using state properties without optional-chaining or multiple if/else | ||
| * @param state the whole state object from metamaskState | ||
| */ | ||
| export const isMetamaskConnectedTypeGuard = (state: MetamaskState | MetamaskIsConnected): state is MetamaskIsConnected => | ||
| !!state.provider && !!state.signer && !!state.account | ||
|
|
||
| export const metamaskState = atom<MetamaskState | MetamaskIsConnected>({ | ||
| key: 'metamaskState', | ||
| dangerouslyAllowMutability: true, | ||
| default: { | ||
| provider: undefined, | ||
| signer: undefined, | ||
| account: undefined, | ||
| }, | ||
| }); |
This file contains hidden or 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,40 @@ | ||
| import { useRecoilState} from 'recoil'; | ||
| import {ethers, providers} from 'ethers'; | ||
| import {isMetamaskConnectedTypeGuard, metamaskState} from "./types"; | ||
|
|
||
| // ethereum isn't on TS standard Window type: | ||
| declare const window: Window & { | ||
| ethereum: providers.ExternalProvider; | ||
| } | ||
|
|
||
| /** | ||
| * A hook to provide Metamask's 'connect' function anywhere, and give global access to the | ||
| * returned values (user's account, provider, signer). | ||
| * - As this uses recoil state, consumers only concerned about the raw state values (and not connect) can simply use | ||
| * useRecoilValue(metamaskState) instead of this hook | ||
| */ | ||
| export const useMetamask = () => { | ||
| const [state, setState] = useRecoilState(metamaskState); | ||
|
|
||
| const connect = async () => { | ||
| const provider = new ethers.providers.Web3Provider(window.ethereum, 'any'); | ||
| await provider.send('eth_requestAccounts', []); | ||
|
|
||
| provider.on('network', (_, oldNetwork) => { | ||
| oldNetwork && window.location.reload(); // might want to change this to render UI | ||
| }); | ||
|
|
||
| const signer = provider.getSigner(); | ||
| const account = await signer.getAddress(); | ||
| setState(() => ({provider, signer, account})); | ||
| }; | ||
|
|
||
| return { | ||
| ...state, | ||
| connect, | ||
| isConnected: isMetamaskConnectedTypeGuard(state) // CAUTION: isConnected is not a type-guard | ||
| }; | ||
| }; | ||
|
|
||
|
|
||
|
|
This file contains hidden or 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,5 @@ | ||
|
|
||
|
|
||
| export type AllMandatory<T> = { | ||
| [Property in keyof T]-?: T[Property] | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useRecoilValueis a hook if you only need to READ state, whereas useRecoilState is a simile to useState, giving you [state, setState] equiv