Working with different Web3 providers
EVM Networks
By default, a viem provider is used for EVM-compatible JSON-RPC interaction. However, you can use Holyheld SDK with other providers like Ethers.js and Web3.js.
Wagmi
import { getPublicClient, getWalletClient } from '@wagmi/core';
const chainId; // token chain id
(async () => {
const publicClient = getPublicClient({ chainId });
const walletClient = await getWalletClient({ chainId });
})();
Ethers.js
import { providers } from 'ethers';
import { createPublicClient, createWalletClient, custom, http } from 'viem';
const chain; // chain entity from viem
const provider = new providers.Web3Provider(window.ethereum);
const publicClient = createPublicClient({
chain,
transport: http(),
});
const walletClient = createWalletClient({
chain,
transport: custom(provider.provider),
account: '0x...', // wallet address
});
Web3.js
import Web3 from 'web3';
import { createPublicClient, createWalletClient, custom, http } from 'viem';
const chain; // chain entity from viem
const provider = new Web3(window.ethereum).currentProvider;
const publicClient = createPublicClient({
chain,
transport: http(),
});
const walletClient = createWalletClient({
chain,
transport: custom(provider),
account: '0x...', // wallet address
});
Solana Network
To interact with the Solana network, Holyheld SDK requires two things: a connection to the network and a wallet client.
Connection
The Connection
object is part of the @solana/web3.js library and is responsible for all communication with the Solana network.
import { Connection, clusterApiUrl } from '@solana/web3.js';
const networkInfo = sdk.solana.getNetwork(SolanaNetwork.Mainnet);
const connection = new Connection(networkInfo.httpRpcURL, {
commitment: 'confirmed',
wsEndpoint: networkInfo.wsRpcURL ?? clusterApiUrl(networkInfo.cluster, true)
});
Wallet client
The wallet client must implement the following interface expected by the SDK:
import { type SendOptions, VersionedTransaction } from '@solana/web3.js';
interface WalletClientSolana {
signMessage(message: string): Promise<string>;
signTransaction(transaction: VersionedTransaction): Promise<VersionedTransaction>;
signAllTransactions?(transactions: Array<VersionedTransaction>): Promise<Array<VersionedTransaction>>;
signAndSendTransaction(transaction: VersionedTransaction, sendOptions?: SendOptions): Promise<string>;
}
You can implement this interface manually, or use one of the wallet adapters. The SDK provides a helper function to wrap such adapters into the required format.
For example, PhantomWalletAdapter
:
import { Connection } from '@solana/web3.js';
import { PhantomWalletAdapter } from '@solana/wallet-adapter-phantom';
import { createSolanaWalletClientFromAdapter } from '@holyheld/sdk';
const walletAdapter = new PhantomWalletAdapter();
const connection = new Connection(/* ... */);
const walletClient = createSolanaWalletClientFromAdapter(walletAdapter, connection);