Skip to content

Commit

Permalink
normalize and denormalize txHash/address (#1810) (#1815)
Browse files Browse the repository at this point in the history
Co-authored-by: walker-16 <[email protected]>
  • Loading branch information
ftocal and walker-16 committed Oct 21, 2024
1 parent 1de45fe commit 26087e6
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions common/domain/chainid.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func TranslateEmitterAddress(chainID sdk.ChainID, address string) (string, error

func NormalizeTxHashByChainId(chainID sdk.ChainID, txHash string) string {
switch chainID {
//EVMs
case sdk.ChainIDEthereum,
sdk.ChainIDBase,
sdk.ChainIDBSC,
Expand All @@ -206,8 +207,17 @@ func NormalizeTxHashByChainId(chainID sdk.ChainID, txHash string) string {
sdk.ChainIDBlast,
sdk.ChainIDXLayer,
sdk.ChainIDSnaxchain:
lowerTxHash := strings.ToLower(txHash)
return utils.Remove0x(lowerTxHash)
return utils.NormalizeHex(txHash)
case sdk.ChainIDAptos:
return utils.NormalizeHex(txHash)
case sdk.ChainIDSei:
return utils.NormalizeHex(txHash)
case sdk.ChainIDWormchain,
sdk.ChainIDInjective,
sdk.ChainIDOsmosis,
sdk.ChainIDEvmos,
sdk.ChainIDKujira:
return utils.NormalizeHex(txHash)
default:
return txHash
}
Expand Down Expand Up @@ -245,11 +255,15 @@ func NormalizeAddressByChainId(chainID sdk.ChainID, address string) string {
return utils.NormalizeHex(address)
case sdk.ChainIDAptos:
return utils.NormalizeHex(address)
case sdk.ChainIDSui:
return utils.NormalizeHex(address)
default:
return address
}
}

// DenormalizeTxHashByChainId denormalizes the transaction hash stored in the database
// to the format compatible with the API v1 response to avoid breaking changes.
func DenormalizeTxHashByChainId(chainID sdk.ChainID, txHash string) string {
switch chainID {
case sdk.ChainIDEthereum,
Expand Down Expand Up @@ -282,11 +296,36 @@ func DenormalizeTxHashByChainId(chainID sdk.ChainID, txHash string) string {
return txHash
}
return utils.DenormalizeHex(txHash)
case sdk.ChainIDAptos:
if utils.StartsWith0x(txHash) {
return txHash
}
return utils.DenormalizeHex(txHash)
case sdk.ChainIDSei:
if utils.StartsWith0x(txHash) {
txHash = utils.Remove0x(txHash)
}
return strings.ToUpper(txHash)
case sdk.ChainIDWormchain:
if utils.StartsWith0x(txHash) {
return txHash
}
return utils.DenormalizeHex(txHash)
case sdk.ChainIDInjective,
sdk.ChainIDOsmosis,
sdk.ChainIDEvmos,
sdk.ChainIDKujira:
if utils.StartsWith0x(txHash) {
txHash = utils.Remove0x(txHash)
}
return strings.ToLower(txHash)
default:
return txHash
}
}

// DenormalizeAddressByChainId denormalizes the address stored in the database
// to the format compatible with the API v1 response to avoid breaking changes.
func DenormalizeAddressByChainId(chainID sdk.ChainID, address string) string {
switch chainID {
case sdk.ChainIDEthereum,
Expand Down Expand Up @@ -319,6 +358,16 @@ func DenormalizeAddressByChainId(chainID sdk.ChainID, address string) string {
return address
}
return utils.DenormalizeHex(address)
case sdk.ChainIDAptos:
if utils.StartsWith0x(address) {
return address
}
return utils.DenormalizeHex(address)
case sdk.ChainIDSui:
if utils.StartsWith0x(address) {
return address
}
return utils.DenormalizeHex(address)
default:
return address
}
Expand Down

0 comments on commit 26087e6

Please sign in to comment.