Skip to content

Commit 0ed8536

Browse files
authored
Merge pull request #59 from NillionNetwork/fix/decimal-parsing
fix: parse token price correctly
2 parents 8730452 + 866e39d commit 0ed8536

File tree

5 files changed

+22
-69
lines changed

5 files changed

+22
-69
lines changed

Cargo.lock

Lines changed: 5 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ metrics = "0.24"
1818
nilauth-client = { git = "https://github.com/NillionNetwork/nilauth-client-rs", rev = "ff2020b8b21b2719ece88d82b71a9fb84532170e" }
1919
nillion-nucs = { git = "https://github.com/NillionNetwork/nuc-rs", rev = "928bfb9bb1816396afbda33a6c5aa955a6b11641" }
2020
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] }
21-
rust_decimal = "1.37"
21+
rust_decimal = { version = "1.39", features = ["serde-float", "serde-arbitrary-precision"] }
2222
serde = { version = "1.0", features = ["derive"] }
2323
serde_json = "1.0"
2424
serde_with = "3.14"

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ services:
2222
ports:
2323
- "30923:80"
2424
command: |
25-
caddy respond --listen :80 --body '{"nillion":{"usd":1}}' --header "Content-Type: application/json"
25+
caddy respond --listen :80 --body '{"nillion":{"usd":1.0}}' --header "Content-Type: application/json"

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub struct PostgresConfig {
159159
}
160160

161161
fn default_token_price_base_url() -> String {
162-
"https://pro-api.coingecko.com/".into()
162+
"https://pro-api.coingecko.com".into()
163163
}
164164

165165
fn default_coin_id() -> String {

src/services/token_price.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl TokenPriceService for CoinGeckoTokenPriceService {
5555
}
5656

5757
let params = [("ids", self.coin_id.as_str()), ("vs_currencies", "usd")];
58-
info!("Fetching token price from CoinGecko");
58+
info!("Fetching token price from CoinGecko from URL {}, and params {params:?}", self.simple_price_url);
5959

6060
let now = Instant::now();
6161
let response = self
@@ -64,7 +64,8 @@ impl TokenPriceService for CoinGeckoTokenPriceService {
6464
.query(&params)
6565
.header("X-CG-PRO-API-KEY", &self.api_key)
6666
.send()
67-
.await;
67+
.await
68+
.and_then(|r| r.error_for_status());
6869
let elapsed = now.elapsed();
6970
histogram!("nil_token_price_fetch_seconds",).record(elapsed.as_millis() as f64 / 1000.0);
7071

@@ -108,3 +109,14 @@ struct CachedPrice {
108109
struct TokenPrice {
109110
usd: Decimal,
110111
}
112+
113+
#[cfg(test)]
114+
mod tests {
115+
use super::*;
116+
117+
#[test]
118+
fn token_price_parsing() {
119+
let input = "0.249524";
120+
serde_json::from_str::<Decimal>(input).expect("parse failed");
121+
}
122+
}

0 commit comments

Comments
 (0)