我正在尝试使用 Solidity 部署合约并使用 Hardhat 部署它,

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

在合约/SimpleContract.sol中:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleContract {
    string public message = "Hello, World!";
}

在脚本/deploy.js中

const hre = require("hardhat");

async function main() {
  console.log("Deploying SimpleContract...");

  const SimpleContract = await hre.ethers.getContractFactory("SimpleContract");
  const simpleContract = await SimpleContract.deploy();

  console.log("Contract deployment transaction created, waiting for      confirmation...");
  await simpleContract.deployed();

  console.log("SimpleContract deployed to:", simpleContract.address);
}

main().catch((error) => {
  console.error("Error during deployment:", error);
  process.exitCode = 1;
});

错误: C:\ProgStuff\solidity-check>npx Hardhat 运行脚本/deploy.js --network localhost 部署 SimpleContract... 合约部署交易已创建,等待确认... 部署期间出错:TypeError: simpleContract.deployed 不是函数 在主要位置(C:\ProgStuff\solidity-check\scripts\deploy.js:29:24) 在 processTicksAndRejections (节点:内部/进程/task_queues:95:5)

让我知道我在这里错过了什么,谢谢!

blockchain solidity hardhat
1个回答
0
投票

您使用了旧版本的部署脚本和solidity。 某些功能已弃用。

你应该写如下:

import { ethers } from 'hardhat'

async function main() {
  console.log("Deploying SimpleContract...");
  
  const [deployer] = await ethers.getSigners();
  const simpleContract = await ethers.deployContract("SimpleContract");
  
  console.log("Contract deployment transaction created, waiting for confirmation...");
  await simpleContract.waitForDeployment();
  
  const contractAddress = await simpleContract.getAddress();
  console.log("SimpleContract deployed to:", contractAddress);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error("Error during deployment:", error)
    process.exitCode = 1
  })

谢谢你。

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