我们可使用 web3.js 框架的 estimateGas 函数获得一个以太坊智能合约的 Gas 估计值 , 通过执行一个消息调用或交易, 该消息调用或交易直接在节点的 VM 中执行, 并未在区块链中确认, 函数会返回估算使用的 gas 量.
函数调用:
web3.eth.estimateGas(callObject [, callback])
参数:
在 web3.eth.sendTransaction 中, 参数大都是可选的.
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 https://github.com/ethereum/wiki/wiki/JavaScript-API#using-callbacks .
https://github.com/ethereum/wiki/wiki/JavaScript-API#returns-47 返回值:
数字: 模拟呼叫 / 交易的需要使用的 gas 值.
https://github.com/ethereum/wiki/wiki/JavaScript-API#example-49 一个简单示例:
- 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 下试试.
来源: https://www.cnblogs.com/helloworld2018/p/8907289.html