Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate dotenv;
- extern crate reqwest;
- use ethers::prelude::*;
- use ethers::providers::{Provider, Http};
- use ethers::contract::abigen;
- use dotenv::dotenv;
- use std::env;
- use std::sync::Arc;
- use std::convert::TryFrom;
- abigen!(
- UniswapV3Pool,
- r#"[
- {"constant":true,"inputs":[],"name":"liquidity","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"payable":false,"stateMutability":"view","type":"function"}
- ]"#
- );
- #[derive(Clone)]
- pub struct InfuraClient {
- provider_url: String,
- provider: Provider<Http>,
- }
- impl InfuraClient {
- pub fn new(infura_key: &str) -> Self {
- let provider_url = format!("https://mainnet.infura.io/v3/{}", infura_key);
- let provider = Provider::<Http>::try_from(provider_url.as_str()).expect("could not create http provider");
- InfuraClient {
- provider_url: provider_url,
- provider: provider,
- }
- }
- pub async fn get_liquidity(&self, pool_address_str: &str) -> Result<U256, Box<dyn std::error::Error>> {
- let pool_address: Address = pool_address_str.parse().unwrap();
- let pool_contract = UniswapV3Pool::new(pool_address, Arc::new(self.provider.clone()));
- let liquidity: U256 = pool_contract.liquidity().call().await?.into();
- Ok(liquidity)
- }
- pub fn get_provider_url(&self) -> String {
- self.provider_url.clone()
- }
- }
- #[tokio::main]
- async fn main() -> Result<(), Box<dyn std::error::Error>> {
- dotenv().ok();
- let infura_project_id = env::var("INFURA_PROJECT_ID").expect("INFURA_PROJECT_ID must be set");
- let client = InfuraClient::new(&infura_project_id);
- let liquidity = client.get_liquidity("0x4585fe77225b41b697c938b018e2ac67ac5a20c0").await?;
- println!("Liquidity: {:?}", liquidity);
- // let token_price_in_usd = get_token_price("ethereum").await?;
- // let liquidity_in_usd = liquidity.as_u128() as f64 * token_price_in_usd;
- // println!("Liquidity in USD: ${:.2}", liquidity_in_usd);
- Ok(())
- }
- // async fn get_token_price(token_id: &str) -> Result<f64, Box<dyn std::error::Error>> {
- // let url = format!("https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=usd", token_id);
- // let response = reqwest::get(&url).await?.json::<serde_json::Value>().await?;
- // let price = response[token_id]["usd"].as_f64().ok_or("Couldn't get the price")?;
- // Ok(price)
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement