Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
Added API key and extra step to avoid assumptions on the base currenc… (
Browse files Browse the repository at this point in the history
#334)

* Added API key and extra step to avoid assumptions on the base currency returned

* rustfmt

* Version bump
  • Loading branch information
jpalvarezl authored Apr 1, 2021
1 parent 9530a59 commit 88039a0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "safe-client-gateway"
version = "1.11.1"
version = "1.11.2"
authors = ["jpalvarezl <[email protected]>", "rmeissner <[email protected]>"]
edition = "2018"

Expand Down
8 changes: 8 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ pub fn base_transaction_service_url() -> String {
format!("{}{}", env::var("TRANSACTION_SERVICE_URL").unwrap(), "/api")
}

pub fn base_exchange_api_url() -> String {
format!(
"{}?access_key={}",
env::var("EXCHANGE_API_BASE_URL").unwrap(),
env::var("EXCHANGE_API_KEY").unwrap()
)
}

pub fn webhook_token() -> String {
env::var("WEBHOOK_TOKEN").unwrap()
}
Expand Down
21 changes: 13 additions & 8 deletions src/providers/info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::config::{
address_info_cache_duration, base_transaction_service_url, exchange_api_cache_duration,
long_error_duration, safe_app_info_request_timeout, safe_app_manifest_cache_duration,
safe_info_cache_duration, short_error_duration, token_info_cache_duration,
address_info_cache_duration, base_exchange_api_url, base_transaction_service_url,
exchange_api_cache_duration, long_error_duration, safe_app_info_request_timeout,
safe_app_manifest_cache_duration, safe_info_cache_duration, short_error_duration,
token_info_cache_duration,
};
use crate::models::commons::Page;
use crate::providers::address_info::{AddressInfo, ContractInfo};
Expand Down Expand Up @@ -246,10 +247,14 @@ impl DefaultInfoProvider<'_> {
let currency_code = currency_code.to_uppercase();
let exchange = self.fetch_exchange()?;
match exchange.rates {
Some(rates) => rates
.get(&currency_code)
.cloned()
.ok_or(client_error!(422, "Currency not found")),
Some(rates) => {
let base_to_usd = rates.get("USD").unwrap_or(&0.0);
rates
.get(&currency_code)
.cloned()
.map(|base_to_requested_code| base_to_requested_code / base_to_usd)
.ok_or(client_error!(422, "Currency not found"))
}
None => Err(client_error!(422, "Currency not found")),
}
}
Expand All @@ -262,7 +267,7 @@ impl DefaultInfoProvider<'_> {
}

fn fetch_exchange(&self) -> ApiResult<Exchange> {
let url = format!("https://api.exchangeratesapi.io/latest?base=USD");
let url = base_exchange_api_url();
let body = self.cache.request_cached(
self.client,
&url,
Expand Down
2 changes: 1 addition & 1 deletion src/services/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn balances(
let backend_balances: Vec<BalanceDto> = serde_json::from_str(&body)?;

let info_provider = DefaultInfoProvider::new(&context);
let usd_to_fiat = info_provider.exchange_usd_to(fiat)?;
let usd_to_fiat = info_provider.exchange_usd_to(fiat).unwrap_or(0.0);

let mut total_fiat = 0.0;

Expand Down

0 comments on commit 88039a0

Please sign in to comment.