ApplyPrecisionWithDBLookup attempts to fetch precision from the database and then applies it to the transaction. Falls back to transaction-defined precision or a default of 1 if DB lookup fails or precision is invalid.
(transaction *Transaction, db *sql.DB)
| 307 | // and then applies it to the transaction. Falls back to transaction-defined |
| 308 | // precision or a default of 1 if DB lookup fails or precision is invalid. |
| 309 | func ApplyPrecisionWithDBLookup(transaction *Transaction, db *sql.DB) *big.Int { |
| 310 | var dbPrecision float64 |
| 311 | var found bool |
| 312 | var err error |
| 313 | |
| 314 | if db != nil && transaction.TransactionID != "" { |
| 315 | dbPrecision, found, err = fetchTransactionPrecisionFromDB(db, transaction.TransactionID) |
| 316 | if err != nil { |
| 317 | logrus.WithError(err).WithField("transaction_id", transaction.TransactionID).Warn("error fetching precision from DB, using local/default precision") |
| 318 | // Fall through to use local or default precision |
| 319 | } |
| 320 | } else { |
| 321 | logrus.Debug("DB connection or TransactionID is nil/empty; skipping DB lookup for precision") |
| 322 | } |
| 323 | |
| 324 | if found && dbPrecision > 0 { |
| 325 | transaction.Precision = dbPrecision |
| 326 | logrus.WithFields(logrus.Fields{ |
| 327 | "transaction_id": transaction.TransactionID, |
| 328 | "precision": transaction.Precision, |
| 329 | }).Debug("using precision from DB") |
| 330 | } else { |
| 331 | if transaction.Precision == 0 { |
| 332 | logrus.WithField("transaction_id", transaction.TransactionID).Debug("precision not found in DB, defaulting to 1") |
| 333 | transaction.Precision = 1 |
| 334 | } else { |
| 335 | logrus.WithFields(logrus.Fields{ |
| 336 | "transaction_id": transaction.TransactionID, |
| 337 | "precision": transaction.Precision, |
| 338 | }).Debug("precision not found in DB, using pre-set precision") |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return applyPrecisionLogic(transaction) |
| 343 | } |
| 344 | |
| 345 | // convertPreciseToDecimal converts the precise integer amount to a decimal value |
| 346 | // by dividing by precision, storing the exact string representation |
no test coverage detected