Skip to content

Commit

Permalink
feat: borrow rate -> base borrow rate
Browse files Browse the repository at this point in the history
  • Loading branch information
5lliot committed Dec 4, 2023
1 parent 967329b commit c799463
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 32 deletions.
4 changes: 2 additions & 2 deletions debts/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (eng *DebtEngine) updateLocalState(blockNum int64, block *schemas.Block) (p
BlockNum: ps.BlockNum,
CumulativeIndexRAY: ps.CumulativeIndexRAY,
AvailableLiquidityBI: ps.AvailableLiquidityBI,
BorrowAPYBI: ps.BorrowAPYBI,
BaseBorrowAPYBI: ps.BaseBorrowAPYBI,
Timestamp: block.Timestamp,
})
}
Expand Down Expand Up @@ -265,7 +265,7 @@ func (eng *DebtEngine) GetCumulativeIndexAndDecimalForCMs(blockNum int64, ts uin
var cumIndexNormalized *big.Int
if poolInterestData != nil {
tsDiff := new(big.Int).SetInt64(int64(ts - poolInterestData.Timestamp))
newInterest := new(big.Int).Mul(poolInterestData.BorrowAPYBI.Convert(), tsDiff)
newInterest := new(big.Int).Mul(poolInterestData.BaseBorrowAPYBI.Convert(), tsDiff)
newInterestPerSec := new(big.Int).Quo(newInterest, big.NewInt(3600*365*24))
predicate := new(big.Int).Add(newInterestPerSec, utils.GetExpInt(27))
cumIndex := new(big.Int).Mul(poolInterestData.CumulativeIndexRAY.Convert(), predicate)
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-20231201034421-ac612148cb40
github.com/Gearbox-protocol/sdk-go v0.0.0-20231204101247-f288f66e61ff
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-20231115094605-e0a52b57f9ff => ../sdk-go
// replace github.com/Gearbox-protocol/sdk-go v0.0.0-20231204091904-7e5cefe36d05 => ../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-20231201034421-ac612148cb40 h1:5Vc5waZ50VxpvacepRTxFS0NgBjH3gyuqmB0tToTEms=
github.com/Gearbox-protocol/sdk-go v0.0.0-20231201034421-ac612148cb40/go.mod h1:GniLx/DU7tCT+QSlKt9REqUaF748X8rbDNR4vAd1m+Y=
github.com/Gearbox-protocol/sdk-go v0.0.0-20231204101247-f288f66e61ff h1:jhKfVC94ejJVrLEFOEu6BfOiYA744PimXIrEWmItRIY=
github.com/Gearbox-protocol/sdk-go v0.0.0-20231204101247-f288f66e61ff/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
8 changes: 6 additions & 2 deletions jsonnet/account_lifecycle/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
sessionId: '#Account_1_3_3',
totalValue: 6000,
totalValueBI: '6000000000',
extraQuotaRAYBI: '0',
},
],
daoOperations: [
Expand Down Expand Up @@ -201,8 +202,8 @@
availableLiquidity: 1000,
availableLiquidityBI: '1000000000',
blockNum: 3,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down Expand Up @@ -321,6 +322,7 @@
sessionId: '#Account_1_3_3',
totalValue: 8500,
totalValueBI: '8500000000',
extraQuotaRAYBI: '0',
},
],
daoOperations: [
Expand Down Expand Up @@ -427,6 +429,7 @@
sessionId: '#Account_1_3_3',
totalValue: 8500,
totalValueBI: '8500000000',
extraQuotaRAYBI: '0',
},
],
timestamp: 432000,
Expand Down Expand Up @@ -511,6 +514,7 @@
sessionId: '#Account_1_3_3',
totalValue: 9500,
totalValueBI: '9500000000',
extraQuotaRAYBI: '0',
},
],
daoOperations: [
Expand Down
12 changes: 8 additions & 4 deletions jsonnet/account_lifecycle_v2/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
sessionId: '#Account_1_4_0',
totalValue: 6000,
totalValueBI: '6000000000',
extraQuotaRAYBI: '0',
},
],
poolLedgers: [
Expand Down Expand Up @@ -149,8 +150,8 @@
availableLiquidity: 5000,
availableLiquidityBI: '5000000000',
blockNum: 4,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down Expand Up @@ -291,6 +292,7 @@
sessionId: '#Account_1_4_0',
totalValue: 8000,
totalValueBI: '8000000000',
extraQuotaRAYBI: '0',
},
],
daoOperations: [
Expand Down Expand Up @@ -337,8 +339,8 @@
availableLiquidity: 5500,
availableLiquidityBI: '5500000000',
blockNum: 5,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down Expand Up @@ -498,6 +500,7 @@
sessionId: '#Account_1_4_0',
totalValue: 8000,
totalValueBI: '8000000000',
extraQuotaRAYBI: '0',
},
],
timestamp: 518400,
Expand Down Expand Up @@ -582,6 +585,7 @@
sessionId: '#Account_1_4_0',
totalValue: 9000,
totalValueBI: '9000000000',
extraQuotaRAYBI: '0',
},
],
daoOperations: [
Expand Down
5 changes: 3 additions & 2 deletions jsonnet/close_credit_account/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
sessionId: '#Account_1_3_3',
totalValue: 9500,
totalValueBI: '9500000000',
extraQuotaRAYBI: '0',
quotaFees: '0',
},
],
Expand Down Expand Up @@ -110,8 +111,8 @@
availableLiquidity: 1000,
availableLiquidityBI: '1000000000',
blockNum: 8,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down
5 changes: 3 additions & 2 deletions jsonnet/close_credit_account_v2/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
sessionId: '#Account_1_4_0',
totalValue: 9000,
totalValueBI: '9000000000',
extraQuotaRAYBI: '0',
quotaFees: '0',
},
],
Expand Down Expand Up @@ -144,8 +145,8 @@
availableLiquidity: 10000,
availableLiquidityBI: '10000000000',
blockNum: 9,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down
5 changes: 3 additions & 2 deletions jsonnet/liquidate_credit_account/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
sessionId: '#Account_1_3_3',
totalValue: 5750,
totalValueBI: '5750000000',
extraQuotaRAYBI: '0',
quotaFees: '0',
},
],
Expand Down Expand Up @@ -121,8 +122,8 @@
availableLiquidity: 6000,
availableLiquidityBI: '6000000000',
blockNum: 8,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down
11 changes: 7 additions & 4 deletions jsonnet/multiple_facade_interaction_single_tx/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
sessionId: '#Account_1_4_0',
totalValue: 6000,
totalValueBI: '6000000000',
extraQuotaRAYBI: '0',
},
],
poolLedgers: [
Expand All @@ -153,8 +154,8 @@
availableLiquidity: 5000,
availableLiquidityBI: '5000000000',
blockNum: 4,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down Expand Up @@ -198,6 +199,7 @@
sessionId: '#Account_1_4_0',
totalValue: 7000,
totalValueBI: '7000000000',
extraQuotaRAYBI: '0',
},
],
timestamp: 432000,
Expand Down Expand Up @@ -372,6 +374,7 @@
sessionId: '#Account_2_6_4',
totalValue: 6000,
totalValueBI: '6000000000',
extraQuotaRAYBI: '0',
},
],
poolLedgers: [
Expand Down Expand Up @@ -401,8 +404,8 @@
availableLiquidity: 5000,
availableLiquidityBI: '5000000000',
blockNum: 6,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down
5 changes: 3 additions & 2 deletions jsonnet/open_credit_account/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
sessionId: '#Account_1_3_0',
totalValue: 5000,
totalValueBI: '5000000000',
extraQuotaRAYBI: '0',
},
],
daoOperations: [
Expand Down Expand Up @@ -169,8 +170,8 @@
availableLiquidity: 1000,
availableLiquidityBI: '1000000000',
blockNum: 3,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down
4 changes: 2 additions & 2 deletions jsonnet/repay_credit_account/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
availableLiquidity: 6000,
availableLiquidityBI: '6000000000',
blockNum: 7,
borrowAPY: 0,
borrowAPYBI: '0',
baseBorrowAPY: 0,
baseBorrowAPYBI: '0',
cumulativeIndexRAY: '1000000000000000000000000000',
depositAPY: 0,
depositAPYBI: '0',
Expand Down
11 changes: 11 additions & 0 deletions migrations/000033_base_borrow_rate.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
alter table pool_stats add base_borrow_apy double precision, add base_borrow_apy_bi varchar(80);
update pool_stats set base_borrow_apy=borrow_apy, base_borrow_apy_bi=borrow_apy_bi;
alter table pool_stats drop column borrow_apy , drop column borrow_apy_bi;

--
alter table pools add base_borrow_apy_bi double precision;
update pools set base_borrow_apy_bi=borrow_apy_bi;
alter table pools drop column borrow_apy_bi;

--
alter table credit_session_snapshots add extra_quota_ray_bi varchar(80);
1 change: 1 addition & 0 deletions models/credit_manager/cm_common/credit_session_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func (mdl *CommonCMAdapter) createCSSnapshot(blockNum int64, session *schemas.Cr
//
// set balances
css.Balances = mdl.addFloatValue(session.Account, blockNum, data.Balances)
css.ExtraQuotaRAYBI = (*core.BigInt)(schemas.QuotaBorrowRate(*css.Balances, css.TotalValueBI))
// for close credit account operation on gearbox v2
// https://github.com/Gearbox-protocol/contracts-v2/blob/main/contracts/credit/CreditFacade.sol#L235
// there is a skipTokenMask which can be used to skip certain tokens from getting transferred to borrower
Expand Down
6 changes: 3 additions & 3 deletions models/pool/pool_v2/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (p *Poolv2) createSnapshot(blockNum int64, state dc.PoolCallData) {
token := p.Repo.GetToken(p.State.UnderlyingToken)
p.State.IsWETH = dc.IsWETH(p.Client, state.Underlying)
// TODO: change borrow apy
p.State.BorrowAPYBI = (*core.BigInt)(state.BaseInterestRate)
p.State.BaseBorrowAPYBI = (*core.BigInt)(state.BaseInterestRate)
p.State.DepositAPYBI = (*core.BigInt)(state.SupplyRate)
p.Repo.AddPoolStat(&schemas.PoolStat{
BlockNum: blockNum,
Expand All @@ -92,9 +92,9 @@ func (p *Poolv2) createSnapshot(blockNum int64, state dc.PoolCallData) {
// for 4% is depositAPY is 4 that is why apy is divided by decimal 25 not 27
DepositAPY: utils.GetFloat64Decimal(state.SupplyRate, 25),

BorrowAPYBI: (*core.BigInt)(state.BaseInterestRate),
BaseBorrowAPYBI: (*core.BigInt)(state.BaseInterestRate),
// TODO change to base borrow rate
BorrowAPY: utils.GetFloat64Decimal(state.BaseInterestRate, 25),
BaseBorrowAPY: utils.GetFloat64Decimal(state.BaseInterestRate, 25),

// dieselrate is how much each diesel rate is worth in terms of underlying token
// that's why it is divide by 27 not 25. it is not a percentage.
Expand Down
6 changes: 3 additions & 3 deletions models/pool/pool_v3/state_snapshot_and_gateway_fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (mdl *Poolv3) onBlockChangeInternally(inputB int64) {
func (p *Poolv3) createSnapshot(blockNum int64, state dc.PoolCallData) {
token := p.Repo.GetToken(p.State.UnderlyingToken)
p.State.IsWETH = dc.IsWETH(p.Client, state.Underlying)
p.State.BorrowAPYBI = (*core.BigInt)(state.BaseInterestRate)
p.State.BaseBorrowAPYBI = (*core.BigInt)(state.BaseInterestRate)
p.State.DepositAPYBI = (*core.BigInt)(state.SupplyRate)
p.Repo.AddPoolStat(&schemas.PoolStat{
BlockNum: blockNum,
Expand All @@ -84,8 +84,8 @@ func (p *Poolv3) createSnapshot(blockNum int64, state dc.PoolCallData) {
// for 4% is depositAPY is 4 that is why apy is divided by decimal 25 not 27
DepositAPY: utils.GetFloat64Decimal(state.SupplyRate, 25),

BorrowAPYBI: (*core.BigInt)(state.BaseInterestRate),
BorrowAPY: utils.GetFloat64Decimal(state.BaseInterestRate, 25),
BaseBorrowAPYBI: (*core.BigInt)(state.BaseInterestRate),
BaseBorrowAPY: utils.GetFloat64Decimal(state.BaseInterestRate, 25),

// dieselrate is how much each diesel rate is worth in terms of underlying token
// that's why it is divide by 27 not 25. it is not a percentage.
Expand Down

0 comments on commit c799463

Please sign in to comment.