Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Nov 21, 2024
1 parent fa44e23 commit 650be7e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion digital_asset_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ thiserror = { workspace = true }
tokio = { workspace = true, features = ["macros"] }
url = { workspace = true }
mpl-token-metadata = { workspace = true }
hex = { workspace = true }

[features]
default = ["json_types", "sql_types"]
Expand Down
13 changes: 5 additions & 8 deletions digital_asset_types/src/dao/scopes/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
use indexmap::IndexMap;
use mpl_token_metadata::accounts::{Edition, MasterEdition};
use sea_orm::{entity::*, query::*, sea_query::Expr, ConnectionTrait, DbErr, Order};
use serde::de::DeserializeOwned;
use serde_json::Value;
use solana_sdk::pubkey::Pubkey;
use std::collections::HashMap;
Expand Down Expand Up @@ -561,11 +562,7 @@ fn filter_out_stale_creators(creators: &mut Vec<asset_creators::Model>) {
}
}

pub fn get_edition_data_from_json(data: Value) -> Result<Edition, DbErr> {
serde_json::from_value(data).map_err(|e| DbErr::Custom(e.to_string()))
}

pub fn get_master_edition_data_from_json(data: Value) -> Result<MasterEdition, DbErr> {
pub fn get_edition_data_from_json<T: DeserializeOwned>(data: Value) -> Result<T, DbErr> {
serde_json::from_value(data).map_err(|e| DbErr::Custom(e.to_string()))
}

Expand All @@ -587,13 +584,13 @@ pub async fn get_nft_editions(

let limit = limit.unwrap_or(10);

let master_edition_data = master_edition
let master_edition_data: MasterEdition = master_edition
.data
.clone()
.ok_or(DbErr::RecordNotFound(
"Master Edition data not found".to_string(),
))
.map(get_master_edition_data_from_json)??;
.map(get_edition_data_from_json)??;

let nft_editions = asset_v1_account_attachments::Entity::find()
.filter(
Expand All @@ -612,7 +609,7 @@ pub async fn get_nft_editions(
let nft_editions = nft_editions
.iter()
.map(|e| -> Result<NftEdition, DbErr> {
let data = e
let data: Edition = e
.data
.clone()
.ok_or(DbErr::RecordNotFound("Edition data not found".to_string()))
Expand Down
17 changes: 14 additions & 3 deletions program_transformers/src/token_metadata/master_edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub async fn save_master_edition(
..Default::default()
};

let query = asset_v1_account_attachments::Entity::insert(model)
let mut query = asset_v1_account_attachments::Entity::insert(model)
.on_conflict(
OnConflict::columns([asset_v1_account_attachments::Column::Id])
.update_columns([
Expand All @@ -86,6 +86,12 @@ pub async fn save_master_edition(
.to_owned(),
)
.build(DbBackend::Postgres);

query.sql = format!(
"{} WHERE excluded.slot_updated >= asset_v1_account_attachments.slot_updated",
query.sql
);

txn.execute(query).await?;
Ok(())
}
Expand All @@ -109,7 +115,7 @@ pub async fn save_edition(
..Default::default()
};

let query = asset_v1_account_attachments::Entity::insert(model)
let mut query = asset_v1_account_attachments::Entity::insert(model)
.on_conflict(
OnConflict::columns([asset_v1_account_attachments::Column::Id])
.update_columns([
Expand All @@ -120,7 +126,12 @@ pub async fn save_edition(
.to_owned(),
)
.build(DbBackend::Postgres);
txn.execute(query).await?;

query.sql = format!(
"{} WHERE excluded.slot_updated >= asset_v1_account_attachments.slot_updated",
query.sql
);

txn.execute(query).await?;
Ok(())
}
21 changes: 20 additions & 1 deletion program_transformers/src/token_metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use {
},
AccountInfo, DownloadMetadataNotifier,
},
blockbuster::programs::token_metadata::{TokenMetadataAccountData, TokenMetadataAccountState},
blockbuster::{
programs::token_metadata::{TokenMetadataAccountData, TokenMetadataAccountState},
token_metadata::types::TokenStandard,
},
master_edition::save_edition,
sea_orm::{DatabaseConnection, TransactionTrait},
};
Expand Down Expand Up @@ -59,3 +62,19 @@ pub async fn handle_token_metadata_account<'a, 'b>(
_ => Err(ProgramTransformerError::NotImplemented),
}
}

pub trait IsNonFungibeFromTokenStandard {
fn is_non_fungible(&self) -> bool;
}

impl IsNonFungibeFromTokenStandard for Option<TokenStandard> {
fn is_non_fungible(&self) -> bool {
matches!(
self,
Some(TokenStandard::NonFungible)
| Some(TokenStandard::NonFungibleEdition)
| Some(TokenStandard::ProgrammableNonFungible)
| Some(TokenStandard::ProgrammableNonFungibleEdition)
)
}
}
18 changes: 3 additions & 15 deletions program_transformers/src/token_metadata/v1_asset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use {
super::IsNonFungibeFromTokenStandard,
crate::{
asset_upserts::{
upsert_assets_metadata_account_columns, upsert_assets_mint_account_columns,
Expand Down Expand Up @@ -144,16 +145,6 @@ async fn index_token_account_data<T: ConnectionTrait + TransactionTrait>(
Ok(())
}

const fn check_is_non_fungible_from_token_standard(token_standard: Option<TokenStandard>) -> bool {
matches!(
token_standard,
Some(TokenStandard::NonFungible)
| Some(TokenStandard::NonFungibleEdition)
| Some(TokenStandard::ProgrammableNonFungible)
| Some(TokenStandard::ProgrammableNonFungibleEdition)
)
}

pub async fn save_v1_asset<T: ConnectionTrait + TransactionTrait>(
conn: &T,
metadata: &Metadata,
Expand Down Expand Up @@ -413,7 +404,7 @@ pub async fn save_v1_asset<T: ConnectionTrait + TransactionTrait>(
txn.commit().await?;

// If the asset is a non-fungible token, then we need to insert to the asset_v1_account_attachments table
if check_is_non_fungible_from_token_standard(metadata.token_standard) {
if metadata.token_standard.is_non_fungible() {
upsert_asset_v1_account_attachments(conn, &mint_pubkey, slot).await?;
}

Expand Down Expand Up @@ -446,10 +437,7 @@ async fn upsert_asset_v1_account_attachments<T: ConnectionTrait + TransactionTra
let query = asset_v1_account_attachments::Entity::insert(attachment)
.on_conflict(
OnConflict::columns([asset_v1_account_attachments::Column::Id])
.update_columns([
asset_v1_account_attachments::Column::SlotUpdated,
asset_v1_account_attachments::Column::AssetId,
])
.update_columns([asset_v1_account_attachments::Column::AssetId])
.to_owned(),
)
.build(DbBackend::Postgres);
Expand Down

0 comments on commit 650be7e

Please sign in to comment.