Skip to content

Commit

Permalink
delete dec and create math mod
Browse files Browse the repository at this point in the history
  • Loading branch information
mkXultra committed Jul 14, 2022
1 parent 2d476fe commit cb73697
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
11 changes: 6 additions & 5 deletions x/cdp/keeper/interest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/UnUniFi/chain/x/cdp/types"
Expand Down Expand Up @@ -50,7 +51,7 @@ func (k Keeper) AccumulateInterest(ctx sdk.Context, ctype string) error {
return nil
}
interestFactor := CalculateInterestFactor(borrowRateSpy, sdk.NewInt(timeElapsed))
interestAccumulated := (interestFactor.Mul(totalPrincipalPrior.ToDec())).RoundInt().Sub(totalPrincipalPrior)
interestAccumulated := (interestFactor.Mul(sdk.NewDecFromInt(totalPrincipalPrior))).RoundInt().Sub(totalPrincipalPrior)
if interestAccumulated.IsZero() {
// in the case accumulated interest rounds to zero, exit early without updating accrual time
return nil
Expand Down Expand Up @@ -94,13 +95,13 @@ func CalculateInterestFactor(perSecondInterestRate sdk.Dec, secondsElapsed sdk.I
scalingFactorInt := sdk.NewInt(int64(scalingFactor))

// Convert per-second interest rate to a uint scaled by 1e18
interestMantissa := sdk.NewUintFromBigInt(perSecondInterestRate.MulInt(scalingFactorInt).RoundInt().BigInt())
interestMantissa := sdkmath.NewUintFromBigInt(perSecondInterestRate.MulInt(scalingFactorInt).RoundInt().BigInt())

// Convert seconds elapsed to uint (*not scaled*)
secondsElapsedUint := sdk.NewUintFromBigInt(secondsElapsed.BigInt())
secondsElapsedUint := sdkmath.NewUintFromBigInt(secondsElapsed.BigInt())

// Calculate the interest factor as a uint scaled by 1e18
interestFactorMantissa := sdk.RelativePow(interestMantissa, secondsElapsedUint, scalingFactorUint)
interestFactorMantissa := sdkmath.RelativePow(interestMantissa, secondsElapsedUint, scalingFactorUint)

// Convert interest factor to an unscaled sdk.Dec
return sdk.NewDecFromBigInt(interestFactorMantissa.BigInt()).QuoInt(scalingFactorInt)
Expand Down Expand Up @@ -152,7 +153,7 @@ func (k Keeper) CalculateNewInterest(ctx sdk.Context, cdp types.Cdp) sdk.Coin {
if cdpInterestFactor.Equal(sdk.OneDec()) {
return sdk.NewCoin(cdp.AccumulatedFees.Denom, sdk.ZeroInt())
}
accumulatedInterest := cdp.GetTotalPrincipal().Amount.ToDec().Mul(cdpInterestFactor).RoundInt().Sub(cdp.GetTotalPrincipal().Amount)
accumulatedInterest := sdk.NewDecFromInt(cdp.GetTotalPrincipal().Amount).Mul(cdpInterestFactor).RoundInt().Sub(cdp.GetTotalPrincipal().Amount)
return sdk.NewCoin(cdp.AccumulatedFees.Denom, accumulatedInterest)
}

Expand Down
2 changes: 1 addition & 1 deletion x/cdp/keeper/seize.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (k Keeper) payoutKeeperLiquidationReward(ctx sdk.Context, keeper sdk.AccAdd
if !found {
return types.Cdp{}, sdkerrors.Wrapf(types.ErrInvalidCollateral, "%s", cdp.Type)
}
reward := cdp.Collateral.Amount.ToDec().Mul(collateralParam.KeeperRewardPercentage).RoundInt()
reward := sdk.NewDecFromInt(cdp.Collateral.Amount).Mul(collateralParam.KeeperRewardPercentage).RoundInt()
rewardCoin := sdk.NewCoin(cdp.Collateral.Denom, reward)
paidReward := false
deposits := k.GetDeposits(ctx, cdp.Id)
Expand Down
2 changes: 1 addition & 1 deletion x/incentive/keeper/payout.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (k Keeper) ClaimCdpMintingReward(ctx sdk.Context, addr sdk.AccAddress, mult
return err
}

rewardAmount := claim.Reward.Amount.ToDec().Mul(multiplier.Factor).RoundInt()
rewardAmount := sdk.NewDecFromInt(claim.Reward.Amount).Mul(multiplier.Factor).RoundInt()
if rewardAmount.IsZero() {
return types.ErrZeroClaim
}
Expand Down
8 changes: 4 additions & 4 deletions x/incentive/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (k Keeper) AccumulateCdpMintingRewards(ctx sdk.Context, rewardPeriod types.

denoms, _ := k.GetGenesisDenoms(ctx)

totalPrincipal := k.cdpKeeper.GetTotalPrincipal(ctx, rewardPeriod.CollateralType, denoms.PrincipalDenom).ToDec()
totalPrincipal := sdk.NewDecFromInt(k.cdpKeeper.GetTotalPrincipal(ctx, rewardPeriod.CollateralType, denoms.PrincipalDenom))
if totalPrincipal.IsZero() {
k.SetPreviousCdpMintingAccrualTime(ctx, rewardPeriod.CollateralType, ctx.BlockTime())
return nil
Expand All @@ -39,7 +39,7 @@ func (k Keeper) AccumulateCdpMintingRewards(ctx sdk.Context, rewardPeriod types.
k.SetPreviousCdpMintingAccrualTime(ctx, rewardPeriod.CollateralType, ctx.BlockTime())
return nil
}
rewardFactor := newRewards.ToDec().Mul(cdpFactor).Quo(totalPrincipal)
rewardFactor := sdk.NewDecFromInt(newRewards).Mul(cdpFactor).Quo(totalPrincipal)

previousRewardFactor, found := k.GetCdpMintingRewardFactor(ctx, rewardPeriod.CollateralType)
if !found {
Expand Down Expand Up @@ -120,7 +120,7 @@ func (k Keeper) SynchronizeCdpMintingReward(ctx sdk.Context, cdp cdptypes.Cdp) {
return
}
claim.RewardIndexes[index].RewardFactor = globalRewardFactor
newRewardsAmount := rewardsAccumulatedFactor.Mul(cdp.GetTotalPrincipal().Amount.ToDec()).RoundInt()
newRewardsAmount := rewardsAccumulatedFactor.Mul(sdk.NewDecFromInt(cdp.GetTotalPrincipal().Amount)).RoundInt()
if newRewardsAmount.IsZero() {
k.SetCdpMintingClaim(ctx, claim)
return
Expand Down Expand Up @@ -212,7 +212,7 @@ func (k Keeper) SimulateCdpMintingSynchronization(ctx sdk.Context, claim types.C
if !found {
continue
}
newRewardsAmount := rewardsAccumulatedFactor.Mul(cdp.GetTotalPrincipal().Amount.ToDec()).RoundInt()
newRewardsAmount := rewardsAccumulatedFactor.Mul(sdk.NewDecFromInt(cdp.GetTotalPrincipal().Amount)).RoundInt()
if newRewardsAmount.IsZero() {
continue
}
Expand Down

0 comments on commit cb73697

Please sign in to comment.