ERC-20 – How to Accept ERC-20 Tokens as Payment

erc-20payments

I'm looking to integrate ERC-20 Token into my shop. Now I have a lot of products so I need automated system to keep track of payments. I did some reading and it looks like if I write smart contract to handle this I would need to prompt user two times (one for allowance and one for transfer) which is not great. I wonder what is the common practice of handling this issue ? I know that ERC-223 solves that problem but unfortunately most tokens are ERC-20.

Another solution is maybe to generate new address for each purchase, but then I would need to transfer ETH to each address in order to move tokens which could get very expensive.

Best Answer

You can use events which are a part of the ERC20 definition to keep track of payments. The Transfer event is triggered when someone makes a payment. So you need to listen for Transfer events where the _to parameter matches your recipient address.

Transfer(address indexed _from, address indexed _to, uint256 _value)

You may still have the problem of two customers buying the same product at the same time which would make it impossible to differentiate who has paid and who has not paid. If you do not want to use separate receiving accounts for each payment, you can use separate amounts for each payment: You could cycle through different values at the least significant digits (1-3 digits) of the amount (the smallest possible values usually are not worth anything). You could also cycle through various payment addresses, thus having only ten or one hundred payment addresses that you receive on, making sure that only one is in use at a time.

You could even use a combination of these two strategies to all but eliminate the risk of a "payment collision" where you are unable to differentiate between two payment requests.

Related Topic