bnbchain-mcp
Version:
---
186 lines (185 loc) • 6.1 kB
JavaScript
import { z } from "zod";
import { parseUnits, toEventSelector, decodeEventLog, } from "viem";
import { account, client } from "../config.js";
const createTokenABI = [
{
"inputs": [
{
"internalType": "string",
"name": "name",
"type": "string"
},
{
"internalType": "string",
"name": "symbol",
"type": "string"
},
{
"internalType": "uint256",
"name": "totalSupply",
"type": "uint256"
}
],
"name": "createToken",
"outputs": [
{
"internalType": "address",
"name": "token",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "creater",
"type": "address"
},
{
"indexed": false,
"internalType": "address",
"name": "token",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "createrNonce",
"type": "uint256"
}
],
"name": "CreateTokenEvent",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "createrNonce",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
];
export function registerCreateBEP20Token(server) {
server.tool("createBEP20Token", "create bep20 token", {
name: z.string(),
symbol: z.string(),
totalSupply: z.string(),
}, async ({ name, symbol, totalSupply }) => {
try {
const contract = "0xad9e6346E87Dfb4c08a47CBDFDF715A700C03918";
const hash = await client.writeContract({
account,
address: contract,
abi: createTokenABI,
functionName: 'createToken',
args: [name, symbol, parseUnits(totalSupply, 18)],
});
const transaction = await client.waitForTransactionReceipt({
hash: hash,
retryCount: 300,
retryDelay: 100,
});
if (transaction.status != "success") {
console.log("Transaction failed", transaction);
throw new Error("Transaction failed");
}
const targetTopic = toEventSelector({
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "creater",
"type": "address"
},
{
"indexed": false,
"internalType": "address",
"name": "token",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "createrNonce",
"type": "uint256"
}
],
"name": "CreateTokenEvent",
"type": "event"
});
const logData = transaction.logs.find((log) => log.topics.includes(targetTopic));
if (!logData) {
throw new Error("Log not found");
}
const decodedLog = decodeEventLog({
abi: [{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "creater",
"type": "address"
},
{
"indexed": false,
"internalType": "address",
"name": "token",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "createrNonce",
"type": "uint256"
}
],
"name": "CreateTokenEvent",
"type": "event"
}],
data: logData.data,
topics: logData.topics,
});
return {
content: [
{
type: "text",
text: `Create token successfully. https://bscscan.com/tx/${hash}, Token Address: ${decodedLog.args.token}`,
url: `https://bscscan.com/tx/${hash}`,
},
],
};
}
catch (error) {
console.error("Native token transfer failed:", error);
const errorMessage = error instanceof Error ? error.message : String(error);
return {
content: [
{
type: "text",
text: `Transaction failed: ${errorMessage}`,
},
],
isError: true,
};
}
});
}