From aaecf33eee231d66c5aca0c824d8ed3c803227d5 Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Tue, 12 Sep 2023 02:24:06 +0000 Subject: [PATCH 01/10] fix: conflict with main --- x/margin/keeper/check_same_asset_position.go | 17 ++++ x/margin/keeper/open.go | 7 +- x/margin/keeper/open_consolidate.go | 45 ++++++++++ x/margin/keeper/open_consolidate_long.go | 16 ++++ x/margin/keeper/open_long.go | 80 +----------------- x/margin/keeper/open_long_process.go | 88 ++++++++++++++++++++ x/margin/types/expected_keepers.go | 1 + x/margin/types/mocks/open_long_checker.go | 45 ++++++++++ 8 files changed, 219 insertions(+), 80 deletions(-) create mode 100644 x/margin/keeper/check_same_asset_position.go create mode 100644 x/margin/keeper/open_consolidate.go create mode 100644 x/margin/keeper/open_consolidate_long.go create mode 100644 x/margin/keeper/open_long_process.go diff --git a/x/margin/keeper/check_same_asset_position.go b/x/margin/keeper/check_same_asset_position.go new file mode 100644 index 000000000..f6554460b --- /dev/null +++ b/x/margin/keeper/check_same_asset_position.go @@ -0,0 +1,17 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/margin/types" +) + +func (k Keeper) CheckSameAssetPosition(ctx sdk.Context, msg *types.MsgOpen) *types.MTP { + mtps := k.GetAllMTPs(ctx) + for _, mtp := range mtps { + if mtp.Address == msg.Creator && mtp.Position == msg.Position { + return &mtp + } + } + + return nil +} diff --git a/x/margin/keeper/open.go b/x/margin/keeper/open.go index c0f2e4c46..95dcc7052 100644 --- a/x/margin/keeper/open.go +++ b/x/margin/keeper/open.go @@ -15,7 +15,12 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenRespons return nil, err } - if err := k.OpenChecker.CheckMaxOpenPositions(ctx); err != nil { + // Check if it is the same asset, same direction position for the same trader. + if mtp := k.CheckSameAssetPosition(ctx, msg); mtp != nil { + return k.OpenConsolidate(ctx, mtp, msg) + } + + if err := k.CheckMaxOpenPositions(ctx); err != nil { return nil, err } diff --git a/x/margin/keeper/open_consolidate.go b/x/margin/keeper/open_consolidate.go new file mode 100644 index 000000000..8e823dc3e --- /dev/null +++ b/x/margin/keeper/open_consolidate.go @@ -0,0 +1,45 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/elys-network/elys/x/margin/types" +) + +func (k Keeper) OpenConsolidate(ctx sdk.Context, mtp *types.MTP, msg *types.MsgOpen) (*types.MsgOpenResponse, error) { + // Get token asset other than USDC + nonNativeAsset := k.OpenLongChecker.GetNonNativeAsset(msg.CollateralAsset, msg.BorrowAsset) + + poolId := mtp.AmmPoolId + pool, found := k.OpenLongChecker.GetPool(ctx, poolId) + if !found { + return nil, sdkerrors.Wrap(types.ErrPoolDoesNotExist, nonNativeAsset) + } + + if !k.OpenLongChecker.IsPoolEnabled(ctx, poolId) { + return nil, sdkerrors.Wrap(types.ErrMTPDisabled, nonNativeAsset) + } + + ammPool, err := k.OpenLongChecker.GetAmmPool(ctx, poolId, nonNativeAsset) + if err != nil { + return nil, err + } + + switch msg.Position { + case types.Position_LONG: + mtp, err = k.OpenConsolidateLong(ctx, poolId, mtp, msg) + if err != nil { + return nil, err + } + default: + return nil, sdkerrors.Wrap(types.ErrInvalidPosition, msg.Position.String()) + } + + ctx.EventManager().EmitEvent(k.GenerateOpenEvent(mtp)) + + if k.hooks != nil { + k.hooks.AfterMarginPositionModified(ctx, ammPool, pool) + } + + return &types.MsgOpenResponse{}, nil +} diff --git a/x/margin/keeper/open_consolidate_long.go b/x/margin/keeper/open_consolidate_long.go new file mode 100644 index 000000000..b2fec4d3d --- /dev/null +++ b/x/margin/keeper/open_consolidate_long.go @@ -0,0 +1,16 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/margin/types" +) + +func (k Keeper) OpenConsolidateLong(ctx sdk.Context, poolId uint64, mtp *types.MTP, msg *types.MsgOpen) (*types.MTP, error) { + maxLeverage := k.OpenLongChecker.GetMaxLeverageParam(ctx) + leverage := sdk.MinDec(msg.Leverage, maxLeverage) + eta := leverage.Sub(sdk.OneDec()) + collateralAmountDec := sdk.NewDecFromBigInt(msg.CollateralAmount.BigInt()) + mtp.Leverage = leverage + + return k.ProcessOpenLong(ctx, mtp, leverage, eta, collateralAmountDec, poolId, msg) +} diff --git a/x/margin/keeper/open_long.go b/x/margin/keeper/open_long.go index 41fa58e2a..ac95bc3e0 100644 --- a/x/margin/keeper/open_long.go +++ b/x/margin/keeper/open_long.go @@ -2,9 +2,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/elys-network/elys/x/margin/types" - ptypes "github.com/elys-network/elys/x/parameter/types" ) func (k Keeper) OpenLong(ctx sdk.Context, poolId uint64, msg *types.MsgOpen) (*types.MTP, error) { @@ -14,81 +12,5 @@ func (k Keeper) OpenLong(ctx sdk.Context, poolId uint64, msg *types.MsgOpen) (*t collateralAmountDec := sdk.NewDecFromBigInt(msg.CollateralAmount.BigInt()) mtp := types.NewMTP(msg.Creator, msg.CollateralAsset, msg.BorrowAsset, msg.Position, leverage, poolId) - // Get token asset other than USDC - nonNativeAsset := k.OpenLongChecker.GetNonNativeAsset(msg.CollateralAsset, msg.BorrowAsset) - - pool, found := k.OpenLongChecker.GetPool(ctx, poolId) - if !found { - return nil, sdkerrors.Wrap(types.ErrPoolDoesNotExist, nonNativeAsset) - } - - if !k.OpenLongChecker.IsPoolEnabled(ctx, poolId) { - return nil, sdkerrors.Wrap(types.ErrMTPDisabled, nonNativeAsset) - } - - ammPool, err := k.OpenLongChecker.GetAmmPool(ctx, poolId, nonNativeAsset) - if err != nil { - return nil, err - } - - leveragedAmount := sdk.NewInt(collateralAmountDec.Mul(leverage).TruncateInt().Int64()) - // If collateral is not native (usdc), calculate the borrowing amount in usdc and check the balance - if msg.CollateralAsset != ptypes.USDC { - custodyAmtToken := sdk.NewCoin(msg.CollateralAsset, leveragedAmount) - borrowingAmount, err := k.OpenLongChecker.EstimateSwapGivenOut(ctx, custodyAmtToken, ptypes.USDC, ammPool) - if err != nil { - return nil, err - } - if !k.OpenLongChecker.HasSufficientPoolBalance(ctx, ammPool, ptypes.USDC, borrowingAmount) { - return nil, sdkerrors.Wrap(types.ErrBorrowTooHigh, leveragedAmount.String()) - } - } else { - if !k.OpenLongChecker.HasSufficientPoolBalance(ctx, ammPool, msg.CollateralAsset, leveragedAmount) { - return nil, sdkerrors.Wrap(types.ErrBorrowTooHigh, leveragedAmount.String()) - } - } - - collateralTokenAmt := sdk.NewCoin(msg.CollateralAsset, msg.CollateralAmount) - err = k.OpenLongChecker.CheckMinLiabilities(ctx, collateralTokenAmt, eta, pool, ammPool, msg.BorrowAsset) - if err != nil { - return nil, err - } - - leveragedAmtTokenIn := sdk.NewCoin(msg.CollateralAsset, leveragedAmount) - custodyAmount, err := k.OpenLongChecker.EstimateSwap(ctx, leveragedAmtTokenIn, msg.BorrowAsset, ammPool) - if err != nil { - return nil, err - } - - // If the collateral asset is not usdc, custody amount equals to leverage amount - if msg.CollateralAsset != ptypes.USDC { - custodyAmount = leveragedAmount - } - - if !k.OpenLongChecker.HasSufficientPoolBalance(ctx, ammPool, msg.BorrowAsset, custodyAmount) { - return nil, sdkerrors.Wrap(types.ErrCustodyTooHigh, custodyAmount.String()) - } - - err = k.OpenLongChecker.Borrow(ctx, msg.CollateralAsset, msg.CollateralAmount, custodyAmount, mtp, &ammPool, &pool, eta) - if err != nil { - return nil, err - } - if err = k.OpenLongChecker.UpdatePoolHealth(ctx, &pool); err != nil { - return nil, err - } - if err = k.OpenLongChecker.TakeInCustody(ctx, *mtp, &pool); err != nil { - return nil, err - } - - lr, err := k.OpenLongChecker.UpdateMTPHealth(ctx, *mtp, ammPool) - if err != nil { - return nil, err - } - - safetyFactor := k.OpenLongChecker.GetSafetyFactor(ctx) - if lr.LTE(safetyFactor) { - return nil, types.ErrMTPUnhealthy - } - - return mtp, nil + return k.ProcessOpenLong(ctx, mtp, leverage, eta, collateralAmountDec, poolId, msg) } diff --git a/x/margin/keeper/open_long_process.go b/x/margin/keeper/open_long_process.go new file mode 100644 index 000000000..00520bfe6 --- /dev/null +++ b/x/margin/keeper/open_long_process.go @@ -0,0 +1,88 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/elys-network/elys/x/margin/types" + ptypes "github.com/elys-network/elys/x/parameter/types" +) + +func (k Keeper) ProcessOpenLong(ctx sdk.Context, mtp *types.MTP, leverage sdk.Dec, eta sdk.Dec, collateralAmountDec sdk.Dec, poolId uint64, msg *types.MsgOpen) (*types.MTP, error) { + // Get token asset other than USDC + nonNativeAsset := k.OpenLongChecker.GetNonNativeAsset(msg.CollateralAsset, msg.BorrowAsset) + + pool, found := k.OpenLongChecker.GetPool(ctx, poolId) + if !found { + return nil, sdkerrors.Wrap(types.ErrPoolDoesNotExist, nonNativeAsset) + } + + if !k.OpenLongChecker.IsPoolEnabled(ctx, poolId) { + return nil, sdkerrors.Wrap(types.ErrMTPDisabled, nonNativeAsset) + } + + ammPool, err := k.OpenLongChecker.GetAmmPool(ctx, poolId, nonNativeAsset) + if err != nil { + return nil, err + } + + leveragedAmount := sdk.NewInt(collateralAmountDec.Mul(leverage).TruncateInt().Int64()) + // If collateral is not native (usdc), calculate the borrowing amount in usdc and check the balance + if msg.CollateralAsset != ptypes.USDC { + custodyAmtToken := sdk.NewCoin(msg.CollateralAsset, leveragedAmount) + borrowingAmount, err := k.OpenLongChecker.EstimateSwapGivenOut(ctx, custodyAmtToken, ptypes.USDC, ammPool) + if err != nil { + return nil, err + } + if !k.OpenLongChecker.HasSufficientPoolBalance(ctx, ammPool, ptypes.USDC, borrowingAmount) { + return nil, sdkerrors.Wrap(types.ErrBorrowTooHigh, leveragedAmount.String()) + } + } else { + if !k.OpenLongChecker.HasSufficientPoolBalance(ctx, ammPool, msg.CollateralAsset, leveragedAmount) { + return nil, sdkerrors.Wrap(types.ErrBorrowTooHigh, leveragedAmount.String()) + } + } + + collateralTokenAmt := sdk.NewCoin(msg.CollateralAsset, msg.CollateralAmount) + err = k.OpenLongChecker.CheckMinLiabilities(ctx, collateralTokenAmt, eta, pool, ammPool, msg.BorrowAsset) + if err != nil { + return nil, err + } + + leveragedAmtTokenIn := sdk.NewCoin(msg.CollateralAsset, leveragedAmount) + custodyAmount, err := k.OpenLongChecker.EstimateSwap(ctx, leveragedAmtTokenIn, msg.BorrowAsset, ammPool) + if err != nil { + return nil, err + } + + // If the collateral asset is not usdc, custody amount equals to leverage amount + if msg.CollateralAsset != ptypes.USDC { + custodyAmount = leveragedAmount + } + + if !k.OpenLongChecker.HasSufficientPoolBalance(ctx, ammPool, msg.BorrowAsset, custodyAmount) { + return nil, sdkerrors.Wrap(types.ErrCustodyTooHigh, custodyAmount.String()) + } + + err = k.OpenLongChecker.Borrow(ctx, msg.CollateralAsset, msg.CollateralAmount, custodyAmount, mtp, &ammPool, &pool, eta) + if err != nil { + return nil, err + } + if err = k.OpenLongChecker.UpdatePoolHealth(ctx, &pool); err != nil { + return nil, err + } + if err = k.OpenLongChecker.TakeInCustody(ctx, *mtp, &pool); err != nil { + return nil, err + } + + lr, err := k.OpenLongChecker.UpdateMTPHealth(ctx, *mtp, ammPool) + if err != nil { + return nil, err + } + + safetyFactor := k.OpenLongChecker.GetSafetyFactor(ctx) + if lr.LTE(safetyFactor) { + return nil, types.ErrMTPUnhealthy + } + + return mtp, nil +} diff --git a/x/margin/types/expected_keepers.go b/x/margin/types/expected_keepers.go index f98eae47c..bfc650e91 100644 --- a/x/margin/types/expected_keepers.go +++ b/x/margin/types/expected_keepers.go @@ -59,6 +59,7 @@ type OpenLongChecker interface { SetPool(ctx sdk.Context, pool Pool) GetAmmPoolBalance(ctx sdk.Context, ammPool ammtypes.Pool, assetDenom string) (sdk.Int, error) CheckLongingAssets(ctx sdk.Context, collateralAsset string, borrowAsset string) error + CheckSameAssetPosition(ctx sdk.Context, msg *MsgOpen) *MTP } //go:generate mockery --srcpkg . --name CloseLongChecker --structname CloseLongChecker --filename close_long_checker.go --with-expecter diff --git a/x/margin/types/mocks/open_long_checker.go b/x/margin/types/mocks/open_long_checker.go index 486bd06a6..7615521b0 100644 --- a/x/margin/types/mocks/open_long_checker.go +++ b/x/margin/types/mocks/open_long_checker.go @@ -166,6 +166,51 @@ func (_c *OpenLongChecker_CheckMinLiabilities_Call) RunAndReturn(run func(types. return _c } +// CheckSameAssetPosition provides a mock function with given fields: ctx, msg +func (_m *OpenLongChecker) CheckSameAssetPosition(ctx types.Context, msg *margintypes.MsgOpen) *margintypes.MTP { + ret := _m.Called(ctx, msg) + + var r0 *margintypes.MTP + if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MsgOpen) *margintypes.MTP); ok { + r0 = rf(ctx, msg) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*margintypes.MTP) + } + } + + return r0 +} + +// OpenLongChecker_CheckSameAssetPosition_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CheckSameAssetPosition' +type OpenLongChecker_CheckSameAssetPosition_Call struct { + *mock.Call +} + +// CheckSameAssetPosition is a helper method to define mock.On call +// - ctx types.Context +// - msg *margintypes.MsgOpen +func (_e *OpenLongChecker_Expecter) CheckSameAssetPosition(ctx interface{}, msg interface{}) *OpenLongChecker_CheckSameAssetPosition_Call { + return &OpenLongChecker_CheckSameAssetPosition_Call{Call: _e.mock.On("CheckSameAssetPosition", ctx, msg)} +} + +func (_c *OpenLongChecker_CheckSameAssetPosition_Call) Run(run func(ctx types.Context, msg *margintypes.MsgOpen)) *OpenLongChecker_CheckSameAssetPosition_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(*margintypes.MsgOpen)) + }) + return _c +} + +func (_c *OpenLongChecker_CheckSameAssetPosition_Call) Return(_a0 *margintypes.MTP) *OpenLongChecker_CheckSameAssetPosition_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenLongChecker_CheckSameAssetPosition_Call) RunAndReturn(run func(types.Context, *margintypes.MsgOpen) *margintypes.MTP) *OpenLongChecker_CheckSameAssetPosition_Call { + _c.Call.Return(run) + return _c +} + // EstimateSwap provides a mock function with given fields: ctx, leveragedAmtTokenIn, borrowAsset, ammPool func (_m *OpenLongChecker) EstimateSwap(ctx types.Context, leveragedAmtTokenIn types.Coin, borrowAsset string, ammPool ammtypes.Pool) (math.Int, error) { ret := _m.Called(ctx, leveragedAmtTokenIn, borrowAsset, ammPool) From 611d0bcaf32a0657926fb03fe43e34bae4e86dd2 Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Tue, 12 Sep 2023 02:14:50 +0000 Subject: [PATCH 02/10] chore: update same asset position checker --- .../keeper/check_same_asset_position_test.go | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 x/margin/keeper/check_same_asset_position_test.go diff --git a/x/margin/keeper/check_same_asset_position_test.go b/x/margin/keeper/check_same_asset_position_test.go new file mode 100644 index 000000000..d2bb3645a --- /dev/null +++ b/x/margin/keeper/check_same_asset_position_test.go @@ -0,0 +1,32 @@ +package keeper_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/margin/keeper" + "github.com/elys-network/elys/x/margin/types/mocks" + "github.com/stretchr/testify/assert" +) + +func TestCheckSameAssets_OpenPositionsBelowMax(t *testing.T) { + // Setup the mock checker + mockChecker := new(mocks.PositionChecker) + + // Create an instance of Keeper with the mock checker + k := keeper.Keeper{ + PositionChecker: mockChecker, + } + + ctx := sdk.Context{} // mock or setup a context + + // Mock behavior + mockChecker.On("GetOpenMTPCount", ctx).Return(uint64(5)) + mockChecker.On("GetMaxOpenPositions", ctx).Return(uint64(10)) + + err := k.CheckMaxOpenPositions(ctx) + + // Expect no error + assert.Nil(t, err) + mockChecker.AssertExpectations(t) +} From d56ed75b316452396dc52aa0175f5b0dc3098a7c Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Wed, 13 Sep 2023 08:47:16 +0000 Subject: [PATCH 03/10] chore: multiple assets per position close, handle interest --- docs/static/openapi.yml | 512 ++++++++++++------ proto/elys/margin/tx.proto | 2 + proto/elys/margin/types.proto | 24 +- x/margin/client/cli/query_mtp_test.go | 28 +- .../keeper/calc_mtp_interest_liabilities.go | 24 +- x/margin/keeper/check_same_asset_position.go | 2 +- x/margin/keeper/close.go | 16 +- x/margin/keeper/close_long.go | 61 ++- x/margin/keeper/close_long_test.go | 14 +- x/margin/keeper/close_short.go | 61 ++- x/margin/keeper/close_short_test.go | 14 +- x/margin/keeper/estimate_and_repay.go | 9 +- x/margin/keeper/generate_open_event.go | 20 +- x/margin/keeper/get_mtp_asset_index.go | 26 + x/margin/keeper/handle_interest.go | 9 +- x/margin/keeper/handle_interest_payment.go | 24 +- x/margin/keeper/invariant_check.go | 4 +- x/margin/keeper/keeper.go | 92 ++-- x/margin/keeper/keeper_test.go | 26 +- x/margin/keeper/open.go | 4 +- x/margin/keeper/open_consolidate_long.go | 2 +- x/margin/keeper/open_long_process.go | 2 +- x/margin/keeper/repay.go | 61 ++- x/margin/keeper/take_out_custody.go | 7 +- x/margin/keeper/update_mtp_health.go | 33 +- x/margin/types/expected_keepers.go | 16 +- x/margin/types/mocks/close_long_checker.go | 73 +-- x/margin/types/mocks/close_short_checker.go | 73 +-- x/margin/types/mocks/open_long_checker.go | 41 +- x/margin/types/tx.pb.go | 187 +++++-- x/margin/types/types.go | 35 +- x/margin/types/types.pb.go | 466 +++++++++++----- 32 files changed, 1310 insertions(+), 658 deletions(-) create mode 100644 x/margin/keeper/get_mtp_asset_index.go diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index 1906e6417..796604c15 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -39092,24 +39092,40 @@ paths: properties: address: type: string - collateral_asset: - type: string - collateral_amount: - type: string + collateral_assets: + type: array + items: + type: string + collateral_amounts: + type: array + items: + type: string liabilities: type: string - interest_paid_collateral: - type: string - interest_paid_custody: - type: string - interest_unpaid_collateral: - type: string - custody_asset: - type: string - custody_amount: - type: string - leverage: - type: string + interest_paid_collaterals: + type: array + items: + type: string + interest_paid_custodys: + type: array + items: + type: string + interest_unpaid_collaterals: + type: array + items: + type: string + custody_assets: + type: array + items: + type: string + custody_amounts: + type: array + items: + type: string + leverages: + type: array + items: + type: string mtp_health: type: string position: @@ -39125,6 +39141,10 @@ paths: amm_pool_id: type: string format: uint64 + consolidate_leverage: + type: string + sum_collateral: + type: string default: description: An unexpected error response. schema: @@ -39172,24 +39192,40 @@ paths: properties: address: type: string - collateral_asset: - type: string - collateral_amount: - type: string + collateral_assets: + type: array + items: + type: string + collateral_amounts: + type: array + items: + type: string liabilities: type: string - interest_paid_collateral: - type: string - interest_paid_custody: - type: string - interest_unpaid_collateral: - type: string - custody_asset: - type: string - custody_amount: - type: string - leverage: - type: string + interest_paid_collaterals: + type: array + items: + type: string + interest_paid_custodys: + type: array + items: + type: string + interest_unpaid_collaterals: + type: array + items: + type: string + custody_assets: + type: array + items: + type: string + custody_amounts: + type: array + items: + type: string + leverages: + type: array + items: + type: string mtp_health: type: string position: @@ -39205,6 +39241,10 @@ paths: amm_pool_id: type: string format: uint64 + consolidate_leverage: + type: string + sum_collateral: + type: string pagination: type: object properties: @@ -39332,24 +39372,40 @@ paths: properties: address: type: string - collateral_asset: - type: string - collateral_amount: - type: string + collateral_assets: + type: array + items: + type: string + collateral_amounts: + type: array + items: + type: string liabilities: type: string - interest_paid_collateral: - type: string - interest_paid_custody: - type: string - interest_unpaid_collateral: - type: string - custody_asset: - type: string - custody_amount: - type: string - leverage: - type: string + interest_paid_collaterals: + type: array + items: + type: string + interest_paid_custodys: + type: array + items: + type: string + interest_unpaid_collaterals: + type: array + items: + type: string + custody_assets: + type: array + items: + type: string + custody_amounts: + type: array + items: + type: string + leverages: + type: array + items: + type: string mtp_health: type: string position: @@ -39365,6 +39421,10 @@ paths: amm_pool_id: type: string format: uint64 + consolidate_leverage: + type: string + sum_collateral: + type: string pagination: type: object properties: @@ -39578,18 +39638,18 @@ paths: items: type: object properties: - liabilities: - type: string custody: type: string asset_balance: type: string - unsettled_liabilities: - type: string block_interest: type: string asset_denom: type: string + liabilities: + type: string + unsettled_liabilities: + type: string default: description: An unexpected error response. schema: @@ -39646,18 +39706,18 @@ paths: items: type: object properties: - liabilities: - type: string custody: type: string asset_balance: type: string - unsettled_liabilities: - type: string block_interest: type: string asset_denom: type: string + liabilities: + type: string + unsettled_liabilities: + type: string pagination: type: object properties: @@ -39780,24 +39840,40 @@ paths: properties: address: type: string - collateral_asset: - type: string - collateral_amount: - type: string + collateral_assets: + type: array + items: + type: string + collateral_amounts: + type: array + items: + type: string liabilities: type: string - interest_paid_collateral: - type: string - interest_paid_custody: - type: string - interest_unpaid_collateral: - type: string - custody_asset: - type: string - custody_amount: - type: string - leverage: - type: string + interest_paid_collaterals: + type: array + items: + type: string + interest_paid_custodys: + type: array + items: + type: string + interest_unpaid_collaterals: + type: array + items: + type: string + custody_assets: + type: array + items: + type: string + custody_amounts: + type: array + items: + type: string + leverages: + type: array + items: + type: string mtp_health: type: string position: @@ -39813,6 +39889,10 @@ paths: amm_pool_id: type: string format: uint64 + consolidate_leverage: + type: string + sum_collateral: + type: string pagination: type: object properties: @@ -78850,24 +78930,40 @@ definitions: properties: address: type: string - collateral_asset: - type: string - collateral_amount: - type: string + collateral_assets: + type: array + items: + type: string + collateral_amounts: + type: array + items: + type: string liabilities: type: string - interest_paid_collateral: - type: string - interest_paid_custody: - type: string - interest_unpaid_collateral: - type: string - custody_asset: - type: string - custody_amount: - type: string - leverage: - type: string + interest_paid_collaterals: + type: array + items: + type: string + interest_paid_custodys: + type: array + items: + type: string + interest_unpaid_collaterals: + type: array + items: + type: string + custody_assets: + type: array + items: + type: string + custody_amounts: + type: array + items: + type: string + leverages: + type: array + items: + type: string mtp_health: type: string position: @@ -78883,6 +78979,10 @@ definitions: amm_pool_id: type: string format: uint64 + consolidate_leverage: + type: string + sum_collateral: + type: string elys.margin.MTPResponse: type: object properties: @@ -78891,24 +78991,40 @@ definitions: properties: address: type: string - collateral_asset: - type: string - collateral_amount: - type: string + collateral_assets: + type: array + items: + type: string + collateral_amounts: + type: array + items: + type: string liabilities: type: string - interest_paid_collateral: - type: string - interest_paid_custody: - type: string - interest_unpaid_collateral: - type: string - custody_asset: - type: string - custody_amount: - type: string - leverage: - type: string + interest_paid_collaterals: + type: array + items: + type: string + interest_paid_custodys: + type: array + items: + type: string + interest_unpaid_collaterals: + type: array + items: + type: string + custody_assets: + type: array + items: + type: string + custody_amounts: + type: array + items: + type: string + leverages: + type: array + items: + type: string mtp_health: type: string position: @@ -78924,6 +79040,10 @@ definitions: amm_pool_id: type: string format: uint64 + consolidate_leverage: + type: string + sum_collateral: + type: string elys.margin.MsgCloseResponse: type: object elys.margin.MsgDewhitelistResponse: @@ -79047,29 +79167,25 @@ definitions: items: type: object properties: - liabilities: - type: string custody: type: string asset_balance: type: string - unsettled_liabilities: - type: string block_interest: type: string asset_denom: type: string + liabilities: + type: string + unsettled_liabilities: + type: string elys.margin.PoolAsset: type: object properties: - liabilities: - type: string custody: type: string asset_balance: type: string - unsettled_liabilities: - type: string block_interest: type: string asset_denom: @@ -79091,24 +79207,40 @@ definitions: properties: address: type: string - collateral_asset: - type: string - collateral_amount: - type: string + collateral_assets: + type: array + items: + type: string + collateral_amounts: + type: array + items: + type: string liabilities: type: string - interest_paid_collateral: - type: string - interest_paid_custody: - type: string - interest_unpaid_collateral: - type: string - custody_asset: - type: string - custody_amount: - type: string - leverage: - type: string + interest_paid_collaterals: + type: array + items: + type: string + interest_paid_custodys: + type: array + items: + type: string + interest_unpaid_collaterals: + type: array + items: + type: string + custody_assets: + type: array + items: + type: string + custody_amounts: + type: array + items: + type: string + leverages: + type: array + items: + type: string mtp_health: type: string position: @@ -79124,6 +79256,10 @@ definitions: amm_pool_id: type: string format: uint64 + consolidate_leverage: + type: string + sum_collateral: + type: string pagination: type: object properties: @@ -79160,24 +79296,40 @@ definitions: properties: address: type: string - collateral_asset: - type: string - collateral_amount: - type: string + collateral_assets: + type: array + items: + type: string + collateral_amounts: + type: array + items: + type: string liabilities: type: string - interest_paid_collateral: - type: string - interest_paid_custody: - type: string - interest_unpaid_collateral: - type: string - custody_asset: - type: string - custody_amount: - type: string - leverage: - type: string + interest_paid_collaterals: + type: array + items: + type: string + interest_paid_custodys: + type: array + items: + type: string + interest_unpaid_collaterals: + type: array + items: + type: string + custody_assets: + type: array + items: + type: string + custody_amounts: + type: array + items: + type: string + leverages: + type: array + items: + type: string mtp_health: type: string position: @@ -79193,6 +79345,10 @@ definitions: amm_pool_id: type: string format: uint64 + consolidate_leverage: + type: string + sum_collateral: + type: string pagination: type: object properties: @@ -79229,24 +79385,40 @@ definitions: properties: address: type: string - collateral_asset: - type: string - collateral_amount: - type: string + collateral_assets: + type: array + items: + type: string + collateral_amounts: + type: array + items: + type: string liabilities: type: string - interest_paid_collateral: - type: string - interest_paid_custody: - type: string - interest_unpaid_collateral: - type: string - custody_asset: - type: string - custody_amount: - type: string - leverage: - type: string + interest_paid_collaterals: + type: array + items: + type: string + interest_paid_custodys: + type: array + items: + type: string + interest_unpaid_collaterals: + type: array + items: + type: string + custody_assets: + type: array + items: + type: string + custody_amounts: + type: array + items: + type: string + leverages: + type: array + items: + type: string mtp_health: type: string position: @@ -79262,6 +79434,10 @@ definitions: amm_pool_id: type: string format: uint64 + consolidate_leverage: + type: string + sum_collateral: + type: string pagination: type: object properties: @@ -79312,18 +79488,18 @@ definitions: items: type: object properties: - liabilities: - type: string custody: type: string asset_balance: type: string - unsettled_liabilities: - type: string block_interest: type: string asset_denom: type: string + liabilities: + type: string + unsettled_liabilities: + type: string pagination: type: object properties: @@ -79372,18 +79548,18 @@ definitions: items: type: object properties: - liabilities: - type: string custody: type: string asset_balance: type: string - unsettled_liabilities: - type: string block_interest: type: string asset_denom: type: string + liabilities: + type: string + unsettled_liabilities: + type: string elys.margin.StatusResponse: type: object properties: diff --git a/proto/elys/margin/tx.proto b/proto/elys/margin/tx.proto index f3fa2f4d8..b3231706d 100644 --- a/proto/elys/margin/tx.proto +++ b/proto/elys/margin/tx.proto @@ -31,6 +31,8 @@ message MsgOpenResponse {} message MsgClose { string creator = 1; uint64 id = 2; + string collateralAsset = 3; + string custodyAsset = 4; } message MsgCloseResponse {} diff --git a/proto/elys/margin/types.proto b/proto/elys/margin/types.proto index 828176e64..a92c899f8 100644 --- a/proto/elys/margin/types.proto +++ b/proto/elys/margin/types.proto @@ -14,8 +14,8 @@ enum Position { message MTP { string address = 1; - string collateral_asset = 2; - string collateral_amount = 3 [ + repeated string collateral_assets = 2; + repeated string collateral_amounts = 3 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false ]; @@ -23,24 +23,24 @@ message MTP { (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false ]; - string interest_paid_collateral = 5 [ + repeated string interest_paid_collaterals = 5 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false ]; - string interest_paid_custody = 6 [ + repeated string interest_paid_custodys = 6 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false ]; - string interest_unpaid_collateral = 7 [ + repeated string interest_unpaid_collaterals = 7 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false ]; - string custody_asset = 8; - string custody_amount = 9 [ + repeated string custody_assets = 8; + repeated string custody_amounts = 9 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false ]; - string leverage = 10 [ + repeated string leverages = 10 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; @@ -51,6 +51,14 @@ message MTP { Position position = 12; uint64 id = 13; uint64 amm_pool_id = 14; + string consolidate_leverage = 15 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false + ]; + string sum_collateral = 16 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", + (gogoproto.nullable) = false + ]; } message WhiteList { diff --git a/x/margin/client/cli/query_mtp_test.go b/x/margin/client/cli/query_mtp_test.go index 6887291ae..e18e55289 100644 --- a/x/margin/client/cli/query_mtp_test.go +++ b/x/margin/client/cli/query_mtp_test.go @@ -37,20 +37,20 @@ func networkWithMTPObjects(t *testing.T, n int) (*network.Network, []*types.MTP) cfg := network.DefaultConfig() for i := 0; i < n; i++ { mtp := types.MTP{ - Address: addr[i].String(), - CollateralAsset: paramtypes.USDC, - CollateralAmount: sdk.NewInt(0), - Liabilities: sdk.NewInt(0), - InterestPaidCollateral: sdk.NewInt(0), - InterestPaidCustody: sdk.NewInt(0), - InterestUnpaidCollateral: sdk.NewInt(0), - CustodyAsset: "ATOM", - CustodyAmount: sdk.NewInt(0), - Leverage: sdk.NewDec(0), - MtpHealth: sdk.NewDec(0), - Position: types.Position_LONG, - Id: (uint64)(i + 1), - AmmPoolId: (uint64)(i + 1), + Address: addr[i].String(), + CollateralAssets: []string{paramtypes.USDC}, + CollateralAmounts: []sdk.Int{sdk.NewInt(0)}, + Liabilities: sdk.NewInt(0), + InterestPaidCollaterals: []sdk.Int{sdk.NewInt(0)}, + InterestPaidCustodys: []sdk.Int{sdk.NewInt(0)}, + InterestUnpaidCollaterals: []sdk.Int{sdk.NewInt(0)}, + CustodyAssets: []string{"ATOM"}, + CustodyAmounts: []sdk.Int{sdk.NewInt(0)}, + Leverages: []sdk.Dec{sdk.NewDec(0)}, + MtpHealth: sdk.NewDec(0), + Position: types.Position_LONG, + Id: (uint64)(i + 1), + AmmPoolId: (uint64)(i + 1), } nullify.Fill(&mtp) mtps = append(mtps, &mtp) diff --git a/x/margin/keeper/calc_mtp_interest_liabilities.go b/x/margin/keeper/calc_mtp_interest_liabilities.go index 96eb11049..4e321e687 100644 --- a/x/margin/keeper/calc_mtp_interest_liabilities.go +++ b/x/margin/keeper/calc_mtp_interest_liabilities.go @@ -4,15 +4,33 @@ import ( "math/big" sdk "github.com/cosmos/cosmos-sdk/types" + ammtypes "github.com/elys-network/elys/x/amm/types" "github.com/elys-network/elys/x/margin/types" + ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (k Keeper) CalcMTPInterestLiabilities(mtp *types.MTP, interestRate sdk.Dec, epochPosition, epochLength int64) sdk.Int { +func (k Keeper) CalcMTPInterestLiabilities(ctx sdk.Context, mtp *types.MTP, interestRate sdk.Dec, epochPosition, epochLength int64, ammPool ammtypes.Pool, collateralAsset string) sdk.Int { var interestRational, liabilitiesRational, rate, epochPositionRational, epochLengthRational big.Rat rate.SetFloat64(interestRate.MustFloat64()) - liabilitiesRational.SetInt(mtp.Liabilities.BigInt().Add(mtp.Liabilities.BigInt(), mtp.InterestUnpaidCollateral.BigInt())) + collateralIndex, _ := k.GetMTPAssetIndex(mtp, collateralAsset, "") + unpaidCollaterals := sdk.ZeroInt() + // Calculate collateral interests in usdc + if mtp.CollateralAssets[collateralIndex] == ptypes.USDC { + unpaidCollaterals = unpaidCollaterals.Add(mtp.InterestUnpaidCollaterals[collateralIndex]) + } else { + // Liability is in usdc, so convert it to usdc + unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAssets[collateralIndex], mtp.InterestUnpaidCollaterals[collateralIndex]) + C, err := k.EstimateSwapGivenOut(ctx, unpaidCollateralIn, ptypes.USDC, ammPool) + if err != nil { + return sdk.ZeroInt() + } + + unpaidCollaterals = unpaidCollaterals.Add(C) + } + + liabilitiesRational.SetInt(mtp.Liabilities.BigInt().Add(mtp.Liabilities.BigInt(), unpaidCollaterals.BigInt())) interestRational.Mul(&rate, &liabilitiesRational) if epochPosition > 0 { // prorate interest if within epoch @@ -24,7 +42,7 @@ func (k Keeper) CalcMTPInterestLiabilities(mtp *types.MTP, interestRate sdk.Dec, interestNew := interestRational.Num().Quo(interestRational.Num(), interestRational.Denom()) - interestNewInt := sdk.NewIntFromBigInt(interestNew.Add(interestNew, mtp.InterestUnpaidCollateral.BigInt())) + interestNewInt := sdk.NewIntFromBigInt(interestNew.Add(interestNew, unpaidCollaterals.BigInt())) // round up to lowest digit if interest too low and rate not 0 if interestNewInt.IsZero() && !interestRate.IsZero() { interestNewInt = sdk.NewInt(1) diff --git a/x/margin/keeper/check_same_asset_position.go b/x/margin/keeper/check_same_asset_position.go index f6554460b..83eb564e9 100644 --- a/x/margin/keeper/check_same_asset_position.go +++ b/x/margin/keeper/check_same_asset_position.go @@ -5,7 +5,7 @@ import ( "github.com/elys-network/elys/x/margin/types" ) -func (k Keeper) CheckSameAssetPosition(ctx sdk.Context, msg *types.MsgOpen) *types.MTP { +func (k Keeper) CheckSamePosition(ctx sdk.Context, msg *types.MsgOpen) *types.MTP { mtps := k.GetAllMTPs(ctx) for _, mtp := range mtps { if mtp.Address == msg.Creator && mtp.Position == msg.Position { diff --git a/x/margin/keeper/close.go b/x/margin/keeper/close.go index 0d8522e80..5acc8aaea 100644 --- a/x/margin/keeper/close.go +++ b/x/margin/keeper/close.go @@ -35,16 +35,16 @@ func (k Keeper) Close(ctx sdk.Context, msg *types.MsgClose) (*types.MsgCloseResp sdk.NewAttribute("id", strconv.FormatInt(int64(closedMtp.Id), 10)), sdk.NewAttribute("position", closedMtp.Position.String()), sdk.NewAttribute("address", closedMtp.Address), - sdk.NewAttribute("collateral_asset", closedMtp.CollateralAsset), - sdk.NewAttribute("collateral_amount", closedMtp.CollateralAmount.String()), - sdk.NewAttribute("custody_asset", closedMtp.CustodyAsset), - sdk.NewAttribute("custody_amount", closedMtp.CustodyAmount.String()), + sdk.NewAttribute("collateral_asset", closedMtp.CollateralAssets[0]), + sdk.NewAttribute("collateral_amount", closedMtp.CollateralAmounts[0].String()), + sdk.NewAttribute("custody_asset", closedMtp.CustodyAssets[0]), + sdk.NewAttribute("custody_amount", closedMtp.CustodyAmounts[0].String()), sdk.NewAttribute("repay_amount", repayAmount.String()), - sdk.NewAttribute("leverage", closedMtp.Leverage.String()), + sdk.NewAttribute("leverage", closedMtp.Leverages[0].String()), sdk.NewAttribute("liabilities", closedMtp.Liabilities.String()), - sdk.NewAttribute("interest_paid_collateral", mtp.InterestPaidCollateral.String()), - sdk.NewAttribute("interest_paid_custody", mtp.InterestPaidCustody.String()), - sdk.NewAttribute("interest_unpaid_collateral", closedMtp.InterestUnpaidCollateral.String()), + sdk.NewAttribute("interest_paid_collateral", mtp.InterestPaidCollaterals[0].String()), + sdk.NewAttribute("interest_paid_custody", mtp.InterestPaidCustodys[0].String()), + sdk.NewAttribute("interest_unpaid_collateral", closedMtp.InterestUnpaidCollaterals[0].String()), sdk.NewAttribute("health", closedMtp.MtpHealth.String()), )) diff --git a/x/margin/keeper/close_long.go b/x/margin/keeper/close_long.go index 50aa94b28..3aaf5532e 100644 --- a/x/margin/keeper/close_long.go +++ b/x/margin/keeper/close_long.go @@ -19,32 +19,41 @@ func (k Keeper) CloseLong(ctx sdk.Context, msg *types.MsgClose) (*types.MTP, sdk return nil, sdk.ZeroInt(), sdkerrors.Wrap(types.ErrInvalidBorrowingAsset, "invalid pool id") } - // Retrieve AmmPool - ammPool, err := k.CloseLongChecker.GetAmmPool(ctx, mtp.AmmPoolId, mtp.CustodyAsset) - if err != nil { - return nil, sdk.ZeroInt(), err - } - - // Handle Interest if within epoch position - if err := k.CloseLongChecker.HandleInterest(ctx, &mtp, &pool, ammPool); err != nil { - return nil, sdk.ZeroInt(), err - } - - // Take out custody - err = k.CloseLongChecker.TakeOutCustody(ctx, mtp, &pool) - if err != nil { - return nil, sdk.ZeroInt(), err - } - - // Estimate swap and repay - repayAmount, err := k.CloseLongChecker.EstimateAndRepay(ctx, mtp, pool, ammPool) - if err != nil { - return nil, sdk.ZeroInt(), err - } - - // Hooks after margin position closed - if k.hooks != nil { - k.hooks.AfterMarginPositionClosed(ctx, ammPool, pool) + repayAmount := sdk.ZeroInt() + for _, custodyAsset := range mtp.CustodyAssets { + // Retrieve AmmPool + ammPool, err := k.CloseLongChecker.GetAmmPool(ctx, mtp.AmmPoolId, custodyAsset) + if err != nil { + return nil, sdk.ZeroInt(), err + } + + for _, collateralAsset := range mtp.CollateralAssets { + // Handle Interest if within epoch position + if err := k.CloseLongChecker.HandleInterest(ctx, &mtp, &pool, ammPool, collateralAsset, custodyAsset); err != nil { + return nil, sdk.ZeroInt(), err + } + } + + // Take out custody + err = k.CloseLongChecker.TakeOutCustody(ctx, mtp, &pool, custodyAsset) + if err != nil { + return nil, sdk.ZeroInt(), err + } + + for _, collateralAsset := range mtp.CollateralAssets { + // Estimate swap and repay + repayAmt, err := k.CloseLongChecker.EstimateAndRepay(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) + if err != nil { + return nil, sdk.ZeroInt(), err + } + + repayAmount = repayAmount.Add(repayAmt) + } + + // Hooks after margin position closed + if k.hooks != nil { + k.hooks.AfterMarginPositionClosed(ctx, ammPool, pool) + } } return &mtp, repayAmount, nil diff --git a/x/margin/keeper/close_long_test.go b/x/margin/keeper/close_long_test.go index 8c34d091e..c9c85746a 100644 --- a/x/margin/keeper/close_long_test.go +++ b/x/margin/keeper/close_long_test.go @@ -88,15 +88,15 @@ func TestCloseLong_AmmPoolNotFound(t *testing.T) { Id: 1, } mtp = types.MTP{ - AmmPoolId: 2, - CustodyAsset: "uatom", + AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, } ) // Mock behavior mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(types.Pool{}, true) - mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAsset).Return(ammtypes.Pool{}, sdkerrors.Wrap(types.ErrPoolDoesNotExist, mtp.CustodyAsset)) + mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammtypes.Pool{}, sdkerrors.Wrap(types.ErrPoolDoesNotExist, mtp.CustodyAssets[0])) _, _, err := k.CloseLong(ctx, msg) @@ -132,7 +132,7 @@ func TestCloseLong_ErrorHandleInterest(t *testing.T) { // Mock behavior 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.CustodyAsset).Return(ammPool, nil) + 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")) _, _, err := k.CloseLong(ctx, msg) @@ -169,7 +169,7 @@ func TestCloseLong_ErrorTakeOutCustody(t *testing.T) { // Mock behavior 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.CustodyAsset).Return(ammPool, nil) + 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")) @@ -207,7 +207,7 @@ func TestCloseLong_ErrorEstimateAndRepay(t *testing.T) { // Mock behavior 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.CustodyAsset).Return(ammPool, nil) + 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")) @@ -247,7 +247,7 @@ func TestCloseLong_SuccessfulClosingLongPosition(t *testing.T) { // Mock behavior 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.CustodyAsset).Return(ammPool, nil) + 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) diff --git a/x/margin/keeper/close_short.go b/x/margin/keeper/close_short.go index 13b860f15..7dff7a26a 100644 --- a/x/margin/keeper/close_short.go +++ b/x/margin/keeper/close_short.go @@ -19,32 +19,41 @@ func (k Keeper) CloseShort(ctx sdk.Context, msg *types.MsgClose) (*types.MTP, sd return nil, sdk.ZeroInt(), sdkerrors.Wrap(types.ErrInvalidBorrowingAsset, "invalid pool id") } - // Retrieve AmmPool - ammPool, err := k.CloseShortChecker.GetAmmPool(ctx, mtp.AmmPoolId, mtp.CustodyAsset) - if err != nil { - return nil, sdk.ZeroInt(), err - } - - // Handle Interest if within epoch position - if err := k.CloseShortChecker.HandleInterest(ctx, &mtp, &pool, ammPool); err != nil { - return nil, sdk.ZeroInt(), err - } - - // Take out custody - err = k.CloseShortChecker.TakeOutCustody(ctx, mtp, &pool) - if err != nil { - return nil, sdk.ZeroInt(), err - } - - // Estimate swap and repay - repayAmount, err := k.CloseShortChecker.EstimateAndRepay(ctx, mtp, pool, ammPool) - if err != nil { - return nil, sdk.ZeroInt(), err - } - - // Hooks after margin position closed - if k.hooks != nil { - k.hooks.AfterMarginPositionClosed(ctx, ammPool, pool) + repayAmount := sdk.ZeroInt() + for _, custodyAsset := range mtp.CustodyAssets { + // Retrieve AmmPool + ammPool, err := k.CloseShortChecker.GetAmmPool(ctx, mtp.AmmPoolId, custodyAsset) + if err != nil { + return nil, sdk.ZeroInt(), err + } + + for _, collateralAsset := range mtp.CollateralAssets { + + // Handle Interest if within epoch position + if err := k.CloseShortChecker.HandleInterest(ctx, &mtp, &pool, ammPool, collateralAsset, custodyAsset); err != nil { + return nil, sdk.ZeroInt(), err + } + } + + // Take out custody + err = k.CloseShortChecker.TakeOutCustody(ctx, mtp, &pool, custodyAsset) + if err != nil { + return nil, sdk.ZeroInt(), err + } + + for _, collateralAsset := range mtp.CollateralAssets { + // Estimate swap and repay + repayAmt, err := k.CloseShortChecker.EstimateAndRepay(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) + if err != nil { + return nil, sdk.ZeroInt(), err + } + repayAmount = repayAmount.Add(repayAmt) + } + + // Hooks after margin position closed + if k.hooks != nil { + k.hooks.AfterMarginPositionClosed(ctx, ammPool, pool) + } } return &mtp, repayAmount, nil diff --git a/x/margin/keeper/close_short_test.go b/x/margin/keeper/close_short_test.go index 4b572088c..b28fe36d8 100644 --- a/x/margin/keeper/close_short_test.go +++ b/x/margin/keeper/close_short_test.go @@ -88,15 +88,15 @@ func TestCloseShort_AmmPoolNotFound(t *testing.T) { Id: 1, } mtp = types.MTP{ - AmmPoolId: 2, - CustodyAsset: "uatom", + AmmPoolId: 2, + CustodyAssets: []string{"uatom"}, } ) // Mock behavior mockChecker.On("GetMTP", ctx, msg.Creator, msg.Id).Return(mtp, nil) mockChecker.On("GetPool", ctx, mtp.AmmPoolId).Return(types.Pool{}, true) - mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAsset).Return(ammtypes.Pool{}, sdkerrors.Wrap(types.ErrPoolDoesNotExist, mtp.CustodyAsset)) + mockChecker.On("GetAmmPool", ctx, mtp.AmmPoolId, mtp.CustodyAssets[0]).Return(ammtypes.Pool{}, sdkerrors.Wrap(types.ErrPoolDoesNotExist, mtp.CustodyAssets[0])) _, _, err := k.CloseShort(ctx, msg) @@ -132,7 +132,7 @@ func TestCloseShort_ErrorHandleInterest(t *testing.T) { // Mock behavior 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.CustodyAsset).Return(ammPool, nil) + 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")) _, _, err := k.CloseShort(ctx, msg) @@ -169,7 +169,7 @@ func TestCloseShort_ErrorTakeOutCustody(t *testing.T) { // Mock behavior 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.CustodyAsset).Return(ammPool, nil) + 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")) @@ -207,7 +207,7 @@ func TestCloseShort_ErrorEstimateAndRepay(t *testing.T) { // Mock behavior 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.CustodyAsset).Return(ammPool, nil) + 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")) @@ -247,7 +247,7 @@ func TestCloseShort_SuccessfulClosingLongPosition(t *testing.T) { // Mock behavior 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.CustodyAsset).Return(ammPool, nil) + 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) diff --git a/x/margin/keeper/estimate_and_repay.go b/x/margin/keeper/estimate_and_repay.go index cb4e4ce33..16ee4e795 100644 --- a/x/margin/keeper/estimate_and_repay.go +++ b/x/margin/keeper/estimate_and_repay.go @@ -6,14 +6,15 @@ import ( "github.com/elys-network/elys/x/margin/types" ) -func (k Keeper) EstimateAndRepay(ctx sdk.Context, mtp types.MTP, pool types.Pool, ammPool ammtypes.Pool) (sdk.Int, error) { - cutodyAmtTokenIn := sdk.NewCoin(mtp.CustodyAsset, mtp.CustodyAmount) - repayAmount, err := k.EstimateSwap(ctx, cutodyAmtTokenIn, mtp.CollateralAsset, ammPool) +func (k Keeper) EstimateAndRepay(ctx sdk.Context, mtp types.MTP, pool types.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) (sdk.Int, error) { + collateralIndex, custodyIndex := k.GetMTPAssetIndex(&mtp, collateralAsset, custodyAsset) + cutodyAmtTokenIn := sdk.NewCoin(mtp.CustodyAssets[custodyIndex], mtp.CustodyAmounts[custodyIndex]) + repayAmount, err := k.EstimateSwap(ctx, cutodyAmtTokenIn, mtp.CollateralAssets[collateralIndex], ammPool) if err != nil { return sdk.ZeroInt(), err } - if err := k.Repay(ctx, &mtp, &pool, ammPool, repayAmount, false); err != nil { + if err := k.Repay(ctx, &mtp, &pool, ammPool, repayAmount, false, collateralAsset); err != nil { return sdk.ZeroInt(), err } diff --git a/x/margin/keeper/generate_open_event.go b/x/margin/keeper/generate_open_event.go index 7bc70c923..2bfb2c97a 100644 --- a/x/margin/keeper/generate_open_event.go +++ b/x/margin/keeper/generate_open_event.go @@ -8,19 +8,23 @@ import ( ) func (k Keeper) GenerateOpenEvent(mtp *types.MTP) sdk.Event { + collateralIndex := len(mtp.CollateralAssets) - 1 + custodyIndex := len(mtp.CustodyAssets) - 1 + mtpPosIndex := len(mtp.Leverages) - 1 + return sdk.NewEvent(types.EventOpen, sdk.NewAttribute("id", strconv.FormatInt(int64(mtp.Id), 10)), sdk.NewAttribute("position", mtp.Position.String()), sdk.NewAttribute("address", mtp.Address), - sdk.NewAttribute("collateral_asset", mtp.CollateralAsset), - sdk.NewAttribute("collateral_amount", mtp.CollateralAmount.String()), - sdk.NewAttribute("custody_asset", mtp.CustodyAsset), - sdk.NewAttribute("custody_amount", mtp.CustodyAmount.String()), - sdk.NewAttribute("leverage", mtp.Leverage.String()), + sdk.NewAttribute("collateral_asset", mtp.CollateralAssets[collateralIndex]), + sdk.NewAttribute("collateral_amount", mtp.CollateralAmounts[collateralIndex].String()), + sdk.NewAttribute("custody_asset", mtp.CustodyAssets[custodyIndex]), + sdk.NewAttribute("custody_amount", mtp.CustodyAmounts[custodyIndex].String()), + sdk.NewAttribute("leverage", mtp.Leverages[mtpPosIndex].String()), sdk.NewAttribute("liabilities", mtp.Liabilities.String()), - sdk.NewAttribute("interest_paid_collateral", mtp.InterestPaidCollateral.String()), - sdk.NewAttribute("interest_paid_custody", mtp.InterestPaidCustody.String()), - sdk.NewAttribute("interest_unpaid_collateral", mtp.InterestUnpaidCollateral.String()), + sdk.NewAttribute("interest_paid_collateral", mtp.InterestPaidCollaterals[collateralIndex].String()), + sdk.NewAttribute("interest_paid_custody", mtp.InterestPaidCustodys[custodyIndex].String()), + sdk.NewAttribute("interest_unpaid_collateral", mtp.InterestUnpaidCollaterals[collateralIndex].String()), sdk.NewAttribute("health", mtp.MtpHealth.String()), ) } diff --git a/x/margin/keeper/get_mtp_asset_index.go b/x/margin/keeper/get_mtp_asset_index.go new file mode 100644 index 000000000..8095ef792 --- /dev/null +++ b/x/margin/keeper/get_mtp_asset_index.go @@ -0,0 +1,26 @@ +package keeper + +import ( + "github.com/elys-network/elys/x/margin/types" +) + +// Get Assets Index +func (k Keeper) GetMTPAssetIndex(mtp *types.MTP, collateralAsset string, borrowAsset string) (int, int) { + collateralIndex := -1 + borrowIndex := -1 + for i, asset := range mtp.CollateralAssets { + if asset == collateralAsset { + collateralIndex = i + break + } + } + + for i, asset := range mtp.CustodyAssets { + if asset == borrowAsset { + borrowIndex = i + break + } + } + + return collateralIndex, borrowIndex +} diff --git a/x/margin/keeper/handle_interest.go b/x/margin/keeper/handle_interest.go index 277c14da4..1aa3f95fb 100644 --- a/x/margin/keeper/handle_interest.go +++ b/x/margin/keeper/handle_interest.go @@ -6,17 +6,18 @@ import ( "github.com/elys-network/elys/x/margin/types" ) -func (k Keeper) HandleInterest(ctx sdk.Context, mtp *types.MTP, pool *types.Pool, ammPool ammtypes.Pool) error { +func (k Keeper) HandleInterest(ctx sdk.Context, mtp *types.MTP, pool *types.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) error { epochLength := k.GetEpochLength(ctx) epochPosition := k.GetEpochPosition(ctx, epochLength) if epochPosition <= 0 { return nil } - interestPayment := k.CalcMTPInterestLiabilities(mtp, pool.InterestRate, epochPosition, epochLength) - finalInterestPayment := k.HandleInterestPayment(ctx, interestPayment, mtp, pool, ammPool) + interestPayment := k.CalcMTPInterestLiabilities(ctx, mtp, pool.InterestRate, epochPosition, epochLength, ammPool, collateralAsset) + finalInterestPayment := k.HandleInterestPayment(ctx, collateralAsset, custodyAsset, interestPayment, mtp, pool, ammPool) - if err := pool.UpdateBlockInterest(ctx, mtp.CollateralAsset, finalInterestPayment, true); err != nil { + // finalInterestPayment is in custodyAsset + if err := pool.UpdateBlockInterest(ctx, custodyAsset, finalInterestPayment, true); err != nil { return err } diff --git a/x/margin/keeper/handle_interest_payment.go b/x/margin/keeper/handle_interest_payment.go index 83f14ba1a..205251b7e 100644 --- a/x/margin/keeper/handle_interest_payment.go +++ b/x/margin/keeper/handle_interest_payment.go @@ -5,20 +5,38 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ammtypes "github.com/elys-network/elys/x/amm/types" "github.com/elys-network/elys/x/margin/types" + ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (k Keeper) HandleInterestPayment(ctx sdk.Context, interestPayment sdk.Int, mtp *types.MTP, pool *types.Pool, ammPool ammtypes.Pool) sdk.Int { +func (k Keeper) HandleInterestPayment(ctx sdk.Context, collateralAsset string, custodyAsset string, interestPayment sdk.Int, mtp *types.MTP, pool *types.Pool, ammPool ammtypes.Pool) sdk.Int { incrementalInterestPaymentEnabled := k.GetIncrementalInterestPaymentEnabled(ctx) // if incremental payment on, pay interest if incrementalInterestPaymentEnabled { - finalInterestPayment, err := k.IncrementalInterestPayment(ctx, interestPayment, mtp, pool, ammPool) + finalInterestPayment, err := k.IncrementalInterestPayment(ctx, collateralAsset, custodyAsset, interestPayment, mtp, pool, ammPool) if err != nil { ctx.Logger().Error(sdkerrors.Wrap(err, "error executing incremental interest payment").Error()) } else { return finalInterestPayment } } else { // else update unpaid mtp interest - mtp.InterestUnpaidCollateral = interestPayment + collateralIndex, _ := k.GetMTPAssetIndex(mtp, collateralAsset, "") + if collateralIndex < 0 { + return sdk.ZeroInt() + } + + // collateralAsset is in usdc + if mtp.CollateralAssets[collateralIndex] == ptypes.USDC { + mtp.InterestUnpaidCollaterals[collateralIndex] = interestPayment + } else { + // swap + amtTokenIn := sdk.NewCoin(ptypes.USDC, interestPayment) + interestPayment, err := k.EstimateSwap(ctx, amtTokenIn, collateralAsset, ammPool) // may need spot price here to not deduct fee + if err != nil { + return sdk.ZeroInt() + } + + mtp.InterestUnpaidCollaterals[collateralIndex] = interestPayment + } } return sdk.ZeroInt() } diff --git a/x/margin/keeper/invariant_check.go b/x/margin/keeper/invariant_check.go index 74231a352..18fd96af7 100644 --- a/x/margin/keeper/invariant_check.go +++ b/x/margin/keeper/invariant_check.go @@ -29,7 +29,9 @@ func (k Keeper) AmmPoolBalanceCheck(ctx sdk.Context, poolId uint64) error { continue } - mtpCollateralBalances = mtpCollateralBalances.Add(sdk.NewCoin(mtp.CollateralAsset, mtp.CollateralAmount)) + for i := range mtp.CollateralAssets { + mtpCollateralBalances = mtpCollateralBalances.Add(sdk.NewCoin(mtp.CollateralAssets[i], mtp.CollateralAmounts[i])) + } } // bank balance should be ammPool balance + collateral balance diff --git a/x/margin/keeper/keeper.go b/x/margin/keeper/keeper.go index dff5611ff..5067246d0 100644 --- a/x/margin/keeper/keeper.go +++ b/x/margin/keeper/keeper.go @@ -130,7 +130,7 @@ func (k Keeper) EstimateSwapGivenOut(ctx sdk.Context, tokenOutAmount sdk.Coin, t return swapResult.Amount, nil } -func (k Keeper) Borrow(ctx sdk.Context, collateralAsset string, collateralAmount sdk.Int, custodyAmount sdk.Int, mtp *types.MTP, ammPool *ammtypes.Pool, pool *types.Pool, eta sdk.Dec) error { +func (k Keeper) Borrow(ctx sdk.Context, collateralAsset string, custodyAsset string, collateralAmount sdk.Int, custodyAmount sdk.Int, mtp *types.MTP, ammPool *ammtypes.Pool, pool *types.Pool, eta sdk.Dec) error { mtpAddress, err := sdk.AccAddressFromBech32(mtp.Address) if err != nil { return err @@ -145,6 +145,7 @@ func (k Keeper) Borrow(ctx sdk.Context, collateralAsset string, collateralAmount liabilitiesDec := collateralAmountDec.Mul(eta) // If collateral asset is not usdc, should calculate liability in usdc with the given out. + // Liability has to be in USDC if collateralAsset != ptypes.USDC { // ATOM amount etaAmt := liabilitiesDec.TruncateInt() @@ -158,13 +159,18 @@ func (k Keeper) Borrow(ctx sdk.Context, collateralAsset string, collateralAmount liabilitiesDec = sdk.NewDecFromInt(liabilityAmt) } - mtp.CollateralAmount = mtp.CollateralAmount.Add(collateralAmount) + collateralIndex, custodyIndex := k.GetMTPAssetIndex(mtp, collateralAsset, custodyAsset) + if collateralIndex < 0 || custodyIndex < 0 { + return sdkerrors.Wrap(types.ErrBalanceNotAvailable, "MTP collateral or custody invalid!") + } + + mtp.CollateralAmounts[collateralIndex] = mtp.CollateralAmounts[collateralIndex].Add(collateralAmount) mtp.Liabilities = mtp.Liabilities.Add(sdk.NewIntFromBigInt(liabilitiesDec.TruncateInt().BigInt())) - mtp.CustodyAmount = mtp.CustodyAmount.Add(custodyAmount) - mtp.Leverage = eta.Add(sdk.OneDec()) + mtp.CustodyAmounts[custodyIndex] = mtp.CustodyAmounts[custodyIndex].Add(custodyAmount) + mtp.Leverages = append(mtp.Leverages, eta.Add(sdk.OneDec())) // print mtp.CustodyAmount - ctx.Logger().Info(fmt.Sprintf("mtp.CustodyAmount: %s", mtp.CustodyAmount.String())) + ctx.Logger().Info(fmt.Sprintf("mtp.CustodyAmount: %s", mtp.CustodyAmounts[custodyIndex].String())) h, err := k.UpdateMTPHealth(ctx, *mtp, *ammPool) // set mtp in func or return h? if err != nil { @@ -235,14 +241,15 @@ func (k Keeper) CalculatePoolHealth(ctx sdk.Context, pool *types.Pool) sdk.Dec { } func (k Keeper) TakeInCustody(ctx sdk.Context, mtp types.MTP, pool *types.Pool) error { - err := pool.UpdateBalance(ctx, mtp.CustodyAsset, mtp.CustodyAmount, false) - if err != nil { - return nil - } - - err = pool.UpdateCustody(ctx, mtp.CustodyAsset, mtp.CustodyAmount, true) - if err != nil { - return nil + for i := range mtp.CustodyAssets { + err := pool.UpdateBalance(ctx, mtp.CustodyAssets[i], mtp.CustodyAmounts[i], false) + if err != nil { + return nil + } + err = pool.UpdateCustody(ctx, mtp.CustodyAssets[i], mtp.CustodyAmounts[i], true) + if err != nil { + return nil + } } k.SetPool(ctx, *pool) @@ -250,62 +257,85 @@ func (k Keeper) TakeInCustody(ctx sdk.Context, mtp types.MTP, pool *types.Pool) return nil } -func (k Keeper) IncrementalInterestPayment(ctx sdk.Context, interestPayment sdk.Int, mtp *types.MTP, pool *types.Pool, ammPool ammtypes.Pool) (sdk.Int, error) { +func (k Keeper) IncrementalInterestPayment(ctx sdk.Context, collateralAsset string, custodyAsset string, interestPayment sdk.Int, mtp *types.MTP, pool *types.Pool, ammPool ammtypes.Pool) (sdk.Int, error) { + collateralIndex, custodyIndex := k.GetMTPAssetIndex(mtp, collateralAsset, custodyAsset) // if mtp has unpaid interest, add to payment - if mtp.InterestUnpaidCollateral.GT(sdk.ZeroInt()) { - interestPayment = interestPayment.Add(mtp.InterestUnpaidCollateral) + // convert it into usdc + if mtp.InterestUnpaidCollaterals[collateralIndex].GT(sdk.ZeroInt()) { + if mtp.CollateralAssets[collateralIndex] == ptypes.USDC { + interestPayment = interestPayment.Add(mtp.InterestUnpaidCollaterals[collateralIndex]) + } else { + unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAssets[collateralIndex], mtp.InterestUnpaidCollaterals[collateralIndex]) + C, err := k.EstimateSwapGivenOut(ctx, unpaidCollateralIn, ptypes.USDC, ammPool) + if err != nil { + return sdk.ZeroInt(), err + } + + interestPayment = interestPayment.Add(C) + } } - interestPaymentTokenIn := sdk.NewCoin(mtp.CollateralAsset, interestPayment) + interestPaymentTokenIn := sdk.NewCoin(ptypes.USDC, interestPayment) // swap interest payment to custody asset for payment - interestPaymentCustody, err := k.EstimateSwap(ctx, interestPaymentTokenIn, mtp.CustodyAsset, ammPool) + interestPaymentCustody, err := k.EstimateSwap(ctx, interestPaymentTokenIn, mtp.CustodyAssets[custodyIndex], ammPool) if err != nil { return sdk.ZeroInt(), err } + // If collateralAset is not in usdc, convert it to original asset format + if collateralAsset != ptypes.USDC { + // swap custody amount to collateral for updating interest unpaid + amtTokenIn := sdk.NewCoin(ptypes.USDC, interestPayment) + interestPayment, err = k.EstimateSwap(ctx, amtTokenIn, collateralAsset, ammPool) // may need spot price here to not deduct fee + if err != nil { + return sdk.ZeroInt(), err + } + } + // if paying unpaid interest reset to 0 - mtp.InterestUnpaidCollateral = sdk.ZeroInt() + mtp.InterestUnpaidCollaterals[collateralIndex] = sdk.ZeroInt() // edge case, not enough custody to cover payment - if interestPaymentCustody.GT(mtp.CustodyAmount) { + if interestPaymentCustody.GT(mtp.CustodyAmounts[custodyIndex]) { // swap custody amount to collateral for updating interest unpaid - custodyAmtTokenIn := sdk.NewCoin(mtp.CustodyAsset, mtp.CustodyAmount) - custodyAmountCollateral, err := k.EstimateSwap(ctx, custodyAmtTokenIn, mtp.CollateralAsset, ammPool) // may need spot price here to not deduct fee + custodyAmtTokenIn := sdk.NewCoin(mtp.CustodyAssets[custodyIndex], mtp.CustodyAmounts[custodyIndex]) + custodyAmountCollateral, err := k.EstimateSwap(ctx, custodyAmtTokenIn, collateralAsset, ammPool) // may need spot price here to not deduct fee if err != nil { return sdk.ZeroInt(), err } - mtp.InterestUnpaidCollateral = interestPayment.Sub(custodyAmountCollateral) + mtp.InterestUnpaidCollaterals[collateralIndex] = interestPayment.Sub(custodyAmountCollateral) + interestPayment = custodyAmountCollateral - interestPaymentCustody = mtp.CustodyAmount + interestPaymentCustody = mtp.CustodyAmounts[custodyIndex] } // add payment to total paid - collateral - mtp.InterestPaidCollateral = mtp.InterestPaidCollateral.Add(interestPayment) + mtp.InterestPaidCollaterals[collateralIndex] = mtp.InterestPaidCollaterals[collateralIndex].Add(interestPayment) // add payment to total paid - custody - mtp.InterestPaidCustody = mtp.InterestPaidCustody.Add(interestPaymentCustody) + mtp.InterestPaidCustodys[custodyIndex] = mtp.InterestPaidCustodys[custodyIndex].Add(interestPaymentCustody) // deduct interest payment from custody amount - mtp.CustodyAmount = mtp.CustodyAmount.Sub(interestPaymentCustody) + mtp.CustodyAmounts[custodyIndex] = mtp.CustodyAmounts[custodyIndex].Sub(interestPaymentCustody) takePercentage := k.GetIncrementalInterestPaymentFundPercentage(ctx) fundAddr := k.GetIncrementalInterestPaymentFundAddress(ctx) - takeAmount, err := k.TakeFundPayment(ctx, interestPaymentCustody, mtp.CustodyAsset, takePercentage, fundAddr, &ammPool) + takeAmount, err := k.TakeFundPayment(ctx, interestPaymentCustody, mtp.CustodyAssets[custodyIndex], takePercentage, fundAddr, &ammPool) if err != nil { return sdk.ZeroInt(), err } actualInterestPaymentCustody := interestPaymentCustody.Sub(takeAmount) if !takeAmount.IsZero() { - k.EmitFundPayment(ctx, mtp, takeAmount, mtp.CustodyAsset, types.EventIncrementalPayFund) + k.EmitFundPayment(ctx, mtp, takeAmount, mtp.CustodyAssets[custodyIndex], types.EventIncrementalPayFund) } - err = pool.UpdateCustody(ctx, mtp.CustodyAsset, interestPaymentCustody, false) + err = pool.UpdateCustody(ctx, mtp.CustodyAssets[custodyIndex], interestPaymentCustody, false) if err != nil { return sdk.ZeroInt(), err } - err = pool.UpdateBalance(ctx, mtp.CustodyAsset, actualInterestPaymentCustody, true) + err = pool.UpdateBalance(ctx, mtp.CustodyAssets[custodyIndex], actualInterestPaymentCustody, true) if err != nil { return sdk.ZeroInt(), err } diff --git a/x/margin/keeper/keeper_test.go b/x/margin/keeper/keeper_test.go index d9a17af5f..c1eae8bca 100644 --- a/x/margin/keeper/keeper_test.go +++ b/x/margin/keeper/keeper_test.go @@ -28,19 +28,19 @@ func TestSetGetMTP(t *testing.T) { for i := 0; i < 2; i++ { mtp := types.MTP{ - Address: addr[i].String(), - CollateralAsset: paramtypes.USDC, - CollateralAmount: sdk.NewInt(0), - Liabilities: sdk.NewInt(0), - InterestPaidCollateral: sdk.NewInt(0), - InterestPaidCustody: sdk.NewInt(0), - InterestUnpaidCollateral: sdk.NewInt(0), - CustodyAsset: "ATOM", - CustodyAmount: sdk.NewInt(0), - Leverage: sdk.NewDec(0), - MtpHealth: sdk.NewDec(0), - Position: types.Position_LONG, - Id: 0, + Address: addr[i].String(), + CollateralAssets: []string{paramtypes.USDC}, + CollateralAmounts: []sdk.Int{sdk.NewInt(0)}, + Liabilities: sdk.NewInt(0), + InterestPaidCollaterals: []sdk.Int{sdk.NewInt(0)}, + InterestPaidCustodys: []sdk.Int{sdk.NewInt(0)}, + InterestUnpaidCollaterals: []sdk.Int{sdk.NewInt(0)}, + CustodyAssets: []string{"ATOM"}, + CustodyAmounts: []sdk.Int{sdk.NewInt(0)}, + Leverages: []sdk.Dec{sdk.NewDec(0)}, + MtpHealth: sdk.NewDec(0), + Position: types.Position_LONG, + Id: 0, } nullify.Fill(&mtp) diff --git a/x/margin/keeper/open.go b/x/margin/keeper/open.go index 95dcc7052..d892b8f40 100644 --- a/x/margin/keeper/open.go +++ b/x/margin/keeper/open.go @@ -15,8 +15,8 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenRespons return nil, err } - // Check if it is the same asset, same direction position for the same trader. - if mtp := k.CheckSameAssetPosition(ctx, msg); mtp != nil { + // Check if it is the same direction position for the same trader. + if mtp := k.CheckSamePosition(ctx, msg); mtp != nil { return k.OpenConsolidate(ctx, mtp, msg) } diff --git a/x/margin/keeper/open_consolidate_long.go b/x/margin/keeper/open_consolidate_long.go index b2fec4d3d..39293d51a 100644 --- a/x/margin/keeper/open_consolidate_long.go +++ b/x/margin/keeper/open_consolidate_long.go @@ -10,7 +10,7 @@ func (k Keeper) OpenConsolidateLong(ctx sdk.Context, poolId uint64, mtp *types.M leverage := sdk.MinDec(msg.Leverage, maxLeverage) eta := leverage.Sub(sdk.OneDec()) collateralAmountDec := sdk.NewDecFromBigInt(msg.CollateralAmount.BigInt()) - mtp.Leverage = leverage + mtp.Leverages = append(mtp.Leverages, leverage) return k.ProcessOpenLong(ctx, mtp, leverage, eta, collateralAmountDec, poolId, msg) } diff --git a/x/margin/keeper/open_long_process.go b/x/margin/keeper/open_long_process.go index 00520bfe6..a294bb8dd 100644 --- a/x/margin/keeper/open_long_process.go +++ b/x/margin/keeper/open_long_process.go @@ -63,7 +63,7 @@ func (k Keeper) ProcessOpenLong(ctx sdk.Context, mtp *types.MTP, leverage sdk.De return nil, sdkerrors.Wrap(types.ErrCustodyTooHigh, custodyAmount.String()) } - err = k.OpenLongChecker.Borrow(ctx, msg.CollateralAsset, msg.CollateralAmount, custodyAmount, mtp, &ammPool, &pool, eta) + err = k.OpenLongChecker.Borrow(ctx, msg.CollateralAsset, msg.BorrowAsset, msg.CollateralAmount, custodyAmount, mtp, &ammPool, &pool, eta) if err != nil { return nil, err } diff --git a/x/margin/keeper/repay.go b/x/margin/keeper/repay.go index cf508895e..d8c8eddba 100644 --- a/x/margin/keeper/repay.go +++ b/x/margin/keeper/repay.go @@ -4,13 +4,26 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ammtypes "github.com/elys-network/elys/x/amm/types" "github.com/elys-network/elys/x/margin/types" + ptypes "github.com/elys-network/elys/x/parameter/types" ) -func (k Keeper) Repay(ctx sdk.Context, mtp *types.MTP, pool *types.Pool, ammPool ammtypes.Pool, repayAmount sdk.Int, takeFundPayment bool) error { +func (k Keeper) Repay(ctx sdk.Context, mtp *types.MTP, pool *types.Pool, ammPool ammtypes.Pool, repayAmount sdk.Int, takeFundPayment bool, collateralAsset string) error { + collateralIndex, _ := k.GetMTPAssetIndex(mtp, collateralAsset, "") // nolint:staticcheck,ineffassign returnAmount, debtP, debtI := sdk.ZeroInt(), sdk.ZeroInt(), sdk.ZeroInt() Liabilities := mtp.Liabilities - InterestUnpaidCollateral := mtp.InterestUnpaidCollateral + InterestUnpaidCollateral := mtp.InterestUnpaidCollaterals[collateralIndex] + + if collateralAsset != ptypes.USDC { + // swap to usdc + unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAssets[collateralIndex], mtp.InterestUnpaidCollaterals[collateralIndex]) + C, err := k.EstimateSwapGivenOut(ctx, unpaidCollateralIn, ptypes.USDC, ammPool) + if err != nil { + return err + } + + InterestUnpaidCollateral = C + } var err error mtp.MtpHealth, err = k.UpdateMTPHealth(ctx, *mtp, ammPool) @@ -37,25 +50,38 @@ func (k Keeper) Repay(ctx sdk.Context, mtp *types.MTP, pool *types.Pool, ammPool debtP = sdk.ZeroInt() debtI = sdk.ZeroInt() } + if !returnAmount.IsZero() { actualReturnAmount := returnAmount if takeFundPayment { takePercentage := k.GetForceCloseFundPercentage(ctx) fundAddr := k.GetForceCloseFundAddress(ctx) - takeAmount, err := k.TakeFundPayment(ctx, returnAmount, mtp.CollateralAsset, takePercentage, fundAddr, &ammPool) + takeAmount, err := k.TakeFundPayment(ctx, returnAmount, ptypes.USDC, takePercentage, fundAddr, &ammPool) if err != nil { return err } actualReturnAmount = returnAmount.Sub(takeAmount) if !takeAmount.IsZero() { - k.EmitFundPayment(ctx, mtp, takeAmount, mtp.CollateralAsset, types.EventRepayFund) + k.EmitFundPayment(ctx, mtp, takeAmount, ptypes.USDC, types.EventRepayFund) } } + // actualReturnAmount is so far in usdc, now should convert it to collateralAsset in order to return if !actualReturnAmount.IsZero() { + if collateralAsset != ptypes.USDC { + // swap to usdc + amtTokenIn := sdk.NewCoin(ptypes.USDC, actualReturnAmount) + C, err := k.EstimateSwapGivenOut(ctx, amtTokenIn, collateralAsset, ammPool) + if err != nil { + return err + } + + actualReturnAmount = C + } + var coins sdk.Coins - returnCoin := sdk.NewCoin(mtp.CollateralAsset, sdk.NewIntFromBigInt(actualReturnAmount.BigInt())) + returnCoin := sdk.NewCoin(collateralAsset, sdk.NewIntFromBigInt(actualReturnAmount.BigInt())) returnCoins := coins.Add(returnCoin) addr, err := sdk.AccAddressFromBech32(mtp.Address) if err != nil { @@ -74,22 +100,39 @@ func (k Keeper) Repay(ctx sdk.Context, mtp *types.MTP, pool *types.Pool, ammPool } } - err = pool.UpdateBalance(ctx, mtp.CollateralAsset, returnAmount, false) + // before updating collateral asset balance, we should convert returnAmount to collateralAsset + // because so far returnAmount is in usdc. + if collateralAsset != ptypes.USDC { + // swap to usdc + amtTokenIn := sdk.NewCoin(ptypes.USDC, returnAmount) + C, err := k.EstimateSwapGivenOut(ctx, amtTokenIn, collateralAsset, ammPool) + if err != nil { + return err + } + + returnAmount = C + } + + err = pool.UpdateBalance(ctx, mtp.CollateralAssets[collateralIndex], returnAmount, false) if err != nil { return err } - err = pool.UpdateLiabilities(ctx, mtp.CollateralAsset, mtp.Liabilities, false) + // Need to be checked by Caner for short + // long position + err = pool.UpdateLiabilities(ctx, ptypes.USDC, mtp.Liabilities, false) if err != nil { return err } - err = pool.UpdateUnsettledLiabilities(ctx, mtp.CollateralAsset, debtI, true) + // long position + err = pool.UpdateUnsettledLiabilities(ctx, ptypes.USDC, debtI, true) if err != nil { return err } - err = pool.UpdateUnsettledLiabilities(ctx, mtp.CollateralAsset, debtP, true) + // long position + err = pool.UpdateUnsettledLiabilities(ctx, ptypes.USDC, debtP, true) if err != nil { return err } diff --git a/x/margin/keeper/take_out_custody.go b/x/margin/keeper/take_out_custody.go index b455a685b..114d7cc0a 100644 --- a/x/margin/keeper/take_out_custody.go +++ b/x/margin/keeper/take_out_custody.go @@ -5,13 +5,14 @@ import ( "github.com/elys-network/elys/x/margin/types" ) -func (k Keeper) TakeOutCustody(ctx sdk.Context, mtp types.MTP, pool *types.Pool) error { - err := pool.UpdateBalance(ctx, mtp.CustodyAsset, mtp.CustodyAmount, true) +func (k Keeper) TakeOutCustody(ctx sdk.Context, mtp types.MTP, pool *types.Pool, custodyAsset string) error { + _, custodyIndex := k.GetMTPAssetIndex(&mtp, "", custodyAsset) + err := pool.UpdateBalance(ctx, mtp.CustodyAssets[custodyIndex], mtp.CustodyAmounts[custodyIndex], true) if err != nil { return err } - err = pool.UpdateCustody(ctx, mtp.CustodyAsset, mtp.CustodyAmount, false) + err = pool.UpdateCustody(ctx, mtp.CustodyAssets[custodyIndex], mtp.CustodyAmounts[custodyIndex], false) if err != nil { return err } diff --git a/x/margin/keeper/update_mtp_health.go b/x/margin/keeper/update_mtp_health.go index 39ae5bc2e..e29613c2c 100644 --- a/x/margin/keeper/update_mtp_health.go +++ b/x/margin/keeper/update_mtp_health.go @@ -14,18 +14,35 @@ func (k Keeper) UpdateMTPHealth(ctx sdk.Context, mtp types.MTP, ammPool ammtypes return sdk.ZeroDec(), nil } // include unpaid interest in debt (from disabled incremental pay) - if mtp.InterestUnpaidCollateral.GT(sdk.ZeroInt()) { - xl = xl.Add(mtp.InterestUnpaidCollateral) + for i := range mtp.CollateralAssets { + if mtp.InterestUnpaidCollaterals[i].GT(sdk.ZeroInt()) { + unpaidCollaterals := sdk.NewCoin(mtp.CollateralAssets[i], mtp.InterestUnpaidCollaterals[i]) + + if mtp.CollateralAssets[i] == ptypes.USDC { + xl = xl.Add(mtp.InterestUnpaidCollaterals[i]) + } else { + C, err := k.EstimateSwapGivenOut(ctx, unpaidCollaterals, ptypes.USDC, ammPool) + if err != nil { + return sdk.ZeroDec(), err + } + + xl = xl.Add(C) + } + } } - custodyTokenIn := sdk.NewCoin(mtp.CustodyAsset, mtp.CustodyAmount) - // All liabilty is in usdc - C, err := k.EstimateSwapGivenOut(ctx, custodyTokenIn, ptypes.USDC, ammPool) - if err != nil { - return sdk.ZeroDec(), err + custodyAmtInUSDC := sdk.ZeroInt() + for i := range mtp.CustodyAssets { + custodyTokenIn := sdk.NewCoin(mtp.CustodyAssets[i], mtp.CustodyAmounts[i]) + // All liabilty is in usdc + C, err := k.EstimateSwapGivenOut(ctx, custodyTokenIn, ptypes.USDC, ammPool) + if err != nil { + return sdk.ZeroDec(), err + } + custodyAmtInUSDC = custodyAmtInUSDC.Add(C) } - lr := sdk.NewDecFromBigInt(C.BigInt()).Quo(sdk.NewDecFromBigInt(xl.BigInt())) + lr := sdk.NewDecFromBigInt(custodyAmtInUSDC.BigInt()).Quo(sdk.NewDecFromBigInt(xl.BigInt())) return lr, nil } diff --git a/x/margin/types/expected_keepers.go b/x/margin/types/expected_keepers.go index bfc650e91..ef3a97d0b 100644 --- a/x/margin/types/expected_keepers.go +++ b/x/margin/types/expected_keepers.go @@ -51,7 +51,7 @@ type OpenLongChecker interface { CheckMinLiabilities(ctx sdk.Context, collateralTokenAmt sdk.Coin, eta sdk.Dec, pool Pool, ammPool ammtypes.Pool, borrowAsset string) error EstimateSwap(ctx sdk.Context, leveragedAmtTokenIn sdk.Coin, borrowAsset string, ammPool ammtypes.Pool) (sdk.Int, error) EstimateSwapGivenOut(ctx sdk.Context, tokenOutAmount sdk.Coin, tokenInDenom string, ammPool ammtypes.Pool) (sdk.Int, error) - Borrow(ctx sdk.Context, collateralAsset string, collateralAmount sdk.Int, custodyAmount sdk.Int, mtp *MTP, ammPool *ammtypes.Pool, pool *Pool, eta sdk.Dec) error + Borrow(ctx sdk.Context, collateralAsset string, custodyAsset string, collateralAmount sdk.Int, custodyAmount sdk.Int, mtp *MTP, ammPool *ammtypes.Pool, pool *Pool, eta sdk.Dec) error UpdatePoolHealth(ctx sdk.Context, pool *Pool) error TakeInCustody(ctx sdk.Context, mtp MTP, pool *Pool) error UpdateMTPHealth(ctx sdk.Context, mtp MTP, ammPool ammtypes.Pool) (sdk.Dec, error) @@ -59,7 +59,7 @@ type OpenLongChecker interface { SetPool(ctx sdk.Context, pool Pool) GetAmmPoolBalance(ctx sdk.Context, ammPool ammtypes.Pool, assetDenom string) (sdk.Int, error) CheckLongingAssets(ctx sdk.Context, collateralAsset string, borrowAsset string) error - CheckSameAssetPosition(ctx sdk.Context, msg *MsgOpen) *MTP + CheckSamePosition(ctx sdk.Context, msg *MsgOpen) *MTP } //go:generate mockery --srcpkg . --name CloseLongChecker --structname CloseLongChecker --filename close_long_checker.go --with-expecter @@ -71,9 +71,9 @@ type CloseLongChecker interface { ) (val Pool, found bool) GetAmmPool(ctx sdk.Context, poolId uint64, nonNativeAsset string) (ammtypes.Pool, error) - HandleInterest(ctx sdk.Context, mtp *MTP, pool *Pool, ammPool ammtypes.Pool) error - TakeOutCustody(ctx sdk.Context, mtp MTP, pool *Pool) error - EstimateAndRepay(ctx sdk.Context, mtp MTP, pool Pool, ammPool ammtypes.Pool) (sdk.Int, error) + HandleInterest(ctx sdk.Context, mtp *MTP, pool *Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) error + TakeOutCustody(ctx sdk.Context, mtp MTP, pool *Pool, custodyAsset string) error + EstimateAndRepay(ctx sdk.Context, mtp MTP, pool Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) (sdk.Int, error) } //go:generate mockery --srcpkg . --name CloseShortChecker --structname CloseShortChecker --filename close_short_checker.go --with-expecter @@ -85,9 +85,9 @@ type CloseShortChecker interface { ) (val Pool, found bool) GetAmmPool(ctx sdk.Context, poolId uint64, nonNativeAsset string) (ammtypes.Pool, error) - HandleInterest(ctx sdk.Context, mtp *MTP, pool *Pool, ammPool ammtypes.Pool) error - TakeOutCustody(ctx sdk.Context, mtp MTP, pool *Pool) error - EstimateAndRepay(ctx sdk.Context, mtp MTP, pool Pool, ammPool ammtypes.Pool) (sdk.Int, error) + HandleInterest(ctx sdk.Context, mtp *MTP, pool *Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) error + TakeOutCustody(ctx sdk.Context, mtp MTP, pool *Pool, custodyAsset string) error + EstimateAndRepay(ctx sdk.Context, mtp MTP, pool Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) (sdk.Int, error) } // AccountKeeper defines the expected account keeper used for simulations (noalias) diff --git a/x/margin/types/mocks/close_long_checker.go b/x/margin/types/mocks/close_long_checker.go index 5d900c128..ce5c1b11b 100644 --- a/x/margin/types/mocks/close_long_checker.go +++ b/x/margin/types/mocks/close_long_checker.go @@ -26,23 +26,23 @@ func (_m *CloseLongChecker) EXPECT() *CloseLongChecker_Expecter { return &CloseLongChecker_Expecter{mock: &_m.Mock} } -// EstimateAndRepay provides a mock function with given fields: ctx, mtp, pool, ammPool -func (_m *CloseLongChecker) EstimateAndRepay(ctx types.Context, mtp margintypes.MTP, pool margintypes.Pool, ammPool ammtypes.Pool) (math.Int, error) { - ret := _m.Called(ctx, mtp, pool, ammPool) +// EstimateAndRepay provides a mock function with given fields: ctx, mtp, pool, ammPool, collateralAsset, custodyAsset +func (_m *CloseLongChecker) EstimateAndRepay(ctx types.Context, mtp margintypes.MTP, pool margintypes.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) (math.Int, error) { + ret := _m.Called(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) var r0 math.Int var r1 error - if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool) (math.Int, error)); ok { - return rf(ctx, mtp, pool, ammPool) + if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool, string, string) (math.Int, error)); ok { + return rf(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) } - if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool) math.Int); ok { - r0 = rf(ctx, mtp, pool, ammPool) + if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool, string, string) math.Int); ok { + r0 = rf(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) } else { r0 = ret.Get(0).(math.Int) } - if rf, ok := ret.Get(1).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool) error); ok { - r1 = rf(ctx, mtp, pool, ammPool) + if rf, ok := ret.Get(1).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool, string, string) error); ok { + r1 = rf(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) } else { r1 = ret.Error(1) } @@ -60,13 +60,15 @@ type CloseLongChecker_EstimateAndRepay_Call struct { // - mtp margintypes.MTP // - pool margintypes.Pool // - ammPool ammtypes.Pool -func (_e *CloseLongChecker_Expecter) EstimateAndRepay(ctx interface{}, mtp interface{}, pool interface{}, ammPool interface{}) *CloseLongChecker_EstimateAndRepay_Call { - return &CloseLongChecker_EstimateAndRepay_Call{Call: _e.mock.On("EstimateAndRepay", ctx, mtp, pool, ammPool)} +// - collateralAsset string +// - custodyAsset string +func (_e *CloseLongChecker_Expecter) EstimateAndRepay(ctx interface{}, mtp interface{}, pool interface{}, ammPool interface{}, collateralAsset interface{}, custodyAsset interface{}) *CloseLongChecker_EstimateAndRepay_Call { + return &CloseLongChecker_EstimateAndRepay_Call{Call: _e.mock.On("EstimateAndRepay", ctx, mtp, pool, ammPool, collateralAsset, custodyAsset)} } -func (_c *CloseLongChecker_EstimateAndRepay_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, pool margintypes.Pool, ammPool ammtypes.Pool)) *CloseLongChecker_EstimateAndRepay_Call { +func (_c *CloseLongChecker_EstimateAndRepay_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, pool margintypes.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string)) *CloseLongChecker_EstimateAndRepay_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(margintypes.Pool), args[3].(ammtypes.Pool)) + run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(margintypes.Pool), args[3].(ammtypes.Pool), args[4].(string), args[5].(string)) }) return _c } @@ -76,7 +78,7 @@ func (_c *CloseLongChecker_EstimateAndRepay_Call) Return(_a0 math.Int, _a1 error return _c } -func (_c *CloseLongChecker_EstimateAndRepay_Call) RunAndReturn(run func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool) (math.Int, error)) *CloseLongChecker_EstimateAndRepay_Call { +func (_c *CloseLongChecker_EstimateAndRepay_Call) RunAndReturn(run func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool, string, string) (math.Int, error)) *CloseLongChecker_EstimateAndRepay_Call { _c.Call.Return(run) return _c } @@ -242,13 +244,13 @@ func (_c *CloseLongChecker_GetPool_Call) RunAndReturn(run func(types.Context, ui return _c } -// HandleInterest provides a mock function with given fields: ctx, mtp, pool, ammPool -func (_m *CloseLongChecker) HandleInterest(ctx types.Context, mtp *margintypes.MTP, pool *margintypes.Pool, ammPool ammtypes.Pool) error { - ret := _m.Called(ctx, mtp, pool, ammPool) +// HandleInterest provides a mock function with given fields: ctx, mtp, pool, ammPool, collateralAsset, custodyAsset +func (_m *CloseLongChecker) HandleInterest(ctx types.Context, mtp *margintypes.MTP, pool *margintypes.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) error { + ret := _m.Called(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) var r0 error - if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MTP, *margintypes.Pool, ammtypes.Pool) error); ok { - r0 = rf(ctx, mtp, pool, ammPool) + if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MTP, *margintypes.Pool, ammtypes.Pool, string, string) error); ok { + r0 = rf(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) } else { r0 = ret.Error(0) } @@ -266,13 +268,15 @@ type CloseLongChecker_HandleInterest_Call struct { // - mtp *margintypes.MTP // - pool *margintypes.Pool // - ammPool ammtypes.Pool -func (_e *CloseLongChecker_Expecter) HandleInterest(ctx interface{}, mtp interface{}, pool interface{}, ammPool interface{}) *CloseLongChecker_HandleInterest_Call { - return &CloseLongChecker_HandleInterest_Call{Call: _e.mock.On("HandleInterest", ctx, mtp, pool, ammPool)} +// - collateralAsset string +// - custodyAsset string +func (_e *CloseLongChecker_Expecter) HandleInterest(ctx interface{}, mtp interface{}, pool interface{}, ammPool interface{}, collateralAsset interface{}, custodyAsset interface{}) *CloseLongChecker_HandleInterest_Call { + return &CloseLongChecker_HandleInterest_Call{Call: _e.mock.On("HandleInterest", ctx, mtp, pool, ammPool, collateralAsset, custodyAsset)} } -func (_c *CloseLongChecker_HandleInterest_Call) Run(run func(ctx types.Context, mtp *margintypes.MTP, pool *margintypes.Pool, ammPool ammtypes.Pool)) *CloseLongChecker_HandleInterest_Call { +func (_c *CloseLongChecker_HandleInterest_Call) Run(run func(ctx types.Context, mtp *margintypes.MTP, pool *margintypes.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string)) *CloseLongChecker_HandleInterest_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.Context), args[1].(*margintypes.MTP), args[2].(*margintypes.Pool), args[3].(ammtypes.Pool)) + run(args[0].(types.Context), args[1].(*margintypes.MTP), args[2].(*margintypes.Pool), args[3].(ammtypes.Pool), args[4].(string), args[5].(string)) }) return _c } @@ -282,18 +286,18 @@ func (_c *CloseLongChecker_HandleInterest_Call) Return(_a0 error) *CloseLongChec return _c } -func (_c *CloseLongChecker_HandleInterest_Call) RunAndReturn(run func(types.Context, *margintypes.MTP, *margintypes.Pool, ammtypes.Pool) error) *CloseLongChecker_HandleInterest_Call { +func (_c *CloseLongChecker_HandleInterest_Call) RunAndReturn(run func(types.Context, *margintypes.MTP, *margintypes.Pool, ammtypes.Pool, string, string) error) *CloseLongChecker_HandleInterest_Call { _c.Call.Return(run) return _c } -// TakeOutCustody provides a mock function with given fields: ctx, mtp, pool -func (_m *CloseLongChecker) TakeOutCustody(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool) error { - ret := _m.Called(ctx, mtp, pool) +// TakeOutCustody provides a mock function with given fields: ctx, mtp, pool, custodyAsset +func (_m *CloseLongChecker) TakeOutCustody(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool, custodyAsset string) error { + ret := _m.Called(ctx, mtp, pool, custodyAsset) var r0 error - if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, *margintypes.Pool) error); ok { - r0 = rf(ctx, mtp, pool) + if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, *margintypes.Pool, string) error); ok { + r0 = rf(ctx, mtp, pool, custodyAsset) } else { r0 = ret.Error(0) } @@ -310,13 +314,14 @@ type CloseLongChecker_TakeOutCustody_Call struct { // - ctx types.Context // - mtp margintypes.MTP // - pool *margintypes.Pool -func (_e *CloseLongChecker_Expecter) TakeOutCustody(ctx interface{}, mtp interface{}, pool interface{}) *CloseLongChecker_TakeOutCustody_Call { - return &CloseLongChecker_TakeOutCustody_Call{Call: _e.mock.On("TakeOutCustody", ctx, mtp, pool)} +// - custodyAsset string +func (_e *CloseLongChecker_Expecter) TakeOutCustody(ctx interface{}, mtp interface{}, pool interface{}, custodyAsset interface{}) *CloseLongChecker_TakeOutCustody_Call { + return &CloseLongChecker_TakeOutCustody_Call{Call: _e.mock.On("TakeOutCustody", ctx, mtp, pool, custodyAsset)} } -func (_c *CloseLongChecker_TakeOutCustody_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool)) *CloseLongChecker_TakeOutCustody_Call { +func (_c *CloseLongChecker_TakeOutCustody_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool, custodyAsset string)) *CloseLongChecker_TakeOutCustody_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(*margintypes.Pool)) + run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(*margintypes.Pool), args[3].(string)) }) return _c } @@ -326,7 +331,7 @@ func (_c *CloseLongChecker_TakeOutCustody_Call) Return(_a0 error) *CloseLongChec return _c } -func (_c *CloseLongChecker_TakeOutCustody_Call) RunAndReturn(run func(types.Context, margintypes.MTP, *margintypes.Pool) error) *CloseLongChecker_TakeOutCustody_Call { +func (_c *CloseLongChecker_TakeOutCustody_Call) RunAndReturn(run func(types.Context, margintypes.MTP, *margintypes.Pool, string) error) *CloseLongChecker_TakeOutCustody_Call { _c.Call.Return(run) return _c } diff --git a/x/margin/types/mocks/close_short_checker.go b/x/margin/types/mocks/close_short_checker.go index 5827fa543..41490848e 100644 --- a/x/margin/types/mocks/close_short_checker.go +++ b/x/margin/types/mocks/close_short_checker.go @@ -26,23 +26,23 @@ func (_m *CloseShortChecker) EXPECT() *CloseShortChecker_Expecter { return &CloseShortChecker_Expecter{mock: &_m.Mock} } -// EstimateAndRepay provides a mock function with given fields: ctx, mtp, pool, ammPool -func (_m *CloseShortChecker) EstimateAndRepay(ctx types.Context, mtp margintypes.MTP, pool margintypes.Pool, ammPool ammtypes.Pool) (math.Int, error) { - ret := _m.Called(ctx, mtp, pool, ammPool) +// EstimateAndRepay provides a mock function with given fields: ctx, mtp, pool, ammPool, collateralAsset, custodyAsset +func (_m *CloseShortChecker) EstimateAndRepay(ctx types.Context, mtp margintypes.MTP, pool margintypes.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) (math.Int, error) { + ret := _m.Called(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) var r0 math.Int var r1 error - if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool) (math.Int, error)); ok { - return rf(ctx, mtp, pool, ammPool) + if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool, string, string) (math.Int, error)); ok { + return rf(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) } - if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool) math.Int); ok { - r0 = rf(ctx, mtp, pool, ammPool) + if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool, string, string) math.Int); ok { + r0 = rf(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) } else { r0 = ret.Get(0).(math.Int) } - if rf, ok := ret.Get(1).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool) error); ok { - r1 = rf(ctx, mtp, pool, ammPool) + if rf, ok := ret.Get(1).(func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool, string, string) error); ok { + r1 = rf(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) } else { r1 = ret.Error(1) } @@ -60,13 +60,15 @@ type CloseShortChecker_EstimateAndRepay_Call struct { // - mtp margintypes.MTP // - pool margintypes.Pool // - ammPool ammtypes.Pool -func (_e *CloseShortChecker_Expecter) EstimateAndRepay(ctx interface{}, mtp interface{}, pool interface{}, ammPool interface{}) *CloseShortChecker_EstimateAndRepay_Call { - return &CloseShortChecker_EstimateAndRepay_Call{Call: _e.mock.On("EstimateAndRepay", ctx, mtp, pool, ammPool)} +// - collateralAsset string +// - custodyAsset string +func (_e *CloseShortChecker_Expecter) EstimateAndRepay(ctx interface{}, mtp interface{}, pool interface{}, ammPool interface{}, collateralAsset interface{}, custodyAsset interface{}) *CloseShortChecker_EstimateAndRepay_Call { + return &CloseShortChecker_EstimateAndRepay_Call{Call: _e.mock.On("EstimateAndRepay", ctx, mtp, pool, ammPool, collateralAsset, custodyAsset)} } -func (_c *CloseShortChecker_EstimateAndRepay_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, pool margintypes.Pool, ammPool ammtypes.Pool)) *CloseShortChecker_EstimateAndRepay_Call { +func (_c *CloseShortChecker_EstimateAndRepay_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, pool margintypes.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string)) *CloseShortChecker_EstimateAndRepay_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(margintypes.Pool), args[3].(ammtypes.Pool)) + run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(margintypes.Pool), args[3].(ammtypes.Pool), args[4].(string), args[5].(string)) }) return _c } @@ -76,7 +78,7 @@ func (_c *CloseShortChecker_EstimateAndRepay_Call) Return(_a0 math.Int, _a1 erro return _c } -func (_c *CloseShortChecker_EstimateAndRepay_Call) RunAndReturn(run func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool) (math.Int, error)) *CloseShortChecker_EstimateAndRepay_Call { +func (_c *CloseShortChecker_EstimateAndRepay_Call) RunAndReturn(run func(types.Context, margintypes.MTP, margintypes.Pool, ammtypes.Pool, string, string) (math.Int, error)) *CloseShortChecker_EstimateAndRepay_Call { _c.Call.Return(run) return _c } @@ -242,13 +244,13 @@ func (_c *CloseShortChecker_GetPool_Call) RunAndReturn(run func(types.Context, u return _c } -// HandleInterest provides a mock function with given fields: ctx, mtp, pool, ammPool -func (_m *CloseShortChecker) HandleInterest(ctx types.Context, mtp *margintypes.MTP, pool *margintypes.Pool, ammPool ammtypes.Pool) error { - ret := _m.Called(ctx, mtp, pool, ammPool) +// HandleInterest provides a mock function with given fields: ctx, mtp, pool, ammPool, collateralAsset, custodyAsset +func (_m *CloseShortChecker) HandleInterest(ctx types.Context, mtp *margintypes.MTP, pool *margintypes.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) error { + ret := _m.Called(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) var r0 error - if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MTP, *margintypes.Pool, ammtypes.Pool) error); ok { - r0 = rf(ctx, mtp, pool, ammPool) + if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MTP, *margintypes.Pool, ammtypes.Pool, string, string) error); ok { + r0 = rf(ctx, mtp, pool, ammPool, collateralAsset, custodyAsset) } else { r0 = ret.Error(0) } @@ -266,13 +268,15 @@ type CloseShortChecker_HandleInterest_Call struct { // - mtp *margintypes.MTP // - pool *margintypes.Pool // - ammPool ammtypes.Pool -func (_e *CloseShortChecker_Expecter) HandleInterest(ctx interface{}, mtp interface{}, pool interface{}, ammPool interface{}) *CloseShortChecker_HandleInterest_Call { - return &CloseShortChecker_HandleInterest_Call{Call: _e.mock.On("HandleInterest", ctx, mtp, pool, ammPool)} +// - collateralAsset string +// - custodyAsset string +func (_e *CloseShortChecker_Expecter) HandleInterest(ctx interface{}, mtp interface{}, pool interface{}, ammPool interface{}, collateralAsset interface{}, custodyAsset interface{}) *CloseShortChecker_HandleInterest_Call { + return &CloseShortChecker_HandleInterest_Call{Call: _e.mock.On("HandleInterest", ctx, mtp, pool, ammPool, collateralAsset, custodyAsset)} } -func (_c *CloseShortChecker_HandleInterest_Call) Run(run func(ctx types.Context, mtp *margintypes.MTP, pool *margintypes.Pool, ammPool ammtypes.Pool)) *CloseShortChecker_HandleInterest_Call { +func (_c *CloseShortChecker_HandleInterest_Call) Run(run func(ctx types.Context, mtp *margintypes.MTP, pool *margintypes.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string)) *CloseShortChecker_HandleInterest_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.Context), args[1].(*margintypes.MTP), args[2].(*margintypes.Pool), args[3].(ammtypes.Pool)) + run(args[0].(types.Context), args[1].(*margintypes.MTP), args[2].(*margintypes.Pool), args[3].(ammtypes.Pool), args[4].(string), args[5].(string)) }) return _c } @@ -282,18 +286,18 @@ func (_c *CloseShortChecker_HandleInterest_Call) Return(_a0 error) *CloseShortCh return _c } -func (_c *CloseShortChecker_HandleInterest_Call) RunAndReturn(run func(types.Context, *margintypes.MTP, *margintypes.Pool, ammtypes.Pool) error) *CloseShortChecker_HandleInterest_Call { +func (_c *CloseShortChecker_HandleInterest_Call) RunAndReturn(run func(types.Context, *margintypes.MTP, *margintypes.Pool, ammtypes.Pool, string, string) error) *CloseShortChecker_HandleInterest_Call { _c.Call.Return(run) return _c } -// TakeOutCustody provides a mock function with given fields: ctx, mtp, pool -func (_m *CloseShortChecker) TakeOutCustody(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool) error { - ret := _m.Called(ctx, mtp, pool) +// TakeOutCustody provides a mock function with given fields: ctx, mtp, pool, custodyAsset +func (_m *CloseShortChecker) TakeOutCustody(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool, custodyAsset string) error { + ret := _m.Called(ctx, mtp, pool, custodyAsset) var r0 error - if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, *margintypes.Pool) error); ok { - r0 = rf(ctx, mtp, pool) + if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, *margintypes.Pool, string) error); ok { + r0 = rf(ctx, mtp, pool, custodyAsset) } else { r0 = ret.Error(0) } @@ -310,13 +314,14 @@ type CloseShortChecker_TakeOutCustody_Call struct { // - ctx types.Context // - mtp margintypes.MTP // - pool *margintypes.Pool -func (_e *CloseShortChecker_Expecter) TakeOutCustody(ctx interface{}, mtp interface{}, pool interface{}) *CloseShortChecker_TakeOutCustody_Call { - return &CloseShortChecker_TakeOutCustody_Call{Call: _e.mock.On("TakeOutCustody", ctx, mtp, pool)} +// - custodyAsset string +func (_e *CloseShortChecker_Expecter) TakeOutCustody(ctx interface{}, mtp interface{}, pool interface{}, custodyAsset interface{}) *CloseShortChecker_TakeOutCustody_Call { + return &CloseShortChecker_TakeOutCustody_Call{Call: _e.mock.On("TakeOutCustody", ctx, mtp, pool, custodyAsset)} } -func (_c *CloseShortChecker_TakeOutCustody_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool)) *CloseShortChecker_TakeOutCustody_Call { +func (_c *CloseShortChecker_TakeOutCustody_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool, custodyAsset string)) *CloseShortChecker_TakeOutCustody_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(*margintypes.Pool)) + run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(*margintypes.Pool), args[3].(string)) }) return _c } @@ -326,7 +331,7 @@ func (_c *CloseShortChecker_TakeOutCustody_Call) Return(_a0 error) *CloseShortCh return _c } -func (_c *CloseShortChecker_TakeOutCustody_Call) RunAndReturn(run func(types.Context, margintypes.MTP, *margintypes.Pool) error) *CloseShortChecker_TakeOutCustody_Call { +func (_c *CloseShortChecker_TakeOutCustody_Call) RunAndReturn(run func(types.Context, margintypes.MTP, *margintypes.Pool, string) error) *CloseShortChecker_TakeOutCustody_Call { _c.Call.Return(run) return _c } diff --git a/x/margin/types/mocks/open_long_checker.go b/x/margin/types/mocks/open_long_checker.go index 7615521b0..4027e7fb1 100644 --- a/x/margin/types/mocks/open_long_checker.go +++ b/x/margin/types/mocks/open_long_checker.go @@ -26,13 +26,13 @@ func (_m *OpenLongChecker) EXPECT() *OpenLongChecker_Expecter { return &OpenLongChecker_Expecter{mock: &_m.Mock} } -// Borrow provides a mock function with given fields: ctx, collateralAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta -func (_m *OpenLongChecker) Borrow(ctx types.Context, collateralAsset string, collateralAmount math.Int, custodyAmount math.Int, mtp *margintypes.MTP, ammPool *ammtypes.Pool, pool *margintypes.Pool, eta math.LegacyDec) error { - ret := _m.Called(ctx, collateralAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta) +// Borrow provides a mock function with given fields: ctx, collateralAsset, custodyAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta +func (_m *OpenLongChecker) Borrow(ctx types.Context, collateralAsset string, custodyAsset string, collateralAmount math.Int, custodyAmount math.Int, mtp *margintypes.MTP, ammPool *ammtypes.Pool, pool *margintypes.Pool, eta math.LegacyDec) error { + ret := _m.Called(ctx, collateralAsset, custodyAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta) var r0 error - if rf, ok := ret.Get(0).(func(types.Context, string, math.Int, math.Int, *margintypes.MTP, *ammtypes.Pool, *margintypes.Pool, math.LegacyDec) error); ok { - r0 = rf(ctx, collateralAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta) + if rf, ok := ret.Get(0).(func(types.Context, string, string, math.Int, math.Int, *margintypes.MTP, *ammtypes.Pool, *margintypes.Pool, math.LegacyDec) error); ok { + r0 = rf(ctx, collateralAsset, custodyAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta) } else { r0 = ret.Error(0) } @@ -48,19 +48,20 @@ type OpenLongChecker_Borrow_Call struct { // Borrow is a helper method to define mock.On call // - ctx types.Context // - collateralAsset string +// - custodyAsset string // - collateralAmount math.Int // - custodyAmount math.Int // - mtp *margintypes.MTP // - ammPool *ammtypes.Pool // - pool *margintypes.Pool // - eta math.LegacyDec -func (_e *OpenLongChecker_Expecter) Borrow(ctx interface{}, collateralAsset interface{}, collateralAmount interface{}, custodyAmount interface{}, mtp interface{}, ammPool interface{}, pool interface{}, eta interface{}) *OpenLongChecker_Borrow_Call { - return &OpenLongChecker_Borrow_Call{Call: _e.mock.On("Borrow", ctx, collateralAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta)} +func (_e *OpenLongChecker_Expecter) Borrow(ctx interface{}, collateralAsset interface{}, custodyAsset interface{}, collateralAmount interface{}, custodyAmount interface{}, mtp interface{}, ammPool interface{}, pool interface{}, eta interface{}) *OpenLongChecker_Borrow_Call { + return &OpenLongChecker_Borrow_Call{Call: _e.mock.On("Borrow", ctx, collateralAsset, custodyAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta)} } -func (_c *OpenLongChecker_Borrow_Call) Run(run func(ctx types.Context, collateralAsset string, collateralAmount math.Int, custodyAmount math.Int, mtp *margintypes.MTP, ammPool *ammtypes.Pool, pool *margintypes.Pool, eta math.LegacyDec)) *OpenLongChecker_Borrow_Call { +func (_c *OpenLongChecker_Borrow_Call) Run(run func(ctx types.Context, collateralAsset string, custodyAsset string, collateralAmount math.Int, custodyAmount math.Int, mtp *margintypes.MTP, ammPool *ammtypes.Pool, pool *margintypes.Pool, eta math.LegacyDec)) *OpenLongChecker_Borrow_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(types.Context), args[1].(string), args[2].(math.Int), args[3].(math.Int), args[4].(*margintypes.MTP), args[5].(*ammtypes.Pool), args[6].(*margintypes.Pool), args[7].(math.LegacyDec)) + run(args[0].(types.Context), args[1].(string), args[2].(string), args[3].(math.Int), args[4].(math.Int), args[5].(*margintypes.MTP), args[6].(*ammtypes.Pool), args[7].(*margintypes.Pool), args[8].(math.LegacyDec)) }) return _c } @@ -70,7 +71,7 @@ func (_c *OpenLongChecker_Borrow_Call) Return(_a0 error) *OpenLongChecker_Borrow return _c } -func (_c *OpenLongChecker_Borrow_Call) RunAndReturn(run func(types.Context, string, math.Int, math.Int, *margintypes.MTP, *ammtypes.Pool, *margintypes.Pool, math.LegacyDec) error) *OpenLongChecker_Borrow_Call { +func (_c *OpenLongChecker_Borrow_Call) RunAndReturn(run func(types.Context, string, string, math.Int, math.Int, *margintypes.MTP, *ammtypes.Pool, *margintypes.Pool, math.LegacyDec) error) *OpenLongChecker_Borrow_Call { _c.Call.Return(run) return _c } @@ -166,8 +167,8 @@ func (_c *OpenLongChecker_CheckMinLiabilities_Call) RunAndReturn(run func(types. return _c } -// CheckSameAssetPosition provides a mock function with given fields: ctx, msg -func (_m *OpenLongChecker) CheckSameAssetPosition(ctx types.Context, msg *margintypes.MsgOpen) *margintypes.MTP { +// CheckSamePosition provides a mock function with given fields: ctx, msg +func (_m *OpenLongChecker) CheckSamePosition(ctx types.Context, msg *margintypes.MsgOpen) *margintypes.MTP { ret := _m.Called(ctx, msg) var r0 *margintypes.MTP @@ -182,31 +183,31 @@ func (_m *OpenLongChecker) CheckSameAssetPosition(ctx types.Context, msg *margin return r0 } -// OpenLongChecker_CheckSameAssetPosition_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CheckSameAssetPosition' -type OpenLongChecker_CheckSameAssetPosition_Call struct { +// OpenLongChecker_CheckSamePosition_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CheckSamePosition' +type OpenLongChecker_CheckSamePosition_Call struct { *mock.Call } -// CheckSameAssetPosition is a helper method to define mock.On call +// CheckSamePosition is a helper method to define mock.On call // - ctx types.Context // - msg *margintypes.MsgOpen -func (_e *OpenLongChecker_Expecter) CheckSameAssetPosition(ctx interface{}, msg interface{}) *OpenLongChecker_CheckSameAssetPosition_Call { - return &OpenLongChecker_CheckSameAssetPosition_Call{Call: _e.mock.On("CheckSameAssetPosition", ctx, msg)} +func (_e *OpenLongChecker_Expecter) CheckSamePosition(ctx interface{}, msg interface{}) *OpenLongChecker_CheckSamePosition_Call { + return &OpenLongChecker_CheckSamePosition_Call{Call: _e.mock.On("CheckSamePosition", ctx, msg)} } -func (_c *OpenLongChecker_CheckSameAssetPosition_Call) Run(run func(ctx types.Context, msg *margintypes.MsgOpen)) *OpenLongChecker_CheckSameAssetPosition_Call { +func (_c *OpenLongChecker_CheckSamePosition_Call) Run(run func(ctx types.Context, msg *margintypes.MsgOpen)) *OpenLongChecker_CheckSamePosition_Call { _c.Call.Run(func(args mock.Arguments) { run(args[0].(types.Context), args[1].(*margintypes.MsgOpen)) }) return _c } -func (_c *OpenLongChecker_CheckSameAssetPosition_Call) Return(_a0 *margintypes.MTP) *OpenLongChecker_CheckSameAssetPosition_Call { +func (_c *OpenLongChecker_CheckSamePosition_Call) Return(_a0 *margintypes.MTP) *OpenLongChecker_CheckSamePosition_Call { _c.Call.Return(_a0) return _c } -func (_c *OpenLongChecker_CheckSameAssetPosition_Call) RunAndReturn(run func(types.Context, *margintypes.MsgOpen) *margintypes.MTP) *OpenLongChecker_CheckSameAssetPosition_Call { +func (_c *OpenLongChecker_CheckSamePosition_Call) RunAndReturn(run func(types.Context, *margintypes.MsgOpen) *margintypes.MTP) *OpenLongChecker_CheckSamePosition_Call { _c.Call.Return(run) return _c } diff --git a/x/margin/types/tx.pb.go b/x/margin/types/tx.pb.go index f72af3428..78d718482 100644 --- a/x/margin/types/tx.pb.go +++ b/x/margin/types/tx.pb.go @@ -136,8 +136,10 @@ func (m *MsgOpenResponse) XXX_DiscardUnknown() { var xxx_messageInfo_MsgOpenResponse proto.InternalMessageInfo type MsgClose struct { - Creator string `protobuf:"bytes,1,opt,name=creator,proto3" json:"creator,omitempty"` - Id uint64 `protobuf:"varint,2,opt,name=id,proto3" json:"id,omitempty"` + Creator string `protobuf:"bytes,1,opt,name=creator,proto3" json:"creator,omitempty"` + Id uint64 `protobuf:"varint,2,opt,name=id,proto3" json:"id,omitempty"` + CollateralAsset string `protobuf:"bytes,3,opt,name=collateralAsset,proto3" json:"collateralAsset,omitempty"` + CustodyAsset string `protobuf:"bytes,4,opt,name=custodyAsset,proto3" json:"custodyAsset,omitempty"` } func (m *MsgClose) Reset() { *m = MsgClose{} } @@ -187,6 +189,20 @@ func (m *MsgClose) GetId() uint64 { return 0 } +func (m *MsgClose) GetCollateralAsset() string { + if m != nil { + return m.CollateralAsset + } + return "" +} + +func (m *MsgClose) GetCustodyAsset() string { + if m != nil { + return m.CustodyAsset + } + return "" +} + type MsgCloseResponse struct { } @@ -603,46 +619,47 @@ func init() { func init() { proto.RegisterFile("elys/margin/tx.proto", fileDescriptor_01b9dbed35cc5a15) } var fileDescriptor_01b9dbed35cc5a15 = []byte{ - // 611 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x54, 0x4f, 0x6b, 0xdb, 0x4e, - 0x10, 0xb5, 0xec, 0xfc, 0xf3, 0x38, 0x24, 0xf9, 0xed, 0xcf, 0x49, 0x14, 0x35, 0x55, 0x5c, 0xb5, - 0x14, 0x43, 0x88, 0x4c, 0xdd, 0x9e, 0x0a, 0x3d, 0x24, 0x4d, 0x0f, 0x29, 0x98, 0x04, 0x41, 0x29, - 0x84, 0x52, 0x90, 0xa5, 0xad, 0x2c, 0x22, 0x6b, 0xc5, 0xee, 0xba, 0x8e, 0xbf, 0x45, 0xbf, 0x52, - 0x6f, 0xa1, 0xa7, 0x1c, 0x4b, 0x0f, 0xa1, 0xd8, 0x5f, 0xa4, 0x68, 0x25, 0xcb, 0x2b, 0xdb, 0x71, - 0xe8, 0xa1, 0x27, 0x7b, 0xdf, 0x7b, 0xf3, 0x66, 0x76, 0x66, 0xb4, 0x50, 0xc5, 0xc1, 0x80, 0x35, - 0xba, 0x36, 0xf5, 0xfc, 0xb0, 0xc1, 0xaf, 0xcd, 0x88, 0x12, 0x4e, 0x50, 0x25, 0x46, 0xcd, 0x04, - 0xd5, 0xaa, 0x1e, 0xf1, 0x88, 0xc0, 0x1b, 0xf1, 0xbf, 0x44, 0xa2, 0xa9, 0x72, 0x60, 0x64, 0x53, - 0xbb, 0xcb, 0x52, 0x66, 0x37, 0x67, 0x39, 0x88, 0x70, 0x4a, 0x18, 0x3f, 0x8a, 0xb0, 0xda, 0x62, - 0xde, 0x79, 0x84, 0x43, 0xa4, 0xc2, 0xaa, 0x43, 0xb1, 0xcd, 0x09, 0x55, 0x95, 0x9a, 0x52, 0x2f, - 0x5b, 0xe3, 0x23, 0xaa, 0xc3, 0xa6, 0x43, 0x82, 0xc0, 0xe6, 0x98, 0xda, 0xc1, 0x31, 0x63, 0x98, - 0xab, 0x45, 0xa1, 0x98, 0x86, 0xd1, 0x25, 0x6c, 0x49, 0x50, 0x97, 0xf4, 0x42, 0xae, 0x96, 0x62, - 0xe9, 0x89, 0x79, 0x73, 0x77, 0x50, 0xf8, 0x75, 0x77, 0xf0, 0xdc, 0xf3, 0x79, 0xa7, 0xd7, 0x36, - 0x1d, 0xd2, 0x6d, 0x38, 0x84, 0x75, 0x09, 0x4b, 0x7f, 0x8e, 0x98, 0x7b, 0x95, 0xd6, 0x76, 0x16, - 0x72, 0x6b, 0xc6, 0x07, 0xd5, 0xa0, 0xd2, 0x26, 0x94, 0x92, 0x7e, 0x52, 0xc1, 0x92, 0xa8, 0x40, - 0x86, 0xd0, 0x0b, 0x58, 0x8b, 0x08, 0xf3, 0xb9, 0x4f, 0x42, 0x75, 0xb9, 0xa6, 0xd4, 0x37, 0x9a, - 0xdb, 0xa6, 0xd4, 0x36, 0xf3, 0x22, 0x25, 0xad, 0x4c, 0x86, 0xde, 0xc3, 0x5a, 0x80, 0xbf, 0x62, - 0x6a, 0x7b, 0x58, 0x5d, 0xf9, 0xeb, 0x42, 0x4f, 0xb1, 0x63, 0x65, 0xf1, 0xc6, 0x7f, 0xb0, 0x99, - 0xf6, 0xd2, 0xc2, 0x2c, 0x22, 0x21, 0xc3, 0xc6, 0x2b, 0x58, 0x6b, 0x31, 0xef, 0x6d, 0x40, 0x18, - 0x5e, 0xd0, 0xdf, 0x0d, 0x28, 0xfa, 0xae, 0x68, 0xe9, 0x92, 0x55, 0xf4, 0x5d, 0x03, 0xc1, 0xd6, - 0x38, 0x2a, 0x73, 0xfa, 0x24, 0xcc, 0x3f, 0x44, 0xae, 0xcd, 0xf1, 0x85, 0x98, 0x2d, 0xda, 0x87, - 0xb2, 0xdd, 0xe3, 0x1d, 0x42, 0x7d, 0x3e, 0x48, 0x2d, 0x27, 0x00, 0x3a, 0x84, 0x95, 0x64, 0x07, - 0x84, 0x71, 0xa5, 0xf9, 0x7f, 0xbe, 0x15, 0x82, 0xb2, 0x52, 0x89, 0xb1, 0x07, 0xbb, 0x53, 0xee, - 0x59, 0xe2, 0x2f, 0xb0, 0x31, 0xa1, 0x08, 0x09, 0x1e, 0xca, 0x5b, 0x85, 0xe5, 0x28, 0x96, 0xa9, - 0xc5, 0x5a, 0xa9, 0x5e, 0xb6, 0x92, 0x43, 0x3c, 0x3c, 0x27, 0xbe, 0x8f, 0x2b, 0x2c, 0xd4, 0x92, - 0xe0, 0x64, 0xc8, 0x50, 0x61, 0x27, 0x9f, 0x47, 0xba, 0xfa, 0x7a, 0x8b, 0x79, 0x1f, 0x3b, 0x3e, - 0xc7, 0x81, 0xcf, 0xf8, 0x03, 0xf9, 0x4d, 0x40, 0xfd, 0xb1, 0x14, 0xbb, 0xc7, 0xae, 0x4b, 0x31, - 0x63, 0xe9, 0xbe, 0xce, 0x61, 0x8c, 0x1d, 0xa8, 0xca, 0xee, 0x59, 0xd6, 0xcf, 0xe2, 0xde, 0xa7, - 0xb8, 0xff, 0x8f, 0xf2, 0x26, 0xf7, 0x95, 0xfc, 0xc7, 0x99, 0x9b, 0xdf, 0x4b, 0x50, 0x6a, 0x31, - 0x0f, 0xbd, 0x86, 0x25, 0xf1, 0x61, 0x56, 0x73, 0x93, 0x4b, 0x57, 0x4c, 0xdb, 0x9f, 0x87, 0x8e, - 0x3d, 0xd0, 0x1b, 0x58, 0x4e, 0xb6, 0x6e, 0x7b, 0x5a, 0x26, 0x60, 0xed, 0xf1, 0x5c, 0x38, 0x0b, - 0xb7, 0x60, 0x3d, 0xbf, 0x6a, 0xd3, 0x72, 0x99, 0xd5, 0x9e, 0x2d, 0x62, 0x33, 0xcf, 0x73, 0xa8, - 0xc8, 0x5b, 0xf4, 0xe8, 0x9e, 0xa0, 0x98, 0xd4, 0x9e, 0x2e, 0x20, 0x33, 0xc3, 0x33, 0x28, 0x4f, - 0x96, 0x62, 0x6f, 0x3a, 0x22, 0xa3, 0xb4, 0x27, 0xf7, 0x52, 0x72, 0x6d, 0xf2, 0xa4, 0x67, 0x6a, - 0x93, 0xc8, 0xd9, 0xda, 0xe6, 0xcc, 0xf0, 0xe4, 0xdd, 0xcd, 0x50, 0x57, 0x6e, 0x87, 0xba, 0xf2, - 0x7b, 0xa8, 0x2b, 0xdf, 0x46, 0x7a, 0xe1, 0x76, 0xa4, 0x17, 0x7e, 0x8e, 0xf4, 0xc2, 0xe5, 0xa1, - 0xf4, 0xae, 0xc4, 0x46, 0x47, 0x21, 0xe6, 0x7d, 0x42, 0xaf, 0xc4, 0xa1, 0x71, 0x9d, 0x7b, 0xa5, - 0xdb, 0x2b, 0xe2, 0x99, 0x7e, 0xf9, 0x27, 0x00, 0x00, 0xff, 0xff, 0x7f, 0xf5, 0x77, 0xa2, 0x14, - 0x06, 0x00, 0x00, + // 631 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x54, 0xcd, 0x4e, 0xdb, 0x4c, + 0x14, 0x8d, 0x63, 0xfe, 0x72, 0x83, 0x80, 0x6f, 0xbe, 0x00, 0xc6, 0xa5, 0x26, 0x75, 0xab, 0x2a, + 0x12, 0xc2, 0x51, 0xe9, 0xae, 0x52, 0x17, 0x50, 0xba, 0xa0, 0x52, 0x04, 0xb2, 0x54, 0x55, 0x42, + 0x55, 0x25, 0x63, 0x4f, 0x8d, 0x85, 0xe3, 0xb1, 0x66, 0x26, 0x0d, 0xd9, 0xf6, 0x09, 0xfa, 0x4a, + 0xdd, 0xa1, 0xae, 0x58, 0x56, 0x5d, 0xa0, 0x8a, 0xbc, 0x48, 0xe5, 0xb1, 0xe3, 0x8c, 0x13, 0x13, + 0xd4, 0x45, 0x57, 0xc9, 0x9c, 0x73, 0xe7, 0xdc, 0x33, 0x77, 0x8e, 0x07, 0x1a, 0x38, 0x1c, 0xb0, + 0x76, 0xd7, 0xa1, 0x7e, 0x10, 0xb5, 0xf9, 0x95, 0x15, 0x53, 0xc2, 0x09, 0xaa, 0x27, 0xa8, 0x95, + 0xa2, 0x7a, 0xc3, 0x27, 0x3e, 0x11, 0x78, 0x3b, 0xf9, 0x97, 0x96, 0xe8, 0x9a, 0xbc, 0x31, 0x76, + 0xa8, 0xd3, 0x65, 0x19, 0xb3, 0x59, 0x90, 0x1c, 0xc4, 0x38, 0x23, 0xcc, 0x1f, 0x55, 0x58, 0xec, + 0x30, 0xff, 0x24, 0xc6, 0x11, 0xd2, 0x60, 0xd1, 0xa5, 0xd8, 0xe1, 0x84, 0x6a, 0x4a, 0x53, 0x69, + 0xd5, 0xec, 0xd1, 0x12, 0xb5, 0x60, 0xd5, 0x25, 0x61, 0xe8, 0x70, 0x4c, 0x9d, 0xf0, 0x80, 0x31, + 0xcc, 0xb5, 0xaa, 0xa8, 0x98, 0x84, 0xd1, 0x19, 0xac, 0x49, 0x50, 0x97, 0xf4, 0x22, 0xae, 0xa9, + 0x49, 0xe9, 0xa1, 0x75, 0x7d, 0xbb, 0x53, 0xf9, 0x75, 0xbb, 0xf3, 0xdc, 0x0f, 0xf8, 0x45, 0xef, + 0xdc, 0x72, 0x49, 0xb7, 0xed, 0x12, 0xd6, 0x25, 0x2c, 0xfb, 0xd9, 0x63, 0xde, 0x65, 0xe6, 0xed, + 0x38, 0xe2, 0xf6, 0x94, 0x0e, 0x6a, 0x42, 0xfd, 0x9c, 0x50, 0x4a, 0xfa, 0xa9, 0x83, 0x39, 0xe1, + 0x40, 0x86, 0xd0, 0x0b, 0x58, 0x8a, 0x09, 0x0b, 0x78, 0x40, 0x22, 0x6d, 0xbe, 0xa9, 0xb4, 0x56, + 0xf6, 0xd7, 0x2d, 0x69, 0x6c, 0xd6, 0x69, 0x46, 0xda, 0x79, 0x19, 0x7a, 0x07, 0x4b, 0x21, 0xfe, + 0x82, 0xa9, 0xe3, 0x63, 0x6d, 0xe1, 0xaf, 0x8d, 0x1e, 0x61, 0xd7, 0xce, 0xf7, 0x9b, 0xff, 0xc1, + 0x6a, 0x36, 0x4b, 0x1b, 0xb3, 0x98, 0x44, 0x0c, 0x9b, 0x5f, 0x15, 0x58, 0xea, 0x30, 0xff, 0x4d, + 0x48, 0x18, 0x9e, 0x31, 0xe0, 0x15, 0xa8, 0x06, 0x9e, 0x98, 0xe9, 0x9c, 0x5d, 0x0d, 0xbc, 0xb2, + 0x81, 0xab, 0xe5, 0x03, 0x37, 0x61, 0xd9, 0xed, 0x31, 0x4e, 0xbc, 0x81, 0x3c, 0x95, 0x02, 0x66, + 0x22, 0x58, 0x1b, 0x79, 0xc8, 0x8d, 0x7d, 0x14, 0x5e, 0xdf, 0xc7, 0x9e, 0xc3, 0xf1, 0xa9, 0x88, + 0x0a, 0xda, 0x86, 0x9a, 0xd3, 0xe3, 0x17, 0x84, 0x06, 0x7c, 0x90, 0x19, 0x1c, 0x03, 0x68, 0x17, + 0x16, 0xd2, 0x48, 0x09, 0x9b, 0xf5, 0xfd, 0xff, 0x8b, 0x93, 0x15, 0x94, 0x9d, 0x95, 0x98, 0x5b, + 0xb0, 0x39, 0xa1, 0x9e, 0x37, 0xfe, 0x0c, 0x2b, 0x63, 0x8a, 0x90, 0xf0, 0xa1, 0xbe, 0x0d, 0x98, + 0x8f, 0x93, 0x32, 0xad, 0xda, 0x54, 0x5b, 0x35, 0x3b, 0x5d, 0x24, 0x59, 0x70, 0x93, 0xf3, 0x78, + 0x42, 0x42, 0x53, 0x05, 0x27, 0x43, 0xa6, 0x06, 0x1b, 0xc5, 0x3e, 0xd2, 0xd1, 0x97, 0x3b, 0xcc, + 0xff, 0x70, 0x11, 0x70, 0x1c, 0x06, 0x8c, 0x3f, 0xd0, 0xdf, 0x02, 0xd4, 0x1f, 0x95, 0x62, 0xef, + 0xc0, 0xf3, 0x28, 0x66, 0x2c, 0x8b, 0x7f, 0x09, 0x63, 0x6e, 0x40, 0x43, 0x56, 0xcf, 0xbb, 0x7e, + 0x12, 0xe7, 0x3e, 0xc2, 0xfd, 0x7f, 0xd4, 0x37, 0x3d, 0xaf, 0xa4, 0x3f, 0xea, 0xbc, 0xff, 0x5d, + 0x05, 0xb5, 0xc3, 0x7c, 0xf4, 0x0a, 0xe6, 0xc4, 0x77, 0xde, 0x28, 0xdc, 0x5c, 0x96, 0x58, 0x7d, + 0xbb, 0x0c, 0x1d, 0x69, 0xa0, 0xd7, 0x30, 0x9f, 0x66, 0x78, 0x7d, 0xb2, 0x4c, 0xc0, 0xfa, 0xe3, + 0x52, 0x38, 0xdf, 0x6e, 0xc3, 0x72, 0x31, 0x6a, 0x93, 0xe5, 0x32, 0xab, 0x3f, 0x9b, 0xc5, 0xe6, + 0x9a, 0x27, 0x50, 0x97, 0x53, 0xf4, 0xe8, 0x9e, 0x4d, 0x09, 0xa9, 0x3f, 0x9d, 0x41, 0xe6, 0x82, + 0xc7, 0x50, 0x1b, 0x87, 0x62, 0x6b, 0x72, 0x47, 0x4e, 0xe9, 0x4f, 0xee, 0xa5, 0x64, 0x6f, 0xf2, + 0x4d, 0x4f, 0x79, 0x93, 0xc8, 0x69, 0x6f, 0x25, 0x77, 0x78, 0xf8, 0xf6, 0xfa, 0xce, 0x50, 0x6e, + 0xee, 0x0c, 0xe5, 0xf7, 0x9d, 0xa1, 0x7c, 0x1b, 0x1a, 0x95, 0x9b, 0xa1, 0x51, 0xf9, 0x39, 0x34, + 0x2a, 0x67, 0xbb, 0xd2, 0x33, 0x95, 0x08, 0xed, 0x45, 0x98, 0xf7, 0x09, 0xbd, 0x14, 0x8b, 0xf6, + 0x55, 0xe1, 0xd1, 0x3f, 0x5f, 0x10, 0xaf, 0xfe, 0xcb, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x88, + 0x9c, 0xb6, 0xab, 0x63, 0x06, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -1017,6 +1034,20 @@ func (m *MsgClose) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.CustodyAsset) > 0 { + i -= len(m.CustodyAsset) + copy(dAtA[i:], m.CustodyAsset) + i = encodeVarintTx(dAtA, i, uint64(len(m.CustodyAsset))) + i-- + dAtA[i] = 0x22 + } + if len(m.CollateralAsset) > 0 { + i -= len(m.CollateralAsset) + copy(dAtA[i:], m.CollateralAsset) + i = encodeVarintTx(dAtA, i, uint64(len(m.CollateralAsset))) + i-- + dAtA[i] = 0x1a + } if m.Id != 0 { i = encodeVarintTx(dAtA, i, uint64(m.Id)) i-- @@ -1372,6 +1403,14 @@ func (m *MsgClose) Size() (n int) { if m.Id != 0 { n += 1 + sovTx(uint64(m.Id)) } + l = len(m.CollateralAsset) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + l = len(m.CustodyAsset) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } return n } @@ -1865,6 +1904,70 @@ func (m *MsgClose) Unmarshal(dAtA []byte) error { break } } + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field CollateralAsset", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.CollateralAsset = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field CustodyAsset", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.CustodyAsset = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTx(dAtA[iNdEx:]) diff --git a/x/margin/types/types.go b/x/margin/types/types.go index 5df7c2f83..dea8236a2 100644 --- a/x/margin/types/types.go +++ b/x/margin/types/types.go @@ -19,26 +19,33 @@ func GetPositionFromString(s string) Position { func NewMTP(signer string, collateralAsset string, borrowAsset string, position Position, leverage sdk.Dec, poolId uint64) *MTP { return &MTP{ - Address: signer, - CollateralAsset: collateralAsset, - CollateralAmount: sdk.ZeroInt(), - Liabilities: sdk.ZeroInt(), - InterestPaidCollateral: sdk.ZeroInt(), - InterestPaidCustody: sdk.ZeroInt(), - InterestUnpaidCollateral: sdk.ZeroInt(), - CustodyAsset: borrowAsset, - CustodyAmount: sdk.ZeroInt(), - Leverage: leverage, - MtpHealth: sdk.ZeroDec(), - Position: position, - AmmPoolId: poolId, + Address: signer, + CollateralAssets: []string{collateralAsset}, + CollateralAmounts: []sdk.Int{sdk.ZeroInt()}, + Liabilities: sdk.ZeroInt(), + InterestPaidCollaterals: []sdk.Int{sdk.ZeroInt()}, + InterestPaidCustodys: []sdk.Int{sdk.ZeroInt()}, + InterestUnpaidCollaterals: []sdk.Int{sdk.ZeroInt()}, + CustodyAssets: []string{borrowAsset}, + CustodyAmounts: []sdk.Int{sdk.ZeroInt()}, + Leverages: []sdk.Dec{leverage}, + MtpHealth: sdk.ZeroDec(), + Position: position, + AmmPoolId: poolId, + ConsolidateLeverage: leverage, + SumCollateral: sdk.ZeroInt(), } } func (mtp MTP) Validate() error { - if mtp.CollateralAsset == "" { + if len(mtp.CollateralAssets) < 1 { return sdkerrors.Wrap(ErrMTPInvalid, "no asset specified") } + for _, asset := range mtp.CollateralAssets { + if asset == "" { + return sdkerrors.Wrap(ErrMTPInvalid, "no asset specified") + } + } if mtp.Address == "" { return sdkerrors.Wrap(ErrMTPInvalid, "no address specified") } diff --git a/x/margin/types/types.pb.go b/x/margin/types/types.pb.go index fcf9709b1..28fdb3277 100644 --- a/x/margin/types/types.pb.go +++ b/x/margin/types/types.pb.go @@ -53,20 +53,22 @@ func (Position) EnumDescriptor() ([]byte, []int) { } type MTP struct { - Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` - CollateralAsset string `protobuf:"bytes,2,opt,name=collateral_asset,json=collateralAsset,proto3" json:"collateral_asset,omitempty"` - CollateralAmount github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,3,opt,name=collateral_amount,json=collateralAmount,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"collateral_amount"` - Liabilities github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,4,opt,name=liabilities,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"liabilities"` - InterestPaidCollateral github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,5,opt,name=interest_paid_collateral,json=interestPaidCollateral,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"interest_paid_collateral"` - InterestPaidCustody github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,6,opt,name=interest_paid_custody,json=interestPaidCustody,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"interest_paid_custody"` - InterestUnpaidCollateral github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,7,opt,name=interest_unpaid_collateral,json=interestUnpaidCollateral,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"interest_unpaid_collateral"` - CustodyAsset string `protobuf:"bytes,8,opt,name=custody_asset,json=custodyAsset,proto3" json:"custody_asset,omitempty"` - CustodyAmount github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,9,opt,name=custody_amount,json=custodyAmount,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"custody_amount"` - Leverage github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,10,opt,name=leverage,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"leverage"` - MtpHealth github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,11,opt,name=mtp_health,json=mtpHealth,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"mtp_health"` - Position Position `protobuf:"varint,12,opt,name=position,proto3,enum=elys.margin.Position" json:"position,omitempty"` - Id uint64 `protobuf:"varint,13,opt,name=id,proto3" json:"id,omitempty"` - AmmPoolId uint64 `protobuf:"varint,14,opt,name=amm_pool_id,json=ammPoolId,proto3" json:"amm_pool_id,omitempty"` + Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` + CollateralAssets []string `protobuf:"bytes,2,rep,name=collateral_assets,json=collateralAssets,proto3" json:"collateral_assets,omitempty"` + CollateralAmounts []github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,3,rep,name=collateral_amounts,json=collateralAmounts,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"collateral_amounts"` + Liabilities github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,4,opt,name=liabilities,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"liabilities"` + InterestPaidCollaterals []github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,5,rep,name=interest_paid_collaterals,json=interestPaidCollaterals,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"interest_paid_collaterals"` + InterestPaidCustodys []github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,6,rep,name=interest_paid_custodys,json=interestPaidCustodys,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"interest_paid_custodys"` + InterestUnpaidCollaterals []github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,7,rep,name=interest_unpaid_collaterals,json=interestUnpaidCollaterals,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"interest_unpaid_collaterals"` + CustodyAssets []string `protobuf:"bytes,8,rep,name=custody_assets,json=custodyAssets,proto3" json:"custody_assets,omitempty"` + CustodyAmounts []github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,9,rep,name=custody_amounts,json=custodyAmounts,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"custody_amounts"` + Leverages []github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,10,rep,name=leverages,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"leverages"` + MtpHealth github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,11,opt,name=mtp_health,json=mtpHealth,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"mtp_health"` + Position Position `protobuf:"varint,12,opt,name=position,proto3,enum=elys.margin.Position" json:"position,omitempty"` + Id uint64 `protobuf:"varint,13,opt,name=id,proto3" json:"id,omitempty"` + AmmPoolId uint64 `protobuf:"varint,14,opt,name=amm_pool_id,json=ammPoolId,proto3" json:"amm_pool_id,omitempty"` + ConsolidateLeverage github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,15,opt,name=consolidate_leverage,json=consolidateLeverage,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"consolidate_leverage"` + SumCollateral github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,16,opt,name=sum_collateral,json=sumCollateral,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"sum_collateral"` } func (m *MTP) Reset() { *m = MTP{} } @@ -109,18 +111,18 @@ func (m *MTP) GetAddress() string { return "" } -func (m *MTP) GetCollateralAsset() string { +func (m *MTP) GetCollateralAssets() []string { if m != nil { - return m.CollateralAsset + return m.CollateralAssets } - return "" + return nil } -func (m *MTP) GetCustodyAsset() string { +func (m *MTP) GetCustodyAssets() []string { if m != nil { - return m.CustodyAsset + return m.CustodyAssets } - return "" + return nil } func (m *MTP) GetPosition() Position { @@ -197,42 +199,44 @@ func init() { func init() { proto.RegisterFile("elys/margin/types.proto", fileDescriptor_cd1c09c977f732f9) } var fileDescriptor_cd1c09c977f732f9 = []byte{ - // 545 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x94, 0xdf, 0x6e, 0xd3, 0x30, - 0x14, 0xc6, 0x9b, 0xb6, 0x5b, 0x9b, 0xd3, 0xb5, 0x2b, 0x86, 0x81, 0xb5, 0x8b, 0xac, 0x1a, 0x02, - 0x15, 0xd0, 0x52, 0x18, 0x4f, 0xb0, 0x3f, 0x85, 0x15, 0xed, 0x4f, 0x94, 0xad, 0x42, 0x82, 0x8b, - 0xc8, 0x6d, 0xac, 0xd6, 0x9a, 0x13, 0x47, 0xb1, 0x3b, 0xe8, 0x5b, 0xf0, 0x26, 0xbc, 0xc6, 0x2e, - 0x77, 0x89, 0xb8, 0x98, 0x50, 0xfb, 0x22, 0x28, 0x69, 0x9a, 0x65, 0xbb, 0x5b, 0xae, 0x12, 0x9f, - 0xf3, 0xe5, 0xf7, 0xf9, 0x58, 0x5f, 0x0c, 0x2f, 0x28, 0x9f, 0xca, 0x8e, 0x47, 0xc2, 0x11, 0xf3, - 0x3b, 0x6a, 0x1a, 0x50, 0x69, 0x06, 0xa1, 0x50, 0x02, 0xd5, 0xa2, 0x86, 0xb9, 0x68, 0x6c, 0x3e, - 0x1b, 0x89, 0x91, 0x88, 0xeb, 0x9d, 0xe8, 0x6d, 0x21, 0xd9, 0xfe, 0x5d, 0x81, 0xd2, 0xc9, 0x85, - 0x85, 0x30, 0x54, 0x88, 0xeb, 0x86, 0x54, 0x4a, 0xac, 0xb5, 0xb4, 0xb6, 0x6e, 0x2f, 0x97, 0xe8, - 0x0d, 0x34, 0x87, 0x82, 0x73, 0xa2, 0x68, 0x48, 0xb8, 0x43, 0xa4, 0xa4, 0x0a, 0x17, 0x63, 0xc9, - 0xfa, 0x5d, 0x7d, 0x2f, 0x2a, 0xa3, 0xef, 0xf0, 0x24, 0x2b, 0xf5, 0xc4, 0xc4, 0x57, 0xb8, 0x14, - 0x69, 0xf7, 0xcd, 0xeb, 0xdb, 0xad, 0xc2, 0xdf, 0xdb, 0xad, 0xd7, 0x23, 0xa6, 0xc6, 0x93, 0x81, - 0x39, 0x14, 0x5e, 0x67, 0x28, 0xa4, 0x27, 0x64, 0xf2, 0xd8, 0x91, 0xee, 0x65, 0xb2, 0xf9, 0x9e, - 0xaf, 0xec, 0x8c, 0xe7, 0x5e, 0xcc, 0x41, 0x16, 0xd4, 0x38, 0x23, 0x03, 0xc6, 0x99, 0x62, 0x54, - 0xe2, 0x72, 0x2e, 0x6c, 0x16, 0x81, 0xc6, 0x80, 0x99, 0xaf, 0x68, 0x48, 0xa5, 0x72, 0x02, 0xc2, - 0x5c, 0xe7, 0xce, 0x13, 0xaf, 0xe4, 0xc2, 0x3f, 0x5f, 0xf2, 0x2c, 0xc2, 0xdc, 0x83, 0x94, 0x86, - 0x06, 0xb0, 0xf1, 0xc0, 0x69, 0x22, 0x95, 0x70, 0xa7, 0x78, 0x35, 0x97, 0xcd, 0xd3, 0x7b, 0x36, - 0x0b, 0x14, 0xe2, 0xb0, 0x99, 0x7a, 0x4c, 0xfc, 0x87, 0xf3, 0x54, 0x72, 0x19, 0xa5, 0xe7, 0xd3, - 0x8f, 0x81, 0x99, 0x89, 0x5e, 0x42, 0x3d, 0x99, 0x21, 0x89, 0x44, 0x35, 0x8e, 0xc4, 0x5a, 0x52, - 0x5c, 0xe4, 0xa1, 0x0f, 0x8d, 0x54, 0xb4, 0x08, 0x83, 0x9e, 0x6b, 0x1b, 0x4b, 0xab, 0x24, 0x09, - 0x5f, 0xa0, 0xca, 0xe9, 0x15, 0x0d, 0xc9, 0x88, 0x62, 0x78, 0x34, 0xf0, 0x90, 0x0e, 0xed, 0xf4, - 0x7b, 0x74, 0x02, 0xe0, 0xa9, 0xc0, 0x19, 0x53, 0xc2, 0xd5, 0x18, 0xd7, 0x72, 0xd1, 0x74, 0x4f, - 0x05, 0x47, 0x31, 0x00, 0x7d, 0x80, 0x6a, 0x20, 0x24, 0x53, 0x4c, 0xf8, 0x78, 0xad, 0xa5, 0xb5, - 0x1b, 0xbb, 0x1b, 0x66, 0xe6, 0x27, 0x34, 0xad, 0xa4, 0x69, 0xa7, 0x32, 0xd4, 0x80, 0x22, 0x73, - 0x71, 0xbd, 0xa5, 0xb5, 0xcb, 0x76, 0x91, 0xb9, 0xc8, 0x80, 0x1a, 0xf1, 0x3c, 0x27, 0x10, 0x82, - 0x3b, 0xcc, 0xc5, 0x8d, 0xb8, 0xa1, 0x13, 0xcf, 0xb3, 0x84, 0xe0, 0x3d, 0x77, 0x7b, 0x17, 0xf4, - 0xaf, 0x63, 0xa6, 0xe8, 0x31, 0x93, 0x0a, 0xbd, 0x82, 0xc6, 0x15, 0xe1, 0xcc, 0x25, 0x4a, 0x84, - 0x0e, 0x67, 0x52, 0x61, 0xad, 0x55, 0x6a, 0xeb, 0x76, 0x3d, 0xad, 0x46, 0xb2, 0xb7, 0xef, 0xa1, - 0xba, 0x74, 0x46, 0xeb, 0x50, 0xeb, 0x9f, 0x9e, 0x5b, 0xdd, 0x83, 0xde, 0xa7, 0x5e, 0xf7, 0xb0, - 0x59, 0x40, 0x55, 0x28, 0x1f, 0x9f, 0x9d, 0x7e, 0x6e, 0x6a, 0x48, 0x87, 0x95, 0xf3, 0xa3, 0x33, - 0xfb, 0xa2, 0x59, 0xdc, 0xef, 0x5e, 0xcf, 0x0c, 0xed, 0x66, 0x66, 0x68, 0xff, 0x66, 0x86, 0xf6, - 0x6b, 0x6e, 0x14, 0x6e, 0xe6, 0x46, 0xe1, 0xcf, 0xdc, 0x28, 0x7c, 0x7b, 0x97, 0x39, 0x95, 0x68, - 0xb4, 0x1d, 0x9f, 0xaa, 0x1f, 0x22, 0xbc, 0x8c, 0x17, 0x9d, 0x9f, 0xf7, 0xee, 0xa1, 0xc1, 0x6a, - 0x7c, 0xcb, 0x7c, 0xfc, 0x1f, 0x00, 0x00, 0xff, 0xff, 0x93, 0x33, 0x6c, 0x16, 0xa3, 0x04, 0x00, - 0x00, + // 591 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x94, 0xdf, 0x4e, 0xdb, 0x3e, + 0x14, 0xc7, 0x9b, 0xf2, 0xaf, 0x39, 0xfd, 0x51, 0xfa, 0xf3, 0xd8, 0xf0, 0x36, 0x29, 0x54, 0x48, + 0x9b, 0xaa, 0x21, 0xda, 0x8d, 0x3d, 0x01, 0xff, 0x36, 0x2a, 0x15, 0x88, 0x02, 0x08, 0x69, 0xd2, + 0x14, 0x99, 0xda, 0x6a, 0x3d, 0xec, 0x38, 0x8a, 0x1d, 0x36, 0xde, 0x62, 0x8f, 0xc5, 0x25, 0x97, + 0xd3, 0x2e, 0xd0, 0x04, 0x4f, 0xb1, 0xbb, 0x29, 0x69, 0xd2, 0x86, 0xed, 0x8a, 0x5c, 0x25, 0x3e, + 0xe7, 0xe8, 0xf3, 0x3d, 0xc7, 0xfe, 0xda, 0xb0, 0xc2, 0xc4, 0x95, 0xee, 0x4a, 0x12, 0x0d, 0x79, + 0xd0, 0x35, 0x57, 0x21, 0xd3, 0x9d, 0x30, 0x52, 0x46, 0xa1, 0x7a, 0x92, 0xe8, 0x8c, 0x13, 0x2f, + 0x96, 0x87, 0x6a, 0xa8, 0xd2, 0x78, 0x37, 0xf9, 0x1b, 0x97, 0xac, 0xfd, 0xae, 0xc1, 0xcc, 0xc1, + 0x89, 0x8b, 0x30, 0x2c, 0x10, 0x4a, 0x23, 0xa6, 0x35, 0xb6, 0x5a, 0x56, 0xdb, 0xf6, 0xf2, 0x25, + 0x5a, 0x87, 0xff, 0x07, 0x4a, 0x08, 0x62, 0x58, 0x44, 0x84, 0x4f, 0xb4, 0x66, 0x46, 0xe3, 0x6a, + 0x6b, 0xa6, 0x6d, 0x7b, 0xcd, 0x69, 0x62, 0x2b, 0x8d, 0xa3, 0xcf, 0x80, 0x8a, 0xc5, 0x52, 0xc5, + 0x81, 0xd1, 0x78, 0x26, 0xa9, 0xde, 0xee, 0x5c, 0xdf, 0xae, 0x56, 0x7e, 0xde, 0xae, 0xbe, 0x1e, + 0x72, 0x33, 0x8a, 0xcf, 0x3b, 0x03, 0x25, 0xbb, 0x03, 0xa5, 0xa5, 0xd2, 0xd9, 0x67, 0x43, 0xd3, + 0x8b, 0xac, 0xff, 0x5e, 0x60, 0xbc, 0x82, 0xec, 0xd6, 0x18, 0x84, 0x5c, 0xa8, 0x0b, 0x4e, 0xce, + 0xb9, 0xe0, 0x86, 0x33, 0x8d, 0x67, 0x93, 0x4e, 0x1f, 0xcd, 0x2d, 0x22, 0xd0, 0x17, 0x78, 0xce, + 0x03, 0xc3, 0x22, 0xa6, 0x8d, 0x1f, 0x12, 0x4e, 0xfd, 0xa9, 0xa8, 0xc6, 0x73, 0xa5, 0xfa, 0x5e, + 0xc9, 0x81, 0x2e, 0xe1, 0x74, 0x67, 0x8a, 0x43, 0x14, 0x9e, 0xfd, 0xa5, 0x15, 0x6b, 0xa3, 0xe8, + 0x95, 0xc6, 0xf3, 0xa5, 0x84, 0x96, 0x1f, 0x08, 0x65, 0x2c, 0x14, 0xc0, 0xcb, 0x89, 0x4a, 0x1c, + 0xfc, 0x33, 0xd3, 0x42, 0x29, 0xa9, 0xc9, 0x26, 0x9d, 0xa6, 0xc4, 0xe2, 0x54, 0xaf, 0xa0, 0x91, + 0xcd, 0x91, 0x9b, 0xa3, 0x96, 0x9a, 0x63, 0x31, 0x8b, 0x66, 0xce, 0x38, 0x83, 0xa5, 0x49, 0x59, + 0x66, 0x0b, 0xbb, 0x54, 0x2b, 0xb9, 0x5a, 0xee, 0x89, 0x3e, 0xd8, 0x82, 0x5d, 0xb2, 0x88, 0x0c, + 0x99, 0xc6, 0xf0, 0x68, 0xe4, 0x2e, 0x1b, 0x78, 0x53, 0x00, 0x3a, 0x00, 0x90, 0x26, 0xf4, 0x47, + 0x8c, 0x08, 0x33, 0xc2, 0xf5, 0x47, 0x1b, 0x2c, 0xc5, 0x49, 0x13, 0xee, 0xa7, 0x00, 0xf4, 0x0e, + 0x6a, 0xa1, 0xd2, 0xdc, 0x70, 0x15, 0xe0, 0xff, 0x5a, 0x56, 0xbb, 0xb1, 0xf9, 0xb4, 0x53, 0xb8, + 0x94, 0x1d, 0x37, 0x4b, 0x7a, 0x93, 0x32, 0xd4, 0x80, 0x2a, 0xa7, 0x78, 0xb1, 0x65, 0xb5, 0x67, + 0xbd, 0x2a, 0xa7, 0xc8, 0x81, 0x3a, 0x91, 0xd2, 0x0f, 0x95, 0x12, 0x3e, 0xa7, 0xb8, 0x91, 0x26, + 0x6c, 0x22, 0xa5, 0xab, 0x94, 0xe8, 0x51, 0x44, 0x60, 0x79, 0xa0, 0x02, 0xad, 0x04, 0xa7, 0xc4, + 0x30, 0x3f, 0x1f, 0x05, 0x2f, 0x95, 0xea, 0xfd, 0x49, 0x81, 0xd5, 0xcf, 0x50, 0xe8, 0x14, 0x1a, + 0x3a, 0x96, 0x05, 0x1b, 0xe1, 0x66, 0xa9, 0x9b, 0xb7, 0xa8, 0x63, 0x39, 0xb5, 0xce, 0xda, 0x26, + 0xd8, 0x67, 0x23, 0x6e, 0x58, 0x9f, 0x6b, 0x93, 0xd8, 0xe8, 0x92, 0xa4, 0xba, 0x2a, 0xf2, 0x05, + 0xd7, 0x06, 0x5b, 0x63, 0x1b, 0x4d, 0xa2, 0x49, 0xd9, 0x9b, 0xb7, 0x50, 0xcb, 0xf7, 0x0c, 0x2d, + 0x41, 0xfd, 0xf4, 0xf0, 0xd8, 0xdd, 0xdb, 0xe9, 0x7d, 0xe8, 0xed, 0xed, 0x36, 0x2b, 0xa8, 0x06, + 0xb3, 0xfd, 0xa3, 0xc3, 0x8f, 0x4d, 0x0b, 0xd9, 0x30, 0x77, 0xbc, 0x7f, 0xe4, 0x9d, 0x34, 0xab, + 0xdb, 0x7b, 0xd7, 0x77, 0x8e, 0x75, 0x73, 0xe7, 0x58, 0xbf, 0xee, 0x1c, 0xeb, 0xfb, 0xbd, 0x53, + 0xb9, 0xb9, 0x77, 0x2a, 0x3f, 0xee, 0x9d, 0xca, 0xa7, 0xf5, 0x42, 0xdb, 0xc9, 0xa1, 0x6c, 0x04, + 0xcc, 0x7c, 0x55, 0xd1, 0x45, 0xba, 0xe8, 0x7e, 0x7b, 0xf0, 0xa2, 0x9e, 0xcf, 0xa7, 0xef, 0xe5, + 0xfb, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x49, 0x3f, 0x3a, 0xe7, 0x6d, 0x05, 0x00, 0x00, } func (m *MTP) Marshal() (dAtA []byte, err error) { @@ -255,6 +259,28 @@ func (m *MTP) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + { + size := m.SumCollateral.Size() + i -= size + if _, err := m.SumCollateral.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1 + i-- + dAtA[i] = 0x82 + { + size := m.ConsolidateLeverage.Size() + i -= size + if _, err := m.ConsolidateLeverage.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x7a if m.AmmPoolId != 0 { i = encodeVarintTypes(dAtA, i, uint64(m.AmmPoolId)) i-- @@ -280,63 +306,85 @@ func (m *MTP) MarshalToSizedBuffer(dAtA []byte) (int, error) { } i-- dAtA[i] = 0x5a - { - size := m.Leverage.Size() - i -= size - if _, err := m.Leverage.MarshalTo(dAtA[i:]); err != nil { - return 0, err + if len(m.Leverages) > 0 { + for iNdEx := len(m.Leverages) - 1; iNdEx >= 0; iNdEx-- { + { + size := m.Leverages[iNdEx].Size() + i -= size + if _, err := m.Leverages[iNdEx].MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x52 } - i = encodeVarintTypes(dAtA, i, uint64(size)) } - i-- - dAtA[i] = 0x52 - { - size := m.CustodyAmount.Size() - i -= size - if _, err := m.CustodyAmount.MarshalTo(dAtA[i:]); err != nil { - return 0, err + if len(m.CustodyAmounts) > 0 { + for iNdEx := len(m.CustodyAmounts) - 1; iNdEx >= 0; iNdEx-- { + { + size := m.CustodyAmounts[iNdEx].Size() + i -= size + if _, err := m.CustodyAmounts[iNdEx].MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x4a } - i = encodeVarintTypes(dAtA, i, uint64(size)) } - i-- - dAtA[i] = 0x4a - if len(m.CustodyAsset) > 0 { - i -= len(m.CustodyAsset) - copy(dAtA[i:], m.CustodyAsset) - i = encodeVarintTypes(dAtA, i, uint64(len(m.CustodyAsset))) - i-- - dAtA[i] = 0x42 + if len(m.CustodyAssets) > 0 { + for iNdEx := len(m.CustodyAssets) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.CustodyAssets[iNdEx]) + copy(dAtA[i:], m.CustodyAssets[iNdEx]) + i = encodeVarintTypes(dAtA, i, uint64(len(m.CustodyAssets[iNdEx]))) + i-- + dAtA[i] = 0x42 + } } - { - size := m.InterestUnpaidCollateral.Size() - i -= size - if _, err := m.InterestUnpaidCollateral.MarshalTo(dAtA[i:]); err != nil { - return 0, err + if len(m.InterestUnpaidCollaterals) > 0 { + for iNdEx := len(m.InterestUnpaidCollaterals) - 1; iNdEx >= 0; iNdEx-- { + { + size := m.InterestUnpaidCollaterals[iNdEx].Size() + i -= size + if _, err := m.InterestUnpaidCollaterals[iNdEx].MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x3a } - i = encodeVarintTypes(dAtA, i, uint64(size)) } - i-- - dAtA[i] = 0x3a - { - size := m.InterestPaidCustody.Size() - i -= size - if _, err := m.InterestPaidCustody.MarshalTo(dAtA[i:]); err != nil { - return 0, err + if len(m.InterestPaidCustodys) > 0 { + for iNdEx := len(m.InterestPaidCustodys) - 1; iNdEx >= 0; iNdEx-- { + { + size := m.InterestPaidCustodys[iNdEx].Size() + i -= size + if _, err := m.InterestPaidCustodys[iNdEx].MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x32 } - i = encodeVarintTypes(dAtA, i, uint64(size)) } - i-- - dAtA[i] = 0x32 - { - size := m.InterestPaidCollateral.Size() - i -= size - if _, err := m.InterestPaidCollateral.MarshalTo(dAtA[i:]); err != nil { - return 0, err + if len(m.InterestPaidCollaterals) > 0 { + for iNdEx := len(m.InterestPaidCollaterals) - 1; iNdEx >= 0; iNdEx-- { + { + size := m.InterestPaidCollaterals[iNdEx].Size() + i -= size + if _, err := m.InterestPaidCollaterals[iNdEx].MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x2a } - i = encodeVarintTypes(dAtA, i, uint64(size)) } - i-- - dAtA[i] = 0x2a { size := m.Liabilities.Size() i -= size @@ -347,22 +395,28 @@ func (m *MTP) MarshalToSizedBuffer(dAtA []byte) (int, error) { } i-- dAtA[i] = 0x22 - { - size := m.CollateralAmount.Size() - i -= size - if _, err := m.CollateralAmount.MarshalTo(dAtA[i:]); err != nil { - return 0, err + if len(m.CollateralAmounts) > 0 { + for iNdEx := len(m.CollateralAmounts) - 1; iNdEx >= 0; iNdEx-- { + { + size := m.CollateralAmounts[iNdEx].Size() + i -= size + if _, err := m.CollateralAmounts[iNdEx].MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a } - i = encodeVarintTypes(dAtA, i, uint64(size)) } - i-- - dAtA[i] = 0x1a - if len(m.CollateralAsset) > 0 { - i -= len(m.CollateralAsset) - copy(dAtA[i:], m.CollateralAsset) - i = encodeVarintTypes(dAtA, i, uint64(len(m.CollateralAsset))) - i-- - dAtA[i] = 0x12 + if len(m.CollateralAssets) > 0 { + for iNdEx := len(m.CollateralAssets) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.CollateralAssets[iNdEx]) + copy(dAtA[i:], m.CollateralAssets[iNdEx]) + i = encodeVarintTypes(dAtA, i, uint64(len(m.CollateralAssets[iNdEx]))) + i-- + dAtA[i] = 0x12 + } } if len(m.Address) > 0 { i -= len(m.Address) @@ -427,28 +481,56 @@ func (m *MTP) Size() (n int) { if l > 0 { n += 1 + l + sovTypes(uint64(l)) } - l = len(m.CollateralAsset) - if l > 0 { - n += 1 + l + sovTypes(uint64(l)) + if len(m.CollateralAssets) > 0 { + for _, s := range m.CollateralAssets { + l = len(s) + n += 1 + l + sovTypes(uint64(l)) + } + } + if len(m.CollateralAmounts) > 0 { + for _, e := range m.CollateralAmounts { + l = e.Size() + n += 1 + l + sovTypes(uint64(l)) + } } - l = m.CollateralAmount.Size() - n += 1 + l + sovTypes(uint64(l)) l = m.Liabilities.Size() n += 1 + l + sovTypes(uint64(l)) - l = m.InterestPaidCollateral.Size() - n += 1 + l + sovTypes(uint64(l)) - l = m.InterestPaidCustody.Size() - n += 1 + l + sovTypes(uint64(l)) - l = m.InterestUnpaidCollateral.Size() - n += 1 + l + sovTypes(uint64(l)) - l = len(m.CustodyAsset) - if l > 0 { - n += 1 + l + sovTypes(uint64(l)) + if len(m.InterestPaidCollaterals) > 0 { + for _, e := range m.InterestPaidCollaterals { + l = e.Size() + n += 1 + l + sovTypes(uint64(l)) + } + } + if len(m.InterestPaidCustodys) > 0 { + for _, e := range m.InterestPaidCustodys { + l = e.Size() + n += 1 + l + sovTypes(uint64(l)) + } + } + if len(m.InterestUnpaidCollaterals) > 0 { + for _, e := range m.InterestUnpaidCollaterals { + l = e.Size() + n += 1 + l + sovTypes(uint64(l)) + } + } + if len(m.CustodyAssets) > 0 { + for _, s := range m.CustodyAssets { + l = len(s) + n += 1 + l + sovTypes(uint64(l)) + } + } + if len(m.CustodyAmounts) > 0 { + for _, e := range m.CustodyAmounts { + l = e.Size() + n += 1 + l + sovTypes(uint64(l)) + } + } + if len(m.Leverages) > 0 { + for _, e := range m.Leverages { + l = e.Size() + n += 1 + l + sovTypes(uint64(l)) + } } - l = m.CustodyAmount.Size() - n += 1 + l + sovTypes(uint64(l)) - l = m.Leverage.Size() - n += 1 + l + sovTypes(uint64(l)) l = m.MtpHealth.Size() n += 1 + l + sovTypes(uint64(l)) if m.Position != 0 { @@ -460,6 +542,10 @@ func (m *MTP) Size() (n int) { if m.AmmPoolId != 0 { n += 1 + sovTypes(uint64(m.AmmPoolId)) } + l = m.ConsolidateLeverage.Size() + n += 1 + l + sovTypes(uint64(l)) + l = m.SumCollateral.Size() + n += 2 + l + sovTypes(uint64(l)) return n } @@ -547,7 +633,7 @@ func (m *MTP) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field CollateralAsset", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field CollateralAssets", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -575,11 +661,11 @@ func (m *MTP) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.CollateralAsset = string(dAtA[iNdEx:postIndex]) + m.CollateralAssets = append(m.CollateralAssets, string(dAtA[iNdEx:postIndex])) iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field CollateralAmount", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field CollateralAmounts", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -607,7 +693,9 @@ func (m *MTP) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.CollateralAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + var v github_com_cosmos_cosmos_sdk_types.Int + m.CollateralAmounts = append(m.CollateralAmounts, v) + if err := m.CollateralAmounts[len(m.CollateralAmounts)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -647,7 +735,7 @@ func (m *MTP) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 5: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field InterestPaidCollateral", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field InterestPaidCollaterals", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -675,13 +763,15 @@ func (m *MTP) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.InterestPaidCollateral.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + var v github_com_cosmos_cosmos_sdk_types.Int + m.InterestPaidCollaterals = append(m.InterestPaidCollaterals, v) + if err := m.InterestPaidCollaterals[len(m.InterestPaidCollaterals)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 6: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field InterestPaidCustody", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field InterestPaidCustodys", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -709,13 +799,15 @@ func (m *MTP) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.InterestPaidCustody.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + var v github_com_cosmos_cosmos_sdk_types.Int + m.InterestPaidCustodys = append(m.InterestPaidCustodys, v) + if err := m.InterestPaidCustodys[len(m.InterestPaidCustodys)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 7: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field InterestUnpaidCollateral", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field InterestUnpaidCollaterals", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -743,13 +835,15 @@ func (m *MTP) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.InterestUnpaidCollateral.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + var v github_com_cosmos_cosmos_sdk_types.Int + m.InterestUnpaidCollaterals = append(m.InterestUnpaidCollaterals, v) + if err := m.InterestUnpaidCollaterals[len(m.InterestUnpaidCollaterals)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 8: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field CustodyAsset", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field CustodyAssets", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -777,11 +871,11 @@ func (m *MTP) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.CustodyAsset = string(dAtA[iNdEx:postIndex]) + m.CustodyAssets = append(m.CustodyAssets, string(dAtA[iNdEx:postIndex])) iNdEx = postIndex case 9: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field CustodyAmount", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field CustodyAmounts", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -809,13 +903,15 @@ func (m *MTP) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.CustodyAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + var v github_com_cosmos_cosmos_sdk_types.Int + m.CustodyAmounts = append(m.CustodyAmounts, v) + if err := m.CustodyAmounts[len(m.CustodyAmounts)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 10: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Leverage", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Leverages", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -843,7 +939,9 @@ func (m *MTP) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.Leverage.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + var v github_com_cosmos_cosmos_sdk_types.Dec + m.Leverages = append(m.Leverages, v) + if err := m.Leverages[len(m.Leverages)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -938,6 +1036,74 @@ func (m *MTP) Unmarshal(dAtA []byte) error { break } } + case 15: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ConsolidateLeverage", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.ConsolidateLeverage.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 16: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field SumCollateral", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.SumCollateral.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) From c8e999703887c1125780e747e578d21669fe7faf Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Wed, 13 Sep 2023 09:17:21 +0000 Subject: [PATCH 04/10] chore: update open consolidate long to manage assets --- go.mod | 4 +-- go.sum | 6 ++++ .../keeper/calc_mtp_consolidate_collateral.go | 34 +++++++++++++++++++ .../keeper/calc_mtp_consolidate_leverage.go | 15 ++++++++ x/margin/keeper/open_consolidate_long.go | 10 ++++++ x/margin/keeper/open_long_process.go | 9 +++++ 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 x/margin/keeper/calc_mtp_consolidate_collateral.go create mode 100644 x/margin/keeper/calc_mtp_consolidate_leverage.go diff --git a/go.mod b/go.mod index b094c372c..426d8eaaf 100644 --- a/go.mod +++ b/go.mod @@ -167,10 +167,10 @@ require ( go.etcd.io/bbolt v1.3.7 // indirect go.opencensus.io v0.24.0 // indirect golang.org/x/crypto v0.11.0 // indirect - golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.12.0 // indirect golang.org/x/oauth2 v0.8.0 // indirect - golang.org/x/sys v0.10.0 // indirect + golang.org/x/sys v0.12.0 // indirect golang.org/x/term v0.10.0 // indirect golang.org/x/text v0.11.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect diff --git a/go.sum b/go.sum index cc77157c6..809a4223c 100644 --- a/go.sum +++ b/go.sum @@ -3369,6 +3369,8 @@ golang.org/x/exp v0.0.0-20230131160201-f062dba9d201/go.mod h1:CxIveKay+FTh1D0yPZ golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU= golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= @@ -3420,6 +3422,7 @@ golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -3788,6 +3791,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -3980,6 +3985,7 @@ golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/x/margin/keeper/calc_mtp_consolidate_collateral.go b/x/margin/keeper/calc_mtp_consolidate_collateral.go new file mode 100644 index 000000000..1a13b38f5 --- /dev/null +++ b/x/margin/keeper/calc_mtp_consolidate_collateral.go @@ -0,0 +1,34 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/margin/types" + ptypes "github.com/elys-network/elys/x/parameter/types" +) + +func (k Keeper) CalcMTPConsolidateCollateral(ctx sdk.Context, mtp *types.MTP) error { + consolidateCollateral := sdk.ZeroInt() + for i, asset := range mtp.CollateralAssets { + if asset == ptypes.USDC { + consolidateCollateral = consolidateCollateral.Add(mtp.CollateralAmounts[i]) + } else { + // swap into usdc + _, ammPool, _, err := k.OpenChecker.PreparePools(ctx, asset) + if err != nil { + return err + } + + collateralAmtIn := sdk.NewCoin(asset, mtp.CollateralAmounts[i]) + C, err := k.EstimateSwapGivenOut(ctx, collateralAmtIn, ptypes.USDC, ammPool) + if err != nil { + return err + } + + consolidateCollateral = consolidateCollateral.Add(C) + } + } + + mtp.SumCollateral = consolidateCollateral + + return nil +} diff --git a/x/margin/keeper/calc_mtp_consolidate_leverage.go b/x/margin/keeper/calc_mtp_consolidate_leverage.go new file mode 100644 index 000000000..c4a2d7e3f --- /dev/null +++ b/x/margin/keeper/calc_mtp_consolidate_leverage.go @@ -0,0 +1,15 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/margin/types" +) + +func (k Keeper) CalcMTPConsolidateLiability(ctx sdk.Context, mtp *types.MTP) { + if mtp.SumCollateral.IsZero() { + return + } + + leverage := mtp.Liabilities.Quo(mtp.SumCollateral) + mtp.ConsolidateLeverage = sdk.NewDecFromInt(leverage) +} diff --git a/x/margin/keeper/open_consolidate_long.go b/x/margin/keeper/open_consolidate_long.go index 39293d51a..1e692b7f3 100644 --- a/x/margin/keeper/open_consolidate_long.go +++ b/x/margin/keeper/open_consolidate_long.go @@ -1,6 +1,8 @@ package keeper import ( + "golang.org/x/exp/slices" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/elys-network/elys/x/margin/types" ) @@ -12,5 +14,13 @@ func (k Keeper) OpenConsolidateLong(ctx sdk.Context, poolId uint64, mtp *types.M collateralAmountDec := sdk.NewDecFromBigInt(msg.CollateralAmount.BigInt()) mtp.Leverages = append(mtp.Leverages, leverage) + if !slices.Contains(mtp.CollateralAssets, msg.CollateralAsset) { + mtp.CollateralAssets = append(mtp.CollateralAssets, msg.CollateralAsset) + } + + if !slices.Contains(mtp.CustodyAssets, msg.BorrowAsset) { + mtp.CollateralAssets = append(mtp.CustodyAssets, msg.BorrowAsset) + } + return k.ProcessOpenLong(ctx, mtp, leverage, eta, collateralAmountDec, poolId, msg) } diff --git a/x/margin/keeper/open_long_process.go b/x/margin/keeper/open_long_process.go index a294bb8dd..bd624eb06 100644 --- a/x/margin/keeper/open_long_process.go +++ b/x/margin/keeper/open_long_process.go @@ -84,5 +84,14 @@ func (k Keeper) ProcessOpenLong(ctx sdk.Context, mtp *types.MTP, leverage sdk.De return nil, types.ErrMTPUnhealthy } + // Update consolidated collateral amount + k.CalcMTPConsolidateCollateral(ctx, mtp) + + // Calculate consolidate liabiltiy + k.CalcMTPConsolidateLiability(ctx, mtp) + + // Set MTP + k.SetMTP(ctx, mtp) + return mtp, nil } From 93fbf4889e4835f0ff1c8083d3b544a87c66a29f Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Wed, 13 Sep 2023 12:50:39 +0000 Subject: [PATCH 05/10] fix: go mod that breaks build --- docs/static/openapi.yml | 44 ++++++++++--------- go.mod | 4 +- go.sum | 6 --- .../keeper/check_same_asset_position_test.go | 23 +++++++--- 4 files changed, 42 insertions(+), 35 deletions(-) diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index 796604c15..1d03fd3a9 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -39638,18 +39638,18 @@ paths: items: type: object properties: + liabilities: + type: string custody: type: string asset_balance: type: string + unsettled_liabilities: + type: string block_interest: type: string asset_denom: type: string - liabilities: - type: string - unsettled_liabilities: - type: string default: description: An unexpected error response. schema: @@ -39706,18 +39706,18 @@ paths: items: type: object properties: + liabilities: + type: string custody: type: string asset_balance: type: string + unsettled_liabilities: + type: string block_interest: type: string asset_denom: type: string - liabilities: - type: string - unsettled_liabilities: - type: string pagination: type: object properties: @@ -79167,25 +79167,29 @@ definitions: items: type: object properties: + liabilities: + type: string custody: type: string asset_balance: type: string + unsettled_liabilities: + type: string block_interest: type: string asset_denom: type: string - liabilities: - type: string - unsettled_liabilities: - type: string elys.margin.PoolAsset: type: object properties: + liabilities: + type: string custody: type: string asset_balance: type: string + unsettled_liabilities: + type: string block_interest: type: string asset_denom: @@ -79488,18 +79492,18 @@ definitions: items: type: object properties: + liabilities: + type: string custody: type: string asset_balance: type: string + unsettled_liabilities: + type: string block_interest: type: string asset_denom: type: string - liabilities: - type: string - unsettled_liabilities: - type: string pagination: type: object properties: @@ -79548,18 +79552,18 @@ definitions: items: type: object properties: + liabilities: + type: string custody: type: string asset_balance: type: string + unsettled_liabilities: + type: string block_interest: type: string asset_denom: type: string - liabilities: - type: string - unsettled_liabilities: - type: string elys.margin.StatusResponse: type: object properties: diff --git a/go.mod b/go.mod index 426d8eaaf..05b4a4004 100644 --- a/go.mod +++ b/go.mod @@ -167,10 +167,10 @@ require ( go.etcd.io/bbolt v1.3.7 // indirect go.opencensus.io v0.24.0 // indirect golang.org/x/crypto v0.11.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc golang.org/x/net v0.12.0 // indirect golang.org/x/oauth2 v0.8.0 // indirect - golang.org/x/sys v0.12.0 // indirect + golang.org/x/sys v0.10.0 // indirect golang.org/x/term v0.10.0 // indirect golang.org/x/text v0.11.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect diff --git a/go.sum b/go.sum index 809a4223c..cc77157c6 100644 --- a/go.sum +++ b/go.sum @@ -3369,8 +3369,6 @@ golang.org/x/exp v0.0.0-20230131160201-f062dba9d201/go.mod h1:CxIveKay+FTh1D0yPZ golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU= golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= @@ -3422,7 +3420,6 @@ golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -3791,8 +3788,6 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -3985,7 +3980,6 @@ golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= -golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/x/margin/keeper/check_same_asset_position_test.go b/x/margin/keeper/check_same_asset_position_test.go index d2bb3645a..b4960374a 100644 --- a/x/margin/keeper/check_same_asset_position_test.go +++ b/x/margin/keeper/check_same_asset_position_test.go @@ -5,11 +5,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/elys-network/elys/x/margin/keeper" + "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_OpenPositionsBelowMax(t *testing.T) { +func TestCheckSameAssets_NewPosition(t *testing.T) { // Setup the mock checker mockChecker := new(mocks.PositionChecker) @@ -19,14 +21,21 @@ func TestCheckSameAssets_OpenPositionsBelowMax(t *testing.T) { } 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) + + msg := &types.MsgOpen{ + Creator: "creator", + CollateralAsset: ptypes.ATOM, + CollateralAmount: sdk.NewInt(100), + BorrowAsset: ptypes.ATOM, + Position: types.Position_SHORT, + Leverage: sdk.NewDec(1), + } - // Mock behavior - mockChecker.On("GetOpenMTPCount", ctx).Return(uint64(5)) - mockChecker.On("GetMaxOpenPositions", ctx).Return(uint64(10)) - - err := k.CheckMaxOpenPositions(ctx) + mtp = k.CheckSamePosition(ctx, msg) // Expect no error - assert.Nil(t, err) + assert.Nil(t, mtp) mockChecker.AssertExpectations(t) } From 25284748ab087b6541874037996d4f7b314a9356 Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Thu, 14 Sep 2023 07:42:36 +0000 Subject: [PATCH 06/10] 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) From dde93f2e7f5209892c0971549005819c3bb86e98 Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Thu, 14 Sep 2023 09:46:45 +0000 Subject: [PATCH 07/10] fix: nullify for mtp position initialization and compare --- x/margin/client/cli/query_mtp_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/x/margin/client/cli/query_mtp_test.go b/x/margin/client/cli/query_mtp_test.go index e18e55289..04f8c106e 100644 --- a/x/margin/client/cli/query_mtp_test.go +++ b/x/margin/client/cli/query_mtp_test.go @@ -15,7 +15,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" simapp "github.com/elys-network/elys/app" "github.com/elys-network/elys/testutil/network" - "github.com/elys-network/elys/testutil/nullify" "github.com/elys-network/elys/x/margin/client/cli" "github.com/elys-network/elys/x/margin/types" paramtypes "github.com/elys-network/elys/x/parameter/types" @@ -51,8 +50,10 @@ func networkWithMTPObjects(t *testing.T, n int) (*network.Network, []*types.MTP) Position: types.Position_LONG, Id: (uint64)(i + 1), AmmPoolId: (uint64)(i + 1), + ConsolidateLeverage: sdk.ZeroDec(), + SumCollateral: sdk.ZeroInt(), } - nullify.Fill(&mtp) + mtps = append(mtps, &mtp) state.MtpList = append(state.MtpList, mtp) } @@ -115,8 +116,8 @@ func TestShowMTP(t *testing.T) { require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.Mtp) require.Equal(t, - nullify.Fill(&tc.obj), - nullify.Fill(resp.Mtp), + tc.obj, + resp.Mtp, ) } }) From 55ce6f1d770a9d36028ab159227acfd7c9418777 Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Thu, 14 Sep 2023 12:27:16 +0000 Subject: [PATCH 08/10] fix: remove nullify in preparing mtp testdata --- x/margin/keeper/keeper_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/x/margin/keeper/keeper_test.go b/x/margin/keeper/keeper_test.go index c1eae8bca..945e2a1c7 100644 --- a/x/margin/keeper/keeper_test.go +++ b/x/margin/keeper/keeper_test.go @@ -6,7 +6,6 @@ import ( tmproto "github.com/cometbft/cometbft/proto/tendermint/types" sdk "github.com/cosmos/cosmos-sdk/types" simapp "github.com/elys-network/elys/app" - "github.com/elys-network/elys/testutil/nullify" "github.com/elys-network/elys/x/margin/types" paramtypes "github.com/elys-network/elys/x/parameter/types" "github.com/stretchr/testify/require" @@ -41,16 +40,15 @@ func TestSetGetMTP(t *testing.T) { MtpHealth: sdk.NewDec(0), Position: types.Position_LONG, Id: 0, + ConsolidateLeverage: sdk.ZeroDec(), + SumCollateral: sdk.ZeroInt(), } - nullify.Fill(&mtp) - err := margin.SetMTP(ctx, &mtp) require.NoError(t, err) } mtpCount := margin.GetMTPCount(ctx) require.Equal(t, mtpCount, (uint64)(2)) - } func TestGetAllWhitelistedAddress(t *testing.T) { From 297307d47562c28b5c302c75976347257accdcb7 Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Thu, 14 Sep 2023 16:57:07 +0200 Subject: [PATCH 09/10] refactor: additional short logic + test fix --- ...n_native_asset.go => get_trading_asset.go} | 2 +- ...sset_test.go => get_trading_asset_test.go} | 16 +- x/margin/keeper/keeper.go | 3 + x/margin/keeper/open.go | 2 +- x/margin/keeper/open_consolidate.go | 2 +- x/margin/keeper/open_long.go | 8 + x/margin/keeper/open_long_process.go | 8 +- x/margin/keeper/open_long_test.go | 20 +- x/margin/keeper/open_short.go | 24 + x/margin/keeper/open_short_process.go | 63 ++ x/margin/keeper/open_test.go | 12 +- x/margin/types/expected_keepers.go | 29 +- x/margin/types/mocks/open_checker.go | 20 +- x/margin/types/mocks/open_long_checker.go | 206 +++- x/margin/types/mocks/open_short_checker.go | 888 ++++++++++++++++++ 15 files changed, 1219 insertions(+), 84 deletions(-) rename x/margin/keeper/{get_non_native_asset.go => get_trading_asset.go} (67%) rename x/margin/keeper/{get_non_native_asset_test.go => get_trading_asset_test.go} (65%) create mode 100644 x/margin/keeper/open_short.go create mode 100644 x/margin/keeper/open_short_process.go create mode 100644 x/margin/types/mocks/open_short_checker.go diff --git a/x/margin/keeper/get_non_native_asset.go b/x/margin/keeper/get_trading_asset.go similarity index 67% rename from x/margin/keeper/get_non_native_asset.go rename to x/margin/keeper/get_trading_asset.go index 3efd41f93..315befdea 100644 --- a/x/margin/keeper/get_non_native_asset.go +++ b/x/margin/keeper/get_trading_asset.go @@ -4,7 +4,7 @@ import ( paramtypes "github.com/elys-network/elys/x/parameter/types" ) -func (k Keeper) GetNonNativeAsset(collateralAsset string, borrowAsset string) string { +func (k Keeper) GetTradingAsset(collateralAsset string, borrowAsset string) string { if collateralAsset == paramtypes.USDC { return borrowAsset } diff --git a/x/margin/keeper/get_non_native_asset_test.go b/x/margin/keeper/get_trading_asset_test.go similarity index 65% rename from x/margin/keeper/get_non_native_asset_test.go rename to x/margin/keeper/get_trading_asset_test.go index ae0f9a65f..e52e49d8a 100644 --- a/x/margin/keeper/get_non_native_asset_test.go +++ b/x/margin/keeper/get_trading_asset_test.go @@ -8,36 +8,36 @@ import ( "github.com/stretchr/testify/assert" ) -func TestGetNonNativeAsset_WhenCollateralIsUSDC(t *testing.T) { +func TestGetTradingAsset_WhenCollateralIsUSDC(t *testing.T) { // Create an instance of Keeper k := keeper.Keeper{} // Test case: collateral is USDC and borrow is ATOM - result := k.GetNonNativeAsset(ptypes.USDC, ptypes.ATOM) + result := k.GetTradingAsset(ptypes.USDC, ptypes.ATOM) assert.Equal(t, ptypes.ATOM, result) // Test case: both collateral and borrow are USDC - result = k.GetNonNativeAsset(ptypes.USDC, ptypes.USDC) + result = k.GetTradingAsset(ptypes.USDC, ptypes.USDC) assert.Equal(t, ptypes.USDC, result) // Test case: collateral is USDC and borrow is some other asset (e.g., BTC) - result = k.GetNonNativeAsset(ptypes.USDC, "BTC") + result = k.GetTradingAsset(ptypes.USDC, "BTC") assert.Equal(t, "BTC", result) } -func TestGetNonNativeAsset_WhenCollateralIsNotUSDC(t *testing.T) { +func TestGetTradingAsset_WhenCollateralIsNotUSDC(t *testing.T) { // Create an instance of Keeper k := keeper.Keeper{} // Test case: collateral is ATOM and borrow is USDC - result := k.GetNonNativeAsset(ptypes.ATOM, ptypes.USDC) + result := k.GetTradingAsset(ptypes.ATOM, ptypes.USDC) assert.Equal(t, ptypes.ATOM, result) // Test case: both collateral and borrow are ATOM - result = k.GetNonNativeAsset(ptypes.ATOM, ptypes.ATOM) + result = k.GetTradingAsset(ptypes.ATOM, ptypes.ATOM) assert.Equal(t, ptypes.ATOM, result) // Test case: collateral is some other asset (e.g., BTC) and borrow is USDC - result = k.GetNonNativeAsset("BTC", ptypes.USDC) + result = k.GetTradingAsset("BTC", ptypes.USDC) assert.Equal(t, "BTC", result) } diff --git a/x/margin/keeper/keeper.go b/x/margin/keeper/keeper.go index 5067246d0..343caac6e 100644 --- a/x/margin/keeper/keeper.go +++ b/x/margin/keeper/keeper.go @@ -28,8 +28,10 @@ type ( types.PoolChecker types.OpenChecker types.OpenLongChecker + types.OpenShortChecker types.CloseLongChecker types.CloseShortChecker + cdc codec.BinaryCodec storeKey storetypes.StoreKey memKey storetypes.StoreKey @@ -71,6 +73,7 @@ func NewKeeper( keeper.PoolChecker = keeper keeper.OpenChecker = keeper keeper.OpenLongChecker = keeper + keeper.OpenShortChecker = keeper keeper.CloseLongChecker = keeper keeper.CloseShortChecker = keeper diff --git a/x/margin/keeper/open.go b/x/margin/keeper/open.go index d892b8f40..04b6031ef 100644 --- a/x/margin/keeper/open.go +++ b/x/margin/keeper/open.go @@ -25,7 +25,7 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenRespons } // Get token asset other than USDC - nonNativeAsset := k.OpenChecker.GetNonNativeAsset(msg.CollateralAsset, msg.BorrowAsset) + nonNativeAsset := k.OpenChecker.GetTradingAsset(msg.CollateralAsset, msg.BorrowAsset) // Get pool id, amm pool, and margin pool poolId, ammPool, pool, err := k.OpenChecker.PreparePools(ctx, nonNativeAsset) diff --git a/x/margin/keeper/open_consolidate.go b/x/margin/keeper/open_consolidate.go index 8e823dc3e..e8c49d05f 100644 --- a/x/margin/keeper/open_consolidate.go +++ b/x/margin/keeper/open_consolidate.go @@ -8,7 +8,7 @@ import ( func (k Keeper) OpenConsolidate(ctx sdk.Context, mtp *types.MTP, msg *types.MsgOpen) (*types.MsgOpenResponse, error) { // Get token asset other than USDC - nonNativeAsset := k.OpenLongChecker.GetNonNativeAsset(msg.CollateralAsset, msg.BorrowAsset) + nonNativeAsset := k.OpenLongChecker.GetTradingAsset(msg.CollateralAsset, msg.BorrowAsset) poolId := mtp.AmmPoolId pool, found := k.OpenLongChecker.GetPool(ctx, poolId) diff --git a/x/margin/keeper/open_long.go b/x/margin/keeper/open_long.go index ac95bc3e0..69a4ffcb7 100644 --- a/x/margin/keeper/open_long.go +++ b/x/margin/keeper/open_long.go @@ -6,11 +6,19 @@ import ( ) func (k Keeper) OpenLong(ctx sdk.Context, poolId uint64, msg *types.MsgOpen) (*types.MTP, error) { + // Determine the maximum leverage available and compute the effective leverage to be used. maxLeverage := k.OpenLongChecker.GetMaxLeverageParam(ctx) leverage := sdk.MinDec(msg.Leverage, maxLeverage) + + // Calculate the eta value. eta := leverage.Sub(sdk.OneDec()) + + // Convert the collateral amount into a decimal format. collateralAmountDec := sdk.NewDecFromBigInt(msg.CollateralAmount.BigInt()) + + // Initialize a new Margin Trading Position (MTP). mtp := types.NewMTP(msg.Creator, msg.CollateralAsset, msg.BorrowAsset, msg.Position, leverage, poolId) + // Call the function to process the open long logic. return k.ProcessOpenLong(ctx, mtp, leverage, eta, collateralAmountDec, poolId, msg) } diff --git a/x/margin/keeper/open_long_process.go b/x/margin/keeper/open_long_process.go index bd624eb06..34e1f4f54 100644 --- a/x/margin/keeper/open_long_process.go +++ b/x/margin/keeper/open_long_process.go @@ -9,7 +9,7 @@ import ( func (k Keeper) ProcessOpenLong(ctx sdk.Context, mtp *types.MTP, leverage sdk.Dec, eta sdk.Dec, collateralAmountDec sdk.Dec, poolId uint64, msg *types.MsgOpen) (*types.MTP, error) { // Get token asset other than USDC - nonNativeAsset := k.OpenLongChecker.GetNonNativeAsset(msg.CollateralAsset, msg.BorrowAsset) + nonNativeAsset := k.OpenLongChecker.GetTradingAsset(msg.CollateralAsset, msg.BorrowAsset) pool, found := k.OpenLongChecker.GetPool(ctx, poolId) if !found { @@ -85,13 +85,13 @@ func (k Keeper) ProcessOpenLong(ctx sdk.Context, mtp *types.MTP, leverage sdk.De } // Update consolidated collateral amount - k.CalcMTPConsolidateCollateral(ctx, mtp) + k.OpenLongChecker.CalcMTPConsolidateCollateral(ctx, mtp) // Calculate consolidate liabiltiy - k.CalcMTPConsolidateLiability(ctx, mtp) + k.OpenLongChecker.CalcMTPConsolidateLiability(ctx, mtp) // Set MTP - k.SetMTP(ctx, mtp) + k.OpenLongChecker.SetMTP(ctx, mtp) return mtp, nil } diff --git a/x/margin/keeper/open_long_test.go b/x/margin/keeper/open_long_test.go index bd8def8d0..5924effc3 100644 --- a/x/margin/keeper/open_long_test.go +++ b/x/margin/keeper/open_long_test.go @@ -40,7 +40,7 @@ func TestOpenLong_PoolNotFound(t *testing.T) { // Mock behavior mockChecker.On("GetMaxLeverageParam", ctx).Return(msg.Leverage) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.CollateralAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.CollateralAsset) mockChecker.On("GetPool", ctx, poolId).Return(types.Pool{}, false) _, err := k.OpenLong(ctx, poolId, msg) @@ -70,7 +70,7 @@ func TestOpenLong_PoolDisabled(t *testing.T) { // Mock behaviors mockChecker.On("GetMaxLeverageParam", ctx).Return(msg.Leverage) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.CollateralAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.CollateralAsset) mockChecker.On("GetPool", ctx, poolId).Return(types.Pool{}, true) mockChecker.On("IsPoolEnabled", ctx, poolId).Return(false) @@ -105,7 +105,7 @@ func TestOpenLong_InsufficientAmmPoolBalanceForLeveragedAmount(t *testing.T) { // Mock the behaviors to get to the HasSufficientPoolBalance check mockChecker.On("GetMaxLeverageParam", ctx).Return(msg.Leverage) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("GetPool", ctx, poolId).Return(types.Pool{}, true) mockChecker.On("IsPoolEnabled", ctx, poolId).Return(true) mockChecker.On("GetAmmPool", ctx, poolId, msg.BorrowAsset).Return(ammtypes.Pool{}, nil) // Assuming a valid pool is returned @@ -144,7 +144,7 @@ func TestOpenLong_InsufficientLiabilities(t *testing.T) { // Mock the behaviors to get to the CheckMinLiabilities check mockChecker.On("GetMaxLeverageParam", ctx).Return(msg.Leverage) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("GetPool", ctx, poolId).Return(types.Pool{}, true) mockChecker.On("IsPoolEnabled", ctx, poolId).Return(true) mockChecker.On("GetAmmPool", ctx, poolId, msg.BorrowAsset).Return(ammtypes.Pool{}, nil) // Assuming a valid pool is returned @@ -186,7 +186,7 @@ func TestOpenLong_InsufficientAmmPoolBalanceForCustody(t *testing.T) { ) // Mock behaviors mockChecker.On("GetMaxLeverageParam", ctx).Return(msg.Leverage) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("GetPool", ctx, poolId).Return(types.Pool{}, true) mockChecker.On("IsPoolEnabled", ctx, poolId).Return(true) mockChecker.On("GetAmmPool", ctx, poolId, msg.BorrowAsset).Return(ammtypes.Pool{}, nil) @@ -238,7 +238,7 @@ func TestOpenLong_ErrorsDuringOperations(t *testing.T) { // Mock behaviors mockChecker.On("GetMaxLeverageParam", ctx).Return(msg.Leverage) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("GetPool", ctx, poolId).Return(types.Pool{}, true) mockChecker.On("IsPoolEnabled", ctx, poolId).Return(true) mockChecker.On("GetAmmPool", ctx, poolId, msg.BorrowAsset).Return(ammtypes.Pool{}, nil) @@ -295,7 +295,7 @@ func TestOpenLong_LeverageRatioLessThanSafetyFactor(t *testing.T) { // Mock behaviors mockChecker.On("GetMaxLeverageParam", ctx).Return(msg.Leverage) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("GetPool", ctx, poolId).Return(types.Pool{}, true) mockChecker.On("IsPoolEnabled", ctx, poolId).Return(true) mockChecker.On("GetAmmPool", ctx, poolId, msg.BorrowAsset).Return(ammtypes.Pool{}, nil) @@ -358,7 +358,7 @@ func TestOpenLong_Success(t *testing.T) { // Mock behaviors mockChecker.On("GetMaxLeverageParam", ctx).Return(msg.Leverage) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("GetPool", ctx, poolId).Return(types.Pool{}, true) mockChecker.On("IsPoolEnabled", ctx, poolId).Return(true) mockChecker.On("GetAmmPool", ctx, poolId, msg.BorrowAsset).Return(ammtypes.Pool{}, nil) @@ -393,6 +393,10 @@ func TestOpenLong_Success(t *testing.T) { mockChecker.On("GetSafetyFactor", ctx).Return(safetyFactor) + mockChecker.On("CalcMTPConsolidateCollateral", ctx, mtp).Return(nil) + mockChecker.On("CalcMTPConsolidateLiability", ctx, mtp).Return() + mockChecker.On("SetMTP", ctx, mtp).Return(nil) + _, err := k.OpenLong(ctx, poolId, msg) // Expect no error assert.Nil(t, err) diff --git a/x/margin/keeper/open_short.go b/x/margin/keeper/open_short.go new file mode 100644 index 000000000..c602d9602 --- /dev/null +++ b/x/margin/keeper/open_short.go @@ -0,0 +1,24 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/margin/types" +) + +func (k Keeper) OpenShort(ctx sdk.Context, poolId uint64, msg *types.MsgOpen) (*types.MTP, error) { + // Determine the maximum leverage available and compute the effective leverage to be used. + maxLeverage := k.OpenShortChecker.GetMaxLeverageParam(ctx) + leverage := sdk.MinDec(msg.Leverage, maxLeverage) + + // Calculate the eta value. + eta := leverage.Sub(sdk.OneDec()) + + // Convert the collateral amount into a decimal format. + collateralAmountDec := sdk.NewDecFromBigInt(msg.CollateralAmount.BigInt()) + + // Initialize a new Margin Trading Position (MTP). + mtp := types.NewMTP(msg.Creator, msg.CollateralAsset, msg.BorrowAsset, msg.Position, leverage, poolId) + + // Call the function to process the open short logic. + return k.ProcessOpenShort(ctx, mtp, leverage, eta, collateralAmountDec, poolId, msg) +} diff --git a/x/margin/keeper/open_short_process.go b/x/margin/keeper/open_short_process.go new file mode 100644 index 000000000..c3db9b6b0 --- /dev/null +++ b/x/margin/keeper/open_short_process.go @@ -0,0 +1,63 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/margin/types" +) + +func (k Keeper) ProcessOpenShort(ctx sdk.Context, mtp *types.MTP, leverage sdk.Dec, eta sdk.Dec, collateralAmountDec sdk.Dec, poolId uint64, msg *types.MsgOpen) (*types.MTP, error) { + // // Determine the non-native asset (i.e., not USDC). + // nonNativeAsset := k.OpenShortChecker.GetTradingAsset(msg.CollateralAsset, msg.BorrowAsset) + + // // Fetch the pool associated with the given pool ID. + // pool, found := k.OpenShortChecker.GetPool(ctx, poolId) + // if !found { + // return nil, sdkerrors.Wrap(types.ErrPoolDoesNotExist, nonNativeAsset) + // } + + // // Check if the pool is enabled. + // if !k.OpenShortChecker.IsPoolEnabled(ctx, poolId) { + // return nil, sdkerrors.Wrap(types.ErrMTPDisabled, nonNativeAsset) + // } + + // // Fetch the corresponding AMM (Automated Market Maker) pool. + // ammPool, err := k.OpenShortChecker.GetAmmPool(ctx, poolId, nonNativeAsset) + // if err != nil { + // return nil, err + // } + + // // Calculate the leveraged amount based on the collateral provided and the leverage. + // leveragedAmount := sdk.NewInt(collateralAmountDec.Mul(leverage).TruncateInt().Int64()) + + // // Borrow the asset the user wants to short. + // // ... (Logic to borrow the asset; error handling) ... + + // // Swap the borrowed asset for USDC. + // swappedAmount, err := k.OpenShortChecker.EstimateSwap(ctx, leveragedAmount, ptypes.USDC, ammPool) + // if err != nil { + // return nil, err + // } + + // // Ensure the AMM pool has enough balance. + // if !k.OpenShortChecker.HasSufficientPoolBalance(ctx, ammPool, msg.BorrowAsset, swappedAmount) { + // return nil, sdkerrors.Wrap(types.ErrSwapTooHigh, swappedAmount.String()) + // } + + // // Additional checks and operations: + // // 1. Check minimum liabilities. + // err = k.OpenShortChecker.CheckMinLiabilities(ctx, swappedAmount, eta, pool, ammPool, msg.CollateralAsset) + // if err != nil { + // return nil, err + // } + + // // 2. Update the pool and MTP health. + // if err = k.OpenShortChecker.UpdatePoolHealth(ctx, &pool); err != nil { + // return nil, err + // } + // if err = k.OpenShortChecker.UpdateMTPHealth(ctx, *mtp, ammPool); err != nil { + // return nil, err + // } + + // Return the updated Margin Trading Position (MTP). + return mtp, nil +} diff --git a/x/margin/keeper/open_test.go b/x/margin/keeper/open_test.go index 1edea14aa..cbbb6a69f 100644 --- a/x/margin/keeper/open_test.go +++ b/x/margin/keeper/open_test.go @@ -115,7 +115,7 @@ func TestOpen_ErrorPreparePools(t *testing.T) { mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil) mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil) mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + 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")) _, err := k.Open(ctx, msg) @@ -146,7 +146,7 @@ func TestOpen_ErrorCheckPoolHealth(t *testing.T) { mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil) mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil) mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil) mockChecker.On("CheckPoolHealth", ctx, poolId).Return(sdkerrors.Wrap(types.ErrInvalidBorrowingAsset, "invalid collateral asset")) @@ -178,7 +178,7 @@ func TestOpen_ErrorInvalidPosition(t *testing.T) { mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil) mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil) mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil) mockChecker.On("CheckPoolHealth", ctx, poolId).Return(nil) @@ -211,7 +211,7 @@ func TestOpen_ErrorOpenLong(t *testing.T) { mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil) mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil) mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil) mockChecker.On("CheckPoolHealth", ctx, poolId).Return(nil) mockChecker.On("OpenLong", ctx, poolId, msg).Return(&types.MTP{}, errors.New("error executing open long")) @@ -245,7 +245,7 @@ func TestOpen_ErrorOpenShort(t *testing.T) { mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil) mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil) mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil) mockChecker.On("CheckPoolHealth", ctx, poolId).Return(nil) mockChecker.On("OpenShort", ctx, poolId, msg).Return(&types.MTP{}, errors.New("error executing open short")) @@ -280,7 +280,7 @@ func TestOpen_Successful(t *testing.T) { mockChecker.On("CheckLongingAssets", ctx, msg.CollateralAsset, msg.BorrowAsset).Return(nil) mockChecker.On("CheckUserAuthorization", ctx, msg).Return(nil) mockChecker.On("CheckMaxOpenPositions", ctx).Return(nil) - mockChecker.On("GetNonNativeAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) + mockChecker.On("GetTradingAsset", msg.CollateralAsset, msg.BorrowAsset).Return(msg.BorrowAsset) mockChecker.On("PreparePools", ctx, msg.BorrowAsset).Return(poolId, ammtypes.Pool{}, types.Pool{}, nil) mockChecker.On("CheckPoolHealth", ctx, poolId).Return(nil) mockChecker.On("OpenShort", ctx, poolId, msg).Return(mtp, nil) diff --git a/x/margin/types/expected_keepers.go b/x/margin/types/expected_keepers.go index ef3a97d0b..b27d21583 100644 --- a/x/margin/types/expected_keepers.go +++ b/x/margin/types/expected_keepers.go @@ -32,7 +32,7 @@ type OpenChecker interface { CheckLongingAssets(ctx sdk.Context, collateralAsset string, borrowAsset string) error CheckUserAuthorization(ctx sdk.Context, msg *MsgOpen) error CheckMaxOpenPositions(ctx sdk.Context) error - GetNonNativeAsset(collateralAsset string, borrowAsset string) string + GetTradingAsset(collateralAsset string, borrowAsset string) string PreparePools(ctx sdk.Context, nonNativeAsset string) (poolId uint64, ammPool ammtypes.Pool, pool Pool, err error) CheckPoolHealth(ctx sdk.Context, poolId uint64) error OpenLong(ctx sdk.Context, poolId uint64, msg *MsgOpen) (*MTP, error) @@ -43,7 +43,7 @@ type OpenChecker interface { //go:generate mockery --srcpkg . --name OpenLongChecker --structname OpenLongChecker --filename open_long_checker.go --with-expecter type OpenLongChecker interface { GetMaxLeverageParam(ctx sdk.Context) sdk.Dec - GetNonNativeAsset(collateralAsset string, borrowAsset string) string + GetTradingAsset(collateralAsset string, borrowAsset string) string GetPool(ctx sdk.Context, poolId uint64) (Pool, bool) IsPoolEnabled(ctx sdk.Context, poolId uint64) bool GetAmmPool(ctx sdk.Context, poolId uint64, nonNativeAsset string) (ammtypes.Pool, error) @@ -60,6 +60,31 @@ type OpenLongChecker interface { GetAmmPoolBalance(ctx sdk.Context, ammPool ammtypes.Pool, assetDenom string) (sdk.Int, error) CheckLongingAssets(ctx sdk.Context, collateralAsset string, borrowAsset string) error CheckSamePosition(ctx sdk.Context, msg *MsgOpen) *MTP + SetMTP(ctx sdk.Context, mtp *MTP) error + CalcMTPConsolidateCollateral(ctx sdk.Context, mtp *MTP) error + CalcMTPConsolidateLiability(ctx sdk.Context, mtp *MTP) +} + +//go:generate mockery --srcpkg . --name OpenShortChecker --structname OpenShortChecker --filename open_short_checker.go --with-expecter +type OpenShortChecker interface { + GetMaxLeverageParam(ctx sdk.Context) sdk.Dec + GetTradingAsset(collateralAsset string, borrowAsset string) string + GetPool(ctx sdk.Context, poolId uint64) (Pool, bool) + IsPoolEnabled(ctx sdk.Context, poolId uint64) bool + GetAmmPool(ctx sdk.Context, poolId uint64, nonNativeAsset string) (ammtypes.Pool, error) + HasSufficientPoolBalance(ctx sdk.Context, ammPool ammtypes.Pool, assetDenom string, requiredAmount sdk.Int) bool + CheckMinLiabilities(ctx sdk.Context, collateralTokenAmt sdk.Coin, eta sdk.Dec, pool Pool, ammPool ammtypes.Pool, borrowAsset string) error + EstimateSwap(ctx sdk.Context, leveragedAmtTokenIn sdk.Coin, borrowAsset string, ammPool ammtypes.Pool) (sdk.Int, error) + EstimateSwapGivenOut(ctx sdk.Context, tokenOutAmount sdk.Coin, tokenInDenom string, ammPool ammtypes.Pool) (sdk.Int, error) + Borrow(ctx sdk.Context, collateralAsset string, custodyAsset string, collateralAmount sdk.Int, custodyAmount sdk.Int, mtp *MTP, ammPool *ammtypes.Pool, pool *Pool, eta sdk.Dec) error + UpdatePoolHealth(ctx sdk.Context, pool *Pool) error + TakeInCustody(ctx sdk.Context, mtp MTP, pool *Pool) error + UpdateMTPHealth(ctx sdk.Context, mtp MTP, ammPool ammtypes.Pool) (sdk.Dec, error) + GetSafetyFactor(ctx sdk.Context) sdk.Dec + SetPool(ctx sdk.Context, pool Pool) + GetAmmPoolBalance(ctx sdk.Context, ammPool ammtypes.Pool, assetDenom string) (sdk.Int, error) + CheckShortAssets(ctx sdk.Context, collateralAsset string, borrowAsset string) error + CheckSamePosition(ctx sdk.Context, msg *MsgOpen) *MTP } //go:generate mockery --srcpkg . --name CloseLongChecker --structname CloseLongChecker --filename close_long_checker.go --with-expecter diff --git a/x/margin/types/mocks/open_checker.go b/x/margin/types/mocks/open_checker.go index fe66b58f8..9b7b0d9ef 100644 --- a/x/margin/types/mocks/open_checker.go +++ b/x/margin/types/mocks/open_checker.go @@ -230,8 +230,8 @@ func (_c *OpenChecker_EmitOpenEvent_Call) RunAndReturn(run func(types.Context, * return _c } -// GetNonNativeAsset provides a mock function with given fields: collateralAsset, borrowAsset -func (_m *OpenChecker) GetNonNativeAsset(collateralAsset string, borrowAsset string) string { +// GetTradingAsset provides a mock function with given fields: collateralAsset, borrowAsset +func (_m *OpenChecker) GetTradingAsset(collateralAsset string, borrowAsset string) string { ret := _m.Called(collateralAsset, borrowAsset) var r0 string @@ -244,31 +244,31 @@ func (_m *OpenChecker) GetNonNativeAsset(collateralAsset string, borrowAsset str return r0 } -// OpenChecker_GetNonNativeAsset_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetNonNativeAsset' -type OpenChecker_GetNonNativeAsset_Call struct { +// OpenChecker_GetTradingAsset_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetTradingAsset' +type OpenChecker_GetTradingAsset_Call struct { *mock.Call } -// GetNonNativeAsset is a helper method to define mock.On call +// GetTradingAsset is a helper method to define mock.On call // - collateralAsset string // - borrowAsset string -func (_e *OpenChecker_Expecter) GetNonNativeAsset(collateralAsset interface{}, borrowAsset interface{}) *OpenChecker_GetNonNativeAsset_Call { - return &OpenChecker_GetNonNativeAsset_Call{Call: _e.mock.On("GetNonNativeAsset", collateralAsset, borrowAsset)} +func (_e *OpenChecker_Expecter) GetTradingAsset(collateralAsset interface{}, borrowAsset interface{}) *OpenChecker_GetTradingAsset_Call { + return &OpenChecker_GetTradingAsset_Call{Call: _e.mock.On("GetTradingAsset", collateralAsset, borrowAsset)} } -func (_c *OpenChecker_GetNonNativeAsset_Call) Run(run func(collateralAsset string, borrowAsset string)) *OpenChecker_GetNonNativeAsset_Call { +func (_c *OpenChecker_GetTradingAsset_Call) Run(run func(collateralAsset string, borrowAsset string)) *OpenChecker_GetTradingAsset_Call { _c.Call.Run(func(args mock.Arguments) { run(args[0].(string), args[1].(string)) }) return _c } -func (_c *OpenChecker_GetNonNativeAsset_Call) Return(_a0 string) *OpenChecker_GetNonNativeAsset_Call { +func (_c *OpenChecker_GetTradingAsset_Call) Return(_a0 string) *OpenChecker_GetTradingAsset_Call { _c.Call.Return(_a0) return _c } -func (_c *OpenChecker_GetNonNativeAsset_Call) RunAndReturn(run func(string, string) string) *OpenChecker_GetNonNativeAsset_Call { +func (_c *OpenChecker_GetTradingAsset_Call) RunAndReturn(run func(string, string) string) *OpenChecker_GetTradingAsset_Call { _c.Call.Return(run) return _c } diff --git a/x/margin/types/mocks/open_long_checker.go b/x/margin/types/mocks/open_long_checker.go index 4027e7fb1..e791f2985 100644 --- a/x/margin/types/mocks/open_long_checker.go +++ b/x/margin/types/mocks/open_long_checker.go @@ -76,6 +76,83 @@ func (_c *OpenLongChecker_Borrow_Call) RunAndReturn(run func(types.Context, stri return _c } +// CalcMTPConsolidateCollateral provides a mock function with given fields: ctx, mtp +func (_m *OpenLongChecker) CalcMTPConsolidateCollateral(ctx types.Context, mtp *margintypes.MTP) error { + ret := _m.Called(ctx, mtp) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MTP) error); ok { + r0 = rf(ctx, mtp) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// OpenLongChecker_CalcMTPConsolidateCollateral_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CalcMTPConsolidateCollateral' +type OpenLongChecker_CalcMTPConsolidateCollateral_Call struct { + *mock.Call +} + +// CalcMTPConsolidateCollateral is a helper method to define mock.On call +// - ctx types.Context +// - mtp *margintypes.MTP +func (_e *OpenLongChecker_Expecter) CalcMTPConsolidateCollateral(ctx interface{}, mtp interface{}) *OpenLongChecker_CalcMTPConsolidateCollateral_Call { + return &OpenLongChecker_CalcMTPConsolidateCollateral_Call{Call: _e.mock.On("CalcMTPConsolidateCollateral", ctx, mtp)} +} + +func (_c *OpenLongChecker_CalcMTPConsolidateCollateral_Call) Run(run func(ctx types.Context, mtp *margintypes.MTP)) *OpenLongChecker_CalcMTPConsolidateCollateral_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(*margintypes.MTP)) + }) + return _c +} + +func (_c *OpenLongChecker_CalcMTPConsolidateCollateral_Call) Return(_a0 error) *OpenLongChecker_CalcMTPConsolidateCollateral_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenLongChecker_CalcMTPConsolidateCollateral_Call) RunAndReturn(run func(types.Context, *margintypes.MTP) error) *OpenLongChecker_CalcMTPConsolidateCollateral_Call { + _c.Call.Return(run) + return _c +} + +// CalcMTPConsolidateLiability provides a mock function with given fields: ctx, mtp +func (_m *OpenLongChecker) CalcMTPConsolidateLiability(ctx types.Context, mtp *margintypes.MTP) { + _m.Called(ctx, mtp) +} + +// OpenLongChecker_CalcMTPConsolidateLiability_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CalcMTPConsolidateLiability' +type OpenLongChecker_CalcMTPConsolidateLiability_Call struct { + *mock.Call +} + +// CalcMTPConsolidateLiability is a helper method to define mock.On call +// - ctx types.Context +// - mtp *margintypes.MTP +func (_e *OpenLongChecker_Expecter) CalcMTPConsolidateLiability(ctx interface{}, mtp interface{}) *OpenLongChecker_CalcMTPConsolidateLiability_Call { + return &OpenLongChecker_CalcMTPConsolidateLiability_Call{Call: _e.mock.On("CalcMTPConsolidateLiability", ctx, mtp)} +} + +func (_c *OpenLongChecker_CalcMTPConsolidateLiability_Call) Run(run func(ctx types.Context, mtp *margintypes.MTP)) *OpenLongChecker_CalcMTPConsolidateLiability_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(*margintypes.MTP)) + }) + return _c +} + +func (_c *OpenLongChecker_CalcMTPConsolidateLiability_Call) Return() *OpenLongChecker_CalcMTPConsolidateLiability_Call { + _c.Call.Return() + return _c +} + +func (_c *OpenLongChecker_CalcMTPConsolidateLiability_Call) RunAndReturn(run func(types.Context, *margintypes.MTP)) *OpenLongChecker_CalcMTPConsolidateLiability_Call { + _c.Call.Return(run) + return _c +} + // CheckLongingAssets provides a mock function with given fields: ctx, collateralAsset, borrowAsset func (_m *OpenLongChecker) CheckLongingAssets(ctx types.Context, collateralAsset string, borrowAsset string) error { ret := _m.Called(ctx, collateralAsset, borrowAsset) @@ -472,49 +549,6 @@ func (_c *OpenLongChecker_GetMaxLeverageParam_Call) RunAndReturn(run func(types. return _c } -// GetNonNativeAsset provides a mock function with given fields: collateralAsset, borrowAsset -func (_m *OpenLongChecker) GetNonNativeAsset(collateralAsset string, borrowAsset string) string { - ret := _m.Called(collateralAsset, borrowAsset) - - var r0 string - if rf, ok := ret.Get(0).(func(string, string) string); ok { - r0 = rf(collateralAsset, borrowAsset) - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// OpenLongChecker_GetNonNativeAsset_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetNonNativeAsset' -type OpenLongChecker_GetNonNativeAsset_Call struct { - *mock.Call -} - -// GetNonNativeAsset is a helper method to define mock.On call -// - collateralAsset string -// - borrowAsset string -func (_e *OpenLongChecker_Expecter) GetNonNativeAsset(collateralAsset interface{}, borrowAsset interface{}) *OpenLongChecker_GetNonNativeAsset_Call { - return &OpenLongChecker_GetNonNativeAsset_Call{Call: _e.mock.On("GetNonNativeAsset", collateralAsset, borrowAsset)} -} - -func (_c *OpenLongChecker_GetNonNativeAsset_Call) Run(run func(collateralAsset string, borrowAsset string)) *OpenLongChecker_GetNonNativeAsset_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string), args[1].(string)) - }) - return _c -} - -func (_c *OpenLongChecker_GetNonNativeAsset_Call) Return(_a0 string) *OpenLongChecker_GetNonNativeAsset_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *OpenLongChecker_GetNonNativeAsset_Call) RunAndReturn(run func(string, string) string) *OpenLongChecker_GetNonNativeAsset_Call { - _c.Call.Return(run) - return _c -} - // GetPool provides a mock function with given fields: ctx, poolId func (_m *OpenLongChecker) GetPool(ctx types.Context, poolId uint64) (margintypes.Pool, bool) { ret := _m.Called(ctx, poolId) @@ -610,6 +644,49 @@ func (_c *OpenLongChecker_GetSafetyFactor_Call) RunAndReturn(run func(types.Cont return _c } +// GetTradingAsset provides a mock function with given fields: collateralAsset, borrowAsset +func (_m *OpenLongChecker) GetTradingAsset(collateralAsset string, borrowAsset string) string { + ret := _m.Called(collateralAsset, borrowAsset) + + var r0 string + if rf, ok := ret.Get(0).(func(string, string) string); ok { + r0 = rf(collateralAsset, borrowAsset) + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// OpenLongChecker_GetTradingAsset_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetTradingAsset' +type OpenLongChecker_GetTradingAsset_Call struct { + *mock.Call +} + +// GetTradingAsset is a helper method to define mock.On call +// - collateralAsset string +// - borrowAsset string +func (_e *OpenLongChecker_Expecter) GetTradingAsset(collateralAsset interface{}, borrowAsset interface{}) *OpenLongChecker_GetTradingAsset_Call { + return &OpenLongChecker_GetTradingAsset_Call{Call: _e.mock.On("GetTradingAsset", collateralAsset, borrowAsset)} +} + +func (_c *OpenLongChecker_GetTradingAsset_Call) Run(run func(collateralAsset string, borrowAsset string)) *OpenLongChecker_GetTradingAsset_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string), args[1].(string)) + }) + return _c +} + +func (_c *OpenLongChecker_GetTradingAsset_Call) Return(_a0 string) *OpenLongChecker_GetTradingAsset_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenLongChecker_GetTradingAsset_Call) RunAndReturn(run func(string, string) string) *OpenLongChecker_GetTradingAsset_Call { + _c.Call.Return(run) + return _c +} + // HasSufficientPoolBalance provides a mock function with given fields: ctx, ammPool, assetDenom, requiredAmount func (_m *OpenLongChecker) HasSufficientPoolBalance(ctx types.Context, ammPool ammtypes.Pool, assetDenom string, requiredAmount math.Int) bool { ret := _m.Called(ctx, ammPool, assetDenom, requiredAmount) @@ -698,6 +775,49 @@ func (_c *OpenLongChecker_IsPoolEnabled_Call) RunAndReturn(run func(types.Contex return _c } +// SetMTP provides a mock function with given fields: ctx, mtp +func (_m *OpenLongChecker) SetMTP(ctx types.Context, mtp *margintypes.MTP) error { + ret := _m.Called(ctx, mtp) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MTP) error); ok { + r0 = rf(ctx, mtp) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// OpenLongChecker_SetMTP_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SetMTP' +type OpenLongChecker_SetMTP_Call struct { + *mock.Call +} + +// SetMTP is a helper method to define mock.On call +// - ctx types.Context +// - mtp *margintypes.MTP +func (_e *OpenLongChecker_Expecter) SetMTP(ctx interface{}, mtp interface{}) *OpenLongChecker_SetMTP_Call { + return &OpenLongChecker_SetMTP_Call{Call: _e.mock.On("SetMTP", ctx, mtp)} +} + +func (_c *OpenLongChecker_SetMTP_Call) Run(run func(ctx types.Context, mtp *margintypes.MTP)) *OpenLongChecker_SetMTP_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(*margintypes.MTP)) + }) + return _c +} + +func (_c *OpenLongChecker_SetMTP_Call) Return(_a0 error) *OpenLongChecker_SetMTP_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenLongChecker_SetMTP_Call) RunAndReturn(run func(types.Context, *margintypes.MTP) error) *OpenLongChecker_SetMTP_Call { + _c.Call.Return(run) + return _c +} + // SetPool provides a mock function with given fields: ctx, pool func (_m *OpenLongChecker) SetPool(ctx types.Context, pool margintypes.Pool) { _m.Called(ctx, pool) diff --git a/x/margin/types/mocks/open_short_checker.go b/x/margin/types/mocks/open_short_checker.go new file mode 100644 index 000000000..dd5419cfc --- /dev/null +++ b/x/margin/types/mocks/open_short_checker.go @@ -0,0 +1,888 @@ +// Code generated by mockery v2.32.4. DO NOT EDIT. + +package mocks + +import ( + ammtypes "github.com/elys-network/elys/x/amm/types" + margintypes "github.com/elys-network/elys/x/margin/types" + + math "cosmossdk.io/math" + + mock "github.com/stretchr/testify/mock" + + types "github.com/cosmos/cosmos-sdk/types" +) + +// OpenShortChecker is an autogenerated mock type for the OpenShortChecker type +type OpenShortChecker struct { + mock.Mock +} + +type OpenShortChecker_Expecter struct { + mock *mock.Mock +} + +func (_m *OpenShortChecker) EXPECT() *OpenShortChecker_Expecter { + return &OpenShortChecker_Expecter{mock: &_m.Mock} +} + +// Borrow provides a mock function with given fields: ctx, collateralAsset, custodyAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta +func (_m *OpenShortChecker) Borrow(ctx types.Context, collateralAsset string, custodyAsset string, collateralAmount math.Int, custodyAmount math.Int, mtp *margintypes.MTP, ammPool *ammtypes.Pool, pool *margintypes.Pool, eta math.LegacyDec) error { + ret := _m.Called(ctx, collateralAsset, custodyAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, string, string, math.Int, math.Int, *margintypes.MTP, *ammtypes.Pool, *margintypes.Pool, math.LegacyDec) error); ok { + r0 = rf(ctx, collateralAsset, custodyAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// OpenShortChecker_Borrow_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Borrow' +type OpenShortChecker_Borrow_Call struct { + *mock.Call +} + +// Borrow is a helper method to define mock.On call +// - ctx types.Context +// - collateralAsset string +// - custodyAsset string +// - collateralAmount math.Int +// - custodyAmount math.Int +// - mtp *margintypes.MTP +// - ammPool *ammtypes.Pool +// - pool *margintypes.Pool +// - eta math.LegacyDec +func (_e *OpenShortChecker_Expecter) Borrow(ctx interface{}, collateralAsset interface{}, custodyAsset interface{}, collateralAmount interface{}, custodyAmount interface{}, mtp interface{}, ammPool interface{}, pool interface{}, eta interface{}) *OpenShortChecker_Borrow_Call { + return &OpenShortChecker_Borrow_Call{Call: _e.mock.On("Borrow", ctx, collateralAsset, custodyAsset, collateralAmount, custodyAmount, mtp, ammPool, pool, eta)} +} + +func (_c *OpenShortChecker_Borrow_Call) Run(run func(ctx types.Context, collateralAsset string, custodyAsset string, collateralAmount math.Int, custodyAmount math.Int, mtp *margintypes.MTP, ammPool *ammtypes.Pool, pool *margintypes.Pool, eta math.LegacyDec)) *OpenShortChecker_Borrow_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(string), args[2].(string), args[3].(math.Int), args[4].(math.Int), args[5].(*margintypes.MTP), args[6].(*ammtypes.Pool), args[7].(*margintypes.Pool), args[8].(math.LegacyDec)) + }) + return _c +} + +func (_c *OpenShortChecker_Borrow_Call) Return(_a0 error) *OpenShortChecker_Borrow_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_Borrow_Call) RunAndReturn(run func(types.Context, string, string, math.Int, math.Int, *margintypes.MTP, *ammtypes.Pool, *margintypes.Pool, math.LegacyDec) error) *OpenShortChecker_Borrow_Call { + _c.Call.Return(run) + return _c +} + +// CheckMinLiabilities provides a mock function with given fields: ctx, collateralTokenAmt, eta, pool, ammPool, borrowAsset +func (_m *OpenShortChecker) CheckMinLiabilities(ctx types.Context, collateralTokenAmt types.Coin, eta math.LegacyDec, pool margintypes.Pool, ammPool ammtypes.Pool, borrowAsset string) error { + ret := _m.Called(ctx, collateralTokenAmt, eta, pool, ammPool, borrowAsset) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, types.Coin, math.LegacyDec, margintypes.Pool, ammtypes.Pool, string) error); ok { + r0 = rf(ctx, collateralTokenAmt, eta, pool, ammPool, borrowAsset) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// OpenShortChecker_CheckMinLiabilities_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CheckMinLiabilities' +type OpenShortChecker_CheckMinLiabilities_Call struct { + *mock.Call +} + +// CheckMinLiabilities is a helper method to define mock.On call +// - ctx types.Context +// - collateralTokenAmt types.Coin +// - eta math.LegacyDec +// - pool margintypes.Pool +// - ammPool ammtypes.Pool +// - borrowAsset string +func (_e *OpenShortChecker_Expecter) CheckMinLiabilities(ctx interface{}, collateralTokenAmt interface{}, eta interface{}, pool interface{}, ammPool interface{}, borrowAsset interface{}) *OpenShortChecker_CheckMinLiabilities_Call { + return &OpenShortChecker_CheckMinLiabilities_Call{Call: _e.mock.On("CheckMinLiabilities", ctx, collateralTokenAmt, eta, pool, ammPool, borrowAsset)} +} + +func (_c *OpenShortChecker_CheckMinLiabilities_Call) Run(run func(ctx types.Context, collateralTokenAmt types.Coin, eta math.LegacyDec, pool margintypes.Pool, ammPool ammtypes.Pool, borrowAsset string)) *OpenShortChecker_CheckMinLiabilities_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(types.Coin), args[2].(math.LegacyDec), args[3].(margintypes.Pool), args[4].(ammtypes.Pool), args[5].(string)) + }) + return _c +} + +func (_c *OpenShortChecker_CheckMinLiabilities_Call) Return(_a0 error) *OpenShortChecker_CheckMinLiabilities_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_CheckMinLiabilities_Call) RunAndReturn(run func(types.Context, types.Coin, math.LegacyDec, margintypes.Pool, ammtypes.Pool, string) error) *OpenShortChecker_CheckMinLiabilities_Call { + _c.Call.Return(run) + return _c +} + +// CheckSamePosition provides a mock function with given fields: ctx, msg +func (_m *OpenShortChecker) CheckSamePosition(ctx types.Context, msg *margintypes.MsgOpen) *margintypes.MTP { + ret := _m.Called(ctx, msg) + + var r0 *margintypes.MTP + if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MsgOpen) *margintypes.MTP); ok { + r0 = rf(ctx, msg) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*margintypes.MTP) + } + } + + return r0 +} + +// OpenShortChecker_CheckSamePosition_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CheckSamePosition' +type OpenShortChecker_CheckSamePosition_Call struct { + *mock.Call +} + +// CheckSamePosition is a helper method to define mock.On call +// - ctx types.Context +// - msg *margintypes.MsgOpen +func (_e *OpenShortChecker_Expecter) CheckSamePosition(ctx interface{}, msg interface{}) *OpenShortChecker_CheckSamePosition_Call { + return &OpenShortChecker_CheckSamePosition_Call{Call: _e.mock.On("CheckSamePosition", ctx, msg)} +} + +func (_c *OpenShortChecker_CheckSamePosition_Call) Run(run func(ctx types.Context, msg *margintypes.MsgOpen)) *OpenShortChecker_CheckSamePosition_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(*margintypes.MsgOpen)) + }) + return _c +} + +func (_c *OpenShortChecker_CheckSamePosition_Call) Return(_a0 *margintypes.MTP) *OpenShortChecker_CheckSamePosition_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_CheckSamePosition_Call) RunAndReturn(run func(types.Context, *margintypes.MsgOpen) *margintypes.MTP) *OpenShortChecker_CheckSamePosition_Call { + _c.Call.Return(run) + return _c +} + +// CheckShortAssets provides a mock function with given fields: ctx, collateralAsset, borrowAsset +func (_m *OpenShortChecker) CheckShortAssets(ctx types.Context, collateralAsset string, borrowAsset string) error { + ret := _m.Called(ctx, collateralAsset, borrowAsset) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, string, string) error); ok { + r0 = rf(ctx, collateralAsset, borrowAsset) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// OpenShortChecker_CheckShortAssets_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CheckShortAssets' +type OpenShortChecker_CheckShortAssets_Call struct { + *mock.Call +} + +// CheckShortAssets is a helper method to define mock.On call +// - ctx types.Context +// - collateralAsset string +// - borrowAsset string +func (_e *OpenShortChecker_Expecter) CheckShortAssets(ctx interface{}, collateralAsset interface{}, borrowAsset interface{}) *OpenShortChecker_CheckShortAssets_Call { + return &OpenShortChecker_CheckShortAssets_Call{Call: _e.mock.On("CheckShortAssets", ctx, collateralAsset, borrowAsset)} +} + +func (_c *OpenShortChecker_CheckShortAssets_Call) Run(run func(ctx types.Context, collateralAsset string, borrowAsset string)) *OpenShortChecker_CheckShortAssets_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(string), args[2].(string)) + }) + return _c +} + +func (_c *OpenShortChecker_CheckShortAssets_Call) Return(_a0 error) *OpenShortChecker_CheckShortAssets_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_CheckShortAssets_Call) RunAndReturn(run func(types.Context, string, string) error) *OpenShortChecker_CheckShortAssets_Call { + _c.Call.Return(run) + return _c +} + +// EstimateSwap provides a mock function with given fields: ctx, leveragedAmtTokenIn, borrowAsset, ammPool +func (_m *OpenShortChecker) EstimateSwap(ctx types.Context, leveragedAmtTokenIn types.Coin, borrowAsset string, ammPool ammtypes.Pool) (math.Int, error) { + ret := _m.Called(ctx, leveragedAmtTokenIn, borrowAsset, ammPool) + + var r0 math.Int + var r1 error + if rf, ok := ret.Get(0).(func(types.Context, types.Coin, string, ammtypes.Pool) (math.Int, error)); ok { + return rf(ctx, leveragedAmtTokenIn, borrowAsset, ammPool) + } + if rf, ok := ret.Get(0).(func(types.Context, types.Coin, string, ammtypes.Pool) math.Int); ok { + r0 = rf(ctx, leveragedAmtTokenIn, borrowAsset, ammPool) + } else { + r0 = ret.Get(0).(math.Int) + } + + if rf, ok := ret.Get(1).(func(types.Context, types.Coin, string, ammtypes.Pool) error); ok { + r1 = rf(ctx, leveragedAmtTokenIn, borrowAsset, ammPool) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// OpenShortChecker_EstimateSwap_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'EstimateSwap' +type OpenShortChecker_EstimateSwap_Call struct { + *mock.Call +} + +// EstimateSwap is a helper method to define mock.On call +// - ctx types.Context +// - leveragedAmtTokenIn types.Coin +// - borrowAsset string +// - ammPool ammtypes.Pool +func (_e *OpenShortChecker_Expecter) EstimateSwap(ctx interface{}, leveragedAmtTokenIn interface{}, borrowAsset interface{}, ammPool interface{}) *OpenShortChecker_EstimateSwap_Call { + return &OpenShortChecker_EstimateSwap_Call{Call: _e.mock.On("EstimateSwap", ctx, leveragedAmtTokenIn, borrowAsset, ammPool)} +} + +func (_c *OpenShortChecker_EstimateSwap_Call) Run(run func(ctx types.Context, leveragedAmtTokenIn types.Coin, borrowAsset string, ammPool ammtypes.Pool)) *OpenShortChecker_EstimateSwap_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(types.Coin), args[2].(string), args[3].(ammtypes.Pool)) + }) + return _c +} + +func (_c *OpenShortChecker_EstimateSwap_Call) Return(_a0 math.Int, _a1 error) *OpenShortChecker_EstimateSwap_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *OpenShortChecker_EstimateSwap_Call) RunAndReturn(run func(types.Context, types.Coin, string, ammtypes.Pool) (math.Int, error)) *OpenShortChecker_EstimateSwap_Call { + _c.Call.Return(run) + return _c +} + +// EstimateSwapGivenOut provides a mock function with given fields: ctx, tokenOutAmount, tokenInDenom, ammPool +func (_m *OpenShortChecker) EstimateSwapGivenOut(ctx types.Context, tokenOutAmount types.Coin, tokenInDenom string, ammPool ammtypes.Pool) (math.Int, error) { + ret := _m.Called(ctx, tokenOutAmount, tokenInDenom, ammPool) + + var r0 math.Int + var r1 error + if rf, ok := ret.Get(0).(func(types.Context, types.Coin, string, ammtypes.Pool) (math.Int, error)); ok { + return rf(ctx, tokenOutAmount, tokenInDenom, ammPool) + } + if rf, ok := ret.Get(0).(func(types.Context, types.Coin, string, ammtypes.Pool) math.Int); ok { + r0 = rf(ctx, tokenOutAmount, tokenInDenom, ammPool) + } else { + r0 = ret.Get(0).(math.Int) + } + + if rf, ok := ret.Get(1).(func(types.Context, types.Coin, string, ammtypes.Pool) error); ok { + r1 = rf(ctx, tokenOutAmount, tokenInDenom, ammPool) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// OpenShortChecker_EstimateSwapGivenOut_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'EstimateSwapGivenOut' +type OpenShortChecker_EstimateSwapGivenOut_Call struct { + *mock.Call +} + +// EstimateSwapGivenOut is a helper method to define mock.On call +// - ctx types.Context +// - tokenOutAmount types.Coin +// - tokenInDenom string +// - ammPool ammtypes.Pool +func (_e *OpenShortChecker_Expecter) EstimateSwapGivenOut(ctx interface{}, tokenOutAmount interface{}, tokenInDenom interface{}, ammPool interface{}) *OpenShortChecker_EstimateSwapGivenOut_Call { + return &OpenShortChecker_EstimateSwapGivenOut_Call{Call: _e.mock.On("EstimateSwapGivenOut", ctx, tokenOutAmount, tokenInDenom, ammPool)} +} + +func (_c *OpenShortChecker_EstimateSwapGivenOut_Call) Run(run func(ctx types.Context, tokenOutAmount types.Coin, tokenInDenom string, ammPool ammtypes.Pool)) *OpenShortChecker_EstimateSwapGivenOut_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(types.Coin), args[2].(string), args[3].(ammtypes.Pool)) + }) + return _c +} + +func (_c *OpenShortChecker_EstimateSwapGivenOut_Call) Return(_a0 math.Int, _a1 error) *OpenShortChecker_EstimateSwapGivenOut_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *OpenShortChecker_EstimateSwapGivenOut_Call) RunAndReturn(run func(types.Context, types.Coin, string, ammtypes.Pool) (math.Int, error)) *OpenShortChecker_EstimateSwapGivenOut_Call { + _c.Call.Return(run) + return _c +} + +// GetAmmPool provides a mock function with given fields: ctx, poolId, nonNativeAsset +func (_m *OpenShortChecker) GetAmmPool(ctx types.Context, poolId uint64, nonNativeAsset string) (ammtypes.Pool, error) { + ret := _m.Called(ctx, poolId, nonNativeAsset) + + var r0 ammtypes.Pool + var r1 error + if rf, ok := ret.Get(0).(func(types.Context, uint64, string) (ammtypes.Pool, error)); ok { + return rf(ctx, poolId, nonNativeAsset) + } + if rf, ok := ret.Get(0).(func(types.Context, uint64, string) ammtypes.Pool); ok { + r0 = rf(ctx, poolId, nonNativeAsset) + } else { + r0 = ret.Get(0).(ammtypes.Pool) + } + + if rf, ok := ret.Get(1).(func(types.Context, uint64, string) error); ok { + r1 = rf(ctx, poolId, nonNativeAsset) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// OpenShortChecker_GetAmmPool_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetAmmPool' +type OpenShortChecker_GetAmmPool_Call struct { + *mock.Call +} + +// GetAmmPool is a helper method to define mock.On call +// - ctx types.Context +// - poolId uint64 +// - nonNativeAsset string +func (_e *OpenShortChecker_Expecter) GetAmmPool(ctx interface{}, poolId interface{}, nonNativeAsset interface{}) *OpenShortChecker_GetAmmPool_Call { + return &OpenShortChecker_GetAmmPool_Call{Call: _e.mock.On("GetAmmPool", ctx, poolId, nonNativeAsset)} +} + +func (_c *OpenShortChecker_GetAmmPool_Call) Run(run func(ctx types.Context, poolId uint64, nonNativeAsset string)) *OpenShortChecker_GetAmmPool_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(uint64), args[2].(string)) + }) + return _c +} + +func (_c *OpenShortChecker_GetAmmPool_Call) Return(_a0 ammtypes.Pool, _a1 error) *OpenShortChecker_GetAmmPool_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *OpenShortChecker_GetAmmPool_Call) RunAndReturn(run func(types.Context, uint64, string) (ammtypes.Pool, error)) *OpenShortChecker_GetAmmPool_Call { + _c.Call.Return(run) + return _c +} + +// GetAmmPoolBalance provides a mock function with given fields: ctx, ammPool, assetDenom +func (_m *OpenShortChecker) GetAmmPoolBalance(ctx types.Context, ammPool ammtypes.Pool, assetDenom string) (math.Int, error) { + ret := _m.Called(ctx, ammPool, assetDenom) + + var r0 math.Int + var r1 error + if rf, ok := ret.Get(0).(func(types.Context, ammtypes.Pool, string) (math.Int, error)); ok { + return rf(ctx, ammPool, assetDenom) + } + if rf, ok := ret.Get(0).(func(types.Context, ammtypes.Pool, string) math.Int); ok { + r0 = rf(ctx, ammPool, assetDenom) + } else { + r0 = ret.Get(0).(math.Int) + } + + if rf, ok := ret.Get(1).(func(types.Context, ammtypes.Pool, string) error); ok { + r1 = rf(ctx, ammPool, assetDenom) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// OpenShortChecker_GetAmmPoolBalance_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetAmmPoolBalance' +type OpenShortChecker_GetAmmPoolBalance_Call struct { + *mock.Call +} + +// GetAmmPoolBalance is a helper method to define mock.On call +// - ctx types.Context +// - ammPool ammtypes.Pool +// - assetDenom string +func (_e *OpenShortChecker_Expecter) GetAmmPoolBalance(ctx interface{}, ammPool interface{}, assetDenom interface{}) *OpenShortChecker_GetAmmPoolBalance_Call { + return &OpenShortChecker_GetAmmPoolBalance_Call{Call: _e.mock.On("GetAmmPoolBalance", ctx, ammPool, assetDenom)} +} + +func (_c *OpenShortChecker_GetAmmPoolBalance_Call) Run(run func(ctx types.Context, ammPool ammtypes.Pool, assetDenom string)) *OpenShortChecker_GetAmmPoolBalance_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(ammtypes.Pool), args[2].(string)) + }) + return _c +} + +func (_c *OpenShortChecker_GetAmmPoolBalance_Call) Return(_a0 math.Int, _a1 error) *OpenShortChecker_GetAmmPoolBalance_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *OpenShortChecker_GetAmmPoolBalance_Call) RunAndReturn(run func(types.Context, ammtypes.Pool, string) (math.Int, error)) *OpenShortChecker_GetAmmPoolBalance_Call { + _c.Call.Return(run) + return _c +} + +// GetMaxLeverageParam provides a mock function with given fields: ctx +func (_m *OpenShortChecker) GetMaxLeverageParam(ctx types.Context) math.LegacyDec { + ret := _m.Called(ctx) + + var r0 math.LegacyDec + if rf, ok := ret.Get(0).(func(types.Context) math.LegacyDec); ok { + r0 = rf(ctx) + } else { + r0 = ret.Get(0).(math.LegacyDec) + } + + return r0 +} + +// OpenShortChecker_GetMaxLeverageParam_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetMaxLeverageParam' +type OpenShortChecker_GetMaxLeverageParam_Call struct { + *mock.Call +} + +// GetMaxLeverageParam is a helper method to define mock.On call +// - ctx types.Context +func (_e *OpenShortChecker_Expecter) GetMaxLeverageParam(ctx interface{}) *OpenShortChecker_GetMaxLeverageParam_Call { + return &OpenShortChecker_GetMaxLeverageParam_Call{Call: _e.mock.On("GetMaxLeverageParam", ctx)} +} + +func (_c *OpenShortChecker_GetMaxLeverageParam_Call) Run(run func(ctx types.Context)) *OpenShortChecker_GetMaxLeverageParam_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context)) + }) + return _c +} + +func (_c *OpenShortChecker_GetMaxLeverageParam_Call) Return(_a0 math.LegacyDec) *OpenShortChecker_GetMaxLeverageParam_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_GetMaxLeverageParam_Call) RunAndReturn(run func(types.Context) math.LegacyDec) *OpenShortChecker_GetMaxLeverageParam_Call { + _c.Call.Return(run) + return _c +} + +// GetPool provides a mock function with given fields: ctx, poolId +func (_m *OpenShortChecker) GetPool(ctx types.Context, poolId uint64) (margintypes.Pool, bool) { + ret := _m.Called(ctx, poolId) + + var r0 margintypes.Pool + var r1 bool + if rf, ok := ret.Get(0).(func(types.Context, uint64) (margintypes.Pool, bool)); ok { + return rf(ctx, poolId) + } + if rf, ok := ret.Get(0).(func(types.Context, uint64) margintypes.Pool); ok { + r0 = rf(ctx, poolId) + } else { + r0 = ret.Get(0).(margintypes.Pool) + } + + if rf, ok := ret.Get(1).(func(types.Context, uint64) bool); ok { + r1 = rf(ctx, poolId) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// OpenShortChecker_GetPool_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetPool' +type OpenShortChecker_GetPool_Call struct { + *mock.Call +} + +// GetPool is a helper method to define mock.On call +// - ctx types.Context +// - poolId uint64 +func (_e *OpenShortChecker_Expecter) GetPool(ctx interface{}, poolId interface{}) *OpenShortChecker_GetPool_Call { + return &OpenShortChecker_GetPool_Call{Call: _e.mock.On("GetPool", ctx, poolId)} +} + +func (_c *OpenShortChecker_GetPool_Call) Run(run func(ctx types.Context, poolId uint64)) *OpenShortChecker_GetPool_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(uint64)) + }) + return _c +} + +func (_c *OpenShortChecker_GetPool_Call) Return(_a0 margintypes.Pool, _a1 bool) *OpenShortChecker_GetPool_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *OpenShortChecker_GetPool_Call) RunAndReturn(run func(types.Context, uint64) (margintypes.Pool, bool)) *OpenShortChecker_GetPool_Call { + _c.Call.Return(run) + return _c +} + +// GetSafetyFactor provides a mock function with given fields: ctx +func (_m *OpenShortChecker) GetSafetyFactor(ctx types.Context) math.LegacyDec { + ret := _m.Called(ctx) + + var r0 math.LegacyDec + if rf, ok := ret.Get(0).(func(types.Context) math.LegacyDec); ok { + r0 = rf(ctx) + } else { + r0 = ret.Get(0).(math.LegacyDec) + } + + return r0 +} + +// OpenShortChecker_GetSafetyFactor_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetSafetyFactor' +type OpenShortChecker_GetSafetyFactor_Call struct { + *mock.Call +} + +// GetSafetyFactor is a helper method to define mock.On call +// - ctx types.Context +func (_e *OpenShortChecker_Expecter) GetSafetyFactor(ctx interface{}) *OpenShortChecker_GetSafetyFactor_Call { + return &OpenShortChecker_GetSafetyFactor_Call{Call: _e.mock.On("GetSafetyFactor", ctx)} +} + +func (_c *OpenShortChecker_GetSafetyFactor_Call) Run(run func(ctx types.Context)) *OpenShortChecker_GetSafetyFactor_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context)) + }) + return _c +} + +func (_c *OpenShortChecker_GetSafetyFactor_Call) Return(_a0 math.LegacyDec) *OpenShortChecker_GetSafetyFactor_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_GetSafetyFactor_Call) RunAndReturn(run func(types.Context) math.LegacyDec) *OpenShortChecker_GetSafetyFactor_Call { + _c.Call.Return(run) + return _c +} + +// GetTradingAsset provides a mock function with given fields: collateralAsset, borrowAsset +func (_m *OpenShortChecker) GetTradingAsset(collateralAsset string, borrowAsset string) string { + ret := _m.Called(collateralAsset, borrowAsset) + + var r0 string + if rf, ok := ret.Get(0).(func(string, string) string); ok { + r0 = rf(collateralAsset, borrowAsset) + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// OpenShortChecker_GetTradingAsset_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetTradingAsset' +type OpenShortChecker_GetTradingAsset_Call struct { + *mock.Call +} + +// GetTradingAsset is a helper method to define mock.On call +// - collateralAsset string +// - borrowAsset string +func (_e *OpenShortChecker_Expecter) GetTradingAsset(collateralAsset interface{}, borrowAsset interface{}) *OpenShortChecker_GetTradingAsset_Call { + return &OpenShortChecker_GetTradingAsset_Call{Call: _e.mock.On("GetTradingAsset", collateralAsset, borrowAsset)} +} + +func (_c *OpenShortChecker_GetTradingAsset_Call) Run(run func(collateralAsset string, borrowAsset string)) *OpenShortChecker_GetTradingAsset_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string), args[1].(string)) + }) + return _c +} + +func (_c *OpenShortChecker_GetTradingAsset_Call) Return(_a0 string) *OpenShortChecker_GetTradingAsset_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_GetTradingAsset_Call) RunAndReturn(run func(string, string) string) *OpenShortChecker_GetTradingAsset_Call { + _c.Call.Return(run) + return _c +} + +// HasSufficientPoolBalance provides a mock function with given fields: ctx, ammPool, assetDenom, requiredAmount +func (_m *OpenShortChecker) HasSufficientPoolBalance(ctx types.Context, ammPool ammtypes.Pool, assetDenom string, requiredAmount math.Int) bool { + ret := _m.Called(ctx, ammPool, assetDenom, requiredAmount) + + var r0 bool + if rf, ok := ret.Get(0).(func(types.Context, ammtypes.Pool, string, math.Int) bool); ok { + r0 = rf(ctx, ammPool, assetDenom, requiredAmount) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// OpenShortChecker_HasSufficientPoolBalance_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HasSufficientPoolBalance' +type OpenShortChecker_HasSufficientPoolBalance_Call struct { + *mock.Call +} + +// HasSufficientPoolBalance is a helper method to define mock.On call +// - ctx types.Context +// - ammPool ammtypes.Pool +// - assetDenom string +// - requiredAmount math.Int +func (_e *OpenShortChecker_Expecter) HasSufficientPoolBalance(ctx interface{}, ammPool interface{}, assetDenom interface{}, requiredAmount interface{}) *OpenShortChecker_HasSufficientPoolBalance_Call { + return &OpenShortChecker_HasSufficientPoolBalance_Call{Call: _e.mock.On("HasSufficientPoolBalance", ctx, ammPool, assetDenom, requiredAmount)} +} + +func (_c *OpenShortChecker_HasSufficientPoolBalance_Call) Run(run func(ctx types.Context, ammPool ammtypes.Pool, assetDenom string, requiredAmount math.Int)) *OpenShortChecker_HasSufficientPoolBalance_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(ammtypes.Pool), args[2].(string), args[3].(math.Int)) + }) + return _c +} + +func (_c *OpenShortChecker_HasSufficientPoolBalance_Call) Return(_a0 bool) *OpenShortChecker_HasSufficientPoolBalance_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_HasSufficientPoolBalance_Call) RunAndReturn(run func(types.Context, ammtypes.Pool, string, math.Int) bool) *OpenShortChecker_HasSufficientPoolBalance_Call { + _c.Call.Return(run) + return _c +} + +// IsPoolEnabled provides a mock function with given fields: ctx, poolId +func (_m *OpenShortChecker) IsPoolEnabled(ctx types.Context, poolId uint64) bool { + ret := _m.Called(ctx, poolId) + + var r0 bool + if rf, ok := ret.Get(0).(func(types.Context, uint64) bool); ok { + r0 = rf(ctx, poolId) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// OpenShortChecker_IsPoolEnabled_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsPoolEnabled' +type OpenShortChecker_IsPoolEnabled_Call struct { + *mock.Call +} + +// IsPoolEnabled is a helper method to define mock.On call +// - ctx types.Context +// - poolId uint64 +func (_e *OpenShortChecker_Expecter) IsPoolEnabled(ctx interface{}, poolId interface{}) *OpenShortChecker_IsPoolEnabled_Call { + return &OpenShortChecker_IsPoolEnabled_Call{Call: _e.mock.On("IsPoolEnabled", ctx, poolId)} +} + +func (_c *OpenShortChecker_IsPoolEnabled_Call) Run(run func(ctx types.Context, poolId uint64)) *OpenShortChecker_IsPoolEnabled_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(uint64)) + }) + return _c +} + +func (_c *OpenShortChecker_IsPoolEnabled_Call) Return(_a0 bool) *OpenShortChecker_IsPoolEnabled_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_IsPoolEnabled_Call) RunAndReturn(run func(types.Context, uint64) bool) *OpenShortChecker_IsPoolEnabled_Call { + _c.Call.Return(run) + return _c +} + +// SetPool provides a mock function with given fields: ctx, pool +func (_m *OpenShortChecker) SetPool(ctx types.Context, pool margintypes.Pool) { + _m.Called(ctx, pool) +} + +// OpenShortChecker_SetPool_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SetPool' +type OpenShortChecker_SetPool_Call struct { + *mock.Call +} + +// SetPool is a helper method to define mock.On call +// - ctx types.Context +// - pool margintypes.Pool +func (_e *OpenShortChecker_Expecter) SetPool(ctx interface{}, pool interface{}) *OpenShortChecker_SetPool_Call { + return &OpenShortChecker_SetPool_Call{Call: _e.mock.On("SetPool", ctx, pool)} +} + +func (_c *OpenShortChecker_SetPool_Call) Run(run func(ctx types.Context, pool margintypes.Pool)) *OpenShortChecker_SetPool_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(margintypes.Pool)) + }) + return _c +} + +func (_c *OpenShortChecker_SetPool_Call) Return() *OpenShortChecker_SetPool_Call { + _c.Call.Return() + return _c +} + +func (_c *OpenShortChecker_SetPool_Call) RunAndReturn(run func(types.Context, margintypes.Pool)) *OpenShortChecker_SetPool_Call { + _c.Call.Return(run) + return _c +} + +// TakeInCustody provides a mock function with given fields: ctx, mtp, pool +func (_m *OpenShortChecker) TakeInCustody(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool) error { + ret := _m.Called(ctx, mtp, pool) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, *margintypes.Pool) error); ok { + r0 = rf(ctx, mtp, pool) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// OpenShortChecker_TakeInCustody_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'TakeInCustody' +type OpenShortChecker_TakeInCustody_Call struct { + *mock.Call +} + +// TakeInCustody is a helper method to define mock.On call +// - ctx types.Context +// - mtp margintypes.MTP +// - pool *margintypes.Pool +func (_e *OpenShortChecker_Expecter) TakeInCustody(ctx interface{}, mtp interface{}, pool interface{}) *OpenShortChecker_TakeInCustody_Call { + return &OpenShortChecker_TakeInCustody_Call{Call: _e.mock.On("TakeInCustody", ctx, mtp, pool)} +} + +func (_c *OpenShortChecker_TakeInCustody_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, pool *margintypes.Pool)) *OpenShortChecker_TakeInCustody_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(*margintypes.Pool)) + }) + return _c +} + +func (_c *OpenShortChecker_TakeInCustody_Call) Return(_a0 error) *OpenShortChecker_TakeInCustody_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_TakeInCustody_Call) RunAndReturn(run func(types.Context, margintypes.MTP, *margintypes.Pool) error) *OpenShortChecker_TakeInCustody_Call { + _c.Call.Return(run) + return _c +} + +// UpdateMTPHealth provides a mock function with given fields: ctx, mtp, ammPool +func (_m *OpenShortChecker) UpdateMTPHealth(ctx types.Context, mtp margintypes.MTP, ammPool ammtypes.Pool) (math.LegacyDec, error) { + ret := _m.Called(ctx, mtp, ammPool) + + var r0 math.LegacyDec + var r1 error + if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, ammtypes.Pool) (math.LegacyDec, error)); ok { + return rf(ctx, mtp, ammPool) + } + if rf, ok := ret.Get(0).(func(types.Context, margintypes.MTP, ammtypes.Pool) math.LegacyDec); ok { + r0 = rf(ctx, mtp, ammPool) + } else { + r0 = ret.Get(0).(math.LegacyDec) + } + + if rf, ok := ret.Get(1).(func(types.Context, margintypes.MTP, ammtypes.Pool) error); ok { + r1 = rf(ctx, mtp, ammPool) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// OpenShortChecker_UpdateMTPHealth_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdateMTPHealth' +type OpenShortChecker_UpdateMTPHealth_Call struct { + *mock.Call +} + +// UpdateMTPHealth is a helper method to define mock.On call +// - ctx types.Context +// - mtp margintypes.MTP +// - ammPool ammtypes.Pool +func (_e *OpenShortChecker_Expecter) UpdateMTPHealth(ctx interface{}, mtp interface{}, ammPool interface{}) *OpenShortChecker_UpdateMTPHealth_Call { + return &OpenShortChecker_UpdateMTPHealth_Call{Call: _e.mock.On("UpdateMTPHealth", ctx, mtp, ammPool)} +} + +func (_c *OpenShortChecker_UpdateMTPHealth_Call) Run(run func(ctx types.Context, mtp margintypes.MTP, ammPool ammtypes.Pool)) *OpenShortChecker_UpdateMTPHealth_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(margintypes.MTP), args[2].(ammtypes.Pool)) + }) + return _c +} + +func (_c *OpenShortChecker_UpdateMTPHealth_Call) Return(_a0 math.LegacyDec, _a1 error) *OpenShortChecker_UpdateMTPHealth_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *OpenShortChecker_UpdateMTPHealth_Call) RunAndReturn(run func(types.Context, margintypes.MTP, ammtypes.Pool) (math.LegacyDec, error)) *OpenShortChecker_UpdateMTPHealth_Call { + _c.Call.Return(run) + return _c +} + +// UpdatePoolHealth provides a mock function with given fields: ctx, pool +func (_m *OpenShortChecker) UpdatePoolHealth(ctx types.Context, pool *margintypes.Pool) error { + ret := _m.Called(ctx, pool) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, *margintypes.Pool) error); ok { + r0 = rf(ctx, pool) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// OpenShortChecker_UpdatePoolHealth_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdatePoolHealth' +type OpenShortChecker_UpdatePoolHealth_Call struct { + *mock.Call +} + +// UpdatePoolHealth is a helper method to define mock.On call +// - ctx types.Context +// - pool *margintypes.Pool +func (_e *OpenShortChecker_Expecter) UpdatePoolHealth(ctx interface{}, pool interface{}) *OpenShortChecker_UpdatePoolHealth_Call { + return &OpenShortChecker_UpdatePoolHealth_Call{Call: _e.mock.On("UpdatePoolHealth", ctx, pool)} +} + +func (_c *OpenShortChecker_UpdatePoolHealth_Call) Run(run func(ctx types.Context, pool *margintypes.Pool)) *OpenShortChecker_UpdatePoolHealth_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(*margintypes.Pool)) + }) + return _c +} + +func (_c *OpenShortChecker_UpdatePoolHealth_Call) Return(_a0 error) *OpenShortChecker_UpdatePoolHealth_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenShortChecker_UpdatePoolHealth_Call) RunAndReturn(run func(types.Context, *margintypes.Pool) error) *OpenShortChecker_UpdatePoolHealth_Call { + _c.Call.Return(run) + return _c +} + +// NewOpenShortChecker creates a new instance of OpenShortChecker. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewOpenShortChecker(t interface { + mock.TestingT + Cleanup(func()) +}) *OpenShortChecker { + mock := &OpenShortChecker{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} From e1bb0cc9bc770b13aa204506ce11ed57b0a96f6f Mon Sep 17 00:00:00 2001 From: kenta-elys Date: Thu, 14 Sep 2023 17:00:41 +0000 Subject: [PATCH 10/10] fix: unit margin unit test failure --- x/margin/keeper/open.go | 4 +- x/margin/keeper/open_test.go | 13 ++ x/margin/types/expected_keepers.go | 4 + x/margin/types/mocks/open_checker.go | 172 +++++++++++++++++++++++++++ 4 files changed, 191 insertions(+), 2 deletions(-) diff --git a/x/margin/keeper/open.go b/x/margin/keeper/open.go index 04b6031ef..9354ab129 100644 --- a/x/margin/keeper/open.go +++ b/x/margin/keeper/open.go @@ -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 } diff --git a/x/margin/keeper/open_test.go b/x/margin/keeper/open_test.go index cbbb6a69f..5f60d32ff 100644 --- a/x/margin/keeper/open_test.go +++ b/x/margin/keeper/open_test.go @@ -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) @@ -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")) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/x/margin/types/expected_keepers.go b/x/margin/types/expected_keepers.go index b27d21583..93907e732 100644 --- a/x/margin/types/expected_keepers.go +++ b/x/margin/types/expected_keepers.go @@ -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 diff --git a/x/margin/types/mocks/open_checker.go b/x/margin/types/mocks/open_checker.go index 9b7b0d9ef..472802669 100644 --- a/x/margin/types/mocks/open_checker.go +++ b/x/margin/types/mocks/open_checker.go @@ -153,6 +153,51 @@ func (_c *OpenChecker_CheckPoolHealth_Call) RunAndReturn(run func(types.Context, return _c } +// CheckSamePosition provides a mock function with given fields: ctx, msg +func (_m *OpenChecker) CheckSamePosition(ctx types.Context, msg *margintypes.MsgOpen) *margintypes.MTP { + ret := _m.Called(ctx, msg) + + var r0 *margintypes.MTP + if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MsgOpen) *margintypes.MTP); ok { + r0 = rf(ctx, msg) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*margintypes.MTP) + } + } + + return r0 +} + +// OpenChecker_CheckSamePosition_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CheckSamePosition' +type OpenChecker_CheckSamePosition_Call struct { + *mock.Call +} + +// CheckSamePosition is a helper method to define mock.On call +// - ctx types.Context +// - msg *margintypes.MsgOpen +func (_e *OpenChecker_Expecter) CheckSamePosition(ctx interface{}, msg interface{}) *OpenChecker_CheckSamePosition_Call { + return &OpenChecker_CheckSamePosition_Call{Call: _e.mock.On("CheckSamePosition", ctx, msg)} +} + +func (_c *OpenChecker_CheckSamePosition_Call) Run(run func(ctx types.Context, msg *margintypes.MsgOpen)) *OpenChecker_CheckSamePosition_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(*margintypes.MsgOpen)) + }) + return _c +} + +func (_c *OpenChecker_CheckSamePosition_Call) Return(_a0 *margintypes.MTP) *OpenChecker_CheckSamePosition_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenChecker_CheckSamePosition_Call) RunAndReturn(run func(types.Context, *margintypes.MsgOpen) *margintypes.MTP) *OpenChecker_CheckSamePosition_Call { + _c.Call.Return(run) + return _c +} + // CheckUserAuthorization provides a mock function with given fields: ctx, msg func (_m *OpenChecker) CheckUserAuthorization(ctx types.Context, msg *margintypes.MsgOpen) error { ret := _m.Called(ctx, msg) @@ -230,6 +275,90 @@ func (_c *OpenChecker_EmitOpenEvent_Call) RunAndReturn(run func(types.Context, * return _c } +// GetMaxOpenPositions provides a mock function with given fields: ctx +func (_m *OpenChecker) GetMaxOpenPositions(ctx types.Context) uint64 { + ret := _m.Called(ctx) + + var r0 uint64 + if rf, ok := ret.Get(0).(func(types.Context) uint64); ok { + r0 = rf(ctx) + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// OpenChecker_GetMaxOpenPositions_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetMaxOpenPositions' +type OpenChecker_GetMaxOpenPositions_Call struct { + *mock.Call +} + +// GetMaxOpenPositions is a helper method to define mock.On call +// - ctx types.Context +func (_e *OpenChecker_Expecter) GetMaxOpenPositions(ctx interface{}) *OpenChecker_GetMaxOpenPositions_Call { + return &OpenChecker_GetMaxOpenPositions_Call{Call: _e.mock.On("GetMaxOpenPositions", ctx)} +} + +func (_c *OpenChecker_GetMaxOpenPositions_Call) Run(run func(ctx types.Context)) *OpenChecker_GetMaxOpenPositions_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context)) + }) + return _c +} + +func (_c *OpenChecker_GetMaxOpenPositions_Call) Return(_a0 uint64) *OpenChecker_GetMaxOpenPositions_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenChecker_GetMaxOpenPositions_Call) RunAndReturn(run func(types.Context) uint64) *OpenChecker_GetMaxOpenPositions_Call { + _c.Call.Return(run) + return _c +} + +// GetOpenMTPCount provides a mock function with given fields: ctx +func (_m *OpenChecker) GetOpenMTPCount(ctx types.Context) uint64 { + ret := _m.Called(ctx) + + var r0 uint64 + if rf, ok := ret.Get(0).(func(types.Context) uint64); ok { + r0 = rf(ctx) + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// OpenChecker_GetOpenMTPCount_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetOpenMTPCount' +type OpenChecker_GetOpenMTPCount_Call struct { + *mock.Call +} + +// GetOpenMTPCount is a helper method to define mock.On call +// - ctx types.Context +func (_e *OpenChecker_Expecter) GetOpenMTPCount(ctx interface{}) *OpenChecker_GetOpenMTPCount_Call { + return &OpenChecker_GetOpenMTPCount_Call{Call: _e.mock.On("GetOpenMTPCount", ctx)} +} + +func (_c *OpenChecker_GetOpenMTPCount_Call) Run(run func(ctx types.Context)) *OpenChecker_GetOpenMTPCount_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context)) + }) + return _c +} + +func (_c *OpenChecker_GetOpenMTPCount_Call) Return(_a0 uint64) *OpenChecker_GetOpenMTPCount_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenChecker_GetOpenMTPCount_Call) RunAndReturn(run func(types.Context) uint64) *OpenChecker_GetOpenMTPCount_Call { + _c.Call.Return(run) + return _c +} + // GetTradingAsset provides a mock function with given fields: collateralAsset, borrowAsset func (_m *OpenChecker) GetTradingAsset(collateralAsset string, borrowAsset string) string { ret := _m.Called(collateralAsset, borrowAsset) @@ -452,6 +581,49 @@ func (_c *OpenChecker_PreparePools_Call) RunAndReturn(run func(types.Context, st return _c } +// SetMTP provides a mock function with given fields: ctx, mtp +func (_m *OpenChecker) SetMTP(ctx types.Context, mtp *margintypes.MTP) error { + ret := _m.Called(ctx, mtp) + + var r0 error + if rf, ok := ret.Get(0).(func(types.Context, *margintypes.MTP) error); ok { + r0 = rf(ctx, mtp) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// OpenChecker_SetMTP_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SetMTP' +type OpenChecker_SetMTP_Call struct { + *mock.Call +} + +// SetMTP is a helper method to define mock.On call +// - ctx types.Context +// - mtp *margintypes.MTP +func (_e *OpenChecker_Expecter) SetMTP(ctx interface{}, mtp interface{}) *OpenChecker_SetMTP_Call { + return &OpenChecker_SetMTP_Call{Call: _e.mock.On("SetMTP", ctx, mtp)} +} + +func (_c *OpenChecker_SetMTP_Call) Run(run func(ctx types.Context, mtp *margintypes.MTP)) *OpenChecker_SetMTP_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(types.Context), args[1].(*margintypes.MTP)) + }) + return _c +} + +func (_c *OpenChecker_SetMTP_Call) Return(_a0 error) *OpenChecker_SetMTP_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *OpenChecker_SetMTP_Call) RunAndReturn(run func(types.Context, *margintypes.MTP) error) *OpenChecker_SetMTP_Call { + _c.Call.Return(run) + return _c +} + // NewOpenChecker creates a new instance of OpenChecker. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewOpenChecker(t interface {