0

UPDATE: I manage to figure out what's wrong is in this line

var byteCode = compiledCode.contracts[':TestStorage'].***byteCode***;

and it should be

var byteCode = compiledCode.contracts[':TestStorage'].***bytecode***;

Completely typo, but it makes me realize that this error is quite general so that could be a lot of reasons.

I have see a lot of people raised this question, but none of the answer/solution could solve my problem so ask here.

I setup truffle on my Windows OS and use Ganache as my personal development blockchain. I create a Node project, use Express setup a server handling request and deploy my contract. I have this error returned:

The contract code couldn't be stored, please check your gas amount

My code is below:

var router = express.Router();
var logger = require('./logger');
var pool = require('./pool');
var web3 = require('./getWeb3');
const fs = require('fs');
const solc = require('solc');

router.get('/',  function(req, res, next){
    var code = fs.readFileSync('TestStorage.sol').toString();
    var compiledCode = solc.compile(code);
    var abi = JSON.parse(compiledCode.contracts[':TestStorage'].interface);
    var TSContract = web3.eth.contract(abi);
    var byteCode = compiledCode.contracts[':TestStorage'].byteCode;

    console.log("Unlocking coinbase account");
    var password = "";
    try {
        web3.personal.unlockAccount(web3.eth.coinbase, password);
    } catch(e) {
        console.log(e);
        return;
    }

    var deployedContract = TSContract.new({data: '0x' + byteCode, from: web3.eth.coinbase, gas: 6721975}, (err, res) => {
        if (err) {
            console.log('error message: ' + err);
            return;
        }

        // Log the tx, you can explore status with eth.getTransaction()
        console.log(res.transactionHash);

        // If we have an address property, the contract was deployed
        if (res.address) {
            console.log('Contract address: ' + res.address);
        }
    });
}

getWeb3.js

const Web3 = require('web3');

var web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:9545'));

module.exports=web3;

I have tried below 1. adjust gas in new() and if it greater than 6721975, then error - "Error: Exceeds block gas limit" returned. 2. I manually input above steps in node command line and contact can be deployed successfully.

2
  • @LauriPeltonen I saw that post and not working for me
    – Bing Lan
    Mar 20, 2018 at 11:44
  • 1
    Generally this is a symptom of your code throwing an error (ie a require/assert failure etc) or the compiled code being vastly huge. Easy way to test it is to put it into remix.ethereum.org and step by step through the debug to find out where it dies.
    – supakaity
    Mar 20, 2018 at 12:29

0