1

I'm building a smart contract and dapp using ganache and web3.js. In my smart contract I have a function (named "add_pilot") which gets 4 parameters and add them to a mapping. It looks like this:

pragma solidity ^0.4.0;  
contract Transaction {

    uint num = 0;
    bytes32 [] storedKeys;
    uint [] x;

    struct Pilot {
        string details;
        uint expected_time;
        address creator; //loc 0 is original creator
        uint amount;
        address [] listenrs;
        address [] reporters;
    }

    mapping (bytes32 => Pilot) _accounts;


    function  get_details_by_name(string key) public returns(string){
        return _accounts[stringToBytes32(key)].details;
    }

    function  get_pilots_names() public constant returns(bytes32 []){
        return storedKeys;
    }

    function stringToBytes32(string memory source) private returns (bytes32 result) {
        bytes memory tempEmptyStringTest = bytes(source);
        if (tempEmptyStringTest.length == 0) {
            return 0x0;
        }

        assembly {
            result := mload(add(source, 32))
        }
    }
    function  get1() public constant returns(uint){
        return num;
    }

    //new pilot as listner
    function add_pilot(string s1, string det, uint hours_till_exp, uint am) public  {
        bytes32 s = stringToBytes32(s1);
        _accounts[s].creator = msg.sender;
        _accounts[s].amount = am;
        _accounts[s].details = det;
        _accounts[s].expected_time  = now + (hours_till_exp * 3600);
        _accounts[s].listenrs.push(msg.sender);
        num+=1;
        storedKeys.push(s);
    }
}

When calling "add_pilot" from web3.js I'm getting the following error:

> Uncaught Error: Exceeds block gas limit
    at Object.InvalidResponse (web3.min.js:1)
    at n.send (web3.min.js:1)
    at l.t [as sendTransaction] (web3.min.js:1)
    at u.sendTransaction (web3.min.js:1)
    at u.execute (web3.min.js:1)
    at HTMLButtonElement.<anonymous> (index.html:151)
    at HTMLButtonElement.dispatch (jquery-3.2.1.slim.min.js:3)
    at HTMLButtonElement.q.handle (jquery-3.2.1.slim.min.js:3)
InvalidResponse @ web3.min.js:1
n.send @ web3.min.js:1
t @ web3.min.js:1
u.sendTransaction @ web3.min.js:1
u.execute @ web3.min.js:1
(anonymous) @ index.html:151
dispatch @ jquery-3.2.1.slim.min.js:3
q.handle @ jquery-3.2.1.slim.min.js:3

When I tried to increase the gas limit in the ganache, it still failed and I got this error:

> web3.min.js:1 Uncaught Error: sender doesn't have enough funds to send tx. The upfront cost is: 4000000000000000000000 and the
sender's account only has: 99989875220000000000
    at Object.InvalidResponse (web3.min.js:1)
    at n.send (web3.min.js:1)
    at l.t [as sendTransaction] (web3.min.js:1)
    at u.sendTransaction (web3.min.js:1)
    at u.execute (web3.min.js:1)
    at HTMLButtonElement.<anonymous> (index.html:151)
    at HTMLButtonElement.dispatch (jquery-3.2.1.slim.min.js:3)
    at HTMLButtonElement.q.handle (jquery-3.2.1.slim.min.js:3)

This is how I'm calling the function from web3.js:

var Coursetro = CoursetroContract.at('...');
Coursetro.add_pilot(($("#pilot_id").val()), $("#pilot_descp").val(), $("#hours_till").val(), $("#amount").val());

Another thing I haven't mentioned is that from remix it's executing the function properly. This problem appears only from the web3.js (the UI).

What am I doing wrong?

  • I don't really investigate but it tells you that the cost of this tx is 4000 ETH ! You have only 99.98987522 ETH on your account. This not a normal amount for a tx fee... – Nicolas Massart Mar 8 at 13:24
  • do you have any idea why it is cost so much? the contract is pretty simple. – todady Mar 9 at 19:15
0

Well, it's failing because your cost would be 4000 ETH and you have only 99. Why does it cost 4000 ETH to execute? What are your gas price and limit settings when you do the transaction? What are you trying to store in your contract, how big is the data?

  • I no idea why it cost 4000 ETH to execute. gas price and limit settings in the ganach are, GAS LIMIT: 20000000000000, and GAS PRICE: 20000000000. i'm trying to store in my contract just a map between string to pilot (where pilot is a simple struct as you can see in the code). the data is not big just 2 string and few addresses. One thing that might cause this cost is the "stringToBytes32" function which I needed because I have to return array of strings from the function (which is not possible) so I used this function every time. do you think it can be the cause to this cost? – todady Mar 8 at 13:43
  • Are you able to provide your raw transaction? Or the one you produce before signing or the signed one – waytosay Mar 11 at 9:00

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.