While reviewing #78 the following needs to be done to track user's shares on-chain instead of inside the ROFL database. This makes the chain the source of truth, avoids data loss in case of ROFL crash, improves privacy, because the backend doesn't have access to the complete history in the local DB anymore and having any discrepancy between the db and actual on-chain shares/assets calculation.
Proposed changes to EarnManager contract:
Ideally, we can then remove earn_transactions and compute cashflow data exclusively on the client side. The pool history is quite data-intensive, but it is public, so the backend can cache it.
While reviewing #78 the following needs to be done to track user's shares on-chain instead of inside the ROFL database. This makes the chain the source of truth, avoids data loss in case of ROFL crash, improves privacy, because the backend doesn't have access to the complete history in the local DB anymore and having any discrepancy between the db and actual on-chain shares/assets calculation.
Proposed changes to
EarnManagercontract:private mapping(address => EarnHistoryEntry[]) historysimilar to what we have in AccountingEarnHistoryEntryshould have:EarnHistoryKind kinduint64 timestampbytes payloadgetHistory()that is gated for specific user the same way as inAccounting.EarnHistoryKind.DepositToPoolandEarnHistoryKind.WithdrawFromPoolthepayloadis ABI-encoded:uint256 accountingHistoryIdxobtained by callingaccounting.HistoryEntry[] _, uint256 total = accounting.getHistory(0, 0, token); accountingHistoryIdx = total - 1;. This binds theEarnHistoryEntrywith the Accounting'sHistoryEntrythat containsamount,poolAddressandtokenId.uint256 sharesthe change in user shares. Always positive. CheckEarnHistoryKindto determine whether to increase or decrease the total users' shares value.EarnHistoryEntryto user's history for each deposit hereprivana-services/solidity/contracts/EarnManager.sol
Line 240 in 8677b91
EarnHistoryEntryto user's history for withdrawal hereprivana-services/solidity/contracts/EarnManager.sol
Line 302 in 8677b91
totalSharesandtotalAssetschange (i.e. insidesyncTotalAssets,withdraw,deposit), also record this in the pool's address history. TheEarnHistoryKind.SyncPoolAssetspayloadis ABI-encoded:uint256 totalSharesuint256 totalAssetsgetPoolHistory()public getter that returns pool's history entries. This way the privana-services backend can cache these values or UI can read it directly to compute the earned amount.Ideally, we can then remove
earn_transactionsand computecashflowdata exclusively on the client side. The pool history is quite data-intensive, but it is public, so the backend can cache it.