美文网首页
solidity-2.数据类型

solidity-2.数据类型

作者: jection | 来源:发表于2018-11-20 16:19 被阅读0次

    数据类型有Value Types值类型Reference Types引用类型

    Value Types 值类型

    Address 地址

    长度:20 byte
    注意:非address类型的数据也可以强制转换成address
    address的成员
    balance变量,获取地址的eth余额(单位是wei)
    transfer(uint256 amount),发送eth到一个地址,如果执行失败抛出异常,合约将停止执行
    send(uint256 amount),发送eth到一个地址,如果执行失败返回false,合约继续执行
    call, callcode and delegatecall 看不懂

        address x = 0x123;
        address myAddress = this;
        if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10);
    

    Fixed-size byte arrays 固定大小字节数组

    bytes1, bytes2, bytes3, …, bytes32

    Enums 枚举

    Function Types 函数类型

    函数类型定义如下:

    function (<parameter types>) {internal|external} [pure|constant|view|payable] [returns (<return types>)]
    

    函数类型即是函数这种特殊的类型。

    • 可以将一个函数赋值给一个变量,一个函数类型的变量。
    • 还可以将一个函数作为参数进行传递。
    • 也可以在函数调用中返回一个函数。

    函数类型有两类;可分为internalexternal函数。
    若不写类型,默认的函数类型是internal的。

    注意:
    this.f 访问外部函数
    f 访问内部函数

    内部函数的调用:在EVM会翻译成简单的跳转指令。
    外部函数的调用:不是EVM的跳转指令,而是发起一个消息调用

    内部函数例子

    pragma solidity ^0.4.16;
    
    library ArrayUtils {
      // internal functions can be used in internal library functions because
      // they will be part of the same code context
      function map(uint[] memory self, function (uint) pure returns (uint) f)
        internal
        pure
        returns (uint[] memory r)
      {
        r = new uint[](self.length);
        for (uint i = 0; i < self.length; i++) {
          r[i] = f(self[i]);
        }
      }
      function reduce(
        uint[] memory self,
        function (uint, uint) pure returns (uint) f
      )
        internal
        pure
        returns (uint r)
      {
        r = self[0];
        for (uint i = 1; i < self.length; i++) {
          r = f(r, self[i]);
        }
      }
      function range(uint length) internal pure returns (uint[] memory r) {
        r = new uint[](length);
        for (uint i = 0; i < r.length; i++) {
          r[i] = i;
        }
      }
    }
    
    contract Pyramid {
      using ArrayUtils for *;
      function pyramid(uint l) public pure returns (uint) {
        return ArrayUtils.range(l).map(square).reduce(sum);
      }
      function square(uint x) internal pure returns (uint) {
        return x * x;
      }
      function sum(uint x, uint y) internal pure returns (uint) {
        return x + y;
      }
    }
    

    内部函数例子

    pragma solidity ^0.4.22;
    
    contract Oracle {
      struct Request {
        bytes data;
        function(bytes memory) external callback;
      }
      Request[] requests;
      event NewRequest(uint);
      function query(bytes data, function(bytes memory) external callback) public {
        requests.push(Request(data, callback));
        emit NewRequest(requests.length - 1);
      }
      function reply(uint requestID, bytes response) public {
        // Here goes the check that the reply comes from a trusted source
        requests[requestID].callback(response);
      }
    }
    
    contract OracleUser {
      Oracle constant oracle = Oracle(0x1234567); // known contract
      function buySomething() {
        oracle.query("USD", this.oracleResponse);
      }
      function oracleResponse(bytes response) public {
        require(
            msg.sender == address(oracle),
            "Only oracle can call this."
        );
        // Use the data
      }
    }
    

    Reference Types 引用类型

    引用类型的保存比值类型要昂贵的多。

    Data location 数据存储位置

    • storage 永久存储
    • memory 内存
    • calldata 内存只读

    强制数据存储位置:
    没有返回参数的外部函数: calldata
    合约的状态变量: storage

    默认数据存储位置:
    有返回值的函数: memory
    局部变量: storage

    arrays 数组类型

    成员

    length变量
    push方法

    • structs 结构类型
      案例如下:
    pragma solidity ^0.4.11;
    
    contract CrowdFunding {
        // Defines a new type with two fields.
        struct Funder {
            address addr;
            uint amount;
        }
    
        struct Campaign {
            address beneficiary;
            uint fundingGoal;
            uint numFunders;
            uint amount;
            mapping (uint => Funder) funders;
        }
    
        uint numCampaigns;
        mapping (uint => Campaign) campaigns;
    
        function newCampaign(address beneficiary, uint goal) public returns (uint campaignID) {
            campaignID = numCampaigns++; // campaignID is return variable
            // Creates new struct and saves in storage. We leave out the mapping type.
            campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
        }
    
        function contribute(uint campaignID) public payable {
            Campaign storage c = campaigns[campaignID];
            // Creates a new temporary memory struct, initialised with the given values
            // and copies it over to storage.
            // Note that you can also use Funder(msg.sender, msg.value) to initialise.
            c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value});
            c.amount += msg.value;
        }
    
        function checkGoalReached(uint campaignID) public returns (bool reached) {
            Campaign storage c = campaigns[campaignID];
            if (c.amount < c.fundingGoal)
                return false;
            uint amount = c.amount;
            c.amount = 0;
            c.beneficiary.transfer(amount);
            return true;
        }
    }
    

    Mappings 映射类型

    pragma solidity ^0.4.0;
    
    contract MappingExample {
        mapping(address => uint) public balances;
    
        function update(uint newBalance) public {
            balances[msg.sender] = newBalance;
        }
    }
    
    contract MappingUser {
        function f() public returns (uint) {
            MappingExample m = new MappingExample();
            m.update(100);
            return m.balances(this);
        }
    }
    

    Operators Involving LValues 运算符

    一般运算符

    a += e
    -=, *=, /=, %=, |=, &= and ^=
    a++ and a--

    delete运算符,重置值/对象。

    比如uint a = 10;delete a;
    将a重置为0

    pragma solidity ^0.4.0;
    
    contract DeleteExample {
        uint data;
        uint[] dataArray;
    
        function f() public {
            uint x = data;
            delete x; // sets x to 0, does not affect data
            delete data; // sets data to 0, does not affect x which still holds a copy
            uint[] storage y = dataArray;
            delete dataArray; // this sets dataArray.length to zero, but as uint[] is a complex object, also
            // y is affected which is an alias to the storage object
            // On the other hand: "delete y" is not valid, as assignments to local variables
            // referencing storage objects can only be made from existing storage objects.
        }
    }
    

    Conversions between Elementary Types 数据类型转换

    Implicit Conversions 隐式转换

    address可以显示转为合约实例,同时合约实例也可以显示转换为address

    MyContract contractInstance = MyContract(address mycontractAddress);
    address mycontractAddress = address(contractInstance);
    

    只能转换成比自己位数大的数据类型,比如:
    uint8 转 uint16
    uint160转address

    Explicit Conversions 显式转换

    int8 y = -3;
    uint x = uint(y);
    

    Type Deduction 未声明类型

    有些时候,定义变量时没有声明数据类型,使用默认的。比如:

    uint24 x = 0x123;
    var y = x;
    

    在这里,y使用var定义,它的类型将与x一致,是uint

    timestamp

    私有链区块时间戳案例:

    • blockNumber: 0 timestamp: 0,
    • blockNumber: 1 timestamp: 1526613702,
    • blockNumber: 4993 timestamp: 1529483513,
    • blockNumber: 4994 timestamp: 1529483514,
    • blockNumber: 4995 timestamp: 1529544543,
    • blockNumber: 4996 timestamp: 1529544712,
    • blockNumber: 4997 timestamp: 1529544713,

    block.timestamp (uint): 自 unix epoch 起始当前区块以秒计的时间戳

    timestamp 以s为单位

    • 1天 = 24h = 2460m = 2460*60s = 86400s
    • 1小时 = 3600s
    • 1分钟 = 60s

    相关文章

      网友评论

          本文标题:solidity-2.数据类型

          本文链接:https://www.haomeiwen.com/subject/cfsxqqtx.html