1

I'm learning how to do swaps with solidity and I have 2 questions:

  1. Should I verify the allowance of both wallets that will do the swap with the respective token? Also, should I verify the balance, or is it enough with the allowance and the safeTransfer?
  2. I'm calling safeTransfer twice and I want to understand how it works: if one is successful, and the other is not, does the transaction revert and the tokens return?
    function swap(uint amount) public payable {
        require(
            BUSD.allowance(msg.sender, address(this)) >= estateInfo.supply,
            "BUSD allowance too low or insufficient"
        );
        require(
            token.allowance(owner, address(this)) >= estateInfo.supply,
            "Token (NOTIFY TO DEV) allowance too low or insufficient"
        );
        require(
            BUSD.balanceOf(msg.sender) >= amount,
            "BUSD too low or insufficient"
        );
        require(
            token.balanceOf(owner) >= amount,
            "Token too low or insufficient"
        );
        

        _safeTransferFrom(BUSD, msg.sender, owner, amount);
        _safeTransferFrom(token, owner, msg.sender, amount);

        emit tokenSwap(true);

    }

1 Answer 1

1

Technically checking for the allowance and the balance in your case is not necessary. During the safeTransfer the tokens will check if allowance and balance are correct and:

  • if allowance and balance correct: safeTransfer will not revert
  • if allowance or balance incorrect: safeTransfer will revert

To the second question, only one of these safeTransfer reverting will revert the whole transaction (unless you're doing something weird on top). So you should be good.

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.