Skip to content

Instantly share code, notes, and snippets.

View Enigo's full-sized avatar

Ruslan S. Enigo

View GitHub Profile
pub struct AssetSaver;
#[async_trait]
impl Persistable<Asset> for AssetSaver {
async fn persist_one(&self, asset: &Asset, pool: &Pool<Postgres>) {
let asset_result = &asset.result;
let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
"insert into asset (token_id, token_address, name, tier, solon, carbon, crypton, silicon, hydrogen, hyperion, landmark) ",
);
pub struct MintSaver;
#[async_trait]
impl Persistable<Mint> for MintSaver {
async fn persist_one(&self, mint: &Mint, pool: &Pool<Postgres>) {
let mint_result = &mint.result;
let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
"insert into mint (transaction_id, status, wallet, token_type, token_id, minted_on) ",
);
...
#[async_trait]
pub trait Persistable<T> {
async fn persist_one(&self, result: &T, pool: &Pool<Postgres>);
}
#[tokio::main]
pub async fn read() {
read_with_cursor_as::<Mint>(MINTS_URL).await;
read_with_cursor_as::<Asset>(ASSETS_URL).await;
}
async fn read_with_cursor_as<T: DeserializeOwned + PaginatedApi>(
url: &str,
) {
let pool = db_handler::open_connection().await;
#[derive(Deserialize, Debug)]
pub struct Asset {
pub result: Vec<TheResult>,
pub cursor: String,
}
impl PaginatedApi for Asset {
fn get_cursor(&self) -> String {
self.cursor.clone()
}
#[derive(Deserialize, Debug)]
pub struct Mint {
pub result: Vec<TheResult>,
pub cursor: String,
}
impl PaginatedApi for Mint {
fn get_cursor(&self) -> String {
self.cursor.clone()
}
pub trait PaginatedApi {
fn get_cursor(&self) -> String;
fn has_results(&self) -> bool;
}
const MINTS_URL: &str = "https://api.x.immutable.com/v1/mints?token_address=0x9e0d99b864e1ac12565125c5a82b59adea5a09cd&page_size=200";
const ASSETS_URL: &str = "https://api.x.immutable.com/v1/assets?collection=0x9e0d99b864e1ac12565125c5a82b59adea5a09cd&page_size=200";
#[tokio::main]
pub async fn read() {
read_with_cursor_as::<Mint>(MINTS_URL).await;
read_with_cursor_as::<Asset>(ASSETS_URL).await;
}
async fn read_with_cursor_as<T: DeserializeOwned>(
#[derive(Deserialize, Debug)]
pub struct Asset {
pub result: Vec<TheResult>,
pub cursor: String,
}
#[derive(Deserialize, Debug)]
pub struct TheResult {
pub token_id: String,
pub token_address: String,
pub fn as_parsed<T: FromStr>(key: &str) -> T
where
<T as FromStr>::Err: Debug,
{
as_string(key)
.parse::<T>()
.expect(format!("{} should be a valid u16", key).as_str())
}
pub fn as_string(key: &str) -> String {