【问题标题】:console.log not working in scripts/deploy.js and in smart_contract in HardHatconsole.log 在脚本/deploy.js 和 HardHat 的 smart_contract 中不起作用
【发布时间】:2022-10-15 01:15:51
【问题描述】:

console.log 不工作脚本/deploy.js合同/WavePortal.sol在安全帽中。当我运行命令时npx 安全帽运行脚本/deploy.js它只显示 "" Compiled 1 Solidity file succeeded "" 。
它在我的 HardHat 环境中不起作用。甚至当我使用 INFURA 并使用命令运行时npx 安全帽运行脚本/deploy.js --network ropsten我从 youtube 到 StackOverflow 到处搜索,但没有找到任何解决方案。我只有一个提示--->

由于合约代码在链上运行,你会从链输出中看到 console.log,而不是在你的 javascript 控制台输出中。但我无法得到这个声明......请帮助我

脚本/deploy.js

    const [owner, randomPerson] = await hre.ethers.getSigners();
    const waveContractFactory = await hre.ethers.getContractFactory("WavePortal");
    const waveContract = await waveContractFactory.deploy();
    await waveContract.deployed();
    
    console.log("Contract deployed to:", waveContract.address);
    console.log("Contract deployed by:", owner.address);
    
    let wavecount;
    wavecount = await waveContract.getTotalWaves();
    
    let waveTxn = await waveContract.wave();
    await waveTxn.wait();

    wavecount = await waveContract.getTotalWaves();

  };
  
  const runMain = async () => {
    try {
      await main();
      process.exit(0); // exit Node process without error
    } catch (error) {
      console.log(error);
      process.exit(1); // exit Node process while indicating 'Uncaught Fatal Exception' error
    }
    // Read more about Node exit ('process.exit(num)') status codes here: https://stackoverflow.com/a/47163396/7974948
  };

合同/WavePortal.sol

// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract WavePortal {
    uint256 totalWaves;

    constructor() {
        console.log("Yo yo, I am a contract and I am smart");
    }

    function wave() public {
        totalWaves += 1;
        console.log("%s has waved!", msg.sender);
    }

    function getTotalWaves() public view returns (uint256) {
        console.log("We have %d total waves!", totalWaves);
        return totalWaves;
    }
}

【问题讨论】:

    标签: javascript solidity console.log ethers.js hardhat


    【解决方案1】:

    在混音中进行了快速测试,您的智能合约似乎运行良好并打印出这些线条。

    console.log:
     Yo yo, I am a contract and I am smart
     
    [vm]from: 0x5B3...eddC4to: WavePortal.(constructor)value: 0 weidata: 0x608...17274logs: 0hash: 0x1d0...16b87
    transact to WavePortal.wave pending ... 
    console.log:
     0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 has waved!
     
    [vm]from: 0x5B3...eddC4to: WavePortal.wave() 0xd91...39138value: 0 weidata: 0x6fe...15b44logs: 0hash: 0xbda...02370
    call to WavePortal.getTotalWaves
    console.log:
     We have 1 total waves!
    

    您的 deploy.js 看起来也不错。可能是除了代码之外的设置问题。

    【讨论】:

    • 非常感谢兄弟,其实我已经安装了vs代码扩展安全帽+Solidity由...提供安全帽正式。所以,我卸载了它并安装了 Juan Blanco 的 Solidity 扩展,我将它的版本降级为 v0.0.135 现在它工作得很好!
    【解决方案2】:

    经过这么努力,我得到了解决方案。实际上我已经安装了 vs 代码扩展安全帽+Solidity由...提供安全帽正式。所以,我卸载了它,并通过Juan Blanco 安装了Solidity 扩展,然后我将其版本降级为v0.0.135,现在它工作得非常好!

    【讨论】:

      猜你喜欢
      • 2020-03-27
      • 2012-04-25
      • 1970-01-01
      • 2022-11-19
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      相关资源
      最近更新 更多