github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/fiat/currency/handler.go (about) 1 package currency 2 3 import ( 4 "context" 5 "fmt" 6 7 constant "github.com/NpoolPlatform/chain-middleware/pkg/const" 8 currencycrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/fiat/currency" 9 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 10 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/fiat/currency" 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 FiatID *uuid.UUID 22 FeedType *basetypes.CurrencyFeedType 23 MarketValueHigh *decimal.Decimal 24 MarketValueLow *decimal.Decimal 25 Reqs []*currencycrud.Req 26 Conds *currencycrud.Conds 27 Offset int32 28 Limit int32 29 } 30 31 func NewHandler(ctx context.Context, options ...func(context.Context, *Handler) error) (*Handler, error) { 32 handler := &Handler{} 33 for _, opt := range options { 34 if err := opt(ctx, handler); err != nil { 35 return nil, err 36 } 37 } 38 return handler, nil 39 } 40 41 func WithID(u *uint32, must bool) func(context.Context, *Handler) error { 42 return func(ctx context.Context, h *Handler) error { 43 if u == nil { 44 if must { 45 return fmt.Errorf("invalid id") 46 } 47 return nil 48 } 49 h.ID = u 50 return nil 51 } 52 } 53 54 func WithEntID(id *string, must bool) func(context.Context, *Handler) error { 55 return func(ctx context.Context, h *Handler) error { 56 if id == nil { 57 if must { 58 return fmt.Errorf("invalid entid") 59 } 60 return nil 61 } 62 _id, err := uuid.Parse(*id) 63 if err != nil { 64 return err 65 } 66 h.EntID = &_id 67 return nil 68 } 69 } 70 71 func WithFiatID(id *string, must bool) func(context.Context, *Handler) error { 72 return func(ctx context.Context, h *Handler) error { 73 if id == nil { 74 if must { 75 return fmt.Errorf("invalid fiatid") 76 } 77 return nil 78 } 79 _id, err := uuid.Parse(*id) 80 if err != nil { 81 return err 82 } 83 h.FiatID = &_id 84 return nil 85 } 86 } 87 88 func WithFeedType(feedType *basetypes.CurrencyFeedType, must bool) func(context.Context, *Handler) error { 89 return func(ctx context.Context, h *Handler) error { 90 if feedType == nil { 91 if must { 92 return fmt.Errorf("invalid feedtype") 93 } 94 return nil 95 } 96 switch *feedType { 97 case basetypes.CurrencyFeedType_CoinGecko: 98 case basetypes.CurrencyFeedType_CoinBase: 99 case basetypes.CurrencyFeedType_StableUSDHardCode: 100 default: 101 return fmt.Errorf("invalid feedtype") 102 } 103 h.FeedType = feedType 104 return nil 105 } 106 } 107 108 func WithMarketValueHigh(value *string, must bool) func(context.Context, *Handler) error { 109 return func(ctx context.Context, h *Handler) error { 110 if value == nil { 111 if must { 112 return fmt.Errorf("invalid marketvaluehigh") 113 } 114 return nil 115 } 116 _value, err := decimal.NewFromString(*value) 117 if err != nil { 118 return err 119 } 120 h.MarketValueHigh = &_value 121 return nil 122 } 123 } 124 125 func WithMarketValueLow(value *string, must bool) func(context.Context, *Handler) error { 126 return func(ctx context.Context, h *Handler) error { 127 if value == nil { 128 if must { 129 return fmt.Errorf("invalid marketvaluelow") 130 } 131 return nil 132 } 133 _value, err := decimal.NewFromString(*value) 134 if err != nil { 135 return err 136 } 137 h.MarketValueLow = &_value 138 return nil 139 } 140 } 141 142 func WithReqs(reqs []*npool.CurrencyReq, must bool) func(context.Context, *Handler) error { 143 return func(ctx context.Context, h *Handler) error { 144 _reqs := []*currencycrud.Req{} 145 for _, req := range reqs { 146 _req := ¤cycrud.Req{} 147 if req.EntID != nil { 148 id, err := uuid.Parse(*req.EntID) 149 if err != nil { 150 return err 151 } 152 _req.EntID = &id 153 } 154 if req.FiatID != nil { 155 id, err := uuid.Parse(*req.FiatID) 156 if err != nil { 157 return err 158 } 159 _req.FiatID = &id 160 } 161 if req.FeedType != nil { 162 switch *req.FeedType { 163 case basetypes.CurrencyFeedType_CoinGecko: 164 case basetypes.CurrencyFeedType_CoinBase: 165 case basetypes.CurrencyFeedType_StableUSDHardCode: 166 default: 167 return fmt.Errorf("invalid feedtype") 168 } 169 _req.FeedType = req.FeedType 170 } 171 if req.MarketValueHigh != nil { 172 amount, err := decimal.NewFromString(*req.MarketValueHigh) 173 if err != nil { 174 return err 175 } 176 _req.MarketValueHigh = &amount 177 } 178 if req.MarketValueLow != nil { 179 amount, err := decimal.NewFromString(*req.MarketValueLow) 180 if err != nil { 181 return err 182 } 183 _req.MarketValueLow = &amount 184 } 185 _reqs = append(_reqs, _req) 186 } 187 h.Reqs = _reqs 188 return nil 189 } 190 } 191 192 func WithConds(conds *npool.Conds) func(context.Context, *Handler) error { 193 return func(ctx context.Context, h *Handler) error { 194 h.Conds = ¤cycrud.Conds{} 195 if conds == nil { 196 return nil 197 } 198 if conds.EntID != nil { 199 id, err := uuid.Parse(conds.GetEntID().GetValue()) 200 if err != nil { 201 return err 202 } 203 h.Conds.EntID = &cruder.Cond{ 204 Op: conds.GetEntID().GetOp(), 205 Val: id, 206 } 207 } 208 if conds.FiatID != nil { 209 id, err := uuid.Parse(conds.GetFiatID().GetValue()) 210 if err != nil { 211 return err 212 } 213 h.Conds.FiatID = &cruder.Cond{ 214 Op: conds.GetFiatID().GetOp(), 215 Val: id, 216 } 217 } 218 if conds.FiatIDs != nil { 219 ids := []uuid.UUID{} 220 for _, id := range conds.GetFiatIDs().GetValue() { 221 _id, err := uuid.Parse(id) 222 if err != nil { 223 return err 224 } 225 ids = append(ids, _id) 226 } 227 h.Conds.FiatIDs = &cruder.Cond{ 228 Op: conds.GetFiatIDs().GetOp(), 229 Val: ids, 230 } 231 } 232 if conds.FiatName != nil { 233 h.Conds.FiatName = &cruder.Cond{ 234 Op: conds.GetFiatName().GetOp(), 235 Val: conds.GetFiatName().GetValue(), 236 } 237 } 238 return nil 239 } 240 } 241 242 func WithOffset(offset int32) func(context.Context, *Handler) error { 243 return func(ctx context.Context, h *Handler) error { 244 h.Offset = offset 245 return nil 246 } 247 } 248 249 func WithLimit(limit int32) func(context.Context, *Handler) error { 250 return func(ctx context.Context, h *Handler) error { 251 if limit == 0 { 252 limit = constant.DefaultRowLimit 253 } 254 h.Limit = limit 255 return nil 256 } 257 }