forked from stellar-deprecated/kelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiatFeed.go
55 lines (45 loc) · 926 Bytes
/
fiatFeed.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
package plugins
import (
"net/http"
"time"
"github.com/lightyeario/kelp/api"
"github.com/lightyeario/kelp/support/utils"
)
/*
{
"success":true,
"terms":"https:\/\/currencylayer.com\/terms",
"privacy":"https:\/\/currencylayer.com\/privacy",
"timestamp":1504027454,
"source":"USD",
"quotes":{"USDPHP":51.080002}
}
*/
type fiatAPIReturn struct {
Quotes map[string]float64
}
type fiatFeed struct {
url string
client http.Client
}
// ensure that it implements PriceFeed
var _ api.PriceFeed = &fiatFeed{}
func newFiatFeed(url string) *fiatFeed {
m := new(fiatFeed)
m.url = url
m.client = http.Client{Timeout: 10 * time.Second}
return m
}
// GetPrice impl
func (f *fiatFeed) GetPrice() (float64, error) {
var ret fiatAPIReturn
err := utils.GetJSON(f.client, f.url, &ret)
if err != nil {
return 0, err
}
var pA float64
for _, value := range ret.Quotes {
pA = value
}
return (1.0 / pA), nil
}