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 module, Gas reduction, Pagination changes #387

Merged
merged 4 commits into from
Aug 10, 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
24 changes: 23 additions & 1 deletion proto/comdex/lend/v1beta1/lend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,29 @@ message ReservePoolRecordsForBorrow{
(gogoproto.moretags) = "yaml:\"borrowing_id\""
];
string interest_accumulated = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"interest_accumulated\""
];
}

message Borrow_interest_tracker{
uint64 borrowing_id = 1 [
(gogoproto.moretags) = "yaml:\"borrowing_id\""
];
string interest_accumulated = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"interest_accumulated\""
];
}

message Lend_rewards_tracker{
uint64 lending_id = 1 [
(gogoproto.moretags) = "yaml:\"lending_id\""
];
string rewards_accumulated = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"interest_accumulated\""
];
Expand Down
20 changes: 14 additions & 6 deletions x/auction/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ func querySurplusAuctions() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "auctions")
flags.AddPaginationFlagsToCmd(cmd, "all-surplus-auctions")
return cmd
}

func querySurplusBiddings() *cobra.Command {
cmd := &cobra.Command{
Use: "suplus-biddings [bidder] [app-id] [history]",
Use: "surplus-biddings [bidder] [app-id] [history]",
Short: "Query surplus biddings by bidder address",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -142,6 +142,8 @@ func querySurplusBiddings() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "surplus-biddings")

return cmd
}

Expand Down Expand Up @@ -229,7 +231,7 @@ func queryDebtAuctions() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "auctions")
flags.AddPaginationFlagsToCmd(cmd, "all-debt-auctions")
return cmd
}

Expand Down Expand Up @@ -276,6 +278,8 @@ func queryDebtBidding() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "debt-biddings")

return cmd
}

Expand Down Expand Up @@ -363,7 +367,7 @@ func queryDutchAuctions() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "auctions")
flags.AddPaginationFlagsToCmd(cmd, "all-dutch-auctions")
return cmd
}

Expand Down Expand Up @@ -400,7 +404,7 @@ func queryProtocolStats() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "stats")
flags.AddPaginationFlagsToCmd(cmd, "all-protocol-stats")
return cmd
}

Expand Down Expand Up @@ -447,6 +451,8 @@ func queryDutchBiddings() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "dutch-biddings")

return cmd
}

Expand Down Expand Up @@ -599,7 +605,7 @@ func queryDutchLendAuctions() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "auctions")
flags.AddPaginationFlagsToCmd(cmd, "all-dutch-auctions-lend")
return cmd
}

Expand Down Expand Up @@ -646,5 +652,7 @@ func queryDutchLendBiddings() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "dutch-biddings-lend")

return cmd
}
8 changes: 4 additions & 4 deletions x/auction/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
var _ paramtypes.ParamSet = (*Params)(nil)

const (
DutchBidGas = sdk.Gas(71183)
DebtBidGas = sdk.Gas(57580)
DutchLendBidGas = sdk.Gas(71183)
SurplusBidGas = sdk.Gas(57580)
DutchBidGas = sdk.Gas(31183)
DebtBidGas = sdk.Gas(27580)
DutchLendBidGas = sdk.Gas(31183)
SurplusBidGas = sdk.Gas(27580)
)

// ParamKeyTable the param key table for launch module.
Expand Down
2 changes: 2 additions & 0 deletions x/collector/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func QueryCollectorLookupByApp() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "collector-lookup-by-app")

return cmd
}

Expand Down
6 changes: 3 additions & 3 deletions x/esm/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var (
)

const (
DepositESMGas = sdk.Gas(66329)
ExecuteESMGas = sdk.Gas(53554)
DepositESMGas = sdk.Gas(36329)
ExecuteESMGas = sdk.Gas(23554)
MsgKillSwitchGas = sdk.Gas(76473)
MsgCollateralRedemptionGas = sdk.Gas(87559)
MsgCollateralRedemptionGas = sdk.Gas(37559)
)

func NewParams(admin []string) Params {
Expand Down
11 changes: 9 additions & 2 deletions x/lend/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ func QueryAllLendsByOwner() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "lends-by-owner")

return cmd
}

Expand Down Expand Up @@ -193,6 +195,7 @@ func QueryAllLendsByOwnerAndPoolID() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "lends-by-owner-pool")
return cmd
}

Expand Down Expand Up @@ -418,7 +421,7 @@ func queryAssetToPairMappings() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "lends")
flags.AddPaginationFlagsToCmd(cmd, "asset-pair-mappings")

return cmd
}
Expand Down Expand Up @@ -489,7 +492,7 @@ func queryBorrows() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "lends")
flags.AddPaginationFlagsToCmd(cmd, "borrows")

return cmd
}
Expand Down Expand Up @@ -524,6 +527,8 @@ func QueryAllBorrowsByOwner() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "borrows-by-owner")

return cmd
}

Expand Down Expand Up @@ -563,6 +568,8 @@ func QueryAllBorrowsByOwnerAndPoolID() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "borrows-by-owner-pool")

return cmd
}

Expand Down
25 changes: 25 additions & 0 deletions x/lend/keeper/borrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,28 @@ func (k Keeper) DeleteReservePoolRecordsForBorrow(ctx sdk.Context, ID uint64) {

store.Delete(key)
}

func (k Keeper) SetBorrowInterestTracker(ctx sdk.Context, interest types.BorrowInterestTracker) {
var (
store = k.Store(ctx)
key = types.BorrowInterestTrackerKey(interest.BorrowingId)
value = k.cdc.MustMarshal(&interest)
)

store.Set(key, value)
}

func (k Keeper) GetBorrowInterestTracker(ctx sdk.Context, id uint64) (interest types.BorrowInterestTracker, found bool) {
var (
store = k.Store(ctx)
key = types.BorrowInterestTrackerKey(id)
value = store.Get(key)
)

if value == nil {
return interest, false
}

k.cdc.MustUnmarshal(value, &interest)
return interest, true
}
95 changes: 49 additions & 46 deletions x/lend/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/comdex-official/comdex/x/lend/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"strconv"
)

func (k Keeper) IterateLends(ctx sdk.Context) error {
Expand All @@ -20,25 +19,29 @@ func (k Keeper) IterateLends(ctx sdk.Context) error {
if err != nil {
continue
}
if interestPerBlock.GT(sdk.ZeroInt()) {

updatedLend := types.LendAsset{
ID: lend.ID,
AssetID: lend.AssetID,
PoolID: lend.PoolID,
Owner: lend.Owner,
AmountIn: lend.AmountIn,
LendingTime: lend.LendingTime,
UpdatedAmountIn: lend.UpdatedAmountIn.Add(interestPerBlock),
AvailableToBorrow: lend.AvailableToBorrow.Add(interestPerBlock),
Reward_Accumulated: lend.Reward_Accumulated.Add(interestPerBlock),
AppID: lend.AppID,
CPoolName: lend.CPoolName,
lendRewardsTracker, found := k.GetLendRewardTracker(ctx, lend.ID)
if !found {
lendRewardsTracker = types.LendRewardsTracker{
LendingId: lend.ID,
RewardsAccumulated: sdk.ZeroDec(),
}
}
lendRewardsTracker.RewardsAccumulated = lendRewardsTracker.RewardsAccumulated.Add(interestPerBlock)
newInterestPerBlock := sdk.ZeroInt()
if lendRewardsTracker.RewardsAccumulated.GTE(sdk.OneDec()) {
newInterestPerBlock = lendRewardsTracker.RewardsAccumulated.TruncateInt()
newRewardDec := sdk.NewDec(newInterestPerBlock.Int64())
lendRewardsTracker.RewardsAccumulated = lendRewardsTracker.RewardsAccumulated.Sub(newRewardDec)
}
k.SetLendRewardTracker(ctx, lendRewardsTracker)
if newInterestPerBlock.GT(sdk.ZeroInt()) {
lend.UpdatedAmountIn = lend.UpdatedAmountIn.Add(newInterestPerBlock)
lend.AvailableToBorrow = lend.AvailableToBorrow.Add(newInterestPerBlock)
lend.Reward_Accumulated = lend.Reward_Accumulated.Add(newInterestPerBlock)

pool, _ := k.GetPool(ctx, lend.PoolID)
asset, _ := k.GetAsset(ctx, lend.AssetID)
Amount := sdk.NewCoin(asset.Denom, interestPerBlock)
Amount := sdk.NewCoin(asset.Denom, newInterestPerBlock)
assetRatesStat, found := k.GetAssetRatesStats(ctx, lend.AssetID)
if !found {
continue
Expand All @@ -53,7 +56,7 @@ func (k Keeper) IterateLends(ctx sdk.Context) error {
if err != nil {
continue
}
k.SetLend(ctx, updatedLend)
k.SetLend(ctx, lend)
}
}
return nil
Expand All @@ -78,41 +81,44 @@ func (k Keeper) IterateBorrows(ctx sdk.Context) error {
if err != nil {
continue
}
borrowInterestTracker, found := k.GetBorrowInterestTracker(ctx, borrow.ID)
if !found {
borrowInterestTracker = types.BorrowInterestTracker{
BorrowingId: borrow.ID,
InterestAccumulated: sdk.ZeroDec(),
}
}
borrowInterestTracker.InterestAccumulated = borrowInterestTracker.InterestAccumulated.Add(interestPerBlock)
newInterestPerBlock := sdk.ZeroInt()
if borrowInterestTracker.InterestAccumulated.GTE(sdk.OneDec()) {
newInterestPerBlock = borrowInterestTracker.InterestAccumulated.TruncateInt()
newRewardDec := sdk.NewDec(newInterestPerBlock.Int64())
borrowInterestTracker.InterestAccumulated = borrowInterestTracker.InterestAccumulated.Sub(newRewardDec)
}
k.SetBorrowInterestTracker(ctx, borrowInterestTracker)

reservePoolRecords, found := k.GetReservePoolRecordsForBorrow(ctx, borrow.ID)
if !found {
reservePoolRecords = types.ReservePoolRecordsForBorrow{
ID: borrow.ID,
InterestAccumulated: sdk.ZeroInt(),
InterestAccumulated: sdk.ZeroDec(),
}
}
if reservePoolAmountPerBlock.GT(sdk.ZeroInt()) {
if reservePoolAmountPerBlock.GT(sdk.ZeroDec()) {
reservePoolRecords.InterestAccumulated = reservePoolRecords.InterestAccumulated.Add(reservePoolAmountPerBlock)
}
k.SetReservePoolRecordsForBorrow(ctx, reservePoolRecords)
if interestPerBlock.GT(sdk.ZeroInt()) {

updatedBorrow := types.BorrowAsset{
ID: borrow.ID,
LendingID: borrow.LendingID,
IsStableBorrow: borrow.IsStableBorrow,
PairID: borrow.PairID,
AmountIn: borrow.AmountIn,
AmountOut: borrow.AmountOut,
BridgedAssetAmount: borrow.BridgedAssetAmount,
BorrowingTime: borrow.BorrowingTime,
StableBorrowRate: borrow.StableBorrowRate,
UpdatedAmountOut: borrow.UpdatedAmountOut.Add(interestPerBlock),
Interest_Accumulated: borrow.Interest_Accumulated.Add(interestPerBlock),
CPoolName: borrow.CPoolName,
}
if newInterestPerBlock.GT(sdk.ZeroInt()) {
borrow.UpdatedAmountOut = borrow.UpdatedAmountOut.Add(newInterestPerBlock)
borrow.Interest_Accumulated = borrow.Interest_Accumulated.Add(newInterestPerBlock)

k.SetBorrow(ctx, updatedBorrow)
k.SetBorrow(ctx, borrow)
}
}
return nil
}

func (k Keeper) CalculateRewards(ctx sdk.Context, amount string, rate sdk.Dec) (sdk.Int, error) {
func (k Keeper) CalculateRewards(ctx sdk.Context, amount string, rate sdk.Dec) (sdk.Dec, error) {

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

Expand All @@ -122,16 +128,13 @@ func (k Keeper) CalculateRewards(ctx sdk.Context, amount string, rate sdk.Dec) (
}
secondsElapsed := currentTime - prevInterestTime
if secondsElapsed < int64(types.Uint64Zero) {
return sdk.ZeroInt(), sdkerrors.Wrap(types.ErrNegativeTimeElapsed, fmt.Sprintf("%d seconds", secondsElapsed))
return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrNegativeTimeElapsed, fmt.Sprintf("%d seconds", secondsElapsed))
}

yearsElapsed := sdk.NewDec(secondsElapsed).QuoInt64(types.SecondsPerYear).MustFloat64()
amtFloat, _ := strconv.ParseFloat(amount, 64)
perc := rate.String()
b, _ := sdk.NewDecFromStr(perc)

newAmount := amtFloat * b.MustFloat64() * (yearsElapsed)
return sdk.NewInt(int64(newAmount)), nil
yearsElapsed := sdk.NewDec(secondsElapsed).QuoInt64(types.SecondsPerYear)
amtFloat, _ := sdk.NewDecFromStr(amount)
perc := rate
newAmount := amtFloat.Mul(perc).Mul(yearsElapsed)
return newAmount, nil
}

func (k Keeper) ReBalanceStableRates(ctx sdk.Context) error {
Expand Down
Loading