简体   繁体   中英

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

// 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,

 {tx: "0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58", 

receipt: {…}, logs: Array(0)} logs : [] receipt : {transactionHash: "0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58", transactionIndex: 0, blockHash: "0x07d691308724c73025de2f346dc0d6bc4eb7e7de9871e29ea2c4d4e8fb8222bb", blockNumber: 20, gasUsed: 56460, …} tx : "0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58" proto : Object

There is no information about the id which should be returned included.

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.

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";
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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