1

Everything I've tried results in strange errors, and it's been difficult to get this working. I've been struggling with this for a while, especially having problems determining which versions of web3 and ganache will work together. The code is these libraries is evolving quickly and they are constantly in flux. This results in many version combinations having broken functionality that's difficult to diagnose.

In particular, there seems to be promise handling async problems with web3 versions later than 1.0.0-beta.37, which makes them not work at all with ganache. What happens is web3.eth.getAccounts() will fail, with no known workaround.

  • What code did you tray that is not working? If you search the site you will find several example working. But probably is better if we explain why your code is not working. – Ismael Mar 5 at 4:50
  • @Ismael, specifically, I was trying to deploy a compiled smart contract to ganache using web3. I was not able to find all the information needed to do that in one place. All of the sample code that I saw failed with various errors mostly because the sample code was written some time ago, and web3 has gone through many iterations; those code samples neither say which version of web3 they work with nor work with the current web3. I provide a working solution that offers these specific details. – djenning90 Mar 7 at 23:00
  • In addition, I have solved the "10 maxListeners warning' problem, as well as making correct use of working async code, which is something many of the samples didn't do. Basically I post this solution as a ready-to-use implementation for deployment that is unlikely to leave the reader hunting for other parts of the answer. – djenning90 Mar 7 at 23:00
  • I think your answer is good but your question is too open ended. You say that something doesn't work, but you do not state the error or show the source causing the error. – Ismael Mar 8 at 2:24
3

After more than a week of google searches, scouring stack exchange, combining bits and pieces of what I found, and with my own trial and error, I finally came up with everything needed to successfully deploy a compiled smart contract ganache. It wasn't easy to figure out. I haven't seen a full solution presented anywhere in my searches, so I wanted to share my findings here, in case you're in a similar situation.

The first key thing is to only use versions of web3 and ganache-cli that are compatible. I've noted my findings in the code below. It looks like very recent web3 and recent ganache versions are currently incompatible due to bugs, which are probably being fixed. If you'd like to reproduce those problems, simply upgrade to a version of web3 later than 1.0.0-beta.37 and try the code posted below.

The second thing I learned was how to correctly implement the async programming, which takes a little care, especially if you're new to JavaScript as I am.

Here is fully working code to deploy a contract from its compiled JSON representation using Web3 and gnache:

// Tested using web3@^1.0.0-beta.37 and ganache-cli@^6.4.1
const ganache = require('ganache-cli');
const Web3 = require('web3');
const fs = require("fs");

async function deploy() {
    const provider = ganache.provider();
    provider.setMaxListeners(15);       // Suppress MaxListenersExceededWarning warning
    const web3 = new Web3(provider);
    this.accounts = await web3.eth.getAccounts();

    // Read in the compiled contract code and fetch ABI description and the bytecode as objects
    const compiled = JSON.parse(fs.readFileSync("output/contracts.json"));
    const abi = compiled.contracts["ContractName.sol"]["ContractName"].abi;
    const bytecode = compiled.contracts['ContractName.sol']['ContractName'].evm.bytecode.object;

    // Deploy the contract and send it gas to run.
    this.contract = await new web3.eth.Contract(abi)
        .deploy({data:'0x'+ bytecode, arguments: []})
        .send({from: this.accounts[0], gas:'5000000'});

    return this;
}

deploy().then(useContract);

function useContract(result) {
    // Use result.accounts and result.contract here to do what you like.
    console.log('Contract deployed to: ' + result.contract.options.address);
    console.log('Owner address is:     ' + result.accounts[0]);
}

If this helps you, please upvote. I'm a new contributor here. Thanks!

2

I upvoted your answer because it seems like a nice template for doing it all with nothing but Web3. This is a good thing to know about but a little tedious, in my opinion.

For the benefit of other readers who find this question/answer, the Truffle framework addresses this and other concerns with higher-level abstractions so we don't have to get down in the weeds to address routine tasks.

The problem is that it's another layer to learn which can be a little much for newcomers already faced with cli, web3 and solidity.

Migrations configuration can coordinate multi-contract deployments (roughly):

// pre-amble, dependencies, etc.
deployer.deploy(ContractName);
// carry on ... deploy another contract?

Run the migrations:

$ truffle migrate

Great.

Truffle is helpful for the next step, when working with deployed contracts - unit tests, servers, user-interface.

This sort of thing:

myContract.contractFunction(args) ...

is more pleasant to work with than the Web3 functions that are wrapped up inside.

It's good to know Truffle exists because answers on sites like SE may use truffle syntax.

Hope it helps.

  • I'm new to this and I only have learned Web3 so far, so your post that there are higher-level frameworks available comes as good news to me. Thanks for sharing that! – djenning90 Mar 4 at 22:10

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