Skip to content

Commit 210f9c3

Browse files
authored
refactor(baserpc): improve transaction address handling and logging (#18)
1 parent 221336c commit 210f9c3

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

internal/baserpc/baserpc.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/hex"
88
"fmt"
99
"math/big"
10+
"strings"
1011
"time"
1112

1213
"github.com/ethereum/go-ethereum/accounts/abi/bind"
@@ -139,12 +140,17 @@ func (b *BaseRPC) GetTransactionsByAddress(address string, fromTxId string) ([]m
139140
// Process transactions in batches to avoid block range limitation
140141
const maxBlockRange = 10000
141142
var allTransactions []model.OnchainIcyTransaction
143+
address = strings.ToLower(address)
142144

143145
for currentStart := startBlock; currentStart <= latestBlock; currentStart += maxBlockRange {
144146
currentEnd := currentStart + maxBlockRange
145147
if currentEnd > latestBlock {
146148
currentEnd = latestBlock
147149
}
150+
b.logger.Info("[GetTransactionsByAddress]", map[string]string{
151+
"startBlock": fmt.Sprintf("%d", currentStart),
152+
"endBlock": fmt.Sprintf("%d", currentEnd),
153+
})
148154

149155
// Prepare filter options for current block range
150156
opts := &bind.FilterOpts{
@@ -170,19 +176,27 @@ func (b *BaseRPC) GetTransactionsByAddress(address string, fromTxId string) ([]m
170176
var transactions []model.OnchainIcyTransaction
171177
for iterator.Next() {
172178
event := iterator.Event
179+
from := strings.ToLower(event.From.Hex())
180+
to := strings.ToLower(event.To.Hex())
173181

174182
// Skip if neither from nor to address is the target address, only interested in transactions related to the contract address
175-
if event.From.Hex() != address && event.To.Hex() != address {
183+
if from != address && to != address {
184+
b.logger.Info("[GetTransactionsByAddress] skipping event", map[string]string{
185+
"from": event.From.Hex(),
186+
"to": event.To.Hex(),
187+
"address": address,
188+
"value": event.Value.String(),
189+
})
176190
continue
177191
}
178192

179193
// Determine transaction type
180194
var txType model.TransactionType
181195
var otherAddress common.Address
182-
if event.From.Hex() == address {
196+
if from == address {
183197
txType = model.Out
184198
otherAddress = event.To
185-
} else if event.To.Hex() == address {
199+
} else if to == address {
186200
txType = model.In
187201
otherAddress = event.From
188202
}
@@ -208,8 +222,17 @@ func (b *BaseRPC) GetTransactionsByAddress(address string, fromTxId string) ([]m
208222
})
209223
}
210224

225+
b.logger.Info("[GetTransactionsByAddress] found transactions", map[string]string{
226+
"len": fmt.Sprintf("%d", len(transactions)),
227+
})
228+
211229
// Append batch transactions to all transactions
212230
allTransactions = append(allTransactions, transactions...)
231+
232+
// limit the number of transactions to fetch
233+
if len(allTransactions) >= 100 {
234+
break
235+
}
213236
}
214237

215238
return allTransactions, nil

internal/telemetry/base.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
)
1515

1616
func (t *Telemetry) IndexIcyTransaction() error {
17+
// Prevent concurrent executions
18+
t.indexIcyTransactionMutex.Lock()
19+
defer t.indexIcyTransactionMutex.Unlock()
20+
1721
t.logger.Info("[IndexIcyTransaction] Start indexing ICY transactions...")
1822

1923
var latestTx *model.OnchainIcyTransaction
@@ -47,6 +51,10 @@ func (t *Telemetry) IndexIcyTransaction() error {
4751
if latestTx != nil {
4852
fromTxId = latestTx.TransactionHash
4953
}
54+
t.logger.Info(fmt.Sprintf("[IndexIcyTransaction] Fetching txs"), map[string]string{
55+
"fromTxId": fromTxId,
56+
"startBlock": fmt.Sprintf("%d", startBlock),
57+
})
5058

5159
// Fetch all transactions for the ICY contract
5260
allTxs, err := t.baseRpc.GetTransactionsByAddress(t.appConfig.Blockchain.ICYContractAddr, fromTxId)
@@ -105,7 +113,3 @@ func (t *Telemetry) IndexIcyTransaction() error {
105113
t.logger.Info(fmt.Sprintf("[IndexIcyTransaction] Processed %d new transactions", len(txsToStore)))
106114
return nil
107115
}
108-
109-
func (t *Telemetry) GetIcyTransactionByHash(txHash string) (*model.OnchainIcyTransaction, error) {
110-
return t.store.OnchainIcyTransaction.GetByTransactionHash(t.db, txHash)
111-
}

internal/telemetry/swap.go

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ func (t *Telemetry) ProcessSwapRequests() error {
2222
}
2323

2424
for _, req := range pendingSwapRequests {
25+
// validate icy tx
26+
_, err := t.store.OnchainIcyTransaction.GetByTransactionHash(t.db, req.IcyTx)
27+
if err != nil {
28+
t.logger.Error("[ProcessSwapRequests][GetIcyTx]", map[string]string{
29+
"error": err.Error(),
30+
"tx_hash": req.IcyTx,
31+
})
32+
continue
33+
}
2534
// Validate BTC address
2635
if err := t.validateBTCAddress(req.BTCAddress); err != nil {
2736
t.logger.Error("[ProcessSwapRequests][ValidateBTCAddress]", map[string]string{

internal/telemetry/telemetry.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package telemetry
22

33
import (
4+
"sync"
5+
46
"gorm.io/gorm"
57

68
"github.com/dwarvesf/icy-backend/internal/baserpc"
@@ -19,6 +21,9 @@ type Telemetry struct {
1921
btcRpc btcrpc.IBtcRpc
2022
baseRpc baserpc.IBaseRPC
2123
oracle oracle.IOracle
24+
25+
indexIcyTransactionMutex sync.Mutex
26+
indexBtcTransactionMutex sync.Mutex
2227
}
2328

2429
func New(

0 commit comments

Comments
 (0)