Solidity

ReferenceError:未定義歡迎程序

  • May 2, 2022

這是我的腳本deploy.js

const main = async () => {


 // We get the contract to deploy
 const Transactions = await hre.ethers.getContractFactory("Transactions");
 const transactions = await Transactions.deploy();

 await greeter.deployed();

 console.log("Greeter deployed to:", greeter.address);
}

const runMain = async() => {
 try {
   await main();
   Process.exit(0);
 } catch (error) {
   console.error(error);
   process.exit(1);
 }
}

runMain();

這是我的腳本hardhat.config.js

// This is a sample Hardhat task. 
// https://eth-ropsten.alchemyapi.io/v2/Bn5tm9fX90ET1hKwb76lKJB0rzU3JBi2

require("@nomiclabs/hardhat-waffle");


module.exports = {
 solidity: '0.8.0',
 networks: {
   ropsten: {
     url: "https://eth-ropsten.alchemyapi.io/v2/Bn5tm9fX90ET1hKwb76lKJB0rzU3JBi2",
     accounts: ["63b598044c3cce8d656............b7c64beb17eb1687aebbefc5cac8a"]
   }
 }
}

這是我的solidity合約腳本:

// SPDX-License-Identifier: MIT

pragma experimental ABIEncoderV2;

pragma solidity >=0.6.0 <0.9.0;

contract Transactions {
   uint256 transactionCount;

   event Transfer(
       address from,
       address reciever,
       uint256 amount,
       string message,
       uint256 timestamp,
       string keyword
   );

   struct TransferStruct {
       address sender;
       address reciever;
       uint256 amount;
       string message;
       uint256 timestamp;
       string keyword;
   }

   TransferStruct[] transactions;

   function addToBlockchain(
       address payable reciever,
       uint256 amount,
       string memory message,
       string memory keyword
   ) public {
       transactionCount += 1;
       transactions.push(
           TransferStruct(
               msg.sender,
               reciever,
               amount,
               message,
               block.timestamp,
               keyword
           )
       );

       emit Transfer(
           msg.sender,
           reciever,
           amount,
           message,
           block.timestamp,
           keyword
       );
   }

   function getAllTransactions()
       public
       view
       returns (TransferStruct[] memory)
   {
       return transactions;
   }

   function getTransactionCount() public view returns (uint256) {
       return transactionCount;
   }
}

以下是我在嘗試編譯時收到的錯誤消息:

PS C:\Users\Temitope\Desktop\kredar\smart_contract> npx hardhat run scripts/deploy.js --network ropsten
ReferenceError: greeter is not defined
at main (C:\Users\Temitope\Desktop\kredar\smart_contract\scripts\deploy.js:8:3)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at runMain (C:\Users\Temitope\Desktop\kredar\smart_contract\scripts\deploy.js:15:5)

請讓我知道我做錯了什麼以及如何解決它。

刪除這些行deploy.js應該沒問題

await greeter.deployed();
console.log("Greeter deployed to:", greeter.address

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