基于以太坊的智能合约开发教程[Solidity] 第一篇:Solidity智能合约基础实战

第一篇:Solidity智能合约基础实战

第一节 实现自己的第一个智能合约

1. 描述一个对象有两种方式

①属性、状态

②行为

2.代码
pragma solidity ^0.5.0;

contract HelloWorld{
    string Myname = "lynn";
    
    function getName() public view returns(string){
        return Myname;
    }

    function changeName(string _newName) public{
        Myname = _newName;
    }

}

错误信息:

(1)

browser/HelloWorld.sol:6:44: TypeError: Data location must be "memory" for return parameter in function, but none was given.
    function getName() public view returns(string){
                                           ^----^

function getName() public view returns(string) 修改为:

function getName() public view returns(string memory)

(2)

browser/HelloWorld.sol:10:25: TypeError: Data location must be "memory" for parameter in function, but none was given.
    function changeName(string _newName) public{
                        ^-------------^

function changeName(string _newName) public 修改为:

function changeName(string memory _newName) public

修改后:

pragma solidity ^0.5.0;

contract HelloWorld{
    string Myname = "lynn";
    
    function getName() public view returns(string memory){
        return Myname;
    }

    function changeName(string memory _newName) public{
        Myname = _newName;
    }

}

第二节 节省gas的利器pure和view

1. 代码
pragma solidity ^0.5.0;

contract HelloWorld{
    string Myname = "lynn";
    
    function getName() public view returns(string memory){
        return Myname;
    }

    function changeName(string memory _newName) public{
        Myname = _newName;
    }
    
    function pureTest(string memory _name) pure public returns(string memory){
        return _name;
    }

}
  • view、pure不消耗gas
  • 函数changeName()需要改变状态变量,因此需要全网节点来同步,会消耗gas

第三节 真假bool值

1.代码
pragma solidity ^0.5.0;

contract BooleanTest{
    
    bool _a;
    int num1 = 100;
    int num2 = 200;
    
    function getBool() public view returns(bool){
        return _a;
    }
    
    function getBool2() public view returns(bool){
        return !_a;
    }
    
    function panduan() public view returns(bool){
        return num1==num2;
    } 
    
    function panduan2() public view returns(bool){
        return num1!=num2;
    }
    
    function yu() public view returns(bool){
        return (num1==num2) && true;
    }
    
    function yu2() public view returns(bool){
        return (num1!=num2) && true;
    }
    
    function huo() public view returns(bool){
        return (num1==num2) && true;
    }
    
    
    function huo2() public view returns(bool){
        return (num1!=num2) && true;
    }
    
    function huo3() public view returns(bool){
        return (num1==num2) && false;
    }
}

第四节 整型特性与运算

1.代码
pragma solidity ^0.5.0;

contract math{
    // int uint
    
    uint numa = 100;
    uint numb = 200;
    
    // uint numc = 2;
    
    function add(uint a, uint b) pure public returns(uint){
        return a+b;
    }
    
    function sub(uint a, uint b) pure public returns(uint){
        return a-b;
    }
    
    function mul(uint a, uint b) pure public returns(uint){
        return a*b;
    }
    
    function div(uint a, uint b) pure public returns(uint){
        return a/b;
    }
    
    function mod(uint a, uint b) pure public returns(uint){
        return a%b;
    }
    
    function squ(uint a,uint b) pure public returns(uint){
        return a**b;
}
  • int 代表可正可负,uint 只能代表正。

第五节 底层位运算

1.六个操作符

& | ~ ^ << >>

2.代码
pragma solidity ^0.5.0;

contract math{
    // int uint
    
    uint numa = 100;
    uint numb = 200;
    
    // uint numc = 2;
    
    function add(uint a, uint b) pure public returns(uint){
        return a+b;
    }
    
    function sub(uint a, uint b) pure public returns(uint){
        return a-b;
    }
    
    function mul(uint a, uint b) pure public returns(uint){
        return a*b;
    }
    
    function div(uint a, uint b) pure public returns(uint){
        return a/b;
    }
    
    function mod(uint a, uint b) pure public returns(uint){
        return a%b;
    }
    
    function squ(uint a,uint b) pure public returns(uint){
        return a**b;
    }
    
    uint8 a = 3;
    uint8 b = 4;
    
    function weiyu() pure public returns(uint){
        return a&b;
    }

    function weihuo() pure public returns(uint){
        return a|b;
    }
        
    function weifan() pure public returns(uint){
        return ~a;
    }
    
    function weiyihuo() pure public returns(uint){
        return a^b;
    }
    
    function zuoyi() pure public returns(uint){
        return a<<1;
    }
        
    function youyi() pure public returns(uint){
        return a>>1;
    }
}

错误提示:

browser/Math.sol:44:16: TypeError: Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
        return a&b;

function weiyu() pure public returns(uint) 修改为:

function weiyu() view public returns(uint)

修改后代码:
pragma solidity ^0.5.0;

contract math{
    // int uint
    
    uint numa = 100;
    uint numb = 200;
    
    // uint numc = 2;
    
    // function add() view public returns(uint){
    //     return numa + numb;
    // }
    
    function add(uint a, uint b) pure public returns(uint){
        return a+b;
    }
    
    function sub(uint a, uint b) pure public returns(uint){
        return a-b;
    }
    
    function mul(uint a, uint b) pure public returns(uint){
        return a*b;
    }
    
    function div(uint a, uint b) pure public returns(uint){
        return a/b;
    }
    
    function mod(uint a, uint b) pure public returns(uint){
        return a%b;
    }
    
    function squ(uint a,uint b) pure public returns(uint){
        return a**b;
    }
    
    uint8 a = 3;
    uint8 b = 4;
    
    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;
    }
    
    function weiyihuo() view public returns(uint){
        return a^b;
    }
    
    function zuoyi() view public returns(uint){
        return a<<1;
    }
        
    function youyi() view public returns(uint){
        return a>>1;
    }
}

第六节 危险的整数溢出以及异常处理

1. 代码
    function flow() view public returns(uint){
        uint8 mm = 255;
        mm++;               
        return mm;                  //溢出,返回0
    }
    
    function flow2() view public returns(uint){
        uint8 mm = 0;
        mm--;
        return mm;                  //溢出,返回255
    }
    
    function errorTest() public returns(int){
        int a = 2;
        int b = 0;  不能执行除零
        return a/b;
    }

***错误提示:***

    function flow() view public returns(uint){
        uint8 mm = 255;
        mm++;
        return mm;
    }
    
    function flow2() view public returns(uint){
        uint8 mm = 0;
        mm--;
        return mm;
    }
    
    function errorTest() public returns(int){
        int a = 2;
        //int b = 0;  不能执行除零
        //return a/b;
        int b = -1;
        a<

***错误提示:***

第七节 整数字面量

1.代码
    function intergerTest() public returns(uint){
        uint num = (2**800+1)-2**800;
        return num;
    }
    
    function intergerTest2() public returns(uint){
        // uint num = 2/4.0;   报错,如下所示
        uint num = 2/4.0*1000;
        return num;
    }
    
    function intergerTest3() public returns(uint){
        uint num = 111111111111111111111111111111111111111111112-                     111111111111111111111111111111111111111111111;
        return num;
    }

uint num = 2/4.0 错误提示:

browser/Math.sol:93:9: TypeError: Type rational_const 1 / 2 is not implicitly convertible to expected type uint256. Try converting to type ufixed8x1 or use an explicit conversion.
        uint num = 2/4.0;
        ^--------------^
总结:

满足精度的需要,可以计算出来只看最后的结果满不满足,赋值给最后的变量

第八节 固定长度字节数组

1. 关键字有:bytes1,bytes2,bytes3,…,bytes32。(以步长1递增)

byte代表bytes1

2.代码
pragma solidity ^0.5.0;

contract ByteArray{
    // 0x7a68656e676a69616e78756e
    bytes1  public num1 = 0x7a;  // 0111 1010    // 加上public后可以自动get到该变量
    bytes2  public num2 = 0x7a68; // 0111 1010 0110 1000
    bytes12 public num3 = 0x7a68656e676a69616e78756e; 
    
    function getLength() public returns(uint){
        return num1.length;     //output: "uint256: 1"
    }
    
    function getLength2() public returns(uint){
        return num2.length;     //output: "uint256: 2"
    }
    
    function getLength3() public returns(uint){
        return num3.length;     //output: "uint256: 12"
    }
}
  • Length不能被修改

    function setLength(){
    	num1.length=18;
    }
    

    错误信息:

    browser/ByteArray.sol:22:5: ParserError: Expected pragma, import directive or contract/interface/library definition.
        function setLength(){
        ^------^
    

第九节 固定长度字节数组深入

1. 代码
pragma solidity ^0.5.0;

contract ByteArray{
    // 0x7a68656e676a69616e78756e
    bytes1  public num1 = 0x7a;  // 0111 1010
    bytes2  public num2 = 0x7a68; // 0111 1010 0110 1000
    bytes12 public num3 = 0x7a68656e676a69616e78756e; 
    
    bytes1 public a = 0x7a;    //新增
    bytes1 public b = 0x68;    //新增
    
    function getLength() public returns(uint){
        return num1.length;
    }
    
    function getLength2() public returns(uint){
        return num2.length;
    }
    
    function getLength3() public returns(uint){
        return num3.length;
    }
    // 新增比较操作
    function bijiao() public returns(bool){
        return a>b;
    }
    
    function bijiao2() public returns(bool){
        return a>=b;
    }
    
    function bijiao3() public returns(bool){
        return a>1;      // 0x3d
    }
}

第十节 动态字节数组

1. 代码
pragma solidity ^0.5.0;

contract DynamicByte{
    bytes public name = new bytes(2);  //新建bytes数组,两个空间,public动态生成get方法
    
    function InitName() public{
        name[0] = 0x7a;
        name[1] = 0x68;
    }
    
    function getLength() public view returns(uint){
        return name.length;
    }
    
    function changeName() public{     //改变名字
        name[0] = 0x88;
    }
    
    function changeLength() public{    // 改变数组长度
        name.length = 5;       // 扩充长度在后(右)边加0
    }
    
    function pushtest() public{
        name.push(0x99);       //添加元素在原来数字的后边
    }
}

你可能感兴趣的:(区块链,solidity)