简体   繁体   中英

Sign and broadcast smart contract transaction in TRON network

Using a node.js client, I'm trying to invoke a smart contract function by:

  1. Creating a transaction
  2. Signing it
  3. Broadcast the signed transaction

I deployed a simple smart contract:

pragma solidity ^0.4.25;

contract Test {

    event MyEvent(address sender, string message, uint sessionId, uint value);
    event TestEvent(address sender);

    constructor() public {
    }


    function testFunction(string message, uint sessionId)
    public
    payable {
        emit MyEvent(msg.sender, message, sessionId, msg.value);
    }
    
    function test2()
    public {
        emit TestEvent(msg.sender);
    }
}

I'm having success invoking test2 (when sending an empty parameters array) but failing to invoke testFunction which takes parameters. I've tried using tronweb triggerSmartContract call :

async function triggerSmartContract() {

    try {

        const options = {
            feeLimit: 1000000000,
            callValue: 50
        };

        const parameters = [{type: 'string', value: 'test-round-id-1'}, {type: 'uint', value: 12345}];
        const issuerAddress = tronWeb.defaultAddress.base58;
        const functionSelector = 'testFunction(string, uint)';

        let transactionObject = await tronWeb.transactionBuilder.triggerSmartContract (
            contract_address,
            functionSelector,
            options,
            parameters,
            tronWeb.address.toHex(issuerAddress)
        );

        if (!transactionObject.result || !transactionObject.result.result)
            return console.error('Unknown error: ' + txJson, null, 2);

        // Signing the transaction
        const signedTransaction = await tronWeb.trx.sign(transactionObject.transaction);

        if (!signedTransaction.signature) {
            return console.error('Transaction was not signed properly');
        }

        // Broadcasting the transaction
        const broadcast = await tronWeb.trx.sendRawTransaction(signedTransaction);
        console.log(`broadcast: ${broadcast}`);
    } catch (e) {
        return console.error(e);
    }
}

The code runs and creates a transaction on the blockchain but with result of FAIL-REVERT OPCODE EXECUTED . The wallet of the address and key that are defined in tronweb object has enough funds in it (it's not out of gas/not enough funds matter). Also the function in the smart contract is defined as payable.

Since I can invoke successfully any function with no parameters I tend to think the problem relates to the way I construct the parameters array. I built the parameters array this way (pairs of type and value ) after going over the source code of the transactionBuilder (line 833) .

Any suggestions?

Had exactly the same issue. Solution:

  1. Remove spaces in const functionSelector = 'testFunction(string,uint)'; as malysh advised
  2. Change uint to uint256

try writing function parameters without spaces. const functionSelector = 'testFunction(string,uint)'; I read in the documentation that you have to write without spaces, it worked for me on python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM