github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/keeper/msg_server.go (about)

     1  package keeper
     2  
     3  // DONTCOVER
     4  
     5  // Although written in msg_server_test.go, it is approached at the keeper level rather than at the msgServer level
     6  // so is not included in the coverage.
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"strconv"
    12  
    13  	sdk "github.com/cosmos/cosmos-sdk/types"
    14  
    15  	"github.com/gravity-devs/liquidity/x/liquidity/types"
    16  )
    17  
    18  type msgServer struct {
    19  	Keeper
    20  }
    21  
    22  // NewMsgServerImpl returns an implementation of the distribution MsgServer interface
    23  // for the provided Keeper.
    24  func NewMsgServerImpl(keeper Keeper) types.MsgServer {
    25  	return &msgServer{Keeper: keeper}
    26  }
    27  
    28  var _ types.MsgServer = msgServer{}
    29  
    30  // Message server, handler for CreatePool msg
    31  func (k msgServer) CreatePool(goCtx context.Context, msg *types.MsgCreatePool) (*types.MsgCreatePoolResponse, error) {
    32  	ctx := sdk.UnwrapSDKContext(goCtx)
    33  
    34  	if k.GetCircuitBreakerEnabled(ctx) {
    35  		return nil, types.ErrCircuitBreakerEnabled
    36  	}
    37  
    38  	pool, err := k.Keeper.CreatePool(ctx, msg)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	ctx.EventManager().EmitEvents(sdk.Events{
    44  		sdk.NewEvent(
    45  			sdk.EventTypeMessage,
    46  			sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
    47  		),
    48  		sdk.NewEvent(
    49  			types.EventTypeCreatePool,
    50  			sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(pool.Id, 10)),
    51  			sdk.NewAttribute(types.AttributeValuePoolTypeId, fmt.Sprintf("%d", msg.PoolTypeId)),
    52  			sdk.NewAttribute(types.AttributeValuePoolName, pool.Name()),
    53  			sdk.NewAttribute(types.AttributeValueReserveAccount, pool.ReserveAccountAddress),
    54  			sdk.NewAttribute(types.AttributeValueDepositCoins, msg.DepositCoins.String()),
    55  			sdk.NewAttribute(types.AttributeValuePoolCoinDenom, pool.PoolCoinDenom),
    56  		),
    57  	})
    58  
    59  	return &types.MsgCreatePoolResponse{}, nil
    60  }
    61  
    62  // Message server, handler for MsgDepositWithinBatch
    63  func (k msgServer) DepositWithinBatch(goCtx context.Context, msg *types.MsgDepositWithinBatch) (*types.MsgDepositWithinBatchResponse, error) {
    64  	ctx := sdk.UnwrapSDKContext(goCtx)
    65  
    66  	if k.GetCircuitBreakerEnabled(ctx) {
    67  		return nil, types.ErrCircuitBreakerEnabled
    68  	}
    69  
    70  	poolBatch, found := k.GetPoolBatch(ctx, msg.PoolId)
    71  	if !found {
    72  		return nil, types.ErrPoolBatchNotExists
    73  	}
    74  
    75  	batchMsg, err := k.Keeper.DepositWithinBatch(ctx, msg)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	ctx.EventManager().EmitEvents(sdk.Events{
    81  		sdk.NewEvent(
    82  			sdk.EventTypeMessage,
    83  			sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
    84  		),
    85  		sdk.NewEvent(
    86  			types.EventTypeDepositWithinBatch,
    87  			sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(batchMsg.Msg.PoolId, 10)),
    88  			sdk.NewAttribute(types.AttributeValueBatchIndex, strconv.FormatUint(poolBatch.Index, 10)),
    89  			sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(batchMsg.MsgIndex, 10)),
    90  			sdk.NewAttribute(types.AttributeValueDepositCoins, batchMsg.Msg.DepositCoins.String()),
    91  		),
    92  	})
    93  
    94  	return &types.MsgDepositWithinBatchResponse{}, nil
    95  }
    96  
    97  // Message server, handler for MsgWithdrawWithinBatch
    98  func (k msgServer) WithdrawWithinBatch(goCtx context.Context, msg *types.MsgWithdrawWithinBatch) (*types.MsgWithdrawWithinBatchResponse, error) {
    99  	ctx := sdk.UnwrapSDKContext(goCtx)
   100  
   101  	poolBatch, found := k.GetPoolBatch(ctx, msg.PoolId)
   102  	if !found {
   103  		return nil, types.ErrPoolBatchNotExists
   104  	}
   105  
   106  	batchMsg, err := k.Keeper.WithdrawWithinBatch(ctx, msg)
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	ctx.EventManager().EmitEvents(sdk.Events{
   112  		sdk.NewEvent(
   113  			sdk.EventTypeMessage,
   114  			sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
   115  		),
   116  		sdk.NewEvent(
   117  			types.EventTypeWithdrawWithinBatch,
   118  			sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(batchMsg.Msg.PoolId, 10)),
   119  			sdk.NewAttribute(types.AttributeValueBatchIndex, strconv.FormatUint(poolBatch.Index, 10)),
   120  			sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(batchMsg.MsgIndex, 10)),
   121  			sdk.NewAttribute(types.AttributeValuePoolCoinDenom, batchMsg.Msg.PoolCoin.Denom),
   122  			sdk.NewAttribute(types.AttributeValuePoolCoinAmount, batchMsg.Msg.PoolCoin.Amount.String()),
   123  		),
   124  	})
   125  
   126  	return &types.MsgWithdrawWithinBatchResponse{}, nil
   127  }
   128  
   129  // Message server, handler for MsgSwapWithinBatch
   130  func (k msgServer) Swap(goCtx context.Context, msg *types.MsgSwapWithinBatch) (*types.MsgSwapWithinBatchResponse, error) {
   131  	ctx := sdk.UnwrapSDKContext(goCtx)
   132  
   133  	if k.GetCircuitBreakerEnabled(ctx) {
   134  		return nil, types.ErrCircuitBreakerEnabled
   135  	}
   136  
   137  	poolBatch, found := k.GetPoolBatch(ctx, msg.PoolId)
   138  	if !found {
   139  		return nil, types.ErrPoolBatchNotExists
   140  	}
   141  
   142  	batchMsg, err := k.Keeper.SwapWithinBatch(ctx, msg, types.CancelOrderLifeSpan)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	ctx.EventManager().EmitEvents(sdk.Events{
   148  		sdk.NewEvent(
   149  			sdk.EventTypeMessage,
   150  			sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
   151  		),
   152  		sdk.NewEvent(
   153  			types.EventTypeSwapWithinBatch,
   154  			sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(batchMsg.Msg.PoolId, 10)),
   155  			sdk.NewAttribute(types.AttributeValueBatchIndex, strconv.FormatUint(poolBatch.Index, 10)),
   156  			sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(batchMsg.MsgIndex, 10)),
   157  			sdk.NewAttribute(types.AttributeValueSwapTypeId, strconv.FormatUint(uint64(batchMsg.Msg.SwapTypeId), 10)),
   158  			sdk.NewAttribute(types.AttributeValueOfferCoinDenom, batchMsg.Msg.OfferCoin.Denom),
   159  			sdk.NewAttribute(types.AttributeValueOfferCoinAmount, batchMsg.Msg.OfferCoin.Amount.String()),
   160  			sdk.NewAttribute(types.AttributeValueOfferCoinFeeAmount, batchMsg.Msg.OfferCoinFee.Amount.String()),
   161  			sdk.NewAttribute(types.AttributeValueDemandCoinDenom, batchMsg.Msg.DemandCoinDenom),
   162  			sdk.NewAttribute(types.AttributeValueOrderPrice, batchMsg.Msg.OrderPrice.String()),
   163  		),
   164  	})
   165  
   166  	return &types.MsgSwapWithinBatchResponse{}, nil
   167  }