-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
normalization.go
89 lines (72 loc) · 2.58 KB
/
normalization.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
package events
import (
"fmt"
"strconv"
"github.com/DefiantLabs/cosmos-indexer/config"
txtypes "github.com/DefiantLabs/cosmos-indexer/cosmos/modules/tx"
cometAbciTypes "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/types"
)
func NormalizedAttributesToAttributes(attrs []txtypes.Attribute) []types.Attribute {
list := []types.Attribute{}
for _, attr := range attrs {
lma := types.Attribute{Key: attr.Key, Value: attr.Value}
list = append(list, lma)
}
return list
}
func AttributesToNormalizedAttributes(attrs []types.Attribute) []txtypes.Attribute {
list := []txtypes.Attribute{}
for _, attr := range attrs {
lma := txtypes.Attribute{Key: attr.Key, Value: attr.Value}
list = append(list, lma)
}
return list
}
func EventAttributesToNormalizedAttributes(attrs []cometAbciTypes.EventAttribute) []txtypes.Attribute {
list := []txtypes.Attribute{}
for _, attr := range attrs {
lma := txtypes.Attribute{Key: attr.Key, Value: attr.Value}
list = append(list, lma)
}
return list
}
func StringEventstoNormalizedEvents(msgEvents types.StringEvents) (list []txtypes.LogMessageEvent) {
for _, evt := range msgEvents {
lme := txtypes.LogMessageEvent{Type: evt.Type, Attributes: AttributesToNormalizedAttributes(evt.Attributes)}
list = append(list, lme)
}
return list
}
func toNormalizedEvents(msgEvents []cometAbciTypes.Event) (list []txtypes.LogMessageEvent) {
for _, evt := range msgEvents {
lme := txtypes.LogMessageEvent{Type: evt.Type, Attributes: EventAttributesToNormalizedAttributes(evt.Attributes)}
list = append(list, lme)
}
return list
}
func ParseTxEventsToMessageIndexEvents(numMessages int, events []cometAbciTypes.Event) (types.ABCIMessageLogs, error) {
parsedLogs := make(types.ABCIMessageLogs, numMessages)
for index := range parsedLogs {
parsedLogs[index] = types.ABCIMessageLog{
MsgIndex: uint32(index),
}
}
// TODO: Fix this to be more efficient, no need to translate multiple times to hack this together
logMessageEvents := toNormalizedEvents(events)
for _, event := range logMessageEvents {
loopEvent := event
val, err := txtypes.GetValueForAttribute("msg_index", &loopEvent)
if err == nil && val != "" {
msgIndex, err := strconv.Atoi(val)
if err != nil {
config.Log.Error(fmt.Sprintf("Error parsing msg_index from event: %v", err))
return nil, err
}
if msgIndex >= 0 && msgIndex < len(parsedLogs) {
parsedLogs[msgIndex].Events = append(parsedLogs[msgIndex].Events, types.StringEvent{Type: event.Type, Attributes: NormalizedAttributesToAttributes(event.Attributes)})
}
}
}
return parsedLogs, nil
}