[英]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, 我想编写一个简单的函数来调用合同中的方法并获取运行结果,
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);
}
// 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.