使用 alchemy 部署到 Hardhat 中的测试网络(npx Hardhat run scripts/deploy.js --network goerli)

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

hardhat.config.js

require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
require("dotenv").config();
require("@nomiclabs/hardhat-ethers");
const { API_URL, PRIVATE_KEY } = process.env;
module.exports = {
  solidity: {
    version: '0.8.17',
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      },
    },
  },
  defaultNetwork: "goerli",
  networks: {
    hardhat: {},
    goerli: {
      url: API_URL, // I use alchemy key
      accounts: [`0x${PRIVATE_KEY}`],
    },
  },
};

脚本/deploy.js

async function main() {
  const MyNFT = await ethers.getContractFactory("MyNFT");

  // Start deployment, returning a promise that resolves to a contract object
  const myNFT = await MyNFT.deploy();
  console.log("Contract deployed to address:", myNFT.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

合约/MyNFT.sol

pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract MyNFT is ERC721URIStorage, Ownable {
   
    using Counters for Counters.Counter;
    
    Counters.Counter private _tokenIds;

    constructor() ERC721("CODE Eater","CER"){}

    function mintNFT(address recipient,string memory tokenURI) public onlyOwner returns(uint256){
      _tokenIds.increment();

      uint256 newItemId = _tokenIds.current();
      _mint(recipient,newItemId);
      _setTokenURI(newItemId, tokenURI);
      return newItemId;
    }
    
}

当我运行命令时 --> npx hardhat run scripts/deploy.js --network goerli

它给出错误:-

TypeError: Invalid URL
    at new NodeError (node:internal/errors:393:5)
    at URL.onParseError (node:internal/url:565:9)
    at new URL (node:internal/url:645:5)
    at new HttpProvider (C:\Users\Lenovo\OneDrive\Desktop\NFTDemo\node_modules\hardhat\src\internal\core\providers\http.ts:51:17)
    at C:\Users\Lenovo\OneDrive\Desktop\NFTDemo\node_modules\hardhat\src\internal\core\runtime-environment.ts:92:28
    at getRealTarget (C:\Users\Lenovo\OneDrive\Desktop\NFTDemo\node_modules\hardhat\src\internal\util\lazy.ts:112:22)
    at Object.get (C:\Users\Lenovo\OneDrive\Desktop\NFTDemo\node_modules\hardhat\src\internal\util\lazy.ts:185:26)
    at createProviderProxy (C:\Users\Lenovo\OneDrive\Desktop\NFTDemo\node_modules\@nomiclabs\hardhat-ethers\src\internal\provider-proxy.ts:25:19)
    at C:\Users\Lenovo\OneDrive\Desktop\NFTDemo\node_modules\@nomiclabs\hardhat-ethers\src\internal\index.ts:36:27 {
  input: '"https://eth-goerli.g.alchemy.com/v2/uR8fEpEHIqXWM_tfZ7gg0utbi0705htE";',
  code: 'ERR_INVALID_URL'
}

有人说在hardhat.config.js中将“account”更改为“accounts”
your text
但它也给出了错误

Error HH8: There's one or more errors in your config file:

  * Invalid account: #0 for network: goerli - private key too long, expected 32 bytes

我使用 alchemy 作为 url,使用 goerli 作为测试网络。

我做什么?请帮助我,我被困了超过 4 天。

ethereum smartcontracts hardhat deploying alchemy
2个回答
0
投票

尝试将 url 替换为类似的内容


0
投票

首先尝试将 .env 文件移动到主文件夹。

那么您是否可以分享 API_URL 值。

您这里有两个错误:

  • 第一个是由 .env 文件位置引起的(应该位于主文件夹中)。
  • 第二个是网址。

尝试使用Sepolia测试网络,以太坊现在推荐使用它。

© www.soinside.com 2019 - 2024. All rights reserved.