github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/app/coin/create.go (about)

     1  package appcoin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/NpoolPlatform/chain-middleware/pkg/db"
     8  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
     9  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    10  
    11  	appcoincrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/app/coin"
    12  	appexratecrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/app/coin/exrate"
    13  	coincrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/coin"
    14  	coinmw "github.com/NpoolPlatform/chain-middleware/pkg/mw/coin"
    15  	redis2 "github.com/NpoolPlatform/go-service-framework/pkg/redis"
    16  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    17  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/app/coin"
    18  
    19  	"github.com/google/uuid"
    20  )
    21  
    22  type createHandler struct {
    23  	*Handler
    24  }
    25  
    26  func (h *createHandler) createAppCoin(ctx context.Context, tx *ent.Tx) error {
    27  	if _, err := appcoincrud.CreateSet(
    28  		tx.AppCoin.Create(),
    29  		&appcoincrud.Req{
    30  			ID:                       h.ID,
    31  			EntID:                    h.EntID,
    32  			AppID:                    h.AppID,
    33  			CoinTypeID:               h.CoinTypeID,
    34  			Name:                     h.Name,
    35  			DisplayNames:             h.DisplayNames,
    36  			Logo:                     h.Logo,
    37  			ForPay:                   h.ForPay,
    38  			ProductPage:              h.ProductPage,
    39  			WithdrawAutoReviewAmount: h.WithdrawAutoReviewAmount,
    40  			DailyRewardAmount:        h.DailyRewardAmount,
    41  			Display:                  h.Display,
    42  			DisplayIndex:             h.DisplayIndex,
    43  			MaxAmountPerWithdraw:     h.MaxAmountPerWithdraw,
    44  		},
    45  	).Save(ctx); err != nil {
    46  		return err
    47  	}
    48  	return nil
    49  }
    50  
    51  func (h *createHandler) createExrate(ctx context.Context, tx *ent.Tx) error {
    52  	if _, err := appexratecrud.CreateSet(
    53  		tx.ExchangeRate.Create(),
    54  		&appexratecrud.Req{
    55  			AppID:         h.AppID,
    56  			CoinTypeID:    h.CoinTypeID,
    57  			MarketValue:   h.MarketValue,
    58  			SettlePercent: h.SettlePercent,
    59  			SettleTips:    h.SettleTips,
    60  			Setter:        h.Setter,
    61  		},
    62  	).Save(ctx); err != nil {
    63  		return err
    64  	}
    65  	return nil
    66  }
    67  
    68  func (h *Handler) CreateCoin(ctx context.Context) (*npool.Coin, error) {
    69  	lockKey := fmt.Sprintf(
    70  		"%v:%v:%v",
    71  		basetypes.Prefix_PrefixCreateAppCoin,
    72  		*h.AppID,
    73  		*h.CoinTypeID,
    74  	)
    75  	if err := redis2.TryLock(lockKey, 0); err != nil {
    76  		return nil, err
    77  	}
    78  	defer func() {
    79  		_ = redis2.Unlock(lockKey)
    80  	}()
    81  
    82  	h.Conds = &appcoincrud.Conds{
    83  		AppID:      &cruder.Cond{Op: cruder.EQ, Val: *h.AppID},
    84  		CoinTypeID: &cruder.Cond{Op: cruder.EQ, Val: *h.CoinTypeID},
    85  	}
    86  	exist, err := h.ExistCoinConds(ctx)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	if exist {
    91  		return nil, fmt.Errorf("appcoin exist")
    92  	}
    93  
    94  	coinHandler, err := coinmw.NewHandler(
    95  		ctx,
    96  	)
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  	coinHandler.Conds = &coincrud.Conds{
   101  		EntID: &cruder.Cond{Op: cruder.EQ, Val: *h.CoinTypeID},
   102  	}
   103  	coin, err := coinHandler.GetCoinOnly(ctx)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	if coin == nil {
   108  		return nil, fmt.Errorf("coin not exist")
   109  	}
   110  	if h.Name == nil {
   111  		h.Name = &coin.Name
   112  	}
   113  
   114  	id := uuid.New()
   115  	if h.EntID == nil {
   116  		h.EntID = &id
   117  	}
   118  
   119  	handler := &createHandler{
   120  		Handler: h,
   121  	}
   122  
   123  	err = db.WithTx(ctx, func(_ctx context.Context, tx *ent.Tx) error {
   124  		if err := handler.createAppCoin(_ctx, tx); err != nil {
   125  			return err
   126  		}
   127  		if err := handler.createExrate(_ctx, tx); err != nil {
   128  			return err
   129  		}
   130  		return nil
   131  	})
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	return h.GetCoin(ctx)
   137  }