Skip to content

Commit

Permalink
Merge pull request #614 from elys-network/develop
Browse files Browse the repository at this point in the history
Bump
  • Loading branch information
Wgil authored Jun 24, 2024
2 parents 97fcc19 + d802dbd commit 22ce58f
Show file tree
Hide file tree
Showing 11 changed files with 828 additions and 150 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/rollback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Rollback Devnet
on:
workflow_dispatch:
inputs:
version:
description: "the version of blockchain to rollback"
type: string
required: true

jobs:
deploy:
runs-on: ubuntu-latest
environment: devnet
steps:
- name: Rollback Devnet
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: /bin/bash /home/ubuntu/ci-cd-devnet-backup-and-rollback/rollback.sh ${{inputs.version}}
47 changes: 47 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40864,6 +40864,48 @@ paths:
type: string
tags:
- Query
/elys-network/elys/leveragelp/liquidation_price/{position_id}:
get:
summary: Query a liquidation price for a position
operationId: ElysLeveragelpLiquidationPrice
responses:
'200':
description: A successful response.
schema:
type: object
properties:
price:
type: string
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: position_id
in: path
required: true
type: string
format: uint64
- name: address
in: query
required: false
type: string
tags:
- Query
/elys-network/elys/leveragelp/open_estimation:
get:
summary: Get estimated amount of return value opening a position
Expand Down Expand Up @@ -87202,6 +87244,11 @@ definitions:
type: string
leverage_max:
type: string
elys.leveragelp.QueryLiquidationPriceResponse:
type: object
properties:
price:
type: string
elys.leveragelp.QueryOpenEstResponse:
type: object
properties:
Expand Down
17 changes: 17 additions & 0 deletions proto/elys/leveragelp/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ service Query {
"/elys-network/elys/leveragelp/position/{address}/{id}";
}

// Query a liquidation price for a position
rpc LiquidationPrice (QueryLiquidationPriceRequest) returns (QueryLiquidationPriceResponse) {
option (google.api.http).get = "/elys-network/elys/leveragelp/liquidation_price/{position_id}";
}

// Get estimated amount of return value opening a position
rpc OpenEst(QueryOpenEstRequest) returns (QueryOpenEstResponse) {
option (google.api.http).get =
Expand Down Expand Up @@ -169,6 +174,18 @@ message PositionRequest {

message PositionResponse { Position position = 1; }

message QueryLiquidationPriceRequest {
string address = 1;
uint64 position_id = 2;
}

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

message QueryOpenEstRequest {
string collateral_asset = 1;
string collateral_amount = 2 [
Expand Down
15 changes: 14 additions & 1 deletion scripts/examples/leveragelp/leveragelp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,17 @@ elysd tx gov submit-proposal proposal.json --from=t2a --keyring-backend=test --c
],
"deposit": "10000000uelys"
}
```
```

elysd query leveragelp liquidation-price elys12tzylat4udvjj56uuhu3vj2n4vgp7cf9fwna9w 1
# price: "0.000000000000080000"

elysd query amm show-pool 1
# extra_info:
# lp_token_price: "1.000000000000000000"
# tvl: "110000.000000000000000000"
# pool:
# address: elys12857uh2f6fus3x3jh07cp5u7xfg2ypeec27dcp6864xhraa33mdscw660j
# pool_assets:
# - token:
# amount: "10000000000"
2 changes: 1 addition & 1 deletion scripts/examples/leveragelp/proposal.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"leverage_max": "10.0",
"max_open_positions": "9999",
"pool_open_threshold": "0.200000000000000000",
"safety_factor": "0.100000000000000000",
"safety_factor": "1.100000000000000000",
"whitelisting_enabled": false,
"epoch_length": "1"
}
Expand Down
1 change: 1 addition & 0 deletions x/leveragelp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdOpenEstimation())
cmd.AddCommand(CmdCloseEstimation())
cmd.AddCommand(CmdRewards())
cmd.AddCommand(CmdLiquidationPrice())

// this line is used by starport scaffolding # 1

Expand Down
55 changes: 55 additions & 0 deletions x/leveragelp/client/cli/query_liquidation_price.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cli

import (
"strconv"

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

func CmdLiquidationPrice() *cobra.Command {
cmd := &cobra.Command{
Use: "liquidation-price [address] [position_id]",
Short: "Query liquidation price",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
reqAddress := args[0]
_, err = sdk.AccAddressFromBech32(reqAddress)
if err != nil {
return err
}

reqId := args[1]
positionId, err := strconv.ParseUint(reqId, 10, 64)
if err != nil {
return err
}

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

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryLiquidationPriceRequest{
Address: reqAddress,
PositionId: positionId,
}

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

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
36 changes: 35 additions & 1 deletion x/leveragelp/keeper/query_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/leveragelp/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -18,8 +19,41 @@ func (k Keeper) Position(goCtx context.Context, req *types.PositionRequest) (*ty

position, err := k.GetPosition(ctx, req.Address, req.Id)
if err != nil {
return &types.PositionResponse{}, nil
return nil, err
}

return &types.PositionResponse{Position: &position}, nil
}

func (k Keeper) LiquidationPrice(goCtx context.Context, req *types.QueryLiquidationPriceRequest) (*types.QueryLiquidationPriceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

position, err := k.GetPosition(ctx, req.Address, req.PositionId)
if err != nil {
return nil, err
}

params := k.GetParams(ctx)

debt := k.stableKeeper.UpdateInterestStackedByAddress(ctx, position.GetPositionAddress())

// Ensure position.LeveragedLpAmount is not zero to avoid division by zero
if position.LeveragedLpAmount.IsZero() {
return nil, types.ErrAmountTooLow
}

// lpTokenPrice * lpTokenAmount / totalDebt = params.SafetyFactor
// lpTokenPrice = totalDebt * params.SafetyFactor / lpTokenAmount
totalDebt := debt.Borrowed.Add(debt.InterestStacked).Sub(debt.InterestPaid)
baseCurrency, _ := k.assetProfileKeeper.GetUsdcDenom(ctx)
usdcDenomPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, baseCurrency)
liquidationPrice := params.SafetyFactor.MulInt(totalDebt).Mul(usdcDenomPrice).MulInt(ammtypes.OneShare).QuoInt(position.LeveragedLpAmount)

return &types.QueryLiquidationPriceResponse{
Price: liquidationPrice,
}, nil
}
4 changes: 2 additions & 2 deletions x/leveragelp/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func NewParams() Params {
LeverageMax: sdk.NewDec(10),
EpochLength: (int64)(1),
MaxOpenPositions: (int64)(9999),
PoolOpenThreshold: sdk.NewDecWithPrec(2, 1), // 0.2
SafetyFactor: sdk.NewDecWithPrec(1, 1), // 0.1
PoolOpenThreshold: sdk.NewDecWithPrec(2, 1), // 0.2
SafetyFactor: sdk.NewDecWithPrec(11, 1), // 1.1
WhitelistingEnabled: false,
}
}
Expand Down
Loading

0 comments on commit 22ce58f

Please sign in to comment.