hardhat
hardhat copied to clipboard
Request: HardHat network can emit specific blocks to subscribers
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):
- Start HardHat locally:
npx hardhat node --fork <archive node url>
- Connect applications to HardHat using
eth_subscribe - 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
- 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 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):
- Fork mainnet from block 99
- Call an rpc method that advances the fork one block
Would that work for your use case?
@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.
+1
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.