UNPKG

linea-mcp

Version:

A Model Context Protocol server for interacting with the Linea blockchain

112 lines (111 loc) 3.42 kB
import { ethers } from 'ethers'; import config from '../config/index.js'; // Add .js extension /** * Service for interacting with the Linea blockchain */ class BlockchainService { _provider; network; /** * Create a new BlockchainService instance * @param network The network to connect to (mainnet, testnet, or ethereum) */ constructor(network = 'mainnet') { this.network = network; let rpcUrl; switch (network) { case 'ethereum': rpcUrl = config.rpc.ethereum; break; case 'testnet': rpcUrl = config.rpc.testnet; break; case 'mainnet': default: rpcUrl = config.rpc.mainnet; break; } this._provider = new ethers.providers.JsonRpcProvider(rpcUrl); } /** * Get the current provider * @returns The JsonRpcProvider instance */ get provider() { return this._provider; } /** * Get the current network * @returns The network name (mainnet, testnet, or ethereum) */ get currentNetwork() { return this.network; } /** * Get the current block number * @returns A promise that resolves to the current block number */ async getBlockNumber() { return this._provider.getBlockNumber(); } /** * Get the balance of an address * @param address The address to check the balance of * @returns A promise that resolves to the balance in ETH */ async getBalance(address) { const balance = await this._provider.getBalance(address); return ethers.utils.formatEther(balance); } /** * Get a transaction by its hash * @param txHash The transaction hash * @returns A promise that resolves to the transaction details */ async getTransaction(txHash) { return this._provider.getTransaction(txHash); } /** * Get a transaction receipt by its hash * @param txHash The transaction hash * @returns A promise that resolves to the transaction receipt */ async getTransactionReceipt(txHash) { return this._provider.getTransactionReceipt(txHash); } /** * Create a contract instance * @param address The contract address * @param abi The contract ABI * @returns A Contract instance */ createContract(address, abi) { return new ethers.Contract(address, abi, this._provider); } /** * Create a contract instance with a signer * @param address The contract address * @param abi The contract ABI * @param wallet The wallet to use as a signer * @returns A Contract instance with a signer */ createContractWithSigner(address, abi, wallet) { return new ethers.Contract(address, abi, wallet.connect(this._provider)); } /** * Estimate gas for a transaction * @param transaction The transaction to estimate gas for * @returns A promise that resolves to the gas estimate */ async estimateGas(transaction) { return this._provider.estimateGas(transaction); } /** * Get the current gas price * @returns A promise that resolves to the current gas price */ async getGasPrice() { return this._provider.getGasPrice(); } } export default BlockchainService;