无法使用以太币将合约部署到 Ganache 本地区块链

问题描述 投票:0回答:1

我在Windows 10上使用vscode写了一个合约demo,主要是使用以太币将合约部署到Ganache本地区块链上。但是我执行node test.js时,报错如下: 错误:缺少恢复数据(action =“estimateGas”,data = null,reason = null,transaction = {“data”:“合同的二进制字符串”,“from”:“0x58b5b7c1674f7174082BE1912869f64630F4145f”,“to”:null} ,调用=null,恢复=null,代码=CALL_EXCEPTION,版本=6.11.1),我应该如何修复它?

顺便说一句,我的合约代码如下:

// 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

希望大家帮我找出可能的代码错误原因并成功执行发布合约代码

solidity ganache
1个回答
0
投票

您问题中的代码片段包含私钥。切勿在互联网上分享您的私钥。您应该考虑此密钥已被泄露 - 任何人都可以从从此密钥派生的地址窃取任何网络上的任何资金 - 因此您可能希望从现在开始使用不同的密钥。


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")
© www.soinside.com 2019 - 2024. All rights reserved.