1

I'd like to try to refund a call. However:

  1. If my contract doesn't have enough native tokens
  2. The sender is not payable, I don't want to fail.

I've tried it in the following way:

function performLoop() nonReentrant external {
    (bool canExecute, ) = requiresExecution(false);
    require(canExecute, "PEL: Nothing to Execute");
    uint gasAtStart = gasleft();


    uint256 gasSpent = gasAtStart - gasleft() + 28925;
    try payable(msg.sender).transfer((gasSpent * tx.gasprice) + (gasSpent * (tx.gasprice/10))) { } catch { }
}

However I cannot use a try catch here since:

Try can only be used with external function calls and contract creation calls.

How can I try to send fund if they're available

2 Answers 2

1

You can use call instead of transfer. call doesn't automatically revert on failure, but returns a boolean with the status of the call. So can just you ignore this value.

(bool success, ) = payable(msg.sender).call{value: gasSpent*tx.gasprice + gasSpent*(tx.gasprice/10)}();
// require(success);  // keep this commented to never revert on failure
1

You can not use try catch to send funds. try catch can only be used for external contract call.

Instead you can use if statement

if(address(this).balance > 0){
   (bool success, ) = msg.sender.call{value: address(this).balance}("");
   require(success, "not able to refund");
}

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.