Skip to content

Commit

Permalink
test(perp): TestTransferFee (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Jun 29, 2022
1 parent 415e07a commit bb2f7e8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
1 change: 0 additions & 1 deletion x/perp/keeper/clearing_house.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ func (k Keeper) ClosePosition(ctx sdk.Context, pair common.AssetPair, addr sdk.A
return positionResp, nil
}

// TODO test: transferFee | https://github.com/NibiruChain/nibiru/issues/299
func (k Keeper) transferFee(
ctx sdk.Context,
pair common.AssetPair,
Expand Down
75 changes: 75 additions & 0 deletions x/perp/keeper/clearing_house_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2160,6 +2160,81 @@ func TestCloseAndOpenReversePosition(t *testing.T) {
}
}

func TestTransferFee(t *testing.T) {
setup := func() (
k Keeper, mocks mockedDependencies, ctx sdk.Context, pair common.AssetPair,
trader sdk.AccAddress, positionNotional sdk.Dec,
) {
perpKeeper, mocks, ctx := getKeeper(t)
pair = common.MustNewAssetPair("btc:usdc")
perpKeeper.SetParams(ctx, types.DefaultParams())
metadata := &types.PairMetadata{
Pair: pair.String(),
}
perpKeeper.PairMetadataState(ctx).Set(metadata)
trader = sample.AccAddress()
positionNotional = sdk.NewDec(5_000)
return perpKeeper, mocks, ctx, pair, trader, positionNotional
}

t.Run("trader has funds - happy",
func(t *testing.T) {
k, mocks, ctx, pair, trader, positionNotional := setup()

var wantError error = nil
mocks.mockBankKeeper.EXPECT().SendCoinsFromAccountToModule(
ctx, trader, types.FeePoolModuleAccount,
sdk.NewCoins(sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 5)),
).Return(wantError)
mocks.mockBankKeeper.EXPECT().SendCoinsFromAccountToModule(
ctx, trader, types.PerpEFModuleAccount,
sdk.NewCoins(sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 5)),
).Return(wantError)

_, err := k.transferFee(
ctx, pair, trader, positionNotional)
require.NoError(t, err)
})

t.Run("not enough funds for Perp Ecosystem Fund (spread) - error",
func(t *testing.T) {
k, mocks, ctx, pair, trader, positionNotional := setup()

mocks.mockBankKeeper.EXPECT().SendCoinsFromAccountToModule(
ctx, trader, types.FeePoolModuleAccount,
sdk.NewCoins(sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 5)),
).Return(nil)

expectedError := fmt.Errorf(
"trader missing funds for %s", types.PerpEFModuleAccount)
mocks.mockBankKeeper.EXPECT().SendCoinsFromAccountToModule(
ctx, trader, types.PerpEFModuleAccount,
sdk.NewCoins(sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 5))).
Return(expectedError)
_, err := k.transferFee(
ctx, pair, trader, positionNotional)
require.ErrorContains(t, err, expectedError.Error())
})

t.Run("not enough funds for Fee Pool (toll) - error",
func(t *testing.T) {
k, mocks, ctx, pair, trader, positionNotional := setup()

expectedError := fmt.Errorf(
"trader missing funds for %s", types.FeePoolModuleAccount)
mocks.mockBankKeeper.EXPECT().SendCoinsFromAccountToModule(
ctx,
/* from */ trader,
/* to */ types.FeePoolModuleAccount,
sdk.NewCoins(sdk.NewInt64Coin(pair.GetQuoteTokenDenom(), 5)),
).Return(expectedError)

_, err := k.transferFee(
ctx, pair, trader, positionNotional)
require.ErrorContains(t, err, expectedError.Error())
})
}

func TestClosePosition(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit bb2f7e8

Please sign in to comment.