Created
April 25, 2024 16:25
-
-
Save Rashmi-278/8728ff58e50d043c98ac1ef9612ff1df to your computer and use it in GitHub Desktop.
Wagmi Utils For EAS SDK
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
import { providers } from "ethers"; | |
import { useEffect, useState } from "react"; | |
import { usePublicClient, useWalletClient } from "wagmi"; | |
export function publicClientToProvider(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.map( | |
({ value }) => new providers.JsonRpcProvider(value?.url, network) | |
) | |
); | |
return new providers.JsonRpcProvider(transport.url, network); | |
} | |
export function walletClientToSigner(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; | |
} | |
export function useSigner() { | |
const { data: walletClient } = useWalletClient(); | |
const [signer, setSigner] = useState(undefined); | |
useEffect(() => { | |
async function getSigner() { | |
if (!walletClient) return; | |
const tmpSigner = walletClientToSigner(walletClient); | |
setSigner(tmpSigner); | |
} | |
getSigner(); | |
}, [walletClient]); | |
return signer; | |
} | |
export function useProvider() { | |
const publicClient = usePublicClient(); | |
const [provider, setProvider] = useState(undefined); | |
useEffect(() => { | |
async function getSigner() { | |
if (!publicClient) return; | |
const tmpProvider = publicClientToProvider(publicClient); | |
setProvider(tmpProvider); | |
} | |
getSigner(); | |
}, [publicClient]); | |
return provider; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment