Skip to main content
Glama
contracts.ts1.95 kB
import { type Hash, type Hex, type ReadContractParameters, type GetLogsParameters, type Log } from 'viem'; import { getPublicClient, getWalletClient } from './clients.js'; import * as services from "./index.js"; import { getPrivateKeyAsHex } from '../config.js'; import {DEFAULT_NETWORK} from "../chains.js"; /** * Read from a contract for a specific network */ export async function readContract(params: ReadContractParameters, network = DEFAULT_NETWORK) { const client = getPublicClient(network); return await client.readContract(params); } /** * Write to a contract for a specific network * @param params Contract parameters * @param network Network name or chain ID * @returns Transaction hash * @throws Error if no private key is available */ export async function writeContract( params: Record<string, any>, network = DEFAULT_NETWORK ): Promise<Hash> { // Get private key from environment const key = getPrivateKeyAsHex(); if (!key) { throw new Error('Private key not available. Set the PRIVATE_KEY environment variable and restart the MCP server.'); } const client = getWalletClient(key, network); return await client.writeContract(params as any); } /** * Get logs for a specific network */ export async function getLogs(params: GetLogsParameters, network = DEFAULT_NETWORK): Promise<Log[]> { const client = getPublicClient(network); return await client.getLogs(params); } /** * Check if an address is a contract * @param address Address * @param network Network name or chain ID * @returns True if the address is a contract, false if it's an EOA */ export async function isContract(address: string, network = DEFAULT_NETWORK): Promise<boolean> { const validatedAddress = services.helpers.validateAddress(address); const client = getPublicClient(network); const code = await client.getBytecode({ address: validatedAddress }); return code !== undefined && code !== '0x'; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/testinguser1111111/sei-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server