Skip to content

Commit

Permalink
Merge branch 'main' into f/rescan_tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez authored Jun 28, 2024
2 parents cb943ab + b4e614e commit f964eeb
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions helpers/web3/web3_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ import (
)

const (
// DefaultMaxWeb3ClientRetries is the default number of retries to connect to
// a web3 provider.
DefaultMaxWeb3ClientRetries = 5

shortNameSourceUri = "https://chainid.network/chains_mini.json"
// shortNameSourceUri is the URI to get the chain metadata from an external
shortNameSourceUri = "https://chainid.network/chains_mini.json"
// checkWeb3EndpointsTimeout is the timeout to check the web3 endpoints.
checkWeb3EndpointsTimeout = time.Second * 10
foundTxErrMessage = "transaction type not supported"
// foundTxErrMessage is the error message when a transaction is found but it
// is not supported.
foundTxErrMessage = "transaction type not supported"
// chainAddressFormat is the format to represent a chain address string.
chainAddressFormat = "%s:%s"
)

var notFoundTxRgx = regexp.MustCompile(`not\s[be\s|]*found`)
var (
// notFoundTxRgx is a regular expression to match the error message when a
// transaction is not found.
notFoundTxRgx = regexp.MustCompile(`not\s[be\s|]*found`)
// chainAddressRgx is a regular expression to match the chain address format.
chainAddressRgx = regexp.MustCompile(`^(.+):(.+)$`)
)

// Web3Pool struct contains a map of chainID-[]*Web3Endpoint, where
// the key is the chainID and the value is a list of Web3Endpoint. It also
Expand Down Expand Up @@ -174,12 +187,30 @@ func (nm *Web3Pool) Client(chainID uint64) (*Client, error) {
func (nps *Web3Pool) ChainAddress(chainID uint64, hexAddress string) (string, bool) {
for _, data := range nps.metadata {
if data.ChainID == chainID {
return fmt.Sprintf("%s:%s", data.ShortName, hexAddress), true
return fmt.Sprintf(chainAddressFormat, data.ShortName, hexAddress), true
}
}
return "", false
}

// AddressFrom method extracts the short name and hex address from a chain
// address string, and returns the hex address and the corresponding
// Web3Endpoint.
func (nps *Web3Pool) AddressFrom(chainAddress string) (string, *Web3Endpoint) {
if !chainAddressRgx.MatchString(chainAddress) {
return "", nil
}
parts := chainAddressRgx.FindStringSubmatch(chainAddress)
shortName := parts[1]
hexAddress := parts[2]
for _, data := range nps.metadata {
if data.ShortName == shortName {
return hexAddress, data
}
}
return "", nil
}

// String method returns a string representation of the *Web3Pool list.
func (nm *Web3Pool) String() string {
shortNames := map[string]bool{}
Expand Down

0 comments on commit f964eeb

Please sign in to comment.