-
Notifications
You must be signed in to change notification settings - Fork 15
/
band_fx.go
96 lines (80 loc) · 2.3 KB
/
band_fx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package price
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/tendermint/tendermint/libs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
cfg "github.com/node-a-team/terra-oracle/config"
)
type FxResponse struct {
Height string `json:"height"`
Result []Result `json:"result"`
}
type Result struct {
Symbol string `json:"symbol"`
Multiplier float64 `json:"multiplier,string"`
Px float64 `json:"px,string"`
RequestID string `json:"request_id"`
ResolveTime int64 `json:"resolve_time,string"`
}
func (ps *PriceService) fxsToUsd(logger log.Logger) {
for {
if !cfg.Config.APIs.Band.Active {
logger.Info("Warning APIs.Band.Active is false in Config.toml. Let's exit the fxsToUsd().")
break
}
func() {
defer func() {
if r := recover(); r != nil {
logger.Error("Unknown error", r)
}
time.Sleep(cfg.Config.Options.Interval.Luna * time.Second)
}()
req, err := http.NewRequest(
"POST",
cfg.Config.APIs.Band.Band+"/oracle/request_prices",
bytes.NewBuffer([]byte(`{"symbols":["LUNA", "XDR", "MNT", "EUR", "CNY", "JPY", "GBP", "INR", "CAD", "CHF", "HKD", "SGD", "AUD"],"min_count":10,"ask_count":16}`)),
)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
logger.Error("Fail to fetch from band-fx", err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Error("Fail to read body", err.Error())
return
}
var res FxResponse
err = json.Unmarshal(body, &res)
if err != nil {
logger.Error("Fail to unmarshal body", err.Error())
return
}
logs := ""
for _, rate := range res.Result {
symbol := strings.ToLower(rate.Symbol)
if symbol == "xdr" {
symbol = "sdr"
}
decAmount, err := sdk.NewDecFromStr(strconv.FormatFloat(rate.Px/rate.Multiplier, 'f', -1, 64))
if err != nil {
logger.Error("Fail to parse price to Dec", err.Error())
return
}
ps.SetPrice(symbol+"/usd", sdk.NewDecCoinFromDec("usd", decAmount), rate.ResolveTime)
logs += fmt.Sprintf(" [%s/usd:%v,timestamp:%d] ", symbol, decAmount, rate.ResolveTime)
}
logger.Info(fmt.Sprintf("Recent [%s]", logs))
}()
}
}