Skip to content

Commit

Permalink
fix: debt comparsion funcs for totalValue
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Jan 23, 2024
1 parent f2dc543 commit 8dbe6ed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions debts/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,15 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
utils.ToJson(data)
// set debt data fetched from dc
// if healthfactor on diff
if !CompareBalance(debt.CalTotalValueBI, (*core.BigInt)(data.TotalValue), cumIndexAndUToken) ||
!CompareBalance(debt.CalDebtBI, (*core.BigInt)(data.Debt), cumIndexAndUToken) ||
if IsChangeMoreThanFraction(debt.CalTotalValueBI, (*core.BigInt)(data.TotalValue), big.NewFloat(0.0001)) ||
IsChangeMoreThanFraction(debt.CalDebtBI, (*core.BigInt)(data.Debt), big.NewFloat(0.0001)) ||
core.ValueDifferSideOf10000(debt.CalHealthFactor, (*core.BigInt)(data.HealthFactor)) {
profile.DCData = &data
notMatched = true
}
// even if data compressor matching is disabled check the calc values with session data at block where last credit snapshot was taken
} else if sessionSnapshot.BlockNum == blockNum {
if !CompareBalance(debt.CalTotalValueBI, sessionSnapshot.TotalValueBI, cumIndexAndUToken) ||
if IsChangeMoreThanFraction(debt.CalTotalValueBI, sessionSnapshot.TotalValueBI, big.NewFloat(0.0001)) ||
// hf value calculated are on different side of 1
core.ValueDifferSideOf10000(debt.CalHealthFactor, sessionSnapshot.HealthFactor) ||
// if healhFactor diff by 4 %
Expand Down
11 changes: 8 additions & 3 deletions debts/index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package debts

import (
"math/big"

"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/core/schemas"
"github.com/Gearbox-protocol/sdk-go/core/schemas/schemas_v3"
Expand Down Expand Up @@ -203,7 +205,10 @@ func (eng *DebtEngine) GetDebts() core.Json {
return obj
}

func CompareBalance(a, b *core.BigInt, token *ds.CumIndexAndUToken) bool {
precision := utils.GetPrecision(token.Symbol)
return utils.AlmostSameBigInt(a.Convert(), b.Convert(), token.Decimals-precision)
func IsChangeMoreThanFraction(a, b *core.BigInt, diff *big.Float) bool {
if a.Cmp(b) > 0 {
return IsChangeMoreThanFraction(b, a, diff)
}
// b < a
return utils.DiffMoreThanFraction(a.Convert(), b.Convert(), diff)
}
4 changes: 4 additions & 0 deletions migrations/000038_closed_sessions.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create table closed_trading_sessions (
session_id varchar(80),
data jsonb,
PRIMARY KEY (session_id));

0 comments on commit 8dbe6ed

Please sign in to comment.