Ta cấu hình Hardhat thông qua file hardhat.config.js.

Networks Configuration

Có hai loại mạng ở trong Hardhat:

  • Built-in local network như đã biết.
  • Các mạng liên kết đến một node bên ngoài: node này có thể là local (chẳng hạn như Ganache1) hoặc là remote (chẳng hạn như Infura hay Alchemy).

Ví dụ cấu hình Ganache network:

const config: HardhatUserConfig = {
	networks: {
		ganache: {
			url: "http://127.0.0.1:7545"
		}
	}
}
 
export default config;

Ví dụ cấu hình Sepolia network:

import * as dotenv from 'dotenv'; dotenv.config()
const { INFURA_API_KEY, PRIVATE_KEY } = process.env;
 
const config: HardhatUserConfig = {
	networks: {
		ganache: {
			url: "http://127.0.0.1:7545"
		},
	    sepolia: {
	      url: `https://sepolia.infura.io/v3/${INFURA_API_KEY}`,
	      accounts: [PRIVATE_KEY as string]
	    },
	}
}
 
export default config;

Seealso

Ta nên khai báo API của Sepolia ở trong file .env, xem thêm Infura Provider.

Câu lệnh deploy:

yarn hardhat run scripts/deploy.ts --network <network name>

Other Configurations

Các cấu hình có thể có ở trong file cấu hình là:

const config: HardhatUserConfig = {
    solidity: {
        version: "0.5.15",
        settings: {
            optimizer: {
                enabled: true,
                runs: 200
            }
        }
    },
    paths: {
        sources: "./contracts",
        tests: "./test",
        cache: "./cache",
        artifacts: "./artifacts"
    },
    mocha: {
        timeout: 40000
    }
}
 
export default config;

Cụ thể:

  • Thuộc tính solidity chủ yếu là để cấu hình cho compiler.
  • Thuộc tính paths là để cấu hình đường dẫn đến các thư mục.
  • Thuộc tính mocha là để cấu hình Mocha.

Footnotes

  1. Xem thêm Ganache.