Skip to content

Commit bbc6bd0

Browse files
authored
fix: set User-Agent header for coingecko requests (#4604)
## What ❔ Sets User-Agent header for coingecko requests. ## Why ❔ Requests started to fail with 403 status code and message suggesting adding the header. ## Is this a breaking change? - [ ] Yes - [x] No ## Operational changes None ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zkstack dev fmt` and `zkstack dev lint`.
1 parent 3c97623 commit bbc6bd0

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

core/lib/external_price_api/src/coingecko_api.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@ pub struct CoinGeckoPriceAPIClient {
1717

1818
const DEFAULT_COINGECKO_API_URL: &str = "https://pro-api.coingecko.com";
1919
const COINGECKO_AUTH_HEADER: &str = "x-cg-pro-api-key";
20+
const USER_AGENT_HEADER: &str = "user-agent";
21+
const USER_AGENT_VALUE: &str = "zksync-era-node/0.1 (https://github.com/matter-labs/zksync-era)";
2022
const ETH_ID: &str = "eth";
2123
const ZKSYNC_ID: &str = "zksync";
2224

2325
impl CoinGeckoPriceAPIClient {
2426
pub fn new(config: ExternalPriceApiClientConfig) -> Self {
25-
let client = if let Some(api_key) = &config.api_key {
26-
let mut headers = reqwest::header::HeaderMap::new();
27+
let mut headers = reqwest::header::HeaderMap::new();
28+
headers.insert(
29+
reqwest::header::HeaderName::from_static(USER_AGENT_HEADER),
30+
reqwest::header::HeaderValue::from_static(USER_AGENT_VALUE),
31+
);
32+
if let Some(api_key) = &config.api_key {
2733
headers.insert(
2834
reqwest::header::HeaderName::from_static(COINGECKO_AUTH_HEADER),
2935
reqwest::header::HeaderValue::from_str(api_key)
3036
.expect("Failed to create header value"),
3137
);
32-
33-
reqwest::Client::builder()
34-
.default_headers(headers)
35-
.timeout(config.client_timeout)
36-
.build()
37-
.expect("Failed to build reqwest client")
38-
} else {
39-
reqwest::Client::new()
40-
};
38+
}
39+
let client = reqwest::Client::builder()
40+
.default_headers(headers)
41+
.timeout(config.client_timeout)
42+
.build()
43+
.expect("Failed to build reqwest client");
4144

4245
let base_url = config
4346
.base_url
@@ -173,6 +176,7 @@ mod test {
173176

174177
when = when.query_param("contract_addresses", address.clone());
175178
when = when.query_param("vs_currencies", ETH_ID);
179+
when = when.header(USER_AGENT_HEADER, USER_AGENT_VALUE);
176180
api_key.map(|key| when.header(COINGECKO_AUTH_HEADER, key));
177181

178182
if let Some(p) = price {

0 commit comments

Comments
 (0)