Skip to content

Commit 30f9ff8

Browse files
committed
BUGFIX: Handle devide by 0 and take inverse of price.
1 parent 8ac65fa commit 30f9ff8

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

cmc.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type cmcResponse struct {
2121
Symbol string `json:"symbol"`
2222
Quotes struct {
2323
BTC struct {
24-
Price float64 `json:"price"`
24+
Price json.Number `json:"price"`
2525
} `json:"BTC"`
2626
} `json:"quotes"`
2727
} `json:"data"`
@@ -87,7 +87,18 @@ func (f *cmcFetcher) fetchPage(start int) (*cmcResponse, error) {
8787

8888
func addPage(rates exchangeRates, pageResp *cmcResponse) {
8989
for _, entry := range pageResp.Data {
90-
formattedPrice := json.Number(strconv.FormatFloat(entry.Quotes.BTC.Price, 'G', -1, 64))
90+
price, err := entry.Quotes.BTC.Price.Float64()
91+
if err != nil {
92+
continue
93+
}
94+
95+
var formattedPrice json.Number
96+
if price == 0 {
97+
formattedPrice = json.Number("0")
98+
} else {
99+
formattedPrice = json.Number(strconv.FormatFloat(1.0/price, 'G', -1, 64))
100+
}
101+
91102
rates[entry.Symbol] = exchangeRate{
92103
Type: exchangeRateTypeCrypto.String(),
93104
Ask: formattedPrice,

proxy_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
}
3838
}`
3939

40-
testExpectedProxiedResponse = `{"$$$":{"ask":0.998877,"bid":0.998877,"last":0.998877,"type":"crypto"},"BCH":{"ask":0.16666667,"bid":0.14285715,"last":0.125,"type":"crypto"},"BTC":{"ask":1,"bid":1,"last":1,"type":"crypto"},"SOIL":{"ask":0.0012345,"bid":0.0012345,"last":0.0012345,"type":"crypto"},"USD":{"ask":1,"bid":2,"last":3,"type":"fiat"},"ZEC":{"ask":0.016666668,"bid":0.014285714,"last":0.0125,"type":"crypto"}}`
40+
testExpectedProxiedResponse = `{"$$$":{"ask":1.0011242625468402,"bid":1.0011242625468402,"last":1.0011242625468402,"type":"crypto"},"BCH":{"ask":0.16666667,"bid":0.14285715,"last":0.125,"type":"crypto"},"BTC":{"ask":1,"bid":1,"last":1,"type":"crypto"},"SOIL":{"ask":810.0445524503848,"bid":810.0445524503848,"last":810.0445524503848,"type":"crypto"},"USD":{"ask":1,"bid":2,"last":3,"type":"fiat"},"ZEC":{"ask":0.016666668,"bid":0.014285714,"last":0.0125,"type":"crypto"}}`
4141
)
4242

4343
func init() {

0 commit comments

Comments
 (0)