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

Refactor token v2 models #704

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions rust/processor/src/db/common/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ pub mod event_models;
pub mod fungible_asset_models;
pub mod object_models;
pub mod stake_models;
pub mod token_models;
pub mod token_v2_models;

const DEFAULT_NONE: &str = "NULL";
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
db::postgres::models::{
default_models::move_resources::MoveResource, token_models::token_utils::Table,
db::{
common::models::token_models::token_utils::Table,
postgres::models::default_models::move_resources::MoveResource,
},
utils::util::{deserialize_from_string, standardize_address},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

pub mod collection_datas;
pub mod nft_points;
pub mod token_activities;
pub mod token_claims;
pub mod token_datas;
pub mod token_ownerships;
pub mod token_royalty;
pub mod token_utils;
pub mod tokens;
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@
#![allow(clippy::unused_unit)]

use crate::{
db::{
common::models::token_v2_models::raw_v2_token_activities::TokenActivityHelperV1,
postgres::models::token_models::{token_utils::TokenWriteSet, tokens::TableHandleToOwner},
bq_analytics::generic_parquet_processor::{GetTimeStamp, HasVersion, NamedTable},
db::common::models::{
token_models::{token_utils::TokenWriteSet, tokens::TableHandleToOwner},
token_v2_models::v2_token_activities::TokenActivityHelperV1,
},
schema::current_token_pending_claims,
utils::util::standardize_address,
};
use ahash::AHashMap;
use allocative_derive::Allocative;
use aptos_protos::transaction::v1::{DeleteTableItem, WriteTableItem};
use bigdecimal::{BigDecimal, Zero};
use bigdecimal::{BigDecimal, ToPrimitive, Zero};
use field_count::FieldCount;
use parquet_derive::ParquetRecordWriter;
use serde::{Deserialize, Serialize};

// Map to keep track of the metadata of token offers that were claimed. The key is the token data id of the offer.
// Potentially it'd also be useful to keep track of offers that were canceled.
pub type TokenV1Claimed = AHashMap<String, TokenActivityHelperV1>;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RawCurrentTokenPendingClaim {
pub struct CurrentTokenPendingClaim {
pub token_data_id_hash: String,
pub property_version: BigDecimal,
pub from_address: String,
Expand All @@ -39,7 +44,7 @@
pub collection_id: String,
}

impl Ord for RawCurrentTokenPendingClaim {
impl Ord for CurrentTokenPendingClaim {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.token_data_id_hash
.cmp(&other.token_data_id_hash)
Expand All @@ -49,13 +54,13 @@
}
}

impl PartialOrd for RawCurrentTokenPendingClaim {
impl PartialOrd for CurrentTokenPendingClaim {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl RawCurrentTokenPendingClaim {
impl CurrentTokenPendingClaim {
/// Token claim is stored in a table in the offerer's account. The key is token_offer_id (token_id + to address)
/// and value is token (token_id + amount)
pub fn from_write_table_item(
Expand Down Expand Up @@ -210,6 +215,124 @@
}
}

pub trait CurrentTokenPendingClaimConvertible {
fn from_raw(raw_item: RawCurrentTokenPendingClaim) -> Self;
/// This is a parquet version of CurrentTokenPendingClaim

Check warning on line 218 in rust/processor/src/db/common/models/token_models/token_claims.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/common/models/token_models/token_claims.rs#L218

Added line #L218 was not covered by tests
#[derive(
Allocative, Clone, Debug, Default, Deserialize, FieldCount, ParquetRecordWriter, Serialize,

Check warning on line 220 in rust/processor/src/db/common/models/token_models/token_claims.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/common/models/token_models/token_claims.rs#L220

Added line #L220 was not covered by tests
)]
pub struct ParquetCurrentTokenPendingClaim {
pub token_data_id_hash: String,
pub property_version: u64,
pub from_address: String,
pub to_address: String,
pub collection_data_id_hash: String,
pub creator_address: String,
pub collection_name: String,
pub name: String,
pub amount: String, // String format of BigDecimal
pub table_handle: String,
pub last_transaction_version: i64,
#[allocative(skip)]
pub last_transaction_timestamp: chrono::NaiveDateTime,
pub token_data_id: String,
pub collection_id: String,
}

impl NamedTable for ParquetCurrentTokenPendingClaim {
const TABLE_NAME: &'static str = "current_token_pending_claims";
}

impl HasVersion for ParquetCurrentTokenPendingClaim {
fn version(&self) -> i64 {
self.last_transaction_version
}

Check warning on line 247 in rust/processor/src/db/common/models/token_models/token_claims.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/common/models/token_models/token_claims.rs#L245-L247

Added lines #L245 - L247 were not covered by tests
}

impl GetTimeStamp for ParquetCurrentTokenPendingClaim {
fn get_timestamp(&self) -> chrono::NaiveDateTime {
self.last_transaction_timestamp
}

Check warning on line 253 in rust/processor/src/db/common/models/token_models/token_claims.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/common/models/token_models/token_claims.rs#L251-L253

Added lines #L251 - L253 were not covered by tests
}

impl From<CurrentTokenPendingClaim> for ParquetCurrentTokenPendingClaim {
fn from(raw_item: CurrentTokenPendingClaim) -> Self {
Self {
token_data_id_hash: raw_item.token_data_id_hash,
property_version: raw_item
.property_version
.to_u64()
.expect("Failed to convert property_version to u64"),
from_address: raw_item.from_address,
to_address: raw_item.to_address,
collection_data_id_hash: raw_item.collection_data_id_hash,
creator_address: raw_item.creator_address,
collection_name: raw_item.collection_name,
name: raw_item.name,
amount: raw_item.amount.to_string(), // (assuming amount is non-critical)
table_handle: raw_item.table_handle,
last_transaction_version: raw_item.last_transaction_version,
last_transaction_timestamp: raw_item.last_transaction_timestamp,
token_data_id: raw_item.token_data_id,
collection_id: raw_item.collection_id,
}
}

Check warning on line 277 in rust/processor/src/db/common/models/token_models/token_claims.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/common/models/token_models/token_claims.rs#L257-L277

Added lines #L257 - L277 were not covered by tests
}

/// This is a postgres version of CurrentTokenPendingClaim
#[derive(
Clone, Debug, Deserialize, Eq, FieldCount, Identifiable, Insertable, PartialEq, Serialize,
)]
#[diesel(primary_key(token_data_id_hash, property_version, from_address, to_address))]
#[diesel(table_name = current_token_pending_claims)]
pub struct PostgresCurrentTokenPendingClaim {
pub token_data_id_hash: String,
pub property_version: BigDecimal,
pub from_address: String,
pub to_address: String,
pub collection_data_id_hash: String,
pub creator_address: String,
pub collection_name: String,
pub name: String,
pub amount: BigDecimal,
pub table_handle: String,
pub last_transaction_version: i64,
pub last_transaction_timestamp: chrono::NaiveDateTime,
pub token_data_id: String,
pub collection_id: String,
}

impl Ord for PostgresCurrentTokenPendingClaim {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.token_data_id_hash
.cmp(&other.token_data_id_hash)
.then(self.property_version.cmp(&other.property_version))
.then(self.from_address.cmp(&other.from_address))
.then(self.to_address.cmp(&other.to_address))
}

Check warning on line 310 in rust/processor/src/db/common/models/token_models/token_claims.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/common/models/token_models/token_claims.rs#L304-L310

Added lines #L304 - L310 were not covered by tests
}

impl PartialOrd for PostgresCurrentTokenPendingClaim {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}

Check warning on line 316 in rust/processor/src/db/common/models/token_models/token_claims.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/common/models/token_models/token_claims.rs#L314-L316

Added lines #L314 - L316 were not covered by tests
}

impl From<CurrentTokenPendingClaim> for PostgresCurrentTokenPendingClaim {
fn from(raw_item: CurrentTokenPendingClaim) -> Self {
Self {
token_data_id_hash: raw_item.token_data_id_hash,
property_version: raw_item.property_version,
from_address: raw_item.from_address,
to_address: raw_item.to_address,
collection_data_id_hash: raw_item.collection_data_id_hash,
creator_address: raw_item.creator_address,
collection_name: raw_item.collection_name,
name: raw_item.name,
amount: raw_item.amount,
table_handle: raw_item.table_handle,
last_transaction_version: raw_item.last_transaction_version,
last_transaction_timestamp: raw_item.last_transaction_timestamp,
token_data_id: raw_item.token_data_id,
collection_id: raw_item.collection_id,
}
}
}
Loading
Loading