Skip to content

Instantly share code, notes, and snippets.

@Enigo
Last active January 23, 2023 10:31
Show Gist options
  • Save Enigo/186de9a30310cade078fa6d6abf3b272 to your computer and use it in GitHub Desktop.
Save Enigo/186de9a30310cade078fa6d6abf3b272 to your computer and use it in GitHub Desktop.
use log::{error, info};
use serde::de::DeserializeOwned;
use crate::model::mint::Mint;
const MINTS_URL: &str = "https://api.x.immutable.com/v1/mints?token_address=0x9e0d99b864e1ac12565125c5a82b59adea5a09cd&page_size=200";
#[tokio::main]
pub async fn read() {
let mut cursor = None;
loop {
cursor = execute_and_get_cursor(cursor).await;
if cursor.is_none() {
break;
} else {
info!("Current cursor: {}", cursor.clone().unwrap());
}
}
}
async fn execute_and_get_cursor(cursor: Option<String>) -> Option<String> {
let url = if cursor.is_some() { MINTS_URL.to_owned() + "&cursor=" + cursor.unwrap().as_str() } else { String::from(MINTS_URL) };
let response = fetch_api_response::<Mint>(url.as_str()).await;
match response {
Ok(mint) => {
info!("Processing mint response");
if !mint.result.is_empty() {
// will be saved to DB later
info!("{:?}", mint.result);
}
if !mint.cursor.is_empty() {
return Some(mint.cursor);
}
None
}
Err(e) => {
error!("Mints API response cannot be parsed! {}", e);
None
}
}
}
async fn fetch_api_response<T: DeserializeOwned>(endpoint: &str) -> reqwest::Result<T> {
let result = reqwest::get(endpoint)
.await?.json::<T>()
.await?;
return Ok(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment