Skip to content

Commit

Permalink
WIP : token extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Nov 11, 2024
1 parent bbb9910 commit d0cfd16
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 69 deletions.
72 changes: 17 additions & 55 deletions program_transformers/src/asset_upserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
TransactionTrait,
},
serde_json::value::Value,
sqlx::types::{chrono, Decimal},
sqlx::types::Decimal,
};

pub struct AssetTokenAccountColumns {
Expand Down Expand Up @@ -57,31 +57,41 @@ pub async fn upsert_assets_token_account_columns<T: ConnectionTrait + Transactio
pub struct AssetMintAccountColumns {
pub mint: Vec<u8>,
pub supply: Decimal,
pub supply_mint: Option<Vec<u8>>,
pub slot_updated_mint_account: u64,
pub slot_updated_mint_account: i64,
pub extensions: Option<Value>,
}

pub async fn upsert_assets_mint_account_columns<T: ConnectionTrait + TransactionTrait>(
columns: AssetMintAccountColumns,
is_non_fungible: bool,
txn_or_conn: &T,
) -> Result<(), DbErr> {
let specification_asset_class = if is_non_fungible {
None
} else {
// If its not non-fungible then
// by default, we assume that the asset is fungibleToken and later update based on token metadata
Some(SpecificationAssetClass::FungibleToken)
};

let active_model = asset::ActiveModel {
id: Set(columns.mint),
id: Set(columns.mint.clone()),
supply: Set(columns.supply),
supply_mint: Set(columns.supply_mint),
slot_updated_mint_account: Set(Some(columns.slot_updated_mint_account as i64)),
supply_mint: Set(Some(columns.mint.clone())),
slot_updated_mint_account: Set(Some(columns.slot_updated_mint_account)),
mint_extensions: Set(columns.extensions),
asset_data: Set(Some(columns.mint)),
specification_asset_class: Set(specification_asset_class),
..Default::default()
};
let mut query = asset::Entity::insert(active_model)
.on_conflict(
OnConflict::columns([asset::Column::Id])
.update_columns([
asset::Column::Supply,
asset::Column::SupplyMint,
asset::Column::SlotUpdatedMintAccount,
asset::Column::MintExtensions,
asset::Column::SlotUpdated,
])
.to_owned(),
)
Expand Down Expand Up @@ -181,51 +191,3 @@ pub async fn upsert_assets_metadata_account_columns<T: ConnectionTrait + Transac
txn_or_conn.execute(query).await?;
Ok(())
}

pub struct FungibleAssetColumns {
pub mint: Vec<u8>,
pub supply: Decimal,
pub slot_updated: i64,
pub asset_data: Option<Vec<u8>>,
pub extensions: Option<Value>,
}

pub async fn upsert_fungible_asset<T: ConnectionTrait + TransactionTrait>(
fungible_asset_data: FungibleAssetColumns,
txn_or_conn: &T,
) -> Result<(), DbErr> {
let active_model = asset::ActiveModel {
id: Set(fungible_asset_data.mint.clone()),
supply: Set(fungible_asset_data.supply),
supply_mint: Set(Some(fungible_asset_data.mint)),
slot_updated: Set(Some(fungible_asset_data.slot_updated)),
slot_updated_mint_account: Set(Some(fungible_asset_data.slot_updated)),
asset_data: Set(fungible_asset_data.asset_data),
mint_extensions: Set(fungible_asset_data.extensions),
specification_asset_class: Set(Some(SpecificationAssetClass::FungibleToken)),
created_at: Set(Some(chrono::Utc::now().into())),
..Default::default()
};

let mut query = asset::Entity::insert(active_model)
.on_conflict(
OnConflict::columns([asset::Column::Id])
.update_columns([
asset::Column::Supply,
asset::Column::SlotUpdated,
asset::Column::SlotUpdatedMintAccount,
asset::Column::MintExtensions,
])
.to_owned(),
)
.build(DbBackend::Postgres);

query.sql = format!(
"{} WHERE excluded.slot_updated >= asset.slot_updated OR asset.slot_updated IS NULL",
query.sql
);

txn_or_conn.execute(query).await?;

Ok(())
}
4 changes: 2 additions & 2 deletions program_transformers/src/mpl_core_program/v1_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ pub async fn save_v1_asset<T: ConnectionTrait + TransactionTrait>(
upsert_assets_mint_account_columns(
AssetMintAccountColumns {
mint: id_vec.clone(),
supply_mint: None,
supply,
slot_updated_mint_account: slot,
slot_updated_mint_account: slot as i64,
extensions: None,
},
true,
&txn,
)
.await?;
Expand Down
11 changes: 6 additions & 5 deletions program_transformers/src/token/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
asset_upserts::{upsert_fungible_asset, FungibleAssetColumns},
asset_upserts::{upsert_assets_mint_account_columns, AssetMintAccountColumns},
error::ProgramTransformerResult,
AccountInfo, DownloadMetadataNotifier,
},
Expand Down Expand Up @@ -116,15 +116,16 @@ pub async fn handle_token_program_account<'a, 'b>(
db.execute(query).await?;

let txn = db.begin().await?;
let is_non_fungible = m.decimals == 0 && m.mint_authority.is_none();

upsert_fungible_asset(
FungibleAssetColumns {
upsert_assets_mint_account_columns(
AssetMintAccountColumns {
mint: account_key.clone(),
supply: m.supply.into(),
slot_updated: slot,
asset_data: Some(account_key.clone()),
slot_updated_mint_account: slot,
extensions: None,
},
is_non_fungible,
&txn,
)
.await?;
Expand Down
12 changes: 7 additions & 5 deletions program_transformers/src/token_extensions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
asset_upserts::{upsert_fungible_asset, FungibleAssetColumns},
asset_upserts::{upsert_assets_mint_account_columns, AssetMintAccountColumns},
error::{ProgramTransformerError, ProgramTransformerResult},
filter_non_null_fields, AccountInfo,
},
Expand Down Expand Up @@ -157,14 +157,16 @@ pub async fn handle_token_extensions_program_account<'a, 'b, 'c>(
db.execute(query).await?;
let txn = db.begin().await?;

upsert_fungible_asset(
FungibleAssetColumns {
let is_non_fungible = m.decimals == 0 && m.mint_authority.is_none();

upsert_assets_mint_account_columns(
AssetMintAccountColumns {
mint: account_key.clone(),
supply: m.supply.into(),
slot_updated: slot,
asset_data: Some(account_key.clone()),
slot_updated_mint_account: slot,
extensions: extensions.clone(),
},
is_non_fungible,
&txn,
)
.await?;
Expand Down
9 changes: 7 additions & 2 deletions program_transformers/src/token_metadata/v1_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,20 @@ pub async fn index_and_fetch_mint_data<T: ConnectionTrait + TransactionTrait>(
)
.await?;

let is_non_fungible = token
.as_ref()
.map(|t| t.decimals == 0 && t.mint_authority.is_none())
.unwrap_or(false);

if let Some(token) = token {
upsert_assets_mint_account_columns(
AssetMintAccountColumns {
mint: mint_pubkey_vec.clone(),
supply_mint: Some(token.mint.clone()),
supply: token.supply,
slot_updated_mint_account: token.slot_updated as u64,
slot_updated_mint_account: token.slot_updated,
extensions: token.extensions.clone(),
},
is_non_fungible,
conn,
)
.await
Expand Down

0 comments on commit d0cfd16

Please sign in to comment.