javascript - 为什么我们不能从运行在智能合约上的 sendTransaction() 获得返回值?

标签 javascript ethereum solidity smartcontracts web3js

所有关于此的讨论都提到不可能从 sendTransaction() 获得返回值,该函数运行在合约状态正在更改的合约函数上。我不明白为什么返回的值不能记录在区 block 链上的事务日志中,类似于事件,因此可以在事务确认时检索它:

web3.eth.sendTransaction(...)
.on('confirmation', function(1, receipt){ ... // retrieving value returned by smart contract function here })

最佳答案

日志用于描述从合约发出的事件——这是从交易中获取数据的当前解决方案——,因此返回数据不能进入那里。
然而,在收据中包含一个 return_data 已经被讨论过并且显然被遗忘了。 EIP758 , 有 following sollution :

EIP 658 originally proposed adding return data to transaction receipts. However, return data is not charged for (as it is not stored on the blockchain), so adding it to transaction receipts could result in DoS and spam opportunities. Instead, a simple Boolean status field was added to transaction receipts. This modified version of EIP 658 was included in the Byzantium hard fork. While the status field is useful, applications often need the return data as well.


The primary advantage of using the strategy outlined here is efficiency: no extra data needs to be stored on the blockchain, and minimal extra computational load is imposed on nodes. Since light clients have the current state, they can compute and send return data notifications without contacting a server. Although after-the-fact lookups of the return value would not be supported, this is consistent with the conventional use of return data, which are only accessible to the caller when the function returns, and are not stored for later use.


还有这个go client pull request ,它没有通过,因为最好的解决方案是以太坊硬 fork ——尽管从那时起我们就有了一些,但它没有发生。

关于javascript - 为什么我们不能从运行在智能合约上的 sendTransaction() 获得返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72429607/

相关文章:

javascript - 如何制作与 JS 库无关的选择器包装器?

ethereum - 使用 ethers.js 过滤事件后,如何使主题易于阅读

ethereum - "Web3ProviderEngine does not support synchronous requests"

ethereum - Solidity 中的重入 hack 不再适用于 pragma ^0.8.0

javascript - 滚动时如何使 <div> 垂直相互删除?

javascript - 使用 javaScript 而非 jquery 的多个切换

javascript - Javascript 中的命名空间问题

javascript - 如何在javascript中使用以太坊合约的状态改变方法?

javascript - 如何使用松露测试代码来读取公共(public)变量?

javascript - 无法通过 JavaScript 更新 solidity 合约的状态变量