再深入理解下web3.js中estimateGas如何計算智能合約消耗的gas量

再深入理解下web3.js中estimateGas如何計算智能合約消耗的gas量git

再深入理解下web3.js中estimateGas如何計算智能合約消耗的gas量github

咱們可以使用web3.js框架的estimateGas函數得到一個以太坊智能合約的Gas估計值 ,經過執行一個消息調用或交易,該消息調用或交易直接在節點的VM中執行,並未在區塊鏈中確認,函數會返回估算使用的gas量。web

函數調用:瀏覽器

web3.eth.estimateGas(callObject [, callback])微信

參數:網絡

在 web3.eth.sendTransaction 中, 參數大都是可選的。app

  1. Object - 要發送的交易對象:框架

    from: String - 用來傳送的帳戶地址. 默認使用web3.eth.defaultAccount屬性。
    to: String - (可選) 目標地址,對於建立合同的交易沒有定義。
    value: Number|String|BigNumber - (可選) 爲交易轉移的價值以Wei爲單位,若是是合同建立交易,也是基金。
    gas: Number|String|BigNumber - (可選, 默認: 待定) 用於交易的gas量(未使用的gas已退還)。
    gasPrice: Number|String|BigNumber - (可選, 默認: 待定) 此交易的gas價格以wei爲單位,默認爲平均網絡gas價格。
    data: String - (可選) Either 包含消息關聯數據的字節字符串,或者建立合同事務的初始化代碼。
    nonce: Number - (可選)一個隨機數的整數。 這容許覆蓋使用相同隨機數的您本身的未決事務。異步

2.Function - (optional)若是傳遞迴調,則HTTP請求將變爲異步。詳細說明在這裏 this note .
返回值:函數

數字:模擬呼叫/交易的須要使用的gas值。
一個簡單示例:
複製代碼

var result = web3.eth.estimateGas({
to: "0xc4abd0339eb8d57087278718986382264244252f",
data: "0xc6888fa10000000000000000000000000000000000000000000000000000000000000003"
});
console.log(result); // "0x0000000000000000000000000000000000000000000000000000000000000015"

複製代碼

用web3js庫中可能遇到estimateGas方法出錯的問題。大多數狀況下獲得的錯誤是這個:「所需的gas超過容許值或老是交易失敗」。

首先要檢查的下交易是否有效。例如,若是正在估計將必定數量的通證發送到另外一個地址的gasAmount,那麼最主要的檢查兩件事:

  1. 發送地址中是否有足夠的以太。
  2. 發送地址中是否有足夠的通證/代幣。
    這些彷佛是顯而易見要檢查的,可是仍是可能會犯這種低級錯誤,認爲方法估計Gas只是用來計算估計值,其實不是。若是參數設置的實際條件不對,它在運行這個方法時不會真正執行任何代碼就直接拋出錯誤。

評估發送通證的所需gas量的代碼片斷:

tokenContract.methods.transfer(recipientAddress,numtokens)
.estimateGas({from:tokenHolderAddress},function(gasAmount){
console.log(gasAmount);
});

官網在這裏https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethestimategas.

也能夠在你的瀏覽器地址欄輸入https://ethereum.github.io/browser-solidity,而後直接copy你的合約就能夠得到估計值。

這個代碼中默認的一個示例是提案投票的代碼以下:
複製代碼

pragma solidity ^0.4.0;
contract Ballot {

struct Voter {
    uint weight;
    bool voted;
    uint8 vote;
    address delegate;
}
struct Proposal {
    uint voteCount;
}

address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;

/// Create a new ballot with $(_numProposals) different proposals. //爲不一樣的提案建立一個新的投票合約
function Ballot(uint8 _numProposals) public {
    chairperson = msg.sender;
    voters[chairperson].weight = 1;
    proposals.length = _numProposals;
}

/// Give $(toVoter) the right to vote on this ballot.//授予投票權
/// May only be called by $(chairperson). //只能被主席調用
function giveRightToVote(address toVoter) public {
    if (msg.sender != chairperson || voters[toVoter].voted) return;
    voters[toVoter].weight = 1;
}

/// Delegate your vote to the voter $(to).//委託你的投票權
function delegate(address to) public {
    Voter storage sender = voters[msg.sender]; // assigns reference  指定參數
    if (sender.voted) return;
    while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
        to = voters[to].delegate;
    if (to == msg.sender) return;
    sender.voted = true;
    sender.delegate = to;
    Voter storage delegateTo = voters[to];
    if (delegateTo.voted)
        proposals[delegateTo.vote].voteCount += sender.weight;
    else
        delegateTo.weight += sender.weight;
}

/// Give a single vote to proposal $(toProposal). //對某個提案投一票
function vote(uint8 toProposal) public {
    Voter storage sender = voters[msg.sender];
    if (sender.voted || toProposal >= proposals.length) return;
    sender.voted = true;
    sender.vote = toProposal;
    proposals[toProposal].voteCount += sender.weight;
}

function winningProposal() public constant returns (uint8 _winningProposal) {
    uint256 winningVoteCount = 0;
    for (uint8 prop = 0; prop < proposals.length; prop++)
        if (proposals[prop].voteCount > winningVoteCount) {
            winningVoteCount = proposals[prop].voteCount;
            _winningProposal = prop;
        }
}

}

複製代碼

能夠run下試試。

安利一個適合區塊鏈新手的以太坊DApp開發教程:以太坊DApp開發實戰入門

若是想加入以太坊技術開發羣聊交流技術能夠加微信.

相關文章
相關標籤/搜索