Search code examples
ethereumsolidity

my smart contract cause error : out of gas error


error message

i have some problem in my solidity code. i think, the problem cause on the account.transfer

i'm using ganache, remix, metamask, web3

this is my solidity code.

`pragma solidity ^0.4.25;
contract test{
    address fire_account=0x6F3c5e42c340736eEa9a1C362592Bef9Ba2E5561;
    mapping(string => uint) fire_record;
    function fireDonation(string contributorName) payable{
        fire_account.transfer(msg.value);
        fire_balance+=msg.value;
        fire_contributor.push(contributorName);
    }
}`

and web3.js code

var Courses;
async function init(){
    web3 = await new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
    web3.eth.defaultAccount = web3.eth.accounts[2];
    console.log("Default account is ",web3.eth.accounts[2]);
    //Change the ABI here
    var CoursesContract = web3.eth.contract(/*skip*/);
    //Replace Deployed Smartcontract Address here
    var contractaddress = "0x9b95f972feaad42f2023246112f450c56d8921ae";
    Courses = CoursesContract.at(contractaddress);
}
init();
function fireDonate(){
    var amount=parseInt(document.getElementById('amount').value);
    console.log(amount);
    Courses.fireDonation(document.getElementById('contributor').value, {from: web3.eth.accounts[2], value: amount});
}

can u tell me solution or link? i can't find links..


Solution

  • It appears that your smart contract requires more gas (fees) to execute than the default amount. You have to increase the gasLimit and gasPrice from their default values. You can specify them in hex like this.

    Courses.fireDonation(document.getElementById('contributor').value, {from: web3.eth.accounts[2], value: amount, gasLimit: '0x27100', gasPrice: '0x09184e72a00'});
    

    You might have to vary the exact values to see what works for your use case.