bnbchain-mcp
Version:
---
60 lines (59 loc) • 1.98 kB
JavaScript
import { isAddress, } from "viem";
import { account, client } from "../config.js";
/**
* Call a contract's function with a given wallet and arguments.
*
* @param args - The arguments for the contract call.
* @param args.abi - The contract ABI as a JSON string.
* @param args.contractAddress - The contract address.
* @param args.functionName - The name of the function to call.
* @param args.functionArgs - The function arguments as a JSON string.
* @param args.value - The transaction value.
* @returns The result of the contract call or transaction hash.
*/
export async function callContractHandler(args) {
let abi;
try {
abi = JSON.parse(args.abi);
}
catch (error) {
throw new Error(`Invalid ABI: ${error}`);
}
if (!isAddress(args.contractAddress, { strict: false })) {
throw new Error(`Invalid contract address: ${args.contractAddress}`);
}
let functionAbi;
try {
functionAbi = abi.find((item) => "name" in item && item.name === args.functionName);
}
catch (error) {
throw new Error(`Invalid function name: ${args.functionName}`);
}
let functionArgs;
try {
functionArgs = JSON.parse(args.functionArgs);
}
catch (error) {
throw new Error(`Invalid function arguments: ${args.functionArgs}`);
}
if (functionAbi.stateMutability === "view" ||
functionAbi.stateMutability === "pure") {
const tx = await client.readContract({
address: args.contractAddress,
abi,
functionName: args.functionName,
args: functionArgs,
});
return String(tx);
}
const tx = await client.simulateContract({
account: account,
abi,
address: args.contractAddress,
functionName: args.functionName,
value: BigInt(args.value ?? 0),
args: functionArgs,
});
const txHash = await client.writeContract(tx.request);
return txHash;
}