@stratosphere-network/shared
Version:
Shared utilities and types for StratoSphere SDK
60 lines • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProvider = createProvider;
exports.createWallet = createWallet;
exports.createViemClient = createViemClient;
exports.getChainConfig = getChainConfig;
const ethers_1 = require("ethers");
const chains_1 = require("../types/chains");
const viem_1 = require("viem");
/**
* Creates a JSON RPC provider for the specified chain
* @param chain The supported chain to create a provider for
* @returns JsonRpcProvider instance
*/
function createProvider(chain) {
const chainConfig = chains_1.CHAIN_CONFIGS[chain];
if (!chainConfig) {
throw new Error(`Unsupported chain: ${chain}`);
}
return new ethers_1.JsonRpcProvider(chainConfig.rpcUrl, {
chainId: chainConfig.chainId,
name: chainConfig.name
});
}
/**
* Creates a wallet from a private key and connects it to the specified chain
* @param privateKey The private key to create the wallet from
* @param chain The supported chain to connect to
* @returns Wallet instance connected to the provider
*/
function createWallet(privateKey, chain) {
const provider = createProvider(chain);
// Ensure private key has 0x prefix
const formattedPrivateKey = privateKey.startsWith('0x') ? privateKey : `0x${privateKey}`;
return new ethers_1.Wallet(formattedPrivateKey, provider);
}
/**
* Creates a viem public client for the specified chain
* @param chain The supported chain to create a client for
* @returns Viem public client instance
*/
function createViemClient(chain) {
return (0, viem_1.createPublicClient)({
chain: chains_1.ViemPublicClientChains[chain],
transport: (0, viem_1.http)(),
});
}
/**
* Gets the chain configuration for a supported chain
* @param chain The supported chain
* @returns ChainConfig object
*/
function getChainConfig(chain) {
const config = chains_1.CHAIN_CONFIGS[chain];
if (!config) {
throw new Error(`Unsupported chain: ${chain}`);
}
return config;
}
//# sourceMappingURL=provider.js.map