简体   繁体   English

使用 alchemy 在 hardhat 中部署到 test.network (npx hardhat run scripts/deploy.js -.network goerli)

[英]Deploying to a test network (npx hardhat run scripts/deploy.js --network goerli) in hardhat by using alchemy

hardhat.config.js安全帽.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}`],
    },
  },
};

scripts/deploy.js脚本/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);
  });

contracts/MyNFT.sol合同/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;
    }
    
}

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

it gives error:-它给出了错误:-

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'
}

some people said that in hardhat.config.js change "account" to "accounts" your text but it gives also error有人说在 hardhat.config.js 中将“帐户”更改为“帐户” 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

I use alchemy for url and goerli for test.network.我对 url 使用 alchemy,对 test.network 使用 goerli。

what i do?我所做的? please help I am stuck more than 4 days.请帮助我被困超过 4 天。

Try to replace url with something like this尝试用这样的东西替换 url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM