Solidity 学习

Solidity 与智能合约

Solidity 是一门面向合约,为实现智能合约而创建的高级编程语言
智能合约:


“一个智能合约是一套以数字形式定义的承诺(promises),包括合约参与方可以在上面执行这些承诺的协议。”

智能合约的本质:数字话合同
特点:代码代替人仲裁和执行合同,同时能够触发支付

Solidity 基础语法

数量类型

  • 值类型(值传递)
    • bool
      • true/false
        • 定义形式
          • bool flag:
          • bool flag = false:
    • 整形
      • 有符号整型 有正负 — 默认 int 是int256
      • 无符号整型 无正负---- 默认uint 是uint256
      • 以8位为区间
        • int8、int16、int24。。直到int256
        • uint同理,也是以8位为区间
    • 地址
      • balance
      • transfer
        -
    • 定长数组(字节类型)
      • byte1…byte32
    • 枚举
      • enum
    • 函数
      • 函数签名(返回值,参数类型,修饰符)
      • view/constant/pure
      • payable
      • 访问可见性 public/private
      • 返回值 returns
  • 引用类型(指针传递)
    • 字符串
    • 不定长数组
    • 数组
    • 枚举
    • 映射 mapping
    • 结构体

值传递和引用传递
值传递:值变了在传递过程中讲数值完整的copy一份,在赋值给新的变量,这种方式需要重新开辟新的内存空间, 两个变量完全独立,互不影响,修改一个不会影响到另外一个,缺点就是效率低
引用类型:solidity没有指针类型,对于复杂的结构进行高效传递方式是使用关键字storage进行修饰,简单来说,如果在变量之前添加storage就是引用传递,不加就是值传递,但是只对复杂类型有效,复杂类型,占用空间较大,所以考虑通过引用传递

Solidity 编译工具

https://remix.ethereum.org/#optimize=false&runs=200&evmVersion=null&version=soljson-v0.8.7+commit.e28d00a7.js

函数的关键字

修饰符说明
public共有,任何人(拥有以太坊账户的)都可以调用。
private私有,只有智能合约内部可以调用
external仅合约外部可以调用,合约内部可以使用this调用
interanl仅合约内部和继承的合约可以调用
view/constant函数会读取但是不会修改任何合约的状态变量
pure函数布使用任何智能合约的状态变量
payable调用函数需要付钱,钱付给了智能合约的账户
returns指定函数返回值

注释

