提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、solidity常用的全局变量
block.blockhash(uint blockNumber) returns (bytes32): 给定区块的哈希值 - 仅适用于最新的256个区块,不包括当前区块
block.coinbase (address):当前区块的矿工的地址
block.difficulty (uint):当前区块的难度系数
block.gaslimit (uint):当前区块gas限制
block.number (uint):当前区块编号
block.timestamp (uint):当前块的时间戳
msg.data (bytes):完整的calldata
msg.gas (uint):剩余的gas
msg.sender (address):消息的发送方(当前调用)
msg.sig (bytes4):calldata的前四个字节(即函数标识符)
msg.value (uint):所发送的消息中wei的数量
now (uint):当前块时间戳(block.timestamp的别名)
tx.gasprice (uint):交易的gas价格
tx.origin (address):交易发送方(完整的调用链)
二、modify修饰符的强大功能
代码重用性(一个modifier方法可以被多处调用),多重modifier的执行顺序,很重要!!!
三、元组 (不同类型的数组)
pragma solidity ^0.4.0;
contract tuple{
uint[] data;
function f() public pure returns(uint, bool, uint){
// 返回一个元组
return (7,true,2);
}
function g() public view{
// 声明并赋值一些变量,但这里没法直接定义变量的类型
var (x, b, y) = f();
// 给变量赋值
(x, y) = (2, 7);
// 交换变量值
(x, y) = (y, x);
// 元素可以省略,在声明和赋值时都可以使用
(data.length,) = f(); //set the length to 7
(, data[3]) = f(); // set data[3] to 2
// (1,) 是唯一定义 1-component 元组的方法,因为(1)是等价于1
(x,) = (1,);
}
}
四、datalocation数据存储位置
不同数据类型的变量会有各自默认的存储位置,
-状态变量总是会存储在storage 中
-函数参数和返回值默认存放在 内存memory 中
-结构、数组或映射类型的局部变量,默认会放在 存储storage 中
-除结构、数组及映射类型之外的简单局部值变量,会储存在栈中
这里需要给返回值加上存储位置,修改如下:
转账时调用transfer2方法,如果日志提示转账失败信息:VM error: revert. revert The transaction has been reverted to the initial state. Note: The constructor should be payable if you send value
解决:添加一个方法即可外部可调用的payable方法即可
底层send()的使用:
调动递归深度不能超过1024。
如果gas不够,执行会失败。
send()方法要检查成功与否。
transfer()相对send()更安全。
Transact to mappingtest.regiter errored:Error encoding arguments:SyntaxError:Unexpected token s in json at position address(this).send(10 ether);
解决:这个错误是因为调用方法传递参数是没有给参数加上双引号,或者双引号不是英文的