eatthepie
Version:
Command line app for interacting with Eat The Pie, the world lottery on World Chain.
66 lines (60 loc) • 1.85 kB
JavaScript
/**
* Ethereum client configuration utilities
* Handles creation of public and wallet clients for different networks
*/
import {
createPublicClient as viemCreatePublicClient,
createWalletClient as viemCreateWalletClient,
http,
} from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { mainnet, worldchain, sepolia } from "viem/chains";
/**
* Create a public client for reading from the blockchain
* @param {Object} config - Client configuration
* @param {string} config.network - Network name (mainnet, worldchain, sepolia)
* @param {string} config.rpcUrl - RPC endpoint URL
* @returns {PublicClient} Configured public client
*/
export function createPublicClient(config) {
const chain = getChain(config.network);
return viemCreatePublicClient({
chain,
transport: http(config.rpcUrl),
});
}
/**
* Create a wallet client for signing transactions
* @param {Object} config - Client configuration
* @param {string} config.network - Network name (mainnet, worldchain, sepolia)
* @param {string} config.rpcUrl - RPC endpoint URL
* @param {string} config.privateKey - Private key for signing
* @returns {WalletClient} Configured wallet client
*/
export function createWalletClient(config) {
const chain = getChain(config.network);
const account = privateKeyToAccount(config.privateKey);
return viemCreateWalletClient({
account,
chain,
transport: http(config.rpcUrl),
});
}
/**
* Get chain configuration for specified network
* @param {string} network - Network name
* @returns {Chain} Chain configuration
* @throws {Error} If network is unsupported
*/
function getChain(network) {
const chains = {
mainnet,
worldchain,
sepolia,
};
const chain = chains[network];
if (!chain) {
throw new Error(`Unsupported network: ${network}`);
}
return chain;
}