Fix token address from native token address in vaa-payload-parser response (#1066)

Update supported tokens

Co-authored-by: walker-16 <agpazos85@gmail.com>
This commit is contained in:
ftocal 2024-02-01 11:26:51 -03:00 committed by GitHub
parent 90f615dd0d
commit d8aeb72cb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1222 additions and 942 deletions

View File

@ -93,7 +93,7 @@ func (r *TokenResolver) GetTransferredTokenByVaa(ctx context.Context, vaa *sdk.V
return nil, nil return nil, nil
} }
token, err := createToken(result.StandardizedProperties, vaa.EmitterChain) token, err := createToken(result, vaa.EmitterChain)
if err != nil { if err != nil {
r.logger.Debug("Creating transferred token", r.logger.Debug("Creating transferred token",
zap.String("vaaId", vaa.MessageID()), zap.String("vaaId", vaa.MessageID()),
@ -104,50 +104,60 @@ func (r *TokenResolver) GetTransferredTokenByVaa(ctx context.Context, vaa *sdk.V
return token, err return token, err
} }
func createToken(s parser.StandardizedProperties, emitterChain sdk.ChainID) (*TransferredToken, error) { func createToken(p *parser.ParseVaaWithStandarizedPropertiesdResponse, emitterChain sdk.ChainID) (*TransferredToken, error) {
if !domain.ChainIdIsValid(s.TokenChain) { if !domain.ChainIdIsValid(p.StandardizedProperties.TokenChain) {
return nil, fmt.Errorf("tokenChain is invalid: %d", s.TokenChain) return nil, fmt.Errorf("tokenChain is invalid: %d", p.StandardizedProperties.TokenChain)
} }
if !domain.ChainIdIsValid(s.ToChain) { if !domain.ChainIdIsValid(p.StandardizedProperties.ToChain) {
return nil, fmt.Errorf("toChain is invalid: %d", s.ToChain) return nil, fmt.Errorf("toChain is invalid: %d", p.StandardizedProperties.ToChain)
} }
if !domain.ChainIdIsValid(emitterChain) { if !domain.ChainIdIsValid(emitterChain) {
return nil, fmt.Errorf("emitterChain is invalid: %d", emitterChain) return nil, fmt.Errorf("emitterChain is invalid: %d", emitterChain)
} }
if s.TokenAddress == "" { if p.StandardizedProperties.TokenAddress == "" {
return nil, errors.New("tokenAddress is empty") return nil, errors.New("tokenAddress is empty")
} }
if s.Amount == "" { if p.StandardizedProperties.Amount == "" {
return nil, errors.New("amount is empty") return nil, errors.New("amount is empty")
} }
address, err := sdk.StringToAddress(s.TokenAddress) addressHex, err := domain.DecodeNativeAddressToHex(p.StandardizedProperties.TokenChain, p.StandardizedProperties.TokenAddress)
if err != nil {
if p.ParsedPayload != nil && p.ParsedPayload.TokenAddress != "" {
addressHex = p.ParsedPayload.TokenAddress
} else {
return nil, fmt.Errorf("cannot decode token with tokenChain [%d] tokenAddress [%s] to hex",
p.StandardizedProperties.TokenChain, p.StandardizedProperties.TokenAddress)
}
}
address, err := sdk.StringToAddress(addressHex)
if err != nil { if err != nil {
return nil, err return nil, err
} }
n := new(big.Int) n := new(big.Int)
n, ok := n.SetString(s.Amount, 10) n, ok := n.SetString(p.StandardizedProperties.Amount, 10)
if !ok { if !ok {
return nil, fmt.Errorf("amount [%s] is not a number", s.Amount) return nil, fmt.Errorf("amount [%s] is not a number", p.StandardizedProperties.Amount)
} }
appId := domain.AppIdUnkonwn appId := domain.AppIdUnkonwn
if len(s.AppIds) > 0 { if len(p.StandardizedProperties.AppIds) > 0 {
appId = s.AppIds[0] appId = p.StandardizedProperties.AppIds[0]
} }
return &TransferredToken{ return &TransferredToken{
AppId: appId, AppId: appId,
FromChain: emitterChain, FromChain: emitterChain,
ToChain: s.ToChain, ToChain: p.StandardizedProperties.ToChain,
TokenAddress: address, TokenAddress: address,
TokenChain: s.TokenChain, TokenChain: p.StandardizedProperties.TokenChain,
Amount: n, Amount: n,
}, nil }, nil
} }

View File

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

View File

@ -90,7 +90,12 @@ func TranslateEmitterAddress(chainID sdk.ChainID, address string) (string, error
sdk.ChainIDCelo, sdk.ChainIDCelo,
sdk.ChainIDMoonbeam, sdk.ChainIDMoonbeam,
sdk.ChainIDArbitrum, sdk.ChainIDArbitrum,
sdk.ChainIDOptimism: sdk.ChainIDOptimism,
sdk.ChainIDSepolia,
sdk.ChainIDArbitrumSepolia,
sdk.ChainIDBaseSepolia,
sdk.ChainIDOptimismSepolia,
sdk.ChainIDHolesky:
return "0x" + hex.EncodeToString(addressBytes[12:]), nil return "0x" + hex.EncodeToString(addressBytes[12:]), nil
@ -223,7 +228,11 @@ func EncodeTrxHashByChainID(chainID sdk.ChainID, txHash []byte) (string, error)
case sdk.ChainIDWormchain: case sdk.ChainIDWormchain:
//TODO: check if this is correct //TODO: check if this is correct
return hex.EncodeToString(txHash), nil return hex.EncodeToString(txHash), nil
case sdk.ChainIDSepolia: case sdk.ChainIDSepolia,
sdk.ChainIDArbitrumSepolia,
sdk.ChainIDBaseSepolia,
sdk.ChainIDOptimismSepolia,
sdk.ChainIDHolesky:
return hex.EncodeToString(txHash), nil return hex.EncodeToString(txHash), nil
default: default:
return hex.EncodeToString(txHash), fmt.Errorf("unknown chain id: %d", chainID) return hex.EncodeToString(txHash), fmt.Errorf("unknown chain id: %d", chainID)
@ -260,7 +269,12 @@ func DecodeNativeAddressToHex(chainID sdk.ChainID, address string) (string, erro
sdk.ChainIDCelo, sdk.ChainIDCelo,
sdk.ChainIDMoonbeam, sdk.ChainIDMoonbeam,
sdk.ChainIDArbitrum, sdk.ChainIDArbitrum,
sdk.ChainIDOptimism: sdk.ChainIDOptimism,
sdk.ChainIDSepolia,
sdk.ChainIDArbitrumSepolia,
sdk.ChainIDBaseSepolia,
sdk.ChainIDOptimismSepolia,
sdk.ChainIDHolesky:
return address, nil return address, nil

File diff suppressed because it is too large Load Diff