顺便说一句,我的合约代码如下:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract SimpleStorage {
uint256 favoriteNumber;
struct People {
uint256 favoriteNumber;
string name;
}
// uint256[] public anArray;
People[] public people;
mapping(string => uint256) public nameToFavoriteNumber;
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
}
function retrieve() public view returns (uint256) {
return favoriteNumber;
}
function addPerson(string memory _name, uint256 _favoriteNumber) public {
people.push(People(_favoriteNumber, _name));
nameToFavoriteNumber[_name] = _favoriteNumber;
}
}
我的js代码如下:
const ethers = require("ethers")
const fs = require("fs-extra")
async function main() {
const provider = new ethers.JsonRpcProvider("https://rpc.sepolia.dev")
const wallet = new ethers.Wallet("f2a0b5a745aa68a7c2343314f0376152329175ec2382fcbc32dc0fb87e6738e7", provider);
const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi","utf8")
const bin = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.bin","utf8")
const contractFactory = new ethers.ContractFactory(abi, bin, wallet)
const contract = await contractFactory.deploy()
console.log(contract)
}
main()
.then(()=>process.exit(0))
.catch((error)=>{
console.log("----:"+error)
process.exit(1)
})
我的nodejs版本是v20.11.1 我的以太版本是^6.11.1
编译并执行代码命令: 纱线 solcjs --bin --abi --include-path node_modules/ --base-path 。 -o.简单存储.sol 节点测试.js
希望大家帮我找出可能的代码错误原因并成功执行发布合约代码
您问题中的代码片段包含私钥。切勿在互联网上分享您的私钥。您应该考虑此密钥已被泄露 - 任何人都可以从从此密钥派生的地址窃取任何网络上的任何资金 - 因此您可能希望从现在开始使用不同的密钥。
const provider = new ethers.JsonRpcProvider("https://rpc.sepolia.dev")
const wallet = new ethers.Wallet("<privatekey>", provider);
// ...
const contractFactory = new ethers.ContractFactory(abi, bin, wallet)
const contract = await contractFactory.deploy()
此代码片段尝试将合约部署到 Sepolia 测试网,而不是部署到本地模拟器。
部署者地址没有任何Sepolia ETH来支付部署交易。
使用本地模拟器的 URL,而不是
rpc.sepolia.dev
RPC URL。我相信 Ganache 默认使用 http://127.0.0.1:7545
(UI) 或 http://127.0.0.1:8545
(CLI)。如果需要,您可以在设置 (Ganache UI) 中或使用 --server.host
和 --server.port
选项覆盖默认 URL(Ganache CLI,请参阅 docs)。
修复示例:
const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545")