naco-api/internal/server/settlement.go
Nacorid 9825c45350
fix(server): surface and log settlement persistence errors
persistSettlement now returns the write error instead of swallowing it, and
both the HTTP and MCP settlement hooks log failures instead of silently
dropping them. Also remove the dead function_runtime_ms webhook field and a
stale log value.
2026-09-13 22:24:37 +00:00

78 lines
2 KiB
Go

package server
import (
"context"
"maps"
"strings"
log "github.com/nacorid/logger"
"github.com/nacorid/naco-api/internal/store"
x402v2 "github.com/x402-foundation/x402/go/v2"
)
func settleHook(ctx context.Context, db *store.Queries, transaction, amount, asset, network, to, from string) error {
ret, err := db.SaveTransaction(ctx, store.SaveTransactionParams{
X402TransactionHash: transaction,
Amount: amount,
Asset: asset,
Network: network,
Recipient: to,
Sender: from,
})
var id int64 = 0
if len(ret) > 0 {
id = ret[0].ID
}
fields := log.Fields{
"transaction_id": id,
"X402_transaction_hash": transaction,
"amount": amount,
"asset": asset,
"network": network,
"recipient": to,
"sender": from,
}
if err != nil {
log.WithFields(fields).ErrorWithContext(ctx, "Failed to save transaction", "error", err)
return err
}
log.WithFields(fields).DebugWithContext(ctx, "Saved transaction")
return nil
}
func (s *Server) buildRequirements(amount string, extra map[string]any, scheme string, timeout int) []x402v2.PaymentRequirements {
requirements := make([]x402v2.PaymentRequirements, 0, len(s.activeNetworks))
for _, info := range s.activeNetworks {
mergedExtra := make(map[string]any)
maps.Copy(mergedExtra, extra)
maps.Copy(mergedExtra, info.Extra)
requirements = append(requirements, x402v2.PaymentRequirements{
Scheme: scheme,
Network: info.Network,
Asset: info.Asset,
Amount: amount,
PayTo: s.x402Wallet,
MaxTimeoutSeconds: timeout,
Extra: mergedExtra,
})
}
return requirements
}
func parseActiveNetworks(raw string) []string {
parts := strings.Split(raw, ",")
var slugs []string
for _, part := range parts {
trimmed := strings.ToLower(strings.TrimSpace(part))
if trimmed != "" {
slugs = append(slugs, trimmed)
}
}
return slugs
}