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

     1  package coin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin"
     8  
     9  	basecrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/coin"
    10  	extracrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/coin/extra"
    11  	settingcrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/coin/setting"
    12  
    13  	"github.com/NpoolPlatform/chain-middleware/pkg/db"
    14  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
    15  	entextra "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/coinextra"
    16  	entsetting "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/setting"
    17  )
    18  
    19  type updateHandler struct {
    20  	*Handler
    21  }
    22  
    23  func (h *updateHandler) updateCoinBase(ctx context.Context, tx *ent.Tx) error {
    24  	info, err := basecrud.UpdateSet(
    25  		tx.CoinBase.UpdateOneID(*h.ID),
    26  		&basecrud.Req{
    27  			Name:           h.Name,
    28  			Logo:           h.Logo,
    29  			Presale:        h.Presale,
    30  			Unit:           h.Unit,
    31  			ENV:            h.ENV,
    32  			ReservedAmount: h.ReservedAmount,
    33  			ForPay:         h.ForPay,
    34  			Disabled:       h.Disabled,
    35  		},
    36  	).Save(ctx)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	h.EntID = &info.EntID
    41  	return nil
    42  }
    43  
    44  func (h *updateHandler) updateCoinExtra(ctx context.Context, tx *ent.Tx) error {
    45  	info, err := tx.
    46  		CoinExtra.
    47  		Query().
    48  		Where(
    49  			entextra.CoinTypeID(*h.EntID),
    50  		).
    51  		ForUpdate().
    52  		Only(ctx)
    53  	if err != nil {
    54  		if !ent.IsNotFound(err) {
    55  			return err
    56  		}
    57  	}
    58  
    59  	if info != nil {
    60  		if _, err := extracrud.UpdateSet(
    61  			info.Update(),
    62  			&extracrud.Req{
    63  				CoinTypeID: h.EntID,
    64  				StableUSD:  h.StableUSD,
    65  				HomePage:   h.HomePage,
    66  				Specs:      h.Specs,
    67  			},
    68  		).Save(ctx); err != nil {
    69  			return err
    70  		}
    71  		return nil
    72  	}
    73  
    74  	if _, err := extracrud.CreateSet(
    75  		tx.CoinExtra.Create(),
    76  		&extracrud.Req{
    77  			CoinTypeID: h.EntID,
    78  			HomePage:   h.HomePage,
    79  			Specs:      h.Specs,
    80  		},
    81  	).Save(ctx); err != nil {
    82  		return err
    83  	}
    84  	return nil
    85  }
    86  
    87  func (h *updateHandler) updateCoinSetting(ctx context.Context, tx *ent.Tx) error {
    88  	info, err := tx.
    89  		Setting.
    90  		Query().
    91  		Where(
    92  			entsetting.CoinTypeID(*h.EntID),
    93  		).
    94  		ForUpdate().
    95  		Only(ctx)
    96  	if err != nil {
    97  		if !ent.IsNotFound(err) {
    98  			return err
    99  		}
   100  	}
   101  
   102  	if info != nil {
   103  		if _, err := settingcrud.UpdateSet(
   104  			info.Update(),
   105  			&settingcrud.Req{
   106  				CoinTypeID:                  h.EntID,
   107  				FeeCoinTypeID:               h.FeeCoinTypeID,
   108  				WithdrawFeeByStableUSD:      h.WithdrawFeeByStableUSD,
   109  				WithdrawFeeAmount:           h.WithdrawFeeAmount,
   110  				CollectFeeAmount:            h.CollectFeeAmount,
   111  				HotWalletFeeAmount:          h.HotWalletFeeAmount,
   112  				LowFeeAmount:                h.LowFeeAmount,
   113  				HotLowFeeAmount:             h.HotLowFeeAmount,
   114  				HotWalletAccountAmount:      h.HotWalletAccountAmount,
   115  				PaymentAccountCollectAmount: h.PaymentAccountCollectAmount,
   116  				LeastTransferAmount:         h.LeastTransferAmount,
   117  				NeedMemo:                    h.NeedMemo,
   118  				RefreshCurrency:             h.RefreshCurrency,
   119  				CheckNewAddressBalance:      h.CheckNewAddressBalance,
   120  			},
   121  		).Save(ctx); err != nil {
   122  			return err
   123  		}
   124  		return nil
   125  	}
   126  
   127  	if _, err := settingcrud.CreateSet(
   128  		tx.Setting.Create(),
   129  		&settingcrud.Req{
   130  			CoinTypeID:                  h.EntID,
   131  			FeeCoinTypeID:               h.FeeCoinTypeID,
   132  			WithdrawFeeByStableUSD:      h.WithdrawFeeByStableUSD,
   133  			WithdrawFeeAmount:           h.WithdrawFeeAmount,
   134  			CollectFeeAmount:            h.CollectFeeAmount,
   135  			HotWalletFeeAmount:          h.HotWalletFeeAmount,
   136  			LowFeeAmount:                h.LowFeeAmount,
   137  			HotLowFeeAmount:             h.HotLowFeeAmount,
   138  			HotWalletAccountAmount:      h.HotWalletAccountAmount,
   139  			PaymentAccountCollectAmount: h.PaymentAccountCollectAmount,
   140  			LeastTransferAmount:         h.LeastTransferAmount,
   141  			NeedMemo:                    h.NeedMemo,
   142  			RefreshCurrency:             h.RefreshCurrency,
   143  			CheckNewAddressBalance:      h.CheckNewAddressBalance,
   144  		},
   145  	).Save(ctx); err != nil {
   146  		return err
   147  	}
   148  
   149  	return nil
   150  }
   151  
   152  func (h *Handler) UpdateCoin(ctx context.Context) (*npool.Coin, error) {
   153  	if h.ID == nil {
   154  		return nil, fmt.Errorf("invalid cointypeid")
   155  	}
   156  
   157  	handler := &updateHandler{
   158  		Handler: h,
   159  	}
   160  
   161  	err := db.WithTx(ctx, func(_ctx context.Context, tx *ent.Tx) error {
   162  		if err := handler.updateCoinBase(_ctx, tx); err != nil {
   163  			return err
   164  		}
   165  		if err := handler.updateCoinExtra(_ctx, tx); err != nil {
   166  			return err
   167  		}
   168  		if err := handler.updateCoinSetting(_ctx, tx); err != nil {
   169  			return err
   170  		}
   171  		return nil
   172  	})
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  
   177  	return h.GetCoin(ctx)
   178  }