feat: chunk correctly coinmarketcap calls

This commit is contained in:
Anthony Laibe 2023-03-30 14:28:08 +02:00 committed by Anthony Laibe
parent e92049b9ca
commit 540001e137
3 changed files with 25 additions and 27 deletions

View File

@ -1,7 +1,6 @@
package market
import (
"fmt"
"sync"
"time"
@ -77,7 +76,6 @@ func (pm *Manager) makeCall(main func() (any, error), fallback func() (any, erro
errChan := hystrix.Go("marketClient", func() error {
res, err := main()
if err != nil {
fmt.Println("MARKET ERROR", err)
return err
}
pm.setIsConnected(true)
@ -90,7 +88,6 @@ func (pm *Manager) makeCall(main func() (any, error), fallback func() (any, erro
res, err := fallback()
if err != nil {
fmt.Println("FALLBACK MARKET ERROR", err)
pm.setIsConnected(false)
return err
}

View File

@ -51,7 +51,7 @@ func (c *Client) DoQuery(url string) (*http.Response, error) {
}
func (c *Client) FetchPrices(symbols []string, currencies []string) (map[string]map[string]float64, error) {
chunks := utils.ChunkSymbols(symbols)
chunks := utils.ChunkSymbols(symbols, 60)
result := make(map[string]map[string]float64)
realCurrencies := utils.RenameSymbols(currencies)
for _, smbls := range chunks {
@ -113,32 +113,33 @@ func (c *Client) FetchTokenDetails(symbols []string) (map[string]thirdparty.Toke
}
func (c *Client) FetchTokenMarketValues(symbols []string, currency string) (map[string]thirdparty.TokenMarketValues, error) {
chunks := utils.ChunkSymbols(symbols)
realCurrency := utils.GetRealSymbol(currency)
realSymbols := utils.RenameSymbols(symbols)
item := map[string]thirdparty.TokenMarketValues{}
for _, smbls := range chunks {
realSymbols := utils.RenameSymbols(smbls)
url := fmt.Sprintf("%s/data/pricemultifull?fsyms=%s&tsyms=%s&extraParams=Status.im", baseURL, strings.Join(realSymbols, ","), realCurrency)
resp, err := c.DoQuery(url)
if err != nil {
return item, err
}
defer resp.Body.Close()
url := fmt.Sprintf("%s/data/pricemultifull?fsyms=%s&tsyms=%s&extraParams=Status.im", baseURL, strings.Join(realSymbols, ","), realCurrency)
resp, err := c.DoQuery(url)
if err != nil {
return item, err
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return item, err
}
container := MarketValuesContainer{}
err = json.Unmarshal(body, &container)
if err != nil {
return item, err
}
for _, symbol := range smbls {
item[symbol] = container.Raw[utils.GetRealSymbol(symbol)][utils.GetRealSymbol(currency)]
}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return item, err
}
container := MarketValuesContainer{}
err = json.Unmarshal(body, &container)
if err != nil {
return item, err
}
for _, symbol := range symbols {
item[symbol] = container.Raw[utils.GetRealSymbol(symbol)][utils.GetRealSymbol(currency)]
}
return item, nil
}

View File

@ -22,7 +22,7 @@ func GetRealSymbol(symbol string) string {
func ChunkSymbols(symbols []string, chunkSizeOptional ...int) [][]string {
var chunks [][]string
chunkSize := 20
chunkSize := 100
if len(chunkSizeOptional) > 0 {
chunkSize = chunkSizeOptional[0]
}