hardhat-etherscan 這個插件可以幫助你在Etherscan上驗證 Solidity 合約的源代碼,
- 只需提供部署地址和建構式引數,插件將在本地檢測要驗證的合約,
- 如果您的合約使用 Solidity庫,插件將檢測它們并自動處理它們,你不需要對它們做任何事情,
- 驗證程序的模擬將在本地運行,允許插件檢測和傳達程序中的任何錯誤,
- 一旦模擬成功,合同將使用 Etherscan API 進行驗證,
安裝:
npm install --save-dev @nomiclabs/hardhat-etherscan
并將以下陳述句添加到您的hardhat.config.js:
require("@nomiclabs/hardhat-etherscan");
將以下 Etherscan 配置添加到您的hardhat.config.js檔案中:
module.exports = {
solidity: {
version: '0.8.4',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
contractSizer: {
alphaSort: true,
runOnCompile: true,
disambiguatePaths: false,
},
networks: {
tbsc: {
url: "https://data-seed-prebsc-1-s1.binance.org:8545/",
accounts:
["你的私鑰"],
},
ropsten: {
url: "https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161",
accounts:
["你的私鑰"],
},
},
etherscan: {
// etherscan:
// bscscan:
apiKey: "your apiKey",
},
};
部署到bsc測驗鏈:
D:\MySC\DMToken>npx hardhat run scripts/yourtoken.js --network tbsc
Compiling 9 files with 0.8.4
Compilation finished successfully
YourToken deployed to: 0x915D4D9aEe18F4c22ab6e2dA51aD9473E4b929B3
運行verify任務(合約源代碼、部署網路、合約的地址)
D:\MySC\DMToken>npx hardhat verify --contract contracts/Greeter2.sol:YourToken --network tbsc 0x915D4D9aEe18F4c22ab6e2dA51aD9473E4b929B3
Nothing to compile
Compiling 1 file with 0.8.4
Successfully submitted source code for contract
contracts/Greeter2.sol:YourToken at 0x915D4D9aEe18F4c22ab6e2dA51aD9473E4b929B3
for verification on Etherscan. Waiting for verification result...
Successfully verified contract YourToken on Etherscan.
https://testnet.bscscan.com/address/0x915D4D9aEe18F4c22ab6e2dA51aD9473E4b929B3#code
另附:
scripts/yourtoken.js
const hre = require("hardhat");
async function main() {
// We get the contract to deploy
const Greeter = await hre.ethers.getContractFactory("YourToken");
const greeter = await Greeter.deploy();
await greeter.deployed();
console.log("YourToken deployed to:", greeter.address);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
contracts/YourToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract YourToken is ERC20, ERC20Burnable, Pausable, Ownable {
constructor() ERC20("YourToken", "YTK") {
_mint(msg.sender, 10000 * 10 ** decimals());
}
function pause() public onlyOwner {
_pause();
}
function unpause() public onlyOwner {
_unpause();
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal
whenNotPaused
override
{
super._beforeTokenTransfer(from, to, amount);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/399624.html
標籤:區塊鏈
上一篇:(一)HyperLedger Fabric 2.4 環境搭建
下一篇:理解區塊鏈之“鏈式結構”
