Skip to content

Commit

Permalink
feat: create settle position function
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt committed Oct 10, 2023
1 parent a7c9447 commit 2714bb7
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 46 deletions.
2 changes: 0 additions & 2 deletions x/perp/v2/client/cli/gen_market.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ func newMarketFromFlags(flagSet *flag.FlagSet,
return types.Market{}, types.AMM{}, err
}

fmt.Println(maxFundingRate)

priceMultiplier, err := sdk.NewDecFromStr(priceMultiplierStr)
if err != nil {
return types.Market{}, types.AMM{}, err
Expand Down
5 changes: 1 addition & 4 deletions x/perp/v2/integration/action/market.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package action

import (
"fmt"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -21,7 +19,6 @@ type logger struct {
}

func (e logger) Do(_ *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
fmt.Println(e.log)
return ctx, nil, true
}

Expand All @@ -38,7 +35,7 @@ type createMarketAction struct {
}

func (c createMarketAction) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
app.PerpKeeperV2.MarketLastVersion.Insert(ctx, c.Market.Pair, types.MarketLastVersion{Version: 1})
app.PerpKeeperV2.MarketLastVersion.Insert(ctx, c.Market.Pair, types.MarketLastVersion{Version: c.Market.Version})
app.PerpKeeperV2.SaveMarket(ctx, c.Market)
app.PerpKeeperV2.SaveAMM(ctx, c.AMM)

Expand Down
38 changes: 38 additions & 0 deletions x/perp/v2/integration/action/settlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,41 @@ func (c closeMarket) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error
func CloseMarket(pair asset.Pair) action.Action {
return closeMarket{pair: pair}
}

type settlePosition struct {
pair asset.Pair
version uint64
trader sdk.AccAddress
}

func (c settlePosition) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
_, err := app.PerpKeeperV2.SettlePosition(ctx, c.pair, c.version, c.trader)
if err != nil {
return ctx, err, false
}

return ctx, nil, true
}

func SettlePosition(pair asset.Pair, version uint64, trader sdk.AccAddress) action.Action {
return settlePosition{pair: pair, version: version, trader: trader}
}

type settlePositionShouldFail struct {
pair asset.Pair
version uint64
trader sdk.AccAddress
}

func (c settlePositionShouldFail) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
_, err := app.PerpKeeperV2.SettlePosition(ctx, c.pair, c.version, c.trader)
if err == nil {
return ctx, err, false
}

return ctx, nil, true
}

func SettlePositionShouldFail(pair asset.Pair, version uint64, trader sdk.AccAddress) action.Action {
return settlePositionShouldFail{pair: pair, version: version, trader: trader}
}
29 changes: 27 additions & 2 deletions x/perp/v2/integration/assertion/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,45 @@ func Position_PositionShouldBeEqualTo(expectedPosition types.Position) PositionC
type positionShouldNotExist struct {
Account sdk.AccAddress
Pair asset.Pair
Version uint64
}

func (p positionShouldNotExist) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
_, err := app.PerpKeeperV2.GetPosition(ctx, p.Pair, 1, p.Account)
_, err := app.PerpKeeperV2.GetPosition(ctx, p.Pair, p.Version, p.Account)
if err == nil {
return ctx, fmt.Errorf("position should not exist, but it does with pair %s", p.Pair), false
}

return ctx, nil, false
}

func PositionShouldNotExist(account sdk.AccAddress, pair asset.Pair) action.Action {
func PositionShouldNotExist(account sdk.AccAddress, pair asset.Pair, version uint64) action.Action {
return positionShouldNotExist{
Account: account,
Pair: pair,
Version: version,
}
}

type positionShouldExist struct {
Account sdk.AccAddress
Pair asset.Pair
Version uint64
}

func (p positionShouldExist) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
_, err := app.PerpKeeperV2.GetPosition(ctx, p.Pair, p.Version, p.Account)
if err != nil {
return ctx, fmt.Errorf("position should exist, but it does not with pair %s", p.Pair), false
}

return ctx, nil, false
}

func PositionShouldExist(account sdk.AccAddress, pair asset.Pair, version uint64) action.Action {
return positionShouldExist{
Account: account,
Pair: pair,
Version: version,
}
}
56 changes: 28 additions & 28 deletions x/perp/v2/keeper/clearing_house_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func TestMarketOrder(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("existing long position, decrease a bit").
Expand Down Expand Up @@ -870,7 +870,7 @@ func TestMarketOrder(t *testing.T) {
sdkerrors.ErrInsufficientFunds),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("new long position, can not open new position after market is not enabled").
Expand All @@ -886,7 +886,7 @@ func TestMarketOrder(t *testing.T) {
types.ErrMarketNotEnabled),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("market doesn't exist").
Expand All @@ -900,7 +900,7 @@ func TestMarketOrder(t *testing.T) {
types.ErrPairNotFound),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("zero quote asset amount").
Expand All @@ -915,7 +915,7 @@ func TestMarketOrder(t *testing.T) {
types.ErrInputQuoteAmtNegative),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("zero leverage").
Expand All @@ -930,7 +930,7 @@ func TestMarketOrder(t *testing.T) {
types.ErrUserLeverageNegative),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("user leverage greater than market max leverage").
Expand All @@ -945,7 +945,7 @@ func TestMarketOrder(t *testing.T) {
types.ErrLeverageIsTooHigh),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("position should not exist after opening a closing manually").
Expand All @@ -960,7 +960,7 @@ func TestMarketOrder(t *testing.T) {
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000_000_000), sdk.OneDec(), sdk.ZeroDec()),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("position should not exist after opening a closing manually - reverse with leverage").
Expand All @@ -975,7 +975,7 @@ func TestMarketOrder(t *testing.T) {
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),
TC("position should not exist after opening a closing manually - open with leverage").
Given(
Expand All @@ -989,7 +989,7 @@ func TestMarketOrder(t *testing.T) {
MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(100_000), sdk.OneDec(), sdk.ZeroDec()),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("position should not exist after opening a closing manually - reverse with leverage").
Expand All @@ -1004,7 +1004,7 @@ func TestMarketOrder(t *testing.T) {
MarketOrder(alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),

TC("position should not exist after opening a closing manually - reverse with leverage - more steps").
Expand All @@ -1020,7 +1020,7 @@ func TestMarketOrder(t *testing.T) {
MarketOrder(alice, pairBtcNusd, types.Direction_SHORT, sdk.NewInt(50_000), sdk.NewDec(2), sdk.ZeroDec()),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),
}

Expand Down Expand Up @@ -1649,7 +1649,7 @@ func TestClosePosition(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{
FinalPosition: types.Position{
Pair: pairBtcNusd,
Expand Down Expand Up @@ -1695,7 +1695,7 @@ func TestClosePosition(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{
FinalPosition: types.Position{
Pair: pairBtcNusd,
Expand Down Expand Up @@ -1743,7 +1743,7 @@ func TestClosePosition(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{
FinalPosition: types.Position{
TraderAddress: alice.String(),
Expand Down Expand Up @@ -1791,7 +1791,7 @@ func TestClosePosition(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{
FinalPosition: types.Position{
Pair: pairBtcNusd,
Expand Down Expand Up @@ -1837,7 +1837,7 @@ func TestClosePosition(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{
FinalPosition: types.Position{
Pair: pairBtcNusd,
Expand Down Expand Up @@ -1885,7 +1885,7 @@ func TestClosePosition(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
PositionChangedEventShouldBeEqual(&types.PositionChangedEvent{
FinalPosition: types.Position{
TraderAddress: alice.String(),
Expand Down Expand Up @@ -1937,7 +1937,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),
TC("only short position - no change to swap invariant").
Given(
Expand All @@ -1952,7 +1952,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
),
TC("only long position - increasing k").
Given(
Expand All @@ -1971,7 +1971,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()),
),
TC("only short position - increasing k").
Expand All @@ -1991,7 +1991,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.OneInt()))),
),

Expand All @@ -2012,7 +2012,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()),
),
TC("only short position - decreasing k").
Expand All @@ -2032,7 +2032,7 @@ func TestUpdateSwapInvariant(t *testing.T) {
ClosePosition(alice, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()),
),

Expand Down Expand Up @@ -2061,8 +2061,8 @@ func TestUpdateSwapInvariant(t *testing.T) {
ClosePosition(bob, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(bob, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
PositionShouldNotExist(bob, pairBtcNusd, 1),

ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()),
ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_782_394)))),
Expand Down Expand Up @@ -2092,8 +2092,8 @@ func TestUpdateSwapInvariant(t *testing.T) {
ClosePosition(bob, pairBtcNusd),
).
Then(
PositionShouldNotExist(alice, pairBtcNusd),
PositionShouldNotExist(bob, pairBtcNusd),
PositionShouldNotExist(alice, pairBtcNusd, 1),
PositionShouldNotExist(bob, pairBtcNusd, 1),

ModuleBalanceShouldBeEqualTo(types.VaultModuleAccount, sdk.NewCoins()),
ModuleBalanceShouldBeEqualTo(types.PerpEFModuleAccount, sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(39_200_810)))),
Expand Down
Loading

0 comments on commit 2714bb7

Please sign in to comment.