web3.js手册

web3
web3.isConnected web3.setProvider web3.currentProvider web3.reset web3.sha3 web3.toHex web3.toAscii web3.fromAscii web3.toDecimal web3.fromDecimal web3.fromWei web3.toWei web3.toBigNumber web3.isAddress
web3.version
web3.version.api web3.version.node web3.version.network web3.version.ethereum web3.version.whisper
web3.net
web3.net.listening web3.net.peerCount
web3.shh
web3.shh.post web3.shh.newIdentity web3.shh.hasIdentity web3.shh.newGroup web3.shh.addGroup web3.shh.filter
web3.db
web3.db.putString web3.db.getString web3.db.putHex web3.db.getHex
web3.eth
web3.eth.defaultAccount web3.eth.defaultBlock web3.eth.syncing web3.eth.isSyncing web3.eth.coinbase web3.eth.hashrate web3.eth.gasPrice web3.eth.accounts web3.eth.mining web3.eth.blockNumber web3.eth.register web3.eth.unRegister web3.eth.getBalance web3.eth.getStorageAt web3.eth.getCode web3.eth.getBlock web3.eth.getBlockTransactionCount web3.eth.getUncle web3.eth.getBlockUncleCount web3.eth.getTransaction web3.eth.getTransactionFromBlock web3.eth.getTransactionReceipt web3.eth.getTransactionCount web3.eth.sendTransaction web3.eth.sendRawTransaction web3.eth.sign web3.eth.call web3.eth.estimateGas web3.eth.filter web3.eth.Contract web3.eth.namereg
iban
web3.eth.sendIBANTransaction web3.eth.iban web3.eth.iban.fromAddress web3.eth.iban.fromBban web3.eth.iban.createIndirect web3.eth.iban.isValid web3.eth.iban.isDirect web3.eth.iban.isIndirect web3.eth.iban.checksun web3.eth.iban.institution web3.eth.iban.client web3.eth.iban.address web3.eth.iban.toString
compiler
web3.eth.getCompilers web3.eth.compile.solidity web3.eth.compile.lll web3.eth.compile.serpent
在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

web3.eth.estimateGas

估计调用需要耗费的gas量。这个方法在节点的VM中执行一个消息调用或交易,但是不会修改区块链。

调用:

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

参数:

  • callObject: Object - 要发送的交易对象,可包含以下字段:
    • from: String - 指定的发送者的地址。如果不指定,使用web3.eth.defaultAccount。
    • to: String - (可选)交易消息的目标地址,如果是合约创建,则不填.
    • value: Number|String|BigNumber - (可选)交易携带的货币量,以wei为单位。如果合约创建交易,则为初始的基金。
    • gas: Number|String|BigNumber - (可选)默认是自动,交易可使用的gas,未使用的gas会退回。
    • gasPrice: Number|String|BigNumber - (可选)默认是自动确定,交易的gas价格,默认是网络gas价格的平均值 。
    • data: String - (可选)或者包含相关数据的字节字符串,如果是合约创建,则是初始化要用到的代码。
    • nonce: Number - (可选)整数,使用此值,可以允许你覆盖你自己的相同nonce的,正在pending中的交易11。
  • callback:Function - 回调函数,用于支持异步的执行方式

返回值:

  • Number - 模拟的call/transcation花费的gas。

示例:

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

教程推荐: