Skip to content

Commit

Permalink
add name and symbol to the asset_data table (metaplex-foundation#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Acharya authored Jun 14, 2023
1 parent 76a424d commit 7ddabe8
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 4 deletions.
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(
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()),
}
} 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

0 comments on commit 7ddabe8

Please sign in to comment.