Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AmazingAng committed May 12, 2024
2 parents 135c254 + 3e0ab22 commit 5a22a2f
Show file tree
Hide file tree
Showing 1,273 changed files with 73,831 additions and 4,055 deletions.
21 changes: 21 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: Bug report(程序Bug报告)
about: Report a bug in the WTF-Solidity(报告WTF-Solidity中的程序Bug)

---

<!-- Remember, this is not a place to ask for help debugging code. For that, we welcome you in the [Discord](https://discord.gg/5akcruXrsk)|[WeChat Group](https://docs.google.com/forms/d/e/1FAIpQLSe4KGT8Sh6sJ7hedQRuIYirOoZK_85miz3dw7vA1-YjodgJ-A/viewform?usp=sf_link). (请记住,这不是一个请求代码调试帮助的地方。对于此类问题,欢迎您加入[Discord](https://discord.gg/5akcruXrsk)|[微信群](https://docs.google.com/forms/d/e/1FAIpQLSe4KGT8Sh6sJ7hedQRuIYirOoZK_85miz3dw7vA1-YjodgJ-A/viewform?usp=sf_link)。)-->

**💻 Environment(环境)**

<!-- Tell us how you're using it: Hardhat, Remix, etc. (告诉我们你使用的环境:Hardhat,Remix等等。)-->

**📝 Details(细节)**

<!-- Describe the problem you have been experiencing in more detail. (请更详细地描述您遇到的问题。) -->

**🔢 Code to reproduce bug(复现程序Bug的代码)**

<!-- We will be able to better help if you provide a minimal example that triggers the bug. (如果您提供一个最小的示例来告知我们如何触发这个Bug,我们将能够更好地帮助您。) -->

<!-- We will assign it to you if you desire to fix it. You can comment on the issue. (如果你愿意修复它,我们将把任务指派给你。你可以发表评论在问题中。)-->
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
name: Feature request(功能请求)
about: Suggest an idea for the WTF-Solidity(给WTF-Solidity提一个想法)

---

**🧐 Motivation(动机)**

<!-- Please describe your motivation in the feature request. (请描述您在功能请求中的动机。)-->

**📝 Details(细节)**

<!-- Please describe your feature request in detail. (请详细描述您的功能请求。)-->

<!-- We will assign it to you if you desire to fix it. You can comment on the issue. (如果你愿意修复它,我们将把任务指派给你。你可以发表评论在问题中。)-->
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE/improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
name: Improvement request(提升请求)
about: Suggest an improvement for the WTF-Solidity(给WTF-Solidity提一个改进提升的请求)

---

**🧐 Motivation(动机)**

<!-- Please describe your motivation in the improvement request. (请描述您在提升请求中的动机。)-->

**📝 Details(细节)**

<!-- Please describe your improvement request in detail. (请详细描述您的提升请求。)-->

<!-- We will assign it to you if you desire to fix it. You can comment on the issue. (如果你愿意修复它,我们将把任务指派给你。你可以发表评论在问题中。)-->
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE/typo_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
name: Typo report(勘误报告)
about: Report a typo in the WTF-Solidity(报告WTF-Solidity中的勘误)

---

**📝 Details(细节)**

<!-- Describe the typo. (描述勘误。)-->

**🔢 Reference(参考)**

<!-- We will be able to better review if you provide a reference. (你提供参考出处,可以更方便我们review你的issue。)-->

<!-- We will assign it to you if you desire to fix it. You can comment on the issue. (如果你愿意修复它,我们将把任务指派给你。你可以发表评论在问题中。)-->
12 changes: 12 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### What type of PR is this (这是什么类型的PR)

- [ ] Typo(勘误)
- [ ] BUG(程序错误)
- [ ] Improvement(提升)
- [ ] Feature(新特性)

### Which issue(s) this PR fixes(Optional) (这个PR 修复了什么问题 (可选择))

* resolve(修复) : #

### What this PR does / why we need it (这个PR 做了什么/ 我们为什么需要这个PR)
2 changes: 1 addition & 1 deletion .github/workflows/codespell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
with:
check_filenames: true
ignore_words_file: .codespellignore
skip: ./.git,package-lock.json
skip: "./.git,package-lock.json,./Languages/es,./Languages/pt-br"
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!";
}
48 changes: 25 additions & 23 deletions 01_HelloWeb3/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ tags:

