Hardhat – How to Re-use Previously Deployed Contract with Hardhat and Brownie

browniehardhathardhat-deploy

In brownie there's a very handy syntax to avoid deploying multiple times the same contract on-chain, while developing and iterating fast:

MyContract.deploy()


# somewhere else, sometime later:

MyContract[-1]  # last known address of YourContract deployed

In this way it's easy to check whether there's a version of your contract already deployed, and if there is you just re-use it instead of going through deploying again. That's particularly useful for Mocks, for instance, as they hardly change at all and it's just a waste of time (and gas… even if on testnet) to re-deploy them every run.

Is there a similar behaviour in Hardhat? Or I need to manually keep track of contract addresses as they're deployed, and re-instantiate the objects in later runs?

const contractAddress = "0x...";  // do I have to manually keep track of this address?
const myContract = await ethers.getContractAt("MyContract", contractAddress);

Best Answer

Turns out that the answer is a Hardhat plugin, hardhat-deploy, available here.

This hardhat plugin adds a mechanism to deploy contracts to any network, keeping track of them and replicating the same environment for testing.