diff --git a/relay/pkg/price/price_0x.go b/relay/pkg/price/price_0x.go index 68a86002..a513e736 100644 --- a/relay/pkg/price/price_0x.go +++ b/relay/pkg/price/price_0x.go @@ -6,6 +6,7 @@ import ( "fmt" "math" "net/http" + "os" ) const ( @@ -16,6 +17,8 @@ const ( var NetworkUrls = []NetworkUrl{EthUrl, BscUrl} var ErrValidationFailed = errors.New("Validation Failed") +var ApiKey = os.Getenv("0X_API_KEY") + type NetworkUrl string type response struct { Price float64 `json:"price,string"` @@ -46,12 +49,22 @@ func Get0x(token *TokenInfo) (price float64, err error) { } func doRequest(urlFormat NetworkUrl, sellToken string) (float64, error) { + client := http.Client{} + url := fmt.Sprintf(string(urlFormat), sellToken) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return 0, err + } + req.Header.Set("0x-api-key", ApiKey) - resp, err := http.Get(url) + resp, err := client.Do(req) if err != nil { return 0, err } + if resp.StatusCode >= 400 { + return 0, fmt.Errorf("0xApi answered error code %d", resp.StatusCode) + } defer resp.Body.Close() var r response