Skip to content

Commit

Permalink
feat: add asset price in position query (#807)
Browse files Browse the repository at this point in the history
* feat: add asset price in position query

* fix test

* fix

* fix

* update
  • Loading branch information
cryptokage1996 authored Sep 24, 2024
1 parent 34798ce commit 0cc4fbd
Show file tree
Hide file tree
Showing 9 changed files with 498 additions and 205 deletions.
16 changes: 12 additions & 4 deletions proto/elys/perpetual/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ service Query {

}
}

message MtpAndPrice {
MTP mtp =1;
string trading_asset_price = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
// ParamsRequest is request type for the Query/Params RPC method.
message ParamsRequest {}

Expand All @@ -102,7 +110,7 @@ message PositionsRequest {
}

message PositionsResponse {
repeated MTP mtps = 1;
repeated MtpAndPrice mtps = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

Expand All @@ -112,7 +120,7 @@ message PositionsByPoolRequest {
}

message PositionsByPoolResponse {
repeated MTP mtps = 1;
repeated MtpAndPrice mtps = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

Expand All @@ -129,7 +137,7 @@ message PositionsForAddressRequest {
}

message PositionsForAddressResponse {
repeated MTP mtps = 1;
repeated MtpAndPrice mtps = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

Expand Down Expand Up @@ -174,7 +182,7 @@ message MTPRequest {
}

message MTPResponse {
MTP mtp = 1;
MtpAndPrice mtp = 1;
}

message QueryOpenEstimationRequest {
Expand Down
4 changes: 2 additions & 2 deletions x/perpetual/client/cli/query_get_positions_by_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func TestShowMTPByPool(t *testing.T) {
}{
{
desc: "found",
ammPoolId: objs[0].AmmPoolId,
ammPoolId: objs[0].Mtp.AmmPoolId,

args: common,
obj: objs[0],
obj: objs[0].Mtp,
},
{
desc: "not found",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func TestShowMTPByAddress(t *testing.T) {
}{
{
desc: "found",
address: objs[0].Address,
address: objs[0].Mtp.Address,

args: common,
obj: objs[0],
obj: objs[0].Mtp,
},
{
desc: "not found",
Expand Down
75 changes: 39 additions & 36 deletions x/perpetual/client/cli/query_mtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,54 @@ import (
"github.com/elys-network/elys/x/perpetual/types"
)

func networkWithMTPObjects(t *testing.T, n int) (*network.Network, []*types.MTP) {
func networkWithMTPObjects(t *testing.T, n int) (*network.Network, []*types.MtpAndPrice) {
t.Helper()
app := simapp.InitElysTestApp(true)
ctx := app.BaseApp.NewContext(true, tmproto.Header{})
state := types.GenesisState{}

mtps := make([]*types.MTP, 0)
mtps := make([]*types.MtpAndPrice, 0)
// Generate n random accounts with 1000000stake balanced
addr := simapp.AddTestAddrs(app, ctx, n, sdk.NewInt(1000000))

cfg := network.DefaultConfig()
for i := 0; i < n; i++ {
mtp := types.MTP{
Address: addr[i].String(),
CollateralAsset: ptypes.BaseCurrency,
TradingAsset: "ATOM",
LiabilitiesAsset: ptypes.BaseCurrency,
CustodyAsset: "ATOM",
Collateral: sdk.NewInt(0),
Liabilities: sdk.NewInt(0),
BorrowInterestPaidCollateral: sdk.NewInt(0),
BorrowInterestPaidCustody: sdk.NewInt(0),
BorrowInterestUnpaidCollateral: sdk.NewInt(0),
Custody: sdk.NewInt(0),
TakeProfitLiabilities: sdk.NewInt(0),
TakeProfitCustody: sdk.NewInt(0),
Leverage: sdk.NewDec(0),
MtpHealth: sdk.NewDec(0),
Position: types.Position_LONG,
Id: (uint64)(i + 1),
AmmPoolId: (uint64)(i + 1),
ConsolidateLeverage: sdk.ZeroDec(),
SumCollateral: sdk.ZeroInt(),
TakeProfitPrice: sdk.MustNewDecFromStr(types.TakeProfitPriceDefault),
TakeProfitBorrowRate: sdk.OneDec(),
FundingFeePaidCollateral: sdk.NewInt(0),
FundingFeePaidCustody: sdk.NewInt(0),
FundingFeeReceivedCollateral: sdk.NewInt(0),
FundingFeeReceivedCustody: sdk.NewInt(0),
OpenPrice: sdk.NewDec(0),
StopLossPrice: sdk.NewDec(0),
mtp := types.MtpAndPrice{
Mtp: &types.MTP{
Address: addr[i].String(),
CollateralAsset: ptypes.BaseCurrency,
TradingAsset: "ATOM",
LiabilitiesAsset: ptypes.BaseCurrency,
CustodyAsset: "ATOM",
Collateral: sdk.NewInt(0),
Liabilities: sdk.NewInt(0),
BorrowInterestPaidCollateral: sdk.NewInt(0),
BorrowInterestPaidCustody: sdk.NewInt(0),
BorrowInterestUnpaidCollateral: sdk.NewInt(0),
Custody: sdk.NewInt(0),
TakeProfitLiabilities: sdk.NewInt(0),
TakeProfitCustody: sdk.NewInt(0),
Leverage: sdk.NewDec(0),
MtpHealth: sdk.NewDec(0),
Position: types.Position_LONG,
Id: (uint64)(i + 1),
AmmPoolId: (uint64)(i + 1),
ConsolidateLeverage: sdk.ZeroDec(),
SumCollateral: sdk.ZeroInt(),
TakeProfitPrice: sdk.MustNewDecFromStr(types.TakeProfitPriceDefault),
TakeProfitBorrowRate: sdk.OneDec(),
FundingFeePaidCollateral: sdk.NewInt(0),
FundingFeePaidCustody: sdk.NewInt(0),
FundingFeeReceivedCollateral: sdk.NewInt(0),
FundingFeeReceivedCustody: sdk.NewInt(0),
OpenPrice: sdk.NewDec(0),
StopLossPrice: sdk.NewDec(0),
},
TradingAssetPrice: sdk.NewDec(0),
}

mtps = append(mtps, &mtp)
state.MtpList = append(state.MtpList, mtp)
state.MtpList = append(state.MtpList, *mtp.Mtp)
}

buf, err := cfg.Codec.MarshalJSON(&state)
Expand All @@ -87,19 +90,19 @@ func TestShowMTP(t *testing.T) {

args []string
err error
obj *types.MTP
obj *types.MtpAndPrice
}{
{
desc: "found",
addr: objs[0].Address,
idIndex: objs[0].Id,
addr: objs[0].Mtp.Address,
idIndex: objs[0].Mtp.Id,

args: common,
obj: objs[0],
},
{
desc: "not found",
addr: objs[0].Address,
addr: objs[0].Mtp.Address,
idIndex: (uint64)(100000),

args: common,
Expand Down
4 changes: 2 additions & 2 deletions x/perpetual/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type (
parameterKeeper *pkeeper.Keeper
amm types.AmmKeeper
bankKeeper types.BankKeeper
oracleKeeper ammtypes.OracleKeeper
oracleKeeper types.OracleKeeper
assetProfileKeeper types.AssetProfileKeeper

hooks types.PerpetualHooks
Expand All @@ -46,7 +46,7 @@ func NewKeeper(
authority string,
amm types.AmmKeeper,
bk types.BankKeeper,
oracleKeeper ammtypes.OracleKeeper,
oracleKeeper types.OracleKeeper,
assetProfileKeeper types.AssetProfileKeeper,
parameterKeeper *pkeeper.Keeper,
) *Keeper {
Expand Down
53 changes: 44 additions & 9 deletions x/perpetual/keeper/mtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (k Keeper) GetAllLegacyMTPs(ctx sdk.Context) []types.LegacyMTP {
return mtpList
}

func (k Keeper) GetMTPs(ctx sdk.Context, pagination *query.PageRequest) ([]*types.MTP, *query.PageResponse, error) {
var mtpList []*types.MTP
func (k Keeper) GetMTPs(ctx sdk.Context, pagination *query.PageRequest) ([]*types.MtpAndPrice, *query.PageResponse, error) {
var mtpList []*types.MtpAndPrice
store := ctx.KVStore(k.storeKey)
mtpStore := prefix.NewStore(store, types.MTPPrefix)

Expand Down Expand Up @@ -167,15 +167,27 @@ func (k Keeper) GetMTPs(ctx sdk.Context, pagination *query.PageRequest) ([]*type
mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)
}

mtpList = append(mtpList, &mtp)
info, found := k.oracleKeeper.GetAssetInfo(ctx, mtp.TradingAsset)
if !found {
return fmt.Errorf("asset not found")
}
trading_asset_price, found := k.oracleKeeper.GetAssetPrice(ctx, info.Display)
if !found {
return fmt.Errorf("asset price not found")
}

mtpList = append(mtpList, &types.MtpAndPrice{
Mtp: &mtp,
TradingAssetPrice: trading_asset_price.Price,
})
return nil
})

return mtpList, pageRes, err
}

func (k Keeper) GetMTPsForPool(ctx sdk.Context, ammPoolId uint64, pagination *query.PageRequest) ([]*types.MTP, *query.PageResponse, error) {
var mtps []*types.MTP
func (k Keeper) GetMTPsForPool(ctx sdk.Context, ammPoolId uint64, pagination *query.PageRequest) ([]*types.MtpAndPrice, *query.PageResponse, error) {
var mtps []*types.MtpAndPrice

store := ctx.KVStore(k.storeKey)
mtpStore := prefix.NewStore(store, types.MTPPrefix)
Expand Down Expand Up @@ -212,7 +224,18 @@ func (k Keeper) GetMTPsForPool(ctx sdk.Context, ammPoolId uint64, pagination *qu
mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)
}

mtps = append(mtps, &mtp)
info, found := k.oracleKeeper.GetAssetInfo(ctx, mtp.TradingAsset)
if !found {
return false, fmt.Errorf("asset not found")
}
trading_asset_price, found := k.oracleKeeper.GetAssetPrice(ctx, info.Display)
if !found {
return false, fmt.Errorf("asset price not found")
}
mtps = append(mtps, &types.MtpAndPrice{
Mtp: &mtp,
TradingAssetPrice: trading_asset_price.Price,
})
return true, nil
}

Expand All @@ -239,8 +262,8 @@ func (k Keeper) GetAllMTPsForAddress(ctx sdk.Context, mtpAddress sdk.AccAddress)
return mtps
}

func (k Keeper) GetMTPsForAddressWithPagination(ctx sdk.Context, mtpAddress sdk.AccAddress, pagination *query.PageRequest) ([]*types.MTP, *query.PageResponse, error) {
var mtps []*types.MTP
func (k Keeper) GetMTPsForAddressWithPagination(ctx sdk.Context, mtpAddress sdk.AccAddress, pagination *query.PageRequest) ([]*types.MtpAndPrice, *query.PageResponse, error) {
var mtps []*types.MtpAndPrice

store := ctx.KVStore(k.storeKey)
mtpStore := prefix.NewStore(store, types.GetMTPPrefixForAddress(mtpAddress))
Expand Down Expand Up @@ -279,7 +302,19 @@ func (k Keeper) GetMTPsForAddressWithPagination(ctx sdk.Context, mtpAddress sdk.
mtp.BorrowInterestUnpaidCollateral = k.GetBorrowInterest(ctx, &mtp, ammPool).Add(mtp.BorrowInterestUnpaidCollateral)
}

mtps = append(mtps, &mtp)
info, found := k.oracleKeeper.GetAssetInfo(ctx, mtp.TradingAsset)
if !found {
return fmt.Errorf("asset not found")
}
trading_asset_price, found := k.oracleKeeper.GetAssetPrice(ctx, info.Display)
if !found {
return fmt.Errorf("asset price not found")
}

mtps = append(mtps, &types.MtpAndPrice{
Mtp: &mtp,
TradingAssetPrice: trading_asset_price.Price,
})
return nil
})
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion x/perpetual/keeper/query_mtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/perpetual/types"
Expand All @@ -25,5 +26,14 @@ func (k Keeper) MTP(goCtx context.Context, req *types.MTPRequest) (*types.MTPRes
return &types.MTPResponse{}, err
}

return &types.MTPResponse{Mtp: &mtp}, nil
info, found := k.oracleKeeper.GetAssetInfo(ctx, mtp.TradingAsset)
if !found {
return nil, fmt.Errorf("asset not found")
}
trading_asset_price, found := k.oracleKeeper.GetAssetPrice(ctx, info.Display)
if !found {
return nil, fmt.Errorf("asset price not found")
}

return &types.MTPResponse{Mtp: &types.MtpAndPrice{Mtp: &mtp, TradingAssetPrice: trading_asset_price.Price}}, nil
}
8 changes: 8 additions & 0 deletions x/perpetual/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
atypes "github.com/elys-network/elys/x/assetprofile/types"
oracletypes "github.com/elys-network/elys/x/oracle/types"
)

//go:generate mockery --srcpkg . --name AuthorizationChecker --structname AuthorizationChecker --filename authorization_checker.go --with-expecter
Expand Down Expand Up @@ -186,3 +187,10 @@ type AssetProfileKeeper interface {
// GetEntryByDenom returns a entry from its denom value
GetEntryByDenom(ctx sdk.Context, denom string) (val atypes.Entry, found bool)
}

type OracleKeeper interface {
GetAssetPrice(ctx sdk.Context, asset string) (oracletypes.Price, bool)
GetAssetPriceFromDenom(ctx sdk.Context, denom string) sdk.Dec
GetPriceFeeder(ctx sdk.Context, feeder sdk.AccAddress) (val oracletypes.PriceFeeder, found bool)
GetAssetInfo(ctx sdk.Context, denom string) (val oracletypes.AssetInfo, found bool)
}
Loading

0 comments on commit 0cc4fbd

Please sign in to comment.