Skip to content

Commit

Permalink
fix: token extensions integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Nov 11, 2024
1 parent db11fd6 commit c22c4c2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 31 deletions.
10 changes: 0 additions & 10 deletions digital_asset_types/src/dapi/common/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,6 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result<RpcAsset, DbE
_ => None,
};

if interface == Interface::FungibleToken {
return Ok(RpcAsset {
interface: interface.clone(),
id: bs58::encode(asset.id).into_string(),
content: Some(content),
mint_extensions: asset.mint_extensions,
..Default::default()
});
}

Ok(RpcAsset {
interface: interface.clone(),
id: bs58::encode(asset.id).into_string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: integration_tests/tests/integration_tests/token_extensions_tests.rs
expression: response
snapshot_kind: text
---
{
"interface": "V1_NFT",
"interface": "FungibleToken",
"id": "BPU5vrAHafRuVeK33CgfdwTKSsmC4p6t3aqyav3cFF7Y",
"content": {
"$schema": "https://schema.metaplex.com/nft1.0.json",
Expand All @@ -15,14 +16,7 @@ expression: response
},
"links": {}
},
"authorities": [
{
"address": "Em34oqDQYQZ9b6ycPHD28K47mttrRsdNu1S1pgK6NtPL",
"scopes": [
"full"
]
}
],
"authorities": [],
"compression": {
"eligible": false,
"compressed": false,
Expand All @@ -48,12 +42,7 @@ expression: response
"delegated": false,
"delegate": null,
"ownership_model": "single",
"owner": "Em34oqDQYQZ9b6ycPHD28K47mttrRsdNu1S1pgK6NtPL"
},
"supply": {
"print_max_supply": 0,
"print_current_supply": 0,
"edition_nonce": null
"owner": ""
},
"mutable": true,
"burnt": false,
Expand Down
1 change: 1 addition & 0 deletions program_transformers/src/asset_upserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub async fn upsert_assets_mint_account_columns<T: ConnectionTrait + Transaction
asset::Column::SlotUpdatedMintAccount,
asset::Column::MintExtensions,
asset::Column::SlotUpdated,
asset::Column::AssetData,
])
.to_owned(),
)
Expand Down
70 changes: 64 additions & 6 deletions program_transformers/src/token_extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ use {
TokenExtensionsProgramAccount,
},
digital_asset_types::dao::{
asset_data,
sea_orm_active_enums::ChainMutability,
asset, asset_data,
sea_orm_active_enums::{ChainMutability, SpecificationAssetClass, SpecificationVersions},
token_accounts,
tokens::{self, IsNonFungible as IsNonFungibleModel},
},
sea_orm::{
entity::ActiveValue, query::QueryTrait, sea_query::query::OnConflict, ConnectionTrait,
DatabaseConnection, DbBackend, EntityTrait, TransactionTrait,
DatabaseConnection, DatabaseTransaction, DbBackend, DbErr, EntityTrait, Set,
TransactionTrait,
},
serde_json::Value,
solana_sdk::program_option::COption,
Expand Down Expand Up @@ -121,15 +122,17 @@ pub async fn handle_token_extensions_program_account<'a, 'b, 'c>(
Ok(())
}
TokenExtensionsProgramAccount::MintAccount(m) => {
println!("Mint account extensions: {:#?}", m.extensions);
let MintAccount {
account,
extensions,
} = m;

let is_non_fungible = m.account.is_non_fungible();

let extensions: Option<Value> = if extensions.is_some() {
if let Some(metadata) = &m.extensions.metadata {
upsert_asset_data(metadata, account_key.clone(), slot, db).await?;
upsert_asset_data(metadata, account_key.clone(), slot, db, is_non_fungible)
.await?;
}

filter_non_null_fields(
Expand Down Expand Up @@ -193,7 +196,7 @@ pub async fn handle_token_extensions_program_account<'a, 'b, 'c>(
slot_updated_mint_account: slot,
extensions: extensions.clone(),
},
m.is_non_fungible(),
is_non_fungible,
&txn,
)
.await?;
Expand All @@ -211,6 +214,7 @@ async fn upsert_asset_data(
key_bytes: Vec<u8>,
slot: i64,
db: &DatabaseConnection,
is_non_fungible: bool,
) -> ProgramTransformerResult<()> {
let metadata_json = serde_json::to_value(metadata.clone())
.map_err(|e| ProgramTransformerError::SerializatonError(e.to_string()))?;
Expand Down Expand Up @@ -244,5 +248,59 @@ async fn upsert_asset_data(
asset_data_query.sql
);
db.execute(asset_data_query).await?;

if !is_non_fungible {
let txn = db.begin().await?;
upsert_assets_metadata_cols(
AssetMetadataAccountCols {
mint: key_bytes.clone(),
slot_updated_metadata_account: slot,
},
&txn,
)
.await?;

txn.commit().await?;
}

Ok(())
}

struct AssetMetadataAccountCols {
mint: Vec<u8>,
slot_updated_metadata_account: i64,
}

async fn upsert_assets_metadata_cols(
metadata: AssetMetadataAccountCols,
db: &DatabaseTransaction,
) -> Result<(), DbErr> {
let asset = asset::ActiveModel {
id: ActiveValue::Set(metadata.mint.clone()),
specification_asset_class: Set(Some(SpecificationAssetClass::FungibleToken)),
specification_version: Set(Some(SpecificationVersions::V0)),
slot_updated_metadata_account: Set(Some(metadata.slot_updated_metadata_account)),
..Default::default()
};

let mut asset_query = asset::Entity::insert(asset)
.on_conflict(
OnConflict::columns([asset::Column::Id])
.update_columns([
asset::Column::SpecificationAssetClass,
asset::Column::SpecificationVersion,
asset::Column::SlotUpdatedMetadataAccount,
])
.to_owned(),
)
.build(DbBackend::Postgres);

asset_query.sql = format!(
"{} WHERE excluded.slot_updated_metadata_account >= asset.slot_updated_metadata_account OR asset.slot_updated_metadata_account IS NULL",
asset_query.sql
);

db.execute(asset_query).await?;

Ok(())
}

0 comments on commit c22c4c2

Please sign in to comment.