Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
fix: make code reusable and update sync transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
wregulski committed Feb 1, 2024
1 parent d86e177 commit 20395f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
10 changes: 5 additions & 5 deletions action_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
19 changes: 19 additions & 0 deletions sync_tx_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ***/
Expand Down
14 changes: 6 additions & 8 deletions sync_tx_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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{
Expand All @@ -259,16 +259,14 @@ 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
}

syncTx.Client().Logger().Info().
Str("txID", syncTx.ID).
Msgf("Transaction processed successfully")
// Done!
return nil
}

Expand Down

0 comments on commit 20395f5

Please sign in to comment.