zetrix_sdk_invoke_contract
Execute smart contract functions that modify blockchain state by sending transactions with required parameters including private key for signing.
Instructions
Invoke a smart contract function with state change (requires private key)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sourceAddress | Yes | The account address initiating the transaction | |
| privateKey | Yes | Private key for signing the transaction | |
| contractAddress | Yes | The smart contract address to invoke | |
| amount | Yes | Amount of ZTX to send with invocation (in micro-ZTX) | |
| input | Yes | JSON string with method and params | |
| metadata | No | Optional transaction description |
Implementation Reference
- src/zetrix-sdk.ts:161-240 (handler)Core handler logic for invoking smart contracts using the official zetrix-sdk-nodejs library. Creates invoke operation, evaluates fees, builds transaction blob, signs with private key, and submits to blockchain.async invokeContract(params: ZetrixContractInvokeParams): Promise<any> { await this.initSDK(); try { // Create contract invoke operation const operation = this.sdk.operation.contractInvokeByGasOperation({ contractAddress: params.contractAddress, amount: params.amount, input: params.input, metadata: params.metadata, }); if (operation.errorCode !== 0) { throw new Error( operation.errorDesc || `SDK Error: ${operation.errorCode}` ); } // Build and sign transaction const feeData = await this.sdk.transaction.evaluateFee({ sourceAddress: params.sourceAddress, nonce: "auto", // SDK will fetch nonce automatically operations: [operation.result.operation], signtureNumber: "1", metadata: params.metadata, }); if (feeData.errorCode !== 0) { throw new Error( feeData.errorDesc || `SDK Error: ${feeData.errorCode}` ); } // Submit transaction const txParams = { sourceAddress: params.sourceAddress, nonce: feeData.result.nonce, operations: [operation.result.operation], gasPrice: feeData.result.gasPrice, feeLimit: feeData.result.feeLimit, metadata: params.metadata, }; const blob = await this.sdk.transaction.buildBlob(txParams); if (blob.errorCode !== 0) { throw new Error(blob.errorDesc || `SDK Error: ${blob.errorCode}`); } // Sign the transaction const signature = this.sdk.transaction.sign({ privateKeys: [params.privateKey], blob: blob.result.transactionBlob, }); if (signature.errorCode !== 0) { throw new Error( signature.errorDesc || `SDK Error: ${signature.errorCode}` ); } // Submit to blockchain const submitResult = await this.sdk.transaction.submit({ blob: blob.result.transactionBlob, signature: signature.result.signatures, }); if (submitResult.errorCode !== 0) { throw new Error( submitResult.errorDesc || `SDK Error: ${submitResult.errorCode}` ); } return submitResult.result; } catch (error) { throw new Error( `Failed to invoke contract: ${error instanceof Error ? error.message : String(error)}` ); } }
- src/index.ts:492-525 (registration)Tool registration in the MCP tools list, including name, description, and input schema.{ name: "zetrix_sdk_invoke_contract", description: "Invoke a smart contract function with state change (requires private key)", inputSchema: { type: "object", properties: { sourceAddress: { type: "string", description: "The account address initiating the transaction", }, privateKey: { type: "string", description: "Private key for signing the transaction", }, contractAddress: { type: "string", description: "The smart contract address to invoke", }, amount: { type: "string", description: "Amount of ZTX to send with invocation (in micro-ZTX)", }, input: { type: "string", description: "JSON string with method and params", }, metadata: { type: "string", description: "Optional transaction description", }, }, required: ["sourceAddress", "privateKey", "contractAddress", "amount", "input"], }, },
- src/index.ts:1216-1236 (handler)MCP server switch case handler that parses tool arguments and delegates to ZetrixSDK.invokeContract method.case "zetrix_sdk_invoke_contract": { if (!args) { throw new Error("Missing arguments"); } const result = await zetrixSDK.invokeContract({ sourceAddress: args.sourceAddress as string, privateKey: args.privateKey as string, contractAddress: args.contractAddress as string, amount: args.amount as string, input: args.input as string, metadata: args.metadata as string | undefined, }); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; }