From 76f7f07d4fdd687d81613100cf877efe1360dee7 Mon Sep 17 00:00:00 2001 From: jelysn Date: Mon, 16 Oct 2023 21:06:42 +0800 Subject: [PATCH] Built basic codebase for open long position --- x/leveragelp/keeper/emit_open_event.go | 10 --------- x/leveragelp/keeper/generate_open_event.go | 24 -------------------- x/leveragelp/keeper/open.go | 26 +++++++++++++--------- x/leveragelp/keeper/open_long.go | 14 +++++++++--- 4 files changed, 27 insertions(+), 47 deletions(-) delete mode 100644 x/leveragelp/keeper/emit_open_event.go delete mode 100644 x/leveragelp/keeper/generate_open_event.go diff --git a/x/leveragelp/keeper/emit_open_event.go b/x/leveragelp/keeper/emit_open_event.go deleted file mode 100644 index e2ecc463c..000000000 --- a/x/leveragelp/keeper/emit_open_event.go +++ /dev/null @@ -1,10 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/elys-network/elys/x/leveragelp/types" -) - -func (k Keeper) EmitOpenEvent(ctx sdk.Context, mtp *types.MTP) { - ctx.EventManager().EmitEvent(k.GenerateOpenEvent(mtp)) -} diff --git a/x/leveragelp/keeper/generate_open_event.go b/x/leveragelp/keeper/generate_open_event.go deleted file mode 100644 index c0ff2fbf1..000000000 --- a/x/leveragelp/keeper/generate_open_event.go +++ /dev/null @@ -1,24 +0,0 @@ -package keeper - -import ( - "strconv" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/elys-network/elys/x/leveragelp/types" -) - -func (k Keeper) GenerateOpenEvent(mtp *types.MTP) sdk.Event { - collateralIndex := len(mtp.CollateralAssets) - 1 - mtpPosIndex := len(mtp.Leverages) - 1 - - return sdk.NewEvent(types.EventOpen, - sdk.NewAttribute("id", strconv.FormatInt(int64(mtp.Id), 10)), - sdk.NewAttribute("address", mtp.Address), - sdk.NewAttribute("collateral_asset", mtp.CollateralAssets[collateralIndex]), - sdk.NewAttribute("collateral_amount", mtp.CollateralAmounts[collateralIndex].String()), - sdk.NewAttribute("leverage", mtp.Leverages[mtpPosIndex].String()), - sdk.NewAttribute("liabilities", mtp.Liabilities.String()), - sdk.NewAttribute("interest_paid_collateral", mtp.InterestPaidCollaterals[collateralIndex].String()), - sdk.NewAttribute("health", mtp.MtpHealth.String()), - ) -} diff --git a/x/leveragelp/keeper/open.go b/x/leveragelp/keeper/open.go index ef72b1f26..3253c8f2d 100644 --- a/x/leveragelp/keeper/open.go +++ b/x/leveragelp/keeper/open.go @@ -1,39 +1,45 @@ package keeper import ( + "strconv" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/elys-network/elys/x/leveragelp/types" ) func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenResponse, error) { - if err := k.OpenChecker.CheckUserAuthorization(ctx, msg); err != nil { + if err := k.CheckUserAuthorization(ctx, msg); err != nil { return nil, err } // Check if it is the same direction position for the same trader. - if mtp := k.OpenChecker.CheckSamePosition(ctx, msg); mtp != nil { + if mtp := k.CheckSamePosition(ctx, msg); mtp != nil { return k.OpenConsolidate(ctx, mtp, msg) } - if err := k.OpenChecker.CheckMaxOpenPositions(ctx); err != nil { + if err := k.CheckMaxOpenPositions(ctx); err != nil { return nil, err } - if err := k.OpenChecker.CheckPoolHealth(ctx, msg.AmmPoolId); err != nil { + if err := k.CheckPoolHealth(ctx, msg.AmmPoolId); err != nil { return nil, err } - mtp, err := k.OpenChecker.OpenLong(ctx, msg.AmmPoolId, msg) + mtp, err := k.OpenLong(ctx, msg.AmmPoolId, msg) if err != nil { return nil, err } - k.OpenChecker.EmitOpenEvent(ctx, mtp) - - if k.hooks != nil { - k.hooks.AfterLeveragelpPositionOpen(ctx, ammPool, pool) - } + event := sdk.NewEvent(types.EventOpen, + sdk.NewAttribute("id", strconv.FormatInt(int64(mtp.Id), 10)), + sdk.NewAttribute("address", mtp.Address), + sdk.NewAttribute("collateral", mtp.Collateral.String()), + sdk.NewAttribute("leverage", mtp.Leverage.String()), + sdk.NewAttribute("liabilities", mtp.Liabilities.String()), + sdk.NewAttribute("health", mtp.MtpHealth.String()), + ) + ctx.EventManager().EmitEvent(event) return &types.MsgOpenResponse{}, nil } diff --git a/x/leveragelp/keeper/open_long.go b/x/leveragelp/keeper/open_long.go index 3a3e750c7..69b4f505b 100644 --- a/x/leveragelp/keeper/open_long.go +++ b/x/leveragelp/keeper/open_long.go @@ -22,6 +22,8 @@ func (k Keeper) OpenLong(ctx sdk.Context, poolId uint64, msg *types.MsgOpen) (*t // Initialize a new Leveragelp Trading Position (MTP). mtp := types.NewMTP(msg.Creator, msg.CollateralAsset, leverage, poolId) + mtp.Id = k.GetMTPCount(ctx) + 1 + k.SetMTPCount(ctx, mtp.Id) // Call the function to process the open long logic. return k.ProcessOpenLong(ctx, mtp, leverage, eta, collateralAmountDec, poolId, msg) @@ -59,10 +61,15 @@ func (k Keeper) ProcessOpenLong(ctx sdk.Context, mtp *types.MTP, leverage sdk.De // return nil, err // } - // Borrow the asset the user wants to long. - // TODO: borrow leveragedAmount - collateralAmount - // TODO: send collateral coins to MTP address from MTP owner address + // send collateral coins to MTP address from MTP owner address + mtpOwner := sdk.MustAccAddressFromBech32(mtp.Address) + err = k.bankKeeper.SendCoins(ctx, mtpOwner, mtp.GetMTPAddress(), sdk.Coins{sdk.NewCoin(msg.CollateralAsset, msg.CollateralAmount)}) + if err != nil { + return nil, err + } leverageCoin := sdk.NewCoin(msg.CollateralAsset, leveragedAmount) + + // borrow leveragedAmount - collateralAmount borrowCoin := sdk.NewCoin(msg.CollateralAsset, leveragedAmount.Sub(msg.CollateralAmount)) err = k.stableKeeper.Borrow(ctx, mtp.GetMTPAddress(), borrowCoin) if err != nil { @@ -81,6 +88,7 @@ func (k Keeper) ProcessOpenLong(ctx sdk.Context, mtp *types.MTP, leverage sdk.De } // Update the MTP health. + mtp.Liabilities = borrowCoin.Amount lr, err := k.UpdateMTPHealth(ctx, *mtp, ammPool) if err != nil { return nil, err