Skip to content

Commit e8358e0

Browse files
committed
fix: tvl
1 parent c67e76f commit e8358e0

File tree

13 files changed

+126
-74
lines changed

13 files changed

+126
-74
lines changed

debts/debt_db.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/Gearbox-protocol/sdk-go/log"
99
"github.com/Gearbox-protocol/sdk-go/utils"
1010
"github.com/Gearbox-protocol/third-eye/ds"
11+
"gorm.io/gorm"
1112
"gorm.io/gorm/clause"
1213
)
1314

@@ -141,16 +142,37 @@ func (eng *DebtEngine) addLastDebt(debt *schemas.Debt) {
141142
eng.lastDebts[debt.SessionId] = debt
142143
}
143144

144-
func (eng *DebtEngine) flushDebt(newDebtSyncTill int64) {
145-
debtLen := len(eng.debts)
146-
if debtLen == 0 && len(eng.tvlSnapshots) == 0 {
145+
func (eng *DebtEngine) flushTvl(tvlDebtSync int64, tx *gorm.DB, lastSync schemas.LastSync) {
146+
tvls := []*schemas.TvlSnapshots{}
147+
for _, tvl := range eng.tvlSnapshots {
148+
if tvl.BlockNum > lastSync.Tvl {
149+
tvls = append(tvls, tvl)
150+
}
151+
}
152+
if len(tvls) == 0 {
153+
return
154+
}
155+
err := tx.Exec(`UPDATE debt=?, field_set='t'`, tvlDebtSync).Error
156+
log.CheckFatal(err)
157+
err = tx.Clauses(clause.OnConflict{UpdateAll: true}).CreateInBatches(tvls, 50).Error
158+
log.CheckFatal(err)
159+
}
160+
func (eng *DebtEngine) flushDebt(newDebtSyncTill int64, tx *gorm.DB, lastSync schemas.LastSync) {
161+
debts := []*schemas.Debt{}
162+
for _, d := range eng.debts {
163+
if d.BlockNumber > lastSync.Debt {
164+
debts = append(debts, d)
165+
}
166+
}
167+
debtLen := len(debts)
168+
if debtLen == 0 {
147169
return
148170
}
149171
log.Infof("Flushing %d till block:%d", debtLen, newDebtSyncTill)
150-
tx := eng.db.Begin()
151-
err := tx.Clauses(clause.OnConflict{
152-
UpdateAll: true,
153-
}).Create(&schemas.DebtSync{LastCalculatedAt: newDebtSyncTill, FieldSet: true}).Error
172+
err := tx.Exec(`UPDATE debt=?, field_set='t'`, newDebtSyncTill).Error
173+
// err := tx.Clauses(clause.OnConflict{
174+
// UpdateAll: true,
175+
// }).Create(&schemas.DebtSync{Debt: newDebtSyncTill, FieldSet: true}).Error
154176
log.CheckFatal(err)
155177
liquidableAccounts := []*schemas.LiquidableAccount{}
156178
for _, la := range eng.liquidableBlockTracker {
@@ -166,14 +188,6 @@ func (eng *DebtEngine) flushDebt(newDebtSyncTill int64) {
166188
}).CreateInBatches(liquidableAccounts, 50).Error
167189
log.CheckFatal(err)
168190
}
169-
err = tx.CreateInBatches(eng.debts, 50).Error
191+
err = tx.CreateInBatches(debts, 50).Error
170192
log.CheckFatal(err)
171-
err = tx.CreateInBatches(eng.tvlSnapshots, 50).Error
172-
log.CheckFatal(err)
173-
info := tx.Commit()
174-
if info.Error != nil {
175-
log.Fatal(info.Error)
176-
}
177-
eng.debts = []*schemas.Debt{}
178-
eng.tvlSnapshots = []*schemas.TvlSnapshots{}
179193
}

debts/engine.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (eng *DebtEngine) updateLocalState(blockNum int64, block *schemas.Block) (p
6565
BlockNum: ps.BlockNum,
6666
CumulativeIndexRAY: ps.CumulativeIndexRAY,
6767
AvailableLiquidityBI: ps.AvailableLiquidityBI,
68+
ExpectedLiqBI: ps.ExpectedLiquidityBI,
6869
BaseBorrowAPYBI: ps.BaseBorrowAPYBI,
6970
Timestamp: block.Timestamp,
7071
})
@@ -180,7 +181,7 @@ func (eng *DebtEngine) createTvlSnapshots(blockNum int64, caTotalValueInUSD floa
180181
if eng.lastTvlSnapshot != nil && blockNum-eng.lastTvlSnapshot.BlockNum < core.NoOfBlocksPerHr { // tvl snapshot every hour
181182
return
182183
}
183-
var totalAvailableLiquidityInUSD float64 = 0
184+
var totalAvailableLiquidityInUSD, expectedLiqInUSD float64 = 0, 0
184185
log.Info("tvl for block", blockNum)
185186
for _, entry := range eng.poolLastInterestData {
186187
adapter := eng.repo.GetAdapter(entry.Address)
@@ -203,11 +204,17 @@ func (eng *DebtEngine) createTvlSnapshots(blockNum int64, caTotalValueInUSD floa
203204
eng.GetAmountInUSD(
204205
underlyingToken,
205206
entry.AvailableLiquidityBI.Convert(), version), 8)
207+
expectedLiqInUSD += utils.GetFloat64Decimal(
208+
eng.GetAmountInUSD(
209+
underlyingToken,
210+
entry.ExpectedLiqBI.Convert(), version), 8)
211+
206212
}
207213
// save as last tvl snapshot and add to db
208214
tvls := &schemas.TvlSnapshots{
209215
BlockNum: blockNum,
210216
AvailableLiquidity: totalAvailableLiquidityInUSD,
217+
ExpectedLiq: expectedLiqInUSD,
211218
CATotalValue: caTotalValueInUSD,
212219
}
213220
eng.tvlSnapshots = append(eng.tvlSnapshots, tvls)

debts/index.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
102103
func (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.
170182
func (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
}

ds/debt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
type DebtEngineI interface {
1414
Clear()
1515
ProcessBackLogs()
16-
CalculateDebtAndClear(to int64)
16+
CalculateDebtAndClear(to int64, lastSync schemas.LastSync)
1717
CalCurrentDebts(to int64)
1818
CalculateDebt()
1919
GetDebts() core.Json
@@ -34,7 +34,7 @@ type DebtProfile struct {
3434
Tokens map[string]TokenDetails `json:"tokens"`
3535
UnderlyingDecimals int8 `json:"underlyingDecimals"`
3636
*CumIndexAndUToken `json:"poolDetails"`
37-
CalcString string `json:"calcString"`
37+
CalcString string `json:"calcString"`
3838
}
3939

4040
type CumIndexAndUToken struct {

ds/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ type RepositoryI interface {
125125
// oracle and uni
126126
GetRetryFeedForDebts() []QueryPriceFeedI
127127
//
128-
LoadLastDebtSync() int64
128+
LoadLastDebtSync() schemas.LastSync
129129
LoadLastAdapterSync() int64
130130
Clear()
131131
// multicall

ds/repo_dummy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ func (DummyRepo) GetRetryFeedForDebts() []QueryPriceFeedI {
225225
func (DummyRepo) AddNewPriceOracleEvent(tokenOracle *schemas.TokenOracle, bounded bool, forChainlinkNewFeed ...bool) {
226226
}
227227

228-
func (DummyRepo) LoadLastDebtSync() int64 {
229-
return 0
228+
func (DummyRepo) LoadLastDebtSync() schemas.LastSync {
229+
return schemas.LastSync{}
230230
}
231231
func (DummyRepo) LoadLastAdapterSync() int64 {
232232
return 0

engine/index.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ func (e *Engine) syncLoop(syncedTill, latestBlockNum int64) int64 {
141141
func (e *Engine) SyncAndFlush(syncTill int64) {
142142
e.Sync(syncTill)
143143
e.repo.Flush(syncTill)
144-
e.debtEng.CalculateDebtAndClear(syncTill)
144+
e.debtEng.CalculateDebtAndClear(syncTill, schemas.LastSync{
145+
Debt: 0,
146+
Tvl: 0,
147+
})
145148
if syncTill > e.syncedBlock.Load().(int64) {
146149
e.syncedBlock.Store(syncTill)
147150
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/Gearbox-protocol/third-eye
33
go 1.20
44

55
require (
6-
github.com/Gearbox-protocol/sdk-go v0.0.0-20250320002156-58bee411faa4
6+
github.com/Gearbox-protocol/sdk-go v0.0.0-20250320003540-9953f5c38c6b
77
github.com/ethereum/go-ethereum v1.13.14
88
github.com/go-playground/validator/v10 v10.4.1
99
github.com/google/go-cmp v0.5.9

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ contrib.go.opencensus.io/exporter/ocagent v0.6.0/go.mod h1:zmKjrJcdo0aYcVS7bmEeS
55
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
66
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
77
github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
8-
github.com/Gearbox-protocol/sdk-go v0.0.0-20250309120715-87a114a7ad27 h1:KhRX015i5aCEzklx8AlY/CneXgeSae8K4FDU5JBb+RA=
9-
github.com/Gearbox-protocol/sdk-go v0.0.0-20250309120715-87a114a7ad27/go.mod h1:jRBSOG94bpGc5ci8EWIPUVXZdaGEaekMNmhajbmWFVU=
10-
github.com/Gearbox-protocol/sdk-go v0.0.0-20250320002156-58bee411faa4 h1:9i0ceC/qhfITsKzoaKYxGOX6r6zau1QhmA460fNY06E=
11-
github.com/Gearbox-protocol/sdk-go v0.0.0-20250320002156-58bee411faa4/go.mod h1:jRBSOG94bpGc5ci8EWIPUVXZdaGEaekMNmhajbmWFVU=
8+
github.com/Gearbox-protocol/sdk-go v0.0.0-20250320003540-9953f5c38c6b h1:l3pNJGDpRDmSkqoh/zI048M4uvqC0mt5fPKrpNp/X7o=
9+
github.com/Gearbox-protocol/sdk-go v0.0.0-20250320003540-9953f5c38c6b/go.mod h1:jRBSOG94bpGc5ci8EWIPUVXZdaGEaekMNmhajbmWFVU=
1210
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
1311
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
1412
github.com/OffchainLabs/go-ethereum v1.13.4-0.20240313010929-e5d8587e7227 h1:+/3TrD+q+BP36jGj2Bycdmrc/joKLNbc5ImePQzKRLM=

migrations/000055_tvl.up.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
alter table debt_sync add debt_block integer, add tvl_block integer;
2+
update debt_sync set debt_block=last_calculated_at;
3+
update debt_sync set tvl_block=last_calculated_at;
4+
alter table debt_sync drop column last_calculated_at;
5+
6+
alter table tvl_snapshots add expected_liq DOUBLE PRECISION;

0 commit comments

Comments
 (0)