了解以太坊生态系统,这样你就不会每次都重新发明轮子。使用Chainlink, ENS, The Graph, IPFS, Ceramic, Polygon, NEAR等开发吧!
如果你打开这个etherscan 链接,你可以看到你可以直接通过 etherscan 与这个智能合约的功能进行交互,类似于你以前在 Remix 上的操作方式。
你是否想知道为什么你的合同没有发生这种情况?
因此,让我们了解为什么以及如何在 etherscan 上验证合约🚀
现在让我们学习如何利用hardhat仅通过几行代码来验证智能合约。
让我们开始🚀🚀🚀
mkdir hardhat-verification
cd hardhat-verification
npm init --yes
npm install --save-dev hardhat
npx hardhat
Create a Javascript project
Hardhat Project root
.gitignore
Do you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)?
现在你有一个hardhat项目准备好了!
如果您在 Windows 上,请执行此额外步骤并安装这些库:)
npm install --save-dev @nomicfoundation/hardhat-toolbox
contracts
名为Verify.sol
.//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.4;
contract Verify {
string private greeting;
constructor() {
}
function hello(bool sayHello) public pure returns (string memory) {
if(sayHello) {
return "hello";
}
return "";
}
}
dotenv
包以便能够导入 env 文件并在我们的配置中使用它。打开指向hardhat-verification
目录的终端并执行此命令npm install dotenv
env
文件,并添加以下几行,使用注释中的说明来获得你的ALCHEMY_API_KEY_URL
、MUMBAI_PRIVATE_KEY
和POLYGONSCAN_KEY
.如果你的MetaMask上没有mumbai ,你可以按照这个来把它添加到你的MetaMask上,确保你获得mumbai 私钥的账户有mumbai Matic资金,你可以从这里得到一些。 // Go to https://www.alchemyapi.io, sign up, create
// a new App in its dashboard and select the network as Mumbai, and replace "add-the-alchemy-key-url-here" with its key url
ALCHEMY_API_KEY_URL="add-the-alchemy-key-url-here"
// Replace this private key with your Mumbai account private key
// To export your private key from Metamask, open Metamask and
// go to Account Details > Export Private Key
// Be aware of NEVER putting real Ether into testing accounts
MUMBAI_PRIVATE_KEY="add-the-mumbai-private-key-here"
// Go to https://polygonscan.com/, sign up, on your account overview page,
// click on `API Keys`, add a new API key and copy the
// `API Key Token`
POLYGONSCAN_KEY="add-the-polygonscan-api-token-here"
mumbai
网络。创建一个新文件,或替换现有的默认文件,deploy.js
在该scripts
文件夹下命名。请注意我们如何使用代码来验证合约。const { ethers } = require("hardhat");
require("dotenv").config({ path: ".env" });
async function main() {
/*
A ContractFactory in ethers.js is an abstraction used to deploy new smart contracts,
so verifyContract here is a factory for instances of our Verify contract.
*/
const verifyContract = await ethers.getContractFactory("Verify");
// deploy the contract
const deployedVerifyContract = await verifyContract.deploy();
await deployedVerifyContract.deployed();
// print the address of the deployed contract
console.log("Verify Contract Address:", deployedVerifyContract.address);
console.log("Sleeping.....");
// Wait for etherscan to notice that the contract has been deployed
await sleep(10000);
// Verify the contract after deploying
await hre.run("verify:verify", {
address: deployedVerifyContract.address,
constructorArguments: [],
});
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Call the main function and catch if there is any error
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
hardhat.config.js
文件,我们将在mumbai
此处添加网络,以便我们可以将合约部署到mumbai和一个etherscan
对象,以便我们可以在polygonscan
. 用下面给出的行替换hardhat.config.js
文件中的所有行。require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config({ path: ".env" });
const ALCHEMY_API_KEY_URL = process.env.ALCHEMY_API_KEY_URL;
const MUMBAI_PRIVATE_KEY = process.env.MUMBAI_PRIVATE_KEY;
const POLYGONSCAN_KEY = process.env.POLYGONSCAN_KEY;
module.exports = {
solidity: "0.8.4",
networks: {
mumbai: {
url: ALCHEMY_API_KEY_URL,
accounts: [MUMBAI_PRIVATE_KEY],
},
},
etherscan: {
apiKey: {
polygonMumbai: POLYGONSCAN_KEY,
},
},
};
hardhat-verification
目录的终端并执行这个命令npx hardhat compile
hardhat-tutorial
目录的终端并执行此命令 npx hardhat run scripts/deploy.js --network mumbai
原文: https://www.learnweb3.io/tracks/junior/etherscan-contract-verification
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!