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

Add objects processor #215

Merged
merged 6 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- This file should undo anything in `up.sql`
ALTER TABLE objects DROP COLUMN IF EXISTS is_token;
ALTER TABLE objects DROP COLUMN IF EXISTS is_fungible_asset;
ALTER TABLE current_objects DROP COLUMN IF EXISTS is_token;
ALTER TABLE current_objects DROP COLUMN IF EXISTS is_fungible_asset;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Your SQL goes here
ALTER TABLE objects
ADD COLUMN IF NOT EXISTS is_token BOOLEAN;
ALTER TABLE objects
ADD COLUMN IF NOT EXISTS is_fungible_asset BOOLEAN;
ALTER TABLE current_objects
ADD COLUMN IF NOT EXISTS is_token BOOLEAN;
ALTER TABLE current_objects
ADD COLUMN IF NOT EXISTS is_fungible_asset BOOLEAN;
ALTER TABLE block_metadata_transactions DROP CONSTRAINT IF EXISTS fk_versions;
ALTER TABLE move_modules DROP CONSTRAINT IF EXISTS fk_transaction_versions;
ALTER TABLE move_resources DROP CONSTRAINT IF EXISTS fk_transaction_versions;
ALTER TABLE table_items DROP CONSTRAINT IF EXISTS fk_transaction_versions;
ALTER TABLE write_set_changes DROP CONSTRAINT IF EXISTS fk_transaction_versions;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use crate::{
models::{
token_v2_models::v2_token_utils::ObjectWithMetadata,
object_models::v2_object_utils::ObjectWithMetadata,
user_transactions_models::user_transactions::UserTransaction,
},
schema::account_transactions,
Expand Down
1 change: 0 additions & 1 deletion rust/processor/src/models/default_models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ pub mod move_modules;
pub mod move_resources;
pub mod move_tables;
pub mod transactions;
pub mod v2_objects;
pub mod write_set_changes;
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#![allow(clippy::unused_unit)]

use super::{
v2_fungible_asset_utils::{
FeeStatement, FungibleAssetAggregatedDataMapping, FungibleAssetEvent,
},
v2_fungible_asset_utils::{FeeStatement, FungibleAssetEvent},
v2_fungible_metadata::FungibleAssetMetadataModel,
};
use crate::{
Expand All @@ -17,6 +15,7 @@ use crate::{
coin_activities::CoinActivity,
coin_utils::{CoinEvent, CoinInfoType, EventGuidResource},
},
object_models::v2_object_utils::ObjectAggregatedDataMapping,
token_v2_models::v2_token_utils::TokenStandard,
},
schema::fungible_asset_activities,
Expand Down Expand Up @@ -70,7 +69,7 @@ impl FungibleAssetActivity {
txn_timestamp: chrono::NaiveDateTime,
event_index: i64,
entry_function_id_str: &Option<String>,
fungible_asset_metadata: &FungibleAssetAggregatedDataMapping,
fungible_asset_metadata: &ObjectAggregatedDataMapping,
conn: &mut PgPoolConnection<'_>,
) -> anyhow::Result<Option<Self>> {
let event_type = event.type_str.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
#![allow(clippy::unused_unit)]

use super::{
v2_fungible_asset_activities::EventToCoinType,
v2_fungible_asset_utils::{FungibleAssetAggregatedDataMapping, FungibleAssetStore},
v2_fungible_asset_activities::EventToCoinType, v2_fungible_asset_utils::FungibleAssetStore,
v2_fungible_metadata::FungibleAssetMetadataModel,
};
use crate::{
models::{
coin_models::coin_utils::{CoinInfoType, CoinResource},
object_models::v2_object_utils::ObjectAggregatedDataMapping,
token_v2_models::v2_token_utils::TokenStandard,
},
schema::{current_fungible_asset_balances, fungible_asset_balances},
Expand Down Expand Up @@ -70,7 +70,7 @@ impl FungibleAssetBalance {
write_set_change_index: i64,
txn_version: i64,
txn_timestamp: chrono::NaiveDateTime,
fungible_asset_metadata: &FungibleAssetAggregatedDataMapping,
fungible_asset_metadata: &ObjectAggregatedDataMapping,
conn: &mut PgPoolConnection<'_>,
) -> anyhow::Result<Option<(Self, CurrentFungibleAssetBalance)>> {
if let Some(inner) = &FungibleAssetStore::from_write_resource(write_resource, txn_version)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@

use crate::{
models::{
coin_models::coin_utils::COIN_ADDR,
default_models::{move_resources::MoveResource, v2_objects::CurrentObjectPK},
token_models::token_utils::URI_LENGTH,
token_v2_models::v2_token_utils::{ObjectWithMetadata, ResourceReference, TokenV2},
coin_models::coin_utils::COIN_ADDR, default_models::move_resources::MoveResource,
token_models::token_utils::URI_LENGTH, token_v2_models::v2_token_utils::ResourceReference,
},
utils::util::{deserialize_from_string, truncate_str},
};
use anyhow::{Context, Result};
use aptos_protos::transaction::v1::WriteResource;
use bigdecimal::BigDecimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

const FUNGIBLE_ASSET_LENGTH: usize = 32;
const FUNGIBLE_ASSET_SYMBOL: usize = 10;

/// Tracks all fungible asset related data in a hashmap for quick access (keyed on address of the object core)
pub type FungibleAssetAggregatedDataMapping = HashMap<CurrentObjectPK, FungibleAssetAggregatedData>;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FeeStatement {
#[serde(deserialize_with = "deserialize_from_string")]
Expand All @@ -49,15 +43,6 @@ impl FeeStatement {
}
}

/// This contains objects used by fungible assets
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FungibleAssetAggregatedData {
pub object: ObjectWithMetadata,
pub fungible_asset_metadata: Option<FungibleAssetMetadata>,
pub fungible_asset_store: Option<FungibleAssetStore>,
pub token: Option<TokenV2>,
}

/* Section on fungible assets resources */
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FungibleAssetMetadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#![allow(clippy::extra_unused_lifetimes)]
#![allow(clippy::unused_unit)]

use super::v2_fungible_asset_utils::{FungibleAssetAggregatedDataMapping, FungibleAssetMetadata};
use super::v2_fungible_asset_utils::FungibleAssetMetadata;
use crate::{
models::{
coin_models::coin_utils::{CoinInfoType, CoinResource},
object_models::v2_object_utils::ObjectAggregatedDataMapping,
token_models::collection_datas::{QUERY_RETRIES, QUERY_RETRY_DELAY_MS},
token_v2_models::v2_token_utils::TokenStandard,
},
Expand Down Expand Up @@ -58,7 +59,7 @@ impl FungibleAssetMetadataModel {
write_resource: &WriteResource,
txn_version: i64,
txn_timestamp: chrono::NaiveDateTime,
fungible_asset_metadata: &FungibleAssetAggregatedDataMapping,
fungible_asset_metadata: &ObjectAggregatedDataMapping,
) -> anyhow::Result<Option<Self>> {
if let Some(inner) =
&FungibleAssetMetadata::from_write_resource(write_resource, txn_version)?
Expand Down Expand Up @@ -138,7 +139,7 @@ impl FungibleAssetMetadataModel {
pub async fn is_address_fungible_asset(
conn: &mut PgPoolConnection<'_>,
address: &str,
fungible_asset_metadata: &FungibleAssetAggregatedDataMapping,
fungible_asset_metadata: &ObjectAggregatedDataMapping,
) -> bool {
if let Some(metadata) = fungible_asset_metadata.get(address) {
metadata.token.is_none()
Expand Down
1 change: 1 addition & 0 deletions rust/processor/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod default_models;
pub mod events_models;
pub mod fungible_asset_models;
pub mod ledger_info;
pub mod object_models;
pub mod processor_status;
pub mod property_map;
pub mod stake_models;
Expand Down
5 changes: 5 additions & 0 deletions rust/processor/src/models/object_models/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

pub mod v2_object_utils;
pub mod v2_objects;
100 changes: 100 additions & 0 deletions rust/processor/src/models/object_models/v2_object_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

// This is required because a diesel macro makes clippy sad
#![allow(clippy::extra_unused_lifetimes)]
#![allow(clippy::unused_unit)]

use crate::{
models::{
default_models::move_resources::MoveResource,
fungible_asset_models::v2_fungible_asset_utils::{
FungibleAssetMetadata, FungibleAssetStore, FungibleAssetSupply,
},
token_v2_models::v2_token_utils::{
AptosCollection, FixedSupply, PropertyMapModel, TokenV2, TransferEvent,
UnlimitedSupply, V2TokenResource,
},
},
utils::util::{deserialize_from_string, standardize_address},
};
use aptos_protos::transaction::v1::WriteResource;
use bigdecimal::BigDecimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// PK of current_objects, i.e. object_address
pub type CurrentObjectPK = String;

/// Tracks all object related metadata in a hashmap for quick access (keyed on address of the object)
pub type ObjectAggregatedDataMapping = HashMap<CurrentObjectPK, ObjectAggregatedData>;

/// Index of the event so that we can write its inverse to the db as primary key (to avoid collisiona)
pub type EventIndex = i64;

/// This contains metadata for the object. This only includes fungible asset and token v2 metadata for now.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ObjectAggregatedData {
pub object: ObjectWithMetadata,
pub transfer_event: Option<(EventIndex, TransferEvent)>,
// Fungible asset structs
pub fungible_asset_metadata: Option<FungibleAssetMetadata>,
pub fungible_asset_supply: Option<FungibleAssetSupply>,
pub fungible_asset_store: Option<FungibleAssetStore>,
// Token v2 structs
pub aptos_collection: Option<AptosCollection>,
pub fixed_supply: Option<FixedSupply>,
pub property_map: Option<PropertyMapModel>,
pub token: Option<TokenV2>,
pub unlimited_supply: Option<UnlimitedSupply>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ObjectCore {
pub allow_ungated_transfer: bool,
#[serde(deserialize_with = "deserialize_from_string")]
pub guid_creation_num: BigDecimal,
owner: String,
}

impl ObjectCore {
pub fn get_owner_address(&self) -> String {
standardize_address(&self.owner)
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ObjectWithMetadata {
pub object_core: ObjectCore,
pub state_key_hash: String,
}

impl ObjectWithMetadata {
pub fn from_write_resource(
write_resource: &WriteResource,
txn_version: i64,
) -> anyhow::Result<Option<Self>> {
let type_str = MoveResource::get_outer_type_from_resource(write_resource);
if !V2TokenResource::is_resource_supported(type_str.as_str()) {
return Ok(None);
}
if let V2TokenResource::ObjectCore(inner) = V2TokenResource::from_resource(
&type_str,
&serde_json::from_str(write_resource.data.as_str()).unwrap(),
txn_version,
)? {
Ok(Some(Self {
object_core: inner,
state_key_hash: standardize_address(
hex::encode(write_resource.state_key_hash.as_slice()).as_str(),
),
}))
} else {
Ok(None)
}
}

pub fn get_state_key_hash(&self) -> String {
standardize_address(&self.state_key_hash)
}
}
Loading
Loading