入门指南
Last updated
node -v
npm -vnpm install -g hardhatnpx hardhat// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedValue;
// 存储一个值
function set(uint256 _value) public {
storedValue = _value;
}
// 获取存储的值
function get() public view returns (uint256) {
return storedValue;
}
}require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.0",
networks: {
juchain_testnet: {
url: "https://testnet-rpc.juchain.org",
chainId:
accounts: ["YOUR_PRIVATE_KEY"] // 替换为您的私钥(注意安全)
}
}
};npm install --save-dev @nomicfoundation/hardhat-toolboxnpx hardhat compileconst hre = require("hardhat");
async function main() {
const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage");
const simpleStorage = await SimpleStorage.deploy();
await simpleStorage.deployed();
console.log("SimpleStorage deployed to:", simpleStorage.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});npx hardhat run scripts/deploy.js --network juchain_testnetSimpleStorage deployed to: 0x1234...abcdnpx hardhat console --network juchain_testnetconst SimpleStorage = await ethers.getContractFactory("SimpleStorage");
const simpleStorage = await SimpleStorage.attach("0x1234...abcd"); // 替换为您的合约地址
await simpleStorage.set(42);
(await simpleStorage.get()).toString();npm init -y
npm install ethersimport { ethers } from "ethers";
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
const contractAddress = "0x1234...abcd"; // 替换为您的合约地址
const abi = [ /* 从 artifacts/SimpleStorage.json 中复制 ABI */ ];
const contract = new ethers.Contract(contractAddress, abi, signer);
// 设置值
await contract.set(100);
// 获取值
const value = await contract.get();
console.log("Stored value:", value.toString());