Advertisement
den4ik2003

Untitled

Aug 9th, 2024
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.37 KB | None | 0 0
  1. extern crate regex;
  2. extern crate num_bigint;
  3. extern crate num_traits;
  4.  
  5.  
  6. use regex::Regex;
  7. use reqwest;
  8. use serde_json::{json, Value};
  9. use std::{error::Error};
  10. use num_bigint::BigUint;
  11. use num_traits::{One, ToPrimitive};
  12.  
  13. #[derive(Clone)]
  14. pub struct Client {
  15.     project_id: String,
  16.     inner_client: reqwest::blocking::Client,
  17. }
  18.  
  19. impl Client {
  20.  
  21.     pub fn new(project_id: String) -> Self {
  22.         Client {
  23.             project_id: project_id,
  24.             inner_client: reqwest::blocking::Client::new(),
  25.         }
  26.     }
  27.  
  28.     pub fn get_price_in_pool(&self /*, pool_address: String, func_hash: String */) -> Result<(), Box<dyn Error>> {
  29.         let url = format!("https://mainnet.infura.io/v3/{}", self.project_id);
  30.    
  31.         let client = &self.inner_client;
  32.  
  33.         let body = json!({
  34.             "jsonrpc": "2.0",
  35.             "method": "eth_call",
  36.             "params": [{
  37.                 "to": "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640",
  38.                 "data": "0x3850c7bd"
  39.             }, "latest"],
  40.             "id": 1
  41.         });
  42.  
  43.         let response = client
  44.             .post(url.as_str())
  45.             .header("Content-Type", "application/json")
  46.             .json(&body) // Automatically serializes the body to JSON
  47.             .send()?;
  48.  
  49.         let json: Value = response.json()?;
  50.         let result = json.get("result").unwrap().as_str().unwrap();
  51.  
  52.         let re_prefix = Regex::new(r"0x0*([a-fA-F0-9]+)").unwrap();
  53.        
  54.         let mut s = String::new();
  55.  
  56.         for cap in re_prefix.captures_iter(result) {
  57.             s = cap[1].to_string();
  58.         }
  59.  
  60.         let mut ans = String::new();
  61.         let re_suffix = Regex::new(r"^(.*?)(?:0000|$)").unwrap();
  62.  
  63.         for cap in re_suffix.captures_iter(&s) {
  64.             ans = cap[1].to_string();
  65.             println!("{}", &cap[1]);
  66.         }
  67.  
  68.         // Используем BigUint для больших чисел.
  69.         let num = BigUint::parse_bytes(ans.as_bytes(), 16).unwrap();
  70.  
  71.         // Используем BigUint для вычислений.
  72.         let base: BigUint = BigUint::one() << 192;
  73.         let price = num.pow(2).to_f64().unwrap() / base.to_f64().unwrap();
  74.         println!("{:.2}", (1.0 / price) * 10f64.powi(12));
  75.  
  76.         Ok(())
  77.     }
  78.  
  79. }
  80.  
  81. fn main() {
  82.     let _= Client::new("ab65c51db1f44b23a92b3426161e8c1d".into()).get_price_in_pool();
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement