-1

If any token other than ETHER is sent to the contract, I want to know the address of the sent token with receive() or fallback() (whichever works). For example, how can I find out that WBNB, BUSD and similar tokens were sent to the contract other than BNB?

hertac
  • 9
  • 2

2 Answers2

1

You should keep in mind that most tokens other than ether are just smart contracts that keep track of who owns what. So if somebody sends you tokens, all he's doing is sending a tx to that contract and incrementing the number of tokens you own in its record.

  1. Either you go into that contract and check what the balance of a specific address is.

  2. It's possible to create a script that runs like a daemon process and listens to events that a token's smart contract emits when someone sends token to a particular address.

Just like if you said in real life I'm going to be issuing Hertac coins and the records of who owns what will be kept in this register. You have to check that register to see how many hertac tokens somebody has. Tokens on the blockchain are just distributed versions of them.

Ravi Verma
  • 74
  • 2
  • I didn't really ask that. Or I don't fully understand. Actually what I want is: When ETHER is sent to a contract, I can detect it with the receive() function. But I can't set up a function to react when tokens of any different tokens (WBNB, WETH, BUST, USDT and other) arrive. – hertac Sep 07 '22 at 18:34
  • 1
    I am trying to explain that you cannot create a method that will be called when you receive ERC20 tokens, as the updating of token balance occurs on the contract of the token. It is also important to note that your contract is not aware of how the state of a particular token's contract is changing at any given time. When a contract receives ether, the recieve method is called. This is an inherent feature of EVM, not something you would get with another token. – Ravi Verma Sep 11 '22 at 07:56
0

Onchain

The ERC20 standard doesn't require the token contract to notify the recipient, so most implementations simply don't notify. And those contracts that do notify, don't use any standardized approach.

There is also the ERC777 standard for fungible tokens that notifies the recipient but it's far less used than the original ERC20.

Offchain

You can read the Transfer event logs that were produced with your address as the recipient param. Code example in this answer. Note that most 3rd party node providers give only limited access to historic blocks (usually the last 200 blocks), so you might have to use their paid plans or run your own node to retrieve the event logs from older blocks.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100