From 25284748ab087b6541874037996d4f7b314a9356 Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Thu, 14 Sep 2023 07:42:36 +0000 Subject: [PATCH] fix: unit tests in close long, short and open long --- .../keeper/check_same_asset_position_test.go | 15 +++----- x/margin/keeper/close_long_test.go | 34 ++++++++++++------- x/margin/keeper/close_short_test.go | 32 ++++++++++------- x/margin/keeper/open_long_test.go | 6 ++-- 4 files changed, 49 insertions(+), 38 deletions(-) diff --git a/x/margin/keeper/check_same_asset_position_test.go b/x/margin/keeper/check_same_asset_position_test.go index b4960374a..1c00ed10d 100644 --- a/x/margin/keeper/check_same_asset_position_test.go +++ b/x/margin/keeper/check_same_asset_position_test.go @@ -3,24 +3,20 @@ package keeper_test import ( "testing" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/elys-network/elys/x/margin/keeper" + simapp "github.com/elys-network/elys/app" "github.com/elys-network/elys/x/margin/types" - "github.com/elys-network/elys/x/margin/types/mocks" ptypes "github.com/elys-network/elys/x/parameter/types" "github.com/stretchr/testify/assert" ) func TestCheckSameAssets_NewPosition(t *testing.T) { - // Setup the mock checker - mockChecker := new(mocks.PositionChecker) + app := simapp.InitElysTestApp(true) + ctx := app.BaseApp.NewContext(true, tmproto.Header{}) - // Create an instance of Keeper with the mock checker - k := keeper.Keeper{ - PositionChecker: mockChecker, - } + k := app.MarginKeeper - ctx := sdk.Context{} // mock or setup a context mtp := types.NewMTP("creator", ptypes.USDC, ptypes.ATOM, types.Position_LONG, sdk.NewDec(5), 1) k.SetMTP(ctx, mtp) @@ -37,5 +33,4 @@ func TestCheckSameAssets_NewPosition(t *testing.T) { // Expect no error assert.Nil(t, mtp) - mockChecker.AssertExpectations(t) } diff --git a/x/margin/keeper/close_long_test.go b/x/margin/keeper/close_long_test.go index c9c85746a..79e850a9a 100644 --- a/x/margin/keeper/close_long_test.go +++ b/x/margin/keeper/close_long_test.go @@ -121,7 +121,9 @@ func TestCloseLong_ErrorHandleInterest(t *testing.T) { Id: 1, } mtp = types.MTP{ - AmmPoolId: 2, + AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, + CollateralAssets: []string{"uusdc"}, } pool = types.Pool{ InterestRate: math.LegacyNewDec(2), @@ -133,7 +135,7 @@ func TestCloseLong_ErrorHandleInterest(t *testing.T) { mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true) mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil) - mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool).Return(errors.New("error executing handle interest")) + mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(errors.New("error executing handle interest")) _, _, err := k.CloseLong(ctx, msg) @@ -158,7 +160,9 @@ func TestCloseLong_ErrorTakeOutCustody(t *testing.T) { Id: 1, } mtp = types.MTP{ - AmmPoolId: 2, + AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, + CollateralAssets: []string{"uusdc"}, } pool = types.Pool{ InterestRate: math.LegacyNewDec(2), @@ -170,8 +174,8 @@ func TestCloseLong_ErrorTakeOutCustody(t *testing.T) { mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true) mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil) - mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool).Return(nil) - mockChecker.On("TakeOutCustody", ctx, mtp, &pool).Return(errors.New("error executing take out custody")) + mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.CustodyAssets[0]).Return(errors.New("error executing take out custody")) _, _, err := k.CloseLong(ctx, msg) @@ -196,7 +200,9 @@ func TestCloseLong_ErrorEstimateAndRepay(t *testing.T) { Id: 1, } mtp = types.MTP{ - AmmPoolId: 2, + AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, + CollateralAssets: []string{"uusdc"}, } pool = types.Pool{ InterestRate: math.LegacyNewDec(2), @@ -208,9 +214,9 @@ func TestCloseLong_ErrorEstimateAndRepay(t *testing.T) { mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true) mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil) - mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool).Return(nil) - mockChecker.On("TakeOutCustody", ctx, mtp, &pool).Return(nil) - mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool).Return(sdk.Int{}, errors.New("error executing estimate and repay")) + mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(sdk.Int{}, errors.New("error executing estimate and repay")) _, _, err := k.CloseLong(ctx, msg) @@ -235,7 +241,9 @@ func TestCloseLong_SuccessfulClosingLongPosition(t *testing.T) { Id: 1, } mtp = types.MTP{ - AmmPoolId: 2, + AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, + CollateralAssets: []string{"uusdc"}, } pool = types.Pool{ InterestRate: math.LegacyNewDec(2), @@ -248,9 +256,9 @@ func TestCloseLong_SuccessfulClosingLongPosition(t *testing.T) { mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true) mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil) - mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool).Return(nil) - mockChecker.On("TakeOutCustody", ctx, mtp, &pool).Return(nil) - mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool).Return(repayAmount, nil) + mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(repayAmount, nil) mtpOut, repayAmountOut, err := k.CloseLong(ctx, msg) diff --git a/x/margin/keeper/close_short_test.go b/x/margin/keeper/close_short_test.go index b28fe36d8..574dd6238 100644 --- a/x/margin/keeper/close_short_test.go +++ b/x/margin/keeper/close_short_test.go @@ -121,7 +121,9 @@ func TestCloseShort_ErrorHandleInterest(t *testing.T) { Id: 1, } mtp = types.MTP{ - AmmPoolId: 2, + AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, + CollateralAssets: []string{"uusdc"}, } pool = types.Pool{ InterestRate: math.LegacyNewDec(2), @@ -133,7 +135,7 @@ func TestCloseShort_ErrorHandleInterest(t *testing.T) { mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true) mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil) - mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool).Return(errors.New("error executing handle interest")) + mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(errors.New("error executing handle interest")) _, _, err := k.CloseShort(ctx, msg) @@ -158,7 +160,9 @@ func TestCloseShort_ErrorTakeOutCustody(t *testing.T) { Id: 1, } mtp = types.MTP{ - AmmPoolId: 2, + AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, + CollateralAssets: []string{"uusdc"}, } pool = types.Pool{ InterestRate: math.LegacyNewDec(2), @@ -170,8 +174,8 @@ func TestCloseShort_ErrorTakeOutCustody(t *testing.T) { mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true) mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil) - mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool).Return(nil) - mockChecker.On("TakeOutCustody", ctx, mtp, &pool).Return(errors.New("error executing take out custody")) + mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.CustodyAssets[0]).Return(errors.New("error executing take out custody")) _, _, err := k.CloseShort(ctx, msg) @@ -196,7 +200,9 @@ func TestCloseShort_ErrorEstimateAndRepay(t *testing.T) { Id: 1, } mtp = types.MTP{ - AmmPoolId: 2, + AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, + CollateralAssets: []string{"uusdc"}, } pool = types.Pool{ InterestRate: math.LegacyNewDec(2), @@ -208,9 +214,9 @@ func TestCloseShort_ErrorEstimateAndRepay(t *testing.T) { mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true) mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil) - mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool).Return(nil) - mockChecker.On("TakeOutCustody", ctx, mtp, &pool).Return(nil) - mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool).Return(sdk.Int{}, errors.New("error executing estimate and repay")) + mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(sdk.Int{}, errors.New("error executing estimate and repay")) _, _, err := k.CloseShort(ctx, msg) @@ -236,6 +242,8 @@ func TestCloseShort_SuccessfulClosingLongPosition(t *testing.T) { } mtp = types.MTP{ AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, + CollateralAssets: []string{"uusdc"}, } pool = types.Pool{ InterestRate: math.LegacyNewDec(2), @@ -248,9 +256,9 @@ func TestCloseShort_SuccessfulClosingLongPosition(t *testing.T) { mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(pool, true) mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammPool, nil) - mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool).Return(nil) - mockChecker.On("TakeOutCustody", ctx, mtp, &pool).Return(nil) - mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool).Return(repayAmount, nil) + mockChecker.On("HandleInterest", ctx, &mtp, &pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("TakeOutCustody", ctx, mtp, &pool, mtp.CustodyAssets[0]).Return(nil) + mockChecker.On("EstimateAndRepay", ctx, mtp, pool, ammPool, mtp.CollateralAssets[0], mtp.CustodyAssets[0]).Return(repayAmount, nil) mtpOut, repayAmountOut, err := k.CloseShort(ctx, msg) diff --git a/x/margin/keeper/open_long_test.go b/x/margin/keeper/open_long_test.go index 67f8050ea..bd8def8d0 100644 --- a/x/margin/keeper/open_long_test.go +++ b/x/margin/keeper/open_long_test.go @@ -262,7 +262,7 @@ func TestOpenLong_ErrorsDuringOperations(t *testing.T) { mtp := types.NewMTP(msg.Creator, msg.CollateralAsset, msg.BorrowAsset, msg.Position, msg.Leverage, poolId) borrowError := errors.New("borrow error") - mockChecker.On("Borrow", ctx, msg.CollateralAsset, msg.CollateralAmount, custodyAmount, mtp, &ammtypes.Pool{}, &types.Pool{}, eta).Return(borrowError) + mockChecker.On("Borrow", ctx, msg.CollateralAsset, msg.BorrowAsset, msg.CollateralAmount, custodyAmount, mtp, &ammtypes.Pool{}, &types.Pool{}, eta).Return(borrowError) _, err := k.OpenLong(ctx, poolId, msg) @@ -318,7 +318,7 @@ func TestOpenLong_LeverageRatioLessThanSafetyFactor(t *testing.T) { mtp := types.NewMTP(msg.Creator, msg.CollateralAsset, msg.BorrowAsset, msg.Position, msg.Leverage, poolId) - mockChecker.On("Borrow", ctx, msg.CollateralAsset, msg.CollateralAmount, custodyAmount, mtp, &ammtypes.Pool{}, &types.Pool{}, eta).Return(nil) + mockChecker.On("Borrow", ctx, msg.CollateralAsset, msg.BorrowAsset, msg.CollateralAmount, custodyAmount, mtp, &ammtypes.Pool{}, &types.Pool{}, eta).Return(nil) mockChecker.On("UpdatePoolHealth", ctx, &types.Pool{}).Return(nil) mockChecker.On("TakeInCustody", ctx, *mtp, &types.Pool{}).Return(nil) @@ -381,7 +381,7 @@ func TestOpenLong_Success(t *testing.T) { mtp := types.NewMTP(msg.Creator, msg.CollateralAsset, msg.BorrowAsset, msg.Position, msg.Leverage, poolId) - mockChecker.On("Borrow", ctx, msg.CollateralAsset, msg.CollateralAmount, custodyAmount, mtp, &ammtypes.Pool{}, &types.Pool{}, eta).Return(nil) + mockChecker.On("Borrow", ctx, msg.CollateralAsset, msg.BorrowAsset, msg.CollateralAmount, custodyAmount, mtp, &ammtypes.Pool{}, &types.Pool{}, eta).Return(nil) mockChecker.On("UpdatePoolHealth", ctx, &types.Pool{}).Return(nil) mockChecker.On("TakeInCustody", ctx, *mtp, &types.Pool{}).Return(nil)