Deploy a Smart Contract and Verify
Last updated
npm install --save-dev hardhat ts-node typescript @nomicfoundation/hardhat-toolbox ethers@^6.1.0 dotenv
npm install --save-dev @nomicfoundation/hardhat-verifynpx hardhatcd contracts
rm Lock.sol
nano Hello.sol// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Hello {
function hi() public pure returns (string memory) {
return "Hey there!";
}
}cd ..
nano hardhat.config.ts import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import "@nomicfoundation/hardhat-verify";
import * as dotenv from "dotenv";
dotenv.config();
const config: HardhatUserConfig = {
etherscan: {
apiKey: {
ernscan: "ernscan", // apiKey is not required, just set a placeholder
},
customChains: [
{
network: "ernscan",
chainId: 233,
urls: {
apiURL: "https://api.routescan.io/v2/network/testnet/evm/233/etherscan",
browserURL: "https://testnet.ernscan.io"
}
}
]
},
networks: {
ernscan: {
url: 'https://testnet.ethernitychain.io',
accounts: [process.env.PRIVATE_KEY!]
},
},
solidity: "0.8.0",
};
export default config;nano .envPRIVATE_KEY=a43b28caca8..................npx hardhat compilemkdir scripts
nano scripts/deploy.tsimport { ethers } from "hardhat";
async function main() {
const Hello = await ethers.getContractFactory("Hello");
const hello = await Hello.deploy();
await hello.waitForDeployment();
console.log("Hello deployed to:", await hello.getAddress());
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
npx hardhat run scripts/deploy.ts --network ernscannpx hardhat verify --network ernscan <deployed_contract_address>