Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web3 client not receiving eth_subscribe data from hardhat network #588

Closed
Detoo opened this issue May 20, 2020 · 28 comments
Closed

web3 client not receiving eth_subscribe data from hardhat network #588

Detoo opened this issue May 20, 2020 · 28 comments
Assignees
Labels
type:bug Something isn't working

Comments

@Detoo
Copy link

Detoo commented May 20, 2020

Hi, I am having a weird issue where my web3 client connecting to a local buidler-evm wasn't able to receive any event. I have a runnable example along with a control group here (https://github.com/Detoo/buidler-eth-subscribe-issue) but here's a quick summary of what I saw:

The code snippet

greeter.events.Greet()
        .on("data", (event) => console.log('event:', event))
        .on("error", (error) => console.log('error:', error))

await greeter.methods.greet().send()

Expected Results

For sanity check, I tested the same codes with a ganache-cli evm instead and it did work:

$ npm run main

> buidler-eth-subscribe-issue@1.0.0 main /Users/detoo/Dropbox/playground/buidler-eth-subscribe-issue
> buidler run src/main.ts

All contracts have already been compiled, skipping compilation.
event: {
  logIndex: 0,
  transactionIndex: 0,
  transactionHash: '0x161b5b7ea58d5c634bcdef15780f5382d65d3ad257e970ef23bb827ea94fd58e',
  blockHash: '0x21da0dab9277f0a4cd7ff44db9c5ad85d2106e851f5d532b38bf5c1f88b85e34',
  blockNumber: 2,
  address: '0xbFbd287DDF9dE89ba9781c92aa21654Dac99c283',
  type: 'mined',
  id: 'log_2406fd8c',
  returnValues: Result {
    '0': '0xbFbd287DDF9dE89ba9781c92aa21654Dac99c283',
    '1': '123',
    from: '0xbFbd287DDF9dE89ba9781c92aa21654Dac99c283',
    value: '123'
  },
  event: 'Greet',
  signature: '0xc3b6fa780771bb4c77c875ed1937bd523871551d0234535b464710f4b4a10788',
  raw: {
    data: '0x000000000000000000000000000000000000000000000000000000000000007b',
    topics: [
      '0xc3b6fa780771bb4c77c875ed1937bd523871551d0234535b464710f4b4a10788',
      '0x000000000000000000000000bfbd287ddf9de89ba9781c92aa21654dac99c283'
    ]
  }
}

Actual Results

However, when it run against a buidler-evm, I got nothing in response:

$ npm run main

> buidler-eth-subscribe-issue@1.0.0 main /Users/detoo/Dropbox/playground/buidler-eth-subscribe-issue
> buidler run src/main.ts

All contracts have already been compiled, skipping compilation.

The evm logs show it did receive eth_subscribe:

eth_accounts
eth_gasPrice
eth_sendTransaction
  Contract deployment: Greeter
  Contract address:    0x7c2c195cd6d34b8f845992d380aadb2730bb9c6f
  Transaction:         0xe8a7d36371b9d4dba1992c73a3690ef711dfc86bbaaeaa25b47ee403f03e3e0f
  From:                0xc783df8a850f42e7f7e57013759c285caa701eb6
  Value:               0 ETH
  Gas used:            517815 of 1000000
  Block #1:            0x09ac09a72e2330f44e9dbff16f299b7b89d6733ffed6651f650ab80ab3e23403
  
  console.log:
    Deploying a Greeter with greeting: Hello, world!

eth_getTransactionReceipt
eth_getCode
eth_subscribe
eth_gasPrice
eth_sendTransaction
  Contract call:       Greeter#greet
  Transaction:         0x92f0fadcd2d44019250beda05e1d0c39619a4f13d4ee1c0204e9c600cee685df
  From:                0xc783df8a850f42e7f7e57013759c285caa701eb6
  To:                  0x7c2c195cd6d34b8f845992d380aadb2730bb9c6f
  Value:               0 ETH
  Gas used:            24904 of 9500000
  Block #2:            0x597b49929b40507799ddaeebff602081858064119d2db4afa0cef749e051ae0f

eth_getTransactionReceipt

That led me to think maybe I did not setup buidler-evm properly but I not sure why. Any suggestion is very much appreciated, thanks!

@mktcode
Copy link

mktcode commented Sep 14, 2020

I have the same issue and I can confirm this. ganache-cli works while npx buidler node doesn't.

