Skip to content

Feature/add gbp currency #5453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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

This file was deleted.

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

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

2 changes: 1 addition & 1 deletion nyx-chain-watcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[package]
name = "nyx-chain-watcher"
version = "0.1.9"
version = "0.1.10"
authors.workspace = true
repository.workspace = true
homepage.workspace = true
Expand Down
1 change: 1 addition & 0 deletions nyx-chain-watcher/migrations/004_add_gbp_column.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE price_history ADD COLUMN gbp REAL NOT NULL DEFAULT 0;
2 changes: 2 additions & 0 deletions nyx-chain-watcher/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) struct CurrencyPrices {
pub(crate) chf: f32,
pub(crate) usd: f32,
pub(crate) eur: f32,
pub(crate) gbp: f32,
pub(crate) btc: f32,
}

Expand All @@ -27,6 +28,7 @@ pub(crate) struct PriceHistory {
pub(crate) chf: f64,
pub(crate) usd: f64,
pub(crate) eur: f64,
pub(crate) gbp: f64,
pub(crate) btc: f64,
}

Expand Down
14 changes: 10 additions & 4 deletions nyx-chain-watcher/src/db/queries/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ pub(crate) async fn insert_nym_prices(
let timestamp = price_data.timestamp;
sqlx::query!(
"INSERT INTO price_history
(timestamp, chf, usd, eur, btc)
(timestamp, chf, usd, eur, gbp, btc)
VALUES
($1, $2, $3, $4, $5)
($1, $2, $3, $4, $5, $6)
ON CONFLICT(timestamp) DO UPDATE SET
chf=excluded.chf,
usd=excluded.usd,
eur=excluded.eur,
gbp=excluded.gbp,
btc=excluded.btc;",
timestamp,
price_data.nym.chf,
price_data.nym.usd,
price_data.nym.eur,
price_data.nym.gbp,
price_data.nym.btc,
)
.execute(&mut *conn)
Expand All @@ -33,7 +35,7 @@ pub(crate) async fn insert_nym_prices(

pub(crate) async fn get_latest_price(pool: &DbPool) -> anyhow::Result<PriceHistory> {
let result = sqlx::query!(
"SELECT timestamp, chf, usd, eur, btc FROM price_history ORDER BY timestamp DESC LIMIT 1;"
"SELECT timestamp, chf, usd, eur, gbp, btc FROM price_history ORDER BY timestamp DESC LIMIT 1;"
)
.fetch_one(pool)
.await?;
Expand All @@ -43,6 +45,7 @@ pub(crate) async fn get_latest_price(pool: &DbPool) -> anyhow::Result<PriceHisto
chf: result.chf,
usd: result.usd,
eur: result.eur,
gbp: result.gbp,
btc: result.btc,
})
}
Expand All @@ -52,7 +55,7 @@ pub(crate) async fn get_average_price(pool: &DbPool) -> anyhow::Result<PriceHist
let earliest_timestamp = Local::now().sub(chrono::Duration::days(1)).timestamp();

let result = sqlx::query!(
"SELECT timestamp, chf, usd, eur, btc FROM price_history WHERE timestamp >= $1;",
"SELECT timestamp, chf, usd, eur, gbp, btc FROM price_history WHERE timestamp >= $1;",
earliest_timestamp
)
.fetch_all(pool)
Expand All @@ -65,20 +68,23 @@ pub(crate) async fn get_average_price(pool: &DbPool) -> anyhow::Result<PriceHist
chf: 0f64,
usd: 0f64,
eur: 0f64,
gbp: 0f64,
btc: 0f64,
};

for p in &result {
price.chf += p.chf;
price.usd += p.usd;
price.eur += p.eur;
price.gbp += p.gbp;
price.btc += p.btc;
}

if count > 0f64 {
price.chf /= count;
price.usd /= count;
price.eur /= count;
price.gbp /= count;
price.btc /= count;
}

Expand Down
2 changes: 1 addition & 1 deletion nyx-chain-watcher/src/price_scraper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::db::DbPool;
const REFRESH_DELAY: Duration = Duration::from_secs(300);
const FAILURE_RETRY_DELAY: Duration = Duration::from_secs(60 * 2);
const COINGECKO_API_URL: &str =
"https://api.coingecko.com/api/v3/simple/price?ids=nym&vs_currencies=chf,usd,eur,btc";
"https://api.coingecko.com/api/v3/simple/price?ids=nym&vs_currencies=chf,usd,eur,gbp,btc";

pub(crate) async fn run_price_scraper(db_pool: &DbPool) -> JoinHandle<()> {
loop {
Expand Down
Loading