-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathorm.go
537 lines (481 loc) · 19.9 KB
/
orm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package logpoller
import (
"database/sql"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
type ORM struct {
chainID *big.Int
q pg.Q
}
// NewORM creates an ORM scoped to chainID.
func NewORM(chainID *big.Int, db *sqlx.DB, lggr logger.Logger, cfg pg.QConfig) *ORM {
namedLogger := lggr.Named("Configs")
q := pg.NewQ(db, namedLogger, cfg)
return &ORM{
chainID: chainID,
q: q,
}
}
// InsertBlock is idempotent to support replays.
func (o *ORM) InsertBlock(h common.Hash, n int64, t time.Time, qopts ...pg.QOpt) error {
q := o.q.WithOpts(qopts...)
err := q.ExecQ(`INSERT INTO evm_log_poller_blocks (evm_chain_id, block_hash, block_number, block_timestamp, created_at)
VALUES ($1, $2, $3, $4, NOW()) ON CONFLICT DO NOTHING`, utils.NewBig(o.chainID), h[:], n, t)
return err
}
// InsertFilter is idempotent.
//
// Each address/event pair must have a unique job id, so it may be removed when the job is deleted.
// If a second job tries to overwrite the same pair, this should fail.
func (o *ORM) InsertFilter(filter Filter, qopts ...pg.QOpt) (err error) {
q := o.q.WithOpts(qopts...)
addresses := make([][]byte, 0)
events := make([][]byte, 0)
for _, addr := range filter.Addresses {
addresses = append(addresses, addr.Bytes())
}
for _, ev := range filter.EventSigs {
events = append(events, ev.Bytes())
}
return q.ExecQ(`INSERT INTO evm_log_poller_filters
(name, evm_chain_id, retention, created_at, address, event)
SELECT * FROM
(SELECT $1, $2::NUMERIC, $3::BIGINT, NOW()) x,
(SELECT unnest($4::BYTEA[]) addr) a,
(SELECT unnest($5::BYTEA[]) ev) e
ON CONFLICT (name, evm_chain_id, address, event) DO UPDATE SET retention=$3::BIGINT;`,
filter.Name, utils.NewBig(o.chainID), filter.Retention, addresses, events)
}
// DeleteFilter removes all events,address pairs associated with the Filter
func (o *ORM) DeleteFilter(name string, qopts ...pg.QOpt) error {
q := o.q.WithOpts(qopts...)
return q.ExecQ(`DELETE FROM evm_log_poller_filters WHERE name = $1 AND evm_chain_id = $2`, name, utils.NewBig(o.chainID))
}
// LoadFiltersForChain returns all filters for this chain
func (o *ORM) LoadFilters(qopts ...pg.QOpt) (map[string]Filter, error) {
q := o.q.WithOpts(qopts...)
rows := make([]Filter, 0)
err := q.Select(&rows, `SELECT name,
ARRAY_AGG(DISTINCT address)::BYTEA[] AS addresses,
ARRAY_AGG(DISTINCT event)::BYTEA[] AS event_sigs,
MAX(retention) AS retention
FROM evm_log_poller_filters WHERE evm_chain_id = $1
GROUP BY name`, utils.NewBig(o.chainID))
filters := make(map[string]Filter)
for _, filter := range rows {
filters[filter.Name] = filter
}
return filters, err
}
func (o *ORM) SelectBlockByHash(h common.Hash, qopts ...pg.QOpt) (*LogPollerBlock, error) {
q := o.q.WithOpts(qopts...)
var b LogPollerBlock
if err := q.Get(&b, `SELECT * FROM evm_log_poller_blocks WHERE block_hash = $1 AND evm_chain_id = $2`, h, utils.NewBig(o.chainID)); err != nil {
return nil, err
}
return &b, nil
}
func (o *ORM) SelectBlockByNumber(n int64, qopts ...pg.QOpt) (*LogPollerBlock, error) {
q := o.q.WithOpts(qopts...)
var b LogPollerBlock
if err := q.Get(&b, `SELECT * FROM evm_log_poller_blocks WHERE block_number = $1 AND evm_chain_id = $2`, n, utils.NewBig(o.chainID)); err != nil {
return nil, err
}
return &b, nil
}
func (o *ORM) SelectLatestBlock(qopts ...pg.QOpt) (*LogPollerBlock, error) {
q := o.q.WithOpts(qopts...)
var b LogPollerBlock
if err := q.Get(&b, `SELECT * FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1`, utils.NewBig(o.chainID)); err != nil {
return nil, err
}
return &b, nil
}
func (o *ORM) SelectLatestLogEventSigWithConfs(eventSig common.Hash, address common.Address, confs int, qopts ...pg.QOpt) (*Log, error) {
q := o.q.WithOpts(qopts...)
var l Log
if err := q.Get(&l, `SELECT * FROM evm_logs
WHERE evm_chain_id = $1
AND event_sig = $2
AND address = $3
AND (block_number + $4) <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1)
ORDER BY (block_number, log_index) DESC LIMIT 1`, utils.NewBig(o.chainID), eventSig, address, confs); err != nil {
return nil, err
}
return &l, nil
}
// DeleteBlocksAfter delete all blocks after and including start.
func (o *ORM) DeleteBlocksAfter(start int64, qopts ...pg.QOpt) error {
q := o.q.WithOpts(qopts...)
return q.ExecQ(`DELETE FROM evm_log_poller_blocks WHERE block_number >= $1 AND evm_chain_id = $2`, start, utils.NewBig(o.chainID))
}
// DeleteBlocksBefore delete all blocks before and including end.
func (o *ORM) DeleteBlocksBefore(end int64, qopts ...pg.QOpt) error {
q := o.q.WithOpts(qopts...)
_, err := q.Exec(`DELETE FROM evm_log_poller_blocks WHERE block_number <= $1 AND evm_chain_id = $2`, end, utils.NewBig(o.chainID))
return err
}
func (o *ORM) DeleteLogsAfter(start int64, qopts ...pg.QOpt) error {
q := o.q.WithOpts(qopts...)
return q.ExecQ(`DELETE FROM evm_logs WHERE block_number >= $1 AND evm_chain_id = $2`, start, utils.NewBig(o.chainID))
}
type Exp struct {
Address common.Address
EventSig common.Hash
Expiration time.Time
TimeNow time.Time
ShouldDelete bool
}
func (o *ORM) DeleteExpiredLogs(qopts ...pg.QOpt) error {
qopts = append(qopts, pg.WithLongQueryTimeout())
q := o.q.WithOpts(qopts...)
return q.ExecQ(`WITH r AS
( SELECT address, event, MAX(retention) AS retention
FROM evm_log_poller_filters WHERE evm_chain_id=$1
GROUP BY evm_chain_id,address, event HAVING NOT 0 = ANY(ARRAY_AGG(retention))
) DELETE FROM evm_logs l USING r
WHERE l.evm_chain_id = $1 AND l.address=r.address AND l.event_sig=r.event
AND l.created_at <= STATEMENT_TIMESTAMP() - (r.retention / 10^9 * interval '1 second')`, // retention is in nanoseconds (time.Duration aka BIGINT)
utils.NewBig(o.chainID))
}
// InsertLogs is idempotent to support replays.
func (o *ORM) InsertLogs(logs []Log, qopts ...pg.QOpt) error {
for _, log := range logs {
if o.chainID.Cmp(log.EvmChainId.ToInt()) != 0 {
return errors.Errorf("invalid chainID in log got %v want %v", log.EvmChainId.ToInt(), o.chainID)
}
}
q := o.q.WithOpts(qopts...)
batchInsertSize := 4000
for i := 0; i < len(logs); i += batchInsertSize {
start, end := i, i+batchInsertSize
if end > len(logs) {
end = len(logs)
}
err := q.ExecQNamed(`INSERT INTO evm_logs
(evm_chain_id, log_index, block_hash, block_number, block_timestamp, address, event_sig, topics, tx_hash, data, created_at) VALUES
(:evm_chain_id, :log_index, :block_hash, :block_number, :block_timestamp, :address, :event_sig, :topics, :tx_hash, :data, NOW()) ON CONFLICT DO NOTHING`, logs[start:end])
if err != nil {
return err
}
}
return nil
}
func (o *ORM) SelectLogsByBlockRange(start, end int64) ([]Log, error) {
var logs []Log
err := o.q.Select(&logs, `
SELECT * FROM evm_logs
WHERE block_number >= $1 AND block_number <= $2 AND evm_chain_id = $3
ORDER BY (block_number, log_index, created_at)`, start, end, utils.NewBig(o.chainID))
if err != nil {
return nil, err
}
return logs, nil
}
// SelectLogsByBlockRangeFilter finds the logs in a given block range.
func (o *ORM) SelectLogsByBlockRangeFilter(start, end int64, address common.Address, eventSig common.Hash, qopts ...pg.QOpt) ([]Log, error) {
var logs []Log
q := o.q.WithOpts(qopts...)
err := q.Select(&logs, `
SELECT * FROM evm_logs
WHERE evm_logs.block_number >= $1 AND evm_logs.block_number <= $2 AND evm_logs.evm_chain_id = $3
AND address = $4 AND event_sig = $5
ORDER BY (evm_logs.block_number, evm_logs.log_index)`, start, end, utils.NewBig(o.chainID), address, eventSig.Bytes())
if err != nil {
return nil, err
}
return logs, nil
}
// SelectLogsCreatedAfter finds logs created after some timestamp.
func (o *ORM) SelectLogsCreatedAfter(eventSig []byte, address common.Address, after time.Time, confs int, qopts ...pg.QOpt) ([]Log, error) {
var logs []Log
q := o.q.WithOpts(qopts...)
err := q.Select(&logs, `
SELECT * FROM evm_logs
WHERE evm_chain_id = $1
AND address = $2
AND event_sig = $3
AND created_at > $4
AND (block_number + $5) <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1)
ORDER BY created_at ASC`, utils.NewBig(o.chainID), address, eventSig, after, confs)
if err != nil {
return nil, err
}
return logs, nil
}
// SelectLogsWithSigsByBlockRangeFilter finds the logs in the given block range with the given event signatures
// emitted from the given address.
func (o *ORM) SelectLogsWithSigsByBlockRangeFilter(start, end int64, address common.Address, eventSigs []common.Hash, qopts ...pg.QOpt) (logs []Log, err error) {
q := o.q.WithOpts(qopts...)
sigs := make([][]byte, 0, len(eventSigs))
for _, sig := range eventSigs {
sigs = append(sigs, sig.Bytes())
}
a := map[string]any{
"start": start,
"end": end,
"chainid": utils.NewBig(o.chainID),
"address": address,
"EventSigs": sigs,
}
query, args, err := sqlx.Named(
`
SELECT
*
FROM evm_logs
WHERE evm_logs.block_number BETWEEN :start AND :end
AND evm_logs.evm_chain_id = :chainid
AND evm_logs.address = :address
AND evm_logs.event_sig IN (:EventSigs)
ORDER BY (evm_logs.block_number, evm_logs.log_index)`, a)
if err != nil {
return nil, errors.Wrap(err, "sqlx Named")
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return nil, errors.Wrap(err, "sqlx In")
}
query = q.Rebind(query)
err = q.Select(&logs, query, args...)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return logs, err
}
func (o *ORM) GetBlocksRange(start uint64, end uint64, qopts ...pg.QOpt) ([]LogPollerBlock, error) {
var blocks []LogPollerBlock
q := o.q.WithOpts(qopts...)
err := q.Select(&blocks, `
SELECT * FROM evm_log_poller_blocks
WHERE block_number >= $1 AND block_number <= $2 AND evm_chain_id = $3
ORDER BY block_number ASC`, start, end, utils.NewBig(o.chainID))
if err != nil {
return nil, err
}
return blocks, nil
}
// SelectLatestLogEventSigsAddrsWithConfs finds the latest log by (address, event) combination that matches a list of Addresses and list of events
func (o *ORM) SelectLatestLogEventSigsAddrsWithConfs(fromBlock int64, addresses []common.Address, eventSigs []common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error) {
var logs []Log
sigs := concatBytes(eventSigs)
addrs := concatBytes(addresses)
q := o.q.WithOpts(qopts...)
err := q.Select(&logs, `
SELECT * FROM evm_logs WHERE (block_number, address, event_sig) IN (
SELECT MAX(block_number), address, event_sig FROM evm_logs
WHERE evm_chain_id = $1 AND
event_sig = ANY($2) AND
address = ANY($3) AND
block_number > $4 AND
block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $5
GROUP BY event_sig, address
)
ORDER BY block_number ASC
`, o.chainID.Int64(), sigs, addrs, fromBlock, confs)
if err != nil {
return nil, errors.Wrap(err, "failed to execute query")
}
return logs, nil
}
// SelectLatestBlockNumberEventSigsAddrsWithConfs finds the latest block number that matches a list of Addresses and list of events. It returns 0 if there is no matching block
func (o *ORM) SelectLatestBlockNumberEventSigsAddrsWithConfs(fromBlock int64, eventSigs []common.Hash, addresses []common.Address, confs int, qopts ...pg.QOpt) (int64, error) {
var blockNumber int64
sigs := concatBytes(eventSigs)
addrs := concatBytes(addresses)
q := o.q.WithOpts(qopts...)
err := q.Get(&blockNumber, `
SELECT COALESCE(MAX(block_number), 0) FROM evm_logs
WHERE evm_chain_id = $1 AND
event_sig = ANY($2) AND
address = ANY($3) AND
block_number > $4 AND
block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $5`,
o.chainID.Int64(), sigs, addrs, fromBlock, confs)
if err != nil {
return 0, err
}
return blockNumber, nil
}
func (o *ORM) SelectDataWordRange(address common.Address, eventSig common.Hash, wordIndex int, wordValueMin, wordValueMax common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error) {
var logs []Log
q := o.q.WithOpts(qopts...)
err := q.Select(&logs,
`SELECT * FROM evm_logs
WHERE evm_logs.evm_chain_id = $1
AND address = $2 AND event_sig = $3
AND substring(data from 32*$4+1 for 32) >= $5
AND substring(data from 32*$4+1 for 32) <= $6
AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $7
ORDER BY (evm_logs.block_number, evm_logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), wordIndex, wordValueMin.Bytes(), wordValueMax.Bytes(), confs)
if err != nil {
return nil, err
}
return logs, nil
}
func (o *ORM) SelectDataWordGreaterThan(address common.Address, eventSig common.Hash, wordIndex int, wordValueMin common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error) {
var logs []Log
q := o.q.WithOpts(qopts...)
err := q.Select(&logs,
`SELECT * FROM evm_logs
WHERE evm_logs.evm_chain_id = $1
AND address = $2 AND event_sig = $3
AND substring(data from 32*$4+1 for 32) >= $5
AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $6
ORDER BY (evm_logs.block_number, evm_logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), wordIndex, wordValueMin.Bytes(), confs)
if err != nil {
return nil, err
}
return logs, nil
}
func (o *ORM) SelectIndexLogsTopicGreaterThan(address common.Address, eventSig common.Hash, topicIndex int, topicValueMin common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error) {
if err := validateTopicIndex(topicIndex); err != nil {
return nil, err
}
var logs []Log
q := o.q.WithOpts(qopts...)
err := q.Select(&logs,
`SELECT * FROM evm_logs
WHERE evm_logs.evm_chain_id = $1
AND address = $2 AND event_sig = $3
AND topics[$4] >= $5
AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $6
ORDER BY (evm_logs.block_number, evm_logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValueMin.Bytes(), confs)
if err != nil {
return nil, err
}
return logs, nil
}
func (o *ORM) SelectIndexLogsTopicRange(address common.Address, eventSig common.Hash, topicIndex int, topicValueMin, topicValueMax common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error) {
if err := validateTopicIndex(topicIndex); err != nil {
return nil, err
}
var logs []Log
q := o.q.WithOpts(qopts...)
err := q.Select(&logs,
`SELECT * FROM evm_logs
WHERE evm_logs.evm_chain_id = $1
AND address = $2 AND event_sig = $3
AND topics[$4] >= $5
AND topics[$4] <= $6
AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $7
ORDER BY (evm_logs.block_number, evm_logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValueMin.Bytes(), topicValueMax.Bytes(), confs)
if err != nil {
return nil, err
}
return logs, nil
}
func (o *ORM) SelectIndexedLogs(address common.Address, eventSig common.Hash, topicIndex int, topicValues []common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error) {
if err := validateTopicIndex(topicIndex); err != nil {
return nil, err
}
q := o.q.WithOpts(qopts...)
var logs []Log
topicValuesBytes := concatBytes(topicValues)
// Add 1 since postgresql arrays are 1-indexed.
err := q.Select(&logs, `
SELECT * FROM evm_logs
WHERE evm_logs.evm_chain_id = $1
AND address = $2 AND event_sig = $3
AND topics[$4] = ANY($5)
AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $6
ORDER BY (evm_logs.block_number, evm_logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValuesBytes, confs)
if err != nil {
return nil, err
}
return logs, nil
}
// SelectIndexedLogsByBlockRangeFilter finds the indexed logs in a given block range.
func (o *ORM) SelectIndexedLogsByBlockRangeFilter(start, end int64, address common.Address, eventSig common.Hash, topicIndex int, topicValues []common.Hash, qopts ...pg.QOpt) ([]Log, error) {
if err := validateTopicIndex(topicIndex); err != nil {
return nil, err
}
var logs []Log
topicValuesBytes := concatBytes(topicValues)
q := o.q.WithOpts(qopts...)
err := q.Select(&logs, `
SELECT * FROM evm_logs
WHERE evm_logs.block_number >= $1 AND evm_logs.block_number <= $2 AND evm_logs.evm_chain_id = $3
AND address = $4 AND event_sig = $5
AND topics[$6] = ANY($7)
ORDER BY (evm_logs.block_number, evm_logs.log_index)`, start, end, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValuesBytes)
if err != nil {
return nil, err
}
return logs, nil
}
func validateTopicIndex(index int) error {
// Only topicIndex 1 through 3 is valid. 0 is the event sig and only 4 total topics are allowed
if !(index == 1 || index == 2 || index == 3) {
return errors.Errorf("invalid index for topic: %d", index)
}
return nil
}
func (o *ORM) SelectIndexedLogsCreatedAfter(address common.Address, eventSig common.Hash, topicIndex int, topicValues []common.Hash, after time.Time, confs int, qopts ...pg.QOpt) ([]Log, error) {
q := o.q.WithOpts(qopts...)
var logs []Log
topicValuesBytes := concatBytes(topicValues)
// Add 1 since postgresql arrays are 1-indexed.
err := q.Select(&logs, `
SELECT * FROM evm_logs
WHERE evm_logs.evm_chain_id = $1
AND address = $2 AND event_sig = $3
AND topics[$4] = ANY($5)
AND created_at > $6
AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $7
ORDER BY created_at ASC`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValuesBytes, after, confs)
if err != nil {
return nil, err
}
return logs, nil
}
// SelectIndexedLogsWithSigsExcluding query's for logs that have signature A and exclude logs that have a corresponding signature B, matching is done based on the topic index both logs should be inside the block range and have the minimum number of confirmations
func (o *ORM) SelectIndexedLogsWithSigsExcluding(sigA, sigB common.Hash, topicIndex int, address common.Address, startBlock, endBlock int64, confs int, qopts ...pg.QOpt) ([]Log, error) {
if err := validateTopicIndex(topicIndex); err != nil {
return nil, err
}
q := o.q.WithOpts(qopts...)
var logs []Log
err := q.Select(&logs, `
SELECT *
FROM evm_logs
WHERE evm_chain_id = $1
AND address = $2
AND event_sig = $3
AND block_number BETWEEN $6 AND $7
AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $8
EXCEPT
SELECT a.*
FROM evm_logs AS a
INNER JOIN evm_logs B
ON a.evm_chain_id = b.evm_chain_id
AND a.address = b.address
AND a.topics[$5] = b.topics[$5]
AND a.event_sig = $3
AND b.event_sig = $4
AND b.block_number BETWEEN $6 AND $7
AND b.block_number <= (SELECT COALESCE(block_number, 0) FROM evm_log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $8
ORDER BY block_number,log_index ASC
`, utils.NewBig(o.chainID), address, sigA.Bytes(), sigB.Bytes(), topicIndex+1, startBlock, endBlock, confs)
if err != nil {
return nil, err
}
return logs, nil
}
type bytesProducer interface {
Bytes() []byte
}
func concatBytes[T bytesProducer](byteSlice []T) pq.ByteaArray {
var output [][]byte
for _, b := range byteSlice {
output = append(output, b.Bytes())
}
return output
}