#adapter-walletconnect.ts
This file ships in @gibs/bridge-sdk, at node_modules/@gibs/bridge-sdk/docs/examples/adapter-walletconnect.ts.
/**
* The WalletConnect adapter: the signer and the receipt wait of the examples,
* written with Reown AppKit and wagmi. The Gibs Finance interface connects
* wallets the same way.
*
* Install it beside the packages:
* `npm install @reown/appkit @reown/appkit-adapter-wagmi wagmi @wagmi/core`.
* AppKit runs on wagmi, and wagmi runs on viem. So the chain reads are the
* viem reads (`adapter-viem.ts`), and only the wallet steps change: connect,
* switch the chain, send, and wait for the receipt.
*
* Typechecked only (`docs/examples/tsconfig.json` in `@gibs/bridge-sdk`). Never run against mainnet.
*/
import { type ChainAdapter, createAppKit } from '@reown/appkit';
import { mainnet, pulsechain } from '@reown/appkit/networks';
import { WagmiAdapter } from '@reown/appkit-adapter-wagmi';
import { type Config, getAccount, sendTransaction, switchChain, waitForTransactionReceipt } from '@wagmi/core';
import type { Hex, PublicClient } from 'viem';
import { viemChainAccess } from './adapter-viem.js';
import type { ChainAccess, ReceiptOutcome, WalletSigner } from './shared.js';
/**
* Your Reown project id. Get one at https://cloud.reown.com/. This placeholder
* does not connect.
*/
export const YOUR_REOWN_PROJECT_ID = 'your-reown-project-id';
/**
* Opens AppKit on Ethereum and PulseChain. In a React app, import
* `createAppKit` from `@reown/appkit/react` instead.
*
* @returns AppKit, and the wagmi config the signer uses
*/
export const createWalletConnect = () => {
const networks: [typeof mainnet, typeof pulsechain] = [mainnet, pulsechain];
const wagmiAdapter = new WagmiAdapter({ projectId: YOUR_REOWN_PROJECT_ID, networks });
const appKit = createAppKit({
// AppKit 1.8.18 types `ChainAdapter.namespace` as required, and the wagmi adapter declares it optional.
// Under `exactOptionalPropertyTypes` the two disagree, so the Gibs interface casts the same way.
adapters: [wagmiAdapter as unknown as ChainAdapter],
networks,
projectId: YOUR_REOWN_PROJECT_ID,
metadata: {
name: 'Your wallet',
description: 'Bridges with Gibs Finance',
url: 'https://your-wallet.example',
icons: ['https://your-wallet.example/icon.png'],
},
});
return { appKit, config: wagmiAdapter.wagmiConfig };
};
/**
* The signer, on the wallet that the user connected in the AppKit modal.
*
* @param config - the wagmi config from {@link createWalletConnect}
* @returns the signer
* @throws when no wallet is connected yet
*/
export const walletConnectSigner = (config: Config): WalletSigner => {
// #region signer
// Open the modal with `appKit.open()`. Once the user connects, wagmi knows the account.
const { address } = getAccount(config);
if (address === undefined) throw new Error('connect a wallet in the AppKit modal first');
const signer: WalletSigner = {
address,
sendTransaction: async (request) => {
// The wallet may sit on another chain. Ask it to switch before it signs.
if (getAccount(config).chainId !== request.chainId) await switchChain(config, { chainId: request.chainId });
return sendTransaction(config, {
chainId: request.chainId,
to: request.to,
data: request.data,
value: request.value,
...(request.gas === undefined ? {} : { gas: request.gas }),
});
},
};
// #endregion signer
return signer;
};
/**
* The chain reads: the viem reads, with the receipt wait through wagmi.
*
* @param config - the wagmi config from {@link createWalletConnect}
* @param requireClient - see `requireClientWith` in `shared.ts`
* @returns the chain reads
*/
export const walletConnectChainAccess = (
config: Config,
requireClient: (chainId: number) => PublicClient,
): ChainAccess => {
// #region receipt
const waitForReceipt = async (request: { chainId: number; hash: Hex }): Promise<ReceiptOutcome> => {
const receipt = await waitForTransactionReceipt(config, { chainId: request.chainId, hash: request.hash });
return receipt.status;
};
// #endregion receipt
return { ...viemChainAccess(requireClient), waitForReceipt };
};