可以使用单行注释 ( //) 和多行注释 ( //)。

HelloWorld.sol

// SPDX-License-Identifier: MIT
//SPDX(软件包数据交换) 许可证标识符:主要处理版权方面的法律问题

pragma  solidity 0.8.7;
//版本编译指示
// * 使用版本编译指示不会更改编译器的版本。它也不会启用或禁用编译器的功能。它只是指示编译器检查其版本是否与编译指示所需的版本匹配。如果不匹配,编译器会发出错误

// 定义合约用contract关键字
contract HelloWorld {

    string public myString = "hello world";
    // 
}

数据类型

// SPDX-License-Identifier: MIT
pragma  solidity 0.8.7;

contract ValueTypes {

    // 布尔
    bool public b = true;

    // uint 无符号整数,即正整数
    uint public u = 123; // uint = uint256,(0, 2**256-1)    
                         //        uint8, (0,2**8-1)
                         //        uint16,(0,2**16-1)
    
    // int 有符号整数,即负整数
    int public i = -123; // int = int256, -2**255 to 2*255-1

	// 获取int的最小值
    int public minInt = type(int).min;
    // 获取int的最大值
    int public maxInt = type(int).max;


    // address
    address public addr = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;

    // //byte
    // bytes32 public b32 = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb26123;


}

函数简介

// SPDX-License-Identifier: MIT
pragma  solidity 0.8.7;

contract FunctionIntro {

    // external : 外部函数
    // pure: 纯函数,不能读取状态变量和修改状态变量
    // returns: 返回值,可以是多个
    function add(uint x, uint y) external pure returns (uint){
        return x + y;
    }

    function sub(uint x, uint y) external pure returns (uint){
        return x - y ;
    }

}

变量

// SPDX-License-Identifier: MIT
pragma  solidity 0.8.7;


contract Variables {

    // 状态变量、局部变量、全局变量
    // 全局变量
    function globalVars() external view returns(address, uint, uint){
        address sender = msg.sender;
        uint timestamp = block.timestamp;
        uint blockNum = block.number;

        return (sender, timestamp, blockNum);
    }

    // 状态变量
    uint public myUint = 123; // 如果不写修改方法,这个变量的值将永久存在区块链

    uint public i;
    bool public b;
    address public myAddress;


    function foo() external {
        // 局部变量
        // uint notStateVariable = 456;
        uint x = 123;
        bool f = false;

        x += 456;
        f = true;

        i = 123;
        b = true;
        myAddress = address(1);
    }
}

只读函数

// SPDX-License-Identifier: MIT
pragma  solidity 0.8.7;

// view 修饰的函数可以读取状态变量,但是不能修改
// pure 修饰的函数,不可读取状态变量,也不能修改
contract ViewAndPureFunctions {

    uint public num;

    function ViewFunc() external view returns (uint){
        return num;
    }


    function PureFunc() external pure returns (uint){
        // return num;
        return 1;
    }

    function addToNum(uint x) external view returns (uint) {
        return  num + x;
    }

    function add(uint x, uint y) external pure returns (uint){
        return x + y ;
    }

}

默认值

// SPDX-License-Identifier: MIT
pragma  solidity ^0.8.7;

contract DefaultValues {
    bool public b; // false
    uint public u; // 0
    int public i; // 0
    address  public adr; //0x0000000000000000000000000000000000000000
    bytes32 public b32; //0x0000000000000000000000000000000000000000000000000000000000000000

}

常量

// SPDX-License-Identifier: MIT
pragma  solidity ^0.8.7;

contract Constants {
    address public constant MY_ADDRESS = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;
    uint public constant MY_UINT = 123;
}

If-Else

// SPDX-License-Identifier: MIT
pragma  solidity ^0.8.7;


contract IfElse{

    function example(uint _x) external pure returns (uint){
        if (_x < 10){
            return 1;
        }else if (_x < 20){
            return 2;
        }
        return 3;
    }

    function ternary(uint _x) external pure returns (uint){
        // if (_x < 10){
        //     return 1;
        // }
        // return 2;
        // 三元运算符
        return  _x < 10 ? 1 : 2;

    }
}

ForAndWhile

// SPDX-License-Identifier: MIT
pragma  solidity ^0.8.7;


contract ForAndWhileLoops{
    function loops() external pure {
        for (uint i = 0; i < 10; i++){
            if (i == 3){
                continue;
            }
            if (i == 5){
                break;
            }
        }
        uint j = 0;
        while (j < 10) {
            j++;
        }
    }

    function sum(uint _n) external pure returns (uint){
        uint s;
        for (uint i = 1; i<= _n; i++){
            s += i;
        }
        return s;
    }
}

Error

// SPDX-License-Identifier: MIT
pragma  solidity ^0.8.7;
// require ,
// revert,
// assert
// custom
contract ErrorDemo {

    function testRequire(uint _i) public pure {
        require (_i < 10, "i > 10");
    }
    
    function testRevert(uint _i) public pure {
        if (_i > 10){
            revert ( "i > 10");
        }
        
    }
    uint public num = 123;
    function testAssert() public view {
        assert (num == 123);
    }

    error MyError(address caller, uint i);

    function testCustomError(uint _i) public view{
        if (_i > 10){
            revert MyError(msg.sender, _i);
        }
    }

}

构造函数

// SPDX-License-Identifier: MIT
pragma  solidity ^0.8.7;


contract Construct{
    address public owner;
    uint public x;

    constructor(uint _x){
        owner = msg.sender;
        x = _x;
    }
}

返回值

// SPDX-License-Identifier: MIT
pragma  solidity ^0.8.7;


contract FunctionOutputs{
    // 返回多个值
    function returnMany() public pure returns (uint , bool){
        return (1, false);
    }

    // 返回值命名
    function named() public pure returns (uint x, bool b) {
        return (1, true);
    }

    // 隐式返回
    function assigned() public  pure returns(uint x, bool b){
        x = 1;
        b = true;

    }

    // 合约调用返回值
    function destructingAssigment() public pure {
        // 调用2个返回值
        (uint x, bool b) = returnMany();
        // 调用单个返回值
        (,bool _b) = returnMany();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值