Fix parsedPayload use as any type for different kind of messages (#1169)

Co-authored-by: walker-16 <agpazos85@gmail.com>
This commit is contained in:
ftocal 2024-03-04 16:49:54 -03:00 committed by GitHub
parent 6bbfa7bf23
commit 08c9427606
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
"math/big"
"github.com/mitchellh/mapstructure"
"github.com/wormhole-foundation/wormhole-explorer/common/client/parser"
"github.com/wormhole-foundation/wormhole-explorer/common/domain"
sdk "github.com/wormhole-foundation/wormhole/sdk/vaa"
@ -128,11 +129,12 @@ func createToken(p *parser.ParseVaaWithStandarizedPropertiesdResponse, emitterCh
addressHex, err := domain.DecodeNativeAddressToHex(p.StandardizedProperties.TokenChain, p.StandardizedProperties.TokenAddress)
if err != nil {
if p.ParsedPayload != nil && p.ParsedPayload.TokenAddress != "" {
addressHex = p.ParsedPayload.TokenAddress
tokenParsedPayload, err := parseTokenPayload(p.ParsedPayload)
if err != nil {
return nil, fmt.Errorf("cannot decode token with tokenChain [%d] tokenAddress [%s] to hex. %v",
p.StandardizedProperties.TokenChain, p.StandardizedProperties.TokenAddress, err)
} else {
return nil, fmt.Errorf("cannot decode token with tokenChain [%d] tokenAddress [%s] to hex",
p.StandardizedProperties.TokenChain, p.StandardizedProperties.TokenAddress)
addressHex = *tokenParsedPayload.TokenAddress
}
}
@ -161,3 +163,28 @@ func createToken(p *parser.ParseVaaWithStandarizedPropertiesdResponse, emitterCh
Amount: n,
}, nil
}
func parseTokenPayload(parsedPayload any) (*tokenParsedPayload, error) {
if parsedPayload == nil {
return nil, fmt.Errorf("parsedPayload is nil")
}
var result *tokenParsedPayload
err := mapstructure.Decode(parsedPayload, &result)
if err != nil {
return nil, fmt.Errorf("parsedPayload can not decode %v", err)
}
if result.TokenAddress == nil {
return nil, fmt.Errorf("tokenAddress in parsedPayload is nil")
}
if result.TokenChain == nil {
return nil, fmt.Errorf("tokenChain in parsedPayload is nil")
}
return result, nil
}
type tokenParsedPayload struct {
TokenAddress *string `json:"tokenAddress"`
TokenChain *int `json:"tokenChain"`
}

View File

@ -58,6 +58,7 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.4.1
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pkg/errors v0.9.1 // indirect

View File

@ -268,6 +268,8 @@ github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=

View File

@ -123,14 +123,9 @@ type StandardizedProperties struct {
Fee string `json:"fee" bson:"fee"`
}
type ParsedPayload struct {
TokenAddress string `json:"tokenAddress"`
TokenChain int `json:"tokenChain"`
}
// ParseVaaWithStandarizedPropertiesdResponse represent a parse vaa response.
type ParseVaaWithStandarizedPropertiesdResponse struct {
ParsedPayload *ParsedPayload `json:"parsedPayload"`
ParsedPayload any `json:"parsedPayload"`
StandardizedProperties StandardizedProperties `json:"standardizedProperties"`
}