Documentation ¶
Index ¶
- func Apr2Apy(apr any) float64
- func Apr2ApyWithCompoundTimes[T types.Number](apr any, compoundTimes T) float64
- func Apy2Apr(apy any) float64
- func Apy2AprWithCompoundTimes[T types.Number](apy any, compoundTimes T) float64
- func BigSqrt(num *big.Int, times int) *big.Int
- func Celcius2Fahrenheit[T types.OrderedNumber](degree T) float64
- func ChecksumEthereumAddress(addr string) string
- func ConnectArray(strList []string, connector string) string
- func ContainAllOfStrs(str string, subStrs []string) bool
- func ContainAnyOfStrs(str string, subStrs []string) bool
- func ContainInArray[T comparable](target T, array []T) bool
- func ContainInArrayX(target string, array []string) bool
- func CreateKey() (privateKey string, address string)
- func CryptoRandBelow[T types.Integer](length T) int
- func CryptoRandBetween(start int, end int, times int) []int
- func CryptoRandFrom[T any](range_ []T) T
- func Fahrenheit2Celcius[T types.OrderedNumber](degree T) float64
- func FindAddressContain(times int, targets ...string) []findAddr
- func FindAddressEnd(times int, targets ...string) []findAddr
- func FindAddressStart(times int, targets ...string) []findAddr
- func FindBytesBetween(fullBytes []byte, bytesBefore []byte, bytesAfter []byte) (target [][]byte)
- func FindStringBetween(fullStr string, strBefore string, strAfter string) (target []string)
- func FormatJson(obj any) string
- func FormatJsonByte(obj any) []byte
- func GetHashAsPriv(input string) (string, string)
- func GetMonthStart(location string) time.Time
- func GetStringHash(input string) string
- func GetTimeByStr(str string, location string) time.Time
- func GetTimeRangeByType(t string, location string) (start int64, end int64)
- func GetTodayStart(location string) time.Time
- func GetWeekStart(location string) time.Time
- func GetYearStart(location string) time.Time
- func LowerFirst(str string) string
- func MathRandBelow[T types.Integer](length T) int
- func Max[T types.OrderedNumber](numbers ...T) T
- func Medium[T types.OrderedNumber](numbers ...T) float64
- func Min[T types.OrderedNumber](numbers ...T) T
- func Mode[T types.OrderedNumber](numbers ...T) []T
- func PrettifyJson(raw string) string
- func PrettyJsonPrintln(obj any)
- func Private2Address(key string) string
- func ReadCsv(filePath string) ([][]string, error)
- func ReadJson(path string, v any) error
- func ReadJsonBytes(path string) ([]byte, error)
- func ReadJsonSlice(path string) ([]string, error)
- func ReadJsonString(path string) (string, error)
- func RemoveFromArray[T comparable](array []T, toRemove T) []T
- func RemoveFromString(str string, subStr string) string
- func RemoveRepetitionInArray[T comparable](array []T) []T
- func Restart(funcToRecover func())
- func RestartAndSleep(funcToRecover func(), sleepTime time.Duration)
- func RunFuncName() string
- func SendMail(sender MailSender, info MailInfo) error
- func SendTelegramSimple(botToken string, chatId int64, text string) error
- func SeperateIntoArray(str string, seperateBy string) (res []string)
- func SimpleMarshal(obj any) []byte
- func SimpleStringify(obj any) string
- func SortSimple[T types.Ordered](ascending bool, input []T) []T
- func SortSimpleMap[S comparable, T types.Ordered](ascending bool, input map[S]T) ([]S, []T)
- func StrCamel2Underline(str string) string
- func StrUnderline2Camel(str string) string
- func StrUnderline2CamelUpperFirst(str string) string
- func StrUnderline2Seperate(str string) string
- func StrUnderline2SeperateTitle(str string) string
- func StringToTrueLocations(str string) []int64
- func Stringify(obj any) string
- func TimeFormat(timestamp int64, location string) string
- func TimeFormatTime(timestamp int64, location string) time.Time
- func TimeNowString() string
- func TimestampNow() int
- func TrueLocationsToString(trueLocs []int64) string
- func UpperFirst(str string) string
- func WriteNewCsv(filePath string, title []string, text [][]string) error
- func WriteToCsv(filePath string, title []string, text []string) error
- func WriteToCsvResume(filePath string, title []string, text []string, noRepeatTitle string) error
- type CurveStableCoinModel
- type CurveStableModeldeprecated
- type MailInfo
- type MailSender
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apr2Apy ¶ added in v0.0.5
Calculate the apy by given apr. Compound times = 365.
Example:
Apr2Apy(0.1) = 0.10515578161622718 // 10% ==> 10.52% Apr2Apy(1) = 1.7145674820220145 // 100% ==> 171.46%
NOTE:
decimal.Decimal method `Pow()` can not use float number as the input parameter, and therefore using decimal.Decimal is meaningless here.
Example ¶
fmt.Println(Apr2Apy(0.1)) fmt.Println(Apr2Apy(1))
Output: 0.10515578161622718 1.7145674820220145
func Apr2ApyWithCompoundTimes ¶ added in v0.0.5
Calculate the apy by given apr.
Example:
Apr2ApyWithCompoundTimes(0.1, 100) = 0.10511569772075302 // 10% ==> 10.51% Apr2ApyWithCompoundTimes(1, 100) = 1.704813829421529 // 100% ==> 170.48%
func Apy2Apr ¶ added in v0.0.5
Calculate the apr by given apy. Compound times = 365.
Example:
Apy2Apr(0.1) = 0.09532262476476205 // 10% ==> 9.53% Apy2Apr(1) = 0.693805752190747 // 100% ==> 69.38%
Example ¶
fmt.Println(Apy2Apr(0.1)) fmt.Println(Apy2Apr(1))
Output: 0.09532262476476205 0.693805752190747
func Apy2AprWithCompoundTimes ¶ added in v0.0.5
Calculate the apr by given apy.
Example:
Apy2AprWithCompoundTimes(0.1, 100) = 0.09535561438964724 // 10% ==> 9.54% Apy2AprWithCompoundTimes(1, 100) = 0.6955550056718884 // 100% ==> 69.56%
func BigSqrt ¶ added in v0.2.5
Sqrt for big int. Examples:
BigSqrt(100, 2) = 10 BigSqrt(1000, 3) = 10
func Celcius2Fahrenheit ¶ added in v0.0.9
func Celcius2Fahrenheit[T types.OrderedNumber](degree T) float64
func ChecksumEthereumAddress ¶
Return checksummed ethereum address.
Example ¶
fmt.Println(ChecksumEthereumAddress("0x5750bd75ADC81367D1b7510A57D79ACF777950a1")) // checksumed fmt.Println(ChecksumEthereumAddress("0x5750bd75adc81367d1b7510a57d79acf777950a1")) // all lower case fmt.Println(ChecksumEthereumAddress("0X5750BD75ADC81367D1B7510A57D79ACF777950A1")) // all upper case
Output: 0x5750bd75ADC81367D1b7510A57D79ACF777950a1 0x5750bd75ADC81367D1b7510A57D79ACF777950a1 0x5750bd75ADC81367D1b7510A57D79ACF777950a1
func ConnectArray ¶
Connect the items in an array with "connector".
Example:
ConnectArray([]string{"one", "year", "old"}, "-") = "one-year-old"
func ContainAllOfStrs ¶ added in v0.2.4
Example:
ContainAnyOfStrs("abracadabra", []string{"bra", "abr"}) = true ContainAnyOfStrs("abracadabra", []string{"bra", "tnt"}) = false
func ContainAnyOfStrs ¶
Example:
ContainAnyOfStrs("abracadabra", []string{"bra", "tnt"}) = true ContainAnyOfStrs("abracadabra", []string{"abc", "tnt"}) = false
func ContainInArray ¶
func ContainInArray[T comparable](target T, array []T) bool
If target contains in array.
Example:
ContainInArray(1,[]int{1, 2, 3}) = true ContainInArray("", []string{"1", "2", "3"}) = false ContainInArray("0", []string{"1", "2", "3"}) = false
func ContainInArrayX ¶ added in v0.0.6
If target contains in string array, ignore lower or upper.
Example:
ContainInArrayX("aa", []string{"aa", "bb", "cc"}) = true ContainInArrayX("aA", []string{"aa", "bb", "cc"}) = true
func CryptoRandBetween ¶
Choose "times" different numbers between "start" and "end".
Example:
CryptoRandBetween(1, 100, 5) = []int{1, 68, 69, 78, 79, 98, 100}
func CryptoRandFrom ¶ added in v0.2.2
func CryptoRandFrom[T any](range_ []T) T
Choose an element randomly from the given array.
func Fahrenheit2Celcius ¶ added in v0.0.9
func Fahrenheit2Celcius[T types.OrderedNumber](degree T) float64
func FindAddressContain ¶ added in v0.2.1
Find an address contain the target string.
func FindAddressEnd ¶ added in v0.2.1
Find an address end with the target string.
func FindAddressStart ¶ added in v0.2.1
Find an address start with the target string.
func FindBytesBetween ¶
Find all []byte between "bytesBefore" and "bytesAfter".
Used to seperate a long []byte into ideal array.
func FindStringBetween ¶
Find all string between "strBefore" and "strAfter".
Used to seperate a long string into ideal array.
Example:
FindStringBetween("aaaaaa", "a", "a") = []string{"", "", ""} FindStringBetween("abcabc", "a", "c") = []string{"b", "b"}
func FormatJson ¶
func FormatJsonByte ¶
func GetHashAsPriv ¶ added in v0.0.9
Get hash and use it as private key, and generate an eth address.
Return:
1st. string: The hash. If "0x" deleted, it is the private key. 2nd. string: The ethereum address generated by the hash.
Example:
GetHashAsPriv("8") = "0xe4b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10", "0xe0FC04FA2d34a66B779fd5CEe748268032a146c0"
Example ¶
priv, addr := GetHashAsPriv("100") fmt.Println("priv:", priv) fmt.Println("addr:", addr)
Output: priv: 0x8c18210df0d9514f2d2e5d8ca7c100978219ee80d3968ad850ab5ead208287b3 addr: 0xef3CDbFf03CE88F5041C82131b0cF15061b6015E
func GetMonthStart ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetStringHash ¶
Get the hash of a string.
Example:
GetStringHash("TraderJoeSupplyEvent(address,address,uint256,address)") = "0x222da511b15d564e69f7e3832eaefbe7fb9875c94fbeecdc358917f0fd0a4f9f"
func GetTimeByStr ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetTimeRangeByType ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetTodayStart ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetWeekStart ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetYearStart ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func LowerFirst ¶ added in v0.0.9
The first letter to lower case.
Example:
LowerFirst("Lower") = "lower"
func Max ¶ added in v0.0.4
func Max[T types.OrderedNumber](numbers ...T) T
Get the maximum one of the input numbers.
Example:
Max(1, 2, 3) = 3 Max[int]() = 0 Max([]int{2, 3, 4}...) = 4
Example ¶
fmt.Println(Max[int]()) fmt.Println(Max[float64](1, 2)) fmt.Println(Max(1, 2, 3, 4, 5)) fmt.Println(Max([]int{2, 3, 4}...))
Output: 0 2 5 4
func Medium ¶ added in v0.2.3
func Medium[T types.OrderedNumber](numbers ...T) float64
Get the medium number.
Example ¶
list := []int{1, 2, 5, 4, 3} fmt.Println(Medium(list...)) list = []int{1, 2, 5, 4, 3, 2} fmt.Println(Medium(list...))
Output: 3 2.5
func Min ¶ added in v0.0.4
func Min[T types.OrderedNumber](numbers ...T) T
Get the minimum one of the input numbers.
Example:
Min(1, 2, 3) = 1 Min[int]() = 0 Min([]int{2, 3, 4}...) = 2
Example ¶
fmt.Println(Min[int]()) fmt.Println(Min[float64](1, 2)) fmt.Println(Min(1, 2, 3, 4, 5)) fmt.Println(Min([]int{2, 3, 4}...))
Output: 0 1 1 2
func Mode ¶ added in v0.2.3
func Mode[T types.OrderedNumber](numbers ...T) []T
Get the mode(math).
Example ¶
list := []int{1, 2, 5, 4, 3, 2} fmt.Println(Mode(list...)) list = []int{1, 2, 5, 4, 3, 2, 3, 4} fmt.Println(Mode(list...))
Output: [2] [2 3 4]
func PrettifyJson ¶
func PrettyJsonPrintln ¶
func PrettyJsonPrintln(obj any)
func Private2Address ¶ added in v0.0.9
From private key to address.
Example ¶
fmt.Println(Private2Address("3fe74272f214ddb0d182a9ae68e3ee844d6f61d04f6e7827aad972e071a31f81"))
Output: 0x5750bd75ADC81367D1b7510A57D79ACF777950a1
func ReadJsonBytes ¶ added in v0.2.4
Read json file into []byte.
func ReadJsonSlice ¶
Read json file into []byte and unmarshal into []string.
func ReadJsonString ¶ added in v0.2.4
Read json file into []byte and convert into string.
func RemoveFromArray ¶
func RemoveFromArray[T comparable](array []T, toRemove T) []T
Remove sth from an array and return the new one.
Example:
RemoveFromArray([]string{"aa", "bb"}, "aa") = []string{"bb"} RemoveFromArray([]uint64{1}, 1) = []uint64{}
func RemoveFromString ¶
Remove the first subStr in str.
func RemoveRepetitionInArray ¶
func RemoveRepetitionInArray[T comparable](array []T) []T
Remove repetiton in an array and return the new one.
Example:
RemoveRepetitionInArray([]string{"aa", "bb", "bb", "BB"}) = []string{"aa", "bb", "BB"} RemoveRepetitionInArray([]int{1, 2, 3, 3}) = []int{1, 2, 3}
func RestartAndSleep ¶
Recover from panic and restart after waiting for a while.
func RunFuncName ¶ added in v0.0.8
func RunFuncName() string
Get the function name running now.
Example ¶
fmt.Println(RunFuncName())
Output: github.com/0xVanfer/utils.ExampleRunFuncName
func SendMail ¶ added in v0.0.7
func SendMail(sender MailSender, info MailInfo) error
Deprecated. Will use new structure later.
func SendTelegramSimple ¶ added in v0.0.7
Send simple telegram message.
Param:
botToken: Telegram bot token, you can search for `BotFather` in telegram and generate your own bot. chatId: If you are sending a message to a user, chatId is his id, a possitive integer, like 12345678, which the user can get by starting `userinfobot`. If you are sending a message to a group, chatId is a negative integer.
NOTE:
Can only send simple text. Not support `\` like `\n`.
func SeperateIntoArray ¶
Seperate a string into an array, the seperater will be deleted.
Example:
SeperateIntoArray("one-year-old", "-") = []string{"one", "year", "old"}
func SimpleMarshal ¶
func SimpleStringify ¶
func SortSimple ¶ added in v0.2.3
Simple sort. Do not need to define Len(), Less(), Swap().
Should not be used to sort too much amount of values. If the length is over 12, sort.Sort() suggested.
func SortSimpleMap ¶ added in v0.2.3
func SortSimpleMap[S comparable, T types.Ordered](ascending bool, input map[S]T) ([]S, []T)
Simple sort by map value. Do not need to define Len(), Less(), Swap().
Should not be used to sort too much amount of values. If the length is over 12, sort.Sort() suggested.
Returns the ordered keys and ordered values.
func StrCamel2Underline ¶ added in v0.0.9
Return underline case of str.
Example:
StrUnderline2Camel("aBC") = "a_b_c" StrUnderline2Camel("ABC") = "a_b_c"
NOTE:
Using both `StrUnderline2Camel()` and `StrCamel2Underline()` (regardless of order) will probably not get the original string!
Example ¶
fmt.Println(StrCamel2Underline("aaBBC")) fmt.Println(StrCamel2Underline("A")) fmt.Println(StrCamel2Underline("bASccccSS"))
Output: aa_b_b_c a b_a_scccc_s_s
func StrUnderline2Camel ¶ added in v0.0.9
Return camel case of str. The first letter is lower case.
Example:
StrUnderline2Camel("a_b_c") = "aBC"
NOTE:
Using both `StrUnderline2Camel()` and `StrCamel2Underline()` (regardless of order) will probably not get the original string!
Example ¶
fmt.Println(StrUnderline2Camel("")) fmt.Println(StrUnderline2Camel("a")) fmt.Println(StrUnderline2Camel("a_b")) fmt.Println(StrUnderline2Camel("a_b_ccc_ddd")) // Output // // a // aB // aBCccDdd
Output:
func StrUnderline2CamelUpperFirst ¶ added in v0.0.9
Return camel case of str. The first letter is upper case.
Example:
StrUnderline2Camel("a_b_c") = "ABC"
NOTE:
Using both `StrUnderline2Camel()` and `StrCamel2Underline()` (regardless of order) will probably not get the original string!
func StrUnderline2Seperate ¶ added in v0.0.9
Return seperated words of str.
Example:
StrUnderline2Seperate("a_b_c") = "a b c"
Example ¶
fmt.Println(StrUnderline2Seperate("")) fmt.Println(StrUnderline2Seperate("a")) fmt.Println(StrUnderline2Seperate("a_b")) fmt.Println(StrUnderline2Seperate("a_b_ccc_ddd"))
Output: a a b a b ccc ddd
func StrUnderline2SeperateTitle ¶ added in v0.0.9
Return title case seperated words of str.
Example:
StrUnderline2SeperateTitle("a_b_c") = "A B C"
Example ¶
fmt.Println(StrUnderline2SeperateTitle("")) fmt.Println(StrUnderline2SeperateTitle("a")) fmt.Println(StrUnderline2SeperateTitle("a_b")) fmt.Println(StrUnderline2SeperateTitle("a_b_ccc_ddd")) // Output // // A // A B // A B Ccc Ddd
Output:
func StringToTrueLocations ¶
Consider a map: map[loc]=bool.
Convert the coded string into locations.
Example:
StringToTrueLocations("1432351") = []int64{0, 1, 2, 3, 4, 8, 9, 11, 12, 14, 15, 16, 18, 20}
Example ¶
fmt.Println(StringToTrueLocations("1432351"))
Output: [0 1 2 3 4 8 9 11 12 14 15 16 18 20]
func TimeFormat ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func TimeFormatTime ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func TimeNowString ¶
func TimeNowString() string
func TimestampNow ¶
func TimestampNow() int
func TrueLocationsToString ¶
Consider a map: map[loc]=bool.
Use a bigInt to locate all true locations and convert into string.
Example:
TrueLocationsToString([]int64{1, 2, 333}) = "17498005798264095394980017816940970922825355447145699491406164851279623993595007385788105416184430598"
Example ¶
fmt.Println(TrueLocationsToString([]int64{1, 2, 33}))
Output: 8589934598
func UpperFirst ¶ added in v0.0.9
The first letter to upper case.
Example:
UpperFirst("upper") = "Upper"
func WriteNewCsv ¶
If file exist, will cover the old file.
func WriteToCsv ¶
Write to csv line by line.
Types ¶
type CurveStableCoinModel ¶ added in v0.2.7
type CurveStableCoinModel struct { // Input info. A int64 // Amplification. A != 0. N int64 // Token number. N >= 2. Fee float64 // Curve Fee. Usually 0.0004. // Input info. // Token amount info. Must be multiplied by 1e18. Balances []*big.Int // Pool balance of tokens. len(Balances) == N. Dx *big.Int // Swap amount of token x. X_Num int64 // The serial number of the token to be swaped. Related to `Balances`. Y_Num int64 // The serial number of the token to be swaped to. Related to `Balances`. D *big.Int // Variable D of the pool. // Output info. ExpectedFee *big.Int // Expected fee to be charged. ExpectedDy *big.Int // Expected give out amount of token y. // contains filtered or unexported fields }
Curve stable coin model.
Example:
info := &CurveStableCoinModel{ A: 50, N: 2, X_Num: 0, Y_Num: 1, Fee: 0.0004, Balances: []*big.Int{ big.NewInt(1).Mul(big.NewInt(409880), big.NewInt(1e18)), big.NewInt(1).Mul(big.NewInt(244647), big.NewInt(1e18)), }, Dx: big.NewInt(1).Mul(big.NewInt(1), big.NewInt(1e18)), } info.CalcDy() utils.PrettyJsonPrintln(info)
func (*CurveStableCoinModel) CalcDy ¶ added in v0.2.7
func (details *CurveStableCoinModel) CalcDy()
Calculate everything.
type CurveStableModel
deprecated
added in
v0.2.6
type CurveStableModel struct { // Input info. A int64 // Amplification. // Input info. // Token amount info. Must be multiplied by 1e18. BalanceX *big.Int // Pool balance of token x. BalanceY *big.Int // Pool balance of token y. Dx *big.Int // Swap amount of token x. // Output info. D *big.Int // Variable D of the pool. ExpectedDy *big.Int // Expected give out amount of token y. Slippage float64 // The slippage }
Deprecated: Use CurveStableCoinModel instead.
func (*CurveStableModel) CalcDy ¶ added in v0.2.6
func (details *CurveStableModel) CalcDy()
Calculate D of the pool, expected dy and slippage of the swap. A, BalanceX, BalanceY must not be nil.
type MailSender ¶ added in v0.0.7
type MailSender struct { Address string `gorm:"column:address" json:"address"` // Sender email address. Nickname string `gorm:"column:nickname" json:"nickname"` // Define sender nickname. Password string `gorm:"column:password" json:"password"` // Mail password or token. Host string `gorm:"column:host" json:"host"` // smtp.xxx.com Port string `gorm:"column:port" json:"port"` // 465 }
Sender info.