【问题标题】:Solidity event n emit syntexSolidity 事件 n 发出语法
【发布时间】:2021-07-05 12:50:45
【问题描述】:

希望一切顺利

我想在下面的solidity代码中做以下活动,但不知道怎么做,请指教。 感谢问候 赛义德

查询:_

使用两个参数发出事件 ScoreChanged:int 数量(等于发送的值)和 bool 方向(向上函数为 true,向下函数为 false)

gameOver 切换为 true 后发出事件 GameEnded

代码:

pragma solidity ^0.4.17;

contract TugOfWar {

    int public score = 0;
    int constant endAt = 1 ether;
    bool public gameOver = false;

    function up() external payable {
        require(msg.value > 0);
        require(!gameOver);

        int value = int(msg.value);
        score += value;

        checkIfGameOver();
    }

        function down() external payable {
        require(msg.value > 0);
        require(!gameOver);

        int value = int(msg.value);
        score -= value;

        checkIfGameOver();
    }
        function checkIfGameOver() internal {
        if(score >= endAt || score <= endAt * -1) {
            gameOver = true;

        }
    }
}

【问题讨论】:

    标签: events solidity emit


    【解决方案1】:

    您可以通过以下方式在合约中定义事件:

    event ScoreChanged(uint amount, bool up);
    event GameEnded(bool up); // where 'up' indicates which side won. 
    

    发出您编写的事件:

    emit ScoreChanged(msg.value, true); // where the second argument indicates the direction
    

    您可以在增加或减少分数后立即执行此操作。

    当游戏结束时:

    emit GameEnded(true); // where the argument indicates which side has won. 
    

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 2016-06-03
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 2019-12-15
      • 1970-01-01
      相关资源
      最近更新 更多