4

I'm writing a script to be executed with truffle exec and I need to add some logic conditional on the network we're calling truffle exec with.

This is (roughly) what I want in my script:

const contract = artifacts.require("./MyContract.sol");
module.exports = function(callback) {
  // TODO: Do some conditional thing based on network!

  contract.callMethod();
}

I'm hoping to call this script in the following way:

truffle exec script.js --network ropsten

5 Answers 5

4

Not sure if this is the best method, but the arguments you pass to truffle, including the network, are available under process.argv.

script.js:

process.argv.forEach(function (val, index, array) {
  console.log(index + ': ' + val);
});

Then:

> truffle exec script.js --network development
Using network 'development'.

0: /usr/bin/node
1: /usr/bin/truffle
2: exec
3: foo.js
4: --network
5: development

So access that array at the relevant index for the network name.

4
  • In which case, the --network argument is redundant. Unlike truffle migrate and truffle deploy (which are identical btw), truffle exec and truffle test do not receive the network as an argument, so one has to pass it explicitly and then access it via process.argv. May 14, 2019 at 6:56
  • Ah, good point - which must be why it's outputting the line Using network 'development', i.e. "I don't know which network to use, so using the first one in the config file." May 14, 2019 at 7:05
  • Actually, now that you've mentioned it... It kinda makes me think that perhaps truffle test can somehow be configured with a specific network. I guess it'll require looking into their source code... May 14, 2019 at 7:11
  • Not the cleanest, but this works! Thanks for the suggestion. May 14, 2019 at 17:45
1

You can use web3.eth.network api .

'use strict';

global.artifacts = artifacts;
global.web3 = web3;

async function main(){
    const newtworkType = await web3.eth.net.getNetworkType();
    const networkId = await web3.eth.net.getId();
    console.log("network type:"+newtworkType);
    console.log("network id:"+networkId);
}

// For truffle exec
module.exports = function(callback) {
    main().then(() => callback()).catch(err => callback(err))
};

I hope it can be useful :)

1

As of Aug 2020 Truffle makes the config global available to scripts. The network property is set in the usual way.

module.exports = function(callback) {
  const network = config.network;
  if (network == 'development') {
     ...
  }
  callback();
}
0

Or you can find the "--network" position argument inside process.argv and following logic next one should be the network name:

module.exports = async (callback) => {
const networkIndex = process.argv.findIndex(arg => arg === '--network');
const networkName = process.argv[networkIndex + 1];
console.log(networkName);

callback();
}
-1

You can define your network in truffle-config.js

For example:

const HDWalletProvider = require('truffle-hdwallet-provider');

module.exports = {
  test: {
    host: `127.0.0.1`,
    port: 9999,
    network_id: 3,
    gas: 5500000,
    confirmations: 2,
    timeoutBlocks: 200,
    skipDryRun: true
  },
  ropsten: {
    provider: () => new HDWalletProvider(mnemonicOrPrivatekey, `https://rinkeby.infura.io/${infuraKey}`),
    network_id: 3,
    gas: 5500000,
    confirmations: 2,
    timeoutBlocks: 200,
    skipDryRun: true
  },
}

Then you can run script with --network test

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.