Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ebdbba
commit cb916b4
Showing
5 changed files
with
75 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useMemo } from 'react'; | ||
import { usePublicClient, useWalletClient } from 'wagmi'; | ||
import type { Provider } from '@wagmi/core'; | ||
import { Signer } from 'ethers'; | ||
import { publicClientToProvider, walletClientToSigner } from './wagmi'; | ||
|
||
export const useEthersProvider = (): Provider | undefined => { | ||
const publicClient = usePublicClient(); | ||
const provider = useMemo(() => { | ||
if (publicClient) publicClientToProvider(publicClient); | ||
return undefined; | ||
}, [publicClient]); | ||
|
||
return provider; | ||
}; | ||
|
||
export const useEthersSigner = (): Signer | undefined => { | ||
const { data: walletClient } = useWalletClient(); | ||
const signer = useMemo(() => { | ||
if (walletClient) walletClientToSigner(walletClient); | ||
return undefined; | ||
}, [walletClient]); | ||
|
||
return signer; | ||
}; |
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,32 @@ | ||
import { type PublicClient, type WalletClient } from 'wagmi'; | ||
|
||
import { providers } from 'ethers'; | ||
import { type HttpTransport } from 'viem'; | ||
|
||
export function publicClientToProvider(publicClient: PublicClient) { | ||
const { chain, transport } = publicClient; | ||
const network = { | ||
chainId: chain.id, | ||
name: chain.name, | ||
ensAddress: chain.contracts?.ensRegistry?.address, | ||
}; | ||
if (transport.type === 'fallback') | ||
return new providers.FallbackProvider( | ||
(transport.transports as ReturnType<HttpTransport>[]).map( | ||
({ value }) => new providers.JsonRpcProvider(value?.url, network) | ||
) | ||
); | ||
return new providers.JsonRpcProvider(transport.url as string, network); | ||
} | ||
|
||
export function walletClientToSigner(walletClient: WalletClient) { | ||
const { account, chain, transport } = walletClient; | ||
const network = { | ||
chainId: chain.id, | ||
name: chain.name, | ||
ensAddress: chain.contracts?.ensRegistry?.address, | ||
}; | ||
const provider = new providers.Web3Provider(transport, network); | ||
const signer = provider.getSigner(account.address); | ||
return signer; | ||
} |
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