区块链之Solidity类型

Solidity语言

 

IDE:https://remix.ethereum.org

 

合约文件结构:

版本申明、import、合约(状态变量、函数、结构类型、事件、函数修改器)、代码注释

结构举例:

//^表示从0.4.0版本到0.5.0版本

pragma solidity ^0.4.0;

//import

import "s2.sol";

 

//This is a test Contract

//合约

contract Test{

    //状态变量

    uint a;

    //函数

    function setA(uint x) public owner{

        a = x;

        emit Set_A(x);

        }

    //事件

    event Set_A(uint a);

    //结构类型

    struct Position{

        int lat;

        int lng;

        }

    //函数修改器(类比python中的装饰器)

    modifier owner(){

        require(msg.sender == owerAddr);

        //函数插入至_这里

        _;

    }

}

 

类型:

值类型(value type)、引用类型(reference type)

 

值类型:

布尔类型

true/false

! && || == !=

pragma solidity ^0.4.0;

 

contract Test{

    bool boola = true;

    bool boolb = false;

    function testbool1() public returns(bool){

        return boola && boolb;

    }

    function testbool2() public returns(bool){

        return boola || boolb;

    }

}

 

整型

int/uint

int——有符号的整型

uint——无符号的整型

int8-int256

<= < == != >= >

& | ^ ~

+ - 一元+ 一元0- * / % ** << >>

pragma solidity ^0.4.0;

 

contract Test{

    uint a;

    int b = 20;

    int c = 30;

    function testadd() public constant returns(int){

        if(b > c){

            return b + c;

        } else if(b == c){

            return b * c;

        } else{

            return b >> 2;

        }

        -1 >> 2;

    }

}

常量(literals)

数字、字符串常量、十六进制常量、地址常量

常量不会有溢出

string没有结尾符/0

十六进制常量:hex"abcd",可以转化为字节数组byte2

地址类型:

address:表示一个账户地址(20字节)

balance:余额,属性

transfer():交易函数

pragma solidity ^0.4.16;

 

contract AddrTest{

    

    function deposit() public payable {

        //可以存款需要payable

    }

    

    function getBalance() public constant returns (uint) {

        return this.balance;

    }

    function transferEther(address towho) public {

        towho.transfer(10);

    }

}

地址常量:

满足地址合法性检查

 

引用类型:

数据位置:

memory、storage

数组:

T[k]:元素类型[固定长度]

T[]:长度动态调整的数组

bytes string

string可以转为bytes,bytes类似byte[]

成员:length、push()

pragma solidity ^0.4.16;

 

contract ArrayTest{

    

   uint[] public u = [1,2,3];

   string s = "abcdefg";

   

   function h() public constant returns (uint){

       return bytes(s).length;

   }

   

   function f() public constant returns (bytes){

       return bytes(s);

   }

   

   function newM(uint len) constant public returns (uint){

       uint[] memory a = new uint[](len);

       bytes memory b = new bytes(len);

       

       return a.length;

   }

   

   function g(uint[3] _data) public constant {

       

   }

}

结构体:

struct Funder{

    address addr;

    uint amount;

}

function newFunder() public {

        funder = Funder({addr:msg.sender,amount:10});

}

映射(Mappings):

mapping(address=>uint)

   mapping(address => uint) public balances;

   function updateBalance(uint newBalance) public{

       balances[msg.sender] = newBalance;

   }

 

全局变量及函数:

有关区块和交易、有关错误处理、有关数字及加密、有关地址和合约

msg.sender(address)    //获取交易地址

msg.value(uint)    //获取交易以太币,单位Wei

block.coinbase(address)    //当前块的地址

block.difficulty(uint)    //当前块的难度

block.number(uint)    //当前块的区号

block.timestamp(uint)    //当前块的时间戳

now(uint)    //当前块的时间戳

tx.gasprice(uint)    //当前块的价格

pragma solidity ^0.4.16;

 

contract ArrayTest{

    //构造函数设定payable

    constructor() payable{

    }

    

    function testApi() public constant returns(uint){

        return msg.value;    //获取交易以太币,单位Wei

    }

}

个人主页:www.unconstraint.cn

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值