Skip to content

Add CIP25 reparse basic #135

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
108 changes: 63 additions & 45 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions indexer/reparse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ entity = { path = "../entity" }
futures = "0.3.21"
cardano-multiplatform-lib = { git = "https://github.com/dcSpark/cardano-multiplatform-lib", branch = "metadata-and-addr" }
hex = "0.4.0"
cml-cip25 = { "path" = "../../../cardano-multiplatform-lib/cip25/rust" }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can't merge this until we have the cip25 crate properly published

76 changes: 76 additions & 0 deletions indexer/reparse/src/reparse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cml_cip25::serialization::FromBytes;
use entity::{
prelude::*,
sea_orm::{prelude::*, JoinType, QueryOrder, QuerySelect},
Expand All @@ -11,11 +12,86 @@ pub async fn start_reparse(conn: DatabaseConnection) -> anyhow::Result<()> {
reparse_addresses(&conn, 0).await?;
reparse_tx_out(&conn, 0).await?;
reparse_txs(&conn, 0).await?;
// note: cip25 errors are extremely common from projects accidentally not following it, so we ignore them
// reparse_nft(&conn, 0).await?;
Ok(())
}

static PAGE_SIZE: usize = 8192 * 4;

async fn reparse_nft(conn: &DatabaseConnection, start_index: u64) -> Result<(), DbErr> {
let cip25_count = Cip25Entry::find().count(conn).await?;
let mut cip25_stream = Cip25Entry::find()
.order_by_asc(Cip25EntryColumn::Id)
.filter(Cip25EntryColumn::Id.gt(start_index))
.paginate(conn, PAGE_SIZE)
.into_stream();

while let Some(cip25_entries) = &cip25_stream.try_next().await? {
println!(
"cip25 entries: {} / {} ({:.1}%)",
cip25_entries.first().unwrap().id,
cip25_count,
(100.0 * cip25_entries.first().unwrap().id as f64) / (cip25_count as f64)
);
for cip25_entry in cip25_entries {
// Option 1:
// let name = asset...get_str('name').as_text();
// let image = asset.get_str('image').as_text();
// match cml_cip25::MetadataDetails::from_bytes(cip25_entry.payload.clone()) {
// Err(_) => {}
// Ok(details) => {
// let name = details.name.to_str();
// let image = String::from(&details.image).as_str();
// }
// }

// Option 2:
// match cardano_multiplatform_lib::metadata::TransactionMetadatum::from_bytes(
// cip25_entry.payload.clone(),
// ) {
// Err(_) => {}
// Ok(metadatum) => {
// let assetMap = metadatum.as_map().unwrap();
// let name = assetMap.get_str("name").and_then(|val| val.as_text());
// let image_base = assetMap.get_str("image");
// match image_base.as_ref() {
// Err(_) => {}
// Ok(base) => match base.as_text() {
// Ok(_) => {}
// Err(_) => {
// let mut result: String = "".to_string();
// if let Ok(list) = base.as_list().as_ref() {
// for i in 0..list.len() {
// result += &list.get(i).as_text().unwrap();
// }
// }
// }
// },
// }
// }
// };

if let Err(e) = &cml_cip25::MetadataDetails::from_bytes(cip25_entry.payload.clone()) {
let asset = NativeAsset::find()
.filter(NativeAssetColumn::Id.eq(cip25_entry.asset_id))
.one(conn)
.await?
.unwrap();
println!(
"\nFailed cip25 entry {}.{} {:?} {}\n",
hex::encode(&asset.policy_id),
hex::encode(&asset.asset_name),
e,
hex::encode(&cip25_entry.payload)
);
};
}
}
println!("Done parsing transactions");
Ok(())
}

async fn reparse_txs(conn: &DatabaseConnection, start_index: u64) -> Result<(), DbErr> {
let tx_count = Transaction::find().count(conn).await?;
let mut tx_stream = Transaction::find()
Expand Down