Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 12 additions & 0 deletions 01_HelloWeb3_en/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
id: '01'
name: 1. HelloWeb3 (Solidity in 3 lines)
summary: '`Solidity` is a programming language used for creating smart contracts on
the Ethereum Virtual Machine (EVM). It''s a necessary skill for working on blockchain
projects. Moreover, as many of them are o'
level: beginner
tags:
- solidity
- tutorial
steps:
- name: HelloWeb3 (Solidity in 3 lines)
path: step1
4 changes: 4 additions & 0 deletions 01_HelloWeb3_en/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# WTF Solidity Tutorial: 1. HelloWeb3 (Solidity in 3 lines)



5 changes: 5 additions & 0 deletions 01_HelloWeb3_en/step1/HelloWeb3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
contract HelloWeb3{
string public _string = "Hello Web3!";}

Binary file added 01_HelloWeb3_en/step1/img/1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 01_HelloWeb3_en/step1/img/1-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 01_HelloWeb3_en/step1/img/1-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions 01_HelloWeb3_en/step1/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# WTF Solidity Tutorial: 1. HelloWeb3 (Solidity in 3 lines)

Twitter: [@0xAA_Science](https://twitter.com/0xAA_Science) | [@WTFAcademy_](https://twitter.com/WTFAcademy_)

Community: [Discord](https://discord.gg/5akcruXrsk)|[Wechat](https://docs.google.com/forms/d/e/1FAIpQLSe4KGT8Sh6sJ7hedQRuIYirOoZK_85miz3dw7vA1-YjodgJ-A/viewform?usp=sf_link)|[Website wtf.academy](https://wtf.academy)

Codes and tutorials are open source on GitHub: [github.com/AmazingAng/WTF-Solidity](https://github.com/AmazingAng/WTF-Solidity)

-----

## WTF is Solidity?

`Solidity` is a programming language used for creating smart contracts on the Ethereum Virtual Machine (EVM). It's a necessary skill for working on blockchain projects. Moreover, as many of them are open-source, understanding the code can help in avoiding money-losing projects.


`Solidity` has two characteristics:

1. Object-oriented: After learning it, you can use it to make money by finding the right projects.
2. Advanced: If you can write smart contracts in Solidity, you are the first class citizen of Ethereum.

## Development tool: Remix

In this tutorial, we will be using `Remix` to run `solidity` contracts. `Remix` is a smart contract development IDE (Integrated Development Environment) recommended by Ethereum official. It is suitable for beginners, allows for quick deployment and testing of smart contracts in the browser, without needing to install any programs on your local machine.

Website: [remix.ethereum.org](https://remix.ethereum.org)

Upon entering `Remix`, you can see that the menu on the left-hand side has three buttons, corresponding to the file (where you write the code), compile (where you run the code), and deploy (where you deploy to the chain). By clicking the "Create New File" button, you can create a blank `solidity` contract.

Within Remix, we can see that there are four buttons on the leftmost vertical menu, corresponding to FILE EXPLORER (where to write code), SEARCH IN FILES (find and replace files), SOLIDITY COMPILER (to run code), and DEPLOY & RUN TRANSACTIONS (on-chain deployment). We can create a blank Solidity contract by clicking the `Create New File` button.

![Remix Menu](https://raw.githubusercontent.com/remix-project-org/remix-workshops/master/01_HelloWeb3_en/step1/img/1-1.png)

## The first Solidity program

This one is easy, the program only contains 1 line of comment and 3 lines of code:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
contract HelloWeb3{
string public _string = "Hello Web3!";}
```

Now, we will breakdown and analyze the source code in detail, understanding the basic structure:

1. The first line is a comment, which denotes the software license (license identifier) used by the program. We are using the MIT license. If you do not indicate the license used, the program can compile successfully but will report a warning during compilation. Solidity's comments are denoted with "//", followed by the content of the comment (which will not be run by the program).

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

2. The second line declares the Solidity version used by the source file because the syntax of different versions is different. This line of code means that the source file will not allow compilation by compiler versions lower than v0.8.21 and not higher than v0.9.0 (the second condition is provided by `^`).

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

3. Lines 3 and 4 are the main body of the smart contract. Line 3 creates a contract with the name `HelloWeb3`. Line 4 is the content of the contract. Here, we created a string variable called `_string` and assigned "Hello Web3!" as value to it.

```solidity
contract HelloWeb3{
string public _string = "Hello Web3!";}
```
We will introduce the different variables in Solidity later.

## Code compilation and deployment

In the code editor, press CTRL+S to compile the code.

After compilation, click the `Deploy` button on the left menu to enter the deployment page.

![](https://raw.githubusercontent.com/remix-project-org/remix-workshops/master/01_HelloWeb3_en/step1/img/1-2.png)

By default, Remix uses the JavaScript virtual machine to simulate the Ethereum chain and run smart contracts, similar to running a testnet on the browser. Remix will allocate several test accounts to you, each with 100 ETH (test tokens). You can click `Deploy` (yellow button) to deploy the contract.

![](https://raw.githubusercontent.com/remix-project-org/remix-workshops/master/01_HelloWeb3_en/step1/img/1-3.png)

After a successful deployment, you will see a contract named `HelloWeb3` below. By clicking on the variable `_string`, it will print its value: `"Hello Web3!"`.

## Summary

In this tutorial, we briefly introduced `Solidity`, `Remix` IDE, and completed our first Solidity program - `HelloWeb3`. Going forward, we will continue our Solidity journey.

### Recommended materials on Solidity:

1. [Solidity Documentation](https://docs.soliditylang.org/en/latest/)
2. Solidity Tutorial by freeCodeCamp:

![youtube](https://www.youtube.com/embed/ipwxYa-F1uY)
11 changes: 11 additions & 0 deletions 02_ValueTypes_en/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
id: '02'
name: 2. Value Types
summary: 1. **Value Type**:This includes boolean, integer, etc. These variables directly
pass values when assigned.
level: beginner
tags:
- solidity
- tutorial
steps:
- name: Value Types
path: step1
4 changes: 4 additions & 0 deletions 02_ValueTypes_en/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# WTF Solidity Tutorial: 2. Value Types



48 changes: 48 additions & 0 deletions 02_ValueTypes_en/step1/ValueTypes.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
contract ValueTypes{
// Boolean
bool public _bool = true;
// Boolean operators
bool public _bool1 = !_bool; // logical NOT
bool public _bool2 = _bool && _bool1; // logical AND
bool public _bool3 = _bool || _bool1; // logical OR
bool public _bool4 = _bool == _bool1; // equality
bool public _bool5 = _bool != _bool1; // inequality


// Integer
int public _int = -1;
uint public _uint = 1;
uint256 public _number = 20220330;
// Integer operators
uint256 public _number1 = _number + 1; // +,-,*,/
uint256 public _number2 = 2**2; // exponent
uint256 public _number3 = 7 % 2; // modulo (modulus)
bool public _numberbool = _number2 > _number3; // greater than


// Address data type
address public _address = 0x7A58c0Be72BE218B41C608b7Fe7C5bB630736C71;
address payable public _address1 = payable(_address); // payable address (allows for token transfer and balance checking)
// Members of addresses
uint256 public balance = _address1.balance; // balance of address


// Fixed-size byte arrays
bytes32 public _byte32 = "MiniSolidity"; // bytes32: 0x4d696e69536f6c69646974790000000000000000000000000000000000000000
bytes1 public _byte = _byte32[0]; // bytes1: 0x4d


// Enumeration
// Let uint 0, 1, 2 represent Buy, Hold, Sell
enum ActionSet { Buy, Hold, Sell }
// Create an enum variable called action
ActionSet action = ActionSet.Buy;

// Enum can be converted into uint
function enumToUint() external view returns(uint){
return uint(action);
}
}

Binary file added 02_ValueTypes_en/step1/img/2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 02_ValueTypes_en/step1/img/2-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 02_ValueTypes_en/step1/img/2-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 160 additions & 0 deletions 02_ValueTypes_en/step1/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# WTF Solidity Tutorial: 2. Value Types

Twitter: [@0xAA_Science](https://twitter.com/0xAA_Science) | [@WTFAcademy_](https://twitter.com/WTFAcademy_)

Community: [Discord](https://discord.gg/5akcruXrsk)|[Wechat](https://docs.google.com/forms/d/e/1FAIpQLSe4KGT8Sh6sJ7hedQRuIYirOoZK_85miz3dw7vA1-YjodgJ-A/viewform?usp=sf_link)|[Website wtf.academy](https://wtf.academy)

Codes and tutorials are open source on GitHub: [github.com/AmazingAng/WTF-Solidity](https://github.com/AmazingAng/WTF-Solidity)


-----

## Variable Types

1. **Value Type**:This includes boolean, integer, etc. These variables directly pass values when assigned.

2. **Reference Type**:including arrays and structures. These variables take up more space, directly pass addresses (similar to pointers) when assigned, and can be modified with multiple variable names.

3. **Mapping Type**: hash tables in Solidity.

4. **Function Type**:The Solidity documentation classifies functions into value types. But it's very different from other types, and I put it in a separate category.

Only the commonly used types will be introduced here. In this chapter, we will introduce value types.

## Value types

### 1. Boolean

Boolean is a binary variable, and its values are `true` or `false`.

```solidity
// Boolean
bool public _bool = true;
```

Operators for Boolean type include:

- `!` (logical NOT)
- `&&` (logical AND)
- `||` (logical OR)
- `==` (equality)
- `!=` (inequality)

Code:

```solidity
// Boolean operators
bool public _bool1 = !_bool; // logical NOT
bool public _bool2 = _bool && _bool1; // logical AND
bool public _bool3 = _bool || _bool1; // logical OR
bool public _bool4 = _bool == _bool1; // equality
bool public _bool5 = _bool != _bool1; // inequality
```

From the above source code: the value of the variable `_bool` is `true`; `_bool1` is not`_bool`, which yields `false`; `_bool && _bool1` is `false`;`_bool || _bool1` is `true`;`_bool == _bool1` is `false`;and `_bool != _bool1` is `true`.

**Important note:** The `&&` and `||` operator follows a short-circuit evaluation rule. This means that for an expression such as `f(x) || g(y)`, if `f(x)` is `true`, `g(y)` will not be evaluated.

### 2. Integers

Integers types in Solidity include signed integer `int` and unsigned integer `uint`. It can store up to a 256-bit integers or data units.

```solidity
// Integer
int public _int = -1; // integers including negative integers
uint public _uint = 1; // unsigned integers
uint256 public _number = 20220330; // 256-bit unsigned integers
```
Commonly used integer operators include:

- Inequality operator (which returns a Boolean): `<=`, `<`, `==`, `!=`, `>=`, `>`
- Arithmetic operator: `+`, `-`, `*`, `/`, `%` (modulo), `**` (exponent)

Code:

```solidity
// Integer operations
uint256 public _number1 = _number + 1; // +, -, *, /
uint256 public _number2 = 2**2; // Exponent
uint256 public _number3 = 7 % 2; // Modulo (Modulus)
bool public _numberbool = _number2 > _number3; // Greater than
```

You can run the above code and check the values of each variable.

### 3. Addresses

Addresses have the following 2 types:
- `address`: Holds a 20 byte value (size of an Ethereum address).

- `address payable`: Same as `address`, but with the additional members `transfer` and `send` to allow ETH transfers.

Code:

```solidity
// Address
address public _address = 0x7A58c0Be72BE218B41C608b7Fe7C5bB630736C71;
address payable public _address1 = payable(_address); // payable address (can transfer fund and check balance)
// Members of address
uint256 public balance = _address1.balance; // balance of address
```

### 4. Fixed-size byte arrays

Byte arrays in Solidity come in two types:

- Fixed-length byte arrays: belong to value types, including `byte`, `bytes8`, `bytes32`, etc, depending on the size of each element (maximum 32 bytes). The length of the array can not be modified after declaration.
- Variable-length byte arrays: belong to reference type, including `bytes`, etc. The length of the array can be modified after declaration. We will learn more detail in later chapters


Code:

```solidity
// Fixed-size byte arrays
bytes32 public _byte32 = "MiniSolidity";
bytes1 public _byte = _byte32[0];
```

In the above code, we assigned the value `MiniSolidity` to the variable `_byte32`, or in hexadecimal: `0x4d696e69536f6c69646974790000000000000000000000000000000000000000`

And `_byte` takes the value of the first byte of `_byte32`, which is `0x4d`.

### 5. Enumeration

Enumeration (`enum`) is a user-defined data type within Solidity. It is mainly used to assign names to `uint`, which keeps the program easy to read.

Code:

```solidity
// Let uint 0, 1, 2 represent Buy, Hold, Sell
enum ActionSet { Buy, Hold, Sell }
// Create an enum variable called action
ActionSet action = ActionSet.Buy;
```

It can be converted to `uint` easily:

```solidity
// Enum can be converted into uint
function enumToUint() external view returns(uint){
return uint(action);
}
```

`enum` is a less popular type in Solidity.

## Demo in Remix

- After deploying the contract, you can check the values of each variable:

![2-1.png](https://raw.githubusercontent.com/remix-project-org/remix-workshops/master/02_ValueTypes_en/step1/img/2-1.png)

- Conversion between enum and uint:

![2-2.png](https://raw.githubusercontent.com/remix-project-org/remix-workshops/master/02_ValueTypes_en/step1/img/2-2.png)

![2-3.png](https://raw.githubusercontent.com/remix-project-org/remix-workshops/master/02_ValueTypes_en/step1/img/2-3.png)

## Summary

In this chapter, we introduced the variable types in Solidity, they are value type, reference type, mapping type, and function type. Then we introduced commonly used types: boolean, integer, address, fixed-length byte array, and enumeration in value types. We will cover other types in the subsequent tutorials.
12 changes: 12 additions & 0 deletions 03_Function_en/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
id: '03'
name: 3. Function
summary: 'Here''s the format of a function in Solidity: ```solidity function <function
name>(<parameter types>) [internal|external] [pure|view|payable] [returns (<return
types>)]'
level: beginner
tags:
- solidity
- tutorial
steps:
- name: Function
path: step1
4 changes: 4 additions & 0 deletions 03_Function_en/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# WTF Solidity Tutorial: 3. Function



Loading
Loading