This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
priceFeed_test.go
166 lines (146 loc) · 5.02 KB
/
priceFeed_test.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package plugins
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/stellar/kelp/tests"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)
const wantLowerBoundXLM = 0.02
const wantUpperBoundXLM = 1.1
// uses real network calls
func TestMakePriceFeed(t *testing.T) {
testCases := []struct {
typ string
url string
wantLowerOrEqualBound float64
wantHigherOrEqualBound float64
}{
{
typ: "exchange",
url: "kraken/XXLM/ZUSD/mid",
wantLowerOrEqualBound: wantLowerBoundXLM,
wantHigherOrEqualBound: wantUpperBoundXLM,
}, {
typ: "exchange",
url: "ccxt-binance/XLM/USDT/bid",
wantLowerOrEqualBound: wantLowerBoundXLM,
wantHigherOrEqualBound: wantUpperBoundXLM,
}, {
typ: "exchange",
url: "ccxt-coinbasepro/XLM/USD/ask",
wantLowerOrEqualBound: wantLowerBoundXLM,
wantHigherOrEqualBound: wantUpperBoundXLM,
}, {
typ: "fixed",
url: "1.23456",
wantLowerOrEqualBound: 1.23456,
wantHigherOrEqualBound: 1.23456,
}, {
typ: "sdex",
url: "XLM:/USD:GDUKMGUGDZQK6YHYA5Z6AY2G4XDSZPSZ3SW5UN3ARVMO6QSRDWP5YLEX",
wantLowerOrEqualBound: wantLowerBoundXLM,
wantHigherOrEqualBound: wantUpperBoundXLM,
}, {
typ: "sdex",
url: "USD:GDUKMGUGDZQK6YHYA5Z6AY2G4XDSZPSZ3SW5UN3ARVMO6QSRDWP5YLEX/XLM:",
wantLowerOrEqualBound: 1 / wantUpperBoundXLM,
wantHigherOrEqualBound: 1 / wantLowerBoundXLM,
}, {
typ: "function",
url: "max(fixed/1.0,fixed/1.4)",
wantLowerOrEqualBound: 1.4,
wantHigherOrEqualBound: 1.4,
}, {
typ: "function",
url: "max(fixed/0.02,exchange/ccxt-binance/XLM/USDT/last)",
wantLowerOrEqualBound: wantLowerBoundXLM,
wantHigherOrEqualBound: wantUpperBoundXLM,
}, {
typ: "function",
url: "invert(fixed/0.02)",
wantLowerOrEqualBound: 50.0,
wantHigherOrEqualBound: 50.0,
},
// disable ccxt-kraken based tests for now because of the 403 Forbidden Security check API error
// {
// typ: "exchange",
// url: "ccxt-kraken/XLM/USD/last",
// wantLowerOrEqualBound: wantLowerBoundXLM,
// wantHigherOrEqualBound: wantUpperBoundXLM,
// },
}
// cannot run this in parallel because ccxt fails (by not recognizing exchanges) when hit with too many requests at once
for _, k := range testCases {
t.Run(k.typ+"/"+k.url, func(t *testing.T) {
pf, e := MakePriceFeed(k.typ, k.url)
if !assert.NoError(t, e) {
return
}
price, e := pf.GetPrice()
if !assert.NoError(t, e) {
return
}
assert.True(t, price >= k.wantLowerOrEqualBound, fmt.Sprintf("price was %.10f, should have been >= %.10f", price, k.wantLowerOrEqualBound))
assert.True(t, price <= k.wantHigherOrEqualBound, fmt.Sprintf("price was %.10f, should have been <= %.10f", price, k.wantHigherOrEqualBound))
})
}
}
// uses mock call
func TestMakePriceFeed_FiatFeed_Success(t *testing.T) {
response := fiatAPIReturn{
Success: true,
Quotes: map[string]float64{"SOME_CODE": 1},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
require.NoError(t, err)
}))
defer ts.Close()
priceFeed, err := MakePriceFeed("fiat", ts.URL)
assert.NoError(t, err)
price, err := priceFeed.GetPrice()
assert.Equal(t, response.Quotes["SOME_CODE"], price)
assert.NoError(t, err)
}
// uses mock call
func TestMakePriceFeed_FiatFeedOxr_Success(t *testing.T) {
symbol := tests.RandomString()
response := createOxrResponse(symbol)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
require.NoError(t, err)
}))
defer ts.Close()
priceFeed, err := MakePriceFeed("fiat-oxr", ts.URL)
assert.NoError(t, err)
price, err := priceFeed.GetPrice()
assert.Equal(t, response.Rates[symbol], price)
assert.NoError(t, err)
}
// uses mock call
func TestMakePriceFeed_CryptoFeed_Success(t *testing.T) {
response := []cmcAPIReturn{{Price: strconv.Itoa(tests.RandomInt())}}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
require.NoError(t, err)
}))
defer ts.Close()
priceFeed, err := MakePriceFeed("crypto", ts.URL)
assert.NoError(t, err)
expected, err := strconv.ParseFloat(response[0].Price, 64)
require.NoError(t, err)
price, err := priceFeed.GetPrice()
assert.Equal(t, expected, price)
assert.NoError(t, err)
}