/
coinbase.go
67 lines (54 loc) · 1.16 KB
/
coinbase.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
package coinbase
import (
"context"
"time"
"github.com/fox-one/dirtoracle/core"
"github.com/fox-one/dirtoracle/pkg/route"
"github.com/patrickmn/go-cache"
"github.com/shopspring/decimal"
)
const (
exchangeName = "coinbase"
QuoteSymbol = "USD"
)
type coinbaseEx struct {
cache *cache.Cache
}
func New() core.Exchange {
return &coinbaseEx{
cache: cache.New(time.Minute, time.Minute),
}
}
func (*coinbaseEx) Name() string {
return exchangeName
}
func (exch *coinbaseEx) GetPrice(ctx context.Context, a *core.Asset) (decimal.Decimal, error) {
symbol := exch.assetSymbol(a.Symbol)
if symbol == QuoteSymbol {
return decimal.New(1, 0), nil
}
pairs, err := exch.getPairs(ctx)
if err != nil {
return decimal.Zero, err
}
routes, ok := route.FindRoutes(pairs, symbol, QuoteSymbol)
if !ok {
return decimal.Zero, err
}
var price = decimal.New(1, 0)
for _, route := range routes {
p, err := exch.getPrice(ctx, route.Symbol)
if err != nil {
return decimal.Zero, err
}
if route.Reverse {
price = price.Div(p)
} else {
price = price.Mul(p)
}
}
return price, nil
}
func (*coinbaseEx) assetSymbol(symbol string) string {
return symbol
}