diff --git a/analytics/cmd/token/token.go b/analytics/cmd/token/token.go index 2c0f3bfe..6ee9df23 100644 --- a/analytics/cmd/token/token.go +++ b/analytics/cmd/token/token.go @@ -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"` +} diff --git a/analytics/go.mod b/analytics/go.mod index 1c2d5465..ca5984d0 100644 --- a/analytics/go.mod +++ b/analytics/go.mod @@ -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 diff --git a/analytics/go.sum b/analytics/go.sum index a07c7c80..91b1fd0a 100644 --- a/analytics/go.sum +++ b/analytics/go.sum @@ -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= diff --git a/common/client/parser/parser.go b/common/client/parser/parser.go index 683f8939..d8dad554 100644 --- a/common/client/parser/parser.go +++ b/common/client/parser/parser.go @@ -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"` }