Is there any way that we can add hex data in ERC20 transaction? Let s say we want to keep track of why we send the token and other detailed information. How can we achieve that?
Yes this is possible.
You have to modify the
transfer function of your token contract.
Doing this would be a bit complicated. Keep in mind you need to pay attention to still comply with the EIP20 Standard.
I would go about this by creating a new function with the same parameters as
transfer and you additional paramets, save your information and then call
transfer function with the
value parameter. Therefore, the
Transfer event gets emitted and you are still compliant to the standard.
See my comment about why I don't think this is feasible.
As a general heuristic, let the currency be a currency - a simple unit of account, a medium of exchange and possibly a store of value. A dollar doesn't know why it moves but the machinery that accepts it usually does. Token contracts spell out the supply and look after basic accounting.
A line-of-business contract can present any set of functions that collect required information, possibly coordinated with payment processes. This is usually the best region to be thinking about implementing business rules. Not as properties of the currency but as terms of service, delivery and usually records.
A token contract can tell you how much money each participant has. A line-of-business contract might be able to explain why.
Hope it helps.
As I mentioned in comments to the other answer: in theory it is possible to add such functionality (by for example calling another function before the real transfer) but, in my opinion, in reality it's not feasible. Mostly because I doubt any third party wants to implement such extra features at their end. The point of standards is that they are standard and one implementation covers all contracts which follow the standard. Modifications which are outside the standard mean extra work for anyone who uses the token just for this one token. Furthermore, it would be tricky to keep tract of which extra data belongs to which transfer if the extra data is stored in a separate function call.
There may be other standards (derived from EIP20) out there which might do what you want, but they are not widely accepted in any case. Still, a bad standard would be better than no standard.
One option would be to simply forget about standards. So just write your token to be whatever you need it to be. It most likely won't be accepted in any exchanges and no third party wants to implement it but depending on your needs this might not be a problem for you.