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

feat(perp): expose 'marginRatioIndex' and block number on QueryTraderPosition #810

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Features

* [#791](https://github.com/NibiruChain/nibiru/pull/791) Add the x/oracle module
* [#810](https://github.com/NibiruChain/nibiru/pull/810) - feat(x/perp): expose 'marginRatioIndex' and block number on QueryTraderPosition

## [v0.12.1](https://github.com/NibiruChain/nibiru/releases/tag/v0.12.1) - 2022-08-04

Expand Down
17 changes: 14 additions & 3 deletions proto/perp/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ service Query {
option (google.api.http).get = "/nibiru/perp/params";
}

rpc TraderPosition(QueryTraderPositionRequest) returns (QueryTraderPositionResponse) {
rpc QueryTraderPosition(QueryTraderPositionRequest) returns (QueryTraderPositionResponse) {
option (google.api.http).get = "/nibiru/perp/trader_position";
}
}
Expand Down Expand Up @@ -54,10 +54,21 @@ message QueryTraderPositionResponse {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// The position's margin ratio, calculated from margin, unrealized PnL, and position notional.
string margin_ratio = 4 [
/* margin ratio of the position based on the mark price, mark TWAP. The higher
value of the possible margin ratios (TWAP and instantaneous) is taken to be
'marginRatioMark'. Calculated from margin, unrealized PnL, and position notional. */
string margin_ratio_mark = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

/* margin ratio of the position based on the index price. Calculated from margin,
unrealized PnL, and position notional. */
string margin_ratio_index = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// BlockNumber is current block number at the time of query.
int64 block_number = 7;
}

// ---------------------------------------- OtherQuery
2 changes: 1 addition & 1 deletion x/oracle/types/oracle.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions x/perp/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func (s *IntegrationTestSuite) TearDownSuite() {
s.network.Cleanup()
}

// TODO test: Include checks for queryResp.MarginRatioIndex
// https://github.com/NibiruChain/nibiru/issues/809
func (s *IntegrationTestSuite) TestOpenPositionsAndCloseCmd() {
val := s.network.Validators[0]

Expand Down Expand Up @@ -200,7 +202,7 @@ func (s *IntegrationTestSuite) TestOpenPositionsAndCloseCmd() {
s.EqualValues(sdk.NewDec(1_000_000), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("999999.999999999999999359"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("-0.000000000000000641"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.NewDec(1), queryResp.MarginRatio)
s.EqualValues(sdk.NewDec(1), queryResp.MarginRatioMark)

s.T().Log("C. open position with 2x leverage and zero baseAmtLimit")
args = []string{
Expand All @@ -226,7 +228,7 @@ func (s *IntegrationTestSuite) TestOpenPositionsAndCloseCmd() {
s.EqualValues(sdk.NewDec(3_000_000), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("3000000.000000000000000938"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("0.000000000000000938"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("0.666666666666666667"), queryResp.MarginRatio)
s.EqualValues(sdk.MustNewDecFromStr("0.666666666666666667"), queryResp.MarginRatioMark)

s.T().Log("D. Open a reverse position smaller than the existing position")
args = []string{
Expand Down Expand Up @@ -260,7 +262,7 @@ func (s *IntegrationTestSuite) TestOpenPositionsAndCloseCmd() {
s.EqualValues(sdk.NewDec(2_999_900), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("2999899.999999999999999506"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("-0.000000000000000494"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("0.666688889629654322"), queryResp.MarginRatio)
s.EqualValues(sdk.MustNewDecFromStr("0.666688889629654322"), queryResp.MarginRatioMark)

s.T().Log("E. Open a reverse position larger than the existing position")
args = []string{
Expand Down Expand Up @@ -288,7 +290,7 @@ func (s *IntegrationTestSuite) TestOpenPositionsAndCloseCmd() {
s.EqualValues(sdk.MustNewDecFromStr("1000099.999999999999999651"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("0.000000000000000843"), queryResp.UnrealizedPnl)
// there is a random delta due to twap margin ratio calculation and random block times in the in-process network
s.InDelta(1, queryResp.MarginRatio.MustFloat64(), 0.001)
s.InDelta(1, queryResp.MarginRatioMark.MustFloat64(), 0.001)

s.T().Log("F. Close position")
args = []string{
Expand Down
2 changes: 1 addition & 1 deletion x/perp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func CmdQueryPosition() *cobra.Command {
return err
}

res, err := queryClient.TraderPosition(
res, err := queryClient.QueryTraderPosition(
cmd.Context(), &types.QueryTraderPositionRequest{
Trader: trader.String(),
TokenPair: tokenPair.String(),
Expand Down
14 changes: 11 additions & 3 deletions x/perp/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewQuerier(k Keeper) queryServer {

var _ types.QueryServer = queryServer{}

func (q queryServer) TraderPosition(
func (q queryServer) QueryTraderPosition(
goCtx context.Context, req *types.QueryTraderPositionRequest,
) (*types.QueryTraderPositionResponse, error) {
if req == nil {
Expand Down Expand Up @@ -49,16 +49,24 @@ func (q queryServer) TraderPosition(
return nil, err
}

marginRatio, err := q.Keeper.GetMarginRatio(ctx, *position, types.MarginCalculationPriceOption_MAX_PNL)
marginRatioMark, err := q.Keeper.GetMarginRatio(ctx, *position, types.MarginCalculationPriceOption_MAX_PNL)
if err != nil {
return nil, err
}
marginRatioIndex, err := q.Keeper.GetMarginRatio(ctx, *position, types.MarginCalculationPriceOption_INDEX)
if err != nil {
// The index portion of the query fails silently as not to distrupt all
// position queries when oracles aren't posting prices.
q.Keeper.Logger(ctx).Error(err.Error())
}

return &types.QueryTraderPositionResponse{
Position: position,
PositionNotional: positionNotional,
UnrealizedPnl: unrealizedPnl,
MarginRatio: marginRatio,
MarginRatioMark: marginRatioMark,
MarginRatioIndex: marginRatioIndex,
BlockNumber: ctx.BlockHeight(),
}, nil
}

Expand Down
6 changes: 4 additions & 2 deletions x/perp/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestQueryPosition(t *testing.T) {

t.Log("query position")
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Second))
resp, err := queryServer.TraderPosition(
resp, err := queryServer.QueryTraderPosition(
sdk.WrapSDKContext(ctx),
&types.QueryTraderPositionRequest{
Trader: traderAddr.String(),
Expand All @@ -131,7 +131,9 @@ func TestQueryPosition(t *testing.T) {

assert.Equal(t, tc.expectedPositionNotional, resp.PositionNotional)
assert.Equal(t, tc.expectedUnrealizedPnl, resp.UnrealizedPnl)
assert.Equal(t, tc.expectedMarginRatio, resp.MarginRatio)
assert.Equal(t, tc.expectedMarginRatio, resp.MarginRatioMark)
// assert.Equal(t, tc.expectedMarginRatioIndex, resp.MarginRatioIndex)
// TODO https://github.com/NibiruChain/nibiru/issues/809
})
}
}
Loading