Hardhat

solidity 文件中的 hardhat console.log 不輸出任何內容

  • April 2, 2022

遵循安全帽文件:https ://hardhat.org/tutorial/debugging-with-hardhat-network.html

測試/Token.js:

const { expect } = require("chai");

describe("Token contract", function () {
 it("Deployment should assign the total supply of tokens to the owner", async function () {
   const [owner] = await ethers.getSigners();

   const Token = await ethers.getContractFactory("Token");

   const hardhatToken = await Token.deploy();

   const ownerBalance = await hardhatToken.balanceOf(owner.address);
   expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
   console.log("Inside Token.js");
 });
});

合約/Token.sol:

// Solidity files have to start with this pragma.
// It will be used by the Solidity compiler to validate its version.
pragma solidity ^0.8.0;
import "hardhat/console.sol";

// This is the main building block for smart contracts.
contract Token {
   // Some string type variables to identify the token.
   // The `public` modifier makes a variable readable from outside the contract.
   string public name = "My Hardhat Token";
   string public symbol = "MHT";

   // The fixed amount of tokens stored in an unsigned integer type variable.
   uint256 public totalSupply = 1000000;

   // An address type variable is used to store ethereum accounts.
   address public owner;

   // A mapping is a key/value map. Here we store each account balance.
   mapping(address => uint256) balances;

   /**
    * Contract initialization.
    *
    * The `constructor` is executed only once when the contract is created.
    */
   constructor() {
       // The totalSupply is assigned to transaction sender, which is the account
       // that is deploying the contract.
       balances[msg.sender] = totalSupply;
       owner = msg.sender;
   }

   /**
    * A function to transfer tokens.
    *
    * The `external` modifier makes a function *only* callable from outside
    * the contract.
    */
   function transfer(address to, uint256 amount) external {
       console.log("Sender balance is %s tokens", balances[msg.sender]);
       console.log("Trying to send %s tokens to %s", amount, to);
       // Check if the transaction sender has enough tokens.
       // If `require`'s first argument evaluates to `false` then the
       // transaction will revert.
       require(balances[msg.sender] >= amount, "Not enough tokens");

       // Transfer the amount.
       balances[msg.sender] -= amount;
       balances[to] += amount;
   }

   /**
    * Read only function to retrieve the token balance of a given account.
    *
    * The `view` modifier indicates that it doesn't modify the contract's
    * state, which allows us to call it without executing a transaction.
    */
   function balanceOf(address account) external view returns (uint256) {
       return balances[account];
   }
}

npx 安全帽測試

Compilation finished successfully


 Token contract
Inside Token.js
   ✓ Deployment should assign the total supply of tokens to the owner (734ms)


 1 passing (737ms)

注意Token.js裡面的console.log輸出到控制台,但是Token.sol裡面的console.log什麼也沒輸出!

由於合約程式碼在鏈上執行,你會從鏈輸出中看到 console.log,而不是在你的 javascript 控制台輸出中。

您尚未觸發具有控制台日誌語句的傳輸功能。

您可以將控制台日誌放在建構子上,該測試將觸發控制台日誌。

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