1

I'm getting a strange issue where if I send ether to a payable function, none of the events fire in that function, but I'm getting no error value in the Web3 callback. I have this function:

function makeDatum(string dataStr)  payable returns (bool) {
    debug("datum");
    data[msg.sender].payment += msg.value;
}

The debug is an event I've made that sends whatever you give it to the listener: event debug(string data);

Here is my Datum struct:
    struct Datum {
        bool exists;
        bytes32 name;
        uint payment;
    }

Now, if I exclude this line:

data[msg.sender] += msg.value;

The debug event fires properly. Also, if I send zero Wei, it works fine and debug fires. Only when I send a value of Wei greater than zero do I not get the debug event.

Here is how I'm sending Wei:

contract.methods.makeDatum("test").estimateGas({from: defaultAccount}, function(error, gas) {
    console.log("Gas estimate:" + gas);
    contract.methods.makeDatum("test").send({from: defaultAccount, gas:gas, value: 50000}, function(error, result) {
        console.log(result);
        console.log("error:" + JSON.stringify(error));
    });
});

The error parameter here is returning NULL, so solidity doesn't seem to be throwing any errors...it's just failing silently. I'm using Solidity compiler 0.4.13 with Truffle.

  • I'm inferring that you have a variable in your contract, like mapping (address=>Datum) data. Can you confirm? I'm not having any trouble getting your contract to emit events with a payment in Remix. What happens when you try in Remix? – carver Aug 17 '17 at 20:22
  • 1
    Hi, Thanks for the efforts in helping me debug. That was the confusing part for me: in Remix, it was running fine. The accepted answer pointed out that I wasn't providing the value along with the gas estimate and that's why it was failing; that turned out to be true, and it's running fine now. – Munawar Aug 22 '17 at 9:52
1

The reason of the unknown error is a wrong gas estimation when you send the transaction because the estimateGas function must include exactly the same parameters (arguments and value) as the transaction function, this has an impact on the gas estimation.

So you only have to pass value: 50000to the estimateGas function:

contract.methods.makeDatum("test").estimateGas({from: defaultAccount, value: 50000}, function(error, gas) {
    console.log("Gas estimate:" + gas);
    contract.methods.makeDatum("test").send({from: defaultAccount, gas:gas, value: 50000}, function(error, result) {
        console.log(result);
        console.log("error:" + JSON.stringify(error));
    });
});

I made a similar project using Truffle Framework (here) and the output is clear:

Without value

Error: VM Exception while processing transaction: out of gas

With value

{ tx: '0xfd7d3f0ca4cab4b8353dd41d19242e199308972b256ea6a742b1f07767df21bf',
  receipt:
   { transactionHash: '0xfd7d3f0ca4cab4b8353dd41d19242e199308972b256ea6a742b1f07767df21bf',
     transactionIndex: 0,
     blockHash: '0xe4cdc1271791a1bc3f8390e56c62ab596d511fa3f6ccd812b87409511b58857e',
     blockNumber: 32,
     gasUsed: 44202,
     cumulativeGasUsed: 44202,
     contractAddress: null,
     logs: [ [Object] ] },
  logs:
   [ { logIndex: 0,
       transactionIndex: 0,
       transactionHash: '0xfd7d3f0ca4cab4b8353dd41d19242e199308972b256ea6a742b1f07767df21bf',
       blockHash: '0xe4cdc1271791a1bc3f8390e56c62ab596d511fa3f6ccd812b87409511b58857e',
       blockNumber: 32,
       address: '0xe973ea06c0caa83807de42ab870b6102785c9a72',
       type: 'mined',
       event: 'debug',
       args: [Object] } ] }
  • Thanks for this response; it solved my issue! I didn't realize you had to send the value with the gas estimate. – Munawar Aug 18 '17 at 7:44

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.