mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-01-11 12:33:11 +00:00
* feat: use wagmi to generate contract types * feat: migrate rln from ethers to viem * fix: remove .gitmodules * fix: update readme * fix: refactor to use a single viem client object * fix: update comments, tsconfig * feat: remove membership event tracking * fix: script name in package.json and readme * fix: only allow linea sepolia * fix: consolidate viem types, typed window * fix: use viem to infer type of decoded event * fix: use js for generate abi script * feat: generate abi and build rln package as release condition * fix: check that eth_requestAccounts returns an array * fix: handle error messages * fix: use https instead of git for cloning in script * fix: add warning annotations for contract typings check * fix: install deps for rln package before building * fix: use pnpm when installing rln contracts * fix: use workspace flag to run abi script * fix: add ref to checkout action * fix: include pnpm in ci
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import "viem/window";
|
|
import {
|
|
type Address,
|
|
createWalletClient,
|
|
custom,
|
|
PublicActions,
|
|
publicActions,
|
|
WalletClient
|
|
} from "viem";
|
|
import { lineaSepolia } from "viem/chains";
|
|
|
|
export type RpcClient = WalletClient & PublicActions;
|
|
|
|
/**
|
|
* Checks window for injected Ethereum provider, requests user to connect, and creates an RPC client object
|
|
* capable of performing both read and write operations on the blockchain.
|
|
*
|
|
* If the wallet is not connected to the Linea Sepolia network, it will attempt to switch to it.
|
|
* If the wallet does not have the Linea Sepolia network added, it will attempt to add it.
|
|
*/
|
|
export const createViemClientFromWindow = async (): Promise<RpcClient> => {
|
|
const ethereum = window.ethereum;
|
|
|
|
if (!ethereum) {
|
|
throw Error(
|
|
"Missing or invalid Ethereum provider. Please install a Web3 wallet such as MetaMask."
|
|
);
|
|
}
|
|
|
|
const accounts = await ethereum.request({ method: "eth_requestAccounts" });
|
|
if (!Array.isArray(accounts)) {
|
|
throw Error("Failed to get accounts");
|
|
}
|
|
const account = accounts[0] as Address;
|
|
|
|
const rpcClient: RpcClient = createWalletClient({
|
|
account: account as Address,
|
|
chain: lineaSepolia,
|
|
transport: custom(window.ethereum!)
|
|
}).extend(publicActions);
|
|
|
|
// Ensure wallet is connected to Linea Sepolia
|
|
try {
|
|
await rpcClient.switchChain({ id: lineaSepolia.id });
|
|
} catch (error: unknown) {
|
|
// This error code indicates that the chain has not been added to the wallet
|
|
if (
|
|
typeof error === "object" &&
|
|
error !== null &&
|
|
"code" in error &&
|
|
error.code === 4902
|
|
) {
|
|
await rpcClient.addChain({ chain: lineaSepolia });
|
|
await rpcClient.switchChain({ id: lineaSepolia.id });
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return rpcClient;
|
|
};
|