Javascript

我無法以程式方式將智能合約(簡單儲存)部署到安全帽

  • August 31, 2022
const {ethers} = require("hardhat")

async function main(){
const SimpleStorageFactory =  await ethers.getContractFactory("SimpleStorage")  
console.log("Deploying,....")
const simpleStorage= SimpleStorageFactory.deploy()
await (await simpleStorage).deployed()
}

在這個函式上,我遇到了一個錯誤:

TypeError: Cannot read properties of undefined (reading 'getContractFactory')

有人可以告訴我發生了什麼事嗎?

在最後一行,我看到兩個“等待”,我認為這是不正確的。我會像這樣創建部署腳本:

const hre = require("hardhat");

async function main() {
 const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage")  
 const simpleStorage= await SimpleStorage.deploy();
 await simpleStorage.deployed();
 console.log(`simple storage deployed: ${simpleStorage.address}` )
}

假設簡單的儲存合約不需要任何部署時間參數

嘗試這個

const simpleStorageFactory =  await ethers.getContractFactory("SimpleStorage") 
const simpleStorage = await simpleStorageFactory.deploy()
await simpleStorage.deployed()

引用自:https://ethereum.stackexchange.com/questions/134663