0

I already read the other topics with the same error, but that didn't solve my problem ...

When I try to communicate with test rpc between my web interface with my smart contract I have always error "vm exception while processing transaction : out of gas", whereas when I have only 4 input in my smart contract there are no problem.

I have already try to use that to initiate tesrpc (because only testrpc that didn't work):

testrpc --account="0x7231a774a538fce22a329729b03087de4cb4a1119494db1c10eae3bb491823e7, 1000000000000000000000"

testrpc -l 4500000000000

testrpc --gasPrice 20000

testrpc -g 1000000000000000

No matter what I use the gas use stays at 90000 and I think the error is related to that ... Thanks to the person who will help me !

Here is my contract :

pragma solidity ^0.4.18;
contract URP {

    uint8 x1; 
    uint8 x2;
    uint8 y1;
    uint8 y2;
    uint8 z1;
    uint8 z2;
    uint8 retailer; 
    uint8 value; 
    uint8 x; 
    uint8 y; 
    uint8 z;
    uint8 shopper;

function SendCoin(uint8 _x1, uint8 _x2, uint8 _y1, uint8 _y2, uint8 _z1, uint8 _z2, uint8 _ret, uint8 _value, uint8 _x, uint8 _y, uint8 _z, uint8 _shop) public {
       x1 = _x1;
       x2 = _x2;
       y1 = _y1;
       y2 = _y2;
       z1 = _z1;
       z2 = _z2;
       retailer = _retailer;
       value = _value;
       x = _x;
       y = _y;
       z =_z;
       shopper = _shopper;
   }

   function getSendCoin() public constant returns (uint8, uint8, uint8, uint8, uint8, uint8, uint8, uint8, uint8, uint8, uint8, uint8) {
       return (x1, x2, y1, y2, z1, z2, retailer, value, x, y, z, shopper);
   }  
}

And here's is my web interface :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <link rel="stylesheet" type="text/css" href="main.css">

    <script src="./node_modules/web3/dist/web3.min.js"></script>

</head>
<body>
    <div class="container">

        <h1>SendCoin</h1>

        <h2 id="sendcoin"></h2>

        <label for="name" class="col-lg-2 control-label">x1</label>
        <input id="x1" type="text">

        <label for="name" class="col-lg-2 control-label">x2</label>
        <input id="x2" type="text">

        <label for="name" class="col-lg-2 control-label">y1</label>
        <input id="y1" type="text">

        <label for="name" class="col-lg-2 control-label">y2</label>
        <input id="y2" type="text">

        <label for="name" class="col-lg-2 control-label">z1</label>
        <input id="z1" type="text">

        <label for="name" class="col-lg-2 control-label">z2</label>
        <input id="z2" type="text">

        <label for="name" class="col-lg-2 control-label">retailer</label>
        <input id="retailer" type="text">

        <label for="name" class="col-lg-2 control-label">value</label>
        <input id="value" type="text">

        <label for="name" class="col-lg-2 control-label">x</label>
        <input id="x" type="text">

        <label for="name" class="col-lg-2 control-label">y</label>
        <input id="y" type="text">

        <label for="name" class="col-lg-2 control-label">z</label>
        <input id="z" type="text">

        <label for="name" class="col-lg-2 control-label">shopper</label>
        <input id="shopper" type="text">

        <button id="button">Push Data</button>


    </div>

    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

    <script>
        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            // set the provider you want from Web3.providers
            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
        }


        web3.eth.defaultAccount = web3.eth.accounts[0];

        var URPContract = web3.eth.contract([
    {
        "constant": true,
        "inputs": [],
        "name": "getSendCoin",
        "outputs": [
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            },
            {
                "name": "",
                "type": "uint8"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_x1",
                "type": "uint8"
            },
            {
                "name": "_x2",
                "type": "uint8"
            },
            {
                "name": "_y1",
                "type": "uint8"
            },
            {
                "name": "_y2",
                "type": "uint8"
            },
            {
                "name": "_z1",
                "type": "uint8"
            },
            {
                "name": "_z2",
                "type": "uint8"
            },
            {
                "name": "_retailer",
                "type": "uint8"
            },
            {
                "name": "_value",
                "type": "uint8"
            },
            {
                "name": "_x",
                "type": "uint8"
            },
            {
                "name": "_y",
                "type": "uint8"
            },
            {
                "name": "_z",
                "type": "uint8"
            },
            {
                "name": "_shopper",
                "type": "uint8"
            }
        ],
        "name": "SendCoin",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    }]);

            web3.eth.accounts.forEach (account => { 
            balance = web3.eth.getBalance (account); 
            console.log (balance); 
            })
        var URP = URPContract.at('0x538882ec49974f8815fee55ad7b40d6dd4b6b75d');
        console.log(URP);
        URP.getSendCoin(function(error, result){
            if(!error)
                {
                    $("#sendcoin").html(result[0]+' = x1 / x2 = '+result[1]+' / y1 = '+result[2]+' / y2 = '+result[3]+' / z1 = ' +result[4]+' / z2 = ' +result[5]+' / ret = '+result[6]+' / value = ' +result[7]+' / x = '+result[8]+' / y = '+result[9]+' / z = '+result[10]+' / shop = '+result[11] );
                    console.log(result);
                }
            else
                console.error(error);
        });

        $("#button").click(function() {
            URP.SendCoin($("#x1").val(), $("#x2").val(), $("#y1").val(), $("#y2").val(), $("#z1").val(), $("#z2").val(), $("#retailer").val(), $("#value").val(), $("#x").val(), $("#y").val(), $("#z").val(), $("#shopper").val());
        });
    </script>

