[Ethereum] Error – Gas required exceeds allowance or always failing transaction

contract-designcontract-developmentremixsolidityweb3js

I'm just starting to program in solidity and deploy smart contracts in a local created blockchain.

I'm trying to deploy a simple smart contract using remix and web3 provider and this error is killing me all time.

Here is my code. I'm simply trying to parse a json file to save it as structs. If you know a better way to do it, it would be great! I'm now using a library from github that I think is usefull for my use case, but I need some help with the code.


pragma solidity ^0.4.21;

import 'github.com/chrisdotn/jsmnSol/contracts/JsmnSolLib.sol';

contract MusicalContract {

    struct parameters {
        string name;
        string mr_share;
        string pr_share;
        string sr_share;
        string country;
        string walletDir;
    }

    //event debugStr(string str);
    //event debugInt(uint i);

    parameters[] public params;

    string public json = '[{"name":"Martin","pr_share":"2","mr_share":"2","sr_share":"2","country":"2101","walletDir":"12345"},{"name":"Ahoj","share":"","walletDir":"2589","pr_share":"2","mr_share":"2","sr_share":"2","country":"36"},{"name":"Cus","share":"","walletDir":"12345","pr_share":"2","mr_share":"3","sr_share":"1","country":"2100"}]';

    /*
    function MusicalContract(string json) {

    }
    */

    function MusCo() public {
        uint returnValue;
        uint actualNum;
        JsmnSolLib.Token[] memory tokens;
        string memory jsonElement;
        JsmnSolLib.Token memory t;

        (returnValue, tokens, actualNum) = JsmnSolLib.parse(json, 200);

        for(uint ielement=0; ielement < actualNum-1; ielement++) {
            t = tokens[ielement];
            jsonElement = JsmnSolLib.getBytes(json, t.start, t.end);
            //emit debugStr(jsonElement);
            //emit debugInt(ielement);
            if(compareStrings(jsonElement, "name")) {
                t = tokens[ielement+1];
                params[ielement].name = JsmnSolLib.getBytes(json, t.start, t.end);
            } else if(compareStrings(jsonElement, "pr_share")) {
                t = tokens[ielement+1];
                params[ielement].pr_share = JsmnSolLib.getBytes(json, t.start, t.end);
            } else if(compareStrings(jsonElement, "mr_share")) {
                t = tokens[ielement+1];
                params[ielement].mr_share = JsmnSolLib.getBytes(json, t.start, t.end);
            } else if(compareStrings(jsonElement, "sr_share")) {
                t = tokens[ielement+1];
                params[ielement].sr_share = JsmnSolLib.getBytes(json, t.start, t.end);
            } else if(compareStrings(jsonElement, "country")) {
                t = tokens[ielement+1];
                params[ielement].country = JsmnSolLib.getBytes(json, t.start, t.end);
            } else if(compareStrings(jsonElement, "walletDir")) {
                t = tokens[ielement+1];
                params[ielement].walletDir = JsmnSolLib.getBytes(json, t.start, t.end);
            }
        }
    }

    function compareStrings (string a, string b) view returns (bool){
       return keccak256(a) == keccak256(b);    }

    function getContractAddress() public view returns (address) {
        return this;
    } }

Best Answer

Good news. It deployed fine costing 3531698 gas. This is over the 3M default that is there to keep us honest and warn that we might be getting close the block gasList on an actual chain. You can raise the gasLimit (I raised to 7M to try it), and then you'll be fine.

Bad news. This contract is about as non-blockchain as you can get. I would show you how to approach it but I'm unsure which if any of the fields shown is a unique identifier for the objects. Consider which fields are absolutely needed in the smart contract and how the data should be organized. https://medium.com/solidified/the-joy-of-minimalism-in-smart-contract-design-b67fb4073422

The last thing a smart contract should be doing is parsing an object passed in by a lazy client. Force the client to speak the contract's language.

Hope it helps.

Related Topic