Skip to content

Commit

Permalink
fix: unit margin unit test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-elys committed Sep 14, 2023
1 parent 61945aa commit e1bb0cc
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 2 deletions.
4 changes: 2 additions & 2 deletions x/margin/keeper/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenRespons
}

// Check if it is the same direction position for the same trader.
if mtp := k.CheckSamePosition(ctx, msg); mtp != nil {
if mtp := k.OpenChecker.CheckSamePosition(ctx, msg); mtp != nil {
return k.OpenConsolidate(ctx, mtp, msg)
}

if err := k.CheckMaxOpenPositions(ctx); err != nil {
if err := k.OpenChecker.CheckMaxOpenPositions(ctx); err != nil {
return nil, err
}

Expand Down
13 changes: 13 additions & 0 deletions x/margin/keeper/open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,18 @@ func TestOpen_ErrorCheckMaxOpenPositions(t *testing.T) {
var (
ctx = sdk.Context{} // Mock or setup a context
msg = &types.MsgOpen{
Creator: "creator",
CollateralAsset: "aaa",
BorrowAsset: "bbb",
Position: types.Position_LONG,
Leverage: sdk.NewDec(10),
}
)

// Mock behavior
mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil)
mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil)
mockChecker.On("CheckSamePosition", ctx, msg).Return(nil)
mockChecker.On("CheckMaxOpenPositions", ctx).Return(sdkerrors.Wrap(types.ErrMaxOpenPositions, "cannot open new positions"))

_, err := k.Open(ctx, msg)
Expand All @@ -106,14 +110,18 @@ func TestOpen_ErrorPreparePools(t *testing.T) {
var (
ctx = sdk.Context{} // Mock or setup a context
msg = &types.MsgOpen{
Creator: "creator",
CollateralAsset: "aaa",
BorrowAsset: "bbb",
Position: types.Position_LONG,
Leverage: sdk.NewDec(10),
}
)

// Mock behavior
mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil)
mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil)
mockChecker.On("CheckSamePosition", ctx, msg).Return(nil)
mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil)
mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset)
mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(uint64(0), ammtypes.Pool{}, types.Pool{}, errors.New("error executing prepare pools"))
Expand Down Expand Up @@ -145,6 +153,7 @@ func TestOpen_ErrorCheckPoolHealth(t *testing.T) {
// Mock behavior
mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil)
mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil)
mockChecker.On("CheckSamePosition", ctx, msg).Return(nil)
mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil)
mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset)
mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil)
Expand Down Expand Up @@ -177,6 +186,7 @@ func TestOpen_ErrorInvalidPosition(t *testing.T) {
// Mock behavior
mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil)
mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil)
mockChecker.On("CheckSamePosition", ctx, msg).Return(nil)
mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil)
mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset)
mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil)
Expand Down Expand Up @@ -210,6 +220,7 @@ func TestOpen_ErrorOpenLong(t *testing.T) {
// Mock behavior
mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil)
mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil)
mockChecker.On("CheckSamePosition", ctx, msg).Return(nil)
mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil)
mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset)
mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil)
Expand Down Expand Up @@ -244,6 +255,7 @@ func TestOpen_ErrorOpenShort(t *testing.T) {
// Mock behavior
mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil)
mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil)
mockChecker.On("CheckSamePosition", ctx, msg).Return(nil)
mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil)
mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset)
mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil)
Expand Down Expand Up @@ -279,6 +291,7 @@ func TestOpen_Successful(t *testing.T) {
// Mock behavior
mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil)
mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil)
mockChecker.On("CheckSamePosition", ctx, msg).Return(nil)
mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil)
mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset)
mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil)
Expand Down
4 changes: 4 additions & 0 deletions x/margin/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type OpenChecker interface {
OpenLong(ctx sdk.Context, poolId uint64, msg *MsgOpen) (*MTP, error)
OpenShort(ctx sdk.Context, poolId uint64, msg *MsgOpen) (*MTP, error)
EmitOpenEvent(ctx sdk.Context, mtp *MTP)
SetMTP(ctx sdk.Context, mtp *MTP) error
CheckSamePosition(ctx sdk.Context, msg *MsgOpen) *MTP
GetOpenMTPCount(ctx sdk.Context) uint64
GetMaxOpenPositions(ctx sdk.Context) uint64
}

//go:generate mockery --srcpkg . --name OpenLongChecker --structname OpenLongChecker --filename open_long_checker.go --with-expecter
Expand Down
172 changes: 172 additions & 0 deletions x/margin/types/mocks/open_checker.go

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

0 comments on commit e1bb0cc

Please sign in to comment.