Skip to content

Commit 5b65a99

Browse files
authored
Merge pull request #16 from Gearbox-protocol/debtConfigurationEnvs
[WIP]Debt configuration env and use as separate module
2 parents ea7a65b + 7b74d42 commit 5b65a99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1071
-525
lines changed

cmd/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"github.com/Gearbox-protocol/third-eye/config"
1212
"github.com/Gearbox-protocol/third-eye/core"
13+
"github.com/Gearbox-protocol/third-eye/debts"
1314
"github.com/Gearbox-protocol/third-eye/engine"
1415
"github.com/Gearbox-protocol/third-eye/ethclient"
1516
"github.com/Gearbox-protocol/third-eye/log"
@@ -47,6 +48,7 @@ func main() {
4748
services.Module,
4849
engine.Module,
4950
fx.NopLogger,
51+
debts.Module,
5052
fx.Invoke(StartServer),
5153
)
5254
startCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)

config/config.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ type Config struct {
2626
ChainId uint `validate:"required"`
2727
NetworkId string `env:"REACT_APP_CHAIN_ID" validate:"required"`
2828

29-
Port string `env:"PORT" default:"8080" validate:"required"`
30-
AMPQUrl string `env:"CLOUDAMQP_URL" validate:"required"`
31-
AMPQEnable string `env:"AMPQ_ENABLE" validate:"required"`
32-
DebtDCMatching string `env:"DEBT_DC_MATCHING" validate:"required"`
33-
DebtCheck bool
34-
35-
MiningAddr string `env:"MINING_ADDR"`
29+
Port string `env:"PORT" default:"8080" validate:"required"`
30+
AMPQUrl string `env:"CLOUDAMQP_URL" validate:"required"`
31+
AMPQEnable string `env:"AMPQ_ENABLE" validate:"required"`
32+
DebtDCMatchingStr string `env:"DEBT_DC_MATCHING" validate:"required"`
33+
DebtDCMatching bool
34+
DisableDebtEngineStr string `env:"DISABLE_DEBT_ENGINE" validate:"required"`
35+
DisableDebtEngine bool
36+
ThrottleDebtCalStr string `env:"THROTTLE_DEBT_CAL" validate:"required"`
37+
ThrottleDebtCal bool
38+
MiningAddr string `env:"MINING_ADDR"`
3639
}

config/helper.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,22 @@ func NewConfig() *Config {
5858
log.Fatal("Cant get chain id")
5959
}
6060

