Skip to content

Commit

Permalink
Built basic codebase for open long position
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Oct 16, 2023
1 parent aa2df35 commit 76f7f07
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 47 deletions.
10 changes: 0 additions & 10 deletions x/leveragelp/keeper/emit_open_event.go

This file was deleted.

24 changes: 0 additions & 24 deletions x/leveragelp/keeper/generate_open_event.go

This file was deleted.

26 changes: 16 additions & 10 deletions x/leveragelp/keeper/open.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 11 additions & 3 deletions x/leveragelp/keeper/open_long.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 76f7f07

Please sign in to comment.