Skip to content

Commit 0b963d9

Browse files
committed
feat: bnb
1 parent f7d0bff commit 0b963d9

File tree

23 files changed

+166
-164
lines changed

23 files changed

+166
-164
lines changed

db_scripts/delete_rewards_v3.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ delete from user_lmdetails_v3 ;
33
delete from sync_adapters where type='LMRewardsv3';
44

55
WITH data as (select * from sync_adapters where type = 'AddressProvider')
6-
insert into sync_adapters(address, type, last_sync, firstlog_at, version, discovered_at, disabled) values (
7-
'0x00000000000000000000000000000000000beef3',
8-
'LMRewardsv3',
9-
(SELECT firstlog_at-1 from data),
10-
(SELECT firstlog_at from data),
11-
'300',
12-
(SELECT firstlog_at from data),
13-
'f'
14-
);
6+
-- insert into sync_adapters(address, type, last_sync, firstlog_at, version, discovered_at, disabled) values (
7+
-- '0x00000000000000000000000000000000000beef3',
8+
-- 'LMRewardsv3',
9+
-- (SELECT firstlog_at-1 from data),
10+
-- (SELECT firstlog_at from data),
11+
-- '300',
12+
-- (SELECT firstlog_at from data),
13+
-- 'f'
14+
-- );

debts/calc_details.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type sessionDetailsForCalc struct {
1414
*schemas.CreditSessionSnapshot
1515
CM string
1616
rebaseDetails *schemas.RebaseDetailsForDB
17-
stETH string
17+
client core.ClientI
1818
// for v3
1919
forQuotas v3DebtDetails
2020
version core.VersionType
@@ -29,7 +29,11 @@ func (s sessionDetailsForCalc) GetCM() string {
2929
}
3030
func (s sessionDetailsForCalc) GetBalances() core.DBBalanceFormat {
3131
// credit session snapshot is not saved to db , so we can use it.
32-
schemas.AdjustRebaseToken(*s.Balances, s.stETH, s.rebaseDetails)
32+
stETH := core.NULL_ADDR
33+
if core.GetBaseChainId(s.client) == 1 {
34+
stETH = core.GetToken(1, "stETH")
35+
}
36+
schemas.AdjustRebaseToken(*s.Balances, stETH.Hex(), s.rebaseDetails)
3337
return *s.Balances
3438
}
3539

debts/engine.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,18 @@ func (eng *DebtEngine) SessionDebtHandler(blockNum int64, session *schemas.Credi
375375
}
376376

377377
// todo fix, also check the feed type at this block number
378-
func (eng *DebtEngine) hasRedStoneToken(balances *core.DBBalanceFormat) bool {
379-
pfs := core.GetRedStonePFByChainId(core.GetChainId(eng.client))
380-
addToSym := core.GetTokenToSymbolByChainId(core.GetChainId(eng.client))
378+
func (eng *DebtEngine) hasRedStoneToken(cm string, version core.VersionType, balances *core.DBBalanceFormat) bool {
381379
for tokenAddr, details := range *balances {
382-
if _, ok := pfs[addToSym[common.HexToAddress(tokenAddr)]]; details.IsEnabled && details.HasBalanceMoreThanOne() && ok {
380+
if !(details.IsEnabled && details.HasBalanceMoreThanOne()) {
381+
continue
382+
}
383+
priceFeed := eng.priceHandler.GetLastPriceFeed(cm, tokenAddr, version, true)
384+
if priceFeed == nil { // don't know if reverse
385+
continue
386+
}
387+
feed := eng.repo.GetAdapter(priceFeed.Feed)
388+
pfType := feed.GetDetails()["pfType"]
389+
if pfType == "RedStonePF" || pfType == "CompositeRedStonePF" {
383390
return true
384391
}
385392
}
@@ -403,10 +410,10 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
403410
},
404411
sessionDetailsForCalc{
405412
addr: session.Account,
413+
client: eng.client,
406414
CreditSessionSnapshot: sessionSnapshot,
407415
CM: session.CreditManager,
408416
rebaseDetails: eng.lastRebaseDetails,
409-
stETH: eng.repo.GetTokenFromSdk("stETH"),
410417
version: session.Version,
411418
forQuotas: eng.v3DebtDetails,
412419
},
@@ -479,10 +486,10 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
479486
profile.CreditSessionSnapshot = sessionSnapshot
480487
profile.UnderlyingDecimals = cumIndexAndUToken.Decimals
481488
log.Warn(utils.ToJson(profile))
482-
} else if eng.hasRedStoneToken(sessionSnapshot.Balances) {
483-
if IsChangeMoreThanFraction(debt.CalTotalValueBI, sessionSnapshot.TotalValueBI, big.NewFloat(0.003)) { // .3% allowed
484-
notMatched = true
485-
}
489+
// } else if eng.hasRedStoneToken(sessionSnapshot.Balances) {
490+
// if IsChangeMoreThanFraction(debt.CalTotalValueBI, sessionSnapshot.TotalValueBI, big.NewFloat(0.003)) { // .3% allowed
491+
// notMatched = true
492+
// }
486493
} else {
487494
notMatched = true
488495
}
@@ -497,7 +504,7 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
497504
}
498505
}
499506
eng.calAmountToPoolAndProfit(debt, session, cumIndexAndUToken, debtDetails)
500-
eng.farmingCalc.addFarmingVal(debt, session, eng.lastCSS[session.ID], storeForCalc{inner: eng})
507+
// eng.farmingCalc.addFarmingVal(debt, session, eng.lastCSS[session.ID], storeForCalc{inner: eng})
501508
if notMatched {
502509
profile.CumIndexAndUToken = cumIndexAndUToken
503510
profile.Debt = debt

debts/index.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ type DebtEngine struct {
2828
currentDebts []*schemas.CurrentDebt
2929
liquidableBlockTracker map[string]*schemas.LiquidableAccount
3030
// cm to paramters
31-
lastParameters map[string]*schemas.Parameters
32-
isTesting bool
33-
farmingCalc *FarmingCalculator
31+
lastParameters map[string]*schemas.Parameters
32+
isTesting bool
33+
// farmingCalc *FarmingCalculator
3434
marketTolastTvlBlock map[string]int64
3535
lastRebaseDetails *schemas.RebaseDetailsForDB
3636
// used for v3 calc account fields
@@ -52,11 +52,11 @@ func GetDebtEngine(db *gorm.DB, client core.ClientI, config *config.Config, repo
5252
liquidableBlockTracker: make(map[string]*schemas.LiquidableAccount),
5353
lastParameters: make(map[string]*schemas.Parameters),
5454
isTesting: testing,
55-
farmingCalc: NewFarmingCalculator(core.GetChainId(client), testing),
56-
v3DebtDetails: Newv3DebtDetails(),
57-
tokenLTRamp: map[string]map[string]*schemas_v3.TokenLTRamp{},
58-
priceHandler: NewPriceHandler(repo),
59-
marketTolastTvlBlock: make(map[string]int64),
55+
// farmingCalc: NewFarmingCalculator(core.GetChainId(client), testing),
56+
v3DebtDetails: Newv3DebtDetails(),
57+
tokenLTRamp: map[string]map[string]*schemas_v3.TokenLTRamp{},
58+
priceHandler: NewPriceHandler(repo),
59+
marketTolastTvlBlock: make(map[string]int64),
6060
}
6161
}
6262

debts/tf_index.go

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,51 @@
11
package debts
22

3-
import (
4-
"github.com/Gearbox-protocol/sdk-go/calc"
5-
"github.com/Gearbox-protocol/sdk-go/core"
6-
"github.com/Gearbox-protocol/sdk-go/core/schemas"
7-
"github.com/Gearbox-protocol/sdk-go/log"
8-
"github.com/Gearbox-protocol/sdk-go/utils"
9-
)
3+
// type FarmingCalculator struct {
4+
// tradingTokensMap map[string]bool
5+
// usdc string
6+
// testing bool
7+
// }
108

11-
type FarmingCalculator struct {
12-
tradingTokensMap map[string]bool
13-
usdc string
14-
testing bool
15-
}
9+
// func NewFarmingCalculator(chainId int64, testing bool) *FarmingCalculator {
10+
// tradingTokens := core.AllTradingSymbolForDBWithW()
11+
// //
12+
// tradingTokensMap := map[string]bool{}
13+
// syms := core.GetSymToAddrByChainId(chainId)
14+
// for _, tradingSym := range tradingTokens {
15+
// if addr, ok := syms.Tokens[string(tradingSym)]; ok {
16+
// tradingTokensMap[addr.Hex()] = true
17+
// } else if (tradingSym == "stETH" || tradingSym == "GUSD") && log.GetBaseNet(chainId) == "ARBITRUM" { // these tokens are not present on arbitrum
18+
// // ignore
19+
// } else if utils.Contains([]core.Symbol{"stETH", "GUSD", "LQTY", "CVX", "yvUSDC", "GMX", "ARB", "MKR", "sDAI", "APE"}, tradingSym) && log.GetBaseNet(chainId) == "OPTIMISM" {
20+
// } else {
21+
// log.Warnf("Trading token(%s) for tf_index missing from sdk config: %s", tradingSym, addr)
22+
// }
23+
// }
24+
// return &FarmingCalculator{
25+
// tradingTokensMap: tradingTokensMap,
26+
// usdc: core.GetToken(chainId, "USDC").Hex(),
27+
// testing: testing,
28+
// }
29+
// }
1630

17-
func NewFarmingCalculator(chainId int64, testing bool) *FarmingCalculator {
18-
tradingTokens := core.AllTradingSymbolForDBWithW()
19-
//
20-
tradingTokensMap := map[string]bool{}
21-
syms := core.GetSymToAddrByChainId(chainId)
22-
for _, tradingSym := range tradingTokens {
23-
if addr, ok := syms.Tokens[string(tradingSym)]; ok {
24-
tradingTokensMap[addr.Hex()] = true
25-
} else if (tradingSym == "stETH" || tradingSym == "GUSD") && log.GetBaseNet(chainId) == "ARBITRUM" { // these tokens are not present on arbitrum
26-
// ignore
27-
} else if utils.Contains([]core.Symbol{"stETH", "GUSD", "LQTY", "CVX", "yvUSDC", "GMX", "ARB", "MKR", "sDAI", "APE"}, tradingSym) && log.GetBaseNet(chainId) == "OPTIMISM" {
28-
} else {
29-
log.Warnf("Trading token(%s) for tf_index missing from sdk config: %s", tradingSym, addr)
30-
}
31-
}
32-
return &FarmingCalculator{
33-
tradingTokensMap: tradingTokensMap,
34-
usdc: syms.Tokens["USDC"].Hex(),
35-
testing: testing,
36-
}
37-
}
38-
39-
func (calc FarmingCalculator) addFarmingVal(debt *schemas.Debt, session *schemas.CreditSession, css *schemas.CreditSessionSnapshot, priceStore calc.TokenDetailsForCalcI) {
40-
if calc.testing || session.Status != schemas.Active {
41-
return
42-
}
43-
var farmingVal float64 = 0
44-
for token, balance := range *css.Balances {
45-
if balance.IsEnabled && balance.HasBalanceMoreThanOne() && !calc.tradingTokensMap[token] {
46-
var priceDecimals int8 = 8
47-
if session.Version.Eq(1) {
48-
priceDecimals = 18
49-
}
50-
farmingVal += balance.F * utils.GetFloat64Decimal(priceStore.GetPriceOnBlock(session.CreditManager, token, session.Version), priceDecimals)
51-
}
52-
}
53-
if session.Version.Eq(1) {
54-
farmingVal = farmingVal / utils.GetFloat64Decimal(priceStore.GetPriceOnBlock(session.CreditManager, calc.usdc, session.Version), 18) // convert to usd
55-
// by dividing by usdc price in eth
56-
}
57-
// farming val is zero for closed accounts
58-
debt.FarmingValUSD = farmingVal
59-
}
31+
// func (calc FarmingCalculator) addFarmingVal(debt *schemas.Debt, session *schemas.CreditSession, css *schemas.CreditSessionSnapshot, priceStore calc.TokenDetailsForCalcI) {
32+
// if calc.testing || session.Status != schemas.Active {
33+
// return
34+
// }
35+
// var farmingVal float64 = 0
36+
// for token, balance := range *css.Balances {
37+
// if balance.IsEnabled && balance.HasBalanceMoreThanOne() && !calc.tradingTokensMap[token] {
38+
// var priceDecimals int8 = 8
39+
// if session.Version.Eq(1) {
40+
// priceDecimals = 18
41+
// }
42+
// farmingVal += balance.F * utils.GetFloat64Decimal(priceStore.GetPriceOnBlock(session.CreditManager, token, session.Version), priceDecimals)
43+
// }
44+
// }
45+
// if session.Version.Eq(1) {
46+
// farmingVal = farmingVal / utils.GetFloat64Decimal(priceStore.GetPriceOnBlock(session.CreditManager, calc.usdc, session.Version), 18) // convert to usd
47+
// // by dividing by usdc price in eth
48+
// }
49+
// // farming val is zero for closed accounts
50+
// debt.FarmingValUSD = farmingVal
51+
// }

ds/adapter_kit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (kit *AdapterKit) Add(adapter SyncAdapterI) {
6363

6464
func (kit *AdapterKit) Get(lvl int) SyncAdapterI {
6565
adapterAddr := kit.levels[lvl].Get()
66-
return kit.addressMap[adapterAddr]
66+
return kit.GetAdapter(adapterAddr)
6767
}
6868

6969
func (kit *AdapterKit) Next(lvl int) bool {

ds/repo.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ type RepositoryI interface {
136136
AddTokenObj(token *schemas.Token)
137137
PrepareSyncAdapter(adapter *SyncAdapter) SyncAdapterI
138138
//
139-
GetTokenFromSdk(symbol string) string
140139

141140
// v3 events
142141
AddTokenLTRamp(*schemas_v3.TokenLTRamp)

ds/repo_dummy.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,6 @@ func (DummyRepo) PrepareSyncAdapter(adapter *SyncAdapter) SyncAdapterI {
248248
return nil
249249
}
250250

251-
func (DummyRepo) GetTokenFromSdk(string) string {
252-
return ""
253-
}
254251
func (DummyRepo) AddTokenLTRamp(*schemas_v3.TokenLTRamp) {}
255252
func (DummyRepo) AddQuotaDetails(*schemas_v3.QuotaDetails) {}
256253

engine/index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ func (e *Engine) init() {
6666

6767
func (e *Engine) addstETHToken() {
6868
// rebase token stETH
69-
chainId := core.GetChainId(e.Client)
69+
chainId := core.GetBaseChainId(e.Client)
7070
if chainId != 1 {
7171
return
7272
}
73-
addr := core.GetSymToAddrByChainId(chainId).Tokens["stETH"]
73+
addr := core.GetToken(1, "stETH")
7474
if core.NULL_ADDR != addr { // on arbitrum
7575
stETHToken := rebase_token.NewRebaseToken(addr.Hex(), e.Client, e.repo)
7676
e.repo.AddSyncAdapter(stETHToken)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.23
55
// toolchain go1.23.2
66

77
require (
8-
github.com/Gearbox-protocol/sdk-go v0.0.0-20250423102654-73c5f41af8b5
8+
github.com/Gearbox-protocol/sdk-go v0.0.0-20250509050800-076666161347
99
github.com/ethereum/go-ethereum v1.13.14
1010
github.com/go-playground/validator/v10 v10.4.1
1111
github.com/google/go-cmp v0.6.0
@@ -84,6 +84,6 @@ require (
8484

8585
replace github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.22.1
8686

87-
replace github.com/Gearbox-protocol/sdk-go v0.0.0-20250408052012-1a750509b0a0 => ../sdk-go
87+
replace github.com/Gearbox-protocol/sdk-go v0.0.0-20250509045933-6db00377fde1 => ../sdk-go
8888

8989
replace github.com/ethereum/go-ethereum v1.13.14 => github.com/OffchainLabs/go-ethereum v1.13.4-0.20240313010929-e5d8587e7227

0 commit comments

Comments
 (0)