Commit d16254ad authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/cron' into 'master'

Feature/cron See merge request !29
parents 05afc415 bf3eb818
......@@ -26,6 +26,7 @@ require (
github.com/oxequa/realize v2.0.2+incompatible // indirect
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect
github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017 // indirect
github.com/robfig/cron v1.2.0
github.com/satori/go.uuid v1.2.0 // indirect
github.com/valyala/fasttemplate v1.1.0 // indirect
github.com/xinliangnote/go-util v0.0.0-20200323134426-527984dc34bf
......
......@@ -170,6 +170,8 @@ github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
......
......@@ -5,14 +5,14 @@ import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"bwallet/models"
"bwallet/pkg/cron"
"bwallet/pkg/gredis"
"bwallet/pkg/logging"
"bwallet/pkg/setting"
"bwallet/routers"
"bwallet/pkg/util"
"bwallet/routers"
"github.com/gin-gonic/gin"
)
func init() {
......@@ -21,6 +21,7 @@ func init() {
logging.Setup()
gredis.Setup()
util.Setup()
cron.Setup()
}
func main() {
......
package cron
import (
"bwallet/service/coin_gas_service"
"github.com/robfig/cron"
)
var c *cron.Cron
func Setup() {
c = cron.New()
c.AddFunc("* * * * * *", func() {
gas_service := coin_gas_service.CoinGas{}
gas_service.Etherscan()
})
c.AddFunc("* * * * * *", func() {
gas_service := coin_gas_service.CoinGas{}
gas_service.Btcscan()
})
c.Start()
}
......@@ -122,6 +122,41 @@ func ZAdd(key string, score int, data interface{}, db int) error {
return nil
}
func HashSet(key, field string, data interface{}, db int) error {
conn := RedisConn.Get()
defer conn.Close()
if _, err := conn.Do("SELECT", db); err != nil {
conn.Close()
return err
}
_, err := conn.Do("HSet", key, field, data)
if err != nil {
return err
}
return nil
}
func HashGet(key, field string, db int) ([]byte, error) {
conn := RedisConn.Get()
defer conn.Close()
if _, err := conn.Do("SELECT", db); err != nil {
conn.Close()
return nil, err
}
val, err := redis.Bytes(conn.Do("hget", key, field))
if err != nil {
return nil, err
}
return val, nil
}
//通过索引区间返回有序集合指定区间内的成员
func ZRange(key string, start, stop int, db int) ([]string, error) {
conn := RedisConn.Get()
......
......@@ -4,6 +4,7 @@ import (
"bwallet/pkg/errno"
"bwallet/pkg/handler"
"bwallet/pkg/util"
"bwallet/service/coin_gas_service"
"bwallet/service/fee_service"
"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
......@@ -53,3 +54,25 @@ func GetTransactionFee(c *gin.Context) {
handler.SendResponse(c, nil, fee)
return
}
func GetTransactionGas(c *gin.Context) {
valid := validation.Validation{}
name := c.DefaultQuery("name", "")
valid.Required(name, "name").Message("币种不能为空")
if valid.HasErrors() {
handler.SendResponse(c, valid.Errors[0], nil)
return
}
gas, err := coin_gas_service.GetCoinGas(name)
if err != nil {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
handler.SendResponse(c, nil, gas)
return
}
......@@ -46,6 +46,9 @@ func InitRouter() *gin.Engine {
client.POST("/live/notifyUrl", app.NotifyUrl)
client.POST("/live/verify",app.VerifyStatus)
client.GET("/live-banners",app.GetLiveBanners)
client.GET("/coin-gas",app.GetTransactionGas)
api := r.Group("/api")
//api.Use(auth.AUTH())
......
package coin_gas_service
import (
"bwallet/pkg/gredis"
"bwallet/pkg/util"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type CoinGas struct {
Id int
CoinName string
Low float32
Average float32
High float32
}
func GetCoinGas(coin string) (float32, error) {
gas, err := gredis.HashGet(strings.ToUpper(coin)+"_GAS", "Final", 3)
if err != nil {
return 0.0, err
}
return util.ToFloat32(string(gas)), nil
}
func (a *CoinGas) Etherscan() error {
params := make(map[string]string)
params["module"] = "gastracker"
params["action"] = "gasoracle"
params["apikey"] = "ZUT8I3GGVCSBRACCCXCTGGVCPYE8WEID6S"
req, err := http.NewRequest(http.MethodGet, "https://api-cn.etherscan.com/api", nil)
if err != nil {
return err
}
condition := make(url.Values)
condition.Add("module", params["module"])
condition.Add("action", params["action"])
condition.Add("apikey", params["apikey"])
req.URL.RawQuery = condition.Encode()
r, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
result := map[string]interface{}{}
json.Unmarshal(body, &result)
eth_gas, _ := result["result"].(map[string]interface{})
gredis.HashSet("ETH_GAS", "Low", eth_gas["SafeGasPrice"], 3)
gredis.HashSet("ETH_GAS", "Average", eth_gas["ProposeGasPrice"], 3)
gredis.HashSet("ETH_GAS", "High", eth_gas["FastGasPrice"], 3)
final := 21000 * util.ToFloat32(eth_gas["SafeGasPrice"]) * 0.000000001
gredis.HashSet("ETH_GAS", "Final", final, 3)
return nil
}
func (a *CoinGas) Btcscan() error {
req, err := http.NewRequest(http.MethodGet, "https://btc.com/service/fees/recommended", nil)
if err != nil {
return err
}
r, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
result := map[string]interface{}{}
json.Unmarshal(body, &result)
btc_gas, _ := result["data"].(map[string]interface{})
gredis.HashSet("BTC_GAS", "Low", 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)
final := util.ToFloat32(btc_gas["one_block_fee"]) * (1 + 0.1)
gredis.HashSet("BTC_GAS", "Final", final, 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