I have an ERC20 token which is already deployed on the mainnet. I want to make a contract which accepts this token from users and pays dividends in Ethereum stored already inside the contract. In my knowledge, ERC20 doesn't have a default tokenfallback function. So how will I detect the incoming token transaction to my contract?
That's actually the biggest drawback of the ERC20 token standard, and that's why ERC777 tokens were invented. Everyone should be using ERC777 tokens instead.
The correct way of accepting ERC20 tokens is to use the approve => transferFrom pattern.
First, the sender sends an approve message to the ERC20 contract, where the sender approves your contract to transfer funds.
Second, the send calls your contract, which is now approved by the ERC20 token, so that your contract can use transferFrom on the ERC20 token, and transfer funds to itself (or someone else).
Tokens sent directly to a contract using the transfer method (which is not the correct way of doing it) will be PERMANENTLY LOCKED in that contract!
The transaction is sent to the ERC20 contract, not to your contract.
The ERC20 contract maintains a record of how many tokens are owned by your contract.
As a result of the transfer, this record within the ERC20 contract is updated.
Holding tokens is more like putting your money in a bank rather than keeping it in your wallet.
That bank maintains a ledger which indicates how much money each client owns at any given moment.