-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use a void signer for ethers providers (#3327)
- Loading branch information
Showing
4 changed files
with
40 additions
and
16 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
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,34 @@ | ||
import { Provider as EthersProvider } from '@ethersproject/abstract-provider' | ||
import { Signer as EthersSigner } from '@ethersproject/abstract-signer' | ||
import { VoidSigner } from '@ethersproject/abstract-signer' | ||
import { Eip1193Bridge } from '@ethersproject/experimental' | ||
import { JsonRpcProvider } from '@ethersproject/providers' | ||
import { Provider as Eip1193Provider } from '@web3-react/types' | ||
import { ZERO_ADDRESS } from 'constants/misc' | ||
import { useMemo } from 'react' | ||
|
||
const voidSigner = new VoidSigner(ZERO_ADDRESS) | ||
|
||
export default function useEip1193Provider( | ||
provider?: Eip1193Provider | EthersProvider | JsonRpcProvider | { provider: EthersProvider; signer: EthersSigner } | ||
): Eip1193Provider | undefined { | ||
return useMemo(() => { | ||
if (provider) { | ||
if (provider instanceof EthersProvider) { | ||
// A JsonRpcProvider includes its own Signer, otherwise use a VoidSigner. | ||
const signer = 'getSigner' in provider ? provider.getSigner() : null ?? voidSigner | ||
return new Eip1193Bridge(signer, provider) | ||
} | ||
|
||
if ('provider' in provider && 'signer' in provider) { | ||
return new Eip1193Bridge(provider.signer, provider.provider) | ||
} | ||
|
||
// See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md. | ||
if ('request' in provider && 'on' in provider && 'removeListener' in provider) { | ||
return provider | ||
} | ||
} | ||
return | ||
}, [provider]) | ||
} |
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