diff --git a/rust/processor/migrations/2024-04-18-173631_fungible_token/up.sql b/rust/processor/migrations/2024-04-18-173631_fungible_token/up.sql index 340782c8e..186d5fa15 100644 --- a/rust/processor/migrations/2024-04-18-173631_fungible_token/up.sql +++ b/rust/processor/migrations/2024-04-18-173631_fungible_token/up.sql @@ -1,4 +1,12 @@ -- Your SQL goes here ALTER TABLE fungible_asset_metadata ADD COLUMN supply_v2 NUMERIC, -ADD COLUMN maximum_v2 NUMERIC; \ No newline at end of file +ADD COLUMN maximum_v2 NUMERIC; + +ALTER TABLE current_token_datas_v2 +ALTER COLUMN supply DROP NOT NULL, +ALTER COLUMN decimals DROP NOT NULL; + +ALTER TABLE token_datas_v2 +ALTER COLUMN supply DROP NOT NULL, +ALTER COLUMN decimals DROP NOT NULL; \ No newline at end of file diff --git a/rust/processor/src/models/token_v2_models/v2_token_activities.rs b/rust/processor/src/models/token_v2_models/v2_token_activities.rs index 1604e938e..824bec15a 100644 --- a/rust/processor/src/models/token_v2_models/v2_token_activities.rs +++ b/rust/processor/src/models/token_v2_models/v2_token_activities.rs @@ -150,7 +150,7 @@ impl TokenActivityV2 { after_value: token_activity_helper.after_value, entry_function_id_str: entry_function_id_str.clone(), token_standard: TokenStandard::V2.to_string(), - is_fungible_v2: Some(false), + is_fungible_v2: None, transaction_timestamp: txn_timestamp, })); } else { @@ -178,7 +178,7 @@ impl TokenActivityV2 { after_value: None, entry_function_id_str: entry_function_id_str.clone(), token_standard: TokenStandard::V2.to_string(), - is_fungible_v2: Some(false), + is_fungible_v2: None, transaction_timestamp: txn_timestamp, })); } diff --git a/rust/processor/src/models/token_v2_models/v2_token_datas.rs b/rust/processor/src/models/token_v2_models/v2_token_datas.rs index eb2f4c2c3..f4f046f82 100644 --- a/rust/processor/src/models/token_v2_models/v2_token_datas.rs +++ b/rust/processor/src/models/token_v2_models/v2_token_datas.rs @@ -12,12 +12,11 @@ use crate::{ token_models::token_utils::TokenWriteSet, }, schema::{current_token_datas_v2, token_datas_v2}, - utils::{database::PgPoolConnection, util::standardize_address}, + utils::util::standardize_address, }; use aptos_protos::transaction::v1::{WriteResource, WriteTableItem}; -use bigdecimal::{BigDecimal, Zero}; +use bigdecimal::BigDecimal; use diesel::prelude::*; -use diesel_async::RunQueryDsl; use field_count::FieldCount; use serde::{Deserialize, Serialize}; @@ -34,7 +33,7 @@ pub struct TokenDataV2 { pub collection_id: String, pub token_name: String, pub maximum: Option, - pub supply: BigDecimal, + pub supply: Option, pub largest_property_version_v1: Option, pub token_uri: String, pub token_properties: serde_json::Value, @@ -42,7 +41,7 @@ pub struct TokenDataV2 { pub token_standard: String, pub is_fungible_v2: Option, pub transaction_timestamp: chrono::NaiveDateTime, - pub decimals: i64, + pub decimals: Option, } #[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)] @@ -53,7 +52,7 @@ pub struct CurrentTokenDataV2 { pub collection_id: String, pub token_name: String, pub maximum: Option, - pub supply: BigDecimal, + pub supply: Option, pub largest_property_version_v1: Option, pub token_uri: String, pub token_properties: serde_json::Value, @@ -62,28 +61,7 @@ pub struct CurrentTokenDataV2 { pub is_fungible_v2: Option, pub last_transaction_version: i64, pub last_transaction_timestamp: chrono::NaiveDateTime, - pub decimals: i64, -} - -#[derive(Debug, Deserialize, Identifiable, Queryable, Serialize)] -#[diesel(primary_key(token_data_id))] -#[diesel(table_name = current_token_datas_v2)] -pub struct CurrentTokenDataV2Query { - pub token_data_id: String, - pub collection_id: String, - pub token_name: String, - pub maximum: Option, - pub supply: BigDecimal, - pub largest_property_version_v1: Option, - pub token_uri: String, - pub description: String, - pub token_properties: serde_json::Value, - pub token_standard: String, - pub is_fungible_v2: Option, - pub last_transaction_version: i64, - pub last_transaction_timestamp: chrono::NaiveDateTime, - pub inserted_at: chrono::NaiveDateTime, - pub decimals: i64, + pub decimals: Option, } impl TokenDataV2 { @@ -132,7 +110,7 @@ impl TokenDataV2 { collection_id: collection_id.clone(), token_name: token_name.clone(), maximum: None, - supply: BigDecimal::zero(), + supply: None, largest_property_version_v1: None, token_uri: token_uri.clone(), token_properties: token_properties.clone(), @@ -140,14 +118,14 @@ impl TokenDataV2 { token_standard: TokenStandard::V2.to_string(), is_fungible_v2, transaction_timestamp: txn_timestamp, - decimals: 0, + decimals: None, }, CurrentTokenDataV2 { token_data_id, collection_id, token_name, maximum: None, - supply: BigDecimal::zero(), + supply: None, largest_property_version_v1: None, token_uri, token_properties, @@ -156,7 +134,7 @@ impl TokenDataV2 { is_fungible_v2, last_transaction_version: txn_version, last_transaction_timestamp: txn_timestamp, - decimals: 0, + decimals: None, }, ))) } else { @@ -204,7 +182,7 @@ impl TokenDataV2 { collection_id: collection_id.clone(), token_name: token_name.clone(), maximum: Some(token_data.maximum.clone()), - supply: token_data.supply.clone(), + supply: Some(token_data.supply.clone()), largest_property_version_v1: Some( token_data.largest_property_version.clone(), ), @@ -214,14 +192,14 @@ impl TokenDataV2 { token_standard: TokenStandard::V1.to_string(), is_fungible_v2: None, transaction_timestamp: txn_timestamp, - decimals: 0, + decimals: Some(0), }, CurrentTokenDataV2 { token_data_id, collection_id, token_name, maximum: Some(token_data.maximum), - supply: token_data.supply, + supply: Some(token_data.supply), largest_property_version_v1: Some(token_data.largest_property_version), token_uri, token_properties: token_data.default_properties, @@ -230,7 +208,7 @@ impl TokenDataV2 { is_fungible_v2: None, last_transaction_version: txn_version, last_transaction_timestamp: txn_timestamp, - decimals: 0, + decimals: Some(0), }, ))); } else { @@ -244,49 +222,4 @@ impl TokenDataV2 { } Ok(None) } - - /// A fungible asset can also be a token. We will make a best effort guess at whether this is a fungible token. - /// 1. If metadata is present with a token object, then is a token - /// 2. If metadata is not present, we will do a lookup in the db. - pub async fn is_address_token( - conn: &mut PgPoolConnection<'_>, - token_data_id: &str, - object_aggregated_data_mapping: &ObjectAggregatedDataMapping, - txn_version: i64, - ) -> bool { - if let Some(object_data) = object_aggregated_data_mapping.get(token_data_id) { - return object_data.token.is_some(); - } - match CurrentTokenDataV2Query::get_exists(conn, token_data_id).await { - Ok(is_token) => is_token, - Err(e) => { - // TODO: Standardize this error handling - panic!("Version: {}, error {:?}", txn_version, e) - }, - } - } -} - -impl CurrentTokenDataV2Query { - /// TODO: change this to diesel exists. Also this only checks once so may miss some data if coming from another thread - pub async fn get_exists( - conn: &mut PgPoolConnection<'_>, - token_data_id: &str, - ) -> anyhow::Result { - match current_token_datas_v2::table - .filter(current_token_datas_v2::token_data_id.eq(token_data_id)) - .first::(conn) - .await - .optional() - { - Ok(result) => { - if result.is_some() { - Ok(true) - } else { - Ok(false) - } - }, - Err(e) => anyhow::bail!("Error checking if token_data_id exists: {:?}", e), - } - } } diff --git a/rust/processor/src/models/token_v2_models/v2_token_ownerships.rs b/rust/processor/src/models/token_v2_models/v2_token_ownerships.rs index 0675274ca..b5d111cdd 100644 --- a/rust/processor/src/models/token_v2_models/v2_token_ownerships.rs +++ b/rust/processor/src/models/token_v2_models/v2_token_ownerships.rs @@ -137,7 +137,7 @@ impl TokenOwnershipV2 { token_properties_mutated_v1: None, is_soulbound_v2: Some(is_soulbound), token_standard: TokenStandard::V2.to_string(), - is_fungible_v2: token_data.is_fungible_v2, + is_fungible_v2: None, transaction_timestamp: token_data.transaction_timestamp, non_transferrable_by_owner: Some(is_soulbound), }); @@ -158,7 +158,7 @@ impl TokenOwnershipV2 { token_properties_mutated_v1: None, is_soulbound_v2: Some(is_soulbound), token_standard: TokenStandard::V2.to_string(), - is_fungible_v2: token_data.is_fungible_v2, + is_fungible_v2: None, last_transaction_version: token_data.transaction_version, last_transaction_timestamp: token_data.transaction_timestamp, non_transferrable_by_owner: Some(is_soulbound), @@ -186,7 +186,7 @@ impl TokenOwnershipV2 { token_properties_mutated_v1: None, is_soulbound_v2: Some(is_soulbound), token_standard: TokenStandard::V2.to_string(), - is_fungible_v2: token_data.is_fungible_v2, + is_fungible_v2: None, transaction_timestamp: token_data.transaction_timestamp, non_transferrable_by_owner: Some(is_soulbound), }); diff --git a/rust/processor/src/schema.rs b/rust/processor/src/schema.rs index f2cf33e87..fafe9ff25 100644 --- a/rust/processor/src/schema.rs +++ b/rust/processor/src/schema.rs @@ -525,7 +525,7 @@ diesel::table! { #[max_length = 128] token_name -> Varchar, maximum -> Nullable, - supply -> Numeric, + supply -> Nullable, largest_property_version_v1 -> Nullable, #[max_length = 512] token_uri -> Varchar, @@ -537,7 +537,7 @@ diesel::table! { last_transaction_version -> Int8, last_transaction_timestamp -> Timestamp, inserted_at -> Timestamp, - decimals -> Int8, + decimals -> Nullable, } } @@ -1064,7 +1064,7 @@ diesel::table! { #[max_length = 128] token_name -> Varchar, maximum -> Nullable, - supply -> Numeric, + supply -> Nullable, largest_property_version_v1 -> Nullable, #[max_length = 512] token_uri -> Varchar, @@ -1075,7 +1075,7 @@ diesel::table! { is_fungible_v2 -> Nullable, transaction_timestamp -> Timestamp, inserted_at -> Timestamp, - decimals -> Int8, + decimals -> Nullable, } }