From ad08a0c639b3fa50cba5123b73f03f460564297d Mon Sep 17 00:00:00 2001 From: 0xVanfer Date: Mon, 22 Jan 2024 13:17:53 +0800 Subject: [PATCH] [fix] remove deprecated --- curveStableModel_2.go | 117 ------------------------------------------ go.mod | 4 +- go.sum | 8 +-- recover.go | 4 +- sendMail.go | 37 ------------- times.go | 102 ------------------------------------ 6 files changed, 5 insertions(+), 267 deletions(-) delete mode 100644 curveStableModel_2.go delete mode 100644 sendMail.go delete mode 100644 times.go diff --git a/curveStableModel_2.go b/curveStableModel_2.go deleted file mode 100644 index 9de212e..0000000 --- a/curveStableModel_2.go +++ /dev/null @@ -1,117 +0,0 @@ -package utils - -import ( - "fmt" - "math/big" - - "github.com/0xVanfer/types" -) - -// Deprecated: Use CurveStableCoinModel instead. -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 -} - -// Calculate D of the pool, expected dy and slippage of the swap. -// A, BalanceX, BalanceY must not be nil. -func (details *CurveStableModel) CalcDy() { - if details.A == 0 { - fmt.Println("A cannot be 0.") - return - } - if details.BalanceX == nil { - fmt.Println("BalanceX cannot be nil.") - return - } - if details.BalanceY == nil { - fmt.Println("BalanceY cannot be nil.") - return - } - x1 := details.BalanceX - x2 := details.BalanceY - // balance1 + balance2; x1 add x2 - x1ax2 := big.NewInt(1).Add(x1, x2) - // balance1 * balance2; x1 mul x2 - x1mx2 := big.NewInt(1).Mul(x1, x2) - - // https://docs.balancer.fi/concepts/math/stable-math - // To get D, solve the function: - // D^3 + (16 * A * X1 * X2 - 4 * X1 * X2) * D - 16 * A * X1 * X2 * (X1 + X2) = 0 - // D^3 + p * D + q = 0 - // p = 16 * A * X1 * X2 - 4 * X1 * X2 - p := big.NewInt(1).Sub(big.NewInt(1).Mul(big.NewInt(16*details.A), x1mx2), big.NewInt(1).Mul(big.NewInt(4), x1mx2)) - // q = - 16 * A * X1 * X2 * (X1 + X2) - q := big.NewInt(1).Mul(big.NewInt(-16*details.A), big.NewInt(1).Mul(x1ax2, x1mx2)) - - // p/3 - pd3 := big.NewInt(1).Div(p, big.NewInt(3)) - // q/2 - qd2 := big.NewInt(1).Div(q, big.NewInt(2)) - - // (q/2)^2 - i2q := big.NewInt(1).Exp(qd2, big.NewInt(2), nil) - // (p/3)^3 - i3p := big.NewInt(1).Exp(pd3, big.NewInt(3), nil) - // (i2q + i3p)^1/2 - internal := BigSqrt(big.NewInt(1).Add(i2q, i3p), 2) - - // (-q/2 + internal)^1/3 - re1 := BigSqrt(big.NewInt(1).Sub(internal, qd2), 3) - // (-q/2 - internal)^1/3 = - (q/2 + internal)^1/3 - re2 := big.NewInt(1).Mul(BigSqrt(big.NewInt(1).Add(internal, qd2), 3), big.NewInt(-1)) - - // D = re1 + re2 - D := big.NewInt(1).Add(re1, re2) - details.D = D - - // To test if D is correct. leftSubRight = Left side of the function minus the right side should be 0. - // - // left := big.NewInt(1).Add(big.NewInt(1).Mul(big.NewInt(details.A*4), x1ax2), D) - // right1 := big.NewInt(1).Mul(big.NewInt(details.A*4), D) - // right2 := big.NewInt(1).Div(big.NewInt(1).Exp(D, big.NewInt(3), nil), big.NewInt(1).Mul(big.NewInt(4), x1mx2)) - // right := big.NewInt(1).Add(right1, right2) - // leftSubRight := big.NewInt(1).Sub(left, right) - // _ = leftSubRight - - // Do not calculate the dy. - dx1 := details.Dx - if (dx1 == nil) || (dx1.Cmp(big.NewInt(0)) == 0) { - return - } - // x1 + dx1 - x1adx1 := big.NewInt(1).Add(x1, dx1) - - // Trading. - // https://dev.balancer.fi/resources/pool-math/stable-math - // Swap token1 for token2. - // y^2 + (D/4A + (X1 + dX1) - D) * y - D^3 / (16A * (X1 + dX1)) = 0 - // y^2 + b * x + c = 0 - // b = D/4A + (X1 + dX1) - D - b := big.NewInt(1).Sub(big.NewInt(1).Add(big.NewInt(1).Div(D, big.NewInt(4*details.A)), x1adx1), D) - // c = -D^3 / (16A * (X1 + dX1)) - c := big.NewInt(1).Mul(big.NewInt(1).Div(big.NewInt(1).Exp(D, big.NewInt(3), nil), big.NewInt(1).Mul(big.NewInt(16*details.A), x1adx1)), big.NewInt(-1)) - - // (b^2 - 4 * a * c)^1/2; a = 1 - delta := BigSqrt(big.NewInt(1).Sub(big.NewInt(1).Exp(b, big.NewInt(2), nil), big.NewInt(1).Mul(big.NewInt(4), c)), 2) - // y = (-b + delta) / 2 - y := big.NewInt(1).Div(big.NewInt(1).Sub(delta, b), big.NewInt(2)) - // dx2 = x2 - y - dx2 := big.NewInt(1).Sub(x2, y) - details.ExpectedDy = dx2 - // Price difference of dx1 and dx2. - diff := big.NewInt(1).Abs(big.NewInt(1).Sub(dx1, dx2)) - slippage := types.ToFloat64(diff) / types.ToFloat64(big.NewInt(1).Abs(details.Dx)) - details.Slippage = slippage -} diff --git a/go.mod b/go.mod index e5761da..69abaf6 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,11 @@ module github.com/0xVanfer/utils go 1.19 require ( - github.com/0xVanfer/types v0.1.4 + github.com/0xVanfer/types v0.1.6 github.com/ethereum/go-ethereum v1.10.25 github.com/imroc/req v0.3.2 golang.org/x/crypto v0.1.0 golang.org/x/text v0.4.0 - gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df ) require ( @@ -16,5 +15,4 @@ require ( github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/shopspring/decimal v1.3.1 // indirect golang.org/x/sys v0.1.0 // indirect - gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect ) diff --git a/go.sum b/go.sum index 1a3c414..0b2b05c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/0xVanfer/types v0.1.4 h1:bQ9IMUC70QQu0rk3fcEBYdY/whFuqDT8YCtmudx/0uw= -github.com/0xVanfer/types v0.1.4/go.mod h1:8QSGeOQOStzyb8kAZXRVnxJLOJVMqI16lO/a+OBXFOc= +github.com/0xVanfer/types v0.1.6 h1:8lUHazjJ2Yir7uiTkNqsuhFH26W4Wy7VD1qw5lPXulQ= +github.com/0xVanfer/types v0.1.6/go.mod h1:8QSGeOQOStzyb8kAZXRVnxJLOJVMqI16lO/a+OBXFOc= github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k= github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= @@ -20,7 +20,3 @@ golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= -gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= -gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= -gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= diff --git a/recover.go b/recover.go index 14afc8b..e4ce7c5 100644 --- a/recover.go +++ b/recover.go @@ -14,7 +14,7 @@ func Restart(funcToRecover func()) { runtime.Callers(2, pc) f := runtime.FuncForPC(pc[1]) fmt.Println(r) - fmt.Println(TimeNowString(), f.Name(), "recovered from panic, will restart.") + fmt.Println(time.Now().UTC().Format("2006-01-02 15:04:05"), f.Name(), "recovered from panic, will restart.") // At least sleep for 1 sec, in case of endless loop. time.Sleep(time.Second) funcToRecover() @@ -29,7 +29,7 @@ func RestartAndSleep(funcToRecover func(), sleepTime time.Duration) { runtime.Callers(2, pc) f := runtime.FuncForPC(pc[1]) fmt.Println(r) - fmt.Println(TimeNowString(), f.Name(), "recovered from panic, will restart.") + fmt.Println(time.Now().UTC().Format("2006-01-02 15:04:05"), f.Name(), "recovered from panic, will restart.") if sleepTime.Seconds() < 1 { sleepTime = time.Second } diff --git a/sendMail.go b/sendMail.go deleted file mode 100644 index fbbecc2..0000000 --- a/sendMail.go +++ /dev/null @@ -1,37 +0,0 @@ -package utils - -import ( - "strconv" - - "gopkg.in/gomail.v2" -) - -// Sender info. -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 -} - -type MailInfo struct { - Receivers []string // receivers - Subject string - Body string -} - -// Deprecated. Will use new structure later. -func SendMail(sender MailSender, info MailInfo) error { - port, _ := strconv.Atoi(sender.Port) - m := gomail.NewMessage() - - m.SetHeader("From", m.FormatAddress(sender.Address, sender.Nickname)) - m.SetHeader("To", info.Receivers...) - m.SetHeader("Subject", info.Subject) - m.SetBody("text/html", info.Body) - - d := gomail.NewDialer(sender.Host, port, sender.Address, sender.Password) - err := d.DialAndSend(m) - return err -} diff --git a/times.go b/times.go deleted file mode 100644 index 8607f7d..0000000 --- a/times.go +++ /dev/null @@ -1,102 +0,0 @@ -package utils - -import ( - "strings" - "time" -) - -// The timestamp now. In seconds. Local. -func TimestampNow() int { - return int(time.Now().Unix()) -} - -// Deprecated. -func GetTimeRangeByType(t string, location string) (start int64, end int64) { - switch t { - case "day": - start = GetTodayStart(location).Unix() - end = start + 24*3600 - case "week": - start = GetWeekStart(location).Unix() - end = start + 24*3600*8 - case "month": - start = GetMonthStart(location).Unix() - end = start + 24*3600*30 - case "daybefore": - start = GetTodayStart(location).Unix() - 24*3600 - end = start + 24*3600 - case "year": - start = GetYearStart(location).Unix() - 24*3600 - end = start + 12*24*3600*30 - default: - start = GetTodayStart(location).Unix() - end = start + 24*3600 - } - - return -} - -// Deprecated. -func TimeFormat(timestamp int64, location string) string { - locationTime, _ := time.LoadLocation(location) - timeStr := time.Unix(timestamp, 0).Format("2006-01-02 15:04:05") - res, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr, locationTime) - ar := strings.Split(res.String(), " ") - return ar[0] + " " + ar[1] -} - -// Deprecated. -func TimeFormatTime(timestamp int64, location string) time.Time { - locationTime, _ := time.LoadLocation(location) - timeStr := time.Unix(timestamp, 0).Format("2006-01-02 15:04:05") - res, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr, locationTime) - return res -} - -// Return the local time. -func TimeNowString() string { - return time.Now().Format("2006-01-02 15:04:05") -} - -// Deprecated. -func GetTodayStart(location string) time.Time { - locationTime, _ := time.LoadLocation(location) - timeStr := time.Now().Format("2006-01-02") - t, _ := time.ParseInLocation("2006-01-02", timeStr, locationTime) - - return t -} - -// Deprecated. -func GetWeekStart(location string) time.Time { - locationTime, _ := time.LoadLocation(location) - timeStr := time.Now().Format("2006-01-02") - t, _ := time.ParseInLocation("2006-01-02", timeStr, locationTime) - - return time.Unix(t.Unix()-7*24*3600, 0) -} - -// Deprecated. -func GetMonthStart(location string) time.Time { - locationTime, _ := time.LoadLocation(location) - timeStr := time.Now().Format("2006-01") - t, _ := time.ParseInLocation("2006-01", timeStr, locationTime) - - return t -} - -// Deprecated. -func GetYearStart(location string) time.Time { - locationTime, _ := time.LoadLocation(location) - timeStr := time.Now().Format("2006") - t, _ := time.ParseInLocation("2006", timeStr, locationTime) - - return t -} - -// Deprecated. -func GetTimeByStr(str string, location string) time.Time { - locationTime, _ := time.LoadLocation(location) - t, _ := time.ParseInLocation("2006-01-02 15:04:05", str, locationTime) - return t -}