Files
hardhat-simple-storage/scripts/deploy.js
2024-08-03 00:08:10 +08:00

95 lines
3.1 KiB
JavaScript

// imports
const { ethers, run, network } = require("hardhat");
// async main
async function main() {
const verifyOnly = false;
if (verifyOnly == true) {
const address = "0xF90F7edF515001CD4293255bfaF7D17ffa415e91";
const ETHERSCAN_API_KEY_VERIFY = process.env.ETHERSCAN_API_KEY;
console.log("Network config:", network.config);
console.log("ChainId: " + network.config.chainId);
if (network.config.chainId === 11155111) {
console.log("Currently on Sepolia Testnet");
if (ETHERSCAN_API_KEY_VERIFY !== undefined) {
console.log("Verifying contract...");
await verify(address, []);
console.log("Verification complete");
} else {
console.log("ETHERSCAN_API_KEY is undefined");
}
}
if (true) return;
}
const simpleStorageFactory =
await ethers.getContractFactory("SimpleStorage");
console.log("Deploying contract...");
const simpleStorage = await simpleStorageFactory.deploy();
await simpleStorage.deployed();
console.log("Deployed contract to: " + simpleStorage.address);
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY;
// No need verify for local hardhat network
// 31337 - Local Hardhat Network
// 5777 - Local Ganache Network
// 11155111 - Sepolia Testnet
console.log("Network config:", network.config);
console.log("ETHERSCAN_API_KEY:", ETHERSCAN_API_KEY);
// Need to delete artifacts & cache folder first
if (network.config.chainId === 31337) {
console.log("Currently on Local Hardhat");
}
if (network.config.chainId === 11155111) {
console.log("Currently on Sepolia Testnet");
if (ETHERSCAN_API_KEY !== undefined) {
console.log("Waiting for 6 block confirmations...");
await simpleStorage.deployTransaction.wait(6);
console.log("Verifying contract...");
await verify(simpleStorage.address, []);
console.log("Verification complete");
} else {
console.log("ETHERSCAN_API_KEY is undefined");
}
} else {
console.log("Not on Sepolia Testnet");
}
const currentValue = await simpleStorage.viewMyNumber();
console.log("Current Value is: " + currentValue);
// Update the current value
const transactionResponse = await simpleStorage.storeMyNumber(7296);
await transactionResponse.wait(1);
const updatedValue = await simpleStorage.viewMyNumber();
console.log("Updated Value is: " + updatedValue);
}
// Auto verify contract in etherscan
async function verify(contractAddress, args) {
console.log("Verifying contract...");
try {
await run("verify:verify", {
address: contractAddress,
constructorArguments: args,
});
} catch (e) {
if (e.message.toLowerCase().includes("already verified")) {
console.log("Already Verified!");
} else {
console.log(e);
}
}
}
// main
main()
.then(() => process.getMaxListeners(0))
.catch((error) => {
console.error(error);
process.exit(1);
});