Using Hardhat
npx hardhat init
// Note to use npm source + proxy
Install the vscode solidity plugin.
Installing Dependencies
pnpm i hardhat ethers @nomicfoundation/hardhat-chai-matchers @nomicfoundation/hardhat-ignition @nomicfoundation/hardhat-toolbox
Direct Compilation
npx hardhat compile
// npm script
"compile": "hardhat compile",
Deploying to Testnet
- Add network to
hardhat.config.ts
networks: {
testnet: {
url: `<rpc>`,
accounts: [
"<private key>",
],
},
},
- Add deploy script
"deploy": "hardhat ignition deploy ./ignition/modules/Lock.ts --network testnet"
Run npm run deploy
.
Verifying the Contract
- Add sourcify to
hardhat.config.ts
sourcify: {
enabled: true,
apiUrl: "...",
browserUrl: "...",
},
etherscan: {
enabled: false,
// apiKey: "..."
}
- Verification command
npx hardhat verify --network testnet 0x...
// npm script
"verify": "hardhat verify --network testnet 0x..."
If you deploy the same contract multiple times, you need to specify the file:
"verify": "hardhat verify --network testnet --contract contracts/xxx.sol:xxx 0x..."
Updating the Code
Solidity code cannot be directly upgraded; upgrades can only be done through a pre-designed proxy.
Update code => Compile => Write deployment file (.ts) => Update and run deployment script.
Add a new deployment ts file:
import { buildModule } from '@nomicfoundation/hardhat-ignition/modules'
const Test01Module = buildModule('<any-module-name>', m => {
const contract = m.contract('<contract-name>', [])
// const contract2 = m.contract("<contract-name-2>", []);
return { contract }
})
export default Test01Module