Ether Units
- 一个整数的后面可以跟一个单位,ether,finney,szabo或者wei。
1 ether = 1000 finney
1 ether = 1000000 szabo
1 ether = 10 ** 18 wei
Time Units
- 时间的单位有seconds, minutes, hours, days, weeks 和 years。换算如下:
1 == 1 seconds
1 minutes == 60 seconds
1 hours == 60 minutes
1 days == 24 hours
1 weeks == 7 days
1 years == 365 days
特殊的变量函数和函数
- 区块和交易属性
block.blockhash(uint blockNumber) returns (bytes32): 某个区块的区块链hash值
block.coinbase (address): 当前区块的挖矿地址
block.difficulty (uint): 当前区块的难度
block.gaslimit (uint): 当前区块的gaslimit
block.number (uint): 当前区块编号
block.timestamp (uint): 当前区块时间戳
msg.data (bytes): 参数
msg.gas (uint): 剩余的gas
msg.sender (address): 当前发送消息的地址
msg.sig (bytes4): 方法ID
msg.value (uint): 伴随消息附带的以太币数量
now (uint): 时间戳,等价于block.timestamp (uint)
tx.gasprice (uint): 交易的gas单价
tx.origin (address):交易发送地址
- 错误处理
1.assert(bool condition):不满足条件,将抛出异常
2.require(bool condition):不满足条件,将抛出异常
3.revert() 抛出异常
在Solidity 0.4.10版本之前,使用throw来处理异常。如下所示:
在Solidity 0.4.10版本之后,我们通常如下使用:
if(msg.sender != owner) { revert(); }
assert(msg.sender == owner);
require(msg.sender == owner);
元组
- 能解决返回一组数据,且这组数据类型不同(数组无法解决)
-
不能直接返回一组mapping
正确方式如下,但是注意需要加个public(这是个坑)
pragma solidity ^0.4.0;
contract C {
mapping(uint => string) public students;
function C(){
students[0] = "melody";
students[1] = "luoxue";
}
function studentsNames() constant returns(string name0,string name1){
name0 = students[0];
name1 = students[1];
}
function studentsNames1() constant returns(string name0,string name1){
name0 = students[0];
name1 = students[1];
return(name0,name1);// 元组
}
// 不同类型的
function f() constant returns(uint,bool,string){
return(101,true,"melody");
}
}
- studentNames和studentNames1是一样的
元组的使用 - 使用1
pragma solidity ^0.4.0;
contract C {
function useage(uint _a,uint _b)constant returns(uint,uint){
return(_a+_b,_a*_b);
}
}
- 使用2
不能在元组里对某个数组进行直接赋值,需要先开辟空间,如下g5
pragma solidity ^0.4.0;
contract C {
uint[] data;
uint[] xx;
bool isOpen;
function f()constant returns(uint,bool,uint){
return(7,true,2);
}
function g1() constant returns(uint,bool,uint){
var (x,b,y) = f();
return (x,b,y);
}
function g2() constant returns(uint,uint){
var (x,b,y) = f();
(x,y) =(2,7);
return(x,y);
}
function g3() constant returns(uint,uint){
var (x,b,y) = f();
(x,y) =(2,7);
(x,y) = (y,x);
return(x,y);
}
function g4() constant returns(uint[]){
(data.length,) = f();
return data;
}
function g5() constant returns(uint[]){
(xx[2],isOpen,data[3]) = f();//7,true,2
return data;
}
}
正确姿势如下:
pragma solidity ^0.4.0;
contract C {
uint[] data = new uint[](5);
uint xx;
bool isOpen;
uint m;
function f()constant returns(uint,bool,uint){
return(7,true,2);
}
function g1() constant returns(uint,bool,uint){
var (x,b,y) = f();
return (x,b,y);
}
function g2() constant returns(uint,uint){
var (x,b,y) = f();
(x,y) =(2,7);
return(x,y);
}
function g3() constant returns(uint,uint){
var (x,b,y) = f();
(x,y) =(2,7);
(x,y) = (y,x);
return(x,y);
}
function g4() constant returns(uint[]){
(data.length,) = f();
return data;
}
function g5() constant returns(uint[]){
(,data[3])=f();
return data;
}
function g6()constant returns(uint){
var(x,)=(1,);
return x;
}
function setDaatata() payable{
data[3]=5;
}
}