0

I have wrote the contract that deposit and withdrawl assets. I am going to use "call" instead of "send" or "transfer", but I don't know about internal process of "call". When I test the "withdrawl" function that uses "call", it sometimes failed. This is the code.

    function withdrawl(address payable _to) public payable {
        (bool sent, bytes memory data) = _to.call{value: msg.value}("");
        require(sent, "Failed to withdrawl");
    } 

I want to know about internal process of "call" on evm. When and why does "call" fail? If "call" returns false, does the transmitted ether come back?

1 Answer 1

0

Using addressX.call(functionSelector) is a call to the functionSelector of the contract deployed at addressX.

By using an empty function selector (ie call(“”)), you explicitly call the fallback function -> if a contract is located at address and no fallback is implemented, this call will fail (ie return false and roll back the state, including eth passed as msg.value). If there is no code, the call will go through (as there is no code to revert).

See https://docs.soliditylang.org/en/v0.8.17/units-and-global-variables.html#members-of-address-types

0

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.