Skip to content

Commit

Permalink
Get bnb price from binance instead of 0x
Browse files Browse the repository at this point in the history
  • Loading branch information
SvineruS committed Nov 15, 2024
1 parent 3037ac6 commit 5b8a6d6
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions relay/pkg/price/price.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package price

import (
"encoding/json"
"math"
"net/http"

"github.com/ethereum/go-ethereum/common"
)

Expand All @@ -16,9 +20,35 @@ func TokenToUSD(token *TokenInfo) (price float64, err error) {
price, err = GetAmb()
} else if token.Symbol == "USDT" || token.Symbol == "BUSD" {
price, err = GetKucoin(token)
} else if token.Symbol == "WBNB" {
price, err = GetWBNB(token)
} else {
price, err = Get0x(token)
}

return price, err
}

func GetWBNB(token *TokenInfo) (price float64, err error) {
amount := math.Pow10(int(token.Decimals))

client := http.Client{}

req, err := http.NewRequest("GET", "https://api.binance.com/api/v1/ticker/price?symbol=BNBUSDT", nil)
if err != nil {
return 0, err
}

resp, err := client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()

var r response
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
return 0, err
}

return r.Price / amount, err
}

0 comments on commit 5b8a6d6

Please sign in to comment.