Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lend test net upgrade #438

Merged
merged 5 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1144,8 +1144,8 @@ func (a *App) ModuleAccountsPermissions() map[string][]string {

func (a *App) registerUpgradeHandlers() {
a.UpgradeKeeper.SetUpgradeHandler(
tv4_0_0.UpgradeNameV4_1_0,
tv4_0_0.CreateUpgradeHandlerV410(a.mm, a.configurator, a.Rewardskeeper, a.LiquidityKeeper),
tv4_0_0.UpgradeNameV4_2_0,
tv4_0_0.CreateUpgradeHandlerV420(a.mm, a.configurator),
)

// When a planned update height is reached, the old binary will panic
Expand Down Expand Up @@ -1215,6 +1215,8 @@ func upgradeHandlers(upgradeInfo storetypes.UpgradeInfo, a *App, storeUpgrades *
}
case upgradeInfo.Name == tv4_0_0.UpgradeNameV4_1_0 && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
storeUpgrades = &storetypes.StoreUpgrades{}
case upgradeInfo.Name == tv4_0_0.UpgradeNameV4_2_0 && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
storeUpgrades = &storetypes.StoreUpgrades{}
}
return storeUpgrades
}
14 changes: 14 additions & 0 deletions app/upgrades/testnet/v4_0_0/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ const (
}
}'`
)

const (
UpgradeNameV4_2_0 = "v4.2.0"
UpgradeHeightV4_2_0 = "" // replace this height
UpgradeInfoV4_2_0 = `'{
"binaries": {
"darwin/arm64":"",
"darwin/x86_64":"",
"linux/arm64":"",
"linux/x86_64":"",
"windows/x86_64":""
}
}'`
)
19 changes: 18 additions & 1 deletion app/upgrades/testnet/v4_0_0/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func CreateSwapFeeGauge(
_ = rewardsKeeper.CreateNewGauge(ctx, newGauge, true)
}

// CreateUpgradeHandler creates an SDK upgrade handler for v4_0_1
// CreateUpgradeHandler creates an SDK upgrade handler for v4_1_0
func CreateUpgradeHandlerV410(
mm *module.Manager,
configurator module.Configurator,
Expand All @@ -74,3 +74,20 @@ func CreateUpgradeHandlerV410(
return newVM, err
}
}

// CreateUpgradeHandler creates an SDK upgrade handler for v4_2_0
func CreateUpgradeHandlerV420(
mm *module.Manager,
configurator module.Configurator,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// This change is only for testnet upgrade

newVM, err := mm.RunMigrations(ctx, configurator, fromVM)

if err != nil {
return newVM, err
}
return newVM, err
}
}
4 changes: 2 additions & 2 deletions x/lend/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
lends, _ := k.GetLends(ctx)
userDeposit, _ := k.GetUserDepositStats(ctx)
reserveDeposit, _ := k.GetReserveDepositStats(ctx)
buyBackDeposir, _ := k.GetBuyBackDepositStats(ctx)
buyBackDeposit, _ := k.GetBuyBackDepositStats(ctx)
borrowDeposit, _ := k.GetBorrowStats(ctx)
return types.NewGenesisState(
k.GetAllBorrow(ctx),
Expand All @@ -101,7 +101,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
lends,
userDeposit,
reserveDeposit,
buyBackDeposir,
buyBackDeposit,
borrowDeposit,
k.GetLendPairs(ctx),
k.GetAllAssetRatesStats(ctx),
Expand Down
47 changes: 27 additions & 20 deletions x/lend/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,22 @@ func (k Keeper) IterateBorrow(ctx sdk.Context, ID uint64) (sdk.Dec, sdk.Dec, err
if err != nil {
return sdk.ZeroDec(), sdk.ZeroDec(), err
}

borrowInterestTracker, found := k.GetBorrowInterestTracker(ctx, borrow.ID)
if !found {
borrowInterestTracker = types.BorrowInterestTracker{
BorrowingId: borrow.ID,
InterestAccumulated: sdk.ZeroDec(),
}
}
borrowInterestTracker.InterestAccumulated = borrowInterestTracker.InterestAccumulated.Add(interestPerBlock)
if !borrow.IsStableBorrow {
borrowInterestTracker.InterestAccumulated = borrowInterestTracker.InterestAccumulated.Add(interestPerBlock)
} else {
stableInterestPerBlock, err := k.CalculateStableInterest(ctx, borrow.AmountOut.Amount.String(), borrow)
if err != nil {
return sdk.ZeroDec(), sdk.ZeroDec(), err
}
borrowInterestTracker.InterestAccumulated = borrowInterestTracker.InterestAccumulated.Add(stableInterestPerBlock)
}
newInterestPerBlock := sdk.ZeroInt()
if borrowInterestTracker.InterestAccumulated.GTE(sdk.OneDec()) {
newInterestPerBlock = borrowInterestTracker.InterestAccumulated.TruncateInt()
Expand Down Expand Up @@ -101,24 +108,24 @@ func (k Keeper) IterateBorrow(ctx sdk.Context, ID uint64) (sdk.Dec, sdk.Dec, err
return indexGlobalCurrent, reserveGlobalIndex, nil
}

//func (k Keeper) CalculateRewards(ctx sdk.Context, amount string, rate sdk.Dec) (sdk.Dec, error) {
//
// currentTime := ctx.BlockTime().Unix()
//
// prevInterestTime := k.GetLastInterestTime(ctx)
// if prevInterestTime == int64(types.Uint64Zero) {
// prevInterestTime = currentTime
// }
// secondsElapsed := currentTime - prevInterestTime
// if secondsElapsed < int64(types.Uint64Zero) {
// return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrNegativeTimeElapsed, fmt.Sprintf("%d seconds", secondsElapsed))
// }
// yearsElapsed := sdk.NewDec(secondsElapsed).QuoInt64(types.SecondsPerYear)
// amt, _ := sdk.NewDecFromStr(amount)
// perc := rate
// newAmount := amt.Mul(perc).Mul(yearsElapsed)
// return newAmount, nil
//}
func (k Keeper) CalculateStableInterest(ctx sdk.Context, amount string, borrow types.BorrowAsset) (sdk.Dec, error) {

currentTime := ctx.BlockTime().Unix()

prevInterestTime := borrow.LastInteractionTime.Unix()
if prevInterestTime == int64(types.Uint64Zero) {
prevInterestTime = currentTime
}
secondsElapsed := currentTime - prevInterestTime
if secondsElapsed < int64(types.Uint64Zero) {
return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrNegativeTimeElapsed, fmt.Sprintf("%d seconds", secondsElapsed))
}
yearsElapsed := sdk.NewDec(secondsElapsed).QuoInt64(types.SecondsPerYear)
amt, _ := sdk.NewDecFromStr(amount)
perc := borrow.StableBorrowRate
newAmount := amt.Mul(perc).Mul(yearsElapsed)
return newAmount, nil
}

func (k Keeper) CalculateLendReward(ctx sdk.Context, amount string, rate sdk.Dec, lend types.LendAsset) (sdk.Dec, sdk.Dec, error) {
currentTime := ctx.BlockTime().Unix()
Expand Down