Skip to content

Commit 365e0a5

Browse files
Merge pull request #101 from availproject/feat/usd-fee-span
feat: Adds USD fee correlation span
2 parents ba8d1c2 + 95211b2 commit 365e0a5

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

script/bin/operator.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use alloy::network::{ReceiptResponse, TransactionBuilder};
2+
use alloy::primitives::utils::Unit;
23
use alloy::signers::local::PrivateKeySigner;
34
use alloy::{
45
network::Network,
@@ -8,13 +9,13 @@ use alloy::{
89
};
910
use futures::future::{join_all, try_join_all};
1011
use std::env;
11-
use std::ops::Mul;
12+
use std::ops::{Div, Mul};
1213
use std::str::FromStr;
1314
use std::time::Duration;
1415
use std::{cmp::min, collections::HashMap};
1516

1617
use anyhow::{Context, Result};
17-
use services::input::{HeaderRangeRequestData, RpcDataFetcher};
18+
use services::input::{fetch_eth_to_usd_rate, HeaderRangeRequestData, RpcDataFetcher};
1819
use services::Timeout;
1920
use sp1_sdk::network::FulfillmentStrategy;
2021
use sp1_sdk::EnvProver;
@@ -814,11 +815,17 @@ where
814815
return Err(anyhow::anyhow!("Transaction reverted!"));
815816
}
816817

817-
let gas_used: u128 = receipt.gas_used() as u128;
818+
let wei = Unit::ETHER.wei_const().to::<u128>() as f64;
819+
let effective_gas_price: f64 = receipt.effective_gas_price() as f64;
820+
let effective_gas_used = effective_gas_price.mul(receipt.gas_used() as f64).div(wei);
821+
822+
let eth_to_usd_rate = fetch_eth_to_usd_rate().await;
823+
let usd_fee = effective_gas_used.mul(eth_to_usd_rate.from_asset.to_asset);
818824

819825
info!(
820826
message = "Transaction gas fee used",
821-
gas_fee = gas_used.mul(receipt.effective_gas_price()),
827+
gas_fee = effective_gas_used,
828+
usd_fee = usd_fee,
822829
tx_hash = %receipt.transaction_hash()
823830
);
824831

services/src/input.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::types::{EncodedFinalityProof, FinalityProof, VectorXJustificationApiResponse};
1+
use crate::types::{
2+
CoingekoApiResponse, EncodedFinalityProof, FinalityProof, VectorXJustificationApiResponse,
3+
};
24
use alloy::primitives::{B256, B512};
35
use anyhow::Result;
46
use codec::{Compact, Decode, Encode};
@@ -543,6 +545,19 @@ impl RpcDataFetcher {
543545
}
544546
}
545547

548+
pub async fn fetch_eth_to_usd_rate() -> CoingekoApiResponse {
549+
let coingeko_url =
550+
env::var("COINGEKO_URL").unwrap_or("https://api.coingecko.com/api".to_string());
551+
let coingeko_api_key = env::var("COINGEKO_API_KEY").expect("Missing COINGEKO_API_KEY env");
552+
let price_endpoint = format!(
553+
"{}/v3/simple/price?ids={}&vs_currencies={}&x_cg_api_key={}",
554+
coingeko_url, "ethereum", "usd", coingeko_api_key
555+
);
556+
let response = reqwest::get(price_endpoint).await.unwrap();
557+
558+
response.json::<CoingekoApiResponse>().await.unwrap()
559+
}
560+
546561
/// Converts GrandpaJustification and validator set to CircuitJustification.
547562
pub fn convert_justification_and_valset_to_circuit(
548563
justification: GrandpaJustification,

services/src/types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ pub struct VectorXJustificationApiResponse {
3232
pub error: Option<String>,
3333
}
3434

35+
#[derive(Debug, Deserialize)]
36+
pub struct CoingekoApiResponse {
37+
#[serde(alias = "ethereum")]
38+
pub from_asset: EthToUSDRate,
39+
}
40+
41+
#[derive(Debug, Deserialize)]
42+
pub struct EthToUSDRate {
43+
#[serde(alias = "usd")]
44+
pub to_asset: f64,
45+
}
46+
3547
#[cfg(test)]
3648
mod tests {
3749
use super::VectorXJustificationApiResponse;

0 commit comments

Comments
 (0)