Skip to content

Commit

Permalink
feat: Add CoinMarketCap external API (#2971)
Browse files Browse the repository at this point in the history
## What ❔

Adds support for CoinMarketCap as a price API for base token prices.

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->

## Why ❔

Currently, the only supported API is CoinGecko. It's not ideal to only
support a single 3rd party API here, so this provides some more degrees
of freedom, and makes base token less reliant on a single third-party
provider.

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated. (See #2970)
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
encody authored Oct 15, 2024
1 parent 977d157 commit c1cb30e
Show file tree
Hide file tree
Showing 10 changed files with 463 additions and 178 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 3 additions & 21 deletions core/bin/zksync_server/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use zksync_node_framework::{
implementations::layers::{
base_token::{
base_token_ratio_persister::BaseTokenRatioPersisterLayer,
base_token_ratio_provider::BaseTokenRatioProviderLayer,
coingecko_client::CoingeckoClientLayer, forced_price_client::ForcedPriceClientLayer,
no_op_external_price_api_client::NoOpExternalPriceApiClientLayer,
base_token_ratio_provider::BaseTokenRatioProviderLayer, ExternalPriceApiLayer,
},
circuit_breaker_checker::CircuitBreakerCheckerLayer,
commitment_generator::CommitmentGeneratorLayer,
Expand Down Expand Up @@ -557,24 +555,8 @@ impl MainNodeBuilder {

fn add_external_api_client_layer(mut self) -> anyhow::Result<Self> {
let config = try_load_config!(self.configs.external_price_api_client_config);
match config.source.as_str() {
CoingeckoClientLayer::CLIENT_NAME => {
self.node.add_layer(CoingeckoClientLayer::new(config));
}
NoOpExternalPriceApiClientLayer::CLIENT_NAME => {
self.node.add_layer(NoOpExternalPriceApiClientLayer);
}
ForcedPriceClientLayer::CLIENT_NAME => {
self.node.add_layer(ForcedPriceClientLayer::new(config));
}
_ => {
anyhow::bail!(
"Unknown external price API client source: {}",
config.source
);
}
}

self.node
.add_layer(ExternalPriceApiLayer::try_from(config)?);
Ok(self)
}

Expand Down
4 changes: 4 additions & 0 deletions core/lib/external_price_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ serde.workspace = true
reqwest = { workspace = true, features = ["json"] }
fraction.workspace = true
rand.workspace = true
tracing.workspace = true

zksync_config.workspace = true
zksync_types.workspace = true
tokio.workspace = true

[dev-dependencies]
httpmock.workspace = true
serde_json.workspace = true
Loading

0 comments on commit c1cb30e

Please sign in to comment.