2

when I deploy a contract, the truffle frame told me this error:“The contract code couldn't be stored, please check your gas amount”。 the solidity code is:

  1. Base.sol

    pragma solidity ^0.4.22; contract Base{ uint public num; event mint(uint n); constructor() public { }

    function Minit(uint _n) external returns (bool) {
        num = _n;
        emit mint(num);
        return true;
    }
    function getN() external view returns(uint result){
        result =  num;
    
    }
    

    }

  2. Extend.sol

    pragma solidity ^0.4.22; import "./Base.sol"; contract Extend{ Base public base; uint public num; constructor() public { base = new Base(); base.Minit(10); }
    function setN() public{ num = base.getN();

     }
     function getN() public view returns(uint){
         return num;
     }
    

    }

  3. I run these smart contracts in a private ethereum. the genesis.json is:

    {

    "config": {

    "chainId": 168,

    "homesteadBlock":5,

    "eip155Block":0,

    "eip158Block":0

    }, "alloc" : {},

    "coinbase" : "0x0000000000000000000000000000000000000000",

    "difficulty" : "0x400",

    "extraData" : "",

    "gasLimit" : "0xffffff",

    "nonce" : "0x0000000000000042",

    "mixhash" :"0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash" :"0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp" : "0x00" }

  4. the truffle.js file is:

    module.exports = { // See http://truffleframework.com/docs/advanced/configuration // to customize your Truffle configuration! networks:{ live: { host:"127.0.0.1", port:8545, network_id:"168", from:"0xd31457ba4448cf4ec0c9b9e87e06d633cd9e5406", gas:6500000

         }
     }
    
  5. the deploy script is:2_deploy_contracts.js

    var Extend = artifacts.require("./Extend.sol"); module.exports = function(deployer){ deployer.deploy(Extend);

}

  1. go's verions

    go version go1.10 linux/amd64

  2. geth's versions

    Geth Version: 1.8.15-stable Architecture: amd64 Protocol Versions: [63 62] Network Id: 1 Go Version: go1.10 Operating System: linux GOPATH=/home/hlf/Gopath GOROOT=/usr/local/go

so, I want to know why I failed. and What should I do?


Now, I found that if one contract call another contract's external function and this function have a returns value,the gas will be burned all, sample:

  1. Base.sol

    pragma solidity ^0.4.22;

    contract Base{ address public owner; function Mint() payable external returns(uint){ return 1; } constructor() public {} }

2)Extend.sol

pragma solidity ^0.4.22;
import "./Base.sol";
contract Extend{
    Base public base;
    uint public es;
    constructor (address  _baseAdd) public {
        base = Base(_baseAdd);
    }
    function mint() public {
     es =   base.Mint();
    }
    function getES() public view returns(uint){
       return es;   
   }

}

when I call ExtendInst.mint() in truffle console, es = base.Mint() will consumes all the gas. why? And Is it can't use a contract's external function to return a value?

2
  • 1
    It looks like you should be deploying Extend.sol. The truffle migrations could be important so truffle migrate works as expected. Sep 5, 2018 at 3:22
  • Also, the backing blockchain could be important because of the Byzantium hard fork. Take a good look at geth version to figure out if the blockchain is pre or post-Byzantium. If it's pre-Byzantium and you don't want to/can't change it, then try compiling with 0.4.21. This could involve rolling back truffle to an earlier version. Sep 5, 2018 at 3:23

1 Answer 1

0

I think that you cannot deploy smart contract by using this code

base = new Base();

the error “The contract code couldn't be stored, please check your gas amount” will throw because web3.js lib cannot get the byte code of the smart contract.

You should deploy the contract base, and pass the contract address into contructor of the Extend contract

pragma solidity ^0.4.22;
import "./Base.sol";
contract Extend{
    Base public base;
    uint public num;
    constructor(address _base) public {
        base = Base(_base);
        base.Minit(10);
    }   
    function setN() public{
        num = base.getN();

    }
    function getN() public view returns(uint){
        return num;
    }
}

Or you should extend from contract Base as below

pragma solidity ^0.4.22;
import "./Base.sol";
contract Extend is Base {
    uint public num;
    constructor() public {
        Minit(10);
    }   
    function setN() public{
        num = getN();

    }
    function getN() public view returns(uint){
        return num;
    }
}
6
  • I konw your idea, and I had tried your means. but I want to know how a contract call another depended contract's external function. do you kown?
    – J.Hao
    Sep 5, 2018 at 11:47
  • You can do like my answer You should deploy the contract base, and pass the contract address into contructor of the Extend . I will put sample code for more clearly.
    – Tony Dang
    Sep 5, 2018 at 11:56
  • I found other question. when One contract call another external contract's function, it will use all gas. sample :
    – J.Hao
    Sep 6, 2018 at 6:57
  • it seems so; when we calling other smart contract; there's a case/chance that it will consume all of gas.
    – Tony Dang
    Sep 7, 2018 at 11:47
  • I found that you can add “byzantiumBlock”:0 or " constantinopleBlock ":0 in genesis.json to solv this question
    – J.Hao
    Sep 11, 2018 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.