@fvictorio
Copy link
Member

I think the Buidler EVM doesn't support websocket connections yet.

You might get this to work if you use an in-process BEVM instead of connecting to ws://localhost:8545, but maybe that depends on support for the EIP1193, that is not released yet. @alcuadrado can you confirm this?

@alcuadrado
Copy link
Member

buidler evm does support websocket connections, and has code in place to support eth_subscribe, so we should consider this a bug on our side.

@fvictorio fvictorio added the type:bug Something isn't working label Sep 21, 2020
@mktcode
Copy link

mktcode commented Sep 22, 2020

As long as you don't start to override internal node objects I'm very pleased with your solution. ;) But it would be even more awesome If I can use your nice console.log plugin again which I currently can't with ganache. Any estimates on how long this could take?

@alcuadrado
Copy link
Member

Do you know which kind of websocket messages does web3 expect? We may have already fixed this in #608

@mktcode
Copy link

mktcode commented Sep 22, 2020

I don't know but maybe I can check out the 2.0 branch, if there's not too much migration to do.

@fvictorio
Copy link
Member

Dependening on the complexity of your setup, the only migration you might need to do is changing:

solc: {
  version: "0.5.5",
  optimizer: { ... }
}

for

solidity: {
  version: "0.5.5",
  settings: {
    optimizer: { ... }
  }
}

There are other breaking changes, but that's the one that almost everyone will run into.

@mktcode
Copy link

mktcode commented Sep 22, 2020

npm install nomiclabs/buidler#2.0 -g

> root@0.0.0 postinstall /home/mkt/.nvm/versions/node/v10.16.3/lib/node_modules/root
> node scripts/install.js

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'shelljs'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/mkt/.nvm/versions/node/v10.16.3/lib/node_modules/root/scripts/install.js:2:15)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

@mktcode
Copy link

mktcode commented Sep 22, 2020

I'm on 1.99.0 now. Running a script says:

npx buidler run scripts/deploy-gateway.js --network localhost
Compiling with 0.7.1
contracts/ConsumerExample.sol:19:58: Warning: Unused function parameter. Remove or comment out the variable name to silence this warning.
  function onEthPriceFulfill(uint256[] memory ethPrices, uint256 requestId) public onlySelf {
                                                         ^---------------^

Compiled 4 contracts successfully
TypeError: plugins_1.readArtifact is not a function
    at getContractFactoryByName (/home/mkt/Projects/simplor/node_modules/@nomiclabs/buidler-ethers/src/helpers.ts:55:26)
    at getContractFactory (/home/mkt/Projects/simplor/node_modules/@nomiclabs/buidler-ethers/src/helpers.ts:35:12)
    at main (/home/mkt/Projects/simplor/scripts/deploy-gateway.js:3:46)
    at Object.<anonymous> (/home/mkt/Projects/simplor/scripts/deploy-gateway.js:13:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
1.| async function main() {
2.|  // We get the contract to deploy
3.|  const SimplorGatewayFactory = await ethers.getContractFactory("SimplorGateway");

Are the changes documented somewhere in an easy to read form?

@fvictorio
Copy link
Member

You'll also need to install the new versions of the plugins. I can publish the relevant ones if you tell me which ones you are using in that project.

@mktcode
Copy link

mktcode commented Sep 22, 2020

I'm only using web3 and console.log.

@alcuadrado
Copy link
Member

I just run @Detoo's reproduction against the 2.0 branch, and I'm afraid it's not fixed.

@mktcode
Copy link

mktcode commented Sep 23, 2020

Well... I will use ganache then but it would be really nice to have this fixed soon. I don't know if I have the time and motivation to take a closer look but I definitely think this should be fixed before 2.0 release anyway. Listening for contract events is at least half of all communication that is going on, I'd say.

@Thrasymachus01
Copy link

Thrasymachus01 commented Nov 23, 2020

Is there any update on the status of this? I cannot listen to contract events.

Major pain as it would be really useful to be able to use hardhat network evm for ALL localhost development instead of switching between ganache and hardhat for testing.

@fvictorio
Copy link
Member

@Thrasymachus01 it's definitely in our backlog, but we can't give an ETA for this right now.

@sebastianst
Copy link

Is there an ETA on this one? I'm trying to replace all uses of ganache-cli with hardhat node in our projects but block header subscriptions don't seem to work.

@fvictorio fvictorio changed the title web3 client not receiving eth_subscribe data from buidler-evm web3 client not receiving eth_subscribe data from hardhat network May 13, 2021
@pizzarob
Copy link

pizzarob commented Jul 2, 2021

Any updates on this? I feel like this is a pretty important feature to have. Have to use ganache if hardhat doesn't support websockets.

@EtDu
Copy link

EtDu commented Jul 5, 2021

Has this been resolved? I'm using a WSS connection to a forked BSC archive node but it's only compatible with ganache. when I try to fork with hardhat node:

Error HH604: Error running JSON-RPC server: Only HTTP(S) protocols are supported.

I can't use HTTPS because the endpoint is broken:

Error HH604: Error running JSON-RPC server: missing trie node 69198cb6c7d5488cd06aa4d6dab87f5a54aa53918c09b3b2c32b7e7a102d7d67 (path )

It's super annoying cause this setup also breaks Hardhat's solidity console.log feature. No logs show up in test processes.

I can't just use another node either, cause nobody seems to be hosting reasonably priced BSC archival nodes other than https://www.quicknode.com/

@alcuadrado
Copy link
Member

I don't think your comment is related to this issue, @EtDu. If you think you need to be able to fork a network using websocket, please open a separate issue.

@fvictorio
Copy link
Member

I can't use HTTPS because the endpoint is broken:

Error HH604: Error running JSON-RPC server: missing trie node 69198cb6c7d5488cd06aa4d6dab87f5a54aa53918c09b3b2c32b7e7a102d7d67 (path )

That doesn't mean that the endpoint is broken, it means that you are using a node that is not an archive node and you are forking from an old block number.

I can't just use another node either, cause nobody seems to be hosting reasonably priced BSC archival nodes other than https://www.quicknode.com/

GetBlock added BSC archive nodes recently btw.

Please use our discord server if you have more questions related to that, so we don't spam this issue.

@EtDu
Copy link

EtDu commented Jul 6, 2021

@fvictorio Thank you for the response, I'll open another issue for the WSS forking

@adamdry
Copy link
Contributor

adamdry commented Jul 19, 2021

As @fvictorio mentions, another issue I created might be a duplicate of this one. Issue #1692

Here is the very small repo (1 tiny contract and 1 very small test file) to reproduce the issue: https://github.com/adamdry/ethers-event-issue

For convenience, here is the smart contract:

//SPDX-License-Identifier: UNLICENSED;
pragma solidity 0.8.4;

contract ContractA {

    event TokensMinted(uint amount);

    function mint(uint amount) public {
        emit TokensMinted(amount);
    }

}

And this is my test code:

import * as chai from 'chai'
import { BigNumber, ContractTransaction } from 'ethers'
import { ethers } from 'hardhat'
import { ContractA, ContractAFactory } from '../typechain'

const expect = chai.expect

describe("Example test", function () {
    it("should fire the event", async function () {
        const [owner] = await ethers.getSigners();

        const contractAFactory = (await ethers.getContractFactory(
            'ContractA',
            owner,
        )) as ContractAFactory

        const contractA: ContractA = await contractAFactory.deploy()

        contractA.on('TokensMinted', (amount: BigNumber) => {
            // THIS LINE NEVER GETS HIT
            console.log('###########')
        })

        const contractTx: ContractTransaction = await contractA.mint(123)
        const contractReceipt: ContractReceipt = await contractTx.wait()

        for (const event of contractReceipt.events!) {
            console.log(JSON.stringify(event))
        }
    });
});

I was expecting the ########### to get printed to the console however it doesn't so the listener function isn't being executed for some reason.

If I dig into the ContractReceipt the correct event data is there:

{
  "transactionIndex": 0,
  "blockNumber": 2,
  "transactionHash": "0x55d118548c8200e5e6c19759d9aab56cb2e6a274186a92643de776d617d51e1a",
  "address": "0x5FbDB2315678afecb367f032d93F642f64180aa3",
  "topics": [
    "0x772f66a00a405709c30e7f18feadcc8f123b20c09c7260165d3eec36c9f21372"
  ],
  "data": "0x000000000000000000000000000000000000000000000000000000000000007b",
  "logIndex": 0,
  "blockHash": "0x808e6949118509b5a9e482e84cf47921a2fcffbcd943ebbd8ce4f6671469ee01",
  "args": [
    {
      "type": "BigNumber",
      "hex": "0x7b"
    }
  ],
  "event": "TokensMinted",
  "eventSignature": "TokensMinted(uint256)"
}

Hopefully this can help with solving the issue.

@DefiCake
Copy link

DefiCake commented Jul 20, 2021

Confirming that eth_subscribe seems broken, simple block subscription does not work.

I am spawning a vanilla hardhat node with npx hardhat node. Nothing fancy.

Then I have an advanced network that points to ws://localhost:8545. I issue npx hardhat test <my test file> --network advanced

First issue: can't access signers, only HTTP(S) supported

Second issue: accessing the provider via waffle or network.provider seems to point to http://localhost:8545. No websocket, thus no events (I am not sure of this)

Third issue: even if I instantiate a websocket provider with my local node, I am not receiving events. provider.on("block") is never triggered, but I can see the logs of new blocks being generated in the terminal where I spawned the hardhat node which supposedly is offering a websocket connection.

it('works', async () => {
    // const signer = (await ethers.getSigners())[0]
    // console.log(signer.provider) // Throws with TypeError: OnlyHTTP(S) protocols are supported
    const provider = new ethers.providers.WebSocketProvider('ws://localhost:8545')
    const alice = new Wallet('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', provider)
    const nft = await new FodlNFTFactory(alice).deploy('tst', 'tst')

    provider.on({}, (log, event) => {
      console.log('heee')
      console.log(log)
      console.log(event)
    })

    provider.on('block', (block) => {
      console.log('hooo')
      console.log(block)
    })

    const tx = await nft.mint(alice.address, 1)
    await tx.wait(1)
    await tx.wait(1)
  })

EDIT. Just out of curiosity I tried to put the hardhat node on automine + interval mining

The tx.wait(10 never gets resolved either, I guess that under the hood it is waiting for the subscription to clear 10 blocks

it('works', async () => {
    // const signer = (await ethers.getSigners())[0]
    // console.log(signer.provider) // Throws with TypeError: OnlyHTTP(S) protocols are supported
    const provider = new ethers.providers.WebSocketProvider('ws://localhost:8545')
    const alice = new Wallet('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', provider)
    const nft = await new FodlNFTFactory(alice).deploy('tst', 'tst')

    provider.on({}, (log, event) => {
      console.log('heee')
      console.log(log)
      console.log(event)
    })

    provider.on('block', (block) => {
      console.log('hooo')
      console.log(block)
    })

    const tx = await nft.mint(alice.address, 1)
    await tx.wait(10)
  })

EDIT 2. Confirmed as people above that code is working well with ganache-cli

@feuGeneA feuGeneA self-assigned this Jul 21, 2021
@feuGeneA
Copy link
Contributor

#1692 may be a duplicate of this

@fvictorio
Copy link
Member

We released hardhat@2.6.1, which should (finally!) fix this issue. Please let us know if you still notice problems related to websocket connections to the hardhat node after upgrading.

https://github.com/nomiclabs/hardhat/releases/tag/hardhat-core-v2.6.1

@adamdry
Copy link
Contributor

adamdry commented Aug 20, 2021

@fvictorio thanks for the update, unfortunately this hasn't fixed the issue for me.

I'm still testing this as per the code in #1692

I do have some additional information:
Before I upgraded from HH 2.4.3 I ran npx hardhat compile and then npx hardhat test as I haven't run the tests in this project for a while and I wanted to check that it still compiled and the tests passed. Amazingly the events were firing! I only ran the tests once (which was stupid, it would have been good to see if the events fired on the 2nd run). I then upgraded to 2.6.1 and ran npx hardhat compile and then npx hardhat test but the events didn't fire.

I tried rebooting my machine and trying again with no luck. I then downgraded back to 2.4.3 (deleting node_modules to ensure a fresh install) but also no luck. And then I rebooted my machine again for a fresh go with 2.4.3 (which worked last time) but that failed to fire the events again. I've no idea why they worked that one time! I am now unable to get them to fire at all.

Is there anything else I can do to try and help nail the problem down?

@fvictorio
Copy link
Member

(I responded to @adamdry here, in case anyone ends up reading this.)

@maraoz
Copy link

maraoz commented Sep 6, 2021

I was going to complain about this same issue until I realized I was on hardhat@2.6.0 🤦
Upgrading to hardhat@2.6.1 fixes the problem (in my case contract.on(EventName, ...) was not working (callback was not being called).
Thanks for the fix!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type:bug Something isn't working
Projects
None yet