solidity数据类型
Solidity 数据类型http://solidity.readthedocs.io/en/develop/types.html#value-types
solidity 是静态类型语言
solidity 提供一系列的元类型,可供组装成复杂类型的数据
值类型
bool
true
false
Integers
int/uint/uint8/uint256
int 默认是 int256
Fixed Point Numbers solidity中浮点类型,目前没有全面支持solidity,并且以后有可能declared
fixed/ufixed
fixedMxN 使用格式,M代表数字位数 8~256bits,N代表小数点后位数 0~80,这两个代表不同概念,fixed128x12。
Address
以太坊地址为20byte,地址类型是智能合约的基础
· balance
· transfer
address x = 0x123;
address myAddress = this;
if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10);
address x = 0x123;
If x is a contract address, its code (more specifically: its fallback function, if present) will be executed together with the transfer call (this is a feature of the EVM and cannot be prevented). If that execution runs out of gas or fails in any way, the Ether transfer will be reverted and the current contract will stop with an exception.
send 是transfer 的一个底层副本,如果执行失败,当前合约不会停止,而是返回一个 false
There are some dangers in using send: The transfer fails if the call stack depth is at 1024 (this can always be forced by the caller) and it also fails if the recipient runs out of gas. So in order to make safe Ether transfers, always check the return value of send, use transfer or even better: use a pattern where the recipient withdraws the money.
call,callcode,delegatecall。可以用来调用合约的方法,方法名.call() 只能调用 不修改状态变量的方法。方法名.send() 可以调用修改状态变量的方法
address nameReg = 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2;
nameReg.call("register", "MyName");
nameReg.call(bytes4(keccak256("fun(uint256)")), a);
namReg.call.gas(1000000)("register", "MyName");
nameReg.call.value(1 ether)("register", "MyName");
nameReg.call.gas(1000000).value(1 ether)("register", "MyName");
这些底层方法使用起来需要非常小心,调用其他合约的时候,反过来也可以使用到你的合约上。可能被恶意合约利用。
FIxed-sizebyte arrays 不可变数组
.length
Dynamically0-size byte array
http://solidity.readthedocs.io/en/develop/types.html#arrays
Enums
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
Function Types
function () {internal|external} [pure|constant|view|payable] [returns ()]
Data location
默认的方法参数(parameters),包括返回值存放在memory中
默认的本地变量存放在storage中
状态变量强制存放在storage中
calldata,是一种不固定(non-persistent),不可修饰的(non-modifiable)的区域,存放方法的形参(arguments),外部方法的参数(parameters, not return parameters)被强制放在 calldata中
Structs结构体
struct Campaign {
address beneficiary;
uint fundingGoal;
uint numFunders;
uint amount;
mapping (uint => Funder) funders;
}
delete
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.
}
}
网友评论