古人云熊市多讀書多學習。話不多說,黑瞎子今天給大家來分享一下ETH智能合約開發語言solidity中區塊交易信息API和ABI的相關知識,碼字不易,希望大家多多支持,期待下輪牛市ETH突破一萬美金🙌$ETH

一、區塊和交易常用的API有哪些?

    blockhash(uint blockNumber)           返回給定區塊號的哈希值,只支持最近的256個區塊

    block.coinbase returns(address)       當前塊礦工的地址

    block.difficulty returns(uint)             當前塊的難度

    block.gaslimit returns(uint)                當前塊的gaslimit

    block.number returns(uint)                當前塊的塊號

    block.timestamp returns(uint)          當前塊的時間戳

    gasleft    returns(uint)                       獲取剩餘gas

    msg.data    returns(uint256)              完整的調用數據(calldata)

    msg.sender    returns(address)          當前調用發起人的地址

    msg.sig    returns(bytes4)                   調用數據(calldata)的前四個字節,例如函數標識符

    msg.value returns(uint)                      這個消息所附帶的以太幣,單位爲wei

    tx.gasprice returns(uint)                    交易的gas價格

    tx.origin returns(address)                   交易的發送者

二、ABI編碼

    ABI全稱是Application Binary Interface 應用程序二進制接口,我們向合約地址發起一個交易(調用函數)其交易內容就是ABI編碼數據。下圖就是ABI

    ABI相關編碼函數

    abi.encode(...) returns(bytes)                              計算參數的ABI編碼

    abi.encodePacked(...) returns(bytes)                  計算參數的緊密打包編碼

    abi.encodeWithSelector(...) returns(bytes)         計算函數選擇器和參數的ABI編碼

    abi.encodeWithSignature(...) returns(bytes)       計算函數選擇器和參數的ABI編碼

三、相關代碼練習