core.addProviders不是函数

时间:2019-01-27 12:26:41

标签: blockchain ethereum solidity web3 truffle

我正在尝试运行具有以下代码的deploy.js文件:

const HDWalletProvider = require("truffle-hdwallet-provider");
const Web3 = require("web3");
const compiledFactory = require('./build/CampaignFactory.json');
//const web3 = new Web3(provider);

const provider = new HDWalletProvider(
  "[12 word mnemonic]",
  "[rinkeby api from infura]"
);

const web3 = new Web3(provider);

const deploy = async () => {
  const accounts = await web3.eth.getAccounts();

  console.log("Attempting to deply from account", accounts[0]);
  const result = await new web3.eth.Contract(JSON.parse(compiledFactory.interface))
    .deploy({ data: compiledFactory.bytecode })
    .send({ gas: '1000000', from: accounts[0] });

  //console.log(interface);
  console.log("Contract deployed to", result.options.address);
};
deploy();

当我运行node deploy.js时:它现在抛出错误,以前可以正常运行, 错误显示如下:

C:\Users\Kartik.Ganiga\Desktop\BlockChain\Ethereum\campaign\node_modules\web3\src\index.js:76
core.addProviders(Web3);
     ^

TypeError: core.addProviders is not a function
    at Object.<anonymous> (C:\Users\Kartik.Ganiga\Desktop\BlockChain\Ethereum\campaign\node_modules\web3\src\index.js:76:6)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\Kartik.Ganiga\Desktop\BlockChain\Ethereum\campaign\ethereum\deploy.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)

和web3.js(位于node_modules内部)文件具有代码:

/*
    This file is part of web3.js.

    web3.js is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    web3.js is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with web3.js.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @file index.js
 * @authors:
 *   Fabian Vogelsteller <fabian@ethereum.org>
 *   Gav Wood <gav@parity.io>
 *   Jeffrey Wilcke <jeffrey.wilcke@ethereum.org>
 *   Marek Kotewicz <marek@parity.io>
 *   Marian Oancea <marian@ethereum.org>
 * @date 2017
 */

"use strict";


var version = require('../package.json').version;
var core = require('web3-core');
var Eth = require('web3-eth');
var Net = require('web3-net');
var Personal = require('web3-eth-personal');
var Shh = require('web3-shh');
var Bzz = require('web3-bzz');
var utils = require('web3-utils');

var Web3 = function Web3() {
    var _this = this;

    // sets _requestmanager etc
    core.packageInit(this, arguments);

    this.version = version;
    this.utils = utils;

    this.eth = new Eth(this);
    this.shh = new Shh(this);
    this.bzz = new Bzz(this);

    // overwrite package setProvider
    var setProvider = this.setProvider;
    this.setProvider = function (provider, net) {
        setProvider.apply(_this, arguments);

        this.eth.setProvider(provider, net);
        this.shh.setProvider(provider, net);
        this.bzz.setProvider(provider);

        return true;
    };
};

Web3.version = version;
Web3.utils = utils;
Web3.modules = {
    Eth: Eth,
    Net: Net,
    Personal: Personal,
    Shh: Shh,
    Bzz: Bzz
};

core.addProviders(Web3);

module.exports = Web3;

3 个答案:

答案 0 :(得分:6)

这对我来说也是个问题:- 然后我切换了我的web3版本:- 到:

1.0.0-beta.37

效果很好

答案 1 :(得分:0)

确保您的geth也在终端中运行。只需在您的终端上运行geth。您的web3是package.json中的"web3": "^1.0.0-beta.37"

答案 2 :(得分:-1)

长话短说,要解决一个错误,我们需要添加两行,并将一行代码更改为我们的测试文件。进行如下所示的三个更改:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');

// UPDATE THESE TWO LINES RIGHT HERE!!!!! <-----------------
const provider = ganache.provider();
const web3 = new Web3(provider);

const { interface, bytecode } = require('../compile');

let accounts;
let inbox;

beforeEach(async () => {
// Get a list of all accounts
accounts = await web3.eth.getAccounts();
// Use one of those accounts to deploy the contract
inbox = await new web3.eth.Contract(JSON.parse(interface))
  .deploy({ data: bytecode, arguments: ['Hi there!'] })
  .send({ from: accounts[0], gas: '1000000' });

// ADD THIS ONE LINE RIGHT HERE!!!!! <---------------------
inbox.setProvider(provider);
 });

describe('Inbox', () => {
   it('deploys a contract', () => {
      assert.ok(inbox.options.address);
     });
   it('has a default message', async () => {
      const message = await inbox.methods.message().call();
      assert.equal(message, 'Hi there!');
     });
});