Skip to content

Commit

Permalink
fix: unit tests in close long, short and open long
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-elys committed Sep 14, 2023
1 parent 93fbf48 commit 2528474
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 38 deletions.
15 changes: 5 additions & 10 deletions x/margin/keeper/check_same_asset_position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -37,5 +33,4 @@ func TestCheckSameAssets_NewPosition(t *testing.T) {

// Expect no error
assert.Nil(t, mtp)
mockChecker.AssertExpectations(t)
}
34 changes: 21 additions & 13 deletions x/margin/keeper/close_long_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)

Expand All @@ -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),
Expand All @@ -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)

Expand All @@ -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),
Expand All @@ -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)

Expand All @@ -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),
Expand All @@ -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)

Expand Down
32 changes: 20 additions & 12 deletions x/margin/keeper/close_short_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)

Expand All @@ -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),
Expand All @@ -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)

Expand All @@ -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),
Expand All @@ -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)

Expand All @@ -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),
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions x/margin/keeper/open_long_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 2528474

Please sign in to comment.