Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate regex;
- extern crate num_bigint;
- extern crate num_traits;
- use regex::Regex;
- use reqwest;
- use serde_json::{json, Value};
- use std::{error::Error};
- use num_bigint::BigUint;
- use num_traits::{One, ToPrimitive};
- #[derive(Clone)]
- pub struct Client {
- project_id: String,
- inner_client: reqwest::blocking::Client,
- }
- impl Client {
- pub fn new(project_id: String) -> Self {
- Client {
- project_id: project_id,
- inner_client: reqwest::blocking::Client::new(),
- }
- }
- pub fn get_price_in_pool(&self /*, pool_address: String, func_hash: String */) -> Result<(), Box<dyn Error>> {
- let url = format!("https://mainnet.infura.io/v3/{}", self.project_id);
- let client = &self.inner_client;
- let body = json!({
- "jsonrpc": "2.0",
- "method": "eth_call",
- "params": [{
- "to": "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640",
- "data": "0x3850c7bd"
- }, "latest"],
- "id": 1
- });
- let response = client
- .post(url.as_str())
- .header("Content-Type", "application/json")
- .json(&body) // Automatically serializes the body to JSON
- .send()?;
- let json: Value = response.json()?;
- let result = json.get("result").unwrap().as_str().unwrap();
- let re_prefix = Regex::new(r"0x0*([a-fA-F0-9]+)").unwrap();
- let mut s = String::new();
- for cap in re_prefix.captures_iter(result) {
- s = cap[1].to_string();
- }
- let mut ans = String::new();
- let re_suffix = Regex::new(r"^(.*?)(?:0000|$)").unwrap();
- for cap in re_suffix.captures_iter(&s) {
- ans = cap[1].to_string();
- println!("{}", &cap[1]);
- }
- // Используем BigUint для больших чисел.
- let num = BigUint::parse_bytes(ans.as_bytes(), 16).unwrap();
- // Используем BigUint для вычислений.
- let base: BigUint = BigUint::one() << 192;
- let price = num.pow(2).to_f64().unwrap() / base.to_f64().unwrap();
- println!("{:.2}", (1.0 / price) * 10f64.powi(12));
- Ok(())
- }
- }
- fn main() {
- let _= Client::new("ab65c51db1f44b23a92b3426161e8c1d".into()).get_price_in_pool();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement