1

So when I try to add two (or even just the one) state string variables to the Boostr contract (title & description) I get the following error

There was a problem deploying the contract: Error: The contract code couldn't be stored, please check your gas limit.

But by removing them the contract deploys no problem.

Boostr.sol (fixed)

pragma solidity ^0.4.17;

    contract BoostrFactory {
            address[] private deployedBoostrs;

            function createBoostr(uint minimum) public {
                    address newBoostr = new Boostr(minimum, msg.sender);
                    deployedBoostrs.push(newBoostr);
            }

            function getAllBoostrs() public view returns (address[]) {
                    return deployedBoostrs;
            }
    }

    contract Boostr {
            struct Request {
                    string description;
                    uint value;
                    address recipient;
                    bool complete;
                    mapping(address=>bool) approval;
                    uint approvalCount;
            }

            Request[] public requests;
            address private manager;
            uint public minimumContribution;
            mapping(address=>bool) private supporters;
            mapping(address=>bool) private approvers;
            uint public approversCount;

            modifier managerRestricted() {
                    require(msg.sender == manager);
                    _;
            }

            modifier approverRestricted() {
                    require(approvers[msg.sender]);
                    _;
            }

            constructor(uint minimum, address creator) public {
                    manager = creator;
                    minimumContribution = minimum;
            }

            function getManager() public view returns (address) {
                    return manager;
            }

            function getApprover(address addr) public view returns (bool) {
                    return approvers[addr];
            }

            function getSupporter(address addr) public view returns (bool) {
                    return supporters[addr];
            }

            function contribute() public payable {
                    if (msg.value > 0) {
                            supporters[msg.sender] = true;
                            if (msg.value > minimumContribution) {
                                    approvers[msg.sender] = true;
                                    approversCount++;
                            }
                    }
            }

            function getSummary() public view returns (uint, uint, uint, uint, address) {
                    return (
                            minimumContribution,
                            address(this).balance,
                            requests.length,
                            approversCount,
                            getManager()
                    );
            }

            function getRequestsCount() public view returns (uint) {
                    return requests.length;
            }

            function createRequest(string description, uint value, address recipient) public managerRestricted {
                    Request memory newRequest = Request ({
                            description: description,
                            value: value,
                            recipient: recipient,
                            complete: false,
                            approvalCount: 0
                    });
                    requests.push(newRequest);
            }

            function approveRequest(uint index) public {
                    Request storage request = requests[index];
                    require (approvers[msg.sender]);
                    require (!request.approval[msg.sender]);
                    request.approval[msg.sender] = true;
                    request.approvalCount++;
            }

            function finalizeRequest(uint index) public managerRestricted payable {
                    Request storage request = requests[index];
                    require (!request.complete);
                    require (request.approvalCount > approversCount / 2);
                    request.complete = true;
                    request.recipient.transfer(request.value);
            }
    }

Boostr.sol (broken)

    pragma solidity ^0.4.17;

    contract BoostrFactory {
            address[] private deployedBoostrs;

            function createBoostr(string title, string description, uint minimum) public {
                    address newBoostr = new Boostr(title, description, minimum, msg.sender);
                    deployedBoostrs.push(newBoostr);
            }

            function getAllBoostrs() public view returns (address[]) {
                    return deployedBoostrs;
            }
    }

    contract Boostr {
            struct Request {
                    string description;
                    uint value;
                    address recipient;
                    bool complete;
                    mapping(address=>bool) approval;
                    uint approvalCount;
            }

            Request[] public requests;
            address private manager;
            string public title;
            string public description;
            uint public minimumContribution;
            mapping(address=>bool) private supporters;
            mapping(address=>bool) private approvers;
            uint public approversCount;

            modifier managerRestricted() {
                    require(msg.sender == manager);
                    _;
            }

            modifier approverRestricted() {
                    require(approvers[msg.sender]);
                    _;
            }

            constructor(string bstrTitle, string bstrDesription, uint minimum, address creator) public {
                    title = bstrTitle;
                    description = bstrDesription;
                    manager = creator;
                    minimumContribution = minimum;
            }

            function getManager() public view returns (address) {
                    return manager;
            }

            function getApprover(address addr) public view returns (bool) {
                    return approvers[addr];
            }

            function getSupporter(address addr) public view returns (bool) {
                    return supporters[addr];
            }

            function contribute() public payable {
                    if (msg.value > 0) {
                            supporters[msg.sender] = true;
                            if (msg.value > minimumContribution) {
                                    approvers[msg.sender] = true;
                                    approversCount++;
                            }
                    }
            }

            function getSummary() public view returns (string, string, uint, uint, uint, uint, address) {
                    return (
                            title,
                            description,
                            minimumContribution,
                            address(this).balance,
                            requests.length,
                            approversCount,
                            getManager()
                    );
            }

            function getRequestsCount() public view returns (uint) {
                    return requests.length;
            }

            function createRequest(string reqDescription, uint value, address recipient) public managerRestricted {
                    Request memory newRequest = Request ({
                            description: reqDescription,
                            value: value,
                            recipient: recipient,
                            complete: false,
                            approvalCount: 0
                    });
                    requests.push(newRequest);
            }

            function approveRequest(uint index) public {
                    Request storage request = requests[index];
                    require (approvers[msg.sender]);
                    require (!request.approval[msg.sender]);
                    request.approval[msg.sender] = true;
                    request.approvalCount++;
            }

            function finalizeRequest(uint index) public managerRestricted payable {
                    Request storage request = requests[index];
                    require (!request.complete);
                    require (request.approvalCount > approversCount / 2);
                    request.complete = true;
                    request.recipient.transfer(request.value);
            }
    }

