Skip to content

Commit

Permalink
Schema changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rtso committed Apr 25, 2024
1 parent b073efa commit a723c3e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
-- Your SQL goes here
ALTER TABLE fungible_asset_metadata
ADD COLUMN supply_v2 NUMERIC,
ADD COLUMN maximum_v2 NUMERIC;
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;
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}));
}
Expand Down
95 changes: 14 additions & 81 deletions rust/processor/src/models/token_v2_models/v2_token_datas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -34,15 +33,15 @@ pub struct TokenDataV2 {
pub collection_id: String,
pub token_name: String,
pub maximum: Option<BigDecimal>,
pub supply: BigDecimal,
pub supply: Option<BigDecimal>,
pub largest_property_version_v1: Option<BigDecimal>,
pub token_uri: String,
pub token_properties: serde_json::Value,
pub description: String,
pub token_standard: String,
pub is_fungible_v2: Option<bool>,
pub transaction_timestamp: chrono::NaiveDateTime,
pub decimals: i64,
pub decimals: Option<i64>,
}

#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)]
Expand All @@ -53,7 +52,7 @@ pub struct CurrentTokenDataV2 {
pub collection_id: String,
pub token_name: String,
pub maximum: Option<BigDecimal>,
pub supply: BigDecimal,
pub supply: Option<BigDecimal>,
pub largest_property_version_v1: Option<BigDecimal>,
pub token_uri: String,
pub token_properties: serde_json::Value,
Expand All @@ -62,28 +61,7 @@ pub struct CurrentTokenDataV2 {
pub is_fungible_v2: Option<bool>,
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<BigDecimal>,
pub supply: BigDecimal,
pub largest_property_version_v1: Option<BigDecimal>,
pub token_uri: String,
pub description: String,
pub token_properties: serde_json::Value,
pub token_standard: String,
pub is_fungible_v2: Option<bool>,
pub last_transaction_version: i64,
pub last_transaction_timestamp: chrono::NaiveDateTime,
pub inserted_at: chrono::NaiveDateTime,
pub decimals: i64,
pub decimals: Option<i64>,
}

impl TokenDataV2 {
Expand Down Expand Up @@ -132,22 +110,22 @@ 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(),
description: inner.description.clone(),
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,
Expand All @@ -156,7 +134,7 @@ impl TokenDataV2 {
is_fungible_v2,
last_transaction_version: txn_version,
last_transaction_timestamp: txn_timestamp,
decimals: 0,
decimals: None,
},
)))
} else {
Expand Down Expand Up @@ -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(),
),
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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<bool> {
match current_token_datas_v2::table
.filter(current_token_datas_v2::token_data_id.eq(token_data_id))
.first::<Self>(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),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
Expand All @@ -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),
Expand Down Expand Up @@ -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),
});
Expand Down
8 changes: 4 additions & 4 deletions rust/processor/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ diesel::table! {
#[max_length = 128]
token_name -> Varchar,
maximum -> Nullable<Numeric>,
supply -> Numeric,
supply -> Nullable<Numeric>,
largest_property_version_v1 -> Nullable<Numeric>,
#[max_length = 512]
token_uri -> Varchar,
Expand All @@ -537,7 +537,7 @@ diesel::table! {
last_transaction_version -> Int8,
last_transaction_timestamp -> Timestamp,
inserted_at -> Timestamp,
decimals -> Int8,
decimals -> Nullable<Int8>,
}
}

Expand Down Expand Up @@ -1064,7 +1064,7 @@ diesel::table! {
#[max_length = 128]
token_name -> Varchar,
maximum -> Nullable<Numeric>,
supply -> Numeric,
supply -> Nullable<Numeric>,
largest_property_version_v1 -> Nullable<Numeric>,
#[max_length = 512]
token_uri -> Varchar,
Expand All @@ -1075,7 +1075,7 @@ diesel::table! {
is_fungible_v2 -> Nullable<Bool>,
transaction_timestamp -> Timestamp,
inserted_at -> Timestamp,
decimals -> Int8,
decimals -> Nullable<Int8>,
}
}

Expand Down

0 comments on commit a723c3e

Please sign in to comment.