Skip to content

Instantly share code, notes, and snippets.

@SlowestTimelord
Created November 20, 2024 23:00
Show Gist options
  • Select an option

  • Save SlowestTimelord/a2e0751883345f182897515471f385c6 to your computer and use it in GitHub Desktop.

Select an option

Save SlowestTimelord/a2e0751883345f182897515471f385c6 to your computer and use it in GitHub Desktop.
get_nfts refactor
pub async fn get_nfts(&self, req: GetNfts) -> Result<GetNftsResponse> {
let wallet = self.wallet()?;
let nfts = match req.collection_id.as_deref() {
Some("all") => self.get_all_nfts(&wallet, &req).await?,
_ => {
let collection_id = req.collection_id.map(parse_collection_id).transpose()?;
self.get_filtered_nfts(&wallet, &req, collection_id).await?
}
};
let records = self.build_nft_records(&wallet, nfts).await?;
Ok(GetNftsResponse { nfts: records })
}
async fn get_all_nfts(&self, wallet: &Wallet, req: &GetNfts) -> Result<Vec<Nft>> {
match (req.sort_mode, req.include_hidden) {
(NftSortMode::Name, true) => wallet.db.nfts_named(req.limit, req.offset).await,
(NftSortMode::Name, false) => wallet.db.nfts_visible_named(req.limit, req.offset).await,
(NftSortMode::Recent, true) => wallet.db.nfts_recent(req.limit, req.offset).await,
(NftSortMode::Recent, false) => wallet.db.nfts_visible_recent(req.limit, req.offset).await,
}
}
async fn get_filtered_nfts(
&self,
wallet: &Wallet,
req: &GetNfts,
collection_id: Option<CollectionId>,
) -> Result<Vec<Nft>> {
match (req.sort_mode, req.include_hidden, collection_id) {
(sort_mode, include_hidden, Some(collection_id)) => {
self.get_collection_nfts(wallet, sort_mode, include_hidden, collection_id, req.limit, req.offset).await
}
(sort_mode, include_hidden, None) => {
self.get_no_collection_nfts(wallet, sort_mode, include_hidden, req.limit, req.offset).await
}
}
}
async fn get_collection_nfts(
&self,
wallet: &Wallet,
sort_mode: NftSortMode,
include_hidden: bool,
collection_id: CollectionId,
limit: u32,
offset: u32,
) -> Result<Vec<Nft>> {
match (sort_mode, include_hidden) {
(NftSortMode::Name, true) => wallet.db.collection_nfts_named(collection_id, limit, offset).await,
(NftSortMode::Name, false) => wallet.db.collection_nfts_visible_named(collection_id, limit, offset).await,
(NftSortMode::Recent, true) => wallet.db.collection_nfts_recent(collection_id, limit, offset).await,
(NftSortMode::Recent, false) => wallet.db.collection_nfts_visible_recent(collection_id, limit, offset).await,
}
}
async fn get_no_collection_nfts(
&self,
wallet: &Wallet,
sort_mode: NftSortMode,
include_hidden: bool,
limit: u32,
offset: u32,
) -> Result<Vec<Nft>> {
match (sort_mode, include_hidden) {
(NftSortMode::Name, true) => wallet.db.no_collection_nfts_named(limit, offset).await,
(NftSortMode::Name, false) => wallet.db.no_collection_nfts_visible_named(limit, offset).await,
(NftSortMode::Recent, true) => wallet.db.no_collection_nfts_recent(limit, offset).await,
(NftSortMode::Recent, false) => wallet.db.no_collection_nfts_visible_recent(limit, offset).await,
}
}
async fn build_nft_records(&self, wallet: &Wallet, nfts: Vec<Nft>) -> Result<Vec<NftRecord>> {
let mut records = Vec::new();
for nft in nfts {
let data = self.get_nft_data(wallet, &nft.launcher_id).await?;
let collection_name = self.get_collection_name(wallet, nft.collection_id).await?;
records.push(nft_record(nft, collection_name, data)?);
}
Ok(records)
}
async fn get_nft_data(&self, wallet: &Wallet, launcher_id: &LauncherId) -> Result<Option<NftData>> {
if let Some(hash) = wallet.db.data_hash(launcher_id).await? {
wallet.db.fetch_nft_data(hash).await
} else {
Ok(None)
}
}
async fn get_collection_name(&self, wallet: &Wallet, collection_id: Option<CollectionId>) -> Result<Option<String>> {
match collection_id {
Some(id) => wallet.db.collection_name(id).await,
None => Ok(None),
}
}
@SlowestTimelord
Copy link
Author

I totally wrote this myself and not Claude 3.5 Sonnet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment