Skip to content

Commit

Permalink
feat: use debtdetails
Browse files Browse the repository at this point in the history
  • Loading branch information
5lliot authored and 5lliot committed Oct 25, 2023
1 parent 9ef8b19 commit 2d83d07
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
18 changes: 8 additions & 10 deletions debts/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
//
// calculating account fields
calculator := calc.Calculator{Store: storeForCalc{inner: eng}}
calHF, calDebt, calTotalValue, calThresholdValue, _calBorowedWithInterst := calculator.CalcAccountFields(
calHF, calTotalValue, calThresholdValue, debtDetails := calculator.CalcAccountFields(
eng.currentTs,
blockNum,
poolDetailsForCalc{
Expand Down Expand Up @@ -365,11 +365,10 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
CalHealthFactor: (*core.BigInt)(calHF),
CalTotalValueBI: (*core.BigInt)(calTotalValue),
// it has fees too for v2
CalDebtBI: (*core.BigInt)(calDebt),
CalDebtBI: (*core.BigInt)(debtDetails.Total()),
// used for calculating the close amount and for comparing with data compressor results as v2 doesn't have borrowedAmountWithInterest
CalBorrowedWithInterestBI: (*core.BigInt)(_calBorowedWithInterst),
CalThresholdValueBI: (*core.BigInt)(calThresholdValue),
CollateralInUnderlying: sessionSnapshot.CollateralInUnderlying,
CalThresholdValueBI: (*core.BigInt)(calThresholdValue),
CollateralInUnderlying: sessionSnapshot.CollateralInUnderlying,
},
SessionId: sessionId,
}
Expand All @@ -396,7 +395,7 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
// set debt data fetched from dc
// if healthfactor on diff
if !CompareBalance(debt.CalTotalValueBI, (*core.BigInt)(data.TotalValue), cumIndexAndUToken) ||
!CompareBalance(debt.CalBorrowedWithInterestBI, (*core.BigInt)(data.BorrowedAmountPlusInterest), cumIndexAndUToken) ||
!CompareBalance(debt.CalDebtBI, (*core.BigInt)(data.Debt), cumIndexAndUToken) ||
core.ValueDifferSideOf10000(debt.CalHealthFactor, (*core.BigInt)(data.HealthFactor)) {
profile.DCData = &data
notMatched = true
Expand All @@ -413,7 +412,7 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
notMatched = true
}
}
eng.calAmountToPoolAndProfit(debt, session, cumIndexAndUToken)
eng.calAmountToPoolAndProfit(debt, session, cumIndexAndUToken, debtDetails)
eng.farmingCalc.addFarmingVal(debt, session, eng.lastCSS[session.ID], storeForCalc{inner: eng})
if notMatched {
profile.CumIndexAndUToken = cumIndexAndUToken
Expand Down Expand Up @@ -449,7 +448,7 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
// v2 - opened accounts
//
// the account might be having some underlying token balance so repayAMount = amountToPool - underlyingToken balance
func (eng *DebtEngine) calAmountToPoolAndProfit(debt *schemas.Debt, session *schemas.CreditSession, cumIndexAndUToken *ds.CumIndexAndUToken) {
func (eng *DebtEngine) calAmountToPoolAndProfit(debt *schemas.Debt, session *schemas.CreditSession, cumIndexAndUToken *ds.CumIndexAndUToken, debtDetails *calc.DebtDetails) {
var amountToPool, calRemainingFunds *big.Int
sessionSnapshot := eng.lastCSS[session.ID]
//
Expand All @@ -460,8 +459,7 @@ func (eng *DebtEngine) calAmountToPoolAndProfit(debt *schemas.Debt, session *sch
// amount to pool
amountToPool, calRemainingFunds, _, _ = calc.CalCloseAmount(eng.lastParameters[session.CreditManager],
session.Version, debt.CalTotalValueBI.Convert(), status,
debt.CalBorrowedWithInterestBI.Convert(),
sessionSnapshot.BorrowedAmountBI.Convert())
debtDetails)

// calculate profit
debt.AmountToPoolBI = (*core.BigInt)(amountToPool)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/Gearbox-protocol/third-eye
go 1.19

require (
github.com/Gearbox-protocol/sdk-go v0.0.0-20231021095619-31d94edf2acb
github.com/Gearbox-protocol/sdk-go v0.0.0-20231025152649-9d89bc0f48e4
github.com/ethereum/go-ethereum v1.10.17
github.com/go-playground/validator/v10 v10.4.1
github.com/google/go-cmp v0.5.8
Expand Down Expand Up @@ -73,4 +73,4 @@ require (

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

// replace github.com/Gearbox-protocol/sdk-go v0.0.0-20231018134112-d7c4678cb114 => ../sdk-go
// replace github.com/Gearbox-protocol/sdk-go v0.0.0-20231025145745-920ddd2e79d3 => ../sdk-go
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Gearbox-protocol/sdk-go v0.0.0-20231021095619-31d94edf2acb h1:p7yYw8hGoRpSL91E66VH21oIoVrNLicOhZ2KhoRL3JU=
github.com/Gearbox-protocol/sdk-go v0.0.0-20231021095619-31d94edf2acb/go.mod h1:GniLx/DU7tCT+QSlKt9REqUaF748X8rbDNR4vAd1m+Y=
github.com/Gearbox-protocol/sdk-go v0.0.0-20231025152649-9d89bc0f48e4 h1:gFdMmTGztRI5/5DM9KDwWM9e4kmewvGO/AIdNkm2X9s=
github.com/Gearbox-protocol/sdk-go v0.0.0-20231025152649-9d89bc0f48e4/go.mod h1:GniLx/DU7tCT+QSlKt9REqUaF748X8rbDNR4vAd1m+Y=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down
7 changes: 5 additions & 2 deletions models/credit_manager/cm_common/credit_session_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ func (mdl *CommonCMAdapter) closeSession(blockNum int64, session *schemas.Credit
amountToPool, _, _, _ := calc.CalCloseAmount(mdl.params,
session.Version, data.TotalValue.Convert(),
closeDetails.Status,
data.BorrowedAmountPlusInterest.Convert(),
data.BorrowedAmount.Convert(),
calc.NewDebtDetails(
data.Debt.Convert(),
data.AccruedInterest.Convert(),
data.BorrowedAmount.Convert(),
),
)
// pool repay
// check for avoiding db errors
Expand Down
4 changes: 2 additions & 2 deletions repository/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func (repo *Repository) GetValueInCurrency(blockNum int64, version core.VersionT
log.CheckFatal(err)
usdcAmount, err := poContract.Convert(opts, amount, common.HexToAddress(token), currencyAddr)
if err != nil {
log.Fatalf("%v %s %d %s %s", err, oracle, amount, token, currencyAddr)
log.Fatalf("%v %s %d %s %s at block %d", err, oracle, amount, token, currencyAddr, blockNum)
}
return usdcAmount
} else { // v2 and above
poContract, err := priceOraclev2.NewPriceOraclev2(common.HexToAddress(oracle), repo.client)
log.CheckFatal(err)
usdcAmount, err := poContract.Convert(opts, amount, common.HexToAddress(token), currencyAddr)
if err != nil {
log.Fatalf("%v %s %d %s %s", err, oracle, amount, token, currencyAddr)
log.Fatalf("%v %s %d %s %s at block %d", err, oracle, amount, token, currencyAddr, blockNum)
}
return usdcAmount
}
Expand Down
11 changes: 8 additions & 3 deletions services/trace_service/get_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/log"
"github.com/Gearbox-protocol/sdk-go/pkg"
"github.com/Gearbox-protocol/sdk-go/utils"
"github.com/ethereum/go-ethereum/common"
)

type TxLogger struct {
Expand Down Expand Up @@ -73,9 +75,12 @@ func (m TxLogger) fetchLogs(blockNum int64) map[string][]Log {

logStore[newTxHash] = append(logStore[newTxHash], formattedLog)
//
valid := len(txLog.Topics) > 0 && (txLog.Topics[0] == core.Topic("ExecuteOrder(address,address)") || // executeOrder
txLog.Topics[0] == core.Topic("Execute(address,address)") || // executeOrder
txLog.Topics[0] == core.Topic("CloseCreditAccount(address,address)")) // close v2
valid := len(txLog.Topics) > 0 && utils.Contains([]common.Hash{
core.Topic("ExecuteOrder(address,address)"), // executeOrder
core.Topic("CloseCreditAccount(address,address)"), // close v2
core.Topic("Execute(address,address)"), // execute on v3
core.Topic("CloseCreditAccount(address,address,address)"), //execute on v3
}, txLog.Topics[0])
op.storeCurTxHash = op.storeCurTxHash || valid
}
op.next(logStore, "")
Expand Down

0 comments on commit 2d83d07

Please sign in to comment.