</body>
</html>

Sorry my ABI is too long ...

bumped to the homepage by Community 18 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • Security alert: Anyone can call function SendCoin!!! – goodvibration May 7 '18 at 11:18
  • Yes of course, for the moment is only to test the interaction between Smart Contract and Web interface – yohan May 7 '18 at 12:13
  • try testrpc --gasLimit=0xfffffffffff --account=0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef,1000000000000000000000000000000000000000 (and make sure that you replicate --account=... with a unique key according to however many accounts that you use in your tests). – goodvibration May 8 '18 at 10:24
  • I tried but I have again out of gas .. :( – yohan May 9 '18 at 8:39
0

try testrpc --gas-limit 4600000.

keep in mind testrpc has been changed to ganache-cli. I run ganache-cli --gas-limit 4600000

also, make sure your truffle.js is configured. you can change the gas price and gas price.

networks: {
    development: {
        host: "localhost",
        port: 8545,
        network_id: "0",
        gas: 10000000,
        gasLimit: 10000000
    },
 }
  • Thanks for your answer. Even if I run testrpc --gas-limit 4600000 or ganache-cli --gas-limit 4600000 I have always the same error .. The error is : Transaction : 0x... Gas usage : 90000 //he stay always at 90000 whatever I run Block Time : Tue May 08 ... Runtime Error : out of gas Isn't possible for me to use "networks: { development: { host: "localhost", port: 8545, network_id: "0", gas: 10000000, gasLimit: 10000000 }, }" because I don't use truffle .. I use remix – yohan May 8 '18 at 7:39
  • if you're using remix, make sure "enable optimization" is checked under the settings tab - this helps me with gas errors – noot May 8 '18 at 14:24
  • Now "enable optimization" is check but they are always the same error :( – yohan May 9 '18 at 8:38
0

SendCoin method will consume 102912 units. I'll suggest you to use estimateGas() function. It will return how much gas consume for a given inputs.

Refer below modified code:

    $("#button").click(function() {
        var _gas = URP.SendCoin.estimateGas($("#x1").val(), $("#x2").val(), $("#y1").val(), $("#y2").val(), $("#z1").val(), $("#z2").val(), $("#retailer").val(), $("#value").val(), $("#x").val(), $("#y").val(), $("#z").val(), $("#shopper").val());
        _gas = Math.floor(gas*1.2); //For safe side.
        URP.SendCoin($("#x1").val(), $("#x2").val(), $("#y1").val(), $("#y2").val(), $("#z1").val(), $("#z2").val(), $("#retailer").val(), $("#value").val(), $("#x").val(), $("#y").val(), $("#z").val(), $("#shopper").val(), {from:'your account', gas:_gas});
    });

I'll suggest you to use async method's. MetaMask will not execute sync methods.

  • Thanks for your answer. Yes the gas is not enought, but I don't why I change the "gas usage" on testrpc .. Or maybe it's on remix ? – yohan May 8 '18 at 7:40
  • And when I add "{gas:_gas}" on the SendCoin function I have an error. I think it's because there aren't of argument planned for that no ? – yohan May 8 '18 at 7:48
  • Pass your account account id ({from:'your account', gas:_gas}). Updated my ans. – Jitendra Kumar. Balla May 8 '18 at 9:06
  • Okay but where I put that in my code ? (Sorry I'm novice...) – yohan May 8 '18 at 11:04
  • URP.SendCoin($("#x1").val(), $("#x2").val(), $("#y1").val(), $("#y2").val(), $("#z1").val(), $("#z2").val(), $("#retailer").val(), $("#value").val(), $("#x").val(), $("#y").val(), $("#z").val(), $("#shopper").val(), {from:'your account', gas:_gas}); – Jitendra Kumar. Balla May 8 '18 at 11:05

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.