From cba8a4e29cc668456a788a9669c1959a2ac91989 Mon Sep 17 00:00:00 2001 From: svin Date: Wed, 12 Jul 2023 17:46:42 +0300 Subject: [PATCH] Add api token to 0x api --- relay/pkg/price/price_0x.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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