Skip to content

Commit

Permalink
feat: accept {provider, signer} as ethers provider (#3314)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp authored Feb 16, 2022
1 parent a60ea70 commit 4c966ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/lib/components/Web3Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { initializeConnector, Web3ReactHooks } from '@web3-react/core'
import { EIP1193 } from '@web3-react/eip1193'
import { Actions, Connector, Provider as EthProvider } from '@web3-react/types'
import { Actions, Connector, Provider as Eip1193Provider } from '@web3-react/types'
import { Url } from '@web3-react/url'
import { SetStateAction } from 'jotai'
import { RESET, useUpdateAtom } from 'jotai/utils'
Expand All @@ -9,7 +9,7 @@ import { ReactNode, useEffect } from 'react'

interface Web3ProviderProps {
jsonRpcEndpoint?: string
provider?: EthProvider
provider?: Eip1193Provider
children: ReactNode
}

Expand Down
20 changes: 17 additions & 3 deletions src/lib/components/Widget.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Provider as EthProvider } from '@web3-react/types'
import { Provider as EthersProvider } from '@ethersproject/abstract-provider'
import { Signer as EthersSigner } from '@ethersproject/abstract-signer'
import { Eip1193Bridge } from '@ethersproject/experimental'
import { ExternalProvider } from '@ethersproject/providers'
import { JsonRpcProvider } from '@ethersproject/providers'
import { Provider as Eip1193Provider } from '@web3-react/types'
import { DEFAULT_LOCALE, SupportedLocale } from 'constants/locales'
import { Provider as AtomProvider } from 'jotai'
import { TransactionsUpdater } from 'lib/hooks/transactions'
Expand Down Expand Up @@ -91,7 +96,7 @@ function Updaters() {
export type WidgetProps = {
theme?: Theme
locale?: SupportedLocale
provider?: EthProvider
provider?: Eip1193Provider | ExternalProvider | JsonRpcProvider | { provider: EthersProvider; signer: EthersSigner }
jsonRpcEndpoint?: string
width?: string | number
dialog?: HTMLElement | null
Expand All @@ -112,6 +117,15 @@ export default function Widget(props: PropsWithChildren<WidgetProps>) {
onError,
} = props

let eip1193: Eip1193Provider | undefined
if (provider && 'provider' in provider && 'signer' in provider) {
eip1193 = new Eip1193Bridge(provider.signer, provider.provider)
} else if (provider instanceof JsonRpcProvider) {
eip1193 = new Eip1193Bridge(provider.getSigner(), provider)
} else if (provider) {
eip1193 = provider as Eip1193Provider
}

const [dialog, setDialog] = useState<HTMLDivElement | null>(null)
return (
<StrictMode>
Expand All @@ -124,7 +138,7 @@ export default function Widget(props: PropsWithChildren<WidgetProps>) {
<WidgetPropValidator {...props}>
<ReduxProvider store={multicallStore}>
<AtomProvider>
<Web3Provider provider={provider} jsonRpcEndpoint={jsonRpcEndpoint}>
<Web3Provider provider={eip1193} jsonRpcEndpoint={jsonRpcEndpoint}>
<Updaters />
{children}
</Web3Provider>
Expand Down

0 comments on commit 4c966ca

Please sign in to comment.