Events up&;down函数-坚固性代码

Events up&;down函数-坚固性代码,events,emit,Events,Emit,在下面的代码中,up n down函数出现错误,请检查并提出建议 问候 赛义德 pragma-solidity^0.7.6 合同拖船{ int public score = 0; int constant endAt = 1 ether; bool public gameOver = false; event ScoreChanged(uint amount, bool up); event GameEnded(bool up); // where 'up' indicates which s

在下面的代码中,up n down函数出现错误,请检查并提出建议

问候 赛义德

pragma-solidity^0.7.6

合同拖船{

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

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

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

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

checkIfGameOver()

emit ScoreChanged(msg.value,true);
}
函数checkIfGameOver()内部{

如果(score>=endAt | | score,当我在RemixIDE中测试它们时,向上和向下函数都起作用

您可以将消息添加到require语句中。然后您可以看到事务出错的地方

require(msg.value > 0, "No Value Sent");
如果您确实遇到此错误,则您没有发送任何值。在REMIX中,您将在单击对应于应付功能“向上”或“向下”的按钮之前,将其添加到值字段中

您还可以向gameOver require语句添加错误消息:

require(!gameOver, "Game Already Over");
你可能还想指出分数的变化方向。 因此,当调用“up”函数时,您将发出:

emit ScoreChanged(msg.value, true); // where the second argument indicates the direction
但当调用“down”函数时:

emit ScoreChanged(msg.value, false); // where the second argument indicates the direction
比赛结束时的情况也一样:

emit GameEnded(score >= 0); // where the argument indicates which side has won. 
最后,我建议您在选中“checkIfGameOver”之前发出事件“ScoreChanged”,以便事件的顺序正确

emit ScoreChanged(msg.value, false); // where the second argument indicates the direction
emit GameEnded(score >= 0); // where the argument indicates which side has won.