-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_log.go
110 lines (91 loc) · 3.09 KB
/
process_log.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
package uniswapv3factoryengine
import (
"reflect"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/soyart/superwatcher"
"github.com/soyart/superwatcher/pkg/logger"
"github.com/soyart/superwatcher/examples/demoservice/internal/domain/entity"
)
func (e *uniswapv3PoolFactoryEngine) handleGoodLog(log *types.Log) (PoolFactoryArtifact, error) {
artifact := make(PoolFactoryArtifact)
logEventKey := log.Topics[0]
for _, event := range e.poolFactoryContract.ContractEvents {
// This engine is supposed to handle more than 1 event,
// but it's not yet finished now.
if logEventKey == event.ID || event.Name == "PoolCreated" {
pool, err := mapLogToPoolCreated(e.poolFactoryContract.ContractABI, event.Name, log)
if err != nil {
return nil, errors.Wrap(err, "failed to map PoolCreated log to domain struct")
}
if pool == nil {
logger.Panic("nil pool mapped - should not happen")
}
if err := e.handlePoolCreated(pool); err != nil {
return nil, errors.Wrap(err, "failed to process poolCreated")
}
// Saves engine artifact
artifact[*pool] = PoolFactoryStateCreated
}
}
return artifact, nil
}
func (e *uniswapv3PoolFactoryEngine) handleReorgedLog(log *types.Log, artifacts []superwatcher.Artifact) (PoolFactoryArtifact, error) {
logEventKey := log.Topics[0]
// Find poolFactory artifact here
var poolArtifact PoolFactoryArtifact
for _, artifacts := range artifacts {
pa, ok := artifacts.(PoolFactoryArtifact)
if ok {
poolArtifact = pa
continue
}
if !ok {
for _, artifact := range artifacts.([]superwatcher.Artifact) {
pa, ok := artifact.(PoolFactoryArtifact)
if !ok {
e.debugger.Debug(1, "found non-pool artifact", zap.String("actual type", reflect.TypeOf(artifact).String()))
continue
}
poolArtifact = pa
}
}
}
var returnArtifact PoolFactoryArtifact
for _, event := range e.poolFactoryContract.ContractEvents {
// This engine is supposed to handle more than 1 event,
// but it's not yet finished now.
if logEventKey == event.ID || event.Name == "PoolCreated" {
pool, err := mapLogToPoolCreated(e.poolFactoryContract.ContractABI, event.Name, log)
if err != nil {
return nil, errors.Wrap(err, "failed to map PoolCreated log to domain struct")
}
returnArtifact, err = e.handleReorgedPool(pool, poolArtifact)
if err != nil {
return nil, errors.Wrap(err, "failed to handle reorged PoolCreated")
}
}
continue
}
return returnArtifact, nil
}
// In uniswapv3poolfactory case, we only revert PoolCreated in the db.
// Other service may need more elaborate HandleReorg.
func (e *uniswapv3PoolFactoryEngine) handleReorgedPool(
pool *entity.Uniswapv3PoolCreated,
poolArtifact PoolFactoryArtifact,
) (
PoolFactoryArtifact,
error,
) {
poolState := poolArtifact[*pool]
switch poolState { //nolint:gocritic
case PoolFactoryStateCreated:
if err := e.revertPoolCreated(pool); err != nil {
return nil, errors.Wrapf(err, "failed to revert poolCreated for pool %s", pool.Address.String())
}
}
poolArtifact[*pool] = PoolFactoryStateNull
return poolArtifact, nil
}