0

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!

0

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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