zoukankan      html  css  js  c++  java
  • solidity语言

    IDE:Atom

      插件:autocomplete-solidity 代码自动补齐

          linter-solium,linter-solidity代码检查错误

          language-ethereum支持solidity代码高亮

    1. 合约文件结构

      版本号  :

      import

      合约 :contract 

    pragma solidity ^0.4.0;
    
    import "solidity_for_import.sol";
    
    // this is a test contract
    contract Test{
        uint a;
        function setA(uint x) public {
            a = x;
        }
        //事件
        event Set_A(uint x);
    
        //结构体
        struct Pos{//经纬度
            int lat; 
            int lng;
        }
    
    
        address public owerAddr;
        //函数修改器, 在函数执行前先执行函数修改器的代码
        modifier owner() {
            require(msg.sender == owerAddr);
            _;
        }
        function mine () public owner{
            a+=1;
        }
    } 
    solidity1.sol

    2. 数据类型值类型

      solidity官网

    pragma solidity ^0.4.0;
    
    // this is a test contract
    contract Test{
        uint a=10;
        uint b=20;
        function setA(uint x) public constant returns (uint) {
            a=x;
            return a+10;
        }
        
    }

    2.1 bool

       取值:true/false

      运算符:!  && || == !=

    2.2 整形

      int/unit

      运算符:<   <=   ==

        位运算:& |  ^(异或)  ~(位取反)

        算数运算:+,-,* ,/,%,**,<<,>>

       constant 关键字的函数不会修改状态变量的值

     2.3 常量

      数字常量计算不会溢出

    pragma solidity ^0.4.0;
    
    // this is a test contract
    contract Test{
      //状态变量 uint a
    =10; uint b=20; function testLiterals() public constant returns(int){ return 1+2e10; } }

       字符串常量:  "hello world"

      十六进制常量:hex"abcd"

    十六进制数可以转化为字节数组

        function testHex() public constant returns(bytes2,bytes1,bytes1){
            bytes2 a=hex"abcd";
            return (a, a[0], a[1]);
        }

       地址常量:用20个字节的16进制数表示

    属性:balance表示余额

        transfer() 用来转义以太币

    合约也是个地址类型。

      

    pragma solidity ^0.4.0;
    
    contract AddrTest{
        //接受以太币 
        function deposit() public payable{
            
        }
        //获取当前地址的余额
        function getBalance() public constant returns(uint){
            return this.balance;
        }
        // transfer ether
        function transferEther(address target) public {
            target.transfer(10);
        }
    }

    3. 引用类型

      memory, storege

    3.1 数组 Arrays

      对于非中文字符,占用1个字节【ASCII】;对于中文字符,占用三个字节【】

        function newMem(uint len) constant public{
            //内存变量
            uint[] memory arr = new uint[](len);
        }
        function param(uint[3] arr) constant public{
            
        }

    4. 区块和交易属性

     5. 错误处理

     处理函数: assert,require

  • 相关阅读:
    再谈iframe自适应高度
    一个软件外包老鸟对外包业的反思
    需求分析中减少客户摩擦的若干法则
    C# 进制转换
    'filter' is not a known css property name
    Ajax之ModalPopupExtender 的后台调用
    Microsoft SQL Server 中的小数数据类型
    ExtJS Combobox 如何设置默认和取值问题
    wp7 控制控件显隐
    wp7 MediaElement播放
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/9396382.html
Copyright © 2011-2022 走看看