compile.js

    const path = require('path');
    const solc = require('solc');
    const fs = require('fs-extra');

    const buildPath = path.resolve(__dirname, 'build');
    const boostrPath = path.resolve(__dirname, 'contracts', 'Boostr.sol');
    const source = fs.readFileSync(boostrPath, 'utf8');
    const output = solc.compile(source, 1).contracts;

    fs.removeSync(buildPath);

    for (let contract in output) {
        fs.outputJsonSync(
            path.resolve(buildPath, `${contract.replace(':', '')}.json`),
            output[contract]
        );
    }

deploy.js

    require('../env');
    const path = require('path');
    const fs = require('fs-extra');
    const HDWalletProvider = require('truffle-hdwallet-provider');
    const Web3 = require('web3');

    const config = require('../config/config');
    const compiledFactory = require('../ethereum/build/BoostrFactory.json');

    const mnemonic = config.mnemonic;
    const network = config.infura.endpoint;

    const provider = new HDWalletProvider(mnemonic, network);
    const web3 = new Web3(provider);

    (async () => {
        const accounts = await web3.eth.getAccounts();
        let factory;
        console.log(`Attempting to deploy from account ${accounts[0]}`);
        try {
            factory = await new web3.eth.Contract(JSON.parse(compiledFactory.interface))
                .deploy({
                    data: '0x' + compiledFactory.bytecode
                })
                .send({
                    gas: 1000000,
                    from: accounts[0]
                });
            console.log('Contract deployed to:', factory.options.address);
        } catch (err) {
            return console.log('There was a problem deploying the contract:', err);
        }

        const factoryFile = path.resolve(__dirname, 'factory.json');
        console.log(factoryFile);
        try {
            await fs.outputFile(
                factoryFile,
                JSON.stringify({ address: factory.options.address })
            );
        } catch (err) {
            console.log(
                `There was a problem generating the address file. You can do this manually by creating ${factoryFile} and saving ${
                    factory.options.address
                } as the content`
            );
        }
    })();

package.json

    "dependencies": {
        "ajv": "^6.5.3",
        "dotenv": "^6.0.0",
        "fs-extra": "^7.0.0",
        "jsonfile": "^4.0.0",
        "next": "^6.1.1",
        "next-routes": "^1.4.2",
        "prop-types": "^15.6.2",
        "react": "^16.4.2",
        "react-dom": "^16.4.2",
        "semantic-ui-css": "^2.3.3",
        "semantic-ui-react": "^0.82.3",
        "solc": "^0.4.24",
        "truffle-hdwallet-provider": "0.0.6",
        "web3": "^1.0.0-beta.35"
    }
1

I think the error message is actually telling you exactly what the problem is (my emphasis):

There was a problem deploying the contract: Error: The contract code couldn't be stored, please check your gas limit.

When I deploy the "broken" version of BoostrFactory in Remix, it reports that it used a little over 1.6 million gas (or a little over 1.2 million when compiler optimizations are enabled), but you're setting a gas limit of 1 million gas. Try upping that to at least 1.7 million and see if that takes care of your problem.

| improve this answer | |
  • Thanks @smarx. That worked. I had only increased to to 1.5 mil before. – Les Ansley Aug 24 '18 at 19:53

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.