23-02-28

First Post:

Last Update:

2023-02-28

锁与Modifier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
pragma solidity ^0.8.17;
//SPDX-License-Identifier: MIT
contract lock_ {

address owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(owner == msg.sender, "not owner! ");
_;
}
modifier validAddress(address addr) {
require(addr != address(0), "not valid address!");
_;
}
function changeOwner(address newOwner) public onlyOwner validAddress(newOwner) {
owner = newOwner;
//更改账户拥有者需要确认是否为袁拥有者跟新地址有效性
}

bool lock;
modifier noReentrancy {
require(!lock, "No reentrancy!");//函数被调用的时候拒绝访问
lock = true;
_;
lock = false;

}
uint x = 10;
function decrease(uint i) public noReentrancy {
x-=i;
if(i>1) {
decrease(i-1);
}
}
}

事件

1
2
3
4
5
6
7
8
9
10
11
12
pragma solidity ^0.8.17;
//SPDX-License-Identifier:MIT
contract eventTest {
event log(address indexed caller, string message);
event anotherLog();
function test() public {
emit log(address(0), "hello1");
emit log(msg.sender, "hello2");
emit anotherLog();
}

}

继承相关的构造器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pragma solidity ^0.8.17;
//SPDX-License-Identifier:MIT
contract A{
uint x;
constructor(uint _x) {
x = _x;
}
}
contract B{
uint y;
constructor(uint _y) {
y = _y;
}
}
contract C is B(1), A(2) {

}
contract D is B, A {
constructor(uint _x, uint _y) A(_x) B(_y) {

}
}
contract E is A, B {
constructor(uint _x, uint _y) B(_y) A(_x) {

}
}
contract F is A, B {
constructor() A(1) B(2) {
}
}