3

I saw there are two type of function available, but I don't understand the difference. If another contract send to my contract for example $100 BUSD, how do I know when I received them? I saw there are these two function:

fallback() external payable { }

receive() external payable { }

I have to do some logic when another contract send me a token, and I also need to know the address of the token that I received. Which of the two functions should I use?

Also, I have another question. If I want to send BUSD/BTC/BNB etc from my contract to a wallet address, what's the difference between:

payable(_to).transfer(_amount);

and

IBEP20(tokenAddress).transfer(to, amount);

I'm on the binance smart chain, but I don't think there is any difference.

Thank you!

3 Answers 3

5

(That's a very long answer to a rather simple question, TLDR: BEP20.transfer() is an external call to a contract, payable.send() isn't)

The difference between fallback and receive is well explained in the article linked in the other answer. About the difference between payable(_to).transfer(_amount); and IBEP20(tokenAddress).transfer(to, amount); well while they might look similar (and are pretty much exactly the same for the end user), they're radically different from a dev standpoint. Let's break them down.

First, IBEP20(tokenAddress).transfer(to, amount);. Here we're making an external call to the contract located at tokenAddress using the transfer function from the IBEP20. What happens there is that we're sending a message to an external contract. If that contract happens to have a function that's called transfer and takes an address and an uint256 (in that order) as parameters, it gets executed and then the execution jumps back to your contract; if it doesn't exist, then the contract's fallback() function gets executed instead, and if it doesn't exist AND there's no fallback function or if you're calling to a non-contract address, the transaction reverts. Essentially, we're treating the external contract as a blackbox, for all we know it could contain completely arbitrary logic, do nothing, or even contain malicious code (look up reentrancy attacks).

payable(_to).transfer(_amount): here we're using a solidity keyword, transfer, which is used to send _amount of ether (I'm referring to the native chain's token as "ether". That's what you'll find in most documentation as well, but it can be anything you want, BNB on the BSC, MATIC on polygon...) to a payable address (making an address payable in your contract just means that you're acknowledging that this address can receive ether, which isn't the case of all addresses. Contracts that don't implement a fallback or receive function simply can't receive ether. If you end up sending ether to one of those, the transaction will revert). We actually know what we're doing, we're not relying on an external contract but on the EVM directly. Now for most application, they essentially work the same, but in some cases (typically when you have the user send a BEP 20 token of its choice, like in a DEX for example), they have to be treated differently.

1
  • Thank you! This is exactly what I wanted to know!
    – Nicola
    Oct 26, 2021 at 13:47
1

receive function is only for the native token the blockchain, eth for ethereum, bnb for bsc. the difference between these 2 functions is well explained there : https://docs.soliditylang.org/en/v0.8.9/contracts.html?highlight=receive#receive-ether-function.

payable is also about the ability or not of an adress to receive the native token of the blockchain.

2
  • Thanks for your answer? So in my contract(on the Binance smart chain) the receive function is called only when I receive BNB or even if I received, for example, BUSD, BTCB, CAKE etc?
    – Nicola
    Oct 24, 2021 at 17:21
  • i've literraly answered it about it clearly just above. Oct 24, 2021 at 17:53
0

Fallback functions can be used instead of receive functions. However, to clearly distinguish between eth transferring and not, receive functions are being used now.

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.