import { ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
import { getNetwork } from "../shared/account.js";
import { ethers, getDefaultProvider, N, Wallet } from "@coti-io/coti-ethers";
import { ERC20_ABI } from "../constants/abis.js";
import { z } from "zod";
export const DEPLOY_PRIVATE_ERC20_CONTRACT: ToolAnnotations = {
title: "Deploy Private ERC20 Contract",
name: "deploy_private_erc20_contract",
description:
"Deploy a new standard private ERC20 token contract on the COTI blockchain. " +
"This creates a new private token with the specified name, symbol, and decimals. " +
"Returns the deployed contract address upon successful deployment.",
inputSchema: {
private_key: z.string().describe("Private key of the account (tracked by AI from previous operations)"),
aes_key: z.string().optional().describe("AES key for private transactions (tracked by AI). Required for private operations."),
network: z.enum(['testnet', 'mainnet']).describe("Network to use: 'testnet' or 'mainnet' (required)."),
name: z.string().describe("Name of the token"),
symbol: z.string().describe("Symbol of the token (typically 3-5 characters)"),
decimals: z.number().describe("Number of decimals for the token (0-6)"),
gas_limit: z.string().optional().describe("Optional gas limit for the deployment transaction"),
},
};
/**
* Validates the arguments for deploying a private ERC20 contract
* @param args The arguments to validate
* @returns True if the arguments are valid, false otherwise
*/
export function isDeployPrivateERC20ContractArgs(args: unknown): args is { private_key: string, name: string, symbol: string, decimals: number, gas_limit?: string, network: 'testnet' | 'mainnet' } {
return (
typeof args === "object" &&
args !== null &&
"name" in args &&
typeof (args as { name: string }).name === "string" &&
"symbol" in args &&
typeof (args as { symbol: string }).symbol === "string" &&
"decimals" in args &&
typeof (args as { decimals: number }).decimals === "number" &&
"private_key" in args &&
typeof (args as { private_key: string }).private_key === "string" &&
(!("gas_limit" in args) || typeof (args as { gas_limit: string }).gas_limit === "string")
);
}
/**
* Handler for the deployPrivateERC20Contract tool
* @param args The arguments for the tool
* @returns The tool response
*/
export async function deployPrivateERC20ContractHandler(args: any): Promise<any> {
if (!isDeployPrivateERC20ContractArgs(args)) {
throw new Error("Invalid arguments for deploy_private_erc20_contract");
}
const { name, symbol, decimals, gas_limit, network, private_key } = args;
const results = await performDeployPrivateERC20Contract( private_key, name, symbol, decimals, network, gas_limit);
return {
structuredContent: {
contractAddress: results.contractAddress,
transactionHash: results.transactionHash,
name: results.name,
symbol: results.symbol,
decimals: results.decimals,
gasLimit: results.gasLimit,
deployer: results.deployer,
abi: ERC20_ABI,
},
content: [{ type: "text", text: results.formattedText }],
isError: false,
};
}
/**
* Deploys a new private ERC20 contract on the COTI blockchain
* @param name The name of the token
* @param symbol The symbol of the token
* @param decimals The number of decimals for the token
* @param gas_limit Optional gas limit for the deployment transaction
* @returns An object with deployment details and formatted text
*/
export async function performDeployPrivateERC20Contract(private_key: string, name: string, symbol: string, decimals: number,network: 'testnet' | 'mainnet', gas_limit?: string, ): Promise<{
contractAddress: string,
transactionHash: string,
name: string,
symbol: string,
decimals: number,
gasLimit?: string,
deployer: string,
formattedText: string
}> {
try {
// Validate decimals parameter
if (decimals < 0 || decimals > 6 || !Number.isInteger(decimals)) {
throw new Error("Decimals must be an integer between 0 and 6");
}
const provider = getDefaultProvider(getNetwork(network));
const wallet = new Wallet(private_key, provider);
const bytecode = "0x60806040523480156200001157600080fd5b5060405162001bb838038062001bb8833981016040819052620000349162000148565b828260046200004483826200025c565b5060056200005382826200025c565b50506006805460ff909316680100000000000000000260ff60401b19909316929092179091555062000328915050565b634e487b7160e01b600052604160045260246000fd5b600082601f830112620000ab57600080fd5b81516001600160401b0380821115620000c857620000c862000083565b604051601f8301601f19908116603f01168101908282118183101715620000f357620000f362000083565b816040528381526020925086838588010111156200011057600080fd5b600091505b8382101562000134578582018301518183018401529082019062000115565b600093810190920192909252949350505050565b6000806000606084860312156200015e57600080fd5b83516001600160401b03808211156200017657600080fd5b620001848783880162000099565b945060208601519150808211156200019b57600080fd5b50620001aa8682870162000099565b925050604084015160ff81168114620001c257600080fd5b809150509250925092565b600181811c90821680620001e257607f821691505b6020821081036200020357634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156200025757600081815260208120601f850160051c81016020861015620002325750805b601f850160051c820191505b8181101562000253578281556001016200023e565b5050505b505050565b81516001600160401b0381111562000278576200027862000083565b6200029081620002898454620001cd565b8462000209565b602080601f831160018114620002c85760008415620002af5750858301515b600019600386901b1c1916600185901b17855562000253565b600085815260208120601f198616915b82811015620002f957888601518255948401946001909101908401620002d8565b5085821015620003185787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b61188080620003386000396000f3fe608060405234801561001057600080fd5b50600436106101515760003560e01c8063313ce567116100cd57806396b2db3811610081578063a7d9ad6a11610066578063a7d9ad6a146102f2578063a9059cbb14610305578063dd62ed3e1461031857600080fd5b806396b2db38146102cc578063a42c0af9146102df57600080fd5b8063722713f7116100b2578063722713f7146102a95780638269bcc3146102b157806395d89b41146102c457600080fd5b8063313ce5671461025857806370a082311461027c57600080fd5b806313691c761161012457806323b872dd1161010957806323b872dd1461021f57806326a9b3f1146102325780632893c5b01461024557600080fd5b806313691c76146101f857806318160ddd1461020d57600080fd5b8063043d20851461015657806306fdde031461019f57806308a2032a146101b4578063095ea7b3146101d5575b600080fd5b6101826101643660046113be565b6001600160a01b039081166000908152602081905260409020541690565b6040516001600160a01b0390911681526020015b60405180910390f35b6101a761034d565b604051610196919061141f565b6101c76101c236600461144a565b6103df565b604051908152602001610196565b6101e86101e33660046114a8565b610416565b6040519015158152602001610196565b61020b6102063660046114d2565b610430565b005b60065467ffffffffffffffff166101c7565b6101c761022d366004611516565b61049f565b6101e8610240366004611552565b6104c1565b61020b6102533660046114d2565b61055b565b60065468010000000000000000900460ff1660405160ff9091168152602001610196565b6101c761028a3660046113be565b6001600160a01b03166000908152600160208190526040909120015490565b6101c761059e565b6101e86102bf3660046113be565b6105ae565b6101a7610625565b6101c76102da366004611552565b610634565b6101e86102ed366004611583565b61069d565b6101c7610300366004611583565b6106c5565b6101c76103133660046114a8565b6106e2565b61032b6103263660046115d1565b6106f8565b6040805182518152602080840151908201529181015190820152606001610196565b60606004805461035c90611604565b80601f016020809104026020016040519081016040528092919081815260200182805461038890611604565b80156103d55780601f106103aa576101008083540402835291602001916103d5565b820191906000526020600020905b8154815290600101906020018083116103b857829003601f168201915b5050505050905090565b600033816103f46103ef856116a8565b61076a565b90506104018683836107fa565b61040c8686836108cd565b9695505050505050565b600033610424818585610965565b60019150505b92915050565b60006104448361043f84610b0f565b610b63565b905061044f81610bbb565b1561049a576006805483919060009061047390849067ffffffffffffffff16611763565b92506101000a81548167ffffffffffffffff021916908367ffffffffffffffff1602179055505b505050565b6000336104ad8582856107fa565b6104b88585856108cd565b95945050505050565b6000806104cd33610c4c565b90508215610518573360009081526002602090815260408083206001600160a01b03881684529091529020805461050d9061050790610c75565b83610c92565b600190910155610424565b6001600160a01b03841660009081526002602090815260408083203384529091529020805461054a9061050790610c75565b600290910155600191505092915050565b600061056f8361056a84610b0f565b610d5b565b905061057a81610bbb565b1561049a576006805483919060009061047390849067ffffffffffffffff1661178b565b60006105a933610dac565b905090565b6000806105ba33610dac565b33600090815260208190526040902080547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b03861617905590506106068184610c92565b3360009081526001602081905260409091208101919091559392505050565b60606005805461035c90611604565b60008115610670573360009081526002602090815260408083206001600160a01b038716845290915290205461066990610c75565b905061042a565b6001600160a01b038316600090815260026020908152604080832033845290915290205461066990610c75565b600033816106ad6103ef856116a8565b90506106ba828683610965565b506001949350505050565b600033816106d56103ef856116a8565b90506104b88286836108cd565b6000336106f08185856108cd565b949350505050565b61071c60405180606001604052806000815260200160008152602001600081525090565b506001600160a01b0391821660009081526002602081815260408084209490951683529283529083902083516060810185528154815260018201549381019390935201549181019190915290565b805160208201516040517fe4f36e1000000000000000000000000000000000000000000000000000000000815260009260649263e4f36e10926107b792600160fa1b9290916004016117c2565b6020604051808303816000875af11580156107d6573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061042a9190611803565b6001600160a01b03831660009081526001602052604081205461081c90610c75565b6001600160a01b0380861660009081526002602090815260408083209388168352929052908120549192509061085190610c75565b9050600061086f8261086a67ffffffffffffffff610b0f565b610dce565b9050600061087d8486610e4e565b9050600061088b8487610e4e565b905060006108b56108a5856108a08686610e63565b610e63565b6108af878a610e77565b87610e8c565b90506108c2898983610965565b505050505050505050565b60006001600160a01b038416610917576040517f96c6fd1e000000000000000000000000000000000000000000000000000000008152600060048201526024015b60405180910390fd5b6001600160a01b03831661095a576040517fec442f050000000000000000000000000000000000000000000000000000000081526000600482015260240161090e565b6106f0848484610f51565b6001600160a01b0383166109a8576040517fe602df050000000000000000000000000000000000000000000000000000000081526000600482015260240161090e565b6001600160a01b0382166109eb576040517f94280d620000000000000000000000000000000000000000000000000000000081526000600482015260240161090e565b60006109f6826110af565b90506000610a0385610c4c565b90506000610a118483610c92565b9050610a1c85610c4c565b91506000610a2a8584610c92565b905060405180606001604052808581526020018381526020018281525060026000896001600160a01b03166001600160a01b031681526020019081526020016000206000886001600160a01b03166001600160a01b03168152602001908152602001600020600082015181600001556020820151816001015560408201518160020155905050856001600160a01b0316876001600160a01b03167fb3fd5071835887567a0671151121894ddccc2842f1d10bedad13e0d17cace9a78484604051610afe929190918252602082015260400190565b60405180910390a350505050505050565b6040517fd9b60b60000000000000000000000000000000000000000000000000000000008152600160fa1b600482015267ffffffffffffffff8216602482015260009060649063d9b60b60906044016107b7565b60006001600160a01b038316610ba8576040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526000600482015260240161090e565b610bb483600084610f51565b9392505050565b6040517f0cfed56100000000000000000000000000000000000000000000000000000000815260006004820181905260248201839052908190606490630cfed561906044016020604051808303816000875af1158015610c1f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c439190611803565b15159392505050565b6001600160a01b038082166000908152602081905260408120549091168061042a575090919050565b600081600003610c895761042a6000610b0f565b61042a82611109565b60408051606083901b7fffffffffffffffffffffffffffffffffffffffff0000000000000000000000001660208201528151601481830301815260348201928390527f3c6f0e6800000000000000000000000000000000000000000000000000000000909252600091606491633c6f0e6891610d1891600160fa1b9188916038016117c2565b6020604051808303816000875af1158015610d37573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610bb49190611803565b60006001600160a01b038316610da0576040517fec442f050000000000000000000000000000000000000000000000000000000081526000600482015260240161090e565b610bb460008484610f51565b6001600160a01b038116600090815260016020526040812054610bb481610c75565b60006064637c12a1eb610de360048085611119565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e084901b1681527fffffff000000000000000000000000000000000000000000000000000000000090911660048201526024810186905260448101859052606401610d18565b6000606463dd148693610de360048085611119565b6000606463fb7da35f610de3838080611119565b6000606463371d1bf2610de360048085611119565b600060646320cc408d610ea160048085611119565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e084901b1681527fffffff000000000000000000000000000000000000000000000000000000000090911660048201526024810187905260448101869052606481018590526084016020604051808303816000875af1158015610f2d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106f09190611803565b6000808281610f606001611176565b90506001600160a01b038716610fb7576000610f7d600354610c75565b9050610f8981876111d5565b9050610f94816110af565b6003556000610fa288610dac565b9050610fae81886111d5565b94505050610fff565b6000610fc288610dac565b90506000610fcf88610dac565b90506000610fde83838a6111ea565b90975094509050610fef8a826112a8565b610ff98683610e77565b94505050505b6001600160a01b03861661103a57600061101a600354610c75565b90506110268184610e77565b9050611031816110af565b60035550611044565b61104486846112a8565b856001600160a01b0316876001600160a01b03167f9ed053bb818ff08b8353cd46f78db1f0799f31c9e4458fdb425c10eccd2efc44611083858b610c92565b61108d868b610c92565b6040805192835260208301919091520160405180910390a39695505050505050565b6000606463c50c9c0260045b60f81b846040518363ffffffff1660e01b81526004016107b79291907fff00000000000000000000000000000000000000000000000000000000000000929092168252602082015260400190565b6000606463d2c135e560046110bb565b600081600281111561112d5761112d6117ac565b60ff166008846004811115611144576111446117ac565b61ffff16901b61ffff166010866004811115611162576111626117ac565b62ffffff16901b171760e81b949350505050565b60008082611185576000611188565b60015b6040517fd9b60b600000000000000000000000000000000000000000000000000000000081526000600482015260ff9190911660248201819052915060649063d9b60b6090604401610d18565b60006064638c5d0150610de360048085611119565b6000808080808060646356c72d2861120560048080866112ee565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e084901b81168252919091166004820152602481018c9052604481018b9052606481018a90526084016060604051808303816000875af1158015611272573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611296919061181c565b919b909a509098509650505050505050565b60006112b383610c4c565b90506112bf828261136e565b6001600160a01b0390931660009081526001602081815260409092208551815594909101519301929092555050565b6000816002811115611302576113026117ac565b60ff166008846004811115611319576113196117ac565b61ffff16901b61ffff166010866004811115611337576113376117ac565b62ffffff16901b62ffffff166018886004811115611357576113576117ac565b63ffffffff16901b17171760e01b95945050505050565b604080518082019091526000808252602082015261138b836110af565b81526113978383610c92565b602082015292915050565b80356001600160a01b03811681146113b957600080fd5b919050565b6000602082840312156113d057600080fd5b610bb4826113a2565b6000815180845260005b818110156113ff576020818501810151868301820152016113e3565b506000602082860101526020601f19601f83011685010191505092915050565b602081526000610bb460208301846113d9565b60006040828403121561144457600080fd5b50919050565b60008060006060848603121561145f57600080fd5b611468846113a2565b9250611476602085016113a2565b9150604084013567ffffffffffffffff81111561149257600080fd5b61149e86828701611432565b9150509250925092565b600080604083850312156114bb57600080fd5b6114c4836113a2565b946020939093013593505050565b600080604083850312156114e557600080fd5b6114ee836113a2565b9150602083013567ffffffffffffffff8116811461150b57600080fd5b809150509250929050565b60008060006060848603121561152b57600080fd5b611534846113a2565b9250611542602085016113a2565b9150604084013590509250925092565b6000806040838503121561156557600080fd5b61156e836113a2565b91506020830135801515811461150b57600080fd5b6000806040838503121561159657600080fd5b61159f836113a2565b9150602083013567ffffffffffffffff8111156115bb57600080fd5b6115c785828601611432565b9150509250929050565b600080604083850312156115e457600080fd5b6115ed836113a2565b91506115fb602084016113a2565b90509250929050565b600181811c9082168061161857607f821691505b60208210810361144457634e487b7160e01b600052602260045260246000fd5b634e487b7160e01b600052604160045260246000fd5b6040805190810167ffffffffffffffff8111828210171561167157611671611638565b60405290565b604051601f8201601f1916810167ffffffffffffffff811182821017156116a0576116a0611638565b604052919050565b6000604082360312156116ba57600080fd5b6116c261164e565b8235815260208084013567ffffffffffffffff808211156116e257600080fd5b9085019036601f8301126116f557600080fd5b81358181111561170757611707611638565b61171984601f19601f84011601611677565b9150808252368482850101111561172f57600080fd5b80848401858401376000908201840152918301919091525092915050565b634e487b7160e01b600052601160045260246000fd5b67ffffffffffffffff8281168282160390808211156117845761178461174d565b5092915050565b67ffffffffffffffff8181168382160190808211156117845761178461174d565b634e487b7160e01b600052602160045260246000fd5b7fff00000000000000000000000000000000000000000000000000000000000000841681528260208201526060604082015260006104b860608301846113d9565b60006020828403121561181557600080fd5b5051919050565b60008060006060848603121561183157600080fd5b835192506020840151915060408401519050925092509256fea264697066735822122007aea178290552905ddf932b42d02f6de6d74a9f890ad7ddadecb162072de77f64736f6c63430008130033"
const factory = new ethers.ContractFactory(
ERC20_ABI,
bytecode,
wallet
);
const txOptions: any = {};
if (gas_limit) {
txOptions.gasLimit = gas_limit;
}
const contract = await factory.deploy(
name,
symbol,
decimals,
txOptions
);
const receipt = await contract.deploymentTransaction();
const contractAddress = await contract.getAddress();
const formattedText = `Private ERC20 Contract Deployment Successful!\n\nName: ${name}\n\nSymbol: ${symbol}\n\nContract Address: ${contractAddress}\n\nTransaction Hash: ${receipt?.hash}`;
return {
contractAddress,
transactionHash: receipt?.hash || '',
name,
symbol,
decimals,
gasLimit: gas_limit,
deployer: wallet.address,
formattedText
};
} catch (error) {
console.error('Error deploying private ERC20 contract:', error);
throw new Error(`Failed to deploy private ERC20 contract: ${error instanceof Error ? error.message : String(error)}`);
}
}