forked from ellipti-labs/terra-oracle
-
Notifications
You must be signed in to change notification settings - Fork 2
/
upbit.go
68 lines (55 loc) · 1.36 KB
/
upbit.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
package upbit
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/everett-protocol/terra-oracle/types"
"io/ioutil"
"net/http"
"regexp"
"time"
)
type UpbitSource struct {
lunaToBtc types.Pair
interval time.Duration
}
var _ types.Source = &UpbitSource{}
func SourceProvider() (string, string, types.SourceProvider) {
return types.PairStr(types.LUNA, types.BTC), "upbit", NewUpbitSource
}
func NewUpbitSource(interval time.Duration) types.Source {
return &UpbitSource{
lunaToBtc: types.NewPair(types.LUNA, types.BTC),
interval: interval,
}
}
func (source UpbitSource) Market() string {
return "upbit"
}
func (source UpbitSource) Pair() types.Pair {
return source.lunaToBtc
}
func (source *UpbitSource) Fetch() error {
resp, err := http.Get("https://crix-api-cdn.upbit.com/v1/crix/trades/ticks?code=CRIX.UPBIT.BTC-LUNA&count=1")
if err != nil {
return err
}
defer func() {
resp.Body.Close()
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
re, _ := regexp.Compile("\"tradePrice\":[0-9.]+")
str := re.FindString(string(body))
re, _ = regexp.Compile("[0-9.]+")
price := re.FindString(str)
decAmount, err := sdk.NewDecFromStr(price)
if err != nil {
return err
}
source.lunaToBtc = source.lunaToBtc.SetPrice(decAmount)
return nil
}
func (source UpbitSource) Interval() time.Duration {
return source.interval
}