hardhat-address-exporter
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

hardhat-address-exporter

A plugin for hardhat that exports deployed contract addresses into typescript files. It is multichain compatible.

Installation

npm install hardhat-address-exporter @nomiclabs/hardhat-ethers ethers hardhat

Import the plugin in your hardhat.config.js:

require("hardhat-address-exporter");

Or if you are using TypeScript, in your hardhat.config.ts:

import "hardhat-address-exporter";

Also import the plugin in the scripts you are using it as well.

Required plugins

Environment extensions

This plugin extends the Hardhat Runtime Environment by adding an addressExporter field whose type is AddressExporterHardhatRuntimeEnvironmentField. See Usage below for more information on how to use it.

Configuration

This plugin extends the HardhatUserConfig object with an optional addressExporter field.

This is an example of how to set it. It also shows the default values:

module.exports = {
  addressExporter: {
    outDir: path.resolve('./addresses'),
    runPrettier: false,
  }
};

Usage

Hardhat

After deploying your contracts in a script (e.g. scripts/deploy.ts), call hre.addressExporter.save(...) with an object of contract-names (keys) and contract-addresses (values). The chain it's deployed on is automatically taken from hre.network.config.chainId and used as a filename for the addresses object.

This is an example:

const ContractName = await ethers.getContractFactory('ContractName')
const contract = await ContractName.deploy()
await contract.deployed()

console.log('ContractName deployed to:', contract.address)

await hre.addressExporter.save({
  ContractName: contract.address,
})

This will result in two files being created in the outDir defined above:

1337.ts

/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */

export const ContractAddresses_1337 = {
  ContractName: '0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0',
}

addresses.ts

/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */

import { ContractAddresses_1337 } from './1337'
export type ContractAddressesKey = keyof typeof ContractAddresses
export const ContractAddresses = {
  1337: ContractAddresses_1337,
}

This assumes that you've deployed on chain id 1337.

Frontend

Now in your frontend you can import this as a type-safe object:

import { ContractAddresses, ContractAddressesKey } from 'addresses/addresses'

const chainId: ContractAddressesKey = 1337
const address = ContractAddresses[chainId].ContractName

Package Sidebar

Install

npm i hardhat-address-exporter

Weekly Downloads

45

Version

0.0.4

License

MIT

Unpacked Size

20.9 kB

Total Files

22

Last publish

Collaborators

  • dennis_zoma