From 419183c80c4065add7f6a439285517d338cb121e Mon Sep 17 00:00:00 2001 From: Nikhil Acharya Date: Tue, 13 Jun 2023 15:40:47 +0530 Subject: [PATCH 1/2] add name and symbol to the asset_data table --- .../src/dao/generated/asset_data.rs | 6 +++ digital_asset_types/src/dapi/common/asset.rs | 29 +++++++++++ migration/src/lib.rs | 2 + ...13_114817_add_name_symbol_to_asset_data.rs | 49 +++++++++++++++++++ .../program_transformers/bubblegum/mint_v1.rs | 4 ++ .../token_metadata/v1_asset.rs | 8 ++- 6 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 migration/src/m20230613_114817_add_name_symbol_to_asset_data.rs diff --git a/digital_asset_types/src/dao/generated/asset_data.rs b/digital_asset_types/src/dao/generated/asset_data.rs index 814eeeb3a..374ed854a 100644 --- a/digital_asset_types/src/dao/generated/asset_data.rs +++ b/digital_asset_types/src/dao/generated/asset_data.rs @@ -24,6 +24,8 @@ pub struct Model { pub metadata: Json, pub slot_updated: i64, pub reindex: Option, + pub raw_name: Vec, + pub raw_symbol: Vec, } #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] @@ -36,6 +38,8 @@ pub enum Column { Metadata, SlotUpdated, Reindex, + RawName, + RawSymbol, } #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] @@ -67,6 +71,8 @@ impl ColumnTrait for Column { Self::Metadata => ColumnType::JsonBinary.def(), Self::SlotUpdated => ColumnType::BigInteger.def(), Self::Reindex => ColumnType::Boolean.def(), + Self::RawName => ColumnType::Binary.def(), + Self::RawSymbol => ColumnType::Binary.def(), } } } diff --git a/digital_asset_types/src/dapi/common/asset.rs b/digital_asset_types/src/dapi/common/asset.rs index cb6fc5aa0..881a0947f 100644 --- a/digital_asset_types/src/dapi/common/asset.rs +++ b/digital_asset_types/src/dapi/common/asset.rs @@ -151,6 +151,35 @@ pub fn safe_select<'a>( .and_then(|v| v.pop()) } +#[cfg(feature = "fetch-raw-fields")] +async fn process_raw_fields( + name: Option, + symbol: Option, + asset_data: &AssetData, +) -> (Option, Option) { + let name_result = if let Some(name) = &name { + let raw_name = asset_data.raw_name.clone(); + match String::from_utf8(raw_name) { + Ok(s) => Some(s), + Err(_) => Some(name.clone()), + } + } else { + None + }; + + let symbol_result = if let Some(symbol) = &symbol { + let raw_symbol = asset_data.raw_symbol.clone(); + match String::from_utf8(raw_symbol) { + Ok(s) => Some(s), + Err(_) => Some(symbol.clone()), + } + } else { + None + }; + + (name_result, symbol_result) +} + pub fn v1_content_from_json( asset_data: &asset_data::Model, cdn_prefix: Option, diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 8dba28b99..0da7cb8b4 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -20,6 +20,7 @@ mod m20230510_183736_add_indices_to_assets; mod m20230516_185005_add_reindex_to_assets; mod m20230525_115717_cl_audit_table; mod m20230528_124011_cl_audit_table_index; +mod m20230613_114817_add_name_symbol_to_asset_data; pub struct Migrator; @@ -47,6 +48,7 @@ impl MigratorTrait for Migrator { Box::new(m20230516_185005_add_reindex_to_assets::Migration), Box::new(m20230525_115717_cl_audit_table::Migration), Box::new(m20230528_124011_cl_audit_table_index::Migration), + Box::new(m20230613_114817_add_name_symbol_to_asset_data::Migration), ] } } diff --git a/migration/src/m20230613_114817_add_name_symbol_to_asset_data.rs b/migration/src/m20230613_114817_add_name_symbol_to_asset_data.rs new file mode 100644 index 000000000..0ab95831f --- /dev/null +++ b/migration/src/m20230613_114817_add_name_symbol_to_asset_data.rs @@ -0,0 +1,49 @@ +use digital_asset_types::dao::asset_data; +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + sea_query::Table::alter() + .table(asset_data::Entity) + .add_column(ColumnDef::new(Alias::new("raw_name")).binary()) + .to_owned(), + ) + .await?; + manager + .alter_table( + sea_query::Table::alter() + .table(asset_data::Entity) + .add_column(ColumnDef::new(Alias::new("raw_symbol")).binary()) + .to_owned(), + ) + .await?; + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + sea_query::Table::alter() + .table(asset_data::Entity) + .drop_column(Alias::new("raw_name")) + .to_owned(), + ) + .await?; + + manager + .alter_table( + sea_query::Table::alter() + .table(asset_data::Entity) + .drop_column(Alias::new("raw_symbol")) + .to_owned(), + ) + .await?; + Ok(()) + } +} diff --git a/nft_ingester/src/program_transformers/bubblegum/mint_v1.rs b/nft_ingester/src/program_transformers/bubblegum/mint_v1.rs index 5f6fddbff..e1c2d6c56 100644 --- a/nft_ingester/src/program_transformers/bubblegum/mint_v1.rs +++ b/nft_ingester/src/program_transformers/bubblegum/mint_v1.rs @@ -61,6 +61,8 @@ where let id_bytes = id.to_bytes(); let slot_i = bundle.slot as i64; let uri = metadata.uri.trim().replace('\0', ""); + let name = metadata.name.clone().into_bytes(); + let symbol = metadata.symbol.clone().into_bytes(); let mut chain_data = ChainDataV1 { name: metadata.name.clone(), symbol: metadata.symbol.clone(), @@ -94,6 +96,8 @@ where metadata_mutability: Set(Mutability::Mutable), slot_updated: Set(slot_i), reindex: Set(Some(true)), + raw_name: Set(name.to_vec()), + raw_symbol: Set(symbol.to_vec()), ..Default::default() }; diff --git a/nft_ingester/src/program_transformers/token_metadata/v1_asset.rs b/nft_ingester/src/program_transformers/token_metadata/v1_asset.rs index 828ee75da..5e10850a9 100644 --- a/nft_ingester/src/program_transformers/token_metadata/v1_asset.rs +++ b/nft_ingester/src/program_transformers/token_metadata/v1_asset.rs @@ -124,9 +124,11 @@ pub async fn save_v1_asset( None => (NotSet, NotSet), }; + let name = data.name.clone().into_bytes(); + let symbol = data.symbol.clone().into_bytes(); let mut chain_data = ChainDataV1 { - name: data.name, - symbol: data.symbol, + name: data.name.clone(), + symbol: data.symbol.clone(), edition_nonce: metadata.edition_nonce, primary_sale_happened: metadata.primary_sale_happened, token_standard: metadata.token_standard, @@ -152,6 +154,8 @@ pub async fn save_v1_asset( slot_updated: Set(slot_i), reindex: Set(Some(true)), id: Set(id.to_vec()), + raw_name: Set(name.to_vec()), + raw_symbol: Set(symbol.to_vec()), }; let txn = conn.begin().await?; let mut query = asset_data::Entity::insert(asset_data_model) From 959d173efc5d912fa5dbf09425123d3118e88723 Mon Sep 17 00:00:00 2001 From: Nikhil Acharya Date: Wed, 14 Jun 2023 09:58:10 +0530 Subject: [PATCH 2/2] fix tests --- digital_asset_types/tests/common.rs | 6 ++++-- digital_asset_types/tests/json_parsing.rs | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/digital_asset_types/tests/common.rs b/digital_asset_types/tests/common.rs index 85fe3923c..28e8e1606 100644 --- a/digital_asset_types/tests/common.rs +++ b/digital_asset_types/tests/common.rs @@ -42,8 +42,8 @@ pub fn create_asset_data( row_num: Vec, ) -> (asset_data::ActiveModel, asset_data::Model) { let chain_data = ChainDataV1 { - name: metadata.name, - symbol: metadata.symbol, + name: metadata.name.clone(), + symbol: metadata.symbol.clone(), edition_nonce: metadata.edition_nonce, primary_sale_happened: metadata.primary_sale_happened, token_standard: metadata.token_standard, @@ -83,6 +83,8 @@ pub fn create_asset_data( metadata: JsonValue::String("processing".to_string()), slot_updated: 0, reindex: None, + raw_name: metadata.name.into_bytes().to_vec().clone(), + raw_symbol: metadata.symbol.into_bytes().to_vec().clone(), }, ) } diff --git a/digital_asset_types/tests/json_parsing.rs b/digital_asset_types/tests/json_parsing.rs index c64363d34..15bf3b511 100644 --- a/digital_asset_types/tests/json_parsing.rs +++ b/digital_asset_types/tests/json_parsing.rs @@ -35,6 +35,8 @@ pub async fn parse_offchain_json(json: serde_json::Value, cdn_prefix: Option