From 7fade566beba1a592ece5d95fd29296a1bcd33fd Mon Sep 17 00:00:00 2001 From: Roshan <19766713+rpalakkal@users.noreply.github.com> Date: Thu, 20 Jun 2024 21:42:56 -0400 Subject: [PATCH] chore: replace pending nft with fetched token id --- src/actions/nft.rs | 38 ++++++++++++++++++++++++-------------- src/db/in_memory.rs | 41 +++++++++++++++++++++++------------------ src/db/mod.rs | 23 ++++++++++++----------- src/endpoints.rs | 26 +++++++++++++++++--------- src/main.rs | 11 +++++++++-- 5 files changed, 85 insertions(+), 54 deletions(-) diff --git a/src/actions/nft.rs b/src/actions/nft.rs index a4bf597..85e4baf 100644 --- a/src/actions/nft.rs +++ b/src/actions/nft.rs @@ -61,20 +61,20 @@ pub async fn subscribe_to_nft_events( } } } - NFTEvents::NewTokenData(new_token_data) => { - let mut db = db.lock().await; - db.promote_pending_nft( - log.transaction_hash.unwrap().encode_hex_with_prefix(), - new_token_data.tokenId.to_string(), - ) - .await?; - drop(db); - log::info!( - "NFT minted with id {} to address {}", - new_token_data.tokenId.to_string(), - new_token_data.to.to_string() - ); - } + // NFTEvents::NewTokenData(new_token_data) => { + // let mut db = db.lock().await; + // db.promote_pending_nft( + // log.transaction_hash.unwrap().encode_hex_with_prefix(), + // new_token_data.tokenId.to_string(), + // ) + // .await?; + // drop(db); + // log::info!( + // "NFT minted with id {} to address {}", + // new_token_data.tokenId.to_string(), + // new_token_data.to.to_string() + // ); + // } _ => continue, } } @@ -133,6 +133,16 @@ pub async fn send_eth( Ok(()) } +pub async fn get_curr_token_id(rpc_url: String) -> eyre::Result { + let rpc_url = rpc_url.parse()?; + let provider = ProviderBuilder::new().on_http(rpc_url); + let nft = NFT::new(NFT_ADDRESS, provider); + let curr_token_id = nft.currentTokenId().call().await?._0; + let curr_token_id: u64 = curr_token_id.to::(); + Ok(curr_token_id) + // Ok(curr_token_id.to_string()) +} + #[cfg(test)] mod tests { use alloy::signers::local::{coins_bip39::English, MnemonicBuilder}; diff --git a/src/db/in_memory.rs b/src/db/in_memory.rs index 18dabe8..d5e8b7f 100644 --- a/src/db/in_memory.rs +++ b/src/db/in_memory.rs @@ -2,13 +2,13 @@ use std::collections::BTreeMap; use serde::{Deserialize, Serialize}; -use super::{PendingNFT, TeleportDB, User, NFT}; +use super::{TeleportDB, User, NFT}; #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct InMemoryDB { pub x_id_to_teleport_id: BTreeMap, pub users: BTreeMap, - pub pending_nfts: BTreeMap, + // pub pending_nfts: BTreeMap, pub nfts: BTreeMap, pub tweets: BTreeMap, } @@ -51,22 +51,27 @@ impl TeleportDB for InMemoryDB { Ok(serialized) } - async fn add_pending_nft( - &mut self, - tx_hash: String, - pending_nft: PendingNFT, - ) -> eyre::Result<()> { - self.pending_nfts.insert(tx_hash, pending_nft); - Ok(()) - } - - async fn promote_pending_nft(&mut self, tx_hash: String, token_id: String) -> eyre::Result<()> { - let pending_nft = self - .pending_nfts - .remove(&tx_hash) - .ok_or_else(|| eyre::eyre!("Pending NFT not found"))?; - let nft = NFT { teleport_id: pending_nft.teleport_id, token_id }; - self.nfts.insert(pending_nft.nft_id, nft); + // async fn add_pending_nft( + // &mut self, + // tx_hash: String, + // pending_nft: PendingNFT, + // ) -> eyre::Result<()> { + // self.pending_nfts.insert(tx_hash, pending_nft); + // Ok(()) + // } + + // async fn promote_pending_nft(&mut self, tx_hash: String, token_id: String) -> + // eyre::Result<()> { let pending_nft = self + // .pending_nfts + // .remove(&tx_hash) + // .ok_or_else(|| eyre::eyre!("Pending NFT not found"))?; + // let nft = NFT { teleport_id: pending_nft.teleport_id, token_id }; + // self.nfts.insert(pending_nft.nft_id, nft); + // Ok(()) + // } + + async fn add_nft(&mut self, nft_id: String, nft: NFT) -> eyre::Result<()> { + self.nfts.insert(nft_id, nft); Ok(()) } diff --git a/src/db/mod.rs b/src/db/mod.rs index 95a43b8..fb64360 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -21,11 +21,11 @@ pub struct NFT { pub token_id: String, } -#[derive(Debug, Serialize, Deserialize, Clone, FromRow, PartialEq, Eq)] -pub struct PendingNFT { - pub teleport_id: String, - pub nft_id: String, -} +// #[derive(Debug, Serialize, Deserialize, Clone, FromRow, PartialEq, Eq)] +// pub struct PendingNFT { +// pub teleport_id: String, +// pub nft_id: String, +// } impl User { pub fn signer(&self) -> eyre::Result { @@ -49,12 +49,13 @@ pub trait TeleportDB { async fn add_user(&mut self, teleport_id: String, user: User) -> eyre::Result<()>; async fn get_user_by_teleport_id(&self, teleport_id: String) -> eyre::Result; async fn get_user_by_x_id(&self, x_id: String) -> eyre::Result; - async fn add_pending_nft( - &mut self, - tx_hash: String, - pending_nft: PendingNFT, - ) -> eyre::Result<()>; - async fn promote_pending_nft(&mut self, tx_hash: String, token_id: String) -> eyre::Result<()>; + // async fn add_pending_nft( + // &mut self, + // tx_hash: String, + // pending_nft: PendingNFT, + // ) -> eyre::Result<()>; + // async fn promote_pending_nft(&mut self, tx_hash: String, token_id: String) -> eyre::Result<()>; + async fn add_nft(&mut self, nft_id: String, nft: NFT) -> eyre::Result<()>; async fn get_nft(&self, nft_id: String) -> eyre::Result; async fn add_tweet(&mut self, token_id: String, tweet_id: String) -> eyre::Result<()>; async fn get_tweet(&self, token_id: String) -> eyre::Result; diff --git a/src/endpoints.rs b/src/endpoints.rs index e36a3ce..3be4ab3 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -1,5 +1,7 @@ -use std::sync::Arc; -use tokio::fs; +use std::sync::{ + atomic::{AtomicU64, Ordering}, + Arc, +}; use axum::{ extract::{Query, State}, @@ -14,7 +16,7 @@ use crate::{ nft::{mint_nft, redeem_nft, send_eth}, wallet::{gen_sk, WalletProvider}, }, - db::{PendingNFT, TeleportDB, User}, + db::{TeleportDB, User, NFT}, twitter::{authorize_token, get_user_x_info, request_oauth_token}, }; @@ -69,6 +71,7 @@ pub struct SharedState { pub db: Arc>, pub rpc_url: String, pub provider: WalletProvider, + pub curr_nft_id: Arc, } pub async fn new_user( @@ -81,6 +84,7 @@ pub async fn new_user( let existing_user = db_lock.get_user_by_teleport_id(teleport_id.clone()).await.ok(); if let Some(user) = existing_user { if user.x_id.is_some() { + log::info!("User already created"); let x_info = get_user_x_info(user.access_token, user.access_secret).await; let encoded_x_info = serde_urlencoded::to_string(&x_info) .expect("Failed to encode x_info as query params"); @@ -170,10 +174,13 @@ pub async fn mint( .await .expect("Failed to mint NFT"); + let curr_nft_id = shared_state.curr_nft_id.clone(); + let new_value = curr_nft_id.fetch_add(1, Ordering::SeqCst) + 1; + let mut db = shared_state.db.lock().await; - db.add_pending_nft( - tx_hash.clone(), - PendingNFT { teleport_id: query.teleport_id.clone(), nft_id: query.nft_id.clone() }, + db.add_nft( + query.nft_id.clone(), + NFT { teleport_id: query.teleport_id.clone(), token_id: new_value.to_string() }, ) .await .expect("Failed to add pending NFT"); @@ -219,9 +226,10 @@ pub async fn get_tweet_id( pub async fn get_ratls_cert( State(shared_state): State>, ) -> Json { - let cert = fs::read_to_string(std::env::var("TLS_CERT_PATH").expect("TLS_CERT_PATH not set")) - .await - .expect("gramine ratls rootCA.crt not found"); + let cert = + tokio::fs::read_to_string(std::env::var("TLS_CERT_PATH").expect("TLS_CERT_PATH not set")) + .await + .expect("gramine ratls rootCA.crt not found"); Json(AttestationResponse { cert }) } diff --git a/src/main.rs b/src/main.rs index ca82ac6..ab314d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,9 @@ -use std::{net::SocketAddr, sync::Arc}; +use std::{ + net::SocketAddr, + sync::{atomic::AtomicU64, Arc}, +}; +use actions::nft::get_curr_token_id; use alloy::{ providers::ProviderBuilder, signers::local::{coins_bip39::English, MnemonicBuilder}, @@ -43,7 +47,10 @@ async fn main() { let db = db::in_memory::InMemoryDB::new(); let db = Arc::new(Mutex::new(db)); - let shared_state = SharedState { db: db.clone(), rpc_url, provider }; + + let curr_nft_id = get_curr_token_id(rpc_url.clone()).await.unwrap(); + let curr_nft_id = Arc::new(AtomicU64::new(curr_nft_id)); + let shared_state = SharedState { db: db.clone(), rpc_url, provider, curr_nft_id }; let eph = fs::read(tls_key_path).await.expect("gramine ratls rootCA.key not found");