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

问题描述 投票:1回答:1

也许这是一个愚蠢的问题。但我想知道是否有可能在以太坊的单一交易中放入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信息):还原。

那么我做错了什么?或者也许在一次交易中只能进行一次合约函数调用?

node.js ethereum web3js
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.