I am developing a smart contract and looking for the best way to track the liquidity deposited by a user for each Epoch on token X. Here is the context of my project:
When a user deposits liquidity, they will be rewarded by the protocol in the future through a fee-sharing mechanism. Therefore, I need to know the liquidity distribution at any time for a given Epoch to distribute these fees appropriately.
Current Problem
I see two possible approaches, but they have their limitations:
1. Store all deposits and withdrawals for each user with the associated Epoch:
- Use mappers to record each operation.
- Calculate the fee distribution on-demand by iterating over these records.
- Drawback: This seems very costly in terms of gas, especially if the number of operations is high.
2. Consolidate the data from Epoch N-1 when transitioning to Epoch N:
- Consolidate all data at the end of each Epoch to avoid frequent calculations.
- Drawback: This requires a potentially costly consolidation operation at each Epoch transition.
Question
What would be the best method to efficiently implement this tracking of liquidity deposited per Epoch? Are there any recommended practices or proven design patterns in this area?
Thank you in advance for your suggestions and help.
Mer0ps is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.