61-
if config.DebtDCMatching == "1" {
62-
config.DebtCheck = true
61+
if config.DebtDCMatchingStr == "1" {
62+
config.DebtDCMatching = true
6363
} else {
64-
config.DebtCheck = false
64+
config.DebtDCMatching = false
65+
}
66+
67+
if config.DisableDebtEngineStr == "1" {
68+
config.DisableDebtEngine = true
69+
} else {
70+
config.DisableDebtEngine = false
71+
}
72+
73+
if config.ThrottleDebtCalStr == "1" {
74+
config.ThrottleDebtCal = true
75+
} else {
76+
config.ThrottleDebtCal = false
6577
}
6678

6779
config.ChainId = uint(chainId)

core/account_operation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type AccountOperation struct {
66
// ordering data
77
TxHash string `gorm:"column:tx_hash"`
88
BlockNumber int64 `gorm:"column:block_num"`
9-
LogId uint `gorm:"column:log_id`
9+
LogId uint `gorm:"column:log_id"`
1010
// owner/account data
1111
Borrower string
1212
SessionId string `gorm:"column:session_id"`

core/bigint.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"database/sql/driver"
1111
"errors"
1212
"fmt"
13-
"github.com/Gearbox-protocol/third-eye/log"
1413
"github.com/ethereum/go-ethereum/common"
1514
"github.com/ethereum/go-ethereum/crypto"
1615
"math/big"
@@ -40,13 +39,25 @@ func NewBigInt(bi *BigInt) *BigInt {
4039
if bi == nil {
4140
return (*BigInt)(big.NewInt(0))
4241
}
43-
obj, ok := new(big.Int).SetString(bi.String(), 10)
44-
if !ok {
45-
log.Fatal("Failed parsing int", bi)
46-
}
42+
obj := new(big.Int).Mul(bi.Convert(), big.NewInt(1))
4743
return (*BigInt)(obj)
4844
}
4945

46+
func DiffMoreThanFraction(oldValue, newValue *BigInt, diff *big.Float) bool {
47+
newFloat := new(big.Float).SetInt(newValue.Convert())
48+
oldFloat := new(big.Float).SetInt(oldValue.Convert())
49+
fractionalChange := new(big.Float).Quo(
50+
new(big.Float).Sub(newFloat, oldFloat),
51+
oldFloat)
52+
return new(big.Float).Abs(fractionalChange).Cmp(diff) > 1
53+
}
54+
func ValueDifferSideOf10000(a, b *BigInt) bool {
55+
return (IntGreaterThanEqualTo(a, 10000) != IntGreaterThanEqualTo(b, 10000))
56+
}
57+
func IntGreaterThanEqualTo(value *BigInt, cmp int64) bool {
58+
return value.Convert().Cmp(big.NewInt(cmp)) >= 0
59+
}
60+
5061
func AddCoreAndInt(a *BigInt, b *big.Int) *BigInt {
5162
if a != nil {
5263
return (*BigInt)(new(big.Int).Add(a.Convert(), b))

core/constant.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
const LogFilterLenError = "Log response size exceeded. You can make eth_getLogs requests with up to a 2K block range and no limit on the response size, or you can request any block range with a cap of 10K logs in the response."
88
const QueryMoreThan10000Error = "query returned more than 10000 results"
9+
const NoOfBlocksPerMin int64 = 5
10+
const NoOfBlocksPerHr int64 = NoOfBlocksPerMin * 60
911

1012
var WETHPrice *big.Int
1113

core/credit_session.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type (
2727
Profit *BigInt `json:"profit"`
2828
ProfitPercentage float64 `gorm:"column:profit_percent" json:"profitPercentage"`
2929
TotalValueBI *BigInt `gorm:"column:total_value" json:"totalValue"`
30-
HealthFactor int64 `gorm:"column:health_factor" json:"healthFactor"`
30+
HealthFactor *BigInt `gorm:"column:health_factor" json:"healthFactor"`
3131
Score float64 `json:"score"`
3232
IsDirty bool `gorm:"-"`
3333
}
@@ -41,7 +41,7 @@ type (
4141
UnderlyingToken string
4242
BorrowedAmountPlusInterest *big.Int
4343
TotalValue *big.Int
44-
HealthFactor int64
44+
HealthFactor *big.Int
4545
BorrowRate *big.Int
4646
}
4747

@@ -64,6 +64,6 @@ type (
6464
Balances *JsonBalance `gorm:"column:balances"`
6565
Borrower string `gorm:"column:borrower"`
6666
СumulativeIndexAtOpen *BigInt `gorm:"column:cumulative_index"`
67-
HealthFactor int64 `gorm:"column:health_factor"`
67+
HealthFactor *BigInt `gorm:"column:health_factor"`
6868
}
6969
)

core/data_compressor_wrapper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"github.com/ethereum/go-ethereum/accounts/abi/bind"
1111
"github.com/ethereum/go-ethereum/common"
1212
"sort"
13+
"sync"
1314
)
1415

1516
type DataCompressorWrapper struct {
17+
mu *sync.Mutex
1618
// blockNumbers of dc in asc order
1719
dcBlockNum []int64
1820
blockNumToName map[int64]string
@@ -27,6 +29,7 @@ var MAINNET = "MAINNET"
2729

2830
func NewDataCompressorWrapper(client *ethclient.Client) *DataCompressorWrapper {
2931
return &DataCompressorWrapper{
32+
mu: &sync.Mutex{},
3033
blockNumToName: make(map[int64]string),
3134
nameToAddr: make(map[string]string),
3235
client: client,
@@ -69,6 +72,8 @@ func (dcw *DataCompressorWrapper) AddDataCompressor(blockNum int64, addr string)
6972
}
7073

7174
func (dcw *DataCompressorWrapper) GetCreditAccountDataExtended(opts *bind.CallOpts, creditManager common.Address, borrower common.Address) (mainnet.DataTypesCreditAccountDataExtended, error) {
75+
dcw.mu.Lock()
76+
defer dcw.mu.Unlock()
7277
if opts == nil || opts.BlockNumber == nil {
7378
panic("opts or blockNumber is nil")
7479
}
@@ -111,6 +116,8 @@ func (dcw *DataCompressorWrapper) GetCreditAccountDataExtended(opts *bind.CallOp
111116
}
112117

113118
func (dcw *DataCompressorWrapper) GetCreditManagerData(opts *bind.CallOpts, _creditManager common.Address, borrower common.Address) (mainnet.DataTypesCreditManagerData, error) {
119+
dcw.mu.Lock()
120+
defer dcw.mu.Unlock()
114121
if opts == nil || opts.BlockNumber == nil {
115122
panic("opts or blockNumber is nil")
116123
}
@@ -148,6 +155,8 @@ func (dcw *DataCompressorWrapper) GetCreditManagerData(opts *bind.CallOpts, _cre
148155
}
149156

150157
func (dcw *DataCompressorWrapper) GetPoolData(opts *bind.CallOpts, _pool common.Address) (mainnet.DataTypesPoolData, error) {
158+
dcw.mu.Lock()
159+
defer dcw.mu.Unlock()
151160
if opts == nil || opts.BlockNumber == nil {
152161
panic("opts or blockNumber is nil")
153162
}

core/debt.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
)
88

99
type Debt struct {
10-
Id int64 `gorm:"primaryKey;column:id;autoincrement:true"`
11-
BlockNumber int64 `gorm:"column:block_num"`
12-
SessionId string `gorm:"column:session_id"`
13-
HealthFactor int64 `gorm:"column:health_factor"`
14-
TotalValueBI string `gorm:"column:total_value"`
15-
BorrowedAmountPlusInterestBI string `gorm:"column:borrowed_amt_with_interest"`
16-
CalHealthFactor int64 `gorm:"column:cal_health_factor"`
17-
CalTotalValue string `gorm:"column:cal_total_value"`
18-
CalBorrowedAmountPlusInterestBI string `gorm:"column:cal_borrowed_amt_with_interest"`
19-
CalThresholdValueBI string `gorm:"column:cal_threshold_value"`
10+
Id int64 `gorm:"primaryKey;column:id"`
11+
BlockNumber int64 `gorm:"column:block_num"`
12+
SessionId string `gorm:"column:session_id"`
13+
HealthFactor *BigInt `gorm:"column:health_factor"`
14+
TotalValueBI *BigInt `gorm:"column:total_value"`
15+
BorrowedAmountPlusInterestBI *BigInt `gorm:"column:borrowed_amt_with_interest"`
16+
CalHealthFactor *BigInt `gorm:"column:cal_health_factor"`
17+
CalTotalValueBI *BigInt `gorm:"column:cal_total_value"`
18+
CalBorrowedAmountPlusInterestBI *BigInt `gorm:"column:cal_borrowed_amt_with_interest"`
19+
CalThresholdValueBI *BigInt `gorm:"column:cal_threshold_value"`
2020
}
2121

2222
type DebtSync struct {
@@ -36,7 +36,7 @@ type TokenDetails struct {
3636
type DebtProfile struct {
3737
*Debt `json:"debt"`
3838
*CreditSessionSnapshot `json:"css"`
39-
RPCBalances JsonBalance `json:"rpcBalances"`
39+
RPCBalances *JsonBalance `json:"rpcBalances"`
4040
Tokens map[string]TokenDetails `json:"tokens"`
4141
UnderlyingDecimals int8 `json:"underlyingDecimals"`
4242
*CumIndexAndUToken `json:"poolDetails"`
@@ -62,3 +62,15 @@ type ProfileTable struct {
6262
func (ProfileTable) TableName() string {
6363
return "profiles"
6464
}
65+
66+
type DebtEngineI interface {
67+
Clear()
68+
Init()
69+
CalculateDebtAndClear()
70+
}
71+
72+
type LiquidableAccount struct {
73+
SessionId string `gorm:"primaryKey;column:session_id"`
74+
BlockNum int64 `gorm:"block_num"`
75+
Updated bool `gorm:"-"`
76+
}

core/eth_node.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func (lf *Node) GetLogs(fromBlock, toBlock int64, addr string) ([]types.Log, err
2828
if err.Error() == QueryMoreThan10000Error ||
2929
strings.Contains(err.Error(), LogFilterLenError) {
3030
middle := (fromBlock + toBlock) / 2
31-
log.Info(fromBlock, middle, toBlock)
3231
bottomHalfLogs, err := lf.GetLogs(fromBlock, middle-1, addr)
3332
if err != nil {
3433
return []types.Log{}, err

0 commit comments

Comments
 (0)