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 { func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins) error {
// Set any new denoms' global borrow index to 1.0 // Set any new denoms' global borrow index to 1.0
for _, coin := range coins { for _, coin := range coins {
_, foundBorrowIndex := k.GetBorrowIndex(ctx, coin.Denom) _, foundInterestFactor := k.GetInterestFactor(ctx, coin.Denom)
if !foundBorrowIndex { if !foundInterestFactor {
_, foundMM := k.GetMoneyMarket(ctx, coin.Denom) _, foundMm := k.GetMoneyMarket(ctx, coin.Denom)
if foundMM { if foundMm {
k.SetBorrowIndex(ctx, coin.Denom, sdk.OneDec()) 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 // 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. // We use a list of BorrowIndexItem here because Amino doesn't support marshaling maps.
if !found { if !found {
var borrowIndexes types.BorrowIndexes var interestFactors types.InterestFactors
for _, coin := range coins { for _, coin := range coins {
borrowIndexValue, _ := k.GetBorrowIndex(ctx, coin.Denom) interestFactorValue, _ := k.GetInterestFactor(ctx, coin.Denom)
borrowIndex := types.NewBorrowIndexItem(coin.Denom, borrowIndexValue) interestFactor := types.NewInterestFactor(coin.Denom, interestFactorValue)
borrowIndexes = append(borrowIndexes, borrowIndex) interestFactors = append(interestFactors, interestFactor)
} }
borrow := types.NewBorrow(borrower, sdk.Coins{}, borrowIndexes) borrow := types.NewBorrow(borrower, sdk.Coins{}, interestFactors)
k.SetBorrow(ctx, borrow) 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 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 } else { // User has an existing borrow index for this denom
// Calculate interest owed by user since asset's last borrow index update // Calculate interest owed by user since asset's last borrow index update
storedAmount := sdk.NewDecFromInt(borrow.Amount.AmountOf(coin.Denom)) storedAmount := sdk.NewDecFromInt(borrow.Amount.AmountOf(coin.Denom))
userLastBorrowIndex := borrow.Index[foundAtIndex].Value userLastInterestFactor := borrow.Index[foundAtIndex].Value
interest := (storedAmount.Quo(userLastBorrowIndex).Mul(borrowIndexValue)).Sub(storedAmount) interest := (storedAmount.Quo(userLastInterestFactor).Mul(interestFactorValue)).Sub(storedAmount)
totalNewInterest = totalNewInterest.Add(sdk.NewCoin(coin.Denom, interest.TruncateInt())) 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 // 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 // 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 { if found {
totalNewInterest := sdk.Coins{} totalNewInterest := sdk.Coins{}
for _, coin := range borrow.Amount { for _, coin := range borrow.Amount {
borrowIndexValue, foundBorrowIndexValue := k.GetBorrowIndex(ctx, coin.Denom) interestFactorValue, foundInterestFactorValue := k.GetInterestFactor(ctx, coin.Denom)
if foundBorrowIndexValue { if foundInterestFactorValue {
// Locate the borrow index item by coin denom in the user's list of borrow indexes // Locate the interest factor by coin denom in the user's list of interest factors
foundAtIndex := -1 foundAtIndex := -1
for i := range borrow.Index { for i := range borrow.Index {
if borrow.Index[i].Denom == coin.Denom { 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 // Calculate interest owed by user for this asset
if foundAtIndex != -1 { if foundAtIndex != -1 {
storedAmount := sdk.NewDecFromInt(borrow.Amount.AmountOf(coin.Denom)) storedAmount := sdk.NewDecFromInt(borrow.Amount.AmountOf(coin.Denom))
userLastBorrowIndex := borrow.Index[foundAtIndex].Value userLastInterestFactor := borrow.Index[foundAtIndex].Value
coinInterest := (storedAmount.Quo(userLastBorrowIndex).Mul(borrowIndexValue)).Sub(storedAmount) coinInterest := (storedAmount.Quo(userLastInterestFactor).Mul(interestFactorValue)).Sub(storedAmount)
totalNewInterest = totalNewInterest.Add(sdk.NewCoin(coin.Denom, coinInterest.TruncateInt())) 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 reservesPrior = newReservesPrior
} }
borrowIndexPrior, foundBorrowIndexPrior := k.GetBorrowIndex(ctx, denom) interestFactorPrior, foundInterestFactorPrior := k.GetInterestFactor(ctx, denom)
if !foundBorrowIndexPrior { if !foundInterestFactorPrior {
newBorrowIndexPrior := sdk.MustNewDecFromStr("1.0") newInterestFactorPrior := sdk.MustNewDecFromStr("1.0")
k.SetBorrowIndex(ctx, denom, newBorrowIndexPrior) k.SetInterestFactor(ctx, denom, newInterestFactorPrior)
borrowIndexPrior = newBorrowIndexPrior interestFactorPrior = newInterestFactorPrior
} }
// Fetch money market from the store // 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) interestAccumulated := (interestFactor.Mul(sdk.NewDecFromInt(borrowsPrior.Amount)).TruncateInt()).Sub(borrowsPrior.Amount)
totalBorrowInterestAccumulated := sdk.NewCoins(sdk.NewCoin(denom, interestAccumulated)) totalBorrowInterestAccumulated := sdk.NewCoins(sdk.NewCoin(denom, interestAccumulated))
totalReservesNew := reservesPrior.Add(sdk.NewCoin(denom, sdk.NewDecFromInt(interestAccumulated).Mul(mm.ReserveFactor).TruncateInt())) 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.IncrementBorrowedCoins(ctx, totalBorrowInterestAccumulated)
k.SetTotalReserves(ctx, denom, totalReservesNew) k.SetTotalReserves(ctx, denom, totalReservesNew)
k.SetPreviousAccrualTime(ctx, denom, ctx.BlockTime()) k.SetPreviousAccrualTime(ctx, denom, ctx.BlockTime())

View File

@ -737,8 +737,8 @@ func (suite *KeeperTestSuite) TestInterest() {
reservesPrior = sdk.NewCoin(tc.args.borrowCoinDenom, sdk.ZeroInt()) reservesPrior = sdk.NewCoin(tc.args.borrowCoinDenom, sdk.ZeroInt())
} }
borrowIndexPrior, foundBorrowIndexPrior := suite.keeper.GetBorrowIndex(prevCtx, tc.args.borrowCoinDenom) interestFactorPrior, foundInterestFactorPrior := suite.keeper.GetInterestFactor(prevCtx, tc.args.borrowCoinDenom)
suite.Require().True(foundBorrowIndexPrior) suite.Require().True(foundInterestFactorPrior)
// 2. Calculate expected interest owed // 2. Calculate expected interest owed
borrowRateApy, err := harvest.CalculateBorrowRate(tc.args.interestRateModel, sdk.NewDecFromInt(cashPrior), sdk.NewDecFromInt(borrowCoinPriorAmount), sdk.NewDecFromInt(reservesPrior.Amount)) 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)) interestFactor := harvest.CalculateInterestFactor(borrowRateSpy, sdk.NewInt(snapshot.elapsedTime))
expectedInterest := (interestFactor.Mul(sdk.NewDecFromInt(borrowCoinPriorAmount)).TruncateInt()).Sub(borrowCoinPriorAmount) 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())) 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 // Set up snapshot chain context and run begin blocker
@ -769,8 +769,8 @@ func (suite *KeeperTestSuite) TestInterest() {
suite.Require().Equal(expectedReserves, currTotalReserves) suite.Require().Equal(expectedReserves, currTotalReserves)
// Check that the borrow index has increased as expected // Check that the borrow index has increased as expected
currIndexPrior, _ := suite.keeper.GetBorrowIndex(snapshotCtx, tc.args.borrowCoinDenom) currIndexPrior, _ := suite.keeper.GetInterestFactor(snapshotCtx, tc.args.borrowCoinDenom)
suite.Require().Equal(expectedBorrowIndex, currIndexPrior) suite.Require().Equal(expectedInterestFactor, currIndexPrior)
// After borrowing again user's borrow balance should have any outstanding interest applied // After borrowing again user's borrow balance should have any outstanding interest applied
if snapshot.shouldBorrow { 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) store.Set([]byte(denom), bz)
} }
// GetBorrowIndex returns the current borrow index for an individual market // GetInterestFactor returns the current interest factor for an individual market
func (k Keeper) GetBorrowIndex(ctx sdk.Context, denom string) (sdk.Dec, bool) { func (k Keeper) GetInterestFactor(ctx sdk.Context, denom string) (sdk.Dec, bool) {
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowIndexPrefix) store := prefix.NewStore(ctx.KVStore(k.key), types.InterestFactorPrefix)
bz := store.Get([]byte(denom)) bz := store.Get([]byte(denom))
if bz == nil { if bz == nil {
return sdk.ZeroDec(), false return sdk.ZeroDec(), false
} }
var borrowIndex sdk.Dec var interestFactor sdk.Dec
k.cdc.MustUnmarshalBinaryBare(bz, &borrowIndex) k.cdc.MustUnmarshalBinaryBare(bz, &interestFactor)
return borrowIndex, true return interestFactor, true
} }
// SetBorrowIndex sets the current borrow index for an individual market // SetInterestFactor sets the current interest factor for an individual market
func (k Keeper) SetBorrowIndex(ctx sdk.Context, denom string, borrowIndex sdk.Dec) { func (k Keeper) SetInterestFactor(ctx sdk.Context, denom string, borrowIndex sdk.Dec) {
store := prefix.NewStore(ctx.KVStore(k.key), types.BorrowIndexPrefix) store := prefix.NewStore(ctx.KVStore(k.key), types.InterestFactorPrefix)
bz := k.cdc.MustMarshalBinaryBare(borrowIndex) bz := k.cdc.MustMarshalBinaryBare(borrowIndex)
store.Set([]byte(denom), bz) store.Set([]byte(denom), bz)
} }

View File

@ -4,35 +4,35 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" 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 // Borrow defines an amount of coins borrowed from a harvest module account
type Borrow struct { type Borrow struct {
Borrower sdk.AccAddress `json:"borrower" yaml:"borrower"` Borrower sdk.AccAddress `json:"borrower" yaml:"borrower"`
Amount sdk.Coins `json:"amount" yaml:"amount"` 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 // 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{ return Borrow{
Borrower: borrower, Borrower: borrower,
Amount: amount, Amount: amount,
Index: index, 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} MoneyMarketsPrefix = []byte{0x07}
PreviousAccrualTimePrefix = []byte{0x08} // denom -> time PreviousAccrualTimePrefix = []byte{0x08} // denom -> time
TotalReservesPrefix = []byte{0x09} // denom -> sdk.Coin TotalReservesPrefix = []byte{0x09} // denom -> sdk.Coin
BorrowIndexPrefix = []byte{0x10} // denom -> sdk.Dec InterestFactorPrefix = []byte{0x10} // denom -> sdk.Dec
LtvIndexPrefix = []byte{0x11} LtvIndexPrefix = []byte{0x11}
sep = []byte(":") sep = []byte(":")
) )