Compare whatTheFee estimates to default

This commit is contained in:
Martin Boehm 2019-04-18 11:18:32 +02:00
parent 239ccdfd78
commit 228d40e7a5
2 changed files with 35 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import (
"blockbook/bchain" "blockbook/bchain"
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"math" "math"
"net/http" "net/http"
"strconv" "strconv"
@ -67,23 +68,29 @@ func InitWhatTheFee(chain bchain.BlockChain, params string) error {
func whatTheFeeDownloader() { func whatTheFeeDownloader() {
period := time.Duration(whatTheFee.params.PeriodSeconds) * time.Second period := time.Duration(whatTheFee.params.PeriodSeconds) * time.Second
timer := time.NewTimer(period) timer := time.NewTimer(period)
counter := 0
for { for {
var data whatTheFeeServiceResult var data whatTheFeeServiceResult
err := whatTheFeeGetData(&data) err := whatTheFeeGetData(&data)
if err != nil { if err != nil {
glog.Error("whatTheFeeGetData ", err) glog.Error("whatTheFeeGetData ", err)
} else { } else {
whatTheFeeProcessData(&data) if whatTheFeeProcessData(&data) {
if counter%60 == 0 {
whatTheFeeCompareToDefault()
}
counter++
}
} }
<-timer.C <-timer.C
timer.Reset(period) timer.Reset(period)
} }
} }
func whatTheFeeProcessData(data *whatTheFeeServiceResult) { func whatTheFeeProcessData(data *whatTheFeeServiceResult) bool {
if len(data.Index) == 0 || len(data.Index) != len(data.Data) || len(data.Columns) == 0 { if len(data.Index) == 0 || len(data.Index) != len(data.Data) || len(data.Columns) == 0 {
glog.Errorf("invalid data %+v", data) glog.Errorf("invalid data %+v", data)
return return false
} }
whatTheFee.mux.Lock() whatTheFee.mux.Lock()
defer whatTheFee.mux.Unlock() defer whatTheFee.mux.Unlock()
@ -92,7 +99,7 @@ func whatTheFeeProcessData(data *whatTheFeeServiceResult) {
for i, blocks := range data.Index { for i, blocks := range data.Index {
if len(data.Columns) != len(data.Data[i]) { if len(data.Columns) != len(data.Data[i]) {
glog.Errorf("invalid data %+v", data) glog.Errorf("invalid data %+v", data)
return return false
} }
fees := make([]int, len(data.Columns)) fees := make([]int, len(data.Columns))
for j, l := range data.Data[i] { for j, l := range data.Data[i] {
@ -105,6 +112,7 @@ func whatTheFeeProcessData(data *whatTheFeeServiceResult) {
} }
whatTheFee.lastSync = time.Now() whatTheFee.lastSync = time.Now()
glog.Infof("%+v", whatTheFee.fees) glog.Infof("%+v", whatTheFee.fees)
return true
} }
func whatTheFeeGetData(res interface{}) error { func whatTheFeeGetData(res interface{}) error {
@ -120,10 +128,30 @@ func whatTheFeeGetData(res interface{}) error {
if err != nil { if err != nil {
return err return err
} }
// if server returns HTTP error code it might not return json with response
// handle both cases
if httpRes.StatusCode != 200 { if httpRes.StatusCode != 200 {
return errors.New("whatthefee.io returned status " + strconv.Itoa(httpRes.StatusCode)) return errors.New("whatthefee.io returned status " + strconv.Itoa(httpRes.StatusCode))
} }
return safeDecodeResponse(httpRes.Body, &res) return safeDecodeResponse(httpRes.Body, &res)
} }
func whatTheFeeCompareToDefault() {
output := ""
for _, fee := range whatTheFee.fees {
output += fmt.Sprint(fee.blocks, ",")
for _, wtf := range fee.feesPerKB {
output += fmt.Sprint(wtf, ",")
}
conservative, err := whatTheFee.chain.EstimateSmartFee(fee.blocks, true)
if err != nil {
glog.Error(err)
return
}
economical, err := whatTheFee.chain.EstimateSmartFee(fee.blocks, false)
if err != nil {
glog.Error(err)
return
}
output += fmt.Sprint(conservative.String(), ",", economical.String(), "\n")
}
glog.Info("whatTheFeeCompareToDefault\n", output)
}

View File

@ -59,10 +59,7 @@
"xpub_magic_segwit_p2sh": 71979618, "xpub_magic_segwit_p2sh": 71979618,
"xpub_magic_segwit_native": 73342198, "xpub_magic_segwit_native": 73342198,
"slip44": 1, "slip44": 1,
"additional_params": { "additional_params": {}
"alternativeEstimateFee": "whatthefee",
"alternativeEstimateFeeParams": "{\"url\": \"https://whatthefee.io/data.json\", \"periodSeconds\": 60}"
}
} }
}, },
"meta": { "meta": {