Open sameh-farouk opened 2 months ago
We may also consider replacing locks with reserves/holds, which would simplify the flow and make it less error-prone.
Update: Here is a sum of the changes so far
Payment state Management: Introducing ContractPaymentState aligns with the need to track and manage contract states accurately, especially during grace periods and when handling overdue payments.
improve events and error handling:
Reserve Mechanism: Uses reserves instead of locks to handle funds ensuring that the funds are earmarked for feature payments, preventing issues with liquidity. Reserves align better with handling common billing scenarios such as partial payments, fund releases, and transfers more straightforwardly and reliably.
Bug Fixes
completed: [x] Reserve Mechanism [X] overdraft tracker [x] better logging [x] better audibility [x] lazy migration [X] Adjust billing tests
Still WIP:
Choosing between locking and distributing less frequently, or not locking and distributing more frequently could be debatable as both actions require modifications to the account balances storage, which involves computational resources.
Locking Tokens: Each lock is recorded in the state, and removing or updating a lock involves additional state changes.
Transferring Tokens: Each transfer results in changes to the state (balance updates) for both the sender and the receiver. Also, distributing tokens in our logic involves multiple transfers to (foundation, sales, staking pool, and service provider) accounts.
The advantages of the current flow:
Locks due amounts every hour, leading to fewer state changes during the day.
Transfers the total amount once daily, spreading the computational load.
The disadvantages:
I believe that the current approach, if works correctly (which is not the case), is generally more efficient in terms of network usage. However, I propose simplifying the logic by transferring overdue amounts directly every cycle. This is because the added complexity of locking brings many flaws that cannot be justified by the potential savings in network usage.
We can still benefit from both worlds by changing the billing cycle to occur every 24 hours instead of hourly, aligning it with the cultivation distribution schedule. This approach consolidates multiple transfers into fewer transactions, reducing the computational load on the network to even lower levels than what we have currently.
Additional context