Skip to content

Commit

Permalink
[Perpetual]: Handle payments for users (#805)
Browse files Browse the repository at this point in the history
* handle topay payments

* fix

* add query

* query

* mtp

* remove

* fix

* fix

* move

* move
  • Loading branch information
amityadav0 committed Sep 20, 2024
1 parent c9f0add commit aa9ced8
Show file tree
Hide file tree
Showing 13 changed files with 730 additions and 146 deletions.
34 changes: 31 additions & 3 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43509,6 +43509,35 @@ paths:
additionalProperties: {}
tags:
- Query
/elys-network/elys/perpetual/get_all_to_pay:
get:
summary: Queries a list of GetAllToPay items.
operationId: ElysPerpetualGetAllToPay
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: {}
tags:
- Query
/elys-network/elys/perpetual/is-whitelisted:
get:
summary: Is an address whitelisted?
Expand Down Expand Up @@ -46098,7 +46127,6 @@ paths:
properties:
market_order_enabled:
type: boolean
title: 'TODO: Add gov proposal message to set this values'
stake_enabled:
type: boolean
process_orders_enabled:
Expand Down Expand Up @@ -90621,6 +90649,8 @@ definitions:
repeated Bar results = 1;
PageResponse page = 2;
}
elys.perpetual.QueryGetAllToPayResponse:
type: object
elys.perpetual.QueryGetPoolResponse:
type: object
properties:
Expand Down Expand Up @@ -91365,7 +91395,6 @@ definitions:
properties:
market_order_enabled:
type: boolean
title: 'TODO: Add gov proposal message to set this values'
stake_enabled:
type: boolean
process_orders_enabled:
Expand Down Expand Up @@ -91793,7 +91822,6 @@ definitions:
properties:
market_order_enabled:
type: boolean
title: 'TODO: Add gov proposal message to set this values'
stake_enabled:
type: boolean
process_orders_enabled:
Expand Down
83 changes: 51 additions & 32 deletions proto/elys/perpetual/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,35 @@ option go_package = "github.com/elys-network/elys/x/perpetual/types";

