-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #546 from DefiantLabs/pharr117/osmos-v24-log-forma…
…t-change feat/Update log parsing functions to normalize new standard for Osmosis v24
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package events | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/DefiantLabs/cosmos-tax-cli/config" | ||
txtypes "github.com/DefiantLabs/cosmos-tax-cli/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 | ||
} |