Skip to content

Commit

Permalink
Adding helpers: Adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
hide-on-bush-x committed Aug 31, 2023
1 parent 9ebdbba commit cb916b4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -45,6 +45,7 @@
"@wagmi/chains": "^1.6.0",
"@wagmi/core": "~0.10.17",
"@walletconnect/modal": "^2.6.1",
"ethers": "5.7.2",
"rc-tooltip": "^6.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
25 changes: 25 additions & 0 deletions src/refactor/helpers/ethers.ts
@@ -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;
};
32 changes: 32 additions & 0 deletions src/refactor/helpers/wagmi.ts
@@ -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;
}
18 changes: 16 additions & 2 deletions src/refactor/wallet-client/wallet/use-wallet.ts
Expand Up @@ -3,7 +3,7 @@ import {
useChainModal,
useAccountModal,
} from '@rainbow-me/rainbowkit';
import type { Connector } from '@wagmi/core';
import type { Connector, Provider } from '@wagmi/core';
import { useEffect, useMemo, useState } from 'react';

import {
Expand All @@ -15,6 +15,9 @@ import {
PublicClient,
WalletClient,
} from 'wagmi';
import { Signer } from 'ethers';

import { useEthersProvider, useEthersSigner } from '../../helpers/ethers';

export interface UseWalletReturn {
address?: `0x${string}`;
Expand All @@ -25,6 +28,8 @@ export interface UseWalletReturn {
shortAddress?: `0x${string}`;
publicClient?: PublicClient;
walletClient?: WalletClient;
signer?: Signer;
provider?: Provider;
connector?: Connector;
isConnected?: boolean;
isConnecting?: boolean;
Expand Down Expand Up @@ -58,8 +63,13 @@ const useWallet = (): UseWalletReturn => {
const [compareAddress, setCompareAddress] = useState(address);
const { data: walletClient, isLoading: isLoadingWalletClient } =
useWalletClient();
const { disconnect, disconnectAsync } = useDisconnect();

const signer = useEthersSigner();

const publicClient = usePublicClient();
const provider = useEthersProvider();

const { disconnect, disconnectAsync } = useDisconnect();
const {
data: balanceResult,
// isError: isErrorBalance,
Expand Down Expand Up @@ -121,6 +131,8 @@ const useWallet = (): UseWalletReturn => {
compareAddress,
publicClient,
walletClient,
signer,
provider,
connector,
isConnected,
isConnecting,
Expand All @@ -145,6 +157,8 @@ const useWallet = (): UseWalletReturn => {
compareAddress,
publicClient,
walletClient,
signer,
provider,
connector,
isConnected,
isConnecting,
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -8092,7 +8092,7 @@ eth-rpc-errors@^4.0.2:
dependencies:
fast-safe-stringify "^2.0.6"

ethers@^5.7.2, ethers@~5.7.2:
ethers@5.7.2, ethers@^5.7.2, ethers@~5.7.2:
version "5.7.2"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.2.tgz#3a7deeabbb8c030d4126b24f84e525466145872e"
integrity sha512-wswUsmWo1aOK8rR7DIKiWSw9DbLWe6x98Jrn8wcTflTVvaXhAMaB5zGAXy0GYQEQp9iO1iSHWVyARQm11zUtyg==
Expand Down

0 comments on commit cb916b4

Please sign in to comment.