// Query defines the gRPC querier service.
service Query {

// Parameters queries the parameters of the module.
rpc Params (ParamsRequest) returns (ParamsResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/params";

}

// Queries a list of positions.
rpc GetPositions (PositionsRequest) returns (PositionsResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/positions/{pagination.key}";

}

// Queries a list of mtp positions by pool.
rpc GetPositionsByPool (PositionsByPoolRequest) returns (PositionsByPoolResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/mtps-by-pool/{amm_pool_id}/{pagination.key}";

}

// Retuns the total number of open and lifetime mtps.
rpc GetStatus (StatusRequest) returns (StatusResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/status";

}

// Queries a list of mtp positions for a given address.
rpc GetPositionsForAddress (PositionsForAddressRequest) returns (PositionsForAddressResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/mtps-for-address/{address}/{pagination.key}";

}

// Queries a list of whitelisted addresses.
Expand All @@ -48,33 +54,45 @@ service Query {
// Is an address whitelisted?
rpc IsWhitelisted (IsWhitelistedRequest) returns (IsWhitelistedResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/is-whitelisted";

}

// Queries a single pool given its index.
rpc Pool (QueryGetPoolRequest) returns (QueryGetPoolResponse) {
rpc Pool (QueryGetPoolRequest) returns (QueryGetPoolResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/pool/{index}";

}

// Queries a list of all pools.
rpc Pools (QueryAllPoolRequest) returns (QueryAllPoolResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/pool/{pagination.key}";

}

// Queries a single mtp position given its address and id.
rpc MTP (MTPRequest) returns (MTPResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/mtp/{address}/{id}";

}

// Queries an estimation of a new open position details.
rpc OpenEstimation (QueryOpenEstimationRequest) returns (QueryOpenEstimationResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/open-estimation";

}

// Queries a list of GetAllToPay items.
rpc GetAllToPay (QueryGetAllToPayRequest) returns (QueryGetAllToPayResponse) {
option (google.api.http).get = "/elys-network/elys/perpetual/get_all_to_pay";

}
}
// ParamsRequest is request type for the Query/Params RPC method.
message ParamsRequest {}

// ParamsResponse is response type for the Query/Params RPC method.
message ParamsResponse {

// params holds all the parameters of this module.
Params params = 1 [(gogoproto.nullable) = false];
}
Expand Down Expand Up @@ -160,39 +178,40 @@ message MTPResponse {
}

message QueryOpenEstimationRequest {
Position position = 1;
string leverage = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string trading_asset = 3;
cosmos.base.v1beta1.Coin collateral = 4 [(gogoproto.nullable) = false ] ;
string discount = 5 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string take_profit_price = 6 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
Position position = 1;
string leverage = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string trading_asset = 3;
cosmos.base.v1beta1.Coin collateral = 4 [(gogoproto.nullable) = false ] ;
string discount = 5 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string take_profit_price = 6 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

message QueryOpenEstimationResponse {
Position position = 1;
string leverage = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string trading_asset = 3;
cosmos.base.v1beta1.Coin collateral = 4 [(gogoproto.nullable) = false ] ;
cosmos.base.v1beta1.Coin min_collateral = 5 [(gogoproto.nullable) = false ] ;
bool valid_collateral = 6;
cosmos.base.v1beta1.Coin position_size = 7 [(gogoproto.nullable) = false ] ;
string swap_fee = 8 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string discount = 9 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string open_price = 10 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string take_profit_price = 11 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string liquidation_price = 12 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string estimated_pnl = 13 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
string estimated_pnl_denom = 14;
Position position = 1;
string leverage = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string trading_asset = 3;
cosmos.base.v1beta1.Coin collateral = 4 [(gogoproto.nullable) = false ] ;
cosmos.base.v1beta1.Coin min_collateral = 5 [(gogoproto.nullable) = false ] ;
bool valid_collateral = 6;
cosmos.base.v1beta1.Coin position_size = 7 [(gogoproto.nullable) = false ] ;
string swap_fee = 8 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string discount = 9 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string open_price = 10 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string take_profit_price = 11 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string liquidation_price = 12 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string estimated_pnl = 13 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
string estimated_pnl_denom = 14;
cosmos.base.v1beta1.Coin available_liquidity = 15 [(gogoproto.nullable) = false ] ;
string slippage = 16 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string slippage = 16 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string weight_balance_ratio = 17 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string borrow_interest_rate = 18 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string funding_rate = 19 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string price_impact = 20 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string borrow_interest_rate = 18 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string funding_rate = 19 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string price_impact = 20 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

message QueryGetAllToPayRequest {}

message QueryGetAllToPayResponse {
repeated ToPay to_pay = 1;
}

2 changes: 2 additions & 0 deletions x/perpetual/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdMtp())
cmd.AddCommand(CmdOpenEstimation())

cmd.AddCommand(CmdGetAllToPay())

// this line is used by starport scaffolding # 1

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

import (
"strconv"

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

var _ = strconv.Itoa(0)

func CmdGetAllToPay() *cobra.Command {
cmd := &cobra.Command{
Use: "get-all-to-pay",
Short: "Query get-all-to-pay",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {

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

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryGetAllToPayRequest{}

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

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
6 changes: 6 additions & 0 deletions x/perpetual/keeper/msg_server_close_positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ func (k msgServer) ClosePositions(goCtx context.Context, msg *types.MsgClosePosi
return nil, nil
}

// Handle toPay
err := k.HandleToPay(ctx)
if err != nil {
ctx.Logger().Error(err.Error())
}

// Handle liquidations
liqLog := []string{}
for _, val := range msg.Liquidate {
Expand Down
42 changes: 33 additions & 9 deletions x/perpetual/keeper/mtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func (k Keeper) GetMTP(ctx sdk.Context, mtpAddress sdk.AccAddress, id uint64) (t
}
baseCurrency := entry.Denom

mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)

mtpHealth, err := k.GetMTPHealth(ctx, mtp, ammPool, baseCurrency)
if err == nil {
mtp.MtpHealth = mtpHealth
}

mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)

return mtp, nil
}

Expand Down Expand Up @@ -159,12 +159,12 @@ func (k Keeper) GetMTPs(ctx sdk.Context, pagination *query.PageRequest) ([]*type
}

if realTime {
mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)

mtpHealth, err := k.GetMTPHealth(ctx, mtp, ammPool, baseCurrency)
if err == nil {
mtp.MtpHealth = mtpHealth
}

mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)
}

mtpList = append(mtpList, &mtp)
Expand Down Expand Up @@ -204,12 +204,12 @@ func (k Keeper) GetMTPsForPool(ctx sdk.Context, ammPoolId uint64, pagination *qu
if accumulate && mtp.AmmPoolId == ammPoolId {
if realTime {
// Interest
mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)

mtpHealth, err := k.GetMTPHealth(ctx, mtp, ammPool, baseCurrency)
if err == nil {
mtp.MtpHealth = mtpHealth
}

mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)
}

mtps = append(mtps, &mtp)
Expand Down Expand Up @@ -271,12 +271,12 @@ func (k Keeper) GetMTPsForAddressWithPagination(ctx sdk.Context, mtpAddress sdk.
}

if realTime {
mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)

mtpHealth, err := k.GetMTPHealth(ctx, mtp, ammPool, baseCurrency)
if err == nil {
mtp.MtpHealth = mtpHealth
}

mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)
}

mtps = append(mtps, &mtp)
Expand Down Expand Up @@ -323,7 +323,6 @@ func (k Keeper) GetOpenMTPCount(ctx sdk.Context) uint64 {
return count
}

// TODO: Handle to pay with a claim message or in begin blocker
func (k Keeper) SetToPay(ctx sdk.Context, toPay *types.ToPay) error {
store := ctx.KVStore(k.storeKey)
address := sdk.MustAccAddressFromBech32(toPay.Address)
Expand All @@ -333,6 +332,31 @@ func (k Keeper) SetToPay(ctx sdk.Context, toPay *types.ToPay) error {
return nil
}

func (k Keeper) GetAllToPayStore(ctx sdk.Context) []types.ToPay {
var toPays []types.ToPay
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.ToPayPrefix)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var toPay types.ToPay
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &toPay)
toPays = append(toPays, toPay)
}
return toPays
}

func (k Keeper) DeleteToPay(ctx sdk.Context, address sdk.AccAddress, id uint64) error {
store := ctx.KVStore(k.storeKey)
key := types.GetToPayKey(address, id)
if !store.Has(key) {
return types.ErrToPayDoesNotExist
}
store.Delete(key)
return nil
}

func (k Keeper) DeleteLegacyMTP(ctx sdk.Context, mtpaddress string, id uint64) error {
store := ctx.KVStore(k.storeKey)
key := types.GetMTPKey(sdk.MustAccAddressFromBech32(mtpaddress), id)
Expand Down
Loading

0 comments on commit aa9ced8

Please sign in to comment.