utils

package
v6.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2026 License: GPL-3.0 Imports: 22 Imported by: 5

Documentation

Overview

Package utils provides various tools such as min/max and other usful things

Index

Constants

View Source
const EarliestEvmTs = 1438269971

EarliestEvmTs - The timestamp of the first Ethereum block in summer 2015 was this value. Since Ethereum was the first EVM based blockchain, all other EVM based block chains have timestamps after this. We can use this fact to distinguish between block numbers and timestamps on the command line (any number in a block range smaller than this is a blockNumber, anything larger than this is a timestamp). This breaks when the block number gets larger than 1,4 billion, which may happen when the chain shards, but not until then.

Variables

View Source
var ErrDocFolderNotFound = errors.New("no documents folder found")

Functions

func DownloadAndStoreJSON

func DownloadAndStoreJSON[T any](url, filename string, cacheTTL time.Duration) (*T, error)

DownloadAndStoreJSON is a generic function that:

  • Downloads from the given URL if the local file is stale.
  • Stores it in the given file path.
  • Unmarshals the JSON bytes into a type T and returns a *T.

T must be a Go type compatible with the JSON structure (e.g. a struct or slice).

func FormattedCode

func FormattedCode(verbose bool, code string) string

func FormattedHash

func FormattedHash(verbose bool, code string) string

func GetCacheDir

func GetCacheDir(appDir string) (string, error)

GetCacheDir returns the user's (OS-specific) caching folder. If the folder is not found, an error is returned. If appDir is not empty, it is appended to the cacheDir and if the resulting folder does not exist, it is created.

func GetConfigFn

func GetConfigFn(appDir, fn string) (string, error)

GetConfigFn returns the user's (OS-specific) configuration folder. If the folder is not found, an error is returned. If appDir is not empty, it is appended to the configDir and if the resulting folder does not exist, it is created.

func GetDocumentsDir

func GetDocumentsDir() (string, error)

GetDocumentsDir returns the user's documents folder. The documents folder is the user's (OS-specific) home folder with the word "Documents" appended to it. If the home folder or the $HOME/Documents folder is not found, an error is returned.

func GetFields

func GetFields(t *reflect.Type, format string, header bool) (fields []string, sep string, quote string)

func GetTestChain

func GetTestChain() string

GetTestChain does not get customized per chain. We can only test against mainnet currently

func IsClientErigon

func IsClientErigon(version string) bool

IsClientErigon checks if `version` looks like Erigon

func IsFuzzing

func IsFuzzing() bool

func IsPermitted

func IsPermitted() bool

func IsServerWriter

func IsServerWriter(w io.Writer) bool

IsServerWriter tries to cast `w` into `http.ResponseWriter` and returns true if the cast was successful

func IterateOverMap

func IterateOverMap[Key comparable, Value any](ctx context.Context, errorChan chan error, target map[Key]Value, step stepFunc[Key, Value])

IterateOverMap distributes batches of `target` items to a pool of goroutines which execute `step` for every `target` item. Use benchmarks to make sure that concurrent iteration is faster than synchronous one.

func LowerIfHex

func LowerIfHex(addr string) string

func MakeFirstLowerCase

func MakeFirstLowerCase(s string) string

func MakeFirstUpperCase

func MakeFirstUpperCase(s string) string

func MustGetConfigFn

func MustGetConfigFn(appDir, fn string) string

MustGetConfigFn ignore errors on GetConfigFn

func OpenBrowser

func OpenBrowser(url string)

func PadLeft

func PadLeft(str string, totalLen int, pad rune) string

func PadNum

func PadNum(n int, totalLen int) string

func PadRight

func PadRight(str string, totalLen int, pad rune) string

func PidExists

func PidExists(pid int64) (bool, error)

func PingServer

func PingServer(serverUrl string) bool

PingServer sends a GET request to the provided URL and returns true if the server responds with a 200 status code.

func PointerOf

func PointerOf[T any](value T) *T

func RemoveAny

func RemoveAny(A, B string) string

RemoveAny returns a new string with all characters from string A that are present in string B removed. The function uses a map for efficient lookups and preserves the order of characters in A.

func ResolvePath

func ResolvePath(path string) string

ResolvePath returns an absolute path expanded for ~, $HOME or other env variables

func ResolveValidPath

func ResolveValidPath(path string) (string, error)

ResolveValidPath returns an absolute path expanded for ~, $HOME or other env variables

func SetIsFuzzing

func SetIsFuzzing(fuzz bool)

func StripColors

func StripColors(input string) string

func StripComments

func StripComments(cmd string) string

func System

func System(cmd string) int

func ToValidPath

func ToValidPath(path string) string

func Trace

func Trace()

Types

type ChainList

type ChainList struct {
	Chains    []ChainListItem `json:"chains"`
	ChainsMap map[int]*ChainListItem
}

func UpdateChainList

func UpdateChainList(configPath string) (*ChainList, error)

type ChainListItem

type ChainListItem struct {
	Name           string         `json:"name"`
	Chain          string         `json:"chain"`
	Icon           string         `json:"icon"`
	Rpc            []string       `json:"rpc"`
	Faucets        []string       `json:"faucets"`
	NativeCurrency NativeCurrency `json:"nativeCurrency"`
	InfoURL        string         `json:"infoURL"`
	ShortName      string         `json:"shortName"`
	ChainID        int            `json:"chainId"`
	NetworkID      int            `json:"networkId"`
	Explorers      []Explorer     `json:"explorers"`
}

func GetChainListItem

func GetChainListItem(configPath string, chainId int) *ChainListItem

type Explorer

type Explorer struct {
	Name     string `json:"name"`
	URL      string `json:"url"`
	Standard string `json:"standard"`
}

type NativeCurrency

type NativeCurrency struct {
	Name     string `json:"name"`
	Symbol   string `json:"symbol"`
	Decimals int    `json:"decimals"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL