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

Fix saving large token supplies #201

Merged
merged 1 commit into from
Jul 31, 2024
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
2 changes: 1 addition & 1 deletion digital_asset_types/src/dao/generated/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Model {
pub owner_type: OwnerType,
pub delegate: Option<Vec<u8>>,
pub frozen: bool,
pub supply: i64,
pub supply: Decimal,
pub supply_mint: Option<Vec<u8>>,
pub compressed: bool,
pub compressible: bool,
Expand Down
2 changes: 1 addition & 1 deletion digital_asset_types/src/dao/generated/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl EntityName for Entity {
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Serialize, Deserialize)]
pub struct Model {
pub mint: Vec<u8>,
pub supply: i64,
pub supply: Decimal,
pub decimals: i32,
pub token_program: Vec<u8>,
pub mint_authority: Option<Vec<u8>>,
Expand Down
4 changes: 2 additions & 2 deletions digital_asset_types/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn create_asset(
owner_type: Set(owner_type.clone()),
delegate: Set(delegate.clone()),
frozen: Set(frozen),
supply: Set(supply),
supply: Set(supply.into()),
supply_mint: Set(supply_mint.clone()),
compressed: Set(compressed),
compressible: Set(compressible),
Expand All @@ -135,7 +135,7 @@ pub fn create_asset(
owner_type,
delegate,
frozen,
supply,
supply: supply.into(),
supply_mint,
compressed,
compressible,
Expand Down
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod m20240313_120101_add_mpl_core_plugins_columns;
mod m20240319_120101_add_mpl_core_enum_vals;
mod m20240320_120101_add_mpl_core_info_items;
mod m20240520_120101_add_mpl_core_external_plugins_columns;
mod m20240718_161232_change_supply_columns_to_numeric;

pub mod model;

Expand Down Expand Up @@ -93,6 +94,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240319_120101_add_mpl_core_enum_vals::Migration),
Box::new(m20240320_120101_add_mpl_core_info_items::Migration),
Box::new(m20240520_120101_add_mpl_core_external_plugins_columns::Migration),
Box::new(m20240718_161232_change_supply_columns_to_numeric::Migration),
]
}
}
47 changes: 47 additions & 0 deletions migration/src/m20240718_161232_change_supply_columns_to_numeric.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::model::table::{Asset, Tokens};
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(
Table::alter()
.table(Asset::Table)
.modify_column(ColumnDef::new(Asset::Supply).decimal_len(20, 0).not_null())
.to_owned(),
)
.await?;
manager
.alter_table(
Table::alter()
.table(Tokens::Table)
.modify_column(ColumnDef::new(Tokens::Supply).decimal_len(20, 0).not_null())
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Asset::Table)
.modify_column(ColumnDef::new(Asset::Supply).big_integer().not_null())
.to_owned(),
)
.await?;

manager
.alter_table(
Table::alter()
.table(Tokens::Table)
.modify_column(ColumnDef::new(Tokens::Supply).big_integer().not_null())
.to_owned(),
)
.await
}
}
5 changes: 3 additions & 2 deletions program_transformers/src/asset_upserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use {
TransactionTrait,
},
serde_json::value::Value,
sqlx::types::Decimal,
};

