2

for the given receive function , is there a way I can detect the receiving tokens address (Detect what coin my contract is receiving )

is it possible to extract this information from msg ?

receive() payable external {
    balance += msg.value;
    emit TransferReceived(msg.sender, msg.value);
}    

1 Answer 1

4

The receive function is triggered by a transfer of the native token only, such as ETH on Ethereum or BNB on Binance Smart Chain.

Therefore, msg.value is always an amount of the native token and never an amount of any ERC-20 token.

2
  • How can I achieve this behavior with an ERC20 token? I want to know when a smart contract I created receives a USDC transfer.
    – tlalco
    Nov 27, 2022 at 4:55
  • 1
    @tlalco You can't. An ERC20 transfer happens when an address calls the ERC20 contract. Your contract has no way of knowing that this ERC20 contract was called.
    – Undead8
    Nov 27, 2022 at 14:59

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.