hardhat-deploy
A Hardhat Plugin For Replicable Deployments And Tests
Hardhat Deployment And Test Plugin.
What is it for ?
This hardhat plugin adds a mechanism to deploy contracts to any network, keeping track of them and replicating the same environment for testing.
It also adds a mechanism to associate names to addresses so test and deployment scripts can be reconfigured by simply changing the address a name points to, allowing different configurations per network. This also results in much clearer tests and deployment scripts (no more accounts[2]
in your code).
This plugin contains a lot more features too, all geared toward a better developer experience :
- chain configuration export, listing deployed contract, their abi and address, usefull for webapps.
- library linking at time of deployment
- deterministic deployment across networks
- ability to submit contract source to etherscan for verification. Because hardhat-deploy will save all necessary info, it can be executed at any time.
- deployment dependency system (allowing you to only deploy what is needed).
- deployment as migration so once a deployment is done, it can be set to never be executed again.
- deployment retrying (by saving pending tx): so you can feel confident when making a deployment that you can always recover.
- deployments as test fixture using
evm_snapshot
to speed up testing. - ability to create your own test fixture that automatically benefit from
evm_snapshot
to speed up tests using it. - combined with hardhat-deploy-ethers it has the ability to get ethers contract instance by name (like
await ethers.getContract("ContractName")
). - importing previously compiled contract (possibly in different solidity compiler version).
- importing artifact from external sources (like npm packages), including truffle support
- importing deployments from external sources (like npm packages), including truffle support
- ability to log information in
deploy
mode only (while in test the console remains clean). - contains helpers to read and execute transaction on deployed contract referring to them by name.
- These helpers contains options to auto mine on dev environment like ganache (to speed up deployments).
- save metadata of deployed contract so they can always be fully verified, via sourcify or etherscan.
- proxy deployment with ability to upgrade them transparently, only if code changes.
- diamond deployment with facets, allowing you to focus on what the new version will be. It will generate the diamondCut necessary to reach the new state.
- watch and deploy: hardhat-deploy can watch both your deploy script and contract code and redeploy on changes.
- HCR (Hot Contract Replacement): the watch feature combined with proxy or diamond, gives you an experience akin to frontend Hot Module Replacement: Once your contract change, the deployment is executed and your contract retain the same address and same state.
Installation
npm install hardhat-deploy
npm install -D hardhat-deploy
And add the following statement to your hardhat.config.js
:
import "hardhat-deploy";
TypeScript support
With hardhad the tsconfig.json is optional.
But if you add folders to the include
field in tsconfig.json
you ll also need to include hardhat.config.ts
like :
include": ["./hardhat.config.ts", "./scripts", "./deploy", "./test"]
for deploy script (see below) you can write them this way to benefit from typing :
import {
HardhatRuntimeEnvironment,
DeployFunction,
} from "hardhat/types";
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
// code here
};
export default func;
TODO update: See a full example of typescript usage here : https://github.com/wighawag/hardhat-deploy-ts-test
Migrating existing deployment to hardhat-deploy
You might want to switch your current deployment process to use hardhat-deploy. In that case you probably have some deployments saved elsewhere.
In order to port them to hardhat-deploy, you'll need to create one .json
file per contract in the deployments/<network>
folder (configurable via paths config).
The network folder is simply the hardhat network name (as configured in hardhat.config.js) (accessible at runtime via hre.network.name
).
Such folder need to have a file named .chainId
containing the chainId as decimal.
For example for network named "rinkeby" (for the corresponding network) the file deployments/rinkeby/.chainId
would be
4
Note, prior to hardhat 0.6 the chainId was appended to the folder name (expect for some known network name). This has changed and upgrading to 0.6 will require you to change the folder name and add the '.chainId' file.
Each contract file must follow this type (as defined in src/type-extensions.d.ts) :
export interface Deployment {
abi: any[];
address: string;
receipt?: Receipt;
transactionHash?: string;
contractFilepath?: string;
contractName?: string;
history?: Deployment[];
args?: any[];
linkedData?: any;
metadata?: string;
bytecode?: string;
deployedBytecode?: string;
userdoc?: any;
devdoc?: any;
methodIdentifiers?: any;
diamondCut?: { address: string; sigs: string[] }[];
facets?: { address: string; sigs: string[] }[];
storageLayout?: any;
}
As you can see, only abi and address are mandatory. But having the other fields allow more feature. For example, metadata and args allow you to benefit from contract code verification.
For Receipt, the following type is expected:
export type Receipt = {
from: string;
transactionHash: string;
blockHash: string;
blockNumber: number;
transactionIndex: number;
cumulativeGasUsed: string | number;
gasUsed: string | number;
contractAddress?: string;
to?: string;
logs?: Log[];
events?: any[];
logsBloom?: string;
byzantium?: boolean;
status?: number;
confirmations?: number;
};
Here is an example:
Let sey you have:
- 2 Contract named Greeter and Registry deployed on rinkeby
- 1 contract named Greeter on mainnet
- 2 Contract named Greeter and Registry deployed on a network named rinkeby2
You would get the following folder structure:
deployments/
mainnet/
.chainId
Greeter.json
rinkeby/
.chainId
Greeter.json
Registry.json
rinkeby2/
.chainId
Greeter.json
Registry.json
The reason why hardhat-deploy save chainId in the .chainId
file is both for
- safety: so that if you were to change the network to point to a different chain, it would not attempt to read the wrong folder and assume that a contract has been deployed while it has not.
- ability to know the chainId without requring to be connected to a node (and so not dependent on hardhat.config.js settings). Useful for
export
task.
Hardhat Tasks Availabled/Updated
hardhat deploy
This plugin adds the deploy task to Hardhat.
This task will execute the scripts in the deploy
folder and save the contract deployments to disk. These deployments are supposed to be saved for example in a git repository. This way they can be accessed later.
With the deployment saved, it allows you to deploy a contract only if changed were made.
Deploy scripts (also called Deploy functions) can also perform aribtrary logic.
For further details on how to use it and write deploy script, see section below.
Options
--export <filepath>
: export one file that contains all contracts (address, abi + extra data) for the network being invoked. The file contains the minimal information so to not bloat your frontend.
--export-all <filepath>
: export one file that contains all contracts across all saved deployment, regardless of the network being invoked.
--tags <tags>
: only excute deploy scripts with the given tags and their dependencies (see more info here about tags and dependencies)
--gasprice <gasprice>
: specify the gasprice to use by default for transactions executed via hardhat-deploy helpers in deploy scripts
--write <boolean>
: default to true (except for hardhat network). If true, write deployments to disk (in deployments path, see path config).
Flags
--reset
: This flag reset the deployments from scratch. Previously deployed contract are not considered and deleted from disk.
--silent
: This flag remove hardhat-deploy log output (see log function and log options for hre.deployments
)
--watch
: This flag make the task never ending, watching for file changes in the deploy scripts folder and the contract source folder. If any changes happen the contracts are recompiled and the deploy script are re-run. Combined with a proxy deployment (Proxies or Diamond) this allow to have HCR (Hot Contract Replacement).
hardhat node
This plugin modify the node task so that it also execute the deployment script before exposing the server http RPC interface
It also add the same options as the deploy task with the same functionality. It adds an extra flag:
--no-deploy
that discard all other options to revert to normal hardhat node
behavior without any deployment being performed.
Note that the deployments are saved as if the network name is localhost
. This is because hardhat node
is expected to be used as localhost: You can for example execute hardhat --network localhost console
after node
is running. Doing builder --network hardhat console
would indeed not do anythong useful. It still take the configuration from hardhat
in the hardhat.config.js file though.
hardhat test
This plugin add to the test task a flag argument --deploy-fixture
that run the global deployments fixture before the tests and snapshot it.
hardhat etherscan-verify
This plugin adds the etherscan-verify task to Hardhat.
This task will submit the contract source and other info of all deployed contracts to allow etherscan to verify and record the sources.
Instead of using the full solc input, this task will first attempt to send the minimal sources from the metadata.
But Etherscan sometime fails due to a bug in solidity compiler (https://github.com/ethereum/solidity/issues/9573). As such this task can fallback on full solc input (see option --solc-input). Note that if your contract was deployed with a previous version of hardhat-deploy, it might not contains the full information.
This task will also attempt to automatically find the SPDX license in the source.
To execute that task, you need to specifiy the network to run against :
hardhat --network mainnet etherscan-verify --api-key <apikey>
Options
--api-key <API_KEY>
: Etherscan API key. Optional as such api key can also be set by setting the environment variable : ETHERSCAN_API_KEY
--license <SPDX>
: specifiy the license to use (using SPDX id) or can be "UNLICENSED". Note that etherscan have limited support for licenses, see : https://etherscan.io/contract-license-types
Flags
--solcInput
: allow the use of full solc-input as fallback. Note that the whole source folder will be included in the result of the verification on etherscan.
--force-license
: This flag force the use of the license specified by --license (useful if your inline SPDX are not compatible with etherscan list)
hardhat export
This plugin adds the export task to Hardhat.
This task will export the contract deployed (saved in deployments
folder) to a simple format containing only contract addresses and abi, useful for web apps.
One of the following options need to be set for this task to have any effects :
Options
--export <filepath>
: export one file that contains all contracts (address, abi + extra data) for the network being invoked. The file contains the minimal information so to not bloat your frontend.
--export-all <filepath>
: export one file that contains all contracts across all saved deployment, regardless of the network being invoked.
This last option has some limitations, when combined with the use of external deployments (see Configuration). If such external deployments were using older version of hardhat-deploy or truffle, the chainId might be missing. In order for these to be exported, the hardhat network config need to explicity state the chainId in the networks
config of hardhat.config.js
.
Hardhat Environment Extensions
This plugin extends the Hardhat Runtime Environment by adding 4 fields:
-
getNamedAccounts: () => Promise<{ [name: string]: string }>
: a function returning an object whose keys are names and values are addresses. It is parsed from the namedAccounts
configuration (see Configuration).
-
getUnamedAccounts: () => Promise<string[]}>
: accounts which has no names, useful for test where you want to be sure that the account is not one of the predefined one
-
deployments
: contains functions to access past deployments or to save new ones, as well as helpers functions.
-
getChainId(): Promise<string>
: offer an easy way to fetch the current chainId.
Configuration
namedAccounts (ability to name addresses)
This plugin extends the HardhatConfig
's object with an optional namedAccounts
field.
namedAccounts
allows you to associate names to addresses and have them configured per chain.
This allows you to have meaningful names in your tests while the addresses match to multi sig in real network for example.
{
namedAccounts: {
deployer: {
default: 0,
1: 0,
4: '0xA296a3d5F026953e17F472B497eC29a5631FB51B',
"goerli": '0x84b9514E013710b9dD0811c9Fe46b837a4A0d8E0',
},
feeCollector:{
default: 1,
1: '0xa5610E1f289DbDe94F3428A9df22E8B518f65751',
4: '0xa250ac77360d4e837a13628bC828a2aDf7BabfB3',
}
}
}
hardhat-deploy add 2 new fields to networks
configuration
live
: this is not used internally but is useful to perform action on a network whether it is a live network (rinkeby, mainnet, etc) or a temporary one (localhost, hardhat). The default is true (except for localhost and hardhat where the default is false).
saveDeployments
: this tell whether hardhat-deploy should save the deployments to disk or not. Default to true.
tags
: network can have tags to represent them. The config is an array and at runtime the hre.network.tags is an object whose fields (the tags) are set to true.
This is useful to conidtionaly operate on network based on their use case.
Example:
{
networks: {
localhost: {
live: false,
saveDeployments: true,
tags: ["local"]
},
hardhat: {
live: false,
saveDeployments: true,
tags: ["test", "local"]
},
rinkeby: {
live: true,
saveDeployments: true,
tags: ["staging"]
}
}
}
It also adds fields to HardhatConfig
's ProjectPaths
object.
Here is an example showing the default values :
{
paths: {
deploy: 'deploy',
deployments: 'deployments',
imports: `imports`
}
}
The deploy folder is expected to contains the deploy script that are executed upon invocation of hardhat deploy
or hardhat node
The deployment folder will contains the resulting deployments (contract addresses along their abi, bytecode, metadata...). One folder per network and one file per contract.
The imports folder is expected to contains artifacts that were pre-compiled. Useful if you want to upgrade to a new solidity version but want to keep using previously compiled contracts. The artifact is the same format as normal hardhat artifact, so you can easily copy them over, before switching to a new compiler version.
Importing deployment from other projects (truffle support too)
It also add the external
field to HardhatConfig
Such fiels allows to specify paths for external artifacts or deployments. The use of the paths
field is not possible because hardhat expects all paths field to be string. It does not accept arrays or objects, see https://github.com/nomiclabs/hardhat/issues/776.
The external object has 3 fields:
{
external: {
artifacts: ["node_modules/@cartesi/arbitration/build/contracts"],
deployments: {
rinkeby: ["node_modules/@cartesi/arbitration/build/contracts"],
},
deploy: ["node_modules/somepackage/deploy"]
}
}
The artifacts fields specify an array of path to look for artifact. it support both hardhat and truffle artifacts.
The deployments fields specify an object whose field name are the hardhat network and the value is an array of path to look for deployments. It supports both hardhat-deploy and truffle formats.
The deploy fields specify an array of path that deploy scripts to execute. This allow project to share the deployment procedure. A boon for developer aiming at integrating it as they can get the contracts to be deployed for testing locally.
Access to Artifacts (non-deployed contract code and abi)
you can access contract artifact via getArtifact
function :
const { deployments } = require("hardhat");
const artifact = await deployments.getArtifact(artifactName);
With the hardhat-deploy-ethers
plugin you can get your ethers contract via :
const { deployments, ethers } = require("hardhat");
const factory = await ethers.getContractFactory(artifactName);
Note that the artifact file need to be either in artifacts
folder that hardhat generate on compilation or in the imports
folder where you can store contracts compiled elsewhere. They can also be present in the folder specified in external.artifacts
see Importing deployment from other projects
How to Deploy Contracts
The deploy
Task
hardhat --network <networkName> deploy [options and flags]
This is a new task that the plugin adds. As the name suggests it deploys contracts.
To be exact it will look for files in the folder deploy
or whatever was configured in paths.deploy
, see paths config
It will scan for files in alphabetical order and execute them in turn.
- it will
require
each of these files and execute the exported function with the HRE as argument
Note that running hardhat deploy
without specifying a network will use the default network. If the default network is an internal ganache or hardhat then nothing will happen as a result but this can be used to ensure the deployment is without issues.
To specified the network, you can use the builtin hardhat argument --network <network name>
or set the env variable HARDHAT_NETWORK
Deploy Scripts
The deploy scripts need to be of the following type :
export interface DeployFunction {
(env: HardhatRuntimeEnvironment): Promise<void | boolean>;
skip?: (env: HardhatRuntimeEnvironment) => Promise<boolean>;
tags?: string[];
dependencies?: string[];
runAtTheEnd?: boolean;
id?: string;
}
The skip function can be used to skip executing the script under whatever condition. It simply need to resolve a promise to true.
The tags is a list of string that when the deploy task is executed with, the script will be executed (unless it skips). In other word if the deploy task is executed with a tag that does not belong to that script, that script will not be executed unless it is a dependency of a script that does get executed.
The dependencies is a list of tag that will be executed if that script is executed. So if the script is executed, every script whose tag match any of the dependency will be executed first.
The runAtTheEnd
is a boolean that if set to true, will queue that script to be executed after all other scripts are executed.
These set of fields allow more flexibility to organize the script. You are not limited to alphabetical order.
Finally the function can return true if it wishes to never be executed again. This can be usfeul to emulate migration scripts that are meant to be executed only once. Once such script return true (async), the id
field is used so to track execution and if that field is not present when the script return true, it will fails.
In any case, as a general advice every deploy function should be idempotent. This is so they can always recover from failure or pending transaction.
This is why the hre.deployments.deploy
function will by default only deploy if the contract code has changed, making it easier to write idempotent script.
An example of a deploy script :
module.exports = async ({
getNamedAccounts,
deployments,
getChainId,
getUnamedAccounts
}) => {
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
await deploy("GenericMetaTxProcessor", {
from: deployer,
gas: 4000000,
args: []
});
};
As you can see the HRE passed in has 4 new fields :
-
getNamedAccounts
is a function that returns a promise to an object whose keys are names and values are addresses. It is parsed from the namedAccounts
configuration (see namedAccounts
).
-
getUnamedAccounts
: function that return a promise to an array of accounts (which were not used in getNamedAccounts
), useful for test where you want to be sure that the account is not one of the predefined one
-
deployments
, which contains functions to access past deployments or to save new ones, as well as helpers functions.
-
getChainId
which return a promise for the chainId
The deploynments field contains the deploy
function taht allow you to deploy contract and save them. It contains a lot more functions though :
The deployments
field
The deployments field contains several helpers function to deploy contract but also execute transaction.
deploy(name: string, options: DeployOptions): Promise<DeployResult>;
diamond: {
deploy(name: string, options: DiamondOptions): Promise<DeployResult>;
};
deterministic(
name: string,
options: Create2DeployOptions
): Promise<{
address: Address;
deploy(): Promise<DeployResult>;
}>;
fetchIfDifferent(name: string, options: DeployOptions): Promise<boolean>;
save(name: string, deployment: DeploymentSubmission): Promise<void>;
get(name: string): Promise<Deployment>;
getOrNull(name: string): Promise<Deployment | null>;
all(): Promise<{ [name: string]: Deployment }>;
getArtifact(name: string): Promise<Artifact>;
run(
tags?: string | string[],
options?: {
resetMemory?: boolean;
deletePreviousDeployments?: boolean;
writeDeploymentsToFiles?: boolean;
export?: string;
exportAll?: string;
}
): Promise<{ [name: string]: Deployment }>;
fixture(tags?: string | string[]): Promise<{ [name: string]: Deployment }>;
createFixture(func: FixtureFunc, id?: string): () => Promise<any>;
log(...args: any[]): void;
execute(
name: string,
options: TxOptions,
methodName: string,
...args: any[]
): Promise<Receipt>;
rawTx(tx: SimpleTx): Promise<Receipt>;
read(
name: string,
options: CallOptions,
methodName: string,
...args: any[]
): Promise<any>;
read(name: string, methodName: string, ...args: any[]): Promise<any>;
deployments.deploy
The deploy function as mentioned allow you to deploy a contract and save it under a specific name.
The deploy function expect 2 parameters: one for the name and one for the options
See below the full list of fields that the option parameter allows and requires:
from: string;
contract?:
| string
| {
abi: ABI;
bytecode: string;
deployedBytecode?: string;
};
args?: any[];
skipIfAlreadyDeployed?: boolean;
log?: boolean;
linkedData?: any;
libraries?: { [libraryName: string]: Address };
proxy?: boolean | string | ProxyOptions;
gasLimit?: string | number | BigNumber;
gasPrice?: string | BigNumber;
value?: string | BigNumber;
nonce?: string | number | BigNumber;
estimatedGasLimit?: string | number | BigNumber;
estimateGasExtra?: string | number | BigNumber;
dev_forceMine?: boolean;
deterministicDeployment? boolean | string;
Handling contract using libraries
In the deploy function, one of the DeployOptions
that can be passed into the function is the libraries
field.
First, deploy the library using the deploy
function, then when we deploy a contract that needs the the linked library, we can pass the deployed library name and address in as an argument to the libraries
object.
const exampleLibrary = await deploy("ExampleLibary", {
from: <deployer>
});
ExampleLibrary is now deployed to whatever network is in the context of the environment.
For example, if we are deploying on Rinkeby, this library will get deployed on rinkeby, and the exampleLibrary
variable will be an deployment object that contains the abi as well as the deployed address for the contract.
Now that the library is deployed, we can link it in our next deployed contract.
const example = await deploy("Example", {
from: <deployer>
args: ["This is an example string argument in the constructor for the 'Example' contract"],
libraries: {
ExampleLibrary: exampleLibrary.address
}
});
This libraries
object takes the name of the library, and its deployed address on the network. Multiple libraries can be passed into the libraries
object.
Exporting Deployments
Apart from deployments saved in the deployments
folder which contains all information available about the contract (compile time data + deployment data), hardhat-deploy
allows you to export lightweight file.
These can be used for example to power your frontend with contract's address and abi.
This come into 2 flavors.
The first one is exported via the --export <file>
option and follow the following format :
export interface Export {
chainId: string;
name: string;
contracts: { [name: string]: ContractExport };
}
where name is the name of the network configuration chosen (see hardhat option --network
)
The second one is exported via the --export-all <file>
option and follow the following format :
export type MultiExport = {
[chainId: string]: { [name: string]: Export }
};
As you see the second format include the previous. While in most case you'll need the single export where your application will support only one network, there are case where your app would want to support multiple network at nonce. This second format allow for that.
Furthermore as hardhat support multiple network configuration for the same network (rinkeby, mainnet...), the export-all format will contains each of them grouped by their chainId.
Deploying and Upgrading Proxies
As mentioned above, the deploy function can also deploy a contract through a proxy. It can be done without modification of the contract as long as it does not have a constructor (or constructor with zero arguments).
The Proxy is both ERC-1967 and ERC-173 Compliant
Code can be found here
To perform such proxy deployment, you just need to invoke the deploy function with the following options : {..., proxy: true}
See example :
module.exports = async ({ getNamedAccounts, deployments, getChainId }) => {
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
await deploy("Greeter", {
from: deployer,
proxy: true
});
};
You can also set it to proxy: "<upgradeMethodName>"
in which case the function <upgradeMethodName>
will be executed upon upgrade.
the args
field will be then used for that function instead of the contructor. It is also possible to then have a constructor with the same arguments and have the proxy be disabled. It can be useful if you want to have your contract as upgradeable in a test network but be non-upgradeable on the mainnet. Further documentation need to written for that.
See example :
module.exports = async ({ getNamedAccounts, deployments, getChainId }) => {
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
await deploy("Greeter", {
from: deployer,
proxy: "postUpgrade",
args: ["arg1", 2, 3]
});
};
The proxy option can also be an object which can set the specific owner that the proxy is going to be managed by.
See example:
module.exports = async ({ getNamedAccounts, deployments, getChainId }) => {
const { deploy } = deployments;
const { deployer, greeterOwner } = await getNamedAccounts();
await deploy("Greeter", {
from: deployer,
proxy: {
owner: greeterOwner,
methodName: "postUpgrade"
},
args: ["arg1", 2, 3]
});
};
Note that for the second invokation, this deployment will fails to upgrade the proxy as the from
which is deployer
is not the same as the proxy's owner : greeterOwner
To make it work, you have to create a new script that have for from
field: greeterOwner
. If such value is a a multi sig or an address not registered as part of hardhat signers, the tx will not be executed but instead an error will be throw, mentionning the tx data necessary to perform the upgrade.
Builtin-In Support For Diamonds (EIP2535)
The deployments field also expose the diamond field: hre.deployments.diamond
that let you deploy Diamonds in an easy way.
Instead of specifying the facets to cut out or cut in, which the diamond contract expects, you specify the facets you want to end up having on the deployed contract.
diamond.deploy
expect the facet as names. The name represent the compiled contract name that going to be deployed as facet. In future version you ll be able to specify deployed contract or artifact object as facet.
To deploy a contract with 3 facet you can do as follow :
module.exports = async ({ getNamedAccounts, deployments, getChainId }) => {
const { diamond } = deployments;
const { deployer, diamondAdmin } = await getNamedAccounts();
await diamond.deploy("ADiamondContract", {
from: deployer,
owner: diamondAdmin,
facets: ["Facet1", "Facet2", "Facet3"]
});
};
if you then later execute the following script:
module.exports = async ({ getNamedAccounts, deployments, getChainId }) => {
const { diamond } = deployments;
const { deployer, diamondAdmin } = await getNamedAccounts();
await diamond.deploy("ADiamondContract", {
from: diamondAdmin,
owner: diamondAdmin,
facets: ["NewFacet", "Facet2", "Facet3"]
});
};
Then the NewFacet will be deployed automatically if needed and then the diamondCut will cut Facet1 out and add NewFacet.
Note that if the code for Facet2 and Facet3 changes, they will also be redeployed automatically and the diamondCuts will replace the existing facets with these new ones.
Note that The Diamond contract's code is part of hardhat-deploy and contains 3 built-in facet that can be removed manually if desired.
These facets are used for ownership, diamondCut and diamond loupe.
The implementation is the reference implementation by Nick Mudge
Like normal proxies you can also execute a function at the time of an upgrade.
This is done by specifying the execute field in the diamond deploy options :
diamond.deploy("ADiamondContract", {
from: deployer,
owner: diamondAdmin,
facets: ["NewFacet", "Facet2", "Facet3"],
execute: {
methodName: "postUpgrade",
args: ["one", 2, "0x3"]
}
});
Since the diamond standard has no builtin mechanism to make the deployment of Diamond with function execution, the Diamond when deployed is actually deployed through a special contract, the Diamantaire
(see code here) that act as factory to build Diamond. It uses deterministic deployment for that so, it is transparently managed by hardhat-deploy.
The Diamantaire also support the deterministic deployment of Diamond.
An extra field can be passed to the Diamond deployment options : deterministicSalt
. It has to be a non-zero 32bytes string (in hex format).
Note that if you want to deploy 2 diamonds with same owner, you'll need 2 different deterministicSalt for them to be 2 separate contracts.
Testing Deployed Contracts
You can continue using the usual test task :
hardhat test
Tests can then use the hre.deployments.fixture
function to run the deployment for the test and snapshot it so that tests don't need to perform all the deployments transaction every time, they simply reuse the snapshot for every test (this leverages evm_snapshot
and evm_revert
provided by both hardhat
and ganache
). You can for example set them in a beaforeEach
.
Here is an example of a test :
const { deployments } = require("hardhat");
describe("Token", () => {
beforeEach(async () => {
await deployments.fixture();
});
it("testing 1 2 3", async function() {
const Token = await deployments.get("Token");
console.log(Token.address);
const ERC721BidSale = await deployments.get("ERC721BidSale");
console.log({ ERC721BidSale });
});
});
If the deployment scripts are complex, the first test could take while (as the fixture need to execute the deployment) but then from the second test onward, the deployments are never re-executed, instead the fixture will do evm_revert
and test will run far faster.
Tests can also leverage named accounts for clearer test. Combined with hardhat-deploy-ethers
plugin, you can write succint test :
const { ethers, getNamedAccounts } = require("hardhat");
describe("Token", () => {
beforeEach(async () => {
await deployments.fixture();
});
it("testing 1 2 3", async function() {
const { tokenOwner } = await getNamedAccounts();
const TokenContract = await ethers.getContract("Token", tokenOwner);
await TokenContract.mint(2);
});
});
Furthermore, tests can easily create efficient fixture using deployments.createFixture
See example :
const setupTest = deployments.createFixture(async ({deployments, getNamedAccounts, ethers}, options) => {
await deployments.fixture();
const { tokenOwner } = await getNamedAccounts();
const TokenContract = await ethers.getContract("Token", tokenOwner);
await TokenContract.mint(10);
return {
tokenOwner: {
address: tokenOwner,
TokenContract
}
};
};
describe("Token", () => {
it("testing 1 2 3", async function() {
const {tokenOwner} = await setupTest()
await tokenOwner.TokenContract.mint(2);
});
});
While this example is trivial, some fixture can requires several transaction and the ability to snapshot them automatically speed up the tests greatly.
More Information On Hardhat Tasks
node task
as mentioned above, the node task is slighly modified and augmented with various flag and options
hardhat node
In particulat It adds an argument --export
that allows you to specify a destination file where the info about the contracts deployed is written.
Your webapp can then access all contracts information.
test task
hardhat test
the test task is augmented with one flag argument --deploy-fixture
that allow to run all deployments in a fixture snapshot before executing the tests. This can speed up tests that use specific tags as the global fixture take precedence (unless specified).
In other word tests can use deployments.fixture(<specific tag>)
where specific tag only deploy the minimal contracts under tests, while still benefiting from global deployment snapshot if used.
If a test need the deployments to only include the specific deployment specified by the tag, it can use the following :
deployments.fixture("<specific tag>", { fallbackToGlobal: false });
Due to how snapshot/revert works in hardhat, this means that these test will not be able to benefit from the global fixture snapshot and will have to deploy their contract as part of the fixture call. This is automatix but means that these tests will run slower.
run task
hardhat --network <networkName> run <script>
The run task act as before but thanks to the hre.deployments
field it can access deployed contract :
const hre = require("hardhat");
const { deployments, getNamedAccounts } = hre;
(async () => {
console.log(await deployments.all());
console.log({ namedAccounts: await getNamedAccounts() });
})();
You can also run it directly from the command line as usual.
HARDHAT_NETWORK=rinkeby node <script>
is the equivalent except it does not load the hardhat environment twice (which the run task does)
console task
hardhat console
The same applies to the console
task.
Deploy Scripts: Tags And Dependencies
It is possible to execute only specific part of the deployments with hardhat deploy --tags <tags>
Tags represent what the deploy script acts on. In general it will be a single string value, the name of the contract it deploys or modifies.
Then if another deploy script has such tag as a dependency, then when this latter deploy script has a specific tag and that tag is requested, the dependency will be executed first.
Here is an example of two deploy scripts :
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deployIfDifferent, log } = deployments;
const namedAccounts = await getNamedAccounts();
const { deployer } = namedAccounts;
const deployResult = await deploy("Token", {
from: deployer,
args: ["hello", 100]
});
if (deployResult.newlyDeployed) {
log(
`contract Token deployed at ${deployResult.contract.address} using ${deployResult.receipt.gasUsed} gas`
);
}
};
module.exports.tags = ["Token"];
module.exports = async function({ getNamedAccounts, deployments }) {
const { deployIfDifferent, log } = deployments;
const namedAccounts = await getNamedAccounts();
const { deployer } = namedAccounts;
const Token = await deployments.get("Token");
const deployResult = await deploy("Sale", {
from: deployer,
contract: "ERC721BidSale",
args: [Token.address, 1, 3600]
});
if (deployResult.newlyDeployed) {
log(
`contract Sale deployed at ${deployResult.contract.address} using ${deployResult.receipt.gasUsed} gas`
);
}
};
module.exports.tags = ["Sale"];
module.exports.dependencies = ["Token"];
As you can see the second one depends on the first. This is because the second script depends on a tag that the first script registers as using.
With that when hardhat deploy --tags Sale
is executed
then both scripts will be run, ensuring Sale is ready.
You can also define the script to run after another script is run by setting runAtTheEnd
to be true. For example:
module.exports = async function({ getNamedAccounts, deployments }) {
const { deployIfDifferent, execute, log } = deployments;
const namedAccounts = await getNamedAccounts();
const { deployer, admin } = namedAccounts;
await execute("Sale", { from: deployer }, "setAdmin", admin);
};
module.exports.tags = ["Sale"];
module.exports.runAtTheEnd = true;
Tags can also be used in test with deployments.fixture
. This allow you to test a subset of the deploy script.