Skip to content

Commit

Permalink
[Tier]: Add query to get consolidated price (#650)
Browse files Browse the repository at this point in the history
* add consolidated price function

* resp proto

* fix typo

* handle zero lev amount
  • Loading branch information
amityadav0 authored Jul 15, 2024
1 parent f44b203 commit 47717bc
Show file tree
Hide file tree
Showing 21 changed files with 854 additions and 163 deletions.
219 changes: 159 additions & 60 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41525,43 +41525,51 @@ paths:
items:
type: object
properties:
address:
type: string
collateral:
position:
type: object
properties:
denom:
type: string
amount:
address:
type: string
description: >-
Coin defines a token with a denomination and an amount.
collateral:
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an
amount.


NOTE: The amount field is an Int which implements the
custom method
NOTE: The amount field is an Int which implements
the custom method

signatures required by gogoproto.
liabilities:
type: string
title: For recording
interest_paid:
type: string
title: For recording
leverage:
type: string
title: For recording
leveraged_lp_amount:
type: string
position_health:
type: string
id:
type: string
format: uint64
amm_pool_id:
signatures required by gogoproto.
liabilities:
type: string
title: For recording
interest_paid:
type: string
title: For recording
leverage:
type: string
title: For recording
leveraged_lp_amount:
type: string
position_health:
type: string
id:
type: string
format: uint64
amm_pool_id:
type: string
format: uint64
stop_loss_price:
type: string
interest_rate_hour:
type: string
format: uint64
stop_loss_price:
interest_rate_hour_usd:
type: string
pagination:
type: object
Expand Down Expand Up @@ -44999,6 +45007,40 @@ paths:
format: int32
tags:
- Query
/elys-network/elys/tier/get_consolidated_price/{denom}:
get:
summary: Queries a list of GetConsolidatedPrice items.
operationId: ElysTierGetConsolidatedPrice
responses:
'200':
description: A successful response.
schema:
type: object
default:
description: An unexpected error response.
schema:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
'@type':
type: string
additionalProperties: {}
parameters:
- name: denom
in: path
required: true
type: string
tags:
- Query
/elys-network/elys/tier/leverage_lp_total/{user}:
get:
summary: Queries a list of LeverageLpTotal items.
Expand Down Expand Up @@ -87207,6 +87249,54 @@ definitions:
format: uint64
stop_loss_price:
type: string
elys.leveragelp.PositionAndInterest:
type: object
properties:
position:
type: object
properties:
address:
type: string
collateral:
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the custom
method

signatures required by gogoproto.
liabilities:
type: string
title: For recording
interest_paid:
type: string
title: For recording
leverage:
type: string
title: For recording
leveraged_lp_amount:
type: string
position_health:
type: string
id:
type: string
format: uint64
amm_pool_id:
type: string
format: uint64
stop_loss_price:
type: string
interest_rate_hour:
type: string
interest_rate_hour_usd:
type: string
elys.leveragelp.PositionResponse:
type: object
properties:
Expand Down Expand Up @@ -87333,43 +87423,50 @@ definitions:
items:
type: object
properties:
address:
type: string
collateral:
position:
type: object
properties:
denom:
type: string
amount:
address:
type: string
description: >-
Coin defines a token with a denomination and an amount.
collateral:
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the custom
method
NOTE: The amount field is an Int which implements the custom
method

signatures required by gogoproto.
liabilities:
type: string
title: For recording
interest_paid:
type: string
title: For recording
leverage:
type: string
title: For recording
leveraged_lp_amount:
type: string
position_health:
type: string
id:
type: string
format: uint64
amm_pool_id:
signatures required by gogoproto.
liabilities:
type: string
title: For recording
interest_paid:
type: string
title: For recording
leverage:
type: string
title: For recording
leveraged_lp_amount:
type: string
position_health:
type: string
id:
type: string
format: uint64
amm_pool_id:
type: string
format: uint64
stop_loss_price:
type: string
interest_rate_hour:
type: string
format: uint64
stop_loss_price:
interest_rate_hour_usd:
type: string
pagination:
type: object
Expand Down Expand Up @@ -89555,6 +89652,8 @@ definitions:
properties:
total:
type: string
elys.tier.QueryGetConsolidatedPriceResponse:
type: object
elys.tier.QueryGetPortfolioResponse:
type: object
properties:
Expand Down
14 changes: 14 additions & 0 deletions proto/elys/tier/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ service Query {
option (google.api.http).get = "/elys-network/elys/tier/get_amm_price/{denom}/{decimal}";

}

// Queries a list of GetConsolidatedPrice items.
rpc GetConsolidatedPrice (QueryGetConsolidatedPriceRequest) returns (QueryGetConsolidatedPriceResponse) {
option (google.api.http).get = "/elys-network/elys/tier/get_consolidated_price/{denom}";

}
}
// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}
Expand Down Expand Up @@ -158,3 +164,11 @@ message QueryGetAmmPriceResponse {
string total = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

message QueryGetConsolidatedPriceRequest {
string denom = 1;
}

message QueryGetConsolidatedPriceResponse {
string price = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

21 changes: 16 additions & 5 deletions x/leveragelp/keeper/position_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@ func (k Keeper) ForceCloseLong(ctx sdk.Context, position types.Position, pool ty
return sdk.ZeroInt(), types.ErrInvalidCloseSize
}

// Old debt
oldDebt := k.stableKeeper.GetDebt(ctx, position.GetPositionAddress())

if position.LeveragedLpAmount.IsZero() {
err := k.masterchefKeeper.ClaimRewards(ctx, position.GetPositionAddress(), []uint64{position.AmmPoolId}, sdk.AccAddress(position.Address))
if err != nil {
return sdk.ZeroInt(), err
}
err = k.DestroyPosition(ctx, position.Address, position.Id, oldDebt.Borrowed.Add(oldDebt.InterestStacked).Sub(oldDebt.InterestPaid))
if err != nil {
return sdk.ZeroInt(), err
}
}

// Exit liquidity with collateral token
exitCoins, err := k.amm.ExitPool(ctx, position.GetPositionAddress(), position.AmmPoolId, lpAmount, sdk.Coins{}, position.Collateral.Denom)
if err != nil {
return sdk.ZeroInt(), err
}

// Old debt
oldDebt := k.stableKeeper.GetDebt(ctx, position.GetPositionAddress())

// Repay with interest
debt := k.stableKeeper.UpdateInterestStackedByAddress(ctx, position.GetPositionAddress())

Expand Down Expand Up @@ -75,12 +86,12 @@ func (k Keeper) ForceCloseLong(ctx sdk.Context, position types.Position, pool ty
if err != nil {
return sdk.ZeroInt(), err
}
err = k.DestroyPosition(ctx, position.Address, position.Id, oldDebt.Borrowed.Add(debt.InterestStacked).Sub(debt.InterestPaid))
err = k.DestroyPosition(ctx, position.Address, position.Id, oldDebt.Borrowed.Add(oldDebt.InterestStacked).Sub(oldDebt.InterestPaid))
if err != nil {
return sdk.ZeroInt(), err
}
} else {
k.SetPosition(ctx, &position, oldDebt.Borrowed.Add(debt.InterestStacked).Sub(debt.InterestPaid))
k.SetPosition(ctx, &position, oldDebt.Borrowed.Add(oldDebt.InterestStacked).Sub(oldDebt.InterestPaid))
}

// Hooks after leveragelp position closed
Expand Down
2 changes: 2 additions & 0 deletions x/tier/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {

cmd.AddCommand(CmdGetAmmPrice())

cmd.AddCommand(CmdGetConsolidatedPrice())

// this line is used by starport scaffolding # 1

return cmd
Expand Down
46 changes: 46 additions & 0 deletions x/tier/client/cli/query_get_consolidated_price.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/elys-network/elys/x/tier/types"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdGetConsolidatedPrice() *cobra.Command {
cmd := &cobra.Command{
Use: "get-consolidated-price [denom]",
Short: "Query get-consolidated-price",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
reqDenom := args[0]

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryGetConsolidatedPriceRequest{

Denom: reqDenom,
}

res, err := queryClient.GetConsolidatedPrice(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Loading

0 comments on commit 47717bc

Please sign in to comment.