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

     1  package coin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	constant "github.com/NpoolPlatform/chain-middleware/pkg/const"
     8  	coincrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/coin"
     9  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    10  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin"
    11  
    12  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    13  
    14  	"github.com/google/uuid"
    15  	"github.com/shopspring/decimal"
    16  )
    17  
    18  type Handler struct {
    19  	ID                          *uint32
    20  	EntID                       *uuid.UUID
    21  	Name                        *string
    22  	Logo                        *string
    23  	Presale                     *bool
    24  	Unit                        *string
    25  	ENV                         *string
    26  	ReservedAmount              *decimal.Decimal
    27  	ForPay                      *bool
    28  	HomePage                    *string
    29  	Specs                       *string
    30  	FeeCoinTypeID               *uuid.UUID
    31  	WithdrawFeeByStableUSD      *bool
    32  	WithdrawFeeAmount           *decimal.Decimal
    33  	CollectFeeAmount            *decimal.Decimal
    34  	HotWalletFeeAmount          *decimal.Decimal
    35  	LowFeeAmount                *decimal.Decimal
    36  	HotLowFeeAmount             *decimal.Decimal
    37  	HotWalletAccountAmount      *decimal.Decimal
    38  	PaymentAccountCollectAmount *decimal.Decimal
    39  	Disabled                    *bool
    40  	StableUSD                   *bool
    41  	LeastTransferAmount         *decimal.Decimal
    42  	NeedMemo                    *bool
    43  	RefreshCurrency             *bool
    44  	CheckNewAddressBalance      *bool
    45  	ChainType                   *string
    46  	ChainNativeUnit             *string
    47  	ChainAtomicUnit             *string
    48  	ChainUnitExp                *uint32
    49  	GasType                     *basetypes.GasType
    50  	ChainID                     *string
    51  	ChainNickname               *string
    52  	ChainNativeCoinName         *string
    53  	Conds                       *coincrud.Conds
    54  	Offset                      int32
    55  	Limit                       int32
    56  }
    57  
    58  func NewHandler(ctx context.Context, options ...func(context.Context, *Handler) error) (*Handler, error) {
    59  	handler := &Handler{}
    60  	for _, opt := range options {
    61  		if err := opt(ctx, handler); err != nil {
    62  			return nil, err
    63  		}
    64  	}
    65  	return handler, nil
    66  }
    67  
    68  func WithID(u *uint32, must bool) func(context.Context, *Handler) error {
    69  	return func(ctx context.Context, h *Handler) error {
    70  		if u == nil {
    71  			if must {
    72  				return fmt.Errorf("invalid id")
    73  			}
    74  			return nil
    75  		}
    76  		h.ID = u
    77  		return nil
    78  	}
    79  }
    80  
    81  func WithEntID(id *string, must bool) func(context.Context, *Handler) error {
    82  	return func(ctx context.Context, h *Handler) error {
    83  		if id == nil {
    84  			if must {
    85  				return fmt.Errorf("invalid entid")
    86  			}
    87  			return nil
    88  		}
    89  		_id, err := uuid.Parse(*id)
    90  		if err != nil {
    91  			return err
    92  		}
    93  		h.EntID = &_id
    94  		return nil
    95  	}
    96  }
    97  
    98  func WithName(name *string, must bool) func(context.Context, *Handler) error {
    99  	return func(ctx context.Context, h *Handler) error {
   100  		if name == nil {
   101  			if must {
   102  				return fmt.Errorf("invalid name")
   103  			}
   104  			return nil
   105  		}
   106  		if *name == "" {
   107  			return fmt.Errorf("invalid coinname")
   108  		}
   109  		h.Name = name
   110  		return nil
   111  	}
   112  }
   113  
   114  func WithLogo(logo *string, must bool) func(context.Context, *Handler) error {
   115  	return func(ctx context.Context, h *Handler) error {
   116  		h.Logo = logo
   117  		return nil
   118  	}
   119  }
   120  
   121  func WithPresale(presale *bool, must bool) func(context.Context, *Handler) error {
   122  	return func(ctx context.Context, h *Handler) error {
   123  		h.Presale = presale
   124  		return nil
   125  	}
   126  }
   127  
   128  func WithUnit(unit *string, must bool) func(context.Context, *Handler) error {
   129  	return func(ctx context.Context, h *Handler) error {
   130  		if unit == nil {
   131  			if must {
   132  				return fmt.Errorf("invalid unit")
   133  			}
   134  			return nil
   135  		}
   136  		if *unit == "" {
   137  			return fmt.Errorf("invalid coinunit")
   138  		}
   139  		h.Unit = unit
   140  		return nil
   141  	}
   142  }
   143  
   144  func WithENV(env *string, must bool) func(context.Context, *Handler) error {
   145  	return func(ctx context.Context, h *Handler) error {
   146  		if env == nil {
   147  			if must {
   148  				return fmt.Errorf("invalid ent")
   149  			}
   150  			return nil
   151  		}
   152  		switch *env {
   153  		case "main":
   154  		case "test":
   155  		case "local":
   156  		default:
   157  			return fmt.Errorf("invalid coinenv")
   158  		}
   159  		h.ENV = env
   160  		return nil
   161  	}
   162  }
   163  
   164  func WithReservedAmount(amount *string, must bool) func(context.Context, *Handler) error {
   165  	return func(ctx context.Context, h *Handler) error {
   166  		if amount == nil {
   167  			return nil
   168  		}
   169  		_amount, err := decimal.NewFromString(*amount)
   170  		if err != nil {
   171  			return err
   172  		}
   173  		h.ReservedAmount = &_amount
   174  		return nil
   175  	}
   176  }
   177  
   178  func WithForPay(forPay *bool, must bool) func(context.Context, *Handler) error {
   179  	return func(ctx context.Context, h *Handler) error {
   180  		h.ForPay = forPay
   181  		return nil
   182  	}
   183  }
   184  
   185  func WithHomePage(homePage *string, must bool) func(context.Context, *Handler) error {
   186  	return func(ctx context.Context, h *Handler) error {
   187  		h.HomePage = homePage
   188  		return nil
   189  	}
   190  }
   191  
   192  func WithSpecs(specs *string, must bool) func(context.Context, *Handler) error {
   193  	return func(ctx context.Context, h *Handler) error {
   194  		h.Specs = specs
   195  		return nil
   196  	}
   197  }
   198  
   199  func WithFeeCoinTypeID(id *string, must bool) func(context.Context, *Handler) error {
   200  	return func(ctx context.Context, h *Handler) error {
   201  		if id == nil {
   202  			if must {
   203  				return fmt.Errorf("invalid feecointypeid")
   204  			}
   205  			return nil
   206  		}
   207  		_id, err := uuid.Parse(*id)
   208  		if err != nil {
   209  			return err
   210  		}
   211  		h.FeeCoinTypeID = &_id
   212  		return nil
   213  	}
   214  }
   215  
   216  func WithWithdrawFeeByStableUSD(stable *bool, must bool) func(context.Context, *Handler) error {
   217  	return func(ctx context.Context, h *Handler) error {
   218  		h.WithdrawFeeByStableUSD = stable
   219  		return nil
   220  	}
   221  }
   222  
   223  func WithWithdrawFeeAmount(amount *string, must bool) func(context.Context, *Handler) error {
   224  	return func(ctx context.Context, h *Handler) error {
   225  		if amount == nil {
   226  			if must {
   227  				return fmt.Errorf("invalid withdrawfeeamount")
   228  			}
   229  			return nil
   230  		}
   231  		_amount, err := decimal.NewFromString(*amount)
   232  		if err != nil {
   233  			return err
   234  		}
   235  		h.WithdrawFeeAmount = &_amount
   236  		return nil
   237  	}
   238  }
   239  
   240  func WithCollectFeeAmount(amount *string, must bool) func(context.Context, *Handler) error {
   241  	return func(ctx context.Context, h *Handler) error {
   242  		if amount == nil {
   243  			if must {
   244  				return fmt.Errorf("invalid collectfeeamount")
   245  			}
   246  			return nil
   247  		}
   248  		_amount, err := decimal.NewFromString(*amount)
   249  		if err != nil {
   250  			return err
   251  		}
   252  		h.CollectFeeAmount = &_amount
   253  		return nil
   254  	}
   255  }
   256  
   257  func WithHotWalletFeeAmount(amount *string, must bool) func(context.Context, *Handler) error {
   258  	return func(ctx context.Context, h *Handler) error {
   259  		if amount == nil {
   260  			if must {
   261  				return fmt.Errorf("invalid hotwalletfeeamount")
   262  			}
   263  			return nil
   264  		}
   265  		_amount, err := decimal.NewFromString(*amount)
   266  		if err != nil {
   267  			return err
   268  		}
   269  		h.HotWalletFeeAmount = &_amount
   270  		return nil
   271  	}
   272  }
   273  
   274  func WithLowFeeAmount(amount *string, must bool) func(context.Context, *Handler) error {
   275  	return func(ctx context.Context, h *Handler) error {
   276  		if amount == nil {
   277  			if must {
   278  				return fmt.Errorf("invalid lowfeeamount")
   279  			}
   280  			return nil
   281  		}
   282  		_amount, err := decimal.NewFromString(*amount)
   283  		if err != nil {
   284  			return err
   285  		}
   286  		h.LowFeeAmount = &_amount
   287  		return nil
   288  	}
   289  }
   290  
   291  func WithHotLowFeeAmount(amount *string, must bool) func(context.Context, *Handler) error {
   292  	return func(ctx context.Context, h *Handler) error {
   293  		if amount == nil {
   294  			if must {
   295  				return fmt.Errorf("invalid hotlowfeeamount")
   296  			}
   297  			return nil
   298  		}
   299  		_amount, err := decimal.NewFromString(*amount)
   300  		if err != nil {
   301  			return err
   302  		}
   303  		h.HotLowFeeAmount = &_amount
   304  		return nil
   305  	}
   306  }
   307  
   308  func WithHotWalletAccountAmount(amount *string, must bool) func(context.Context, *Handler) error {
   309  	return func(ctx context.Context, h *Handler) error {
   310  		if amount == nil {
   311  			if must {
   312  				return fmt.Errorf("invalid hotwalletaccountamount")
   313  			}
   314  			return nil
   315  		}
   316  		_amount, err := decimal.NewFromString(*amount)
   317  		if err != nil {
   318  			return err
   319  		}
   320  		h.HotWalletAccountAmount = &_amount
   321  		return nil
   322  	}
   323  }
   324  
   325  func WithPaymentAccountCollectAmount(amount *string, must bool) func(context.Context, *Handler) error {
   326  	return func(ctx context.Context, h *Handler) error {
   327  		if amount == nil {
   328  			if must {
   329  				return fmt.Errorf("invalid paymentaccountcollectamount")
   330  			}
   331  			return nil
   332  		}
   333  		_amount, err := decimal.NewFromString(*amount)
   334  		if err != nil {
   335  			return err
   336  		}
   337  		h.PaymentAccountCollectAmount = &_amount
   338  		return nil
   339  	}
   340  }
   341  
   342  func WithDisabled(disabled *bool, must bool) func(context.Context, *Handler) error {
   343  	return func(ctx context.Context, h *Handler) error {
   344  		h.Disabled = disabled
   345  		return nil
   346  	}
   347  }
   348  
   349  func WithStableUSD(stable *bool, must bool) func(context.Context, *Handler) error {
   350  	return func(ctx context.Context, h *Handler) error {
   351  		h.StableUSD = stable
   352  		return nil
   353  	}
   354  }
   355  
   356  func WithLeastTransferAmount(amount *string, must bool) func(context.Context, *Handler) error {
   357  	return func(ctx context.Context, h *Handler) error {
   358  		if amount == nil {
   359  			if must {
   360  				return fmt.Errorf("invalid leasttransferamount")
   361  			}
   362  			return nil
   363  		}
   364  		_amount, err := decimal.NewFromString(*amount)
   365  		if err != nil {
   366  			return err
   367  		}
   368  		h.LeastTransferAmount = &_amount
   369  		return nil
   370  	}
   371  }
   372  
   373  func WithNeedMemo(needMemo *bool, must bool) func(context.Context, *Handler) error {
   374  	return func(ctx context.Context, h *Handler) error {
   375  		h.NeedMemo = needMemo
   376  		return nil
   377  	}
   378  }
   379  
   380  func WithRefreshCurrency(refresh *bool, must bool) func(context.Context, *Handler) error {
   381  	return func(ctx context.Context, h *Handler) error {
   382  		h.RefreshCurrency = refresh
   383  		return nil
   384  	}
   385  }
   386  
   387  func WithCheckNewAddressBalance(check *bool, must bool) func(context.Context, *Handler) error {
   388  	return func(ctx context.Context, h *Handler) error {
   389  		h.CheckNewAddressBalance = check
   390  		return nil
   391  	}
   392  }
   393  
   394  func WithChainType(chainType *string, must bool) func(context.Context, *Handler) error {
   395  	return func(ctx context.Context, h *Handler) error {
   396  		if chainType == nil {
   397  			if must {
   398  				return fmt.Errorf("invalid chaintype")
   399  			}
   400  			return nil
   401  		}
   402  		if *chainType == "" {
   403  			return fmt.Errorf("invalid chaintype")
   404  		}
   405  		h.ChainType = chainType
   406  		return nil
   407  	}
   408  }
   409  
   410  func WithChainNativeUnit(unit *string, must bool) func(context.Context, *Handler) error {
   411  	return func(ctx context.Context, h *Handler) error {
   412  		if unit == nil {
   413  			if must {
   414  				return fmt.Errorf("invalid chainnativeunit")
   415  			}
   416  			return nil
   417  		}
   418  		if *unit == "" {
   419  			return fmt.Errorf("invalid nativeunit")
   420  		}
   421  		h.ChainNativeUnit = unit
   422  		return nil
   423  	}
   424  }
   425  
   426  func WithChainAtomicUnit(unit *string, must bool) func(context.Context, *Handler) error {
   427  	return func(ctx context.Context, h *Handler) error {
   428  		if unit == nil {
   429  			if must {
   430  				return fmt.Errorf("invalid chainatomicunit")
   431  			}
   432  			return nil
   433  		}
   434  		if *unit == "" {
   435  			return fmt.Errorf("invalid atomicunit")
   436  		}
   437  		h.ChainAtomicUnit = unit
   438  		return nil
   439  	}
   440  }
   441  
   442  func WithChainUnitExp(exp *uint32, must bool) func(context.Context, *Handler) error {
   443  	return func(ctx context.Context, h *Handler) error {
   444  		h.ChainUnitExp = exp
   445  		return nil
   446  	}
   447  }
   448  
   449  func WithGasType(gasType *basetypes.GasType, must bool) func(context.Context, *Handler) error {
   450  	return func(ctx context.Context, h *Handler) error {
   451  		if gasType == nil {
   452  			if must {
   453  				return fmt.Errorf("invalid gastype")
   454  			}
   455  			return nil
   456  		}
   457  		switch *gasType {
   458  		case basetypes.GasType_FixedGas:
   459  		case basetypes.GasType_DynamicGas:
   460  		case basetypes.GasType_GasUnsupported:
   461  		default:
   462  			return fmt.Errorf("invalid gastype")
   463  		}
   464  		h.GasType = gasType
   465  		return nil
   466  	}
   467  }
   468  
   469  func WithChainID(id *string, must bool) func(context.Context, *Handler) error {
   470  	return func(ctx context.Context, h *Handler) error {
   471  		if id == nil {
   472  			if must {
   473  				return fmt.Errorf("invalid chainid")
   474  			}
   475  			return nil
   476  		}
   477  		if *id == "" {
   478  			return fmt.Errorf("invalid chainid")
   479  		}
   480  		h.ChainID = id
   481  		return nil
   482  	}
   483  }
   484  
   485  func WithChainNickname(nickname *string, must bool) func(context.Context, *Handler) error {
   486  	return func(ctx context.Context, h *Handler) error {
   487  		if nickname == nil {
   488  			if must {
   489  				return fmt.Errorf("invalid chainnickname")
   490  			}
   491  			return nil
   492  		}
   493  		if *nickname == "" {
   494  			return fmt.Errorf("invalid nickname")
   495  		}
   496  		h.ChainNickname = nickname
   497  		return nil
   498  	}
   499  }
   500  
   501  func WithChainNativeCoinName(name *string, must bool) func(context.Context, *Handler) error {
   502  	return func(ctx context.Context, h *Handler) error {
   503  		if name == nil {
   504  			if must {
   505  				return fmt.Errorf("invalid chainnativecoinname")
   506  			}
   507  			return nil
   508  		}
   509  		if *name == "" {
   510  			return fmt.Errorf("invalid chainnativecoinname")
   511  		}
   512  		h.ChainNativeCoinName = name
   513  		return nil
   514  	}
   515  }
   516  
   517  func WithConds(conds *npool.Conds) func(context.Context, *Handler) error {
   518  	return func(ctx context.Context, h *Handler) error {
   519  		h.Conds = &coincrud.Conds{}
   520  		if conds == nil {
   521  			return nil
   522  		}
   523  		if conds.EntID != nil {
   524  			id, err := uuid.Parse(conds.GetEntID().GetValue())
   525  			if err != nil {
   526  				return err
   527  			}
   528  			h.Conds.EntID = &cruder.Cond{
   529  				Op:  conds.GetEntID().GetOp(),
   530  				Val: id,
   531  			}
   532  		}
   533  		if conds.Presale != nil {
   534  			h.Conds.Presale = &cruder.Cond{
   535  				Op:  conds.GetPresale().GetOp(),
   536  				Val: conds.GetPresale().GetValue(),
   537  			}
   538  		}
   539  		if conds.ENV != nil {
   540  			h.Conds.ENV = &cruder.Cond{
   541  				Op:  conds.GetENV().GetOp(),
   542  				Val: conds.GetENV().GetValue(),
   543  			}
   544  		}
   545  		if conds.ForPay != nil {
   546  			h.Conds.ForPay = &cruder.Cond{
   547  				Op:  conds.GetForPay().GetOp(),
   548  				Val: conds.GetForPay().GetValue(),
   549  			}
   550  		}
   551  		if conds.EntIDs != nil {
   552  			ids := []uuid.UUID{}
   553  			for _, id := range conds.GetEntIDs().GetValue() {
   554  				_id, err := uuid.Parse(id)
   555  				if err != nil {
   556  					return err
   557  				}
   558  				ids = append(ids, _id)
   559  			}
   560  			h.Conds.EntIDs = &cruder.Cond{
   561  				Op:  conds.GetEntIDs().GetOp(),
   562  				Val: ids,
   563  			}
   564  		}
   565  		if conds.Disabled != nil {
   566  			h.Conds.Disabled = &cruder.Cond{
   567  				Op:  conds.GetDisabled().GetOp(),
   568  				Val: conds.GetDisabled().GetValue(),
   569  			}
   570  		}
   571  		if conds.Name != nil {
   572  			h.Conds.Name = &cruder.Cond{
   573  				Op:  conds.GetName().GetOp(),
   574  				Val: conds.GetName().GetValue(),
   575  			}
   576  		}
   577  		if conds.Names != nil {
   578  			h.Conds.Names = &cruder.Cond{
   579  				Op:  conds.GetNames().GetOp(),
   580  				Val: conds.GetNames().GetValue(),
   581  			}
   582  		}
   583  		return nil
   584  	}
   585  }
   586  
   587  func WithOffset(offset int32) func(context.Context, *Handler) error {
   588  	return func(ctx context.Context, h *Handler) error {
   589  		h.Offset = offset
   590  		return nil
   591  	}
   592  }
   593  
   594  func WithLimit(limit int32) func(context.Context, *Handler) error {
   595  	return func(ctx context.Context, h *Handler) error {
   596  		if limit == 0 {
   597  			limit = constant.DefaultRowLimit
   598  		}
   599  		h.Limit = limit
   600  		return nil
   601  	}
   602  }