Solidity 练习:使用 Hardhat 调试

2022-12-30 Web3 Solidity

# 一、题目说明

Solidity 练习中的所有题目都可以使用 Hardhat 的 console.sol 进行调试。

您可以通过调用 console.log 打印日志消息和合约变量。当运行 test 时,console.log 的输出将打印在终端上。

对于更多的内容,可以查看文档:Debugging with Hardhat Network (opens new window)

# 二、任务列表

  1. 导入 hardhat/console.sol。
  2. 在函数 transfer 中,调用 console.log 打印变量。

# 三、解答代码

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

import "hardhat/console.sol";

contract Token {
    mapping(address => uint) public balances;

    constructor() {
        balances[msg.sender] = 100;
    }

    function transfer(address to, uint amount) external {
        balances[msg.sender] -= amount;
        balances[to] += amount;

        console.log("transfer", msg.sender, to, amount);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 四、参考资料

Last Updated: 2023-01-28 4:31:25