time_window: A time-period, starting at each pool period = (end of month timestamp - beginning of month timestamp)
struct timeWeightedWaterHoldingsStruct {
total_time: uint256; // total time deposits were held
total_value: uint256; // Weighted sum of all deposits with their held time.
current_deposit_time: uint256; // Timestamp of the first deposit (it gets updated upon withdrawal).
current_deposit_value: uint256; // The current total deposit value.
}
Every deposit/withdrawal will update these values.
Upon Deposit:
If this is a new deposit (i.e., no ongoing deposit), set current_deposit_time to the current timestamp.
Add the deposit to current_deposit_value.
Upon Withdrawal:
Calculate the time the deposit was held: held_time = current_timestamp - current_deposit_time.
Update the weighted sum: total_value += current_deposit_value * held_time.
Update the total time: total_time += held_time.
Reset current_deposit_value to 0 (or subtract the withdrawal if you allow partial withdrawals).
Compute the Average:
The average deposit value over (poolEndTime - poolStartTime) would be - average = total_value / total_time.
Here's a method using the idea of a running weighted average for the monthly average of held Water:
Data Structure:
time_window: A time-period, starting at each pool period = (end of month timestamp - beginning of month timestamp)
Every deposit/withdrawal will update these values.
Upon Deposit:
Upon Withdrawal:
Compute the Average: