代码之家  ›  专栏  ›  技术社区  ›  tim

在契约上运行方法时,如何获得运行结果?

  •  1
  • tim  · 技术社区  · 8 年前

    我想写一个简单的函数来调用contract上的方法并得到运行结果,

    这是合同代码

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

    前端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"
            })
    

    但事实上,我发现r return是一个**事务细节**,比如,

    {tx: "0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58",
    

    收据:{–logs:array(0)} 原木 以下内容: [] 收据 : {事务哈希:“0xa543fff3c3bac2268c0c94a21f6cf62faa8cf667defcd9fd8dcdbcf7669a4e58”, 事务索引:0,块哈希: “0x07D691308724C73025DE2F346DC0D6BC4EB7E7DE9871E29EA2C4D4E8FB8222BB”, 区块号:20,使用气体:56460,} 德克萨斯州 以下内容: “0xA543FFF3C3BAC2268C0C94A21F6CF62FAA8CF667DEFCD9FD8DCDBCF7669A4E58” 原型 以下内容: 对象

    没有应该返回的id信息。

    我做错什么了吗?

    2 回复  |  直到 8 年前
        1
  •  2
  •   tim    8 年前

    我想我得到了答案。

    当前无法从函数返回 修改区块链要接收返回值,可以标记 只读功能为“常量”。

    对于非常量函数,“返回”信息的唯一方法是 使用固化事件,在以太坊中合并为日志操作码 虚拟机。

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

        2
  •  0
  •   jkdev james murphy    8 年前

    在函数定义中指定“视图”:

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