在 send() 调用内的“options”对象中不包括“gas”或“gasPrice”属性

问题描述

这是一个 web3 问题:有谁知道当您不指定 send()gas 时,智能合约方法gasPrice 函数调用认是什么?它会自动分配足够的gas并计算当前的平均gasPrice吗?这些属性总是可选的还是在某些情况下必须包含其中之一?

解决方法

documentation 来看,gasgas 似乎总是可选的。

不幸的是,文档没有说明未提供时默认值是什么,但是在代码上有一个快速峰值(希望这是正确的代码路径)它似乎在内部调用 getGasPrice 来获取 gas价格,然后默认 gasPrice 为该价格。


    // Send the actual transaction
    if (isSendTx && _.isObject(payload.params[0]) && typeof payload.params[0].gasPrice === 'undefined') {

        var getGasPrice = (new Method({
            name: 'getGasPrice',call: 'eth_gasPrice',params: 0
        })).createFunction(method.requestManager);

        getGasPrice(function (err,gasPrice) {

            if (gasPrice) {
                payload.params[0].gasPrice = gasPrice;
            }

            if (isSendTx) {
                setTimeout(() => {
                    defer.eventEmitter.emit('sending',payload);
                },0);
            }

            sendRequest(payload,method);
        });

GitHub Source