I need to build a smart contract where users can lock their ERC20 tokens for a fixed amount of time (same amount of time for all users, e.g. 1 year). Lets say a user locks 100 tokens and half a year later he locks another 100 tokens, the first 100 tokens should still unlock after 6 months and the second batch of tokens after a year.
My current idea is to use an ERC1155 token contract to keep track of the time locks. From the example above, when the first user deposits their tokens, 100 ERC1155 tokens with the token id 0 would be minted that unlock after 1 year. When they deposit the next 100 tokens another 100 ERC1155 tokens with the token id 1 would be minted, and so on. The solution is not ideal because in the worst case scenario a user would have to withdraw multiple times and it would be harder to keep track of all tokens.
Another idea I had that you could have the tokens unlock linearly, so after 3 months 1/4 of the tokens are unlocked, after half a year 1/2 of the tokens are unlocked, etc. When a user deposits more tokens, this equation is adjusted so that the full amount of tokens releases after 1 year but they can still withdraw the same amount as before after depositing more tokens. Would need to figure out the Math behind this one though. This would have the benefit that the total amount of unlocked tokens could be withdrawn at once and it's easy to keep track of the users balance if tokens are deposited multiple times.
Does anyone have another idea to tackle this issue or maybe is there a project that has similar mechanics already I could have a look at?