Added task

This commit is contained in:
2024-08-03 00:08:10 +08:00
parent e7c96c8ec3
commit d5730e4a94
5 changed files with 97 additions and 7 deletions

View File

@@ -11,3 +11,10 @@ REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat ignition deploy ./ignition/modules/Lock.js
```
yarn hardhat run scripts/deploy.js --network sepolia
yarn hardhat run scripts/deploy.js --network hardhat
yarn hardhat verify --network sepolia DEPLOYED_CONTRACT_ADDRESS "0x2f86Ad03BE88664cB3d73219fF6dDdDBa598449F"
0xE5EA2da0357072607cE5bA85Cd0b73935c14CAF7
0xc2022b56eBC140B5FebCf9FBaB14c17db4C315C4 - Verified

View File

@@ -3,7 +3,7 @@ pragma solidity ^0.8.8;
contract SimpleStorage {
// boolean, uint, int, address, bytes
address myAddress = 0xc9445E993dAeA4bA3F1FE1080F0F6f8c46b4d967;
address public myAddress = 0xc9445E993dAeA4bA3F1FE1080F0F6f8c46b4d967;
uint256 public myNumber = 123; // With Getter, Retrieval Need Gas
string myText = "Hello World";
//int256 favoriteInt = -5;
@@ -11,8 +11,8 @@ contract SimpleStorage {
uint256 myDefaultZero; // Initialize default to 0
// Paid
function storeMyNumber(uint256 _favariteNumber) public virtual {
myNumber = _favariteNumber;
function storeMyNumber(uint256 _favoriteNumber) public virtual {
myNumber = _favoriteNumber;
//retrieve();
}

View File

@@ -4,6 +4,7 @@ require("@nomiclabs/hardhat-ethers");
//require("@nomiclabs/hardhat-etherscan");
//require("@nomicfoundation/hardhat-verify");
require("dotenv").config();
require("./tasks/block-number");
const RPC_URL_SEPOLIA = process.env.SEPOLIA_RPC_URL;
const PRIVATE_KEY_SEPOLIA = process.env.SEPOLIA_PRIVATE_KEY;
@@ -20,18 +21,21 @@ module.exports = {
sepolia: {
url: RPC_URL_SEPOLIA,
accounts: [PRIVATE_KEY_SEPOLIA],
chainID: 11155111,
chainId: 11155111,
},
ganache: {
url: RPC_URL_GANACHE,
accounts: [PRIVATE_KEY_GANACHE],
chainID: 5777,
chainId: 5777,
},
},
solidity: "0.8.24",
etherscan: {
apiKey: ETHERSCAN_API_KEY,
},
sourcify: {
enabled: false,
},
};
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {

View File

@@ -1,18 +1,89 @@
// imports
const { ethers } = require("hardhat");
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) {}
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()

8
tasks/block-number.js Normal file
View File

@@ -0,0 +1,8 @@
const { tast, task } = require("hardhat/config");
task("block-number", "Prints current block number").setAction(
async (taskArgs, hre) => {
const blockNumber = await hre.ethers.provider.getBlockNumber();
console.log("Current block number: " + blockNumber);
},
);