【问题标题】:How to get the value 3 on the first request without await?如何在不等待的情况下获得第一个请求的值 3?
【发布时间】:2021-12-15 03:15:31
【问题描述】:

在这种情况下,如果有机会使用await,则没有理由创建问题,但不建议在合同中使用await

注意:此功能旨在用于 debots,在通常的合同中使用它需要您自担风险。 https://github.com/tonlabs/TON-Solidity-Compiler/blob/master/API.md#synchronous-calls

Pong.sol

pragma ton-solidity ^0.51.0;

contract Pong {
    function get(uint b) external responsible returns (uint) {
        return b + 1;
    }
}

Ping.sol

pragma ton-solidity ^0.51.0;

interface IPong {
    function get(uint b) external responsible returns (uint);
}

contract Ping {
    uint public result;
    uint public tmp;

    function run(address pong, uint a) public view returns(uint) {
        update(pong, a);
        tvm.accept();
        return a + tmp;
    }

    function update(address pong, uint a) internal pure {
        IPong(pong).get{callback: Ping.onGet}(a);
    }

    function onGet(uint b) external {
        tvm.accept();
        tmp = b;
    }
}

运行.bash

#!/usr/bin/env bash

set -o errexit

tondev se reset

rm -fr *.abi.json *.tvc

# Deploy Pong Contract
tondev sol compile Pong.sol
tondev contract deploy Pong --value 1000000000
pongAddress=$(tondev contract info Pong | grep Address | cut -d':' -f3 | cut -d' ' -f1)
echo "$pongAddress"

# Deploy Ping Contract
tondev sol compile Ping.sol
tondev contract deploy Ping --value 1000000000
pingAddress=$(tondev contract info Ping | grep Address | cut -d':' -f3 | cut -d' ' -f1)
echo "$pingAddress"

# Run
tondev contract run Ping run --input "pong:$pongAddress,a:1" | grep value0
# value0:0x0000000000000000000000000000000000000000000000000000000000000001
tondev contract run Ping run --input "pong:$pongAddress,a:1" | grep value0
# value0:0x0000000000000000000000000000000000000000000000000000000000000003

问题的目的是了解如何在第一个请求中获得值 3(如果可能)。如果没有,那么更广泛地解释如何在异步区块链的条件下开发合约。

【问题讨论】:

    标签: solidity everscale


    【解决方案1】:

    您可以更简单地以异步方式执行此操作(无需回调和等待)。

    我更改了你的合约代码:

    pragma ton-solidity ^0.51.0;
    
    interface IPing {
        function onGet(uint b) external;
    }
    
    contract Pong {
        function get(uint b) external {       
            IPing(msg.sender).onGet(b + 1);        
        }
    }
    
    pragma ton-solidity ^0.51.0;
    
    interface IPong {
        function get(uint b) external;
    }
    
    contract Ping {
        uint public tmp;
    
        function run(address pong, uint a) public  { 
            IPong(pong).get(a);
            tvm.accept();
            tmp = a + tmp;        
        }
    
        function onGet(uint b) external {
            tvm.accept();
            tmp = b + tmp;
        }
    }
    

    现在调用稍作改动的this Run.bash

    #!/usr/bin/env bash
    
    set -o errexit
    
    tondev se reset
    tondev network default se
    rm -fr *.abi.json *.tvc
    
    # Deploy Pong Contract
    tondev sol compile Pong.sol
    tondev contract deploy Pong --value 1000000000
    pongAddress=$(tondev contract info Pong | grep Address | cut -d':' -f3 | cut -d' ' -f1)
    echo "$pongAddress"
    
    # Deploy Ping Contract
    tondev sol compile Ping.sol
    tondev contract deploy Ping --value 1000000000
    pingAddress=$(tondev contract info Ping | grep Address | cut -d':' -f3 | cut -d' ' -f1)
    echo "$pingAddress"
    
    # Run
    tondev contract run Ping run --input "pong:$pongAddress,a:1" &> /dev/null
    tondev contract run-local Ping tmp | grep tmp
    # tmp:0x0000000000000000000000000000000000000000000000000000000000000003
    
    tondev contract run Ping run --input "pong:$pongAddress,a:1" &> /dev/null
    tondev contract run-local Ping tmp | grep tmp
    # tmp:0x0000000000000000000000000000000000000000000000000000000000000006
    

    我的一点哲学,为什么它更好: 合同必须在没有等待的情况下生效。所以回调和等待,这是不好的。 回调在 debots 中非常有用,它可以在那里发生,因为 debots 在你的设备上运行。但这是另一个故事……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多