Web3.js以太坊 - 是否可以在一次交易中调用2个函数?

时间:2018-03-02 05:16:06

标签: node.js ethereum web3js

也许这是一个愚蠢的问题。但我想知道是否有可能在以太坊的单一交易中放入2个(或更多)智能合约方法调用。

我在单个事务中调用一个函数的代码部分:

var data = self.MyContract.MyFunc1.getData(
        param1);
const options = {
    gasPrice: gasPrice,
    gasLimit: gasLimit,
    nonce,
    data,
    to: addressContract,
};
const tx = new Tx(options);
tx.sign(new Buffer(private_key, 'hex'));
const rawTx = `0x${tx.serialize().toString('hex')}`;
self.web3.eth.sendRawTransaction(rawTx, (err, result) => {
    if (err)
        reject(err);
    resolve(result);
});

完美无缺。我尝试用这样的方式向数据变量添加另一个函数调用:

var data = self.MyContract.MyFunc1.getData(
        param1);
var data2 = self.MyContract.MyFunc2.getData(
        param2);
data += data2;
const options = {
    gasPrice: gasPrice,
    gasLimit: gasLimit,
    nonce,
    data,
    to: addressContract,
};
const tx = new Tx(options);
tx.sign(new Buffer(private_key, 'hex'));
const rawTx = `0x${tx.serialize().toString('hex')}`;
self.web3.eth.sendRawTransaction(rawTx, (err, result) => {
    if (err)
        reject(err);
    resolve(result);
});

这次交易失败的原因(根据etherscan.io信息):还原。

那么我做错了什么?或者在一次交易中可能只有一个合约函数调用?

1 个答案:

答案 0 :(得分:0)

不,您无法在单个事务中从以太坊客户端调用多个非常量函数。但是,您可以启动交易以收缩A,然后从同一交易中的第一个合约中调用合约B中的函数。

相关问题