Skip to content

Commit

Permalink
feat(perp): Add position fields in ClosePosition response (#724)
Browse files Browse the repository at this point in the history
* Add fields to ClosePosition response

* Return fields in ClosePositionResp

* Update CHANGELOG.md

Co-authored-by: Agent Smith <agentsmith@matrixsystems.co>
  • Loading branch information
NibiruHeisenberg and AgentSmithMatrix authored Jul 22, 2022
1 parent e9b4dec commit 4ba111c
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### API Breaking

- [#721](https://github.com/NibiruChain/nibiru/pull/721) - Updated proto property names to adhere to standard snake_casing and added Unlock REST endpoint
- [#724](https://github.com/NibiruChain/nibiru/pull/724) - Add position fields in `ClosePositionResponse`.

### State Machine Breaking

Expand Down
24 changes: 24 additions & 0 deletions proto/perp/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,29 @@ message MsgClosePosition {
}

message MsgClosePositionResponse {
// The amount of quote assets exchanged.
string exchanged_notional_value = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// The amount of base assets exchanged.
string exchanged_position_size = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// The funding payment applied on this position change, measured in quote units.
string funding_payment = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// The amount of PnL realized on this position changed, measured in quote units.
string realized_pnl = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// The amount of margin the trader receives after closing the position, from the vault.
// Should never be negative.
string margin_to_trader = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];
}
13 changes: 11 additions & 2 deletions x/perp/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ func (m msgServer) ClosePosition(goCtx context.Context, position *types.MsgClose
traderAddr := sdk.MustAccAddressFromBech32(position.Sender)
tokenPair := common.MustNewAssetPair(position.TokenPair)

_, err := m.k.ClosePosition(ctx, tokenPair, traderAddr)
resp, err := m.k.ClosePosition(ctx, tokenPair, traderAddr)
if err != nil {
return nil, err
}

return &types.MsgClosePositionResponse{}, err
return &types.MsgClosePositionResponse{
ExchangedNotionalValue: resp.ExchangedNotionalValue,
ExchangedPositionSize: resp.ExchangedPositionSize,
FundingPayment: resp.FundingPayment,
RealizedPnl: resp.RealizedPnl,
MarginToTrader: resp.MarginToVault.Neg(),
}, nil
}

func (m msgServer) Liquidate(goCtx context.Context, msg *types.MsgLiquidate,
Expand Down
53 changes: 31 additions & 22 deletions x/perp/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,15 @@ func TestMsgServerClosePosition(t *testing.T) {
tests := []struct {
name string

pair string
sender string
pair common.AssetPair
traderAddr sdk.AccAddress

expectedErr error
}{
{
name: "success",
pair: common.PairBTCStable.String(),
sender: sample.AccAddress().String(),
pair: common.PairBTCStable,
traderAddr: sample.AccAddress(),
expectedErr: nil,
},
}
Expand All @@ -344,31 +344,35 @@ func TestMsgServerClosePosition(t *testing.T) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeper)

t.Log("create vpool")
app.VpoolKeeper.CreatePool(ctx, common.PairBTCStable, sdk.OneDec(), sdk.NewDec(1_000_000), sdk.NewDec(1_000_000), sdk.OneDec(), sdk.OneDec())
app.VpoolKeeper.CreatePool(
ctx,
common.PairBTCStable,
/* tradeLimitRatio */ sdk.OneDec(),
/* quoteAssetReserve */ sdk.NewDec(1_000_000),
/* baseAssetReserve */ sdk.NewDec(1_000_000),
/* fluctuationLimitRatio */ sdk.OneDec(),
/* maxOracleSpreadRatio */ sdk.OneDec(),
)
app.PerpKeeper.PairMetadataState(ctx).Set(&types.PairMetadata{
Pair: common.PairBTCStable,
CumulativePremiumFractions: []sdk.Dec{sdk.ZeroDec()},
})

pair, err := common.NewAssetPair(tc.pair)
traderAddr, err2 := sdk.AccAddressFromBech32(tc.sender)
if err == nil && err2 == nil {
t.Log("create position")
require.NoError(t, app.PerpKeeper.PositionsState(ctx).Create(&types.Position{
TraderAddress: traderAddr.String(),
Pair: pair,
Size_: sdk.OneDec(),
Margin: sdk.OneDec(),
OpenNotional: sdk.OneDec(),
LastUpdateCumulativePremiumFraction: sdk.ZeroDec(),
BlockNumber: 1,
}))
require.NoError(t, simapp.FundModuleAccount(app.BankKeeper, ctx, types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 1))))
}
t.Log("create position")
require.NoError(t, app.PerpKeeper.PositionsState(ctx).Create(&types.Position{
TraderAddress: tc.traderAddr.String(),
Pair: tc.pair,
Size_: sdk.OneDec(),
Margin: sdk.OneDec(),
OpenNotional: sdk.OneDec(),
LastUpdateCumulativePremiumFraction: sdk.ZeroDec(),
BlockNumber: 1,
}))
require.NoError(t, simapp.FundModuleAccount(app.BankKeeper, ctx, types.VaultModuleAccount, sdk.NewCoins(sdk.NewInt64Coin(tc.pair.GetQuoteTokenDenom(), 1))))

resp, err := msgServer.ClosePosition(sdk.WrapSDKContext(ctx), &types.MsgClosePosition{
Sender: tc.sender,
TokenPair: tc.pair,
Sender: tc.traderAddr.String(),
TokenPair: tc.pair.String(),
})

if tc.expectedErr != nil {
Expand All @@ -377,6 +381,11 @@ func TestMsgServerClosePosition(t *testing.T) {
} else {
require.NoError(t, err)
require.NotNil(t, resp)
assert.EqualValues(t, sdk.MustNewDecFromStr("0.999999000000999999"), resp.ExchangedNotionalValue)
assert.EqualValues(t, sdk.NewDec(-1), resp.ExchangedPositionSize)
assert.EqualValues(t, sdk.ZeroDec(), resp.FundingPayment)
assert.EqualValues(t, sdk.MustNewDecFromStr("0.999999000000999999"), resp.MarginToTrader)
assert.EqualValues(t, sdk.MustNewDecFromStr("-0.000000999999000001"), resp.RealizedPnl)
}
})
}
Expand Down
Loading

0 comments on commit 4ba111c

Please sign in to comment.