Skip to content

Commit

Permalink
feat: added back JoinMessageParser function
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM authored and MonikaCat committed Dec 22, 2023
1 parent 6d05191 commit 073b9b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
34 changes: 11 additions & 23 deletions modules/messages/account_parser.go → modules/messages/types.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package messages

import (
"fmt"

"github.com/gogo/protobuf/proto"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/forbole/juno/v4/types"
)

// MessageNotSupported returns an error telling that the given message is not supported
func MessageNotSupported(msg sdk.Msg) error {
return fmt.Errorf("message type not supported: %s", proto.MessageName(msg))
}

// MessageAddressesParser represents a function that extracts all the
// involved addresses from a provided message (both accounts and validators)
type MessageAddressesParser = func(tx *types.Tx) ([]string, error)
Expand All @@ -25,8 +16,7 @@ var CosmosMessageAddressesParser = DefaultMessagesParser
// DefaultMessagesParser represents the default messages parser that simply returns the list
// of all the signers of a message
func DefaultMessagesParser(tx *types.Tx) ([]string, error) {
allAddressess := parseAddressesFromEvents(tx)
return allAddressess, nil
return parseAddressesFromEvents(tx), nil
}

// function to remove duplicate values
Expand All @@ -43,28 +33,26 @@ func removeDuplicates(s []string) []string {
}

func parseAddressesFromEvents(tx *types.Tx) []string {
var allAddressess []string

var addresses []string
for _, event := range tx.Events {
for _, attribute := range event.Attributes {
// check if event value is validator address
valAddresss, _ := sdk.ValAddressFromBech32(string(attribute.Value))
if valAddresss != nil {
allAddressess = append(allAddressess, valAddresss.String())
// Try parsing the address as a validator address
validatorAddress, _ := sdk.ValAddressFromBech32(string(attribute.Value))
if validatorAddress != nil {
addresses = append(addresses, validatorAddress.String())
}

// check if event value is sdk address
sdkAddress, err := sdk.AccAddressFromBech32(string(attribute.Value))
// Try parsing the address as an account address
accountAddress, err := sdk.AccAddressFromBech32(string(attribute.Value))
if err != nil {
// skip if value is not sdk address
// Skip if the address is not an account address
continue
}

allAddressess = append(allAddressess, sdkAddress.String())
addresses = append(addresses, accountAddress.String())
}

}
allInvolvedAddresses := removeDuplicates(allAddressess)

return allInvolvedAddresses
return removeDuplicates(addresses)
}
22 changes: 21 additions & 1 deletion modules/messages/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package messages

import "unicode/utf8"
import (
"unicode/utf8"

"github.com/forbole/juno/v4/types"
)

func TrimLastChar(s string) string {
r, size := utf8.DecodeLastRuneInString(s)
Expand All @@ -9,3 +13,19 @@ func TrimLastChar(s string) string {
}
return s[:len(s)-size]
}

// JoinMessageParsers joins together all the given parsers, calling them in order
func JoinMessageParsers(parsers ...MessageAddressesParser) MessageAddressesParser {
return func(tx *types.Tx) ([]string, error) {
for _, parser := range parsers {
// Try getting the addresses
addresses, _ := parser(tx)

// If some addresses are found, return them
if len(addresses) > 0 {
return addresses, nil
}
}
return DefaultMessagesParser(tx)
}
}

0 comments on commit 073b9b8

Please sign in to comment.