# WTF Solidity极简入门: 1. Hello Web3 (三行代码)

我最近在重新学solidity,巩固一下细节,也写一个“WTF Solidity极简入门”,供小白们使用(编程大佬可以另找教程),每周更新1-3讲
我最近在重新学 Solidity,巩固一下细节,也写一个“WTF Solidity极简入门”,供小白们使用(编程大佬可以另找教程),每周更新 1-3 讲

推特:[@0xAA_Science](https://twitter.com/0xAA_Science)
推特:[@0xAA_Science](https://twitter.com/0xAA_Science)[@WTFAcademy_](https://twitter.com/WTFAcademy_)

社区:[Discord](https://discord.gg/5akcruXrsk)[微信群](https://docs.google.com/forms/d/e/1FAIpQLSe4KGT8Sh6sJ7hedQRuIYirOoZK_85miz3dw7vA1-YjodgJ-A/viewform?usp=sf_link)[官网 wtf.academy](https://wtf.academy)

所有代码和教程开源在github: [github.com/AmazingAng/WTFSolidity](https://github.com/AmazingAng/WTFSolidity)
所有代码和教程开源在 github: [github.com/AmazingAng/WTFSolidity](https://github.com/AmazingAng/WTFSolidity)

-----
---

## Solidity 简介

Expand All @@ -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 @@ -53,22 +53,23 @@ contract HelloWeb3{

1. 第 1 行是注释,说明代码所使用的软件许可(license),这里使用的是 MIT 许可。如果不写许可,编译时会出现警告(warning),但程序仍可运行。Solidity 注释以“//”开头,后面跟注释内容,注释不会被程序执行。

```solidity
// SPDX-License-Identifier: MIT
```
```solidity
// SPDX-License-Identifier: MIT
```

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

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

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

3. 第 3-4 行是合约部分。第 3 行创建合约(contract),并声明合约名为 `HelloWeb3`。第 4 行是合约内容,声明了一个 string(字符串)变量 `_string`,并赋值为 "Hello Web3!"。

```solidity
contract HelloWeb3 {
string public _string = "Hello Web3!";
}
```
```solidity
contract HelloWeb3 {
string public _string = "Hello Web3!";
}
```

后续我们会更详细地介绍 Solidity 中的变量。

Expand All @@ -78,18 +79,19 @@ contract HelloWeb3{

编译完成后,点击左侧菜单的“部署”按钮,进入部署页面。

![](./img/1-2.png)
![Deploy配图](./img/1-2.png)

默认情况下,`Remix` 会使用 `Remix` 虚拟机(以前称为 JavaScript 虚拟机)来模拟以太坊链,运行智能合约,类似在浏览器里运行一条测试链。`Remix` 还会为你分配一些测试账户,每个账户里有 100 ETH(测试代币),随意使用。点击 `Deploy`(黄色按钮),即可部署我们编写的合约。

![](./img/1-3.png)
![_string配图](./img/1-3.png)

部署成功后,在下方会看到名为 `HelloWeb3` 的合约。点击 `_string`,即可看到 "Hello Web3!"。

## 总结

本节课程中,我们简要介绍了 `Solidity``Remix` 工具,并完成了第一个 `Solidity` 程序 —— `HelloWeb3`。接下来,我们将继续深入学习 `Solidity`

### 中文 Solidity 资料推荐:
1. [Solidity中文文档](https://docs.soliditylang.org/zh/v0.8.19/index.html)(官方文档的中文翻译)
### 中文 Solidity 资料推荐

2. [崔棉大师solidity教程](https://space.bilibili.com/286084162) web3技术教学博主,我看他视频学到了很多
1. [Solidity中文文档](https://docs.soliditylang.org/zh/v0.8.19/index.html)(官方文档的中文翻译)
2. [崔棉大师solidity教程](https://space.bilibili.com/286084162) web3技术教学博主,我看他视频学到了很多
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
92 changes: 53 additions & 39 deletions 02_ValueTypes/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ tags:

# WTF Solidity极简入门: 2. 值类型

我最近在重新学solidity,巩固一下细节,也写一个“WTF Solidity极简入门”,供小白们使用(编程大佬可以另找教程),每周更新1-3讲
我最近在重新学 Solidity,巩固一下细节,也写一个“WTF Solidity极简入门”,供小白们使用(编程大佬可以另找教程),每周更新 1-3 讲

推特:[@0xAA_Science](https://twitter.com/0xAA_Science)
推特:[@0xAA_Science](https://twitter.com/0xAA_Science)[@WTFAcademy_](https://twitter.com/WTFAcademy_)

社区:[Discord](https://discord.gg/5akcruXrsk)[微信群](https://docs.google.com/forms/d/e/1FAIpQLSe4KGT8Sh6sJ7hedQRuIYirOoZK_85miz3dw7vA1-YjodgJ-A/viewform?usp=sf_link)[官网 wtf.academy](https://wtf.academy)

所有代码和教程开源在github: [github.com/AmazingAng/WTFSolidity](https://github.com/AmazingAng/WTFSolidity)
所有代码和教程开源在 github: [github.com/AmazingAng/WTFSolidity](https://github.com/AmazingAng/WTFSolidity)

-----
---

## Solidity中的变量类型

1. **值类型(Value Type)**:包括布尔型,整数型等等,这类变量赋值时候直接传递数值。

2. **引用类型(Reference Type)**:包括数组和结构体,这类变量占空间大,赋值时候直接传递地址(类似指针)。
Expand All @@ -28,12 +29,14 @@ tags:
我们将仅介绍常用类型,不常用的类型不会涉及,本篇将介绍值类型。

## 值类型

### 1. 布尔型

布尔型是二值变量,取值为 `true``false`

```solidity
// 布尔值
bool public _bool = true;
// 布尔值
bool public _bool = true;
```

布尔值的运算符包括:
Expand All @@ -45,44 +48,47 @@ tags:
- `!=` (不等于)

```solidity
// 布尔运算
bool public _bool1 = !_bool; // 取非
bool public _bool2 = _bool && _bool1; // 与
bool public _bool3 = _bool || _bool1; // 或
bool public _bool4 = _bool == _bool1; // 相等
bool public _bool5 = _bool != _bool1; // 不相等
// 布尔运算
bool public _bool1 = !_bool; // 取非
bool public _bool2 = _bool && _bool1; // 与
bool public _bool3 = _bool || _bool1; // 或
bool public _bool4 = _bool == _bool1; // 相等
bool public _bool5 = _bool != _bool1; // 不相等
```

在上述代码中:变量 `_bool` 的取值是 `true``_bool1``_bool` 的非,为 `false``_bool && _bool1``false``_bool || _bool1``true``_bool == _bool1``false``_bool != _bool1``true`

**值得注意的是:**`&&``||` 运算符遵循短路规则,这意味着,假如存在 `f(x) || g(y)` 的表达式,如果 `f(x)``true``g(y)` 不会被计算,即使它和 `f(x)` 的结果是相反的。
**值得注意的是:**`&&``||` 运算符遵循短路规则,这意味着,假如存在 `f(x) || g(y)` 的表达式,如果 `f(x)``true``g(y)` 不会被计算,即使它和 `f(x)` 的结果是相反的。假如存在`f(x) && g(y)` 的表达式,如果 `f(x)``false``g(y)` 不会被计算。
所谓“短路规则”,一般出现在逻辑与(&&)和逻辑或(||)中。 当逻辑与(&&)的第一个条件为false时,就不会再去判断第二个条件; 当逻辑或(||)的第一个条件为true时,就不会再去判断第二个条件,这就是短路规则。

### 2. 整型

整型是 Solidity 中的整数,最常用的包括:

```solidity
// 整型
int public _int = -1; // 整数,包括负数
uint public _uint = 1; // 正整数
uint256 public _number = 20220330; // 256位正整数
// 整型
int public _int = -1; // 整数,包括负数
uint public _uint = 1; // 正整数
uint256 public _number = 20220330; // 256位正整数
```

常用的整型运算符包括:

- 比较运算符(返回布尔值): `<=``<``==``!=``>=``>`
- 比较运算符(返回布尔值): `<=``<``==``!=``>=``>`
- 算数运算符: `+``-``*``/``%`(取余),`**`(幂)

```solidity
// 整数运算
uint256 public _number1 = _number + 1; // +,-,*,/
uint256 public _number2 = 2**2; // 指数
uint256 public _number3 = 7 % 2; // 取余数
bool public _numberbool = _number2 > _number3; // 比大小
// 整数运算
uint256 public _number1 = _number + 1; // +,-,*,/
uint256 public _number2 = 2**2; // 指数
uint256 public _number3 = 7 % 2; // 取余数
bool public _numberbool = _number2 > _number3; // 比大小
```

大家可以运行一下代码,看看这 4 个变量分别是多少。

### 3. 地址类型

地址类型(address)有两类:

- 普通地址(address): 存储一个 20 字节的值(以太坊地址的大小)。
Expand All @@ -91,11 +97,11 @@ tags:
我们会在之后的章节更加详细地介绍 payable address。

```solidity
// 地址
address public _address = 0x7A58c0Be72BE218B41C608b7Fe7C5bB630736C71;
address payable public _address1 = payable(_address); // payable address,可以转账、查余额
// 地址类型的成员
uint256 public balance = _address1.balance; // balance of address
// 地址
address public _address = 0x7A58c0Be72BE218B41C608b7Fe7C5bB630736C71;
address payable public _address1 = payable(_address); // payable address,可以转账、查余额
// 地址类型的成员
uint256 public balance = _address1.balance; // balance of address
```

### 4. 定长字节数组
Expand All @@ -106,37 +112,45 @@ tags:
- 不定长字节数组: 属于引用类型(之后的章节介绍),数组长度在声明之后可以改变,包括 `bytes` 等。

```solidity
// 固定长度的字节数组
bytes32 public _byte32 = "MiniSolidity";
bytes1 public _byte = _byte32[0];
// 固定长度的字节数组
bytes32 public _byte32 = "MiniSolidity";
bytes1 public _byte = _byte32[0];
```

在上述代码中,`MiniSolidity` 变量以字节的方式存储进变量 `_byte32`。如果把它转换成 `16 进制`,就是:`0x4d696e69536f6c69646974790000000000000000000000000000000000000000`

`_byte` 变量的值为 `_byte32` 的第一个字节,即 `0x4d`

### 5. 枚举 enum

枚举(`enum`)是 Solidity 中用户定义的数据类型。它主要用于为 `uint` 分配名称,使程序易于阅读和维护。它与 `C 语言` 中的 `enum` 类似,使用名称来代替从 `0` 开始的 `uint`

```solidity
// 用enum将uint 0, 1, 2表示为Buy, Hold, Sell
enum ActionSet { Buy, Hold, Sell }
// 创建enum变量 action
ActionSet action = ActionSet.Buy;
// 用enum将uint 0, 1, 2表示为Buy, Hold, Sell
enum ActionSet { Buy, Hold, Sell }
// 创建enum变量 action
ActionSet action = ActionSet.Buy;
```

枚举可以显式地和 `uint` 相互转换,并会检查转换的正整数是否在枚举的长度内,否则会报错:

```solidity
// enum可以和uint显式的转换
function enumToUint() external view returns(uint){
return uint(action);
}
// enum可以和uint显式的转换
function enumToUint() external view returns(uint){
return uint(action);
}
```

`enum` 是一个比较冷门的变量,几乎没什么人用。

## 在 Remix 上运行

- 部署合约后可以查看每个类型的变量的数值:

![2-1.png](./img/2-1.png)

- `enum``uint` 转换的示例:

![2-2.png](./img/2-2.png)
![2-3.png](./img/2-3.png)

Expand Down
4 changes: 2 additions & 2 deletions 03_Function/Function.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 FunctionTypes{
uint256 public number = 5;

Expand All @@ -22,7 +22,7 @@ contract FunctionTypes{
new_number = number + 1;
}

// internal: 内部
// internal: 内部函数
function minus() internal {
number = number - 1;
}
Expand Down
Loading

0 comments on commit 5a22a2f

Please sign in to comment.