forked from synapsecns/sanguine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethers.ts
55 lines (50 loc) · 1.76 KB
/
ethers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { getWalletClient, getPublicClient } from '@wagmi/core'
import { providers } from 'ethers'
import { type HttpTransport, type WalletClient, type PublicClient } from 'viem'
import { wagmiConfig } from './wagmiConfig'
export const 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, network)
}
/** Action to convert a viem Public Client to an ethers.js Provider. */
export const getEthersProvider = ({ chainId }: { chainId?: number } = {}) => {
const publicClient = getPublicClient(wagmiConfig, { chainId })
return publicClientToProvider(publicClient as any)
}
export const walletClientToSigner = (walletClient: WalletClient) => {
console.log(walletClient)
const { account, chain, transport } = walletClient
const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
}
// @ts-ignore
const provider = new providers.Web3Provider(transport, network)
const signer = provider.getSigner(account.address)
return signer
}
/** Action to convert a viem Wallet Client to an ethers.js Signer. */
export const getEthersSigner = async ({
chainId,
}: { chainId?: number } = {}) => {
const walletClient = await getWalletClient(wagmiConfig, {
chainId,
})
if (!walletClient) {
return undefined
}
return walletClientToSigner(walletClient)
}