From 5b8a6d632e64ec9c674caad1eba9bca119123caf Mon Sep 17 00:00:00 2001 From: svin Date: Fri, 15 Nov 2024 10:33:29 +0200 Subject: [PATCH] Get bnb price from binance instead of 0x --- relay/pkg/price/price.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/relay/pkg/price/price.go b/relay/pkg/price/price.go index e3104ecb..11ea3141 100644 --- a/relay/pkg/price/price.go +++ b/relay/pkg/price/price.go @@ -1,6 +1,10 @@ package price import ( + "encoding/json" + "math" + "net/http" + "github.com/ethereum/go-ethereum/common" ) @@ -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 +}