-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethereumdb.go
60 lines (48 loc) · 1.21 KB
/
ethereumdb.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
package coinprice
import (
"fmt"
"github.com/blcokchina110/coinprice/xtime"
"github.com/blcokchina110/xhttp"
"github.com/shopspring/decimal"
)
const ()
type EthereumDB struct {
currencypair *CurrencyPair
timestamp int64
}
//
type ethereumDBInfo struct {
Price decimal.Decimal `json:"price"`
QuoteVolume24h decimal.Decimal `json:"quoteVolume24h"`
Timestamp int64 `json:"timestamp"`
}
//
func NewEthereumDB(currencypair *CurrencyPair) *EthereumDB {
return &EthereumDB{
currencypair: currencypair,
timestamp: xtime.Second(),
}
}
//接口渠道
func (e *EthereumDB) Name() string {
return "ethereumdb"
}
//时间戳
func (e *EthereumDB) TimeStamp() int64 {
return e.timestamp
}
//获取指定币种美元价格
func (e *EthereumDB) GetPrice() decimal.Decimal {
var infos []ethereumDBInfo
url := fmt.Sprintf(ethereumdbApiUrl, e.currencypair.Currency1(), e.currencypair.Currency2())
if err := xhttp.GetParseData(url, nil, &infos); err == nil {
if len(infos) > 0 {
e.timestamp = infos[0].Timestamp
if !e.currencypair.Reverse() {
return infos[0].Price.Truncate(2)
}
return decimal.NewFromInt(1).Div(infos[0].Price).Truncate(8)
}
}
return decimal.NewFromInt(0)
}