简体   繁体   English

在合同上运行方法时如何获得运行结果?

[英]How can I get the running result when I run method on contract?

I want to write a simple function to call the method on contract and get the running result, 我想编写一个简单的函数来调用合同中的方法并获取运行结果,

This is the contract code 这是合同代码

function _evaluate(uint8[5] _uploads) internal returns (bytes32 resultId){

    resultId= keccak256(abi.encodePacked(now,  msg.sender));

    addressToid[msg.sender] = resultId;
    idToResult[resultId] = Result(msg.sender, r);
  }

function upload(uint8[5] _inputs) public returns ( bytes32 resultId) {

    return _evaluate(_inputs);
  }

front end js codes 前端js代码

// DEE is the contract name
return this.DEE.deployed()
        .then((instance) => instance.upload(this.inputs,  {from: base.accounts[0]}))
        .then((r) => {
          this.message = "Transaction done"

          console.log(r);



        })
        .catch((e) => {
          console.error(e)
          this.message = "Transaction failed"
        })

but in fact, I found r returned is a ** transaction detail**, like, 但事实上,我发现r返回的是**交易明细**,例如,

 {tx: "0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58", 

receipt: {…}, logs: Array(0)} logs : [] receipt : {transactionHash: "0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58", transactionIndex: 0, blockHash: "0x07d691308724c73025de2f346dc0d6bc4eb7e7de9871e29ea2c4d4e8fb8222bb", blockNumber: 20, gasUsed: 56460, …} tx : "0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58" proto : Object 收据:{...},原木:阵列(0)}的日志:[]收据:{transactionHash: “0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58”,transactionIndex:0,blockHash: “0x07d691308724c73025de2f346dc0d6bc4eb7e7de9871e29ea2c4d4e8fb8222bb”,blockNumber:20,gasUsed:56460,...} TX:“0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58 原型 :对象

There is no information about the id which should be returned included. 没有有关应返回的ID的信息。

Did I do something wrong? 我做错什么了吗?

I think I got the answer. 我想我得到了答案。

It is not currently possible to return values from functions which modify the blockchain. 当前无法从修改区块链的函数返回值。 To receive a return value, you can mark read-only functions as "constant". 要接收返回值,可以将只读函数标记为“常量”。

For non-constant functions, the only way to "return" information is by using Solidity Events, which coalesce as LOG opcodes in the Ethereum Virtual Machine. 对于非恒定函数,“返回”信息的唯一方法是使用Solidity Events,该事件在以太坊虚拟机中合并为LOG操作码。

https://ethereum.stackexchange.com/questions/3285/how-to-get-return-values-when-function-with-argument-is-called https://ethereum.stackexchange.com/questions/3285/how-to-get-return-values-when-function-with-argument-is-called

Specify "view" in the function definition: 在函数定义中指定“视图”:

function upload(uint8[5] _inputs) public view returns ( bytes32 resultId) {
    // do something here
    // save ad calculate
    return "123";
 }

暂无
暂无

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

相关问题 在 Solidity 中调用 contract.transfer 方法时如何调试运行时错误? - How can I debug the runtime error when call contract.transfer method in Solidity? 如何在以太坊上运行投票代码并获得结果? - How can I run the voting code on Ethereum and get the result? 我正在尝试使用 Metamask 将彩票合约连接到 React 应用程序,但是当我在合约上调用 manager() 方法时,我得到一个空数组 - I am trying to connect Lottery Contract to React Application using Metamask, But when I call manager() method on the contract I get an empty array 在浏览器或chrome扩展程序中确认交易之前如何获取web3合约地址? - How can I get the web3 contract address before confirming transaction in the browser or chrome extension? 在我的智能合约中创建函数时,我如何知道要指定多少数量进行测试? - When creating a function in my smart contract, how can I know what amount to specify for testing? 如何自动执行智能合约代码? - How can I automate Smart Contract code execution? 我如何在智能合约中从 pancakeswap 获取 BNB 价格? - how i can fetch BNB price from pancakeswap in smart contract? 如何将智能合约与Java应用程序集成? - How can i integrate smart contract with java application? 如何从不同节点访问合约功能? - How can i access the function of contract from different nodes? 我如何在区块链网络上部署我的合约? - How can i deploy my contract on blockchain network?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM