utils

package
v0.0.0-...-f27a35c Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var POW96 = new(big.Float).SetInt(new(big.Int).Lsh(big.NewInt(1), 96))

Pre-calculate 2^96 as a big.Float for better precision

Functions

func All

func All[T any](source []T, predicate func(T) bool) bool

All tests if all of the elements of the slice match the predicate

func Any

func Any[T any](source []T, predicate func(T) bool) bool

Any tests if any of the elements of the slice match the predicate

func BigFloatToNumeric

func BigFloatToNumeric(f *big.Float) (pgtype.Numeric, error)

BigFloatToNumeric converts a big.Float to pgtype.Numeric preserving full precision including decimals

func BindAndValidate

func BindAndValidate(c echo.Context, i interface{}) error

func BlueMsg

func BlueMsg(s string) string

func CalculatePrice

func CalculatePrice(sqrtPriceX96 *big.Int, token0Decimals, token1Decimals int32, token0IsUSD bool) *big.Float

CalculatePrice converts sqrtPriceX96 to a price with proper decimal adjustments

func ColorMethod

func ColorMethod(method string) string

func Concat

func Concat[T any](first []T, other ...[]T) []T

Concat concatenates all given slices

func ConvertDecimalsToWei

func ConvertDecimalsToWei(num *pgtype.Numeric) (*big.Int, error)

func ConvertFloat8ToDecimals

func ConvertFloat8ToDecimals(num pgtype.Float8, decimals uint64) (*big.Int, error)

func ConvertNumericToDecimals

func ConvertNumericToDecimals(num *pgtype.Numeric, decimals uint8) (*big.Int, error)

func Distinct

func Distinct[T comparable](source []T) []T

Distinct returns a new slice where duplicate entries are removed

func Expand

func Expand[T any](generator func(idx int) T, count int) []T

Expand creates a slice by executing the generator function count times

func Expand2

func Expand2[T any](generator func() T, count int) []T

Expand2 creates a slice by executing the generator function count times

func Filter

func Filter[T any](source []T, predicate func(T) bool) []T

Filter returns a new slice that only contains elements that match the predicate

func FlatMap

func FlatMap[T, S any](source []T, f func(T) []S) []S

FlatMap composes Map and Flatten

func Flatten

func Flatten[T any](source [][]T) []T

Flatten flattens a slice of slices into a single slice

func ForEach

func ForEach[T any](source []T, f func(T))

ForEach performs the given function on every element of the slice

func GetFunctionName

func GetFunctionName(f interface{}) string

func GroupBy

func GroupBy[T any, K comparable](source []T, fn func(T) K) map[K][]T

GroupBy returns a map with given items each group into a slice

func HasDuplicates

func HasDuplicates[T comparable](source []T) bool

HasDuplicates returns true if a slice has duplicate entries, false otherwise.

func HexToAddress

func HexToAddress(address string) (common.Address, error)

func HttpErrorHandler

func HttpErrorHandler(err error, c echo.Context)

func IsHex

func IsHex(hex string) bool

func IsHexAddress

func IsHexAddress(address string) bool

func Last

func Last[T any](source []T) T

Last returns the last element of a slice, panics if the slice is empty

func Logf

func Logf(foreground ForegroundColor, v ...interface{}) string

func MagentaMsg

func MagentaMsg(s string) string

func Map

func Map[T, S any](source []T, f func(T) S) []S

Map maps a slice of T to a slice of S

func NormalizeAddress

func NormalizeAddress(address string) string

func NormalizeHex

func NormalizeHex(h string) ([]byte, error)

func RandomNumber

func RandomNumber(n int) (string, error)

func RandomString

func RandomString(n int) (string, error)

func Recover

func Recover(module string, event interface{}, message string)

func Reduce

func Reduce[T, S any](source []T, initial S, f func(current S, element T) S) S

Reduce performs a reduction to a single value of the source slice according to the given function

func Request

func RequestLogMiddleware

func RequestLogMiddleware() echo.MiddlewareFunc

func Reverse

func Reverse[T1 any](source []T1) []T1

Reverse returns a new slice in which the elements aree listed in reverse order

func SafeExecute

func SafeExecute(ctx context.Context, fn func() error) (err error)

SafeExecute runs a function with panic recovery Returns any error from the function or from a panic

func ScanBoolValue

func ScanBoolValue(value string) (*pgtype.Bool, error)

func ScanNumericValue

func ScanNumericValue(value string) (*pgtype.Numeric, error)

ScanValue is a generic function that converts a string value to a pgtype type

func ScanStringValue

func ScanStringValue(value string) (*pgtype.Text, error)

func ToMap

func ToMap[T1 any, T2 comparable](source []T1, lookup func(T1) T2, strictUniqueness ...bool) map[T2]T1

ToMap returns a map from the given slice with keys associated by the lookup function. Panics if strictUniqueness is set, otherwise overrides values for colliding keys.

func TryCast

func TryCast[T1 any, T2 any](source []T1) []T2

TryCast tries to cast each element of the slice from type T1 to type T2. Elements get filtered out of the cast is unsuccessful.

func While

func While[T any](source []T, f func(T) bool)

While executes the given function on every element of the slice until the function returns false

func YellowMsg

func YellowMsg(s string) string

Types

type CustomValidator

type CustomValidator struct {
	// contains filtered or unexported fields
}

CustomValidator is type setting of third party validator

func NewValidator

func NewValidator() *CustomValidator

Init validator

func (*CustomValidator) AddRules

func (cv *CustomValidator) AddRules(rules ...ValidationRule) error

func (*CustomValidator) Validate

func (cv *CustomValidator) Validate(i interface{}) error

type ErrResponse

type ErrResponse struct {
	Message  string      `json:"message"`
	Metadata interface{} `json:"metadata,omitempty"`
}

type ForegroundColor

type ForegroundColor int
const (
	Black   ForegroundColor = 30
	Red     ForegroundColor = 31
	Green   ForegroundColor = 32
	Yellow  ForegroundColor = 33
	Blue    ForegroundColor = 34
	Magenta ForegroundColor = 35
	Cyan    ForegroundColor = 36
	White   ForegroundColor = 37
)

type ListPublicResponse

type ListPublicResponse[T any] struct {
	Data  T     `json:"data"`
	Total int64 `json:"total,omitempty"`
}

func NewListResult

func NewListResult[T any](data T, total int64) *ListPublicResponse[T]

type RequestOption

type RequestOption struct {
	Method      string
	URL         string
	Body        interface{}
	ContentType string
}

type ValidationError

type ValidationError struct {
	Message  string                 `json:"message"`
	Metadata map[string]interface{} `json:"metadata"`
}

func (ValidationError) Error

func (ve ValidationError) Error() string

type ValidationRule

type ValidationRule struct {
	Tag string
	Fn  validator.Func
}

Jump to

Keyboard shortcuts

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