YYSuni
cover

Solidity Note

Quickly deploy and verify contracts using Hardhat.

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

  1. Add network to hardhat.config.ts
networks: {
    testnet: {
      url: `<rpc>`,
      accounts: [
        "<private key>",
      ],
    },
},
  1. Add deploy script
"deploy": "hardhat ignition deploy ./ignition/modules/Lock.ts --network testnet"

Run npm run deploy.

Verifying the Contract

  1. Add sourcify to hardhat.config.ts
sourcify: {
    enabled: true,
    apiUrl: "...",
    browserUrl: "...",
},
etherscan: {
    enabled: false,
    // apiKey: "..."
}
  1. 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