Advertisement
den4ik2003

Untitled

Aug 9th, 2024
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.42 KB | None | 0 0
  1. extern crate dotenv;
  2. extern crate reqwest;
  3.  
  4. use ethers::prelude::*;
  5. use ethers::providers::{Provider, Http};
  6. use ethers::contract::abigen;
  7. use dotenv::dotenv;
  8. use std::env;
  9. use std::sync::Arc;
  10. use std::convert::TryFrom;
  11.  
  12. abigen!(
  13.     UniswapV3Pool,
  14.     r#"[
  15.        {"constant":true,"inputs":[],"name":"liquidity","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"payable":false,"stateMutability":"view","type":"function"}
  16.    ]"#
  17. );
  18.  
  19. #[derive(Clone)]
  20. pub struct InfuraClient {
  21.     provider_url: String,
  22.     provider: Provider<Http>,
  23. }
  24.  
  25. impl InfuraClient {
  26.     pub fn new(infura_key: &str) -> Self {
  27.         let provider_url = format!("https://mainnet.infura.io/v3/{}", infura_key);
  28.         let provider = Provider::<Http>::try_from(provider_url.as_str()).expect("could not create http provider");
  29.  
  30.         InfuraClient {
  31.             provider_url: provider_url,
  32.             provider: provider,
  33.         }
  34.     }
  35.  
  36.     pub async fn get_liquidity(&self, pool_address_str: &str) -> Result<U256, Box<dyn std::error::Error>> {
  37.         let pool_address: Address = pool_address_str.parse().unwrap();
  38.         let pool_contract = UniswapV3Pool::new(pool_address, Arc::new(self.provider.clone()));
  39.  
  40.         let liquidity: U256 = pool_contract.liquidity().call().await?.into();
  41.         Ok(liquidity)
  42.     }
  43.  
  44.     pub fn get_provider_url(&self) -> String {
  45.         self.provider_url.clone()
  46.     }
  47. }
  48.  
  49. #[tokio::main]
  50. async fn main() -> Result<(), Box<dyn std::error::Error>> {    
  51.     dotenv().ok();
  52.     let infura_project_id = env::var("INFURA_PROJECT_ID").expect("INFURA_PROJECT_ID must be set");
  53.     let client = InfuraClient::new(&infura_project_id);
  54.  
  55.     let liquidity = client.get_liquidity("0x4585fe77225b41b697c938b018e2ac67ac5a20c0").await?;
  56.     println!("Liquidity: {:?}", liquidity);
  57.  
  58.     // let token_price_in_usd = get_token_price("ethereum").await?;
  59.     // let liquidity_in_usd = liquidity.as_u128() as f64 * token_price_in_usd;
  60.     // println!("Liquidity in USD: ${:.2}", liquidity_in_usd);
  61.  
  62.     Ok(())
  63. }
  64.  
  65. // async fn get_token_price(token_id: &str) -> Result<f64, Box<dyn std::error::Error>> {
  66. //     let url = format!("https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=usd", token_id);
  67. //     let response = reqwest::get(&url).await?.json::<serde_json::Value>().await?;
  68. //     let price = response[token_id]["usd"].as_f64().ok_or("Couldn't get the price")?;
  69. //     Ok(price)
  70. // }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement