I'm wondering in which cases (if) the following code would fail:

function aOfContractA(uint256 amount):
   uint256 toSend = contractB.calculateAmount(msg.sender, amount); 
   require(toSend <= address(this).balance, "To low contract balance");

   bool success = payable(msg.sender).send(toSend); //here

   emit Success(msg.sender, toSend);

   return success;

Is it possible that in the line where the comment "//here" is the transaction/send of ETH fail? And if it would fail, why?


Yes, this can fail. This line of code will send ETH to msg.sender. Msg.sender could be anyone, including another smart contract. When a smart contract directly receives any amount of ETH, the "receive()" fallback function when gets called and its code, if any, would be executed. This fallback function may fail and cause entire transaction to revert.

  • Thank you. How would you check that? require(success,"Fail"); or is there any other way to check that before calling the function (that would also need a differentation between wallets and contracts right)?
    – MaTok
    Sep 20 '21 at 6:40
  • Well, you can't know what code would get executed on the receiver address. You can use the require(address.send(), "failed") approach and let transaction revert or alternatively, use try {} catch {} and handle any error in sending.
    – Bigbrotha
    Sep 21 '21 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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