使用Web3呼叫以太智能合約的朋友可能都知道,從Web3的介面呼叫以太的智能合約有兩種方式:eth_call和eth_sendTransaction,
eth_call不消耗gas,而eth_sendTransaction需要消耗gas,
本文從以太源代碼詳細解釋兩種呼叫方式的實作:
1)智能合約的介面
以以太坊的ERC20介面為例,介面有一個描述字:constant,

帶constant的函式,web3會自動使用eth_call遠程呼叫,
不帶constant的函式,web3使用eth_sendTransaction遠程呼叫,
比如查詢地址余額的函式balanceOf帶有constant描述字,web3會使用eth_call遠程呼叫,

2)eth_sendTransaction呼叫程序
eth_sendTransaction呼叫發起一筆交易,并等待打包,

3)eth_call呼叫程序
eth_call呼叫只是在本地呼叫evm虛擬機執行獲取狀態,并沒有打包操作,

總結:eth_call呼叫只是本地查詢呼叫,不會發送交易,記錄在鏈,eth_sendTransaction呼叫會發起交易請求,交易會打包記錄在鏈,Web3會根據智能合約中函式有無constant描述字自動區分兩種呼叫,
https://mp.weixin.qq.com/s/4ntwZlajGA-Z4j6LzB_LHA
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/295001.html
標籤:區塊鏈
