Skip to content

Commit fbddacf

Browse files
committed
Error handling for wrong dc output
1 parent 46f2501 commit fbddacf

File tree

7 files changed

+67
-33
lines changed

7 files changed

+67
-33
lines changed

core/gear.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type RepositoryI interface {
4444
AddAllowedToken(atoken *AllowedToken)
4545
AddTokenObj(token *Token)
4646
GetToken(addr string) *Token
47-
ConvertToBalanceWithMask(balances []mainnet.DataTypesTokenBalance, mask *big.Int) *JsonBalance
47+
ConvertToBalanceWithMask(balances []mainnet.DataTypesTokenBalance, mask *big.Int) (*JsonBalance, error)
4848
// credit session funcs
4949
AddCreditSession(session *CreditSession, loadedFromDB bool)
5050
GetCreditSession(sessionId string) *CreditSession

core/token.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package core
22

33
import (
4-
"fmt"
54
"github.com/Gearbox-protocol/third-eye/artifacts/eRC20"
65
"github.com/Gearbox-protocol/third-eye/ethclient"
7-
"github.com/Gearbox-protocol/third-eye/log"
86
"github.com/Gearbox-protocol/third-eye/utils"
97
"github.com/ethereum/go-ethereum/accounts/abi/bind"
108
"github.com/ethereum/go-ethereum/common"
@@ -21,31 +19,31 @@ func (Token) TableName() string {
2119
return "tokens"
2220
}
2321

24-
func NewToken(addr string, client *ethclient.Client) *Token {
22+
func NewToken(addr string, client *ethclient.Client) (*Token, error) {
2523
token := &Token{
2624
Address: addr,
2725
client: client,
2826
}
29-
token.init()
30-
return token
27+
err := token.init()
28+
return token, err
3129
}
3230

33-
func (t *Token) init() {
31+
func (t *Token) init() error {
3432
contract, err := eRC20.NewERC20(common.HexToAddress(t.Address), t.client)
3533
if err != nil {
36-
log.Fatal(err, t.Address)
34+
return err
3735
}
3836
if symbol, err := contract.Symbol(&bind.CallOpts{}); err != nil {
39-
log.Error(fmt.Sprintf("%s %s", err, t.Address))
40-
panic("")
37+
return err
4138
} else {
4239
t.Symbol = symbol
4340
}
4441
if decimals, err := contract.Decimals(&bind.CallOpts{}); err != nil {
45-
log.Fatal(err, t.Address)
42+
return err
4643
} else {
4744
t.Decimals = int8(decimals)
4845
}
46+
return nil
4947
}
5048

5149
type AllowedToken struct {

debts/engine.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,23 +192,25 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, sessionId string, cm
192192
// profiling
193193
tokenDetails := map[string]core.TokenDetails{}
194194
for tokenAddr, balance := range *sessionSnapshot.Balances {
195-
if !balance.Linked {
196-
continue
197-
}
198195
decimal := eng.repo.GetToken(tokenAddr).Decimals
199196
price := eng.GetTokenLastPrice(tokenAddr)
200-
tokenValue := new(big.Int).Mul(price, balance.BI.Convert())
201-
tokenValueInDecimal := utils.GetInt64Decimal(tokenValue, decimal-underlyingtoken.Decimals)
202197
tokenLiquidityThreshold := eng.allowedTokensThreshold[cmAddr][tokenAddr]
203-
tokenThresholdValue := new(big.Int).Mul(tokenValueInDecimal, tokenLiquidityThreshold.Convert())
204-
calThresholdValue = new(big.Int).Add(calThresholdValue, tokenThresholdValue)
205-
calTotalValue = new(big.Int).Add(calTotalValue, tokenValueInDecimal)
206198
// profiling
207199
tokenDetails[tokenAddr] = core.TokenDetails{
208200
Price: price,
209201
Decimals: decimal,
210202
TokenLiqThreshold: tokenLiquidityThreshold,
211203
Symbol: eng.repo.GetToken(tokenAddr).Symbol}
204+
// token not linked continue
205+
if !balance.Linked {
206+
continue
207+
}
208+
tokenValue := new(big.Int).Mul(price, balance.BI.Convert())
209+
tokenValueInDecimal := utils.GetInt64Decimal(tokenValue, decimal-underlyingtoken.Decimals)
210+
tokenThresholdValue := new(big.Int).Mul(tokenValueInDecimal, tokenLiquidityThreshold.Convert())
211+
calThresholdValue = new(big.Int).Add(calThresholdValue, tokenThresholdValue)
212+
calTotalValue = new(big.Int).Add(calTotalValue, tokenValueInDecimal)
213+
212214
}
213215
// the value of credit account is in terms of underlying asset
214216
underlyingPrice := eng.GetTokenLastPrice(cumIndexAndUToken.Token)
@@ -242,7 +244,11 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, sessionId string, cm
242244
!core.CompareBalance(debt.CalBorrowedAmountPlusInterestBI, debt.BorrowedAmountPlusInterestBI, underlyingtoken) ||
243245
core.ValueDifferSideOf10000(debt.CalHealthFactor, debt.HealthFactor) {
244246
mask := eng.repo.GetMask(blockNum, cmAddr, accountAddr)
245-
profile.RPCBalances = eng.repo.ConvertToBalanceWithMask(data.Balances, mask)
247+
var err error
248+
profile.RPCBalances, err = eng.repo.ConvertToBalanceWithMask(data.Balances, mask)
249+
if err != nil {
250+
log.Fatalf("DC wrong token values block:%d dc:%s", blockNum, utils.ToJson(eng.repo.GetDCWrapper()))
251+
}
246252
notMatched = true
247253
}
248254
// even if data compressor matching is disabled check the values with values at which last credit snapshot was taken
@@ -300,7 +306,6 @@ func (eng *DebtEngine) requestPriceFeed(blockNum int64, feed, token string) {
300306
if err != nil {
301307
log.Fatal(err)
302308
}
303-
log.Info(token, roundData.Answer.String(), utils.GetFloat64Decimal(roundData.Answer, 18))
304309
eng.AddTokenLastPrice(&core.PriceFeed{
305310
BlockNumber: blockNum,
306311
Token: token,

models/credit_manager/credit_session_state.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ func (mdl *CreditManager) closeSession(sessionId string, blockNum int64, closeDe
8787
css.TotalValueBI = core.NewBigInt(session.TotalValueBI)
8888
css.TotalValue = utils.GetFloat64Decimal(data.TotalValue, mdl.GetUnderlyingDecimal())
8989
mask := mdl.Repo.GetMask(blockNum, mdl.GetAddress(), session.Account)
90-
css.Balances = mdl.Repo.ConvertToBalanceWithMask(data.Balances, mask)
90+
var err error
91+
css.Balances, err = mdl.Repo.ConvertToBalanceWithMask(data.Balances, mask)
92+
if err != nil {
93+
log.Fatalf("DC wrong token values block:%d dc:%s", blockNum, utils.ToJson(mdl.Repo.GetDCWrapper()))
94+
}
9195
css.BorrowedAmountBI = core.NewBigInt(session.BorrowedAmount)
9296
css.BorrowedAmount = utils.GetFloat64Decimal(data.BorrowedAmount, mdl.GetUnderlyingDecimal())
9397
css.СumulativeIndexAtOpen = core.NewBigInt((*core.BigInt)(data.CumulativeIndexAtOpen))
@@ -115,7 +119,11 @@ func (mdl *CreditManager) updateSession(sessionId string, blockNum int64) {
115119
css.TotalValueBI = core.NewBigInt(session.TotalValueBI)
116120
css.TotalValue = utils.GetFloat64Decimal(data.TotalValue, mdl.GetUnderlyingDecimal())
117121
mask := mdl.Repo.GetMask(blockNum, mdl.GetAddress(), session.Account)
118-
css.Balances = mdl.Repo.ConvertToBalanceWithMask(data.Balances, mask)
122+
var err error
123+
css.Balances, err = mdl.Repo.ConvertToBalanceWithMask(data.Balances, mask)
124+
if err != nil {
125+
log.Fatalf("DC wrong token values block:%d dc:%s", blockNum, utils.ToJson(mdl.Repo.GetDCWrapper()))
126+
}
119127
css.BorrowedAmountBI = core.NewBigInt(session.BorrowedAmount)
120128
css.BorrowedAmount = utils.GetFloat64Decimal(data.BorrowedAmount, mdl.GetUnderlyingDecimal())
121129
css.СumulativeIndexAtOpen = core.NewBigInt((*core.BigInt)(data.CumulativeIndexAtOpen))

repository/credit_session_snapshot.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@ func (repo *Repository) AddCreditSessionSnapshot(css *core.CreditSessionSnapshot
1515
repo.blocks[css.BlockNum].AddCreditSessionSnapshot(css)
1616
}
1717

18-
func (repo *Repository) ConvertToBalanceWithMask(balances []mainnet.DataTypesTokenBalance, mask *big.Int) *core.JsonBalance {
18+
func (repo *Repository) ConvertToBalanceWithMask(balances []mainnet.DataTypesTokenBalance, mask *big.Int) (*core.JsonBalance, error) {
1919
jsonBalance := core.JsonBalance{}
2020
maskInBits := fmt.Sprintf("%b", mask)
21+
maskLen := len(maskInBits)
2122
for i, token := range balances {
2223
tokenAddr := token.Token.Hex()
2324
if token.Balance.Sign() != 0 {
25+
tokenObj, err := repo.getTokenWithError(tokenAddr)
26+
if err != nil {
27+
return nil, err
28+
}
2429
jsonBalance[tokenAddr] = &core.BalanceType{
2530
BI: (*core.BigInt)(token.Balance),
26-
F: utils.GetFloat64Decimal(token.Balance, repo.GetToken(tokenAddr).Decimals),
27-
Linked: maskInBits[i] == '1',
31+
F: utils.GetFloat64Decimal(token.Balance, tokenObj.Decimals),
32+
Linked: maskInBits[maskLen-i-1] == '1',
2833
}
2934
}
3035
}
31-
return &jsonBalance
36+
return &jsonBalance, nil
3237
}

repository/token.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,38 @@ import (
77

88
// For token with symbol/decimals
99
func (repo *Repository) AddToken(addr string) *core.Token {
10+
token, err := repo.addToken(addr)
11+
if err != nil {
12+
log.Fatal("Adding token failed for", token)
13+
}
14+
return token
15+
}
16+
17+
func (repo *Repository) addToken(addr string) (*core.Token, error) {
1018
repo.mu.Lock()
1119
defer repo.mu.Unlock()
1220
if repo.tokens[addr] == nil {
13-
repo.tokens[addr] = core.NewToken(addr, repo.client)
21+
var err error
22+
repo.tokens[addr], err = core.NewToken(addr, repo.client)
23+
if err != nil {
24+
return nil, err
25+
}
1426
}
15-
return repo.tokens[addr]
27+
return repo.tokens[addr], nil
1628
}
1729

1830
func (repo *Repository) GetToken(addr string) *core.Token {
31+
token, err := repo.getTokenWithError(addr)
32+
log.CheckFatal(err)
33+
return token
34+
}
35+
36+
func (repo *Repository) getTokenWithError(addr string) (*core.Token, error) {
1937
token := repo.tokens[addr]
2038
if token == nil {
21-
return repo.AddToken(addr)
39+
return repo.addToken(addr)
2240
}
23-
return token
41+
return token, nil
2442
}
2543

2644
func (repo *Repository) loadToken() {

utils/index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ func IntDiffMoreThanFraction(oldValue, newValue, diff int64) bool {
157157
return absInt64((newValue-oldValue)/oldValue) > diff
158158
}
159159

160-
func ToJson(obj interface{}) {
160+
func ToJson(obj interface{}) string {
161161
str, err := json.Marshal(obj)
162162
log.CheckFatal(err)
163-
log.Info(string(str))
163+
return string(str)
164164
}

0 commit comments

Comments
 (0)