Solidity 练习:全局变量

2022-12-29 Web3 Solidity

# 一、题目说明

全局变量提供有关区块链的信息。这里我们介绍一些常用的全局变量。

// address that called this function
address sender = msg.sender;

// timestamp (in seconds) of current block
uint timeStamp = block.timestamp;

// current block number
uint blockNum = block.number;

// hash of given block
// here we get the hash of the current block
// WARNING: only works for 256 recent blocks
bytes32 blockHash = blockhash(block.number);
1
2
3
4
5
6
7
8
9
10
11
12
13

# 二、任务列表

  • 编写函数 returnSender。
  • 这个函数是 external 和 view 的,返回值是调用者的地址。

# 三、解答代码

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

contract GlobalVariables {
    function returnSender() external view returns (address) {
        return msg.sender;
    }
}
1
2
3
4
5
6
7
8

# 四、参考资料

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