Skip to content

Commit 6c36eee

Browse files
committed
Add vote list for Proposal#38 {THB}, Use Bithumb API, not Coinone for luna/krw
1 parent 5f1c27a commit 6c36eee

File tree

11 files changed

+165
-89
lines changed

11 files changed

+165
-89
lines changed

cmd/terra-oracle/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
)
3232

3333
var (
34-
version = "v0.0.5-alpha.5"
34+
version = "v0.0.5-alpha.6"
3535
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
3636
)
3737

config.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ operatorAddr = "terravaloper~"
99
password = ""
1010

1111
[APIs]
12-
[APIs.Luna.Krw]
12+
[APIs.Luna.Krw]
1313
coinone = "https://tb.coinone.co.kr/api/v1/tradehistory/recent/?market=krw&target=luna"
14+
bithumb = "https://api.bithumb.com/public/ticker/luna_krw"
1415

1516
[APIs.Luna.Usd]
1617
binance = "https://api.binance.com/api/v3/ticker/price?symbol=LUNAUSDT"
@@ -20,12 +21,14 @@ password = ""
2021
# https://currencylayer.com/product
2122
currencylayer = "https://api.currencylayer.com/live?access_key="
2223

23-
[APIs.sdr]
24-
imf = "https://www.imf.org/external/np/fin/data/rms_five.aspx?tsvflag=Y"
24+
# [APIs.sdr]
25+
# imf = "https://www.imf.org/external/np/fin/data/rms_five.aspx?tsvflag=Y"
2526

2627
[APIs.band]
27-
active = true # true/false
28+
active = true
2829
band = "https://terra-lcd.bandchain.org"
2930

3031
[Options]
31-
interval = 200
32+
[Options.Interval]
33+
luna = 5
34+
stables = 200

config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type configType struct {
3030
Luna struct {
3131
Krw struct {
3232
Coinone string `json:"coinone"`
33+
Bithumb string `json:"bithumb"`
3334
}
3435
Usd struct {
3536
Binance string `json:"binance"`
@@ -50,7 +51,10 @@ type configType struct {
5051
}
5152
}
5253
Options struct {
53-
Interval time.Duration `json:"interval"`
54+
Interval struct {
55+
Luna time.Duration `json:"luna"`
56+
Stables time.Duration `json:"stables"`
57+
}
5458
}
5559
}
5660

oracle/tx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var (
3535
voteMode string
3636
salt, exchangeRatesStr = []string{"1234", ""}, []string{"", ""}
3737

38-
denoms = []string{"krw", "usd", "eur", "mnt", "cny", "jpy", "gbp", "inr", "cad", "chf", "hkd", "sgd", "aud", "sdr"}
38+
denoms = []string{"krw", "usd", "eur", "mnt", "cny", "jpy", "gbp", "inr", "cad", "chf", "hkd", "sgd", "aud", "sdr", "thb"}
3939
)
4040

4141
func (os *OracleService) init() error {

price/band_fx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (ps *PriceService) fxsToUsd(logger log.Logger) {
4242
logger.Error("Unknown error", r)
4343
}
4444

45-
time.Sleep(cfg.Config.Options.Interval * time.Second)
45+
time.Sleep(cfg.Config.Options.Interval.Luna * time.Second)
4646
}()
4747

4848
req, err := http.NewRequest(

price/band_luna.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (ps *PriceService) bandLunaToKrw(logger log.Logger) {
118118
logger.Error("Unknown error", r)
119119
}
120120

121-
time.Sleep(cfg.Config.Options.Interval * time.Second)
121+
time.Sleep(cfg.Config.Options.Interval.Luna * time.Second)
122122
}()
123123

124124
resp, err := http.Get(cfg.Config.APIs.Band.Band + LUNA_PRICE_END_POINT)

price/lunaToKrw.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package price
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"time"
9+
10+
"github.com/tendermint/tendermint/libs/log"
11+
12+
sdk "github.com/cosmos/cosmos-sdk/types"
13+
cfg "github.com/node-a-team/terra-oracle/config"
14+
)
15+
16+
// TradeHistory response from coinone
17+
type TradeHistory struct {
18+
Trades []Trade `json:"trades"`
19+
}
20+
21+
// Trade response from coinone
22+
type Trade struct {
23+
Timestamp uint64 `json:"timestamp"`
24+
Price string `json:"price"`
25+
Volume string `json:"volume"`
26+
IsSellerMaker bool `json:"is_seller_maker"`
27+
}
28+
29+
// for bithumn
30+
type Ticker_bithumb struct {
31+
Data struct {
32+
Closing_price string `json:"closing_price"`
33+
Date string `json:"date"`
34+
}
35+
}
36+
37+
38+
39+
func (ps *PriceService) lunaToKrw(logger log.Logger) {
40+
41+
// coinone(ps, logger)
42+
bithumb(ps, logger)
43+
}
44+
45+
func bithumb(ps *PriceService, logger log.Logger) {
46+
for {
47+
func() {
48+
defer func() {
49+
if r := recover(); r != nil {
50+
logger.Error("Unknown error", r)
51+
}
52+
53+
time.Sleep(cfg.Config.Options.Interval.Luna * time.Second)
54+
}()
55+
56+
// resp, err := http.Get("https://api.bithumb.com/public/ticker/luna_krw")
57+
resp, err := http.Get(cfg.Config.APIs.Luna.Krw.Bithumb)
58+
if err != nil {
59+
logger.Error("Fail to fetch from coinone", err.Error())
60+
return
61+
}
62+
defer func() {
63+
resp.Body.Close()
64+
}()
65+
66+
body, err := ioutil.ReadAll(resp.Body)
67+
if err != nil {
68+
logger.Error("Fail to read body", err.Error())
69+
return
70+
}
71+
72+
t := Ticker_bithumb{}
73+
err = json.Unmarshal(body, &t)
74+
if err != nil {
75+
logger.Error("Fail to unmarshal json", err.Error())
76+
return
77+
}
78+
79+
timestamp := time.Now().UTC().Unix()
80+
logger.Info(fmt.Sprintf("Recent luna/krw: %s, timestamp: %d", t.Data.Closing_price, timestamp))
81+
82+
decAmount, err := sdk.NewDecFromStr(t.Data.Closing_price)
83+
if err != nil {
84+
logger.Error("Fail to parse price to Dec")
85+
}
86+
87+
ps.SetPrice("luna/krw", sdk.NewDecCoinFromDec("krw", decAmount), timestamp)
88+
89+
}()
90+
}
91+
92+
}
93+
94+
95+
96+
func coinone(ps *PriceService, logger log.Logger) {
97+
for {
98+
func() {
99+
defer func() {
100+
if r := recover(); r != nil {
101+
logger.Error("Unknown error", r)
102+
}
103+
104+
time.Sleep(cfg.Config.Options.Interval.Luna * time.Second)
105+
}()
106+
107+
// resp, err := http.Get("https://tb.coinone.co.kr/api/v1/tradehistory/recent/?market=krw&target=luna")
108+
resp, err := http.Get(cfg.Config.APIs.Luna.Krw.Coinone)
109+
if err != nil {
110+
logger.Error("Fail to fetch from coinone", err.Error())
111+
return
112+
}
113+
defer func() {
114+
resp.Body.Close()
115+
}()
116+
117+
body, err := ioutil.ReadAll(resp.Body)
118+
if err != nil {
119+
logger.Error("Fail to read body", err.Error())
120+
return
121+
}
122+
123+
th := TradeHistory{}
124+
err = json.Unmarshal(body, &th)
125+
if err != nil {
126+
logger.Error("Fail to unmarshal json", err.Error())
127+
return
128+
}
129+
130+
trades := th.Trades
131+
recent := trades[len(trades)-1]
132+
logger.Info(fmt.Sprintf("Recent luna/krw: %s, timestamp: %d", recent.Price, recent.Timestamp))
133+
134+
decAmount, err := sdk.NewDecFromStr(recent.Price)
135+
if err != nil {
136+
logger.Error("Fail to parse price to Dec")
137+
}
138+
139+
ps.SetPrice("luna/krw", sdk.NewDecCoinFromDec("krw", decAmount), int64(recent.Timestamp))
140+
141+
}()
142+
}
143+
144+
}

price/lunaToUsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (ps *PriceService) lunaToUsd(logger log.Logger) {
2727
logger.Error("Unknown error", r)
2828
}
2929

30-
time.Sleep(cfg.Config.Options.Interval * time.Second)
30+
time.Sleep(cfg.Config.Options.Interval.Luna * time.Second)
3131
}()
3232

3333
// resp, err := http.Get("https://api.binance.com/api/v3/ticker/price?symbol=LUNAUSDT")

price/lunaTokrw.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

price/sdr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (ps *PriceService) sdrToUsd(logger log.Logger) {
2222
logger.Error("Unknown error", r)
2323
}
2424

25-
time.Sleep(cfg.Config.Options.Interval * time.Second)
25+
time.Sleep(cfg.Config.Options.Interval.Stables * time.Second)
2626
}()
2727

2828
// resp, err := http.Get("https://www.imf.org/external/np/fin/data/rms_five.aspx?tsvflag=Y")

price/stables.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type APILayerResponse struct {
2626
}
2727

2828
var (
29-
stables = []string{"XDR", "MNT", "EUR", "CNY", "JPY", "GBP", "INR", "CAD", "CHF", "HKD", "SGD", "AUD"}
29+
stables = []string{"XDR", "MNT", "EUR", "CNY", "JPY", "GBP", "INR", "CAD", "CHF", "HKD", "SGD", "AUD", "THB"}
3030
)
3131

3232
func (ps *PriceService) stablesToUsd(logger log.Logger) {
@@ -37,7 +37,7 @@ func (ps *PriceService) stablesToUsd(logger log.Logger) {
3737
logger.Error("Unknown error", r)
3838
}
3939

40-
time.Sleep(cfg.Config.Options.Interval * time.Second)
40+
time.Sleep(cfg.Config.Options.Interval.Stables * time.Second)
4141
}()
4242

4343
// resp, err := http.Get("https://api.currencylayer.com/live?access_key=")

0 commit comments

Comments
 (0)