Solidity 代码文件

Solidity 代码文件以 .sol 作为文件后缀。在 Solidity 源代码的开头,需要注明源代码锁使用的开源协议和 Solidity 的版本号:

1
2
3
4
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// 上面两行表明使用 MIT 开源协议,以及 solidity 版本须在 0.8.0 以上

内置类型


contract 关键字

我们提到 Solidity 是面向对象的语言,在 EVM 的世界里,我们把智能合约建模成 class,用户与合约之间的交互视为和 class object 进行交互


struct, enum 关键字

Solidity 里的 struct 很类似于 C 语言里的 struct,就是单纯的 Data Composite. 不能定义方法等等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct AuctionSummary {
/// @notice Information about the domain being auctioned
uint256 tokenID;
string domain;
/// @notice Information about the current owner
address owner;
/// @notice Information about the auction
address auction;
address beneficiary;
// Status
Phase phase;
bool finalized;
// Timing information for the auction
uint256 commitEnd;
uint256 revealEnd;
// Winning information
address winner;
uint256 winningBid;
}

此外 enum 的行为也是类似的,因为没有什么好讲的就放一起了

1
2
3
4
5
6
/// @notice Different phases of the auction.
enum Phase {
Commit,
Reveal,
Ended
}

library 关键字


Functions/Methods

Modifier

Solidity 特性,用于在 Runtime、即将进入函数体之前,检查参数是否满足特定的条件,或者用于满足特定的任务。