diff --git a/services/wallet/transfer/database.go b/services/wallet/transfer/database.go index b4445203a..9f93bffcb 100644 --- a/services/wallet/transfer/database.go +++ b/services/wallet/transfer/database.go @@ -492,7 +492,9 @@ func updateOrInsertTransfersDBFields(creator statementCreator, transfers []trans log.Error("can't save transfer", "b-hash", t.blockHash, "b-n", t.blockNumber, "a", t.address, "h", t.id) return err } + } + for _, t := range transfers { err = removeGasOnlyEthTransfer(creator, t) if err != nil { log.Error("can't remove gas only eth transfer", "b-hash", t.blockHash, "b-n", t.blockNumber, "a", t.address, "h", t.id, "err", err) @@ -503,18 +505,40 @@ func updateOrInsertTransfersDBFields(creator statementCreator, transfers []trans } func removeGasOnlyEthTransfer(creator statementCreator, t transferDBFields) error { - if t.transferType != w_common.EthTransfer { - query, err := creator.Prepare(`DELETE FROM transfers WHERE tx_hash = ? AND address = ? AND network_id = ? - AND account_nonce = ? AND type = 'eth' AND amount_padded128hex = '00000000000000000000000000000000'`) + if t.transferType == w_common.EthTransfer { + countQuery, err := creator.Prepare(`SELECT COUNT(*) FROM transfers WHERE tx_hash = ?`) + if err != nil { + return err + } + defer countQuery.Close() + + var count int + err = countQuery.QueryRow(t.txHash).Scan(&count) if err != nil { return err } - _, err = query.Exec(t.txHash, t.address, t.chainID, t.txNonce) - if err != nil { - return err + // If there's only one (or none), return without deleting + if count <= 1 { + log.Debug("Only one or no transfer found with the same tx_hash, skipping deletion.") + return nil } } + query, err := creator.Prepare(`DELETE FROM transfers WHERE tx_hash = ? AND address = ? AND network_id = ? AND account_nonce = ? AND type = 'eth' AND amount_padded128hex = '00000000000000000000000000000000'`) + if err != nil { + return err + } + defer query.Close() + + res, err := query.Exec(t.txHash, t.address, t.chainID, t.txNonce) + if err != nil { + return err + } + count, err := res.RowsAffected() + if err != nil { + return err + } + log.Debug("removeGasOnlyEthTransfer row deleted ", count) return nil } diff --git a/services/wallet/transfer/database_test.go b/services/wallet/transfer/database_test.go index a411aceae..c83e2b3d8 100644 --- a/services/wallet/transfer/database_test.go +++ b/services/wallet/transfer/database_test.go @@ -118,11 +118,7 @@ func TestDBGetTransfersFromBlock(t *testing.T) { require.NoError(t, saveTransfersMarkBlocksLoaded(db.client, 777, address, transfers, blockNumbers)) rst, err := db.GetTransfers(777, big.NewInt(7), nil) require.NoError(t, err) - require.Len(t, rst, 3) - - rst, err = db.GetTransfers(777, big.NewInt(2), big.NewInt(5)) - require.NoError(t, err) - require.Len(t, rst, 4) + require.Len(t, rst, 1) } func TestGetTransfersForIdentities(t *testing.T) {