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 a2a9a66
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
43 changes: 16 additions & 27 deletions src/exchange_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,40 @@ sol!(
// TODO: figure out how to erase this type
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"
pub async fn grt_per_usd(provider: Url) -> watch::Receiver<NotNan<f64>> {
// https://data.chain.link/feeds/arbitrum/mainnet/grt-usd
let chainlink_usd_per_grt: 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 usd_per_grt = ChainlinkPriceFeed::new(chainlink_usd_per_grt, 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_grt = match fetch_price(&usd_per_grt).await {
Ok(usd_per_grt) => usd_per_grt,
Err(exchange_rate_err) => {
tracing::error!(%exchange_rate_err);
continue;
}
};
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 NotNan::new(usd_per_grt.recip()) {
Ok(grt_per_usd) => 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();
}
});

let _ = rx.wait_for(|v| *v != 0.0).await;
Ok(rx)
rx.wait_for(|v| *v != 0.0).await.unwrap();
rx
}

async fn fetch_price(
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async fn main() {

let grt_per_usd = match conf.exchange_rate_provider {
ExchangeRateProvider::Fixed(grt_per_usd) => watch::channel(grt_per_usd).1,
ExchangeRateProvider::Rpc(url) => exchange_rate::grt_per_usd(url).await.unwrap(),
ExchangeRateProvider::Rpc(url) => exchange_rate::grt_per_usd(url).await,
};

let attestation_domain: &'static Eip712Domain =
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 a2a9a66

Please sign in to comment.