Solidity语言学习学习总结
文章目录
Solidity语言学习学习总结
一、合约文件剖析
1.1一个合约文件的结构如下:
1.2合约的引入import
1.3引入状态变量、函数、事件、函数修改器
二、Solidity语言类型
2.1常量
2.2地址类型
2.3bool真假值类型
2.4整型特性与运算
2.5底层位运算
2.6固定长度字节数组byte
使用byte数组的理由:
三、数据存储
3.1string内存原理
四、以太坊地址本质
五、使用钱包转移资金
六、智能合约众筹例子
6.1、角色说明
关于solidity语言我们从三个方面来学习,分别从
文件结构
数据类型
错误处理(独特)
我们在学习一门新语言的时候,通常从基础语法到函数,再到文件、结构体进行学习,最后深入到框架。以上三个点是solidity语言区别于其他语言的三个显著点,所以提出来着重讲。
这是solidity官方文档:https://learnblockchain.cn/docs/solidity/installing-solidity.html#remix
一、合约文件剖析
1.1一个合约文件的结构如下:
这是一个完整的合约结构:
1.2合约的引入import
import "文件名.sol"
1
两个合约文件的引入:
1.3引入状态变量、函数、事件、函数修改器
pragma solidity ^0.4.0;
import "solidity_for_import.sol";//引入另外一个合约
/*this is a Contract
@auth:Linghu
*/
contract Test{
//引入状态变量
uint a;
//引入函数
function setA(uint x) public {
a=x;
//调用setA时触发事件Set_A
emit Set_A(x);//利用web3随时监听我们的事件
}
//引入事件
event Set_A(uint a);
//定义结构体
struct Position{
int lat;
int lng;
}
address public ownerAddr;
//定义函数修改器
modifier ownerAddr(){
require(msg.sender==ownerAddr);
_;
}
function mine() public owner {
a+=1;
}
}
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
38
39
40
41
42
43
44
二、Solidity语言类型
2.1常量
有理数常量和整型常量
字符常量
十六进制常量
地址常量
2.2地址类型
address:表示一个账户地址(20B)
成员 属性 函数
balance transfer()
balance表示账户地址的余额;函数transfer表示地址转移的以太币;
2.3bool真假值类型
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.8.7;
contract BooleanTest{
bool a;
function getBool() public view returns(bool){
return a;
}
/*
function getBool2() public view returns(bool){
return !a;//取反
}
*/
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
默认返回值是 false
2.4整型特性与运算
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.8.7;
contract Math{
uint numa=4;
uint numb=2;
function add(uint a,uint b)public pure returns(uint){
return a+b;
}
function jian(uint a,uint b)public pure returns(uint){
return a-b;
}
function cheng(uint a,uint b)public pure returns(uint){
return a*b;
}
function chu(uint a,uint b)public pure returns(uint){
return a/b;
}
}
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
2.5底层位运算
&
|
^
<< 左移
》》(>>)不好表示括号里的符号 右移
1.& 操作数之间转换成二进制之后每位进行与运算操作(同1取1)
2.| 操作数之间转换成二进制之后每位进行或运算操作(有1取1)
3.~ 操作数转换成二进制之后每位进行取反操作(直接相反)
4.^ 操作数之间转换成二进制之后每位进行异或操作(不同取1)
5.<<操作数转换成二进制之后每位向左移动x位的操作
6.>>操作数转换成二进制之后每位向右移动x位的操作
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.8.7;
contract Math{
uint a=4;
uint b=2;
function weiyu()view public returns(uint){
return a & b;
}
function weihuo()view public returns(uint){
return a|b;
}
function weifan()view public returns(uint){
return a^b;
}
function zuoyi()view public returns(uint){
return a<<b;
}
function youyi()view public returns(uint){
return a>>b;
}
}
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
2.6固定长度字节数组byte
一个byte=8个位(XXXX XXXX)X为0或1,二进制表示
byte数组为bytes1,bytes2,。。。,bytes32,以八个位递增,即是对位的封装
举例
bytes1=uint8
bytes2=unit16
。
。
。
bytes32=unit256
使用byte数组的理由:
1.bytesX可以更好地显示16进制
举例:bytes1=0x6A,bytes1=(XXXX XXXX)正好四个表示一个16进制,以此类推
2.bytes数据声明时加入public可以自动生成调用长度的函数,见下
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.8.7;
contract Math{
bytes1 public num1=0x12;
bytes4 public num2=0x12121212;
}
1
2
3
4
5
6
7
8
9
3.bytes内部自带length长度函数,而且长度固定,而且长度不可以被修改。
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.8.7;
contract Math{
bytes1 public num1=0x12;
bytes4 public num2=0x12121212;
function getlength1()public view returns(uint8){
return num1.length;
}
function getlength2()public view returns(uint8){
return num2.length;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
三、数据存储
3.1string内存原理
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.8.7;
contract DynamicByte{
string name="linghu";
function getLength() public view returns(uint){
return bytes(name).length;//通过byte强制转换获取长度
}
function changename() public view returns(bytes1){
return bytes(name)[1];
}
function getName() public view returns(bytes memory){
return bytes(name);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
四、以太坊地址本质
账户初始化的地址为:
每个账户都有自己的地址。
五、使用钱包转移资金
payable关键字代表我们可以通过这个函数给我们的合约进行充值、转账、默认、
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.8.7;
contract PayableTest{
function pay() payable{
}
//获取账户上的金额
function getBalance() returns(uint){
return address(this).balance;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
地址主要有两个:
合约的地址 this代表合约地址
账户的地址
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.4.0;
contract Transfer{
function transfer() public payable{
address account=0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
account.transfer(msg.value);//把钱转到account中去
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
六、智能合约众筹例子
6.1、角色说明
该案例涉及两个角色:
募集资金者(受益者)
资金投资者(捐赠者)
// SPDX-License-Identifier: SimPL-2.0
pragma solidity ^0.4.4;
contract zhongchou{
//对象二:捐赠者
struct funder{
address Funderaddress;
uint Tomoney;//捐款金额
}
//对象一:受益者
struct needer{//声明受益者的结构体
address Neederaddress;//声明一个变量地址,方便打钱给受益者
uint goal;//目标金额
uint amout;//已经募得金额
uint funderAccount;//多少捐赠者
mapping(uint=>funder)map;//映射,将捐赠者的id与捐赠者绑定在一起,从而能够得知,是谁给当前的受益人捐钱了。
}
uint neederAmount;//受益人的id数量
mapping(uint=>needer) needmap;
//行为:
function NewNeeder(address _Neederaddress,uint _goal){
//创建对象,将id号与受益者联系在一起了
neederAmount++;
needmap[neederAmount]=needer(_Neederaddress,_goal,0,0);
}
//@param _address 捐赠者的地址,
//@param _neederAmount 受益人的id
function contribute( address _address, uint _neederAmount) payable{
//通过id获取到受益人对象
needer storage _needer = needmap[_neederAmount];
//聚集到的资金增加
_needer.amount += msg.value;//动态获取
// 捐赠人数增加
_needer.funderAcoount++;
//将受益人id与受益人绑定
_needer.map[ _needer.funderAcoount] = funder(_address , msg.value );
}
//当募集到的资金满足条件,就会给给受益人的地址转账
//@param _neederAmount 受益人的id
function ISconpelete( uint _neederAmount){
needer storage _needer = needmap[_neederAmount];
if(_needer.amount >=_needer.goal ){
_needer.Neederaddress.transfer(_needer.amount);
}
}
}
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
与受益人绑定
_needer.map[ _needer.funderAcoount] = funder(_address , msg.value );
}
//当募集到的资金满足条件,就会给给受益人的地址转账
1
//@param _neederAmount 受益人的id
function ISconpelete( uint _neederAmount){
needer storage _needer = needmap[_neederAmount];
if(_needer.amount >=_needer.goal ){
_needer.Neederaddress.transfer(_needer.amount);
}
}