Skip to content

Commit

Permalink
Solidity Version update from 0.8.4 to 0.8.21
Browse files Browse the repository at this point in the history
This will compatible to newest version of OpenZeppelin contracts.
  • Loading branch information
AmazingAng committed Mar 13, 2024
1 parent 7523394 commit 4b70691
Show file tree
Hide file tree
Showing 295 changed files with 318 additions and 319 deletions.
2 changes: 1 addition & 1 deletion 01_HelloWeb3/HelloWeb3.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract HelloWeb3{
string public _string = "Hello Web3!";
}
6 changes: 3 additions & 3 deletions 01_HelloWeb3/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tags:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract HelloWeb3{
string public _string = "Hello Web3!";
}
Expand All @@ -57,10 +57,10 @@ contract HelloWeb3{
// SPDX-License-Identifier: MIT
```

2. 第 2 行声明源文件所使用的 Solidity 版本,因为不同版本的语法有差异。这行代码表示源文件将不允许小于 0.8.4 版本或大于等于 0.9.0 的编译器编译(第二个条件由 `^` 提供)。Solidity 语句以分号(;)结尾。
2. 第 2 行声明源文件所使用的 Solidity 版本,因为不同版本的语法有差异。这行代码表示源文件将不允许小于 0.8.21 版本或大于等于 0.9.0 的编译器编译(第二个条件由 `^` 提供)。Solidity 语句以分号(;)结尾。

```solidity
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
```

3. 第 3-4 行是合约部分。第 3 行创建合约(contract),并声明合约名为 `HelloWeb3`。第 4 行是合约内容,声明了一个 string(字符串)变量 `_string`,并赋值为 "Hello Web3!"。
Expand Down
2 changes: 1 addition & 1 deletion 02_ValueTypes/ValueTypes.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract ValueTypes{
// 布尔值
bool public _bool = true;
Expand Down
2 changes: 1 addition & 1 deletion 03_Function/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function <function name>(<parameter types>) {internal|external|public|private} [

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract FunctionTypes{
uint256 public number = 5;
}
Expand Down
2 changes: 1 addition & 1 deletion 04_Return/Return.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

// 返回多个变量
// 命名式返回
Expand Down
2 changes: 1 addition & 1 deletion 05_DataStorage/DataStorage.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract DataStorage {
// The data location of x is storage.
Expand Down
6 changes: 3 additions & 3 deletions 06_ArrayAndStruct/ArrayAndStruct.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract ArrayTypes {

// 固定长度 Array
Expand Down Expand Up @@ -32,7 +32,7 @@ contract ArrayTypes {
}
}

pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract StructTypes {
// 结构体 Struct
struct Student{
Expand Down Expand Up @@ -65,7 +65,7 @@ contract StructTypes {
}
}

pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract EnumTypes {
// 将uint 0, 1, 2表示为Buy, Hold, Sell
enum ActionSet { Buy, Hold, Sell }
Expand Down
2 changes: 1 addition & 1 deletion 07_Mapping/Mapping.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract Mapping {
mapping(uint => address) public idToAddress; // id映射到地址
mapping(address => address) public swapPair; // 币对的映射,地址到地址
Expand Down
2 changes: 1 addition & 1 deletion 08_InitialValue/InitialValue.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract InitialValue {
// Value Types
Expand Down
2 changes: 1 addition & 1 deletion 10_InsertionSort/InsertionSort.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract InsertionSort {
// if else
function ifElseTest(uint256 _number) public pure returns(bool){
Expand Down
6 changes: 3 additions & 3 deletions 11_Modifier/Owner.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract Owner {
address public owner; // 定义owner变量

// 构造函数
constructor() {
owner = msg.sender; // 在部署合约的时候,将owner设置为部署者的地址
constructor(address initialOwner) {
owner = initialOwner; // 在部署合约的时候,将owner设置为传入的initialOwner地址
}

// 定义modifier
Expand Down
6 changes: 3 additions & 3 deletions 11_Modifier/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ tags:
address owner; // 定义owner变量
// 构造函数
constructor() {
owner = msg.sender; // 在部署合约的时候,将owner设置为部署者的地址
constructor(address initialOwner) {
owner = initialOwner; // 在部署合约的时候,将owner设置为传入的initialOwner地址
}
```

**注意**⚠️:构造函数在不同的Solidity版本中的语法并不一致,在Solidity 0.4.22之前,构造函数不使用 `constructor` 而是使用与合约名同名的函数作为构造函数而使用,由于这种旧写法容易使开发者在书写时发生疏漏(例如合约名叫 `Parents`,构造函数名写成 `parents`),使得构造函数变成普通函数,引发漏洞,所以0.4.22版本及之后,采用了全新的 `constructor` 写法。
**注意**:构造函数在不同的Solidity版本中的语法并不一致,在Solidity 0.4.22之前,构造函数不使用 `constructor` 而是使用与合约名同名的函数作为构造函数而使用,由于这种旧写法容易使开发者在书写时发生疏漏(例如合约名叫 `Parents`,构造函数名写成 `parents`),使得构造函数变成普通函数,引发漏洞,所以0.4.22版本及之后,采用了全新的 `constructor` 写法。

构造函数的旧写法代码示例:

Expand Down
2 changes: 1 addition & 1 deletion 12_Event/Event.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract Events {
// 定义_balances映射变量,记录每个地址的持币数量
mapping(address => uint256) public _balances;
Expand Down
2 changes: 1 addition & 1 deletion 13_Inheritance/Inheritance.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

// 合约继承
contract Yeye {
Expand Down
2 changes: 1 addition & 1 deletion 13_Inheritance/ModifierInheritance.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract Base1 {
modifier exactDividedBy2And3(uint _a) virtual {
Expand Down
2 changes: 1 addition & 1 deletion 14_Interface/AbstractDemo.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
abstract contract Base{
string public name = "Base";
function getAlias() public pure virtual returns(string memory);
Expand Down
2 changes: 1 addition & 1 deletion 14_Interface/Interface.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

abstract contract InsertionSort{
function insertionSort(uint[] memory a) public pure virtual returns(uint[] memory);
Expand Down
2 changes: 1 addition & 1 deletion 14_Interface/InterfaceDemo.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
interface Base {
function getFirstName() external pure returns(string memory);
function getLastName() external pure returns(string memory);
Expand Down
2 changes: 1 addition & 1 deletion 15_Errors/Error.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

// Gas cost在Remix中测试得到 使用0.8.17版本编译
// 参数使用 tokenId = 123, address = {any address}
Expand Down
2 changes: 1 addition & 1 deletion 16_Overloading/Overloading.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
contract Overload {
function saySomething() public pure returns(string memory){
return("Nothing");
Expand Down
2 changes: 1 addition & 1 deletion 17_Library/Library.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

library Strings {
bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
Expand Down
2 changes: 1 addition & 1 deletion 18_Import/Import.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

// 通过文件相对位置import
import './Yeye.sol';
Expand Down
2 changes: 1 addition & 1 deletion 18_Import/Yeye.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

// 第10讲合约继承中的Yeye合约
contract Yeye {
Expand Down
2 changes: 1 addition & 1 deletion 18_Import/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ tags:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;
// 通过文件相对位置import
import './Yeye.sol';
Expand Down
2 changes: 1 addition & 1 deletion 19_Fallback/Fallback.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract Fallback {
/* 触发fallback() 还是 receive()?
Expand Down
2 changes: 1 addition & 1 deletion 20_SendETH/SendETH.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

// 3种方法发送ETH
// transfer: 2300 gas, revert
Expand Down
2 changes: 1 addition & 1 deletion 21_CallContract/CallContract.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract OtherContract {
uint256 private _x = 0; // 状态变量x
Expand Down
2 changes: 1 addition & 1 deletion 22_Call/Call.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract OtherContract {
uint256 private _x = 0; // 状态变量x
Expand Down
2 changes: 1 addition & 1 deletion 23_Delegatecall/Delegatecall.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

// delegatecall和call类似,都是低级函数
// call: B call C, 上下文为 C (msg.sender = B, C中的状态变量受影响)
Expand Down
2 changes: 1 addition & 1 deletion 24_Create/Create.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract Pair{
address public factory; // 工厂合约地址
Expand Down
2 changes: 1 addition & 1 deletion 25_Create2/Create2.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract Pair{
address public factory; // 工厂合约地址
Expand Down
2 changes: 1 addition & 1 deletion 26_DeleteContract/DeleteContract.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

// selfdestruct: 删除合约,并强制将合约剩余的ETH转入指定账户

Expand Down
2 changes: 1 addition & 1 deletion 27_ABIEncode/ABIEncode.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract ABIEncode{
uint x = 10;
Expand Down
2 changes: 1 addition & 1 deletion 28_Hash/Hash.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract Hash{
bytes32 _msg = keccak256(abi.encodePacked("0xAA"));
Expand Down
2 changes: 1 addition & 1 deletion 29_Selector/Selector.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract Selector{
// event 返回msg.data
Expand Down
2 changes: 1 addition & 1 deletion 30_TryCatch/TryCatch.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

contract OnlyEven{
constructor(uint a){
Expand Down
2 changes: 1 addition & 1 deletion 31_ERC20/ERC20.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// WTF Solidity by 0xAA

pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

import "./IERC20.sol";

Expand Down
2 changes: 1 addition & 1 deletion 31_ERC20/IERC20.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// WTF Solidity by 0xAA

pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

/**
* @dev ERC20 接口合约.
Expand Down
2 changes: 1 addition & 1 deletion 32_Faucet/Faucet.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
// By 0xAA
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

import "./IERC20.sol"; //import IERC20

Expand Down
2 changes: 1 addition & 1 deletion 32_Faucet/IERC20.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// WTF Solidity by 0xAA

pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

/**
* @dev ERC20 接口合约.
Expand Down
2 changes: 1 addition & 1 deletion 33_Airdrop/Airdrop.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
// By 0xAA
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

import "./IERC20.sol"; //import IERC20

Expand Down
2 changes: 1 addition & 1 deletion 33_Airdrop/IERC20.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// WTF Solidity by 0xAA

pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

/**
* @dev ERC20 接口合约.
Expand Down
2 changes: 1 addition & 1 deletion 34_ERC721/ERC721.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
// by 0xAA
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

import "./IERC165.sol";
import "./IERC721.sol";
Expand Down
2 changes: 1 addition & 1 deletion 34_ERC721/String.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/Strings.sol)

pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

/**
* @dev String operations.
Expand Down
2 changes: 1 addition & 1 deletion 34_ERC721/WTFApe.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
// by 0xAA
pragma solidity ^0.8.4;
pragma solidity ^0.8.21;

import "./ERC721.sol";

Expand Down
Loading

0 comments on commit 4b70691

Please sign in to comment.