Skip to content

Commit

Permalink
Merge pull request #146 from tonkeeper/update_platform_rates
Browse files Browse the repository at this point in the history
add exchangerate platform to rates
  • Loading branch information
mr-tron authored Jul 19, 2023
2 parents 1078a2f + 13df51d commit b7c51a3
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions pkg/rates/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ type storage interface {
}

func (r *ratesMock) GetCurrentRates() (map[string]float64, error) {
fiatPrices := getFiatPrices()
fiatPrices := getCoinbaseFiatPrices()
fiatExchangeratePrices := getExchangerateFiatPrices()
for currency, rate := range fiatExchangeratePrices {
_, ok := fiatPrices[currency]
if !ok {
fiatPrices[currency] = rate
}
}
tonPriceOKX := getTonOKXPrice()
tonPriceHuobi := getTonHuobiPrice()
if tonPriceOKX == 0 && tonPriceHuobi == 0 {
Expand Down Expand Up @@ -280,7 +287,31 @@ func getTonOKXPrice() float64 {
return price
}

func getFiatPrices() map[string]float64 {
func getExchangerateFiatPrices() map[string]float64 {
resp, err := http.Get("https://api.exchangerate.host/latest?base=USD")
if err != nil {
log.Errorf("can't load exchangerate prices")
return nil
}
defer resp.Body.Close()

var respBody struct {
Rates map[string]float64 `json:"rates"`
}
if err = json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
log.Errorf("failed to decode response: %v", err)
return nil
}

mapOfPrices := make(map[string]float64)
for currency, rate := range respBody.Rates {
mapOfPrices[currency] = rate
}

return mapOfPrices
}

func getCoinbaseFiatPrices() map[string]float64 {
resp, err := http.Get("https://api.coinbase.com/v2/exchange-rates?currency=USD")
if err != nil {
log.Errorf("can't load coinbase prices")
Expand Down

0 comments on commit b7c51a3

Please sign in to comment.