Skip to content

Instantly share code, notes, and snippets.

@sarthakxv
Last active November 2, 2024 17:20
Show Gist options
  • Select an option

  • Save sarthakxv/8d036136feca0303b9e6bba795f2bc53 to your computer and use it in GitHub Desktop.

Select an option

Save sarthakxv/8d036136feca0303b9e6bba795f2bc53 to your computer and use it in GitHub Desktop.
Viem to Ethers Signer

WAGMI to Ethers Signer

  • useUniSdk is a hook that initize UniSdk, a SDK which uses ethers implementation and requires ether Signer to be passes to init and use the SDK.
  • Our attempt is to use Wagmi and Viem on client side, hence creating a ethers Signer to from Wagmi to pass into SDK constructor inorder to call the Contract methods.
  1. UniswapPoolDeployer is the example component which uses useUniSdk
  2. useUniSdk is a hook to initilises the SDK
  3. useEthersSigner contains the adapter and returns the Signer
  4. useEthersWalletSigners is another iteration of previous hook implemented using walletClient to get Signer
/* Example of using the useUniSdk hook in a Component /*
import { useUniSdk } from "@lib/hooks/useUniSdk";
// Rest of the imports
const UniswapPoolDeployer() {
const { uniSdk } = useUniSdk();
const handleCreatePool = async (baseToken, quoteToken, priceOfBaseInQuote, fee) => {
// sample call
const pool = await uniSdk?.createPool(
baseToken,
quoteToken,
4,
FeeAmount.LOW
);
}
return {
// Rest of the component
}
}
import { config } from "@lib/utils/wagmi";
import { ethers } from "ethers";
import { useMemo } from "react";
import type { Account, Chain, Client, Transport } from "viem";
import { Config, useConnectorClient } from "wagmi";
export function clientToSigner(client: Client<Transport, Chain, Account>) {
const { account, chain, transport } = client;
const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
};
const provider = new ethers.providers.Web3Provider(transport, network);
const signer = provider.getSigner(account.address);
return signer;
}
/** Hook to convert a Viem Client to an ethers.js Signer. */
export function useEthersSigner({ chainId }: { chainId?: number } = {}) {
const { data: client } = useConnectorClient<Config>({ chainId });
return useMemo(() => (client ? clientToSigner(client) : undefined), [client]);
}
// Another iteration of `useEthersSigners` using walletClient
import { useMemo } from "react";
import type { Account, Chain, Client, Transport } from "viem";
import { useWalletClient } from "wagmi";
import { ethers, providers } from "ethers";
import { BASE_SEPOLIA_RPC } from "@lib/keys";
export function useEthersWalletSigner({ chainId }: { chainId?: number } = {}) {
const { data: walletClient } = useWalletClient({ chainId });
const walletSigner = useMemo(() => {
if (!walletClient) return undefined;
// Base Sepolia configuration
const network = {
chainId: 84532,
name: "Base Sepolia",
ensAddress: undefined,
// You can add the RPC URL here if needed
_defaultProvider: (providers: any) =>
new providers.JsonRpcProvider(
BASE_SEPOLIA_RPC // or your preferred RPC URL
),
};
const provider = new providers.Web3Provider(
walletClient.transport,
network
);
return provider.getSigner(walletClient.account.address);
}, [walletClient, chainId]);
return walletSigner;
}
import { useMemo, useState } from "react";
import { useAccount } from "wagmi";
import { UniSdk } from "@sample/uni-sdk";
import { useEthersSigner } from "./useEthersSigner";
export function useUniSdk() {
const [error, setError] = useState<Error | null>(null);
const { address, isConnected, chainId } = useAccount();
const signer = useEthersSigner({
chainId,
});
const uniSdk = useMemo(() => {
if (!isConnected || !signer || !window?.ethereum) {
return undefined;
}
try {
console.log("signer", signer);
return new UniSdk(signer);
} catch (err) {
setError(
err instanceof Error ? err : new Error("Failed to initialise UniSdk")
);
return undefined;
}
}, [signer, isConnected]);
return {
uniSdk,
error,
isInitialised: !!uniSdk,
address,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment