encapsulate total principle calculation within cdp type and use (#610)

throughout codebase
This commit is contained in:
Nick DeLuca 2020-07-10 08:16:05 -05:00 committed by GitHub
parent d83c43dcb4
commit 86c0225174
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 31 additions and 18 deletions

View File

@ -72,9 +72,9 @@ func InitGenesis(ctx sdk.Context, k Keeper, pk types.PricefeedKeeper, sk types.S
panic(fmt.Sprintf("error setting cdp: %v", err)) panic(fmt.Sprintf("error setting cdp: %v", err))
} }
k.IndexCdpByOwner(ctx, cdp) k.IndexCdpByOwner(ctx, cdp)
ratio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) ratio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
k.IndexCdpByCollateralRatio(ctx, cdp.Collateral.Denom, cdp.ID, ratio) k.IndexCdpByCollateralRatio(ctx, cdp.Collateral.Denom, cdp.ID, ratio)
k.IncrementTotalPrincipal(ctx, cdp.Collateral.Denom, cdp.Principal.Add(cdp.AccumulatedFees)) k.IncrementTotalPrincipal(ctx, cdp.Collateral.Denom, cdp.GetTotalPrincipal())
} }
k.SetNextCdpID(ctx, gs.StartingCdpID) k.SetNextCdpID(ctx, gs.StartingCdpID)

View File

@ -437,7 +437,7 @@ func (k Keeper) LoadAugmentedCDP(ctx sdk.Context, cdp types.CDP) types.Augmented
return types.AugmentedCDP{CDP: cdp} return types.AugmentedCDP{CDP: cdp}
} }
// convert collateral value to debt coin // convert collateral value to debt coin
totalDebt := cdp.Principal.Amount.Add(cdp.AccumulatedFees.Amount) totalDebt := cdp.GetTotalPrincipal().Amount
collateralValueInDebtDenom := sdk.NewDecFromInt(totalDebt).Mul(collateralizationRatio) collateralValueInDebtDenom := sdk.NewDecFromInt(totalDebt).Mul(collateralizationRatio)
collateralValueInDebt := sdk.NewCoin(cdp.Principal.Denom, collateralValueInDebtDenom.RoundInt()) collateralValueInDebt := sdk.NewCoin(cdp.Principal.Denom, collateralValueInDebtDenom.RoundInt())
// create new augmuented cdp // create new augmuented cdp

View File

@ -42,11 +42,11 @@ func (k Keeper) DepositCollateral(ctx sdk.Context, owner, depositor sdk.AccAddre
k.SetDeposit(ctx, deposit) k.SetDeposit(ctx, deposit)
oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
k.RemoveCdpCollateralRatioIndex(ctx, cdp.Collateral.Denom, cdp.ID, oldCollateralToDebtRatio) k.RemoveCdpCollateralRatioIndex(ctx, cdp.Collateral.Denom, cdp.ID, oldCollateralToDebtRatio)
cdp.Collateral = cdp.Collateral.Add(collateral) cdp.Collateral = cdp.Collateral.Add(collateral)
collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
return k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio) return k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio)
} }
@ -88,11 +88,11 @@ func (k Keeper) WithdrawCollateral(ctx sdk.Context, owner, depositor sdk.AccAddr
if err != nil { if err != nil {
panic(err) panic(err)
} }
oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
k.RemoveCdpCollateralRatioIndex(ctx, cdp.Collateral.Denom, cdp.ID, oldCollateralToDebtRatio) k.RemoveCdpCollateralRatioIndex(ctx, cdp.Collateral.Denom, cdp.ID, oldCollateralToDebtRatio)
cdp.Collateral = cdp.Collateral.Sub(collateral) cdp.Collateral = cdp.Collateral.Sub(collateral)
collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
err = k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio) err = k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio)
if err != nil { if err != nil {
return err return err

View File

@ -57,7 +57,7 @@ func (k Keeper) AddPrincipal(ctx sdk.Context, owner sdk.AccAddress, denom string
) )
// remove old collateral:debt index // remove old collateral:debt index
oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
k.RemoveCdpCollateralRatioIndex(ctx, denom, cdp.ID, oldCollateralToDebtRatio) k.RemoveCdpCollateralRatioIndex(ctx, denom, cdp.ID, oldCollateralToDebtRatio)
// update cdp state // update cdp state
@ -67,7 +67,7 @@ func (k Keeper) AddPrincipal(ctx sdk.Context, owner sdk.AccAddress, denom string
k.IncrementTotalPrincipal(ctx, cdp.Collateral.Denom, principal) k.IncrementTotalPrincipal(ctx, cdp.Collateral.Denom, principal)
// set cdp state and indexes in the store // set cdp state and indexes in the store
collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
return k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio) return k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio)
} }
@ -86,7 +86,7 @@ func (k Keeper) RepayPrincipal(ctx sdk.Context, owner sdk.AccAddress, denom stri
} }
// Note: assumes cdp.Principal and cdp.AccumulatedFees don't change during calculations // Note: assumes cdp.Principal and cdp.AccumulatedFees don't change during calculations
totalPrincipal := cdp.Principal.Add(cdp.AccumulatedFees) totalPrincipal := cdp.GetTotalPrincipal()
// calculate fee and principal payment // calculate fee and principal payment
feePayment, principalPayment := k.calculatePayment(ctx, totalPrincipal, cdp.AccumulatedFees, payment) feePayment, principalPayment := k.calculatePayment(ctx, totalPrincipal, cdp.AccumulatedFees, payment)
@ -167,13 +167,13 @@ func (k Keeper) RepayPrincipal(ctx sdk.Context, owner sdk.AccAddress, denom stri
} }
// set cdp state and update indexes // set cdp state and update indexes
collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
return k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio) return k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio)
} }
// ValidatePaymentCoins validates that the input coins are valid for repaying debt // ValidatePaymentCoins validates that the input coins are valid for repaying debt
func (k Keeper) ValidatePaymentCoins(ctx sdk.Context, cdp types.CDP, payment sdk.Coin) error { func (k Keeper) ValidatePaymentCoins(ctx sdk.Context, cdp types.CDP, payment sdk.Coin) error {
debt := cdp.Principal.Add(cdp.AccumulatedFees) debt := cdp.GetTotalPrincipal()
if payment.Denom != debt.Denom { if payment.Denom != debt.Denom {
return sdkerrors.Wrapf(types.ErrInvalidPayment, "cdp %d: expected %s, got %s", cdp.ID, debt.Denom, payment.Denom) return sdkerrors.Wrapf(types.ErrInvalidPayment, "cdp %d: expected %s, got %s", cdp.ID, debt.Denom, payment.Denom)
} }

View File

@ -26,7 +26,7 @@ func (k Keeper) CalculateFees(ctx sdk.Context, principal sdk.Coin, periods sdk.I
func (k Keeper) UpdateFeesForAllCdps(ctx sdk.Context, collateralDenom string) error { func (k Keeper) UpdateFeesForAllCdps(ctx sdk.Context, collateralDenom string) error {
var iterationErr error var iterationErr error
k.IterateCdpsByDenom(ctx, collateralDenom, func(cdp types.CDP) bool { k.IterateCdpsByDenom(ctx, collateralDenom, func(cdp types.CDP) bool {
oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
// periods = bblock timestamp - fees updated // periods = bblock timestamp - fees updated
periods := sdk.NewInt(ctx.BlockTime().Unix()).Sub(sdk.NewInt(cdp.FeesUpdated.Unix())) periods := sdk.NewInt(ctx.BlockTime().Unix()).Sub(sdk.NewInt(cdp.FeesUpdated.Unix()))
@ -84,7 +84,7 @@ func (k Keeper) UpdateFeesForAllCdps(ctx sdk.Context, collateralDenom string) er
// and set the fees updated time to the current block time since we just updated it // and set the fees updated time to the current block time since we just updated it
cdp.FeesUpdated = ctx.BlockTime() cdp.FeesUpdated = ctx.BlockTime()
collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) collateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
k.RemoveCdpCollateralRatioIndex(ctx, cdp.Collateral.Denom, cdp.ID, oldCollateralToDebtRatio) k.RemoveCdpCollateralRatioIndex(ctx, cdp.Collateral.Denom, cdp.ID, oldCollateralToDebtRatio)
err = k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio) err = k.SetCdpAndCollateralRatioIndex(ctx, cdp, collateralToDebtRatio)
if err != nil { if err != nil {

View File

@ -17,11 +17,11 @@ import (
// (this is the equivalent of saying that fees are no longer accumulated by a cdp once it gets liquidated) // (this is the equivalent of saying that fees are no longer accumulated by a cdp once it gets liquidated)
func (k Keeper) SeizeCollateral(ctx sdk.Context, cdp types.CDP) error { func (k Keeper) SeizeCollateral(ctx sdk.Context, cdp types.CDP) error {
// Calculate the previous collateral ratio // Calculate the previous collateral ratio
oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) oldCollateralToDebtRatio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.GetTotalPrincipal())
// Move debt coins from cdp to liquidator account // Move debt coins from cdp to liquidator account
deposits := k.GetDeposits(ctx, cdp.ID) deposits := k.GetDeposits(ctx, cdp.ID)
debt := cdp.Principal.Amount.Add(cdp.AccumulatedFees.Amount) debt := cdp.GetTotalPrincipal().Amount
modAccountDebt := k.getModAccountDebt(ctx, types.ModuleName) modAccountDebt := k.getModAccountDebt(ctx, types.ModuleName)
debt = sdk.MinInt(debt, modAccountDebt) debt = sdk.MinInt(debt, modAccountDebt)
debtCoin := sdk.NewCoin(k.GetDebtDenom(ctx), debt) debtCoin := sdk.NewCoin(k.GetDebtDenom(ctx), debt)
@ -54,7 +54,7 @@ func (k Keeper) SeizeCollateral(ctx sdk.Context, cdp types.CDP) error {
} }
// Decrement total principal for this collateral type // Decrement total principal for this collateral type
coinsToDecrement := cdp.Principal.Add(cdp.AccumulatedFees) coinsToDecrement := cdp.GetTotalPrincipal()
k.DecrementTotalPrincipal(ctx, cdp.Collateral.Denom, coinsToDecrement) k.DecrementTotalPrincipal(ctx, cdp.Collateral.Denom, coinsToDecrement)
// Delete CDP from state // Delete CDP from state

View File

@ -76,6 +76,11 @@ func (cdp CDP) Validate() error {
return nil return nil
} }
// GetTotalPrinciple returns the total principle for the cdp
func (cdp CDP) GetTotalPrincipal() sdk.Coin {
return cdp.Principal.Add(cdp.AccumulatedFees)
}
// CDPs a collection of CDP objects // CDPs a collection of CDP objects
type CDPs []CDP type CDPs []CDP

View File

@ -157,7 +157,15 @@ func (suite *CdpValidationSuite) TestDepositValidation() {
} }
}) })
} }
}
func (suite *CdpValidationSuite) TestCdpGetTotalPrinciple() {
principal := sdk.Coin{"usdx", sdk.NewInt(100500)}
acummulatedFees := sdk.Coin{"usdx", sdk.NewInt(25000)}
cdp := types.CDP{Principal: principal, AccumulatedFees: acummulatedFees}
suite.Require().Equal(cdp.GetTotalPrincipal(), principal.Add(acummulatedFees))
} }
func TestCdpValidationSuite(t *testing.T) { func TestCdpValidationSuite(t *testing.T) {

View File

@ -73,7 +73,7 @@ func (k Keeper) ApplyRewardsToCdps(ctx sdk.Context) {
rewardsThisPeriod := rp.Reward.Amount.Mul(timeElapsed) rewardsThisPeriod := rp.Reward.Amount.Mul(timeElapsed)
id := k.GetNextClaimPeriodID(ctx, rp.Denom) id := k.GetNextClaimPeriodID(ctx, rp.Denom)
k.cdpKeeper.IterateCdpsByDenom(ctx, rp.Denom, func(cdp cdptypes.CDP) bool { k.cdpKeeper.IterateCdpsByDenom(ctx, rp.Denom, func(cdp cdptypes.CDP) bool {
rewardsShare := sdk.NewDecFromInt(cdp.Principal.Amount.Add(cdp.AccumulatedFees.Amount)).Quo(sdk.NewDecFromInt(totalPrincipal)) rewardsShare := sdk.NewDecFromInt(cdp.GetTotalPrincipal().Amount).Quo(sdk.NewDecFromInt(totalPrincipal))
// sanity check - don't create zero claims // sanity check - don't create zero claims
if rewardsShare.IsZero() { if rewardsShare.IsZero() {
return false return false