I have to transfer tokens from one account to another and I would like to associate a timestamp with that tokens so that i can identify which token was transferred on which time.
How do we write this code in a smart contract?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityYou can just enrich your structure in order to store the timestamp.
Consider a simple mapping like this:
mapping (address => uint256) public balanceOf;
to do something like you said would be enough to have a structure like this:
struct Balance { uint amount; uint timestamp; } mapping (address => Balance) public balanceOf;
and structure your code accordingly in order to save the timestamp together with the amount.
If you don't need the timestamp within the contract, you could just use the transaction block's timestamp. So whenever a token is transferred you can check in which block it was and check its timestamp.
But if you need to store the info in the contract itself then @mirg's answer will do.