trufflesuite / truffle-migrate

On-chain migrations management
17 stars 17 forks source link

web3 available for truffle test but not for truffle migrate #28

Open barakman opened 6 years ago

barakman commented 6 years ago

Here is what the doc says:

A web3 instance is available in each test file, configured to the correct provider. So calling web3.eth.getBalance just works!

This is indeed the case for a JS file which is used as input to truffle test.

But using the same file as (implicit) input to truffle migrate gives:

(node:1944) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): ReferenceError: web3 is not defined

You may ask yourselves, when would the same file be used for both truffle test and truffle migrate?

Well, for example, I need to initialize a large database (3-dimensional array).

I need this both for testing and for the actual deployment.

Adding let web3 = require("web3") in the file doesn't seem appropriate (because it is not necessarily necessary).

I believe that truffle test and truffle migrate should behave identically on this aspect.

Thanks

thackerronak commented 6 years ago

any updates about web3 in truffle migrate? or is there anyway to use batch request in migrate script?

hiddentao commented 4 years ago

My workaround for this in my migration scripts:

const Web3 = require('web3')

const { networks } = require('../truffle-config.js')

module.exports = async (deployer, network) => {
  if ('development' === network) {
    const { provider } = (networks[network] || {})
    if (!provider) {
      throw new Error(`Unable to find provider for network: ${network}`)
    }

    const web3 = new Web3(provider)

    // do stuff with web3 instance
  }
}

Note: using truffle@5.0.28, web3@1.0.0-beta33

ysqi commented 3 years ago

it is work for me using Truffle v5.1.46 :

const Migrations = artifacts.require("Migrations");
module.exports = async function (deployer, network, accounts) {
  var adapter = Migrations.interfaceAdapter;
  const web3 = adapter.web3;
  const provider = web3.currentProvider
}