-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gas-oracle): support gas token volatile exchange rate (#1526)
Co-authored-by: yiweichi <[email protected]> Co-authored-by: colin <[email protected]>
- Loading branch information
1 parent
d6b9176
commit f2a656d
Showing
5 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/scroll-tech/go-ethereum/log" | ||
) | ||
|
||
var BinanceApiEndpoint string = "https://api.binance.com/api/v3/ticker/price?symbol=%s" | ||
|
||
type BinanceResponse struct { | ||
Price string `json:"price"` | ||
} | ||
|
||
func GetExchangeRateFromBinanceApi(tokenSymbolPair string, maxRetries int) (float64, error) { | ||
for i := 0; i < maxRetries; i++ { | ||
if i > 0 { | ||
time.Sleep(5 * time.Second) | ||
} | ||
|
||
// make HTTP GET request | ||
resp, err := http.Get(fmt.Sprintf(BinanceApiEndpoint, tokenSymbolPair)) | ||
if err != nil { | ||
log.Error("error making HTTP request", "err", err) | ||
continue | ||
} | ||
defer func() { | ||
err = resp.Body.Close() | ||
if err != nil { | ||
log.Error("error closing response body", "err", err) | ||
} | ||
}() | ||
|
||
// check for successful response | ||
if resp.StatusCode != http.StatusOK { | ||
log.Error("unexpected status code", "code", resp.StatusCode) | ||
continue | ||
} | ||
|
||
// read response body | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Error("error reading response body", "err", err) | ||
continue | ||
} | ||
|
||
// unmarshal JSON response | ||
var data BinanceResponse | ||
err = json.Unmarshal(body, &data) | ||
if err != nil { | ||
log.Error("error unmarshaling JSON", "err", err) | ||
continue | ||
} | ||
|
||
// convert price string to float64 | ||
price, err := strconv.ParseFloat(data.Price, 64) | ||
if err != nil { | ||
log.Error("error parsing price string", "err", err) | ||
continue | ||
} | ||
|
||
// successful response, return price | ||
return price, nil | ||
} | ||
|
||
// all retries failed, return error | ||
return 0, fmt.Errorf("failed to get exchange rate after %d retries", maxRetries) | ||
} |