Skip to content

Commit

Permalink
Add api token to 0x api
Browse files Browse the repository at this point in the history
  • Loading branch information
SvineruS committed Jul 12, 2023
1 parent 552f060 commit cba8a4e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion relay/pkg/price/price_0x.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math"
"net/http"
"os"
)

const (
Expand All @@ -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"`
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cba8a4e

Please sign in to comment.