Skip to content

Commit

Permalink
fix: update exchange rate price feed
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Nov 23, 2024
1 parent 7732aaa commit 1f6afed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
35 changes: 10 additions & 25 deletions src/exchange_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,30 @@ sol!(
type Provider = alloy::providers::RootProvider<Http<reqwest::Client>>;

pub async fn grt_per_usd(provider: Url) -> anyhow::Result<watch::Receiver<NotNan<f64>>> {
// https://data.chain.link/ethereum/mainnet/crypto-eth/grt-eth
let chainlink_eth_per_grt: Address = "0x17d054ecac33d91f7340645341efb5de9009f1c1"
.parse()
.unwrap();
// https://data.chain.link/ethereum/mainnet/crypto-usd/eth-usd
let chainlink_usd_per_eth: Address = "0x5f4ec3df9cbd43714fe2740f5e3616155c5b8419"
// https://data.chain.link/feeds/arbitrum/mainnet/grt-usd
let chainlink_grt_per_usd: Address = "0x0F38D86FceF4955B705F35c9e41d1A16e0637c73"
.parse()
.unwrap();

let provider = Arc::new(ProviderBuilder::new().on_http(provider));
let eth_per_grt = ChainlinkPriceFeed::new(chainlink_eth_per_grt, provider.clone());
let usd_per_eth = ChainlinkPriceFeed::new(chainlink_usd_per_eth, provider);
let grt_per_usd = ChainlinkPriceFeed::new(chainlink_grt_per_usd, provider);

let (tx, mut rx) = watch::channel(NotNan::new(0.0).unwrap());
tokio::spawn(async move {
let mut interval = interval(Duration::from_secs(60));
interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
loop {
interval.tick().await;

let eth_per_grt = match fetch_price(&eth_per_grt).await {
Ok(eth_per_grt) => eth_per_grt,
Err(eth_per_grt_err) => {
tracing::error!(%eth_per_grt_err);
return;
}
};
let usd_per_eth = match fetch_price(&usd_per_eth).await {
Ok(usd_per_eth) => usd_per_eth,
Err(usd_per_eth_err) => {
tracing::error!(%usd_per_eth_err);
return;
let grt_per_usd = match fetch_price(&grt_per_usd).await {
Ok(grt_per_usd) => grt_per_usd,
Err(grt_per_usd_err) => {
tracing::error!(%grt_per_usd_err);
continue;
}
};
let grt_per_usd = NotNan::new((eth_per_grt * usd_per_eth).recip()).unwrap();
tracing::info!(%grt_per_usd);
if let Err(grt_per_usd_send_err) = tx.send(grt_per_usd) {
tracing::error!(%grt_per_usd_send_err);
}
tx.send(grt_per_usd).unwrap();
}
});

Expand Down
3 changes: 1 addition & 2 deletions src/receipts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{collections::HashMap, sync::Arc, time::SystemTime};

use alloy::primitives::U256;
use alloy::{dyn_abi::Eip712Domain, signers::local::PrivateKeySigner};
use alloy::{dyn_abi::Eip712Domain, primitives::U256, signers::local::PrivateKeySigner};
use parking_lot::{Mutex, RwLock};
use rand::RngCore;
pub use receipts::QueryStatus as ReceiptStatus;
Expand Down

0 comments on commit 1f6afed

Please sign in to comment.