Skip to content

Commit 11afc75

Browse files
committed
refactor: simplify transaction handling in GetTransactionsByAddress
1 parent 3a5099f commit 11afc75

4 files changed

+29
-29
lines changed

internal/baserpc/baserpc.go

+14-26
Original file line numberDiff line numberDiff line change
@@ -179,47 +179,35 @@ func (b *BaseRPC) GetTransactionsByAddress(address string, fromTxId string) ([]m
179179
from := strings.ToLower(event.From.Hex())
180180
to := strings.ToLower(event.To.Hex())
181181

182-
// Skip if neither from nor to address is the target address, only interested in transactions related to the contract 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-
})
190-
continue
182+
transaction := model.OnchainIcyTransaction{
183+
TransactionHash: event.Raw.TxHash.Hex(),
184+
Amount: event.Value.String(),
185+
BlockNumber: event.Raw.BlockNumber,
191186
}
192-
193187
// Determine transaction type
194-
var txType model.TransactionType
195-
var otherAddress common.Address
196188
if from == address {
197-
txType = model.Out
198-
otherAddress = event.To
189+
transaction.Type = model.Out
190+
transaction.ToAddress = event.To.Hex()
199191
} else if to == address {
200-
txType = model.In
201-
otherAddress = event.From
192+
transaction.Type = model.In
193+
transaction.FromAddress = event.From.Hex()
194+
} else {
195+
transaction.Type = model.Transfer
196+
transaction.FromAddress = event.From.Hex()
197+
transaction.ToAddress = event.To.Hex()
202198
}
203199

204200
// Get block time if possible
205201
block, err := b.erc20Service.client.BlockByNumber(context.Background(), big.NewInt(int64(event.Raw.BlockNumber)))
206-
var blockTime int64
207202
if err == nil {
208-
blockTime = int64(block.Time())
203+
transaction.BlockTime = int64(block.Time())
209204
} else {
210205
b.logger.Error("[GetTransactionsByAddress][BlockByNumber] cannot get block data", map[string]string{
211206
"error": err.Error(),
212207
})
213208
}
214209

215-
transactions = append(transactions, model.OnchainIcyTransaction{
216-
TransactionHash: event.Raw.TxHash.Hex(),
217-
Amount: event.Value.String(),
218-
Type: txType,
219-
OtherAddress: otherAddress.Hex(),
220-
BlockTime: blockTime,
221-
BlockNumber: event.Raw.BlockNumber,
222-
})
210+
transactions = append(transactions, transaction)
223211
}
224212

225213
b.logger.Info("[GetTransactionsByAddress] found transactions", map[string]string{

internal/model/onchain_icy_transaction.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import "time"
55
type TransactionType string
66

77
const (
8-
Out TransactionType = "out"
9-
In TransactionType = "in"
8+
Out TransactionType = "out"
9+
In TransactionType = "in"
10+
Transfer TransactionType = "transfer"
1011
)
1112

1213
type OnchainIcyTransaction struct {
@@ -18,7 +19,8 @@ type OnchainIcyTransaction struct {
1819
Type TransactionType `json:"type"`
1920
Amount string `json:"amount"`
2021
Fee string `json:"fee"`
21-
OtherAddress string `json:"other_address"`
22+
FromAddress string `json:"from_address"`
23+
ToAddress string `json:"to_address"`
2224
CreatedAt time.Time `json:"created_at"`
2325
UpdatedAt time.Time `json:"updated_at"`
2426
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Revert changes to onchain_icy_transactions table
2+
ALTER TABLE onchain_icy_transactions
3+
ADD COLUMN IF NOT EXISTS other_address VARCHAR(255),
4+
DROP COLUMN IF EXISTS to_address,
5+
DROP COLUMN IF EXISTS from_address;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Drop otheraddress column and add to_address and from_address columns
2+
ALTER TABLE onchain_icy_transactions
3+
DROP COLUMN IF EXISTS other_address,
4+
ADD COLUMN IF NOT EXISTS to_address VARCHAR(255),
5+
ADD COLUMN IF NOT EXISTS from_address VARCHAR(255);

0 commit comments

Comments
 (0)