Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add name and symbol to the asset_data table #54

Merged
2 commits merged into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions digital_asset_types/src/dao/generated/asset_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct Model {
pub metadata: Json,
pub slot_updated: i64,
pub reindex: Option<bool>,
pub raw_name: Vec<u8>,
pub raw_symbol: Vec<u8>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
Expand All @@ -36,6 +38,8 @@ pub enum Column {
Metadata,
SlotUpdated,
Reindex,
RawName,
RawSymbol,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions digital_asset_types/src/dapi/common/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ pub fn safe_select<'a>(
.and_then(|v| v.pop())
}

#[cfg(feature = "fetch-raw-fields")]
async fn process_raw_fields(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will this function be used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, I am using it for testing purpose. Its disabled due the compile/build time flag. However, based on a new optional field in the api, we will extract the raw fields or the sanitised fields

name: Option<String>,
symbol: Option<String>,
asset_data: &AssetData,
) -> (Option<String>, Option<String>) {
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()),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what scenarios would this happen? Do you have an example NFT?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can fail if the utf8 check validation of the string fails. I don't have an example nft

}
} 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<String>,
Expand Down
6 changes: 4 additions & 2 deletions digital_asset_types/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ pub fn create_asset_data(
row_num: Vec<u8>,
) -> (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,
Expand Down Expand Up @@ -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(),
},
)
}
Expand Down
2 changes: 2 additions & 0 deletions digital_asset_types/tests/json_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub async fn parse_offchain_json(json: serde_json::Value, cdn_prefix: Option<Str
metadata: json,
slot_updated: 0,
reindex: None,
raw_name: String::from("Handalf").into_bytes().to_vec(),
raw_symbol: String::from("").into_bytes().to_vec(),
};

v1_content_from_json(&asset_data, cdn_prefix).unwrap()
Expand Down
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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),
]
}
}
49 changes: 49 additions & 0 deletions migration/src/m20230613_114817_add_name_symbol_to_asset_data.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
4 changes: 4 additions & 0 deletions nft_ingester/src/program_transformers/bubblegum/mint_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ pub async fn save_v1_asset<T: ConnectionTrait + TransactionTrait>(
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,
Expand All @@ -152,6 +154,8 @@ pub async fn save_v1_asset<T: ConnectionTrait + TransactionTrait>(
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)
Expand Down