9

I use truffle. When I deploy contracts, sometimes I encounter gas related errors. I always set fixed value of gas price in truffle.js. I want to change it. I think we can use web3.eth.estimateGas and web3.eth.gasPrice before sign and deploy. However, I don't know how to use it in truffle way. If possible, I prefer to see the suggested price then I want to sign txes and deploy it. Can you help me?

The following are what I did.

$ mkdir /tmp/manatoken-infra
$ cd /tmp/manatoken-infra
$ truffle unbox decentraland/mana
$ npm install ethereumjs-wallet bip39 web3-provider-engine@8.6.1 web3@0.18.4 zeppelin-solidity --save
$ vi ./truffle.js
$ vi ./migrations/2_deploy_contracts.js
$ truffle compile
$ truffle deploy --network ropsten

./truffle.js

var bip39 = require("bip39");
var hdkey = require('ethereumjs-wallet/hdkey');
var ProviderEngine = require("web3-provider-engine");
var WalletSubprovider = require('web3-provider-engine/subproviders/wallet.js');
var Web3Subprovider = require("web3-provider-engine/subproviders/web3.js");
var Web3 = require("web3");
var FilterSubprovider = require('web3-provider-engine/subproviders/filters.js');

// Get our mnemonic and create an hdwallet
var mnemonic = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic));

// Get the first account using the standard hd path.
var wallet_hdpath = "m/44'/60'/0'/0/";
var wallet = hdwallet.derivePath(wallet_hdpath + "0").getWallet();
var address = "0x" + wallet.getAddress().toString("hex");

var providerUrl = "https://testnet.infura.io";
var engine = new ProviderEngine();
engine.addProvider(new FilterSubprovider());
engine.addProvider(new WalletSubprovider(wallet, {}));
engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(providerUrl)));
engine.start(); // Required by the provider engine.

module.exports = {
  networks: {
    "ropsten": {
      network_id: 3,    // Official ropsten network id
      provider: engine, // Use our custom provider
      from: address,    // Use the address we derived
      gas: 4600000
    }
  },
  rpc: {
    // Use the default host and port when not using ropsten
    host: "localhost",
    port: 8545
  }
};

./migrations/2_deploy_contracts.js

const MANAContinuousSale = artifacts.require("./MANAContinuousSale.sol");

module.exports = function(deployer) {
  deployer.deploy(MANAContinuousSale);
};

Error Message

Running migration: 2_deploy_contracts.js
  Deploying MANAContinuousSale...
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: The contract code couldn't be stored, please check your gas amount.

Truffle Doc

gas: Gas limit used for deploys. Default is 4712388.

gasPrice: Gas price used for deploys. Default is 100000000000 (100 Shannon).

http://truffleframework.com/docs/advanced/configuration


Update 1

Seems already discussed and not fixed it...?

https://github.com/trufflesuite/truffle-contract/issues/25


Update 2

Deployment was failed even I set the maximum gas price. After I splitted into two contracts, seems it worked. I will confirm the contract methods.

./migrations/2_deploy_contracts.js

const MANAToken = artifacts.require("./MANAToken.sol");

module.exports = function(deployer) {
  deployer.deploy(MANAToken);
};

./migrations/3_deploy_contracts.js

const MANAContinuousSale = artifacts.require("./MANAContinuousSale.sol");

module.exports = function(deployer) {
  deployer.deploy(MANAContinuousSale, 1, "0x8005ceb675d2ff8c989cc95354438b9fab568681", "0x3dc439963dfbfd3993d29dd888a0747169fbed35");
};

Deploy

$ truffle deploy --network ropsten
Using network 'ropsten'.

Running migration: 2_deploy_contracts.js
  Deploying MANAToken...
  MANAToken: 0x3dc439963dfbfd3993d29dd888a0747169fbed35
Saving successful migration to network...
Saving artifacts...

$ vi ./migrations/3_deploy_contracts.js 
$ truffle deploy --network ropsten
Using network 'ropsten'.

Running migration: 3_deploy_contracts.js
  Deploying MANAContinuousSale...
  MANAContinuousSale: 0x74ba5124592071065418fabee0959c74b99c5815
Saving successful migration to network...
Saving artifacts...

Upadte 3

I sent 1ETH to the created contract address. However, Bad instruction error showed. Seems my deployment was not correct. I expected transfer the token automatically.

https://ropsten.etherscan.io/tx/0xa4871c69bc190917e08a81b84d1ac94111cf681be458a634b9698b4f9dbf7e30


Upadte 4

I noticed my mistake. I sent 1ETH to token's contract. I sent again to the MANAContinuousSale contract address. However, instruction error was showed again.

https://ropsten.etherscan.io/tx/0x703830d0f9337f347796fb9688fb039bdd69fa6c08d6cfd92808e5471740558c


Update 5

I changed to use promise but still I get error..

./migrations/2_deploy_contracts.js

const MANAToken = artifacts.require("./MANAToken.sol");
const MANAContinuousSale = artifacts.require("./MANAContinuousSale.sol");

module.exports = function(deployer) {
  deployer.deploy(MANAToken).then(function() {
    return MANAToken;
  }).then(function(){
    return deployer.deploy(MANAContinuousSale, 1, "0x8005ceb675d2ff8c989cc95354438b9fab568681", MANAToken);
  }).then(function(){
  });
};

deploy log

$ truffle deploy --network ropsten
Using network 'ropsten'.

Running migration: 2_deploy_contracts.js
  Deploying MANAToken...
  MANAToken: 0x5c236453ac621a6a28eb3fc50b7724b6ce3c30ca
  Deploying MANAContinuousSale...
Error encountered, bailing. Network state unknown. Review successful transactions manually.
BigNumber Error: new BigNumber() not a number: function TruffleContract() {
        this.constructor = temp;
        return Contract.apply(this, arguments);
      }
    at raise (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:38210:25)
zono
  • 1,473
  • 5
  • 17
  • 30

1 Answers1

13

You can pass extra transaction parameters to deployer.deploy() like from, gas and gasPrice

deployer.deploy(MyContract, constructorParam, {
  from: "0x01234...",
  gas: 1000000
});

I've tested this in a sample project

truffle.js

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*",
      gas: 4700000,
      from: "0x7777..."
    }
  }
};

migrations/1_initial_migration.js

var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
  deployer.deploy(Migrations, {
    gas: 2000000,
    from: "0x4444...."
  });
};

migrations/2_deploy_contracts.js

var A = artifacts.require("./A.sol");

module.exports = function(deployer) {
  deployer.deploy(A, {
    gas: 1000000,
    from: "0x6666...."
  });
};

contracts/A.sol

pragma solidity ^0.4.16;

contract A {
    address public owner;
    function A() {
        owner = msg.sender;
    }
}
Ismael
  • 30,570
  • 21
  • 53
  • 96
  • That great! It can save my gas. I think truffle does not use estimate gas price now. It uses only fixed price. So I think your way is the best way for now. – zono Sep 06 '17 at 06:12