简体   繁体   English

Web3.js:从合同方法执行中获取返回值

[英]Web3.js: Get return value from contract method execution

I have ERC-20 token smart-contract which methods I call using sendSignedTransaction from web3.js. 我有ERC-20我呼叫使用哪些方法令牌智能合同sendSignedTransaction从web3.js. After I know transaction is succesfully mined I need to check contract method execution result. 我知道交易已成功开采后,我需要检查合同方法的执行结果。 How do I do it if all I have is transaction hash? 如果我只有交易哈希怎么办?

Example: method transferFrom(from, to, tokens) returns true or false depending on whether transferring was successful. 示例:方法transferFrom(from, to, tokens)根据传输是否成功返回truefalse So if I try to transfer 100 tokens from empty wallet, contract method will return false . 因此,如果我尝试从空的钱包中转移100个令牌,合同方法将返回false

Upd: Okay, as I understood there is no way of determining method outcome using txHash after transaction is mined and confirmed. Upd:好的,据我所知,在挖掘和确认交易之后,无法使用txHash确定方法结果。 Then which ways exists to handle this case? 那么,存在哪些方法可以处理这种情况? How can I make sure that tokens were transferred? 如何确保令牌已转移?

您可以在合同代码内部发出一个事件(实际上在ERC20标准中始终存在一个转移事件),然后在web3js内部,使用以下代码行读取所有事件,直到最新的代码块为止:

Events = Contract.eventName({}, {fromBlock: 0, toBlock: 'latest'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM