update naming conventions (#745)

This commit is contained in:
Denali Marsh 2020-12-21 18:07:02 +01:00 committed by GitHub
parent 06fd215de1
commit f140d7aff6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 63 deletions

View File

@ -13,11 +13,11 @@ import (
func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins) error {
// Set any new denoms' global borrow index to 1.0
for _, coin := range coins {
_, foundBorrowIndex := k.GetBorrowIndex(ctx, coin.Denom)
if !foundBorrowIndex {
_, foundMM := k.GetMoneyMarket(ctx, coin.Denom)
if foundMM {
k.SetBorrowIndex(ctx, coin.Denom, sdk.OneDec())
_, foundInterestFactor := k.GetInterestFactor(ctx, coin.Denom)
if !foundInterestFactor {
_, foundMm := k.GetMoneyMarket(ctx, coin.Denom)
if foundMm {
k.SetInterestFactor(ctx, coin.Denom, sdk.OneDec())
}
}
}
@ -60,13 +60,13 @@ func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins
// On user's first borrow, build borrow index list containing denoms and current global borrow index value
// We use a list of BorrowIndexItem here because Amino doesn't support marshaling maps.
if !found {
var borrowIndexes types.BorrowIndexes
var interestFactors types.InterestFactors
for _, coin := range coins {
borrowIndexValue, _ := k.GetBorrowIndex(ctx, coin.Denom)
borrowIndex := types.NewBorrowIndexItem(coin.Denom, borrowIndexValue)
borrowIndexes = append(borrowIndexes, borrowIndex)
interestFactorValue, _ := k.GetInterestFactor(ctx, coin.Denom)
interestFactor := types.NewInterestFactor(coin.Denom, interestFactorValue)
interestFactors = append(interestFactors, interestFactor)
}
borrow := types.NewBorrow(borrower, sdk.Coins{}, borrowIndexes)
borrow := types.NewBorrow(borrower, sdk.Coins{}, interestFactors)
k.SetBorrow(ctx, borrow)
}
@ -112,17 +112,17 @@ func (k Keeper) SyncOutstandingInterest(ctx sdk.Context, addr sdk.AccAddress) {
}
}
borrowIndexValue, _ := k.GetBorrowIndex(ctx, coin.Denom)
interestFactorValue, _ := k.GetInterestFactor(ctx, coin.Denom)
if foundAtIndex == -1 { // First time user has borrowed this denom
borrow.Index = append(borrow.Index, types.NewBorrowIndexItem(coin.Denom, borrowIndexValue))
borrow.Index = append(borrow.Index, types.NewInterestFactor(coin.Denom, interestFactorValue))
} else { // User has an existing borrow index for this denom
// Calculate interest owed by user since asset's last borrow index update
storedAmount := sdk.NewDecFromInt(borrow.Amount.AmountOf(coin.Denom))
userLastBorrowIndex := borrow.Index[foundAtIndex].Value
interest := (storedAmount.Quo(userLastBorrowIndex).Mul(borrowIndexValue)).Sub(storedAmount)
userLastInterestFactor := borrow.Index[foundAtIndex].Value
interest := (storedAmount.Quo(userLastInterestFactor).Mul(interestFactorValue)).Sub(storedAmount)
totalNewInterest = totalNewInterest.Add(sdk.NewCoin(coin.Denom, interest.TruncateInt()))
// We're synced up, so update user's borrow index value to match the current global borrow index value
borrow.Index[foundAtIndex].Value = borrowIndexValue
borrow.Index[foundAtIndex].Value = interestFactorValue
}
}
// Add all pending interest to user's borrow
@ -275,9 +275,9 @@ func (k Keeper) GetBorrowBalance(ctx sdk.Context, borrower sdk.AccAddress) sdk.C
if found {
totalNewInterest := sdk.Coins{}
for _, coin := range borrow.Amount {
borrowIndexValue, foundBorrowIndexValue := k.GetBorrowIndex(ctx, coin.Denom)
if foundBorrowIndexValue {
// Locate the borrow index item by coin denom in the user's list of borrow indexes
interestFactorValue, foundInterestFactorValue := k.GetInterestFactor(ctx, coin.Denom)
if foundInterestFactorValue {
// Locate the interest factor by coin denom in the user's list of interest factors
foundAtIndex := -1
for i := range borrow.Index {
if borrow.Index[i].Denom == coin.Denom {
@ -288,8 +288,8 @@ func (k Keeper) GetBorrowBalance(ctx sdk.Context, borrower sdk.AccAddress) sdk.C
// Calculate interest owed by user for this asset
if foundAtIndex != -1 {
storedAmount := sdk.NewDecFromInt(borrow.Amount.AmountOf(coin.Denom))
userLastBorrowIndex := borrow.Index[foundAtIndex].Value
coinInterest := (storedAmount.Quo(userLastBorrowIndex).Mul(borrowIndexValue)).Sub(storedAmount)
userLastInterestFactor := borrow.Index[foundAtIndex].Value
coinInterest := (storedAmount.Quo(userLastInterestFactor).Mul(interestFactorValue)).Sub(storedAmount)
totalNewInterest = totalNewInterest.Add(sdk.NewCoin(coin.Denom, coinInterest.TruncateInt()))
}
}

View File

@ -86,11 +86,11 @@ func (k Keeper) AccrueInterest(ctx sdk.Context, denom string) error {
reservesPrior = newReservesPrior
}
borrowIndexPrior, foundBorrowIndexPrior := k.GetBorrowIndex(ctx, denom)
if !foundBorrowIndexPrior {
newBorrowIndexPrior := sdk.MustNewDecFromStr("1.0")
k.SetBorrowIndex(ctx, denom, newBorrowIndexPrior)
borrowIndexPrior = newBorrowIndexPrior
interestFactorPrior, foundInterestFactorPrior := k.GetInterestFactor(ctx, denom)
if !foundInterestFactorPrior {
newInterestFactorPrior := sdk.MustNewDecFromStr("1.0")
k.SetInterestFactor(ctx, denom, newInterestFactorPrior)
interestFactorPrior = newInterestFactorPrior
}
// Fetch money market from the store
@ -115,9 +115,9 @@ func (k Keeper) AccrueInterest(ctx sdk.Context, denom string) error {
interestAccumulated := (interestFactor.Mul(sdk.NewDecFromInt(borrowsPrior.Amount)).TruncateInt()).Sub(borrowsPrior.Amount)
totalBorrowInterestAccumulated := sdk.NewCoins(sdk.NewCoin(denom, interestAccumulated))
totalReservesNew := reservesPrior.Add(sdk.NewCoin(denom, sdk.NewDecFromInt(interestAccumulated).Mul(mm.ReserveFactor).TruncateInt()))
borrowIndexNew := borrowIndexPrior.Mul(interestFactor)
interestFactorNew := interestFactorPrior.Mul(interestFactor)
k.SetBorrowIndex(ctx, denom, borrowIndexNew)
k.SetInterestFactor(ctx, denom, interestFactorNew)
k.IncrementBorrowedCoins(ctx, totalBorrowInterestAccumulated)
k.SetTotalReserves(ctx, denom, totalReservesNew)
k.SetPreviousAccrualTime(ctx, denom, ctx.BlockTime())

View File

@ -737,8 +737,8 @@ func (suite *KeeperTestSuite) TestInterest() {
reservesPrior = sdk.NewCoin(tc.args.borrowCoinDenom, sdk.ZeroInt())
}
borrowIndexPrior, foundBorrowIndexPrior := suite.keeper.GetBorrowIndex(prevCtx, tc.args.borrowCoinDenom)
suite.Require().True(foundBorrowIndexPrior)
interestFactorPrior, foundInterestFactorPrior := suite.keeper.GetInterestFactor(prevCtx, tc.args.borrowCoinDenom)
suite.Require().True(foundInterestFactorPrior)
// 2. Calculate expected interest owed
borrowRateApy, err := harvest.CalculateBorrowRate(tc.args.interestRateModel, sdk.NewDecFromInt(cashPrior), sdk.NewDecFromInt(borrowCoinPriorAmount), sdk.NewDecFromInt(reservesPrior.Amount))
@ -751,7 +751,7 @@ func (suite *KeeperTestSuite) TestInterest() {
interestFactor := harvest.CalculateInterestFactor(borrowRateSpy, sdk.NewInt(snapshot.elapsedTime))
expectedInterest := (interestFactor.Mul(sdk.NewDecFromInt(borrowCoinPriorAmount)).TruncateInt()).Sub(borrowCoinPriorAmount)
expectedReserves := reservesPrior.Add(sdk.NewCoin(tc.args.borrowCoinDenom, sdk.NewDecFromInt(expectedInterest).Mul(tc.args.reserveFactor).TruncateInt()))
expectedBorrowIndex := borrowIndexPrior.Mul(interestFactor)
expectedInterestFactor := interestFactorPrior.Mul(interestFactor)
// -------------------------------------------------------------------------------------
// Set up snapshot chain context and run begin blocker
@ -769,8 +769,8 @@ func (suite *KeeperTestSuite) TestInterest() {
suite.Require().Equal(expectedReserves, currTotalReserves)
// Check that the borrow index has increased as expected
currIndexPrior, _ := suite.keeper.GetBorrowIndex(snapshotCtx, tc.args.borrowCoinDenom)
suite.Require().Equal(expectedBorrowIndex, currIndexPrior)
currIndexPrior, _ := suite.keeper.GetInterestFactor(snapshotCtx, tc.args.borrowCoinDenom)
suite.Require().Equal(expectedInterestFactor, currIndexPrior)
// After borrowing again user's borrow balance should have any outstanding interest applied
if snapshot.shouldBorrow {

View File

@ -326,21 +326,21 @@ func (k Keeper) SetTotalReserves(ctx sdk.Context, denom string, coin sdk.Coin) {
store.Set([]byte(denom), bz)
}
// GetBorrowIndex returns the current borrow index for an individual market
func (k Keeper) GetBorrowIndex(ctx sdk.Context, denom string) (sdk.Dec, bool) {
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowIndexPrefix)
// GetInterestFactor returns the current interest factor for an individual market
func (k Keeper) GetInterestFactor(ctx sdk.Context, denom string) (sdk.Dec, bool) {
store := prefix.NewStore(ctx.KVStore(k.key), types.InterestFactorPrefix)
bz := store.Get([]byte(denom))
if bz == nil {
return sdk.ZeroDec(), false
}
var borrowIndex sdk.Dec
k.cdc.MustUnmarshalBinaryBare(bz, &borrowIndex)
return borrowIndex, true
var interestFactor sdk.Dec
k.cdc.MustUnmarshalBinaryBare(bz, &interestFactor)
return interestFactor, true
}
// SetBorrowIndex sets the current borrow index for an individual market
func (k Keeper) SetBorrowIndex(ctx sdk.Context, denom string, borrowIndex sdk.Dec) {
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowIndexPrefix)
// SetInterestFactor sets the current interest factor for an individual market
func (k Keeper) SetInterestFactor(ctx sdk.Context, denom string, borrowIndex sdk.Dec) {
store := prefix.NewStore(ctx.KVStore(k.key), types.InterestFactorPrefix)
bz := k.cdc.MustMarshalBinaryBare(borrowIndex)
store.Set([]byte(denom), bz)
}

View File

@ -4,35 +4,35 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// BorrowIndexItem defines an individual borrow index
type BorrowIndexItem struct {
Denom string `json:"denom" yaml:"denom"`
Value sdk.Dec `json:"value" yaml:"value"`
}
// NewBorrowIndexItem returns a new BorrowIndexItem instance
func NewBorrowIndexItem(denom string, value sdk.Dec) BorrowIndexItem {
return BorrowIndexItem{
Denom: denom,
Value: value,
}
}
// BorrowIndexes is a slice of BorrowIndexItem, because Amino won't marshal maps
type BorrowIndexes []BorrowIndexItem
// Borrow defines an amount of coins borrowed from a harvest module account
type Borrow struct {
Borrower sdk.AccAddress `json:"borrower" yaml:"borrower"`
Amount sdk.Coins `json:"amount" yaml:"amount"`
Index BorrowIndexes `json:"index" yaml:"index"`
Index InterestFactors `json:"index" yaml:"index"`
}
// NewBorrow returns a new Borrow instance
func NewBorrow(borrower sdk.AccAddress, amount sdk.Coins, index BorrowIndexes) Borrow {
func NewBorrow(borrower sdk.AccAddress, amount sdk.Coins, index InterestFactors) Borrow {
return Borrow{
Borrower: borrower,
Amount: amount,
Index: index,
}
}
// InterestFactor defines an individual interest factor
type InterestFactor struct {
Denom string `json:"denom" yaml:"denom"`
Value sdk.Dec `json:"value" yaml:"value"`
}
// NewInterestFactor returns a new InterestFactor instance
func NewInterestFactor(denom string, value sdk.Dec) InterestFactor {
return InterestFactor{
Denom: denom,
Value: value,
}
}
// InterestFactors is a slice of InterestFactor, because Amino won't marshal maps
type InterestFactors []InterestFactor

View File

@ -43,7 +43,7 @@ var (
MoneyMarketsPrefix = []byte{0x07}
PreviousAccrualTimePrefix = []byte{0x08} // denom -> time
TotalReservesPrefix = []byte{0x09} // denom -> sdk.Coin
BorrowIndexPrefix = []byte{0x10} // denom -> sdk.Dec
InterestFactorPrefix = []byte{0x10} // denom -> sdk.Dec
LtvIndexPrefix = []byte{0x11}
sep = []byte(":")
)