Skip to content

Commit

Permalink
feat: compressed collection minting
Browse files Browse the repository at this point in the history
  • Loading branch information
kespinola committed Jul 25, 2023
1 parent d8f51c0 commit a268ab0
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 30 deletions.
6 changes: 4 additions & 2 deletions consumer/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ pub struct MintMetaplexAddresses {

#[derive(Clone)]
pub struct MintCompressedMintV1Addresses {
pub owner: Pubkey,
pub recipient: Pubkey,
pub merkle_tree: Pubkey,
pub tree_authority: Pubkey,
pub tree_delegate: Pubkey,
pub leaf_owner: Pubkey,
}

#[derive(Clone)]
Expand Down
50 changes: 36 additions & 14 deletions consumer/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anchor_lang::Event;
use holaplex_hub_nfts_solana_core::{
db,
proto::{
Expand All @@ -12,9 +11,9 @@ use holaplex_hub_nfts_solana_core::{
SolanaTransactionFailureReason, TransferMetaplexAssetTransaction,
},
sea_orm::{DbErr, Set},
Collection, CollectionMint, Services,
Collection, CollectionMint, CompressionLeaf, Services,
};
use holaplex_hub_nfts_solana_entity::{collection_mints, collections};
use holaplex_hub_nfts_solana_entity::{collection_mints, collections, compression_leafs};
use hub_core::{
chrono::Utc,
prelude::*,
Expand All @@ -30,7 +29,7 @@ use crate::{
CollectionBackend, MasterEditionAddresses, MintBackend, MintEditionAddresses,
MintMetaplexAddresses, TransferBackend,
},
solana::{EditionRef, Solana, UncompressedRef},
solana::{CompressedRef, EditionRef, Solana, UncompressedRef},
};

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -330,7 +329,7 @@ impl Processor {
self.process_nft(
EventKind::MintToCollection,
&key,
self.mint_to_collection(&UncompressedRef(self.solana()), &key, payload),
self.mint_to_collection(&key, payload),
)
.await
},
Expand Down Expand Up @@ -595,11 +594,8 @@ impl Processor {
Ok(tx.into())
}

async fn mint_to_collection<
B: MintBackend<MintMetaplexMetadataTransaction, MintMetaplexAddresses>,
>(
async fn mint_to_collection(
&self,
backend: &B,
key: &SolanaNftEventKey,
payload: MintMetaplexMetadataTransaction,
) -> ProcessResult<SolanaPendingTransaction> {
Expand All @@ -609,17 +605,43 @@ impl Processor {
.await?
.ok_or(ProcessorErrorKind::RecordNotFound)?;

if payload.compressed {
let backend = &CompressedRef(self.solana());

let tx = backend
.mint(&collection, payload)
.map_err(ProcessorErrorKind::Solana)?;

let compression_leaf = compression_leafs::Model {
id,
collection_id: collection.id,
merkle_tree: tx.addresses.merkle_tree.to_string(),
tree_authority: tx.addresses.tree_authority.to_string(),
tree_delegate: tx.addresses.tree_delegate.to_string(),
leaf_owner: tx.addresses.leaf_owner.to_string(),
created_at: Utc::now().naive_utc(),
..Default::default()
};

CompressionLeaf::create(&self.db, compression_leaf).await?;

return Ok(tx.into());
}

let backend = &UncompressedRef(self.solana());

let tx = backend
.mint(&collection, payload)
.map_err(ProcessorErrorKind::Solana)?;

let collection_mint = collection_mints::Model {
id,
collection_id: collection.id,
mint: tx.addresses.mint.to_string(),
owner: tx.addresses.recipient.to_string(),
associated_token_account: Some(tx.addresses.associated_token_account.to_string()),
mint: tx.addresses.mint.to_string(),
created_at: Utc::now().naive_utc(),
associated_token_account: tx.addresses.associated_token_account.to_string(),
..Default::default()
};

CollectionMint::create(&self.db, collection_mint).await?;
Expand Down Expand Up @@ -648,7 +670,7 @@ impl Processor {
collection_id: collection.id,
mint: tx.addresses.mint.to_string(),
owner: tx.addresses.recipient.to_string(),
associated_token_account: Some(tx.addresses.associated_token_account.to_string()),
associated_token_account: tx.addresses.associated_token_account.to_string(),
created_at: Utc::now().naive_utc(),
};

Expand Down Expand Up @@ -764,7 +786,7 @@ impl Processor {

collection_mint.mint = Set(mint.to_string());
collection_mint.owner = Set(recipient.to_string());
collection_mint.associated_token_account = Set(Some(associated_token_account.to_string()));
collection_mint.associated_token_account = Set(associated_token_account.to_string());

CollectionMint::update(&self.db, collection_mint).await?;

Expand Down Expand Up @@ -803,7 +825,7 @@ impl Processor {

collection_mint.mint = Set(mint.to_string());
collection_mint.owner = Set(recipient.to_string());
collection_mint.associated_token_account = Set(Some(associated_token_account.to_string()));
collection_mint.associated_token_account = Set(associated_token_account.to_string());

CollectionMint::update(&self.db, collection_mint).await?;

Expand Down
23 changes: 14 additions & 9 deletions consumer/src/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ pub struct SolanaArgs {
#[arg(long, env)]
pub digital_asset_api_endpoint: String,

// TODO: remove these
#[arg(long, env)]
pub tree_authority: Pubkey,
#[arg(long, env)]
Expand Down Expand Up @@ -280,7 +279,7 @@ impl<'a> CollectionBackend for UncompressedRef<'a> {
true,
None,
None,
None,
Some(mpl_token_metadata::state::CollectionDetails::V1 { size: 0 }),
);
let create_master_edition_ins = mpl_token_metadata::instruction::create_master_edition_v3(
mpl_token_metadata::ID,
Expand Down Expand Up @@ -668,7 +667,8 @@ impl<'a> MintBackend<MintMetaplexMetadataTransaction, MintCompressedMintV1Addres
let payer = self.0.treasury_wallet_address;
let recipient = recipient_address.parse()?;
let owner = owner_address.parse()?;
let treasury_wallet_address = self.0.treasury_wallet_address;
let merkle_tree = self.0.bubblegum_merkle_tree;
let tree_authority = self.0.bubblegum_tree_authority;

let mut accounts = vec![
// Tree authority
Expand All @@ -679,11 +679,11 @@ impl<'a> MintBackend<MintMetaplexMetadataTransaction, MintCompressedMintV1Addres
// Leaf delegate
AccountMeta::new_readonly(recipient, false),
// Merkle tree
AccountMeta::new(self.0.bubblegum_merkle_tree, false),
AccountMeta::new(merkle_tree, false),
// Payer [signer]
AccountMeta::new_readonly(payer, true),
// Tree delegate [signer]
AccountMeta::new_readonly(treasury_wallet_address, true),
AccountMeta::new_readonly(payer, true),
// Collection authority [signer]
AccountMeta::new_readonly(owner, true),
// Collection authority pda
Expand Down Expand Up @@ -724,7 +724,7 @@ impl<'a> MintBackend<MintMetaplexMetadataTransaction, MintCompressedMintV1Addres
edition_nonce: None,
token_standard: None,
collection: Some(Collection {
verified: true,
verified: false,
key: collection.mint.parse()?,
}),
uses: None,
Expand All @@ -748,7 +748,12 @@ impl<'a> MintBackend<MintMetaplexMetadataTransaction, MintCompressedMintV1Addres
Ok(TransactionResponse {
serialized_message,
signatures_or_signers_public_keys: vec![payer.to_string(), owner.to_string()],
addresses: MintCompressedMintV1Addresses { owner, recipient },
addresses: MintCompressedMintV1Addresses {
leaf_owner: recipient,
tree_delegate: payer,
tree_authority,
merkle_tree,
},
})
}
}
Expand Down Expand Up @@ -853,7 +858,7 @@ impl<'a> MintBackend<MintMetaplexMetadataTransaction, MintMetaplexAddresses>
None,
);

let verify_collection = mpl_token_metadata::instruction::verify_collection(
let verify_collection = mpl_token_metadata::instruction::verify_sized_collection_item(
mpl_token_metadata::ID,
metadata,
owner,
Expand Down Expand Up @@ -886,8 +891,8 @@ impl<'a> MintBackend<MintMetaplexMetadataTransaction, MintMetaplexAddresses>
serialized_message,
signatures_or_signers_public_keys: vec![
payer.to_string(),
owner.to_string(),
mint_signature.to_string(),
owner.to_string(),
],
addresses: MintMetaplexAddresses {
update_authority: owner,
Expand Down
2 changes: 1 addition & 1 deletion core/src/collection_mints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl CollectionMint {

let mut active_model: ActiveModel = model.clone().into();
active_model.owner = Set(owner);
active_model.associated_token_account = Set(Some(ata));
active_model.associated_token_account = Set(ata);
active_model.update(conn).await
}

Expand Down
28 changes: 28 additions & 0 deletions core/src/compression_leafs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use holaplex_hub_nfts_solana_entity::compression_leafs::{ActiveModel, Column, Entity, Model};
use sea_orm::prelude::*;

use crate::db::Connection;

pub struct CompressionLeaf;

impl CompressionLeaf {
pub async fn create(db: &Connection, model: Model) -> Result<Model, DbErr> {
let conn = db.get();

let active_model: ActiveModel = model.into();

active_model.insert(conn).await
}

pub async fn find_by_id(db: &Connection, id: Uuid) -> Result<Option<Model>, DbErr> {
let conn = db.get();

Entity::find().filter(Column::Id.eq(id)).one(conn).await
}

pub async fn update(db: &Connection, model: ActiveModel) -> Result<Model, DbErr> {
let conn = db.get();

model.update(conn).await
}
}
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

mod collection_mints;
mod collections;
mod compression_leafs;
pub mod db;

pub use collection_mints::CollectionMint;
pub use collections::Collection;
pub use compression_leafs::CompressionLeaf;
use hub_core::{consumer::RecvError, prelude::*};
use proto::{NftEventKey, SolanaNftEventKey, TreasuryEventKey};
pub use sea_orm;
Expand Down
4 changes: 2 additions & 2 deletions entity/src/collection_mints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub struct Model {
pub mint: String,
#[sea_orm(column_type = "Text")]
pub owner: String,
#[sea_orm(column_type = "Text", nullable)]
pub associated_token_account: Option<String>,
#[sea_orm(column_type = "Text")]
pub associated_token_account: String,
pub created_at: DateTime,
}

Expand Down
27 changes: 27 additions & 0 deletions entity/src/compression_leafs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.5

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, Default, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "compression_leafs")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub collection_id: Uuid,
#[sea_orm(column_type = "Text")]
pub merkle_tree: String,
#[sea_orm(column_type = "Text")]
pub tree_authority: String,
#[sea_orm(column_type = "Text")]
pub tree_delegate: String,
#[sea_orm(column_type = "Text")]
pub leaf_owner: String,
#[sea_orm(column_type = "Text", nullable)]
pub asset_id: Option<String>,
pub created_at: DateTime,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
1 change: 1 addition & 0 deletions entity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod collection_mints;
pub mod collections;
pub mod compression_leafs;

pub mod prelude;
3 changes: 2 additions & 1 deletion entity/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod prelude;

pub mod collection_mints;
pub mod editions;
pub mod certified_collections;
pub mod certified_collections;
pub mod compression_leafs;
5 changes: 4 additions & 1 deletion entity/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.5

pub use super::{collection_mints::Entity as CollectionMints, collections::Entity as Collections};
pub use super::{
collection_mints::Entity as CollectionMints, collections::Entity as Collections,
compression_leafs::Entity as CompressionLeafs,
};
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod m20230529_134752_create_collections_table;
mod m20230530_131917_create_collection_mints_table;
mod m20230614_132203_make_associated_token_account_nullable_on_collection_mints;
mod m20230616_091724_backfill_associated_token_account_on_collection_mints;
mod m20230725_143421_add_compression_leafs_table;

pub struct Migrator;

Expand All @@ -15,6 +16,7 @@ impl MigratorTrait for Migrator {
Box::new(m20230530_131917_create_collection_mints_table::Migration),
Box::new(m20230614_132203_make_associated_token_account_nullable_on_collection_mints::Migration),
Box::new(m20230616_091724_backfill_associated_token_account_on_collection_mints::Migration),
Box::new(m20230725_143421_add_compression_leafs_table::Migration),
]
}
}
Loading

0 comments on commit a268ab0

Please sign in to comment.