@@ -73,31 +73,32 @@ func (eng *DebtEngine) ProcessBackLogs() {
7373 return
7474 }
7575 // synced till
76- lastDebtSynced := eng .repo .LoadLastDebtSync ()
76+ lastSync := eng .repo .LoadLastDebtSync ()
77+ minSynced := lastSync .Min ()
7778 // lastDebtSynced = 227143579
78- log .Info ("Debt engine started, from" , lastDebtSynced )
79+ log .Info ("Debt engine started, from" , minSynced )
7980 eng .loadLastTvlSnapshot ()
80- eng .loadLastCSS (lastDebtSynced )
81- eng .loadLastRebaseDetails (lastDebtSynced )
82- eng .loadTokenLastPrice (lastDebtSynced )
83- eng .loadAllowedTokenThreshold (lastDebtSynced )
84- eng .loadLastLTRamp (lastDebtSynced )
85- eng .loadPoolLastInterestData (lastDebtSynced )
86- eng .loadLastDebts (lastDebtSynced )
87- eng .loadParameters (lastDebtSynced )
88- eng .loadLiquidableAccounts (lastDebtSynced )
81+ eng .loadLastCSS (minSynced )
82+ eng .loadLastRebaseDetails (minSynced )
83+ eng .loadTokenLastPrice (minSynced )
84+ eng .loadAllowedTokenThreshold (minSynced )
85+ eng .loadLastLTRamp (minSynced )
86+ eng .loadPoolLastInterestData (minSynced )
87+ eng .loadLastDebts (minSynced )
88+ eng .loadParameters (minSynced )
89+ eng .loadLiquidableAccounts (minSynced )
8990 // v3
9091 // eng.loadAccounQuotaInfo(lastDebtSynced, eng.db)
91- eng .loadPoolQuotaDetails (lastDebtSynced , eng .db )
92+ eng .loadPoolQuotaDetails (minSynced , eng .db )
9293 //
9394 // process blocks for calculating debts
9495 adaptersSyncedTill := eng .repo .LoadLastAdapterSync ()
9596 // adaptersSyncedTill = 227143580
9697 batchSize := eng .config .BatchSizeForHistory
97- for ; lastDebtSynced + batchSize < adaptersSyncedTill ; lastDebtSynced += batchSize {
98- eng .processBlocksInBatch (lastDebtSynced , lastDebtSynced + batchSize )
98+ for ; minSynced + batchSize < adaptersSyncedTill ; minSynced += batchSize {
99+ eng .processBlocksInBatch (minSynced , minSynced + batchSize , lastSync )
99100 }
100- eng .processBlocksInBatch (lastDebtSynced , adaptersSyncedTill )
101+ eng .processBlocksInBatch (minSynced , adaptersSyncedTill , lastSync )
101102}
102103func (eng * DebtEngine ) loadLastTvlSnapshot () {
103104 lastTvlSnapshot := & schemas.TvlSnapshots {}
@@ -108,23 +109,34 @@ func (eng *DebtEngine) loadLastTvlSnapshot() {
108109}
109110
110111// load blocks from > and to <=
111- func (eng * DebtEngine ) processBlocksInBatch (from , to int64 ) {
112+ func (eng * DebtEngine ) processBlocksInBatch (from , to int64 , lastSync schemas. LastSync ) {
112113 if from == to {
113114 return
114115 }
115116 eng .repo .LoadBlocks (from , to )
116117 if len (eng .repo .GetBlocks ()) > 0 {
117- eng .CalculateDebtAndClear (to )
118+ eng .CalculateDebtAndClear (to , lastSync )
118119 }
119120}
120121
121122// called for the engine/index.go and the debt engine
122- func (eng * DebtEngine ) CalculateDebtAndClear (to int64 ) {
123+ func (eng * DebtEngine ) CalculateDebtAndClear (to int64 , lastSync schemas. LastSync ) {
123124 if ! eng .config .DisableDebtEngine {
124125 eng .CalculateDebt ()
125- eng .flushDebt (to )
126- eng .CalCurrentDebts (to )
127- eng .flushCurrentDebts (to )
126+ //
127+ tx := eng .db .Begin ()
128+ eng .flushDebt (to , tx , lastSync )
129+ eng .flushTvl (to , tx , lastSync )
130+ if info := tx .Commit (); info .Error != nil {
131+ log .Fatal (info .Error )
132+ }
133+ eng .tvlSnapshots = []* schemas.TvlSnapshots {}
134+ eng .debts = []* schemas.Debt {}
135+ //
136+ if to > lastSync .Debt {
137+ eng .CalCurrentDebts (to )
138+ eng .flushCurrentDebts (to )
139+ }
128140 }
129141 eng .Clear ()
130142}
@@ -168,14 +180,16 @@ func (eng *DebtEngine) notifiedIfLiquidable(sessionId string, notified bool) {
168180
169181// QueryPriceFeed is updated only till the lastFetchedBlock, not the syncTill that is provided to the aqfwrapper's aftersynchook from engine/index.go in the syncmodel. So, ignore that for updating the debts.
170182func (eng * DebtEngine ) AreActiveAdapterSynchronized () bool {
171- data := schemas.DebtSync {}
172- query := `SELECT count(distinct last_sync) as last_calculated_at FROM sync_adapters
183+ data := struct {
184+ LastSync int64 `json:"last_sync"`
185+ }{}
186+ query := `SELECT count(distinct last_sync) as last_sync FROM sync_adapters
173187 WHERE disabled=false AND type NOT IN ('QueryPriceFeed','RebaseToken','Treasury','LMRewardsv2','LMRewardsv3','GearToken')`
174188 err := eng .db .Raw (query ).Find (& data ).Error
175189 if err != nil {
176190 log .Fatal (err )
177191 }
178- val := data .LastCalculatedAt <= 1
192+ val := data .LastSync <= 1
179193 if ! val {
180194 log .Warn ("DebtEngine disabled active adapters are not synchronised" )
181195 }
0 commit comments