2025-10-16 18:54:27 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"math"
|
|
|
|
|
"math/big"
|
|
|
|
|
)
|
|
|
|
|
|
2025-10-31 13:46:58 +08:00
|
|
|
func BigIntETHToFloat64(value *big.Int) float64 {
|
|
|
|
|
f := new(big.Float).SetInt(value)
|
|
|
|
|
scale := new(big.Float).SetFloat64(1e18) // USDT 精度 6 位
|
|
|
|
|
f.Quo(f, scale)
|
|
|
|
|
result, _ := f.Float64()
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-16 18:54:27 +08:00
|
|
|
func BigIntUSDTToFloat64(value *big.Int) float64 {
|
|
|
|
|
f := new(big.Float).SetInt(value)
|
|
|
|
|
scale := new(big.Float).SetFloat64(1e6) // USDT 精度 6 位
|
|
|
|
|
f.Quo(f, scale)
|
|
|
|
|
result, _ := f.Float64()
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// USDT 一般精度是 6 位小数
|
|
|
|
|
const USDTDecimals = 6
|
|
|
|
|
|
|
|
|
|
// Float64ToBigIntUSDT 将 float64 金额转换成 *big.Int
|
|
|
|
|
func Float64ToBigIntUSDT(amount float64) *big.Int {
|
|
|
|
|
// 乘上精度系数
|
|
|
|
|
scale := math.Pow10(USDTDecimals)
|
|
|
|
|
bigAmount := new(big.Int)
|
|
|
|
|
bigAmount.SetInt64(int64(amount * scale))
|
|
|
|
|
return bigAmount
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-31 13:46:58 +08:00
|
|
|
const ETHDecimals = 18
|
|
|
|
|
|
|
|
|
|
func Float64ToBigIntETH(amount float64) *big.Int {
|
|
|
|
|
// 乘上精度系数
|
|
|
|
|
scale := math.Pow10(ETHDecimals)
|
|
|
|
|
bigAmount := new(big.Int)
|
|
|
|
|
bigAmount.SetInt64(int64(amount * scale))
|
|
|
|
|
return bigAmount
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-16 18:54:27 +08:00
|
|
|
func Slice_delete(arr []any, index int) []any {
|
|
|
|
|
if index < 0 || index >= len(arr) {
|
|
|
|
|
// 处理越界
|
|
|
|
|
log.Fatalf("slice arr error: index=%d, arr length=%d", index, len(arr))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if index >= 0 && index < len(arr) {
|
|
|
|
|
copy(arr[index:], arr[index+1:]) // 后面的元素往前移动
|
|
|
|
|
arr = arr[:len(arr)-1] // 去掉最后一个多余元素
|
|
|
|
|
}
|
|
|
|
|
return arr
|
|
|
|
|
}
|
2025-11-13 17:08:38 +08:00
|
|
|
|
|
|
|
|
// 函数:检查一个切片是否包含某个字符串
|
|
|
|
|
func Contains(slice []string, str string) bool {
|
|
|
|
|
for _, s := range slice {
|
|
|
|
|
if s == str {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Float64ToBigInt(symbol string, amount float64) *big.Int {
|
|
|
|
|
var scale float64
|
|
|
|
|
switch symbol {
|
|
|
|
|
case "ETH":
|
|
|
|
|
scale = math.Pow10(ETHDecimals)
|
|
|
|
|
case "USDT":
|
|
|
|
|
scale = math.Pow10(USDTDecimals)
|
|
|
|
|
default:
|
|
|
|
|
log.Printf("don`t support symbol: %s", symbol)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
bigAmount := new(big.Int)
|
|
|
|
|
bigAmount.SetInt64(int64(amount * scale))
|
|
|
|
|
return bigAmount
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BigIntToFloat64(symbol string, amount *big.Int) float64 {
|
|
|
|
|
var scale *big.Float
|
|
|
|
|
f := new(big.Float).SetInt(amount)
|
|
|
|
|
switch symbol {
|
|
|
|
|
case "ETH":
|
|
|
|
|
scale = new(big.Float).SetFloat64(1e18)
|
|
|
|
|
case "USDT":
|
|
|
|
|
scale = new(big.Float).SetFloat64(1e6)
|
|
|
|
|
default:
|
|
|
|
|
log.Printf("don`t support symbol: %s", symbol)
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
f.Quo(f, scale)
|
|
|
|
|
result, _ := f.Float64()
|
|
|
|
|
return result
|
|
|
|
|
}
|