diff --git a/action_transaction.go b/action_transaction.go index 01d0f1f5..74aaa3cd 100644 --- a/action_transaction.go +++ b/action_transaction.go @@ -403,17 +403,17 @@ func (c *Client) UpdateTransaction(ctx context.Context, callbackResp *broadcast. tx, err := c.GetTransaction(ctx, "", txInfo.ID) if err != nil { - c.options.logger.Err(err).Msgf("failed to get transaction by id while processing callback: %v", txInfo.ID) + c.options.logger.Err(err).Msgf("failed to get transaction by id: %v", txInfo.ID) return err } - tx.setChainInfo(txInfo) - if err = tx.Save(ctx); err != nil { - c.options.logger.Err(err).Msgf("failed to save transaction while processing callback: %v", txInfo.ID) + syncTx, err := GetSyncTransactionByTxID(ctx, txInfo.ID, c.DefaultModelOptions()...) + if err != nil { + c.options.logger.Err(err).Msgf("failed to get sync transaction by tx id: %v", txInfo.ID) return err } - return nil + return processSyncTxSave(ctx, txInfo, syncTx, tx) } func generateTxIDFilterConditions(txIDs []string) *map[string]interface{} { diff --git a/sync_tx_repository.go b/sync_tx_repository.go index 8532521e..ecb584e3 100644 --- a/sync_tx_repository.go +++ b/sync_tx_repository.go @@ -30,6 +30,25 @@ func GetSyncTransactionByID(ctx context.Context, id string, opts ...ModelOps) (* return txs[0], nil } +// GetSyncTransactionByTxID will get a sync transaction by it's transaction id. +func GetSyncTransactionByTxID(ctx context.Context, txID string, opts ...ModelOps) (*SyncTransaction, error) { + // Get the records by status + txs, err := _getSyncTransactionsByConditions(ctx, + map[string]interface{}{ + txID: txID, + }, + nil, opts..., + ) + if err != nil { + return nil, err + } + if len(txs) != 1 { + return nil, nil + } + + return txs[0], nil +} + /*** /exported funcs ***/ /*** public unexported funcs ***/ diff --git a/sync_tx_service.go b/sync_tx_service.go index 9f707d65..d5acc738 100644 --- a/sync_tx_service.go +++ b/sync_tx_service.go @@ -221,14 +221,17 @@ func _syncTxDataFromChain(ctx context.Context, syncTx *SyncTransaction, transact } return err } + return processSyncTxSave(ctx, txInfo, syncTx, transaction) +} +func processSyncTxSave(ctx context.Context, txInfo *chainstate.TransactionInfo, syncTx *SyncTransaction, transaction *Transaction) error { if !txInfo.Valid() { syncTx.Client().Logger().Warn(). Str("txID", syncTx.ID). Msgf("txInfo is invalid, will try again later") if syncTx.Client().IsDebug() { - txInfoJSON, _ := json.Marshal(txInfo) //nolint:errchkjson // error is not needed + txInfoJSON, _ := json.Marshal(txInfo) syncTx.Client().Logger().Debug(). Str("txID", syncTx.ID). Msgf("txInfo: %s", string(txInfoJSON)) @@ -238,18 +241,15 @@ func _syncTxDataFromChain(ctx context.Context, syncTx *SyncTransaction, transact transaction.setChainInfo(txInfo) - // Create status message message := "transaction was found on-chain by " + chainstate.ProviderBroadcastClient - // Save the transaction (should NOT error) - if err = transaction.Save(ctx); err != nil { + if err := transaction.Save(ctx); err != nil { _bailAndSaveSyncTransaction( ctx, syncTx, SyncStatusError, syncActionSync, "internal", err.Error(), ) return err } - // Update the sync status syncTx.SyncStatus = SyncStatusComplete syncTx.Results.LastMessage = message syncTx.Results.Results = append(syncTx.Results.Results, &SyncResult{ @@ -259,8 +259,7 @@ func _syncTxDataFromChain(ctx context.Context, syncTx *SyncTransaction, transact StatusMessage: message, }) - // Update the sync transaction record - if err = syncTx.Save(ctx); err != nil { + if err := syncTx.Save(ctx); err != nil { _bailAndSaveSyncTransaction(ctx, syncTx, SyncStatusError, syncActionSync, "internal", err.Error()) return err } @@ -268,7 +267,6 @@ func _syncTxDataFromChain(ctx context.Context, syncTx *SyncTransaction, transact syncTx.Client().Logger().Info(). Str("txID", syncTx.ID). Msgf("Transaction processed successfully") - // Done! return nil }