pub struct AssetTokenAccountColumns {
Expand Down Expand Up @@ -54,7 +55,7 @@ pub async fn upsert_assets_token_account_columns<T: ConnectionTrait + Transactio

pub struct AssetMintAccountColumns {
pub mint: Vec<u8>,
pub supply: u64,
pub supply: Decimal,
pub supply_mint: Option<Vec<u8>>,
pub slot_updated_mint_account: u64,
}
Expand All @@ -65,7 +66,7 @@ pub async fn upsert_assets_mint_account_columns<T: ConnectionTrait + Transaction
) -> Result<(), DbErr> {
let active_model = asset::ActiveModel {
id: Set(columns.mint),
supply: Set(columns.supply as i64),
supply: Set(columns.supply),
supply_mint: Set(columns.supply_mint),
slot_updated_mint_account: Set(Some(columns.slot_updated_mint_account as i64)),
..Default::default()
Expand Down
5 changes: 3 additions & 2 deletions program_transformers/src/bubblegum/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
mpl_bubblegum::types::{Collection, Creator},
sea_orm::{
entity::{ActiveValue, ColumnTrait, EntityTrait},
prelude::*,
query::{JsonValue, QueryFilter, QuerySelect, QueryTrait},
sea_query::query::OnConflict,
ConnectionTrait, DbBackend, TransactionTrait,
Expand Down Expand Up @@ -269,7 +270,7 @@ pub async fn upsert_asset_with_compression_info<T>(
id: Vec<u8>,
compressed: bool,
compressible: bool,
supply: i64,
supply: u64,
supply_mint: Option<Vec<u8>>,
) -> ProgramTransformerResult<()>
where
Expand All @@ -279,7 +280,7 @@ where
id: ActiveValue::Set(id),
compressed: ActiveValue::Set(compressed),
compressible: ActiveValue::Set(compressible),
supply: ActiveValue::Set(supply),
supply: ActiveValue::Set(Decimal::from(supply)),
supply_mint: ActiveValue::Set(supply_mint),
..Default::default()
};
Expand Down
3 changes: 2 additions & 1 deletion program_transformers/src/mpl_core_program/v1_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use {
heck::ToSnakeCase,
sea_orm::{
entity::{ActiveValue, ColumnTrait, EntityTrait},
prelude::*,
query::{JsonValue, QueryFilter, QueryTrait},
sea_query::query::OnConflict,
sea_query::Expr,
Expand Down Expand Up @@ -309,7 +310,7 @@ pub async fn save_v1_asset<T: ConnectionTrait + TransactionTrait>(
)
.await?;

let supply = 1;
let supply = Decimal::from(1);

// Note: these need to be separate for Token Metadata but here could be one upsert.
upsert_assets_mint_account_columns(
Expand Down
4 changes: 2 additions & 2 deletions program_transformers/src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub async fn handle_token_program_account<'a, 'b>(
mint: ActiveValue::Set(account_key.clone()),
token_program: ActiveValue::Set(account_owner),
slot_updated: ActiveValue::Set(account_info.slot as i64),
supply: ActiveValue::Set(m.supply as i64),
supply: ActiveValue::Set(m.supply.into()),
decimals: ActiveValue::Set(m.decimals as i32),
close_authority: ActiveValue::Set(None),
extension_data: ActiveValue::Set(None),
Expand Down Expand Up @@ -155,7 +155,7 @@ pub async fn handle_token_program_account<'a, 'b>(
AssetMintAccountColumns {
mint: account_key.clone(),
supply_mint: Some(account_key),
supply: m.supply,
supply: m.supply.into(),
slot_updated_mint_account: account_info.slot,
},
db,
Expand Down
3 changes: 2 additions & 1 deletion program_transformers/src/token_metadata/master_edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use {
},
sea_orm::{
entity::{ActiveModelTrait, ActiveValue, EntityTrait, RelationTrait},
prelude::*,
query::{JoinType, QuerySelect, QueryTrait},
sea_query::query::OnConflict,
ConnectionTrait, DatabaseTransaction, DbBackend,
Expand Down Expand Up @@ -86,7 +87,7 @@ pub async fn save_master_edition(

if let Some((_me, Some(asset))) = master_edition {
let mut updatable: asset::ActiveModel = asset.into();
updatable.supply = ActiveValue::Set(1);
updatable.supply = ActiveValue::Set(Decimal::from(1));
updatable.specification_asset_class = ActiveValue::Set(Some(SpecificationAssetClass::Nft));
updatable.update(txn).await?;
}
Expand Down
18 changes: 9 additions & 9 deletions program_transformers/src/token_metadata/v1_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ use {
sea_query::query::OnConflict,
ConnectionTrait, DbBackend, DbErr, TransactionTrait,
},
solana_sdk::{pubkey, pubkey::Pubkey},
solana_sdk::pubkey,
sqlx::types::Decimal,
tracing::warn,
};

pub async fn burn_v1_asset<T: ConnectionTrait + TransactionTrait>(
conn: &T,
id: Pubkey,
id: pubkey::Pubkey,
slot: u64,
) -> ProgramTransformerResult<()> {
let slot_i = slot as i64;
Expand All @@ -62,7 +63,7 @@ pub async fn burn_v1_asset<T: ConnectionTrait + TransactionTrait>(
}

const RETRY_INTERVALS: &[u64] = &[0, 5, 10];
static WSOL_PUBKEY: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
static WSOL_PUBKEY: pubkey::Pubkey = pubkey!("So11111111111111111111111111111111111111112");

pub async fn index_and_fetch_mint_data<T: ConnectionTrait + TransactionTrait>(
conn: &T,
Expand All @@ -84,7 +85,7 @@ pub async fn index_and_fetch_mint_data<T: ConnectionTrait + TransactionTrait>(
AssetMintAccountColumns {
mint: mint_pubkey_vec.clone(),
supply_mint: Some(token.mint.clone()),
supply: token.supply as u64,
supply: token.supply,
slot_updated_mint_account: token.slot_updated as u64,
},
conn,
Expand Down Expand Up @@ -189,13 +190,12 @@ pub async fn save_v1_asset<T: ConnectionTrait + TransactionTrait>(
index_and_fetch_mint_data(conn, mint_pubkey_vec.clone()).await?;

// get supply of token, default to 1 since most cases will be NFTs. Token mint ingester will properly set supply if token_result is None
let supply = token.map(|t| t.supply).unwrap_or(1);

let supply = token.map(|t| t.supply).unwrap_or_else(|| Decimal::from(1));
// Map unknown ownership types based on the supply.
if ownership_type == OwnerType::Unknown {
ownership_type = match supply.cmp(&1) {
std::cmp::Ordering::Equal => OwnerType::Single,
std::cmp::Ordering::Greater => OwnerType::Token,
ownership_type = match supply {
s if s == Decimal::from(1) => OwnerType::Single,
s if s > Decimal::from(1) => OwnerType::Token,
_ => OwnerType::Unknown,
};
};
Expand Down
Loading