From 3bb33b88959157a06d53a7918b7ba819ab087b7b Mon Sep 17 00:00:00 2001 From: Stephan D Date: Fri, 26 Dec 2025 01:21:16 +0100 Subject: [PATCH] fixed wallet fetcher --- .../gateway/commands/transfer/destination.go | 16 ++++----- .../gateway/commands/transfer/submit.go | 36 +++++++++---------- .../service/gateway/rpcclient/clients.go | 5 ++- .../chain/storage/mongo/store/wallets.go | 24 +++++++------ 4 files changed, 42 insertions(+), 39 deletions(-) diff --git a/api/gateway/chain/internal/service/gateway/commands/transfer/destination.go b/api/gateway/chain/internal/service/gateway/commands/transfer/destination.go index d8b3647..15b77a4 100644 --- a/api/gateway/chain/internal/service/gateway/commands/transfer/destination.go +++ b/api/gateway/chain/internal/service/gateway/commands/transfer/destination.go @@ -17,21 +17,21 @@ func resolveDestination(ctx context.Context, deps Deps, dest *chainv1.TransferDe managedRef := strings.TrimSpace(dest.GetManagedWalletRef()) external := strings.TrimSpace(dest.GetExternalAddress()) if managedRef != "" && external != "" { - deps.Logger.Warn("both managed and external destination provided") + deps.Logger.Warn("Both managed and external destination provided") return model.TransferDestination{}, merrors.InvalidArgument("destination must be managed_wallet_ref or external_address") } if managedRef != "" { wallet, err := deps.Storage.Wallets().Get(ctx, managedRef) if err != nil { - deps.Logger.Warn("destination wallet lookup failed", zap.Error(err), zap.String("managed_wallet_ref", managedRef)) + deps.Logger.Warn("Destination wallet lookup failed", zap.Error(err), zap.String("managed_wallet_ref", managedRef)) return model.TransferDestination{}, err } if !strings.EqualFold(wallet.Network, source.Network) { - deps.Logger.Warn("destination wallet network mismatch", zap.String("source_network", source.Network), zap.String("dest_network", wallet.Network)) + deps.Logger.Warn("Destination wallet network mismatch", zap.String("source_network", source.Network), zap.String("dest_network", wallet.Network)) return model.TransferDestination{}, merrors.InvalidArgument("destination wallet network mismatch") } if strings.TrimSpace(wallet.DepositAddress) == "" { - deps.Logger.Warn("destination wallet missing deposit address", zap.String("managed_wallet_ref", managedRef)) + deps.Logger.Warn("Destination wallet missing deposit address", zap.String("managed_wallet_ref", managedRef)) return model.TransferDestination{}, merrors.InvalidArgument("destination wallet missing deposit address") } return model.TransferDestination{ @@ -40,21 +40,21 @@ func resolveDestination(ctx context.Context, deps Deps, dest *chainv1.TransferDe }, nil } if external == "" { - deps.Logger.Warn("destination external address missing") + deps.Logger.Warn("Destination external address missing") return model.TransferDestination{}, merrors.InvalidArgument("destination is required") } if deps.Drivers == nil { - deps.Logger.Warn("chain drivers missing", zap.String("network", source.Network)) + deps.Logger.Warn("Chain drivers missing", zap.String("network", source.Network)) return model.TransferDestination{}, merrors.Internal("chain drivers not configured") } chainDriver, err := deps.Drivers.Driver(source.Network) if err != nil { - deps.Logger.Warn("unsupported chain driver", zap.String("network", source.Network), zap.Error(err)) + deps.Logger.Warn("Unsupported chain driver", zap.String("network", source.Network), zap.Error(err)) return model.TransferDestination{}, merrors.InvalidArgument("unsupported chain for wallet") } normalized, err := chainDriver.NormalizeAddress(external) if err != nil { - deps.Logger.Warn("invalid external address", zap.Error(err)) + deps.Logger.Warn("Invalid external address", zap.Error(err)) return model.TransferDestination{}, err } return model.TransferDestination{ diff --git a/api/gateway/chain/internal/service/gateway/commands/transfer/submit.go b/api/gateway/chain/internal/service/gateway/commands/transfer/submit.go index 7729e72..8652c97 100644 --- a/api/gateway/chain/internal/service/gateway/commands/transfer/submit.go +++ b/api/gateway/chain/internal/service/gateway/commands/transfer/submit.go @@ -25,7 +25,7 @@ func NewSubmitTransfer(deps Deps) *submitTransferCommand { func (c *submitTransferCommand) Execute(ctx context.Context, req *chainv1.SubmitTransferRequest) gsresponse.Responder[chainv1.SubmitTransferResponse] { if err := c.deps.EnsureRepository(ctx); err != nil { - c.deps.Logger.Warn("repository unavailable", zap.Error(err)) + c.deps.Logger.Warn("Repository unavailable", zap.Error(err)) return gsresponse.Unavailable[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, err) } if req == nil { @@ -35,78 +35,78 @@ func (c *submitTransferCommand) Execute(ctx context.Context, req *chainv1.Submit idempotencyKey := strings.TrimSpace(req.GetIdempotencyKey()) if idempotencyKey == "" { - c.deps.Logger.Warn("missing idempotency key") + c.deps.Logger.Warn("Missing idempotency key") return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("idempotency_key is required")) } organizationRef := strings.TrimSpace(req.GetOrganizationRef()) if organizationRef == "" { - c.deps.Logger.Warn("missing organization ref") + c.deps.Logger.Warn("mMssing organization ref") return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("organization_ref is required")) } sourceWalletRef := strings.TrimSpace(req.GetSourceWalletRef()) if sourceWalletRef == "" { - c.deps.Logger.Warn("missing source wallet ref") + c.deps.Logger.Warn("Missing source wallet ref") return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("source_wallet_ref is required")) } amount := req.GetAmount() if amount == nil { - c.deps.Logger.Warn("missing amount") + c.deps.Logger.Warn("Missing amount") return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("amount is required")) } amountCurrency := strings.ToUpper(strings.TrimSpace(amount.GetCurrency())) if amountCurrency == "" { - c.deps.Logger.Warn("missing amount currency") + c.deps.Logger.Warn("Missing amount currency") return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("amount.currency is required")) } amountValue := strings.TrimSpace(amount.GetAmount()) if amountValue == "" { - c.deps.Logger.Warn("missing amount value") + c.deps.Logger.Warn("Missing amount value") return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("amount.amount is required")) } sourceWallet, err := c.deps.Storage.Wallets().Get(ctx, sourceWalletRef) if err != nil { if errors.Is(err, merrors.ErrNoData) { - c.deps.Logger.Warn("source wallet not found", zap.String("source_wallet_ref", sourceWalletRef)) + c.deps.Logger.Warn("Source wallet not found", zap.String("source_wallet_ref", sourceWalletRef)) return gsresponse.NotFound[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, err) } - c.deps.Logger.Warn("storage get wallet failed", zap.Error(err), zap.String("source_wallet_ref", sourceWalletRef)) + c.deps.Logger.Warn("Storage get wallet failed", zap.Error(err), zap.String("source_wallet_ref", sourceWalletRef)) return gsresponse.Auto[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, err) } if !strings.EqualFold(sourceWallet.OrganizationRef, organizationRef) { - c.deps.Logger.Warn("organization mismatch", zap.String("wallet_org", sourceWallet.OrganizationRef), zap.String("req_org", organizationRef)) + c.deps.Logger.Warn("Organization mismatch", zap.String("wallet_org", sourceWallet.OrganizationRef), zap.String("req_org", organizationRef)) return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("organization_ref mismatch with wallet")) } networkKey := strings.ToLower(strings.TrimSpace(sourceWallet.Network)) networkCfg, ok := c.deps.Networks.Network(networkKey) if !ok { - c.deps.Logger.Warn("unsupported chain", zap.String("network", networkKey)) + c.deps.Logger.Warn("Unsupported chain", zap.String("network", networkKey)) return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("unsupported chain for wallet")) } destination, err := resolveDestination(ctx, c.deps, req.GetDestination(), sourceWallet) if err != nil { if errors.Is(err, merrors.ErrNoData) { - c.deps.Logger.Warn("destination not found", zap.String("destination_wallet_ref", req.GetDestination().GetManagedWalletRef())) + c.deps.Logger.Warn("Destination not found", zap.String("destination_wallet_ref", req.GetDestination().GetManagedWalletRef())) return gsresponse.NotFound[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, err) } - c.deps.Logger.Warn("invalid destination", zap.Error(err)) + c.deps.Logger.Warn("Invalid destination", zap.Error(err)) return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, err) } fees, feeSum, err := convertFees(req.GetFees(), amountCurrency) if err != nil { - c.deps.Logger.Warn("fee conversion failed", zap.Error(err)) + c.deps.Logger.Warn("Fee conversion failed", zap.Error(err)) return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, err) } amountDec, err := decimal.NewFromString(amountValue) if err != nil { - c.deps.Logger.Warn("invalid amount", zap.Error(err)) + c.deps.Logger.Warn("Invalid amount", zap.Error(err)) return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("invalid amount")) } netDec := amountDec.Sub(feeSum) if netDec.IsNegative() { - c.deps.Logger.Warn("fees exceed amount", zap.String("amount", amountValue), zap.String("fee_sum", feeSum.String())) + c.deps.Logger.Warn("Fees exceed amount", zap.String("amount", amountValue), zap.String("fee_sum", feeSum.String())) return gsresponse.InvalidArgument[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("fees exceed amount")) } @@ -141,10 +141,10 @@ func (c *submitTransferCommand) Execute(ctx context.Context, req *chainv1.Submit saved, err := c.deps.Storage.Transfers().Create(ctx, transfer) if err != nil { if errors.Is(err, merrors.ErrDataConflict) { - c.deps.Logger.Debug("transfer already exists", zap.String("transfer_ref", transfer.TransferRef), zap.String("idempotency_key", idempotencyKey)) + c.deps.Logger.Debug("Transfer already exists", zap.String("transfer_ref", transfer.TransferRef), zap.String("idempotency_key", idempotencyKey)) return gsresponse.Success(&chainv1.SubmitTransferResponse{Transfer: toProtoTransfer(saved)}) } - c.deps.Logger.Warn("storage create failed", zap.Error(err), zap.String("transfer_ref", transfer.TransferRef)) + c.deps.Logger.Warn("Storage create failed", zap.Error(err), zap.String("transfer_ref", transfer.TransferRef)) return gsresponse.Auto[chainv1.SubmitTransferResponse](c.deps.Logger, mservice.ChainGateway, err) } diff --git a/api/gateway/chain/internal/service/gateway/rpcclient/clients.go b/api/gateway/chain/internal/service/gateway/rpcclient/clients.go index 18ac597..06bc659 100644 --- a/api/gateway/chain/internal/service/gateway/rpcclient/clients.go +++ b/api/gateway/chain/internal/service/gateway/rpcclient/clients.go @@ -156,12 +156,11 @@ func (l *loggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, erro fields := []zap.Field{ zap.String("network", l.network), - zap.String("rpc_endpoint", l.endpoint), } if len(reqBody) > 0 { fields = append(fields, zap.String("rpc_request", truncate(string(reqBody), 2048))) } - l.logger.Error("RPC request", fields...) + l.logger.Debug("RPC request", fields...) resp, err := l.base.RoundTrip(req) if err != nil { @@ -182,7 +181,7 @@ func (l *loggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, erro if len(bodyBytes) > 0 { respFields = append(respFields, zap.String("rpc_response", truncate(string(bodyBytes), 2048))) } - l.logger.Error("RPC response", respFields...) + l.logger.Debug("RPC response", respFields...) if resp.StatusCode >= 400 { l.logger.Warn("RPC response error", respFields...) } else if len(bodyBytes) == 0 { diff --git a/api/gateway/chain/storage/mongo/store/wallets.go b/api/gateway/chain/storage/mongo/store/wallets.go index aff58a4..c8d3038 100644 --- a/api/gateway/chain/storage/mongo/store/wallets.go +++ b/api/gateway/chain/storage/mongo/store/wallets.go @@ -14,6 +14,7 @@ import ( "github.com/tech/sendico/pkg/merrors" "github.com/tech/sendico/pkg/mlogger" "github.com/tech/sendico/pkg/mservice" + "github.com/tech/sendico/pkg/mutil/mzap" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.uber.org/zap" @@ -110,13 +111,16 @@ func (w *Wallets) Create(ctx context.Context, wallet *model.ManagedWallet) (*mod return wallet, nil } -func (w *Wallets) Get(ctx context.Context, walletRef string) (*model.ManagedWallet, error) { - walletRef = strings.TrimSpace(walletRef) - if walletRef == "" { - return nil, merrors.InvalidArgument("walletsStore: empty walletRef") +func (w *Wallets) Get(ctx context.Context, walletID string) (*model.ManagedWallet, error) { + walletID = strings.TrimSpace(walletID) + walletRef, err := primitive.ObjectIDFromHex(walletID) + if err != nil { + w.logger.Warn("Invalid wallet refernce", zap.Error(err), zap.String("wallet_id", walletID)) + return nil, err } wallet := &model.ManagedWallet{} - if err := w.walletRepo.FindOneByFilter(ctx, repository.Filter("walletRef", walletRef), wallet); err != nil { + if err := w.walletRepo.Get(ctx, walletRef, wallet); err != nil { + w.logger.Warn("Managed wallet not found", zap.Error(err), mzap.ObjRef("wallet_ref", walletRef)) return nil, err } return wallet, nil @@ -142,7 +146,7 @@ func (w *Wallets) List(ctx context.Context, filter model.ManagedWalletFilter) (* if oid, err := primitive.ObjectIDFromHex(cursor); err == nil { query = query.Comparison(repository.IDField(), builder.Gt, oid) } else { - w.logger.Warn("ignoring invalid wallet cursor", zap.String("cursor", cursor), zap.Error(err)) + w.logger.Warn("Ignoring invalid wallet cursor", zap.String("cursor", cursor), zap.Error(err)) } } @@ -211,13 +215,13 @@ func (w *Wallets) SaveBalance(ctx context.Context, balance *model.WalletBalance) } } -func (w *Wallets) GetBalance(ctx context.Context, walletRef string) (*model.WalletBalance, error) { - walletRef = strings.TrimSpace(walletRef) - if walletRef == "" { +func (w *Wallets) GetBalance(ctx context.Context, walletID string) (*model.WalletBalance, error) { + walletID = strings.TrimSpace(walletID) + if walletID == "" { return nil, merrors.InvalidArgument("walletsStore: empty walletRef") } balance := &model.WalletBalance{} - if err := w.balanceRepo.FindOneByFilter(ctx, repository.Filter("walletRef", walletRef), balance); err != nil { + if err := w.balanceRepo.FindOneByFilter(ctx, repository.Filter("walletRef", walletID), balance); err != nil { return nil, err } return balance, nil -- 2.49.1