1

I want to switch function according to "result" value.

Here is my solidity code.

function rockPaperScissors(uint _userHand) public affordPay() payable {
  uint randHand = random();
  uint result = rpsResult(_userHand, randHand);
  address toAccount = 0x*************************;
  if (result == 1) {
    // LOSE
    toAccount.transfer(msg.value / 2);
    msg.sender.transfer(msg.value / 2);
  } else if (result == 2) {
    // WIN
    toAccount.transfer(msg.value);
  } else {
    // DRAW
    msg.sender.transfer(msg.value);
  }
  return result;
}

In fact, I want to send half amount of msg.value in case "result" is 1, send full amount of msg.value in case "result" is 2, and send nothing in case other "result".

Additionaly, I want to send back remaining eth to msg.sender.

I use msg.sender.transfer(msg.value); when send back, but I read the docs, which says revert() will not consume any gas.

Is this means it is suitable to use revert() when you want to send back all eth you send to the contract ?

I'm a newbie in solidity develpment, so if there are any other good solutions, please teach me.

Thanks in advance!

1

1 Answer 1

1

revert() will send back the eth, yes, but it will also revert all changes done in the call. In your example case this looks like it would be fine since there are no state changes and I'm assuming the called functions don't modify state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.