hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

Request: HardHat network can emit specific blocks to subscribers

Open CodeForcer opened this issue 4 years ago • 3 comments
trafficstars

A typical setup for a production trading application is to subscribe to the blockchain with eth_subscribe, and process new blocks which are emitted, before deciding whether to make transactions.

For the purposes of back-testing DeFi applications, it would be extremely useful to be able to emit blocks to applications which are subscribed to your local HardHat network.

I believe most of the architecture necessary for this feature is already completed thanks to the main-net forking feature of HardHat. However, currently it doesn't seem to emit blocks to subscribers when you fork from a specific block.

The ideal work-flow would be (potentially):

  1. Start HardHat locally:
npx hardhat node --fork <archive node url>
  1. Connect applications to HardHat using eth_subscribe
  2. Run a HardHat command for main-net forking at a block:
curl -H "Content-Type: application/json" -X POST --data \
        '{"id":1337,"jsonrpc":"2.0","method":"hardhat_reset","params":["11297907"]}' \
        http://localhost:8545
  1. HardHat network emits the desired block to all subscribers

This feature would allow DeFi traders to seamlessly integrate production applications for backtesting alongside smart contracts written in HardHat.

Thank you for your excellent development toolkit!

CodeForcer avatar Nov 29 '20 13:11 CodeForcer

@CodeForcer if I understand this correctly, you want the hardhat network to emit a block that is already mined in the mainnet. If that's the case, I think a slightly different way of doing this would be this (assuming you want to mine block 100):

  1. Fork mainnet from block 99
  2. Call an rpc method that advances the fork one block

Would that work for your use case?

fvictorio avatar Dec 17 '20 18:12 fvictorio

@fvictorio sorry about late reply, just saw this.

That would be great, but right now it suffers from the above issue that subscribers to the RPC method do not receive the new blocks. So you call the rpc method which advances the fork by a block, but nothing is actually emitted to the subscribers.

With this ability in HardHat, I would be able to deprecate Ganache entirely for local simulation and solely use HardHat.

CodeForcer avatar Jan 21 '21 22:01 CodeForcer

+1

0xDualCube avatar Jul 25 '22 16:07 0xDualCube

Hi, I'm not sure what to do about this issue.

As far as I can tell, eth_subscribe does work. And the part about simulating a new block from the fork can be done with this plugin (I haven't tried it though).

I think I'm going to tentatively close this, but please let me know if we should have a follow-up issue for some aspect of this.

fvictorio avatar Dec 30 '22 10:12 fvictorio