[英]Sign and broadcast smart contract transaction in TRON network
Using a node.js client, I'm trying to invoke a smart contract function by:使用 node.js 客户端,我试图通过以下方式调用智能合约 function:
I deployed a simple smart contract:我部署了一个简单的智能合约:
pragma solidity ^0.4.25;
contract Test {
event MyEvent(address sender, string message, uint sessionId, uint value);
event TestEvent(address sender);
constructor() public {
}
function testFunction(string message, uint sessionId)
public
payable {
emit MyEvent(msg.sender, message, sessionId, msg.value);
}
function test2()
public {
emit TestEvent(msg.sender);
}
}
I'm having success invoking test2
(when sending an empty parameters array) but failing to invoke testFunction
which takes parameters.我成功调用了
test2
(发送空参数数组时),但未能调用带有参数的testFunction
。 I've tried using tronweb triggerSmartContract call :我试过使用tronweb triggerSmartContract 调用:
async function triggerSmartContract() {
try {
const options = {
feeLimit: 1000000000,
callValue: 50
};
const parameters = [{type: 'string', value: 'test-round-id-1'}, {type: 'uint', value: 12345}];
const issuerAddress = tronWeb.defaultAddress.base58;
const functionSelector = 'testFunction(string, uint)';
let transactionObject = await tronWeb.transactionBuilder.triggerSmartContract (
contract_address,
functionSelector,
options,
parameters,
tronWeb.address.toHex(issuerAddress)
);
if (!transactionObject.result || !transactionObject.result.result)
return console.error('Unknown error: ' + txJson, null, 2);
// Signing the transaction
const signedTransaction = await tronWeb.trx.sign(transactionObject.transaction);
if (!signedTransaction.signature) {
return console.error('Transaction was not signed properly');
}
// Broadcasting the transaction
const broadcast = await tronWeb.trx.sendRawTransaction(signedTransaction);
console.log(`broadcast: ${broadcast}`);
} catch (e) {
return console.error(e);
}
}
The code runs and creates a transaction on the blockchain but with result of FAIL-REVERT OPCODE EXECUTED
.代码在区块链上运行并创建交易,但结果为
FAIL-REVERT OPCODE EXECUTED
。 The wallet of the address and key that are defined in tronweb object has enough funds in it (it's not out of gas/not enough funds matter).在 tronweb object 中定义的地址和密钥的钱包中有足够的资金(它不是用完gas/没有足够的资金问题)。 Also the function in the smart contract is defined as payable.
此外,智能合约中的 function 被定义为应付账款。
Since I can invoke successfully any function with no parameters I tend to think the problem relates to the way I construct the parameters
array.由于我可以在没有参数的情况下成功调用任何 function,我倾向于认为问题与我构造
parameters
数组的方式有关。 I built the parameters
array this way (pairs of type
and value
) after going over the source code of the transactionBuilder (line 833) .在查看了 transactionBuilder 的源代码(第 833 行)之后,我以这种方式构建了
parameters
数组( type
和value
对)。
Any suggestions?有什么建议么?
Had exactly the same issue.有完全相同的问题。 Solution:
解决方案:
const functionSelector = 'testFunction(string,uint)';
const functionSelector = 'testFunction(string,uint)';
as malysh adviseduint
to uint256
uint
更改为uint256
try writing function parameters without spaces.尝试编写不带空格的 function 参数。
const functionSelector = 'testFunction(string,uint)';
I read in the documentation that you have to write without spaces, it worked for me on python.我在文档中读到你必须写没有空格,它在 python 上对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.