3

When calling sendSignedTransaction I got:

VM Exception while processing transaction: stack underflow

Used code is below, using TESTRPC:

const serializeTx = async (contractInstance, publicKey, privateKey, funcData) => {
    let privateKeyBuff = new Buffer(privateKey, 'hex');

    const gasLimit = 500000;

    const nonceNumber = await web3.eth.getTransactionCount(publicKey);
    const nonce = web3.utils.toHex(nonceNumber);
    const gasPrice = web3.utils.toHex(web3.eth.gasPrice);
    const gasLimitHex = web3.utils.toHex(gasLimit);
    const rawTx = {
        'nonce': nonce,
        'gasPrice': gasPrice,
        'gasLimit': gasLimitHex,
        'from': publicKey,
        'to': contractInstance.address,
        'data': funcData
    };

    let tx = new EthereumTx(rawTx);
    tx.sign(privateKeyBuff);

    return '0x' + tx.serialize().toString('hex');
};

    let funcData = MyContract.methods.reserve(param).encodeABI();

        const serializedTx = await serializeTx(
            MyContract,
            body.callerPublicKey,
            body.callerPrivateKey,
            funcData,
        );

        let result = await web3.eth.sendSignedTransaction(serializedTx);

Same contract function is working nice in Remix, also working nice when called using truffle-contract. Now when I try to sign the transaction data it throws Error: Returned error: VM Exception while processing transaction: stack underflow

EDIT: The problem was using contractInstance.address it should be contractInstance._address

  • It seems you are using web3 v1.0-beta, from their example you do not need to prefix the serialized transaction with '0x' web3js.readthedocs.io/en/1.0/web3-eth.html#id67. – Ismael Oct 19 '17 at 6:48
  • 2
    Just found out, the problem was in using contractInstance.address. In current version the address is under contractInstance._address. I will edit the question with the fix. – Kristian Oct 19 '17 at 6:59
0

I had this problem recently, and from what I could gather (your comment, this comment in an issue in ganache-cli and this other), VM Exception while processing transaction: stack underflow happens when the to: value is undefined or incorrect when trying to sign a transaction.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.