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?