Skip to content

Commit

Permalink
feat: make connectors resettable
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp committed Dec 20, 2021
1 parent 12f522c commit db5af68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/lib/components/Web3Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useAtom } from 'jotai'
import { RESET } from 'jotai/utils'
import { injectedConnectorAtom, networkConnectorAtom } from 'lib/state'
import { ReactNode, useEffect } from 'react'
import { initializeConnector } from 'widgets-web3-react/core'
Expand All @@ -18,6 +19,8 @@ export default function Web3Provider({ provider, jsonRpcEndpoint, children }: We
if (jsonRpcEndpoint) {
const [connector, hooks] = initializeConnector<Network>((actions) => new Network(actions, jsonRpcEndpoint))
setNetworkConnector([connector, hooks])
} else {
setNetworkConnector(RESET)
}
}, [setNetworkConnector, jsonRpcEndpoint])

Expand All @@ -26,6 +29,8 @@ export default function Web3Provider({ provider, jsonRpcEndpoint, children }: We
if (provider) {
const [connector, hooks] = initializeConnector<EIP1193>((actions) => new EIP1193(actions, provider))
setInjectedConnector([connector, hooks])
} else {
setInjectedConnector(RESET)
}
}, [setInjectedConnector, provider])

Expand Down
13 changes: 8 additions & 5 deletions src/lib/state/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { createMulticall } from '@uniswap/redux-multicall'
import { NETWORK_URLS } from 'connectors'
import { atom } from 'jotai'
import { atomWithStore } from 'jotai/redux'
import { atomWithDefault } from 'jotai/utils'
import { createStore } from 'redux'
import { Web3ReactHooks } from 'widgets-web3-react/core'
import { initializeConnector } from 'widgets-web3-react/core'
import { Network } from 'widgets-web3-react/network'
import { Connector } from 'widgets-web3-react/types'

export const networkConnectorAtom = atom<[Network, Web3ReactHooks]>(
initializeConnector<Network>((actions) => new Network(actions, NETWORK_URLS))
const EMPTY_CONNECTOR = [undefined, undefined]

export const networkConnectorAtom = atomWithDefault<[Network, Web3ReactHooks] | typeof EMPTY_CONNECTOR>(
() => EMPTY_CONNECTOR
)
export const injectedConnectorAtom = atomWithDefault<[Connector, Web3ReactHooks] | typeof EMPTY_CONNECTOR>(
() => EMPTY_CONNECTOR
)
export const injectedConnectorAtom = atom<[Connector, Web3ReactHooks] | [undefined, undefined]>([undefined, undefined])

export const connectorAtom = atom((get) => {
const injectedContext = get(injectedConnectorAtom)
Expand Down

0 comments on commit db5af68

Please sign in to comment.