Search code examples
blockchainethereumsoliditybitcoinbep20

How can I detect which coin or token was sent to the contract?


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?


Solution

  • 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.