Is there an ERC20 function to ensure payment if specific token?

Viewed 627

is there a ERC/IERC20 function or util that ensures received payment is denominated in a specific token?

receive() external payable virtual {
    
    IERC20 token = IERC20(address(0x123...789));
    
    require(
    paymentToken == MYtoken,
    "Payment Must be MY token"
    );
    emit PaymentReceived(_msgSender(), msg.value);
1 Answers

The receive() function is for your contract to accept ETH. Or generally - the native token of the network where the contract is deployed (BNB on BSC network, TRX on Tron network, ...).

But if your contract address receives a standard ERC-20 token, it doesn't get notified in any way.

Except for cases, where the token contract specifically calls the receiver to let them know about the transfer (as defined for example in the EIP-777 tokensReceived() hook). But this hook needs to be defined in the token contract in the first place, so that you can make use of it in your receiver contract. If it's not in the token contract, then your contract can't get directly notified about received tokens.

Related