提示:文章寫完後,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- 一、solidity常用的全局變量
- 二、modify修飾符的強大功能
- 三、元組 (不同類型的數組)
- 四、datalocation資料存儲位置
一、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);
解決:這個錯誤是因為調用方法傳遞參數是沒有給參數加上雙引号,或者雙引号不是英文的