Commit dcb43814 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/cron' into 'master'

优化 See merge request !33
parents 46c79da4 61bad8a5
...@@ -61,7 +61,7 @@ func GetSupportedCurrencyTotal(maps interface{}) (int, error) { ...@@ -61,7 +61,7 @@ func GetSupportedCurrencyTotal(maps interface{}) (int, error) {
func GetSupportedCurrencies(pageNum, pageSize int, maps interface{}) ([]*SupportedCurrency, error) { func GetSupportedCurrencies(pageNum, pageSize int, maps interface{}) ([]*SupportedCurrency, error) {
var currencies []*SupportedCurrency var currencies []*SupportedCurrency
err := db.Preload("Currency").Where(maps).Offset(pageNum).Limit(pageSize).Find(&currencies).Error err := db.Preload("Currency").Order("sort").Where(maps).Offset(pageNum).Limit(pageSize).Find(&currencies).Error
if err != nil && err != gorm.ErrRecordNotFound { if err != nil && err != gorm.ErrRecordNotFound {
return nil, err return nil, err
} }
......
...@@ -11,13 +11,7 @@ func Setup(){ ...@@ -11,13 +11,7 @@ func Setup(){
c = cron.New() c = cron.New()
c.AddFunc("* * * * * *", func() { c.AddFunc("* * * * * *", func() {
gas_service := coin_gas_service.CoinGas{} coin_gas_service.TokenView()
gas_service.Etherscan()
})
c.AddFunc("* * * * * *", func() {
gas_service := coin_gas_service.CoinGas{}
gas_service.Btcscan()
}) })
c.Start() c.Start()
......
...@@ -100,3 +100,12 @@ func StringToMd5(str string) string { ...@@ -100,3 +100,12 @@ func StringToMd5(str string) string {
md5String := hex.EncodeToString(m5.Sum(nil)) md5String := hex.EncodeToString(m5.Sum(nil))
return md5String return md5String
} }
func Contains(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
...@@ -66,7 +66,7 @@ func GetTransactionGas(c *gin.Context) { ...@@ -66,7 +66,7 @@ func GetTransactionGas(c *gin.Context) {
return return
} }
gas, err := coin_gas_service.GetCoinGas(name) gas, err := coin_gas_service.GetTransactionGas(name)
if err != nil { if err != nil {
handler.SendResponse(c, errno.ErrCoinNotFound, nil) handler.SendResponse(c, errno.ErrCoinNotFound, nil)
......
...@@ -47,7 +47,7 @@ func InitRouter() *gin.Engine { ...@@ -47,7 +47,7 @@ func InitRouter() *gin.Engine {
client.POST("/live/verify",app.VerifyStatus) client.POST("/live/verify",app.VerifyStatus)
client.GET("/live-banners",app.GetLiveBanners) client.GET("/live-banners",app.GetLiveBanners)
client.GET("/coin-gas",app.GetTransactionGas) client.GET("/fees/recommended",app.GetTransactionGas)
api := r.Group("/api") api := r.Group("/api")
......
package coin_gas_service package coin_gas_service
import ( import (
"bwallet/pkg/errno"
"bwallet/pkg/gredis" "bwallet/pkg/gredis"
"bwallet/pkg/util" "bwallet/pkg/util"
"encoding/json" "encoding/json"
...@@ -10,24 +11,53 @@ import ( ...@@ -10,24 +11,53 @@ import (
"strings" "strings"
) )
type CoinGas struct { type Response struct {
Id int Code int `json:"code"`
CoinName string Msg string `json:"msg"`
Low float32 Data []Data `json:"data"`
Average float32
High float32
} }
func GetCoinGas(coin string) (float32, error) { type Data struct {
gas, err := gredis.HashGet(strings.ToUpper(coin)+"_GAS", "Final", 3) Network string `json:network`
if err != nil { Time int `json:time`
return 0.0, err TxFee string `json:txFee`
GasPrice string `json:gasPrice`
}
func GetTransactionGas(name string) (map[string]interface{}, error) {
data := make(map[string]interface{})
items_fee := []string{"BTC", "BCH", "LTC", "ZEC", "ZCASH", "DCR", "NEO", "TRX", "ATOM", "BTY", "ETC", "ETH"}
_, found_fee := util.Contains(items_fee, strings.ToUpper(name))
if !found_fee {
return nil, errno.ErrCoinNotFound
} }
return util.ToFloat32(string(gas)), nil
if "ETC" == strings.ToUpper(name) {
fee, _ := gredis.HashGet(strings.ToUpper(name)+"_GAS", "gasPrice", 3)
data["name"] = strings.ToUpper(name)
data["low"] = util.ToFloat32(string(fee)) / 1000000000 * 21000 * 0.000000001 * 30 * (1 + 0.1)
data["average"] = util.ToFloat32(string(fee)) / 1000000000 * 21000 * 0.000000001 * 30 * (1 + 0.2)
data["high"] = util.ToFloat32(string(fee)) / 1000000000 * 21000 * 0.000000001 * 30 * (1 + 0.3)
} else if "ETH" == strings.ToUpper(name) {
fee, _ := gredis.HashGet(strings.ToUpper(name)+"_GAS", "gasPrice", 3)
data["name"] = strings.ToUpper(name)
data["low"] = util.ToFloat32(string(fee)) * 0.000000001 * (1 + 0.1)
data["average"] = util.ToFloat32(string(fee)) * 0.000000001 * (1 + 0.2)
data["high"] = util.ToFloat32(string(fee)) * 0.000000001 * (1 + 0.3)
} else {
fee, _ := gredis.HashGet(strings.ToUpper(name)+"_GAS", "txFee", 3)
data["name"] = strings.ToUpper(name)
data["low"] = util.ToFloat32(string(fee)) * (1 + 0.1)
data["average"] = util.ToFloat32(string(fee)) * (1 + 0.2)
data["high"] = util.ToFloat32(string(fee)) * (1 + 0.3)
}
return data, nil
} }
func (a *CoinGas) Etherscan() error { func Etherscan() error {
params := make(map[string]string) params := make(map[string]string)
params["module"] = "gastracker" params["module"] = "gastracker"
params["action"] = "gasoracle" params["action"] = "gasoracle"
...@@ -66,7 +96,7 @@ func (a *CoinGas) Etherscan() error { ...@@ -66,7 +96,7 @@ func (a *CoinGas) Etherscan() error {
return nil return nil
} }
func (a *CoinGas) Btcscan() error { func Btcscan() error {
req, err := http.NewRequest(http.MethodGet, "https://btc.com/service/fees/recommended", nil) req, err := http.NewRequest(http.MethodGet, "https://btc.com/service/fees/recommended", nil)
if err != nil { if err != nil {
return err return err
...@@ -88,8 +118,61 @@ func (a *CoinGas) Btcscan() error { ...@@ -88,8 +118,61 @@ func (a *CoinGas) Btcscan() error {
gredis.HashSet("BTC_GAS", "Average", btc_gas["one_block_fee"], 3) gredis.HashSet("BTC_GAS", "Average", btc_gas["one_block_fee"], 3)
gredis.HashSet("BTC_GAS", "High", btc_gas["one_block_fee"], 3) gredis.HashSet("BTC_GAS", "High", btc_gas["one_block_fee"], 3)
final := util.ToFloat32(btc_gas["one_block_fee"]) * (1 + 0.1) final := util.ToFloat32(btc_gas["one_block_fee"]) * (1 + 0.1) * 0.00001
gredis.HashSet("BTC_GAS", "Final", final, 3) gredis.HashSet("BTC_GAS", "Final", final, 3)
return nil return nil
} }
func TokenView() error {
req, err := http.NewRequest(http.MethodGet, "https://tokenview.com/api/block/latest", nil)
if err != nil {
return err
}
r, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
responseData, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
var responseObject Response
json.Unmarshal(responseData, &responseObject)
for _, val := range responseObject.Data {
if "-" == val.TxFee || "-" == val.GasPrice {
continue
}
if "BTC" == strings.ToUpper(val.Network) {
gredis.HashSet("BTC_GAS", "txFee", val.TxFee, 3)
gredis.HashSet("BTC_GAS", "time", val.Time, 3)
}
if "BCH" == strings.ToUpper(val.Network) {
gredis.HashSet("BCH_GAS", "txFee", val.TxFee, 3)
gredis.HashSet("BCH_GAS", "time", val.Time, 3)
}
if "LTC" == strings.ToUpper(val.Network) {
gredis.HashSet("LTC_GAS", "txFee", val.TxFee, 3)
gredis.HashSet("LTC_GAS", "time", val.Time, 3)
}
if "ZCASH" == strings.ToUpper(val.Network) || "ZEC" == strings.ToUpper(val.Network) {
gredis.HashSet("ZEC_GAS", "txFee", val.TxFee, 3)
gredis.HashSet("ZEC_GAS", "time", val.Time, 3)
}
if "DCR" == strings.ToUpper(val.Network) {
gredis.HashSet("DCR_GAS", "txFee", val.TxFee, 3)
gredis.HashSet("DCR_GAS", "time", val.Time, 3)
}
if "ETC" == strings.ToUpper(val.Network) {
gredis.HashSet("ETC_GAS", "gasPrice", val.GasPrice, 3)
gredis.HashSet("ETC_GAS", "time", val.Time, 3)
}
if "ETH" == strings.ToUpper(val.Network) {
gredis.HashSet("ETH_GAS", "gasPrice", val.GasPrice, 3)
gredis.HashSet("ETH_GAS", "time", val.Time, 3)
}
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment