Skip to content

Commit

Permalink
[Processor] Simplify some logic to reduce unnecessary duplicated work.
Browse files Browse the repository at this point in the history
  • Loading branch information
grao1991 committed Sep 6, 2024
1 parent e7bc40d commit 3cb1385
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 452 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ impl AccountTransaction {
// owner as well.
// This handles partial deletes as well.
accounts.insert(standardize_address(res.address.as_str()));
if let Some(inner) =
&ObjectWithMetadata::from_write_resource(res, txn_version).unwrap()
{
if let Some(inner) = &ObjectWithMetadata::from_write_resource(res).unwrap() {
accounts.insert(inner.object_core.get_owner_address());
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

use crate::{
db::common::models::{
coin_models::coin_utils::COIN_ADDR,
default_models::move_resources::MoveResource,
fungible_asset_models::v2_fungible_asset_utils::{
ConcurrentFungibleAssetBalance, ConcurrentFungibleAssetSupply, FungibleAssetMetadata,
FungibleAssetStore, FungibleAssetSupply,
},
token_v2_models::v2_token_utils::{
AptosCollection, ConcurrentSupply, FixedSupply, PropertyMapModel, TokenIdentifiers,
TokenV2, TransferEvent, UnlimitedSupply, V2TokenResource,
TokenV2, TransferEvent, UnlimitedSupply,
},
},
utils::util::{deserialize_from_string, standardize_address},
Expand Down Expand Up @@ -100,35 +101,33 @@ impl ObjectCore {
}
}

impl TryFrom<&WriteResource> for ObjectCore {
type Error = anyhow::Error;

fn try_from(write_resource: &WriteResource) -> anyhow::Result<Self> {
serde_json::from_str(write_resource.data.as_str()).map_err(anyhow::Error::msg)
}
}

#[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>> {
pub fn from_write_resource(write_resource: &WriteResource) -> anyhow::Result<Option<Self>> {
let type_str = MoveResource::get_outer_type_from_write_resource(write_resource);
if !V2TokenResource::is_resource_supported(type_str.as_str()) {
if type_str != format!("{}::object::ObjectCore", COIN_ADDR) {
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)
}
let object_core = write_resource.try_into()?;
let state_key_hash =
standardize_address(hex::encode(write_resource.state_key_hash.as_slice()).as_str());
Ok(Some(Self {
object_core,
state_key_hash,
}))
}

pub fn get_state_key_hash(&self) -> String {
Expand All @@ -139,28 +138,20 @@ impl ObjectWithMetadata {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Untransferable {}

impl TryFrom<&WriteResource> for Untransferable {
type Error = anyhow::Error;

fn try_from(write_resource: &WriteResource) -> anyhow::Result<Self> {
serde_json::from_str(write_resource.data.as_str()).map_err(anyhow::Error::msg)
}
}

impl Untransferable {
pub fn from_write_resource(
write_resource: &WriteResource,
txn_version: i64,
) -> anyhow::Result<Option<Self>> {
pub fn from_write_resource(write_resource: &WriteResource) -> anyhow::Result<Option<Self>> {
let type_str = MoveResource::get_outer_type_from_write_resource(write_resource);
if !V2TokenResource::is_resource_supported(type_str.as_str()) {
if type_str != format!("{}::object::Untransferable", COIN_ADDR) {
return Ok(None);
}
let resource = MoveResource::from_write_resource(
write_resource,
0, // Placeholder, this isn't used anyway
txn_version,
0, // Placeholder, this isn't used anyway
);

if let V2TokenResource::Untransferable(inner) =
V2TokenResource::from_resource(&type_str, resource.data.as_ref().unwrap(), txn_version)?
{
Ok(Some(inner))
} else {
Ok(None)
}
Ok(Some(write_resource.try_into()?))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl TokenDataV2 {
txn_timestamp: chrono::NaiveDateTime,
object_metadatas: &ObjectAggregatedDataMapping,
) -> anyhow::Result<Option<Self>> {
if let Some(inner) = &TokenV2::from_write_resource(write_resource, txn_version)? {
if let Some(inner) = &TokenV2::from_write_resource(write_resource)? {
let token_data_id = standardize_address(&write_resource.address.to_string());
let mut token_name = inner.get_name_trunc();
let is_fungible_v2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,7 @@ impl TokenOwnershipV2 {
.get(&standardize_address(&token_data_id))
.is_some()
{
if let Some(object) =
&ObjectWithMetadata::from_write_resource(write_resource, txn_version)?
{
if let Some(object) = &ObjectWithMetadata::from_write_resource(write_resource)? {
let object_core = &object.object_core;
let owner_address = object_core.get_owner_address();
let storage_id = token_data_id.clone();
Expand Down
202 changes: 95 additions & 107 deletions rust/processor/src/db/common/models/token_v2_models/v2_collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(clippy::extra_unused_lifetimes)]
#![allow(clippy::unused_unit)]

use super::v2_token_utils::{TokenStandard, V2TokenResource};
use super::v2_token_utils::{Collection, TokenStandard, TOKEN_V2_ADDR};
use crate::{
db::common::models::{
default_models::move_resources::MoveResource,
Expand Down Expand Up @@ -88,121 +88,109 @@ impl CollectionV2 {
object_metadatas: &ObjectAggregatedDataMapping,
) -> anyhow::Result<Option<(Self, CurrentCollectionV2)>> {
let type_str = MoveResource::get_outer_type_from_write_resource(write_resource);
if !V2TokenResource::is_resource_supported(type_str.as_str()) {
if type_str != format!("{}::collection::Collection", TOKEN_V2_ADDR) {
return Ok(None);
}
let resource = MoveResource::from_write_resource(
write_resource,
0, // Placeholder, this isn't used anyway
txn_version,
0, // Placeholder, this isn't used anyway
);

if let V2TokenResource::Collection(inner) = &V2TokenResource::from_resource(
&type_str,
resource.data.as_ref().unwrap(),
txn_version,
)? {
let (mut current_supply, mut max_supply, mut total_minted_v2) =
(BigDecimal::zero(), None, None);
let (mut mutable_description, mut mutable_uri) = (None, None);
let mut collection_properties = serde_json::Value::Null;
if let Some(object_data) = object_metadatas.get(&resource.address) {
// Getting supply data (prefer fixed supply over unlimited supply although they should never appear at the same time anyway)
let fixed_supply = object_data.fixed_supply.as_ref();
let unlimited_supply = object_data.unlimited_supply.as_ref();
if let Some(supply) = unlimited_supply {
(current_supply, max_supply, total_minted_v2) = (
supply.current_supply.clone(),
None,
Some(supply.total_minted.clone()),
);
}
if let Some(supply) = fixed_supply {
(current_supply, max_supply, total_minted_v2) = (
supply.current_supply.clone(),
Some(supply.max_supply.clone()),
Some(supply.total_minted.clone()),
);
}
let inner: Collection = write_resource.try_into()?;

// Aggregator V2 enables a separate struct for supply
let concurrent_supply = object_data.concurrent_supply.as_ref();
if let Some(supply) = concurrent_supply {
(current_supply, max_supply, total_minted_v2) = (
supply.current_supply.value.clone(),
if supply.current_supply.max_value == u64::MAX.into() {
None
} else {
Some(supply.current_supply.max_value.clone())
},
Some(supply.total_minted.value.clone()),
);
}

// Getting collection mutability config from AptosCollection
let collection = object_data.aptos_collection.as_ref();
if let Some(collection) = collection {
mutable_description = Some(collection.mutable_description);
mutable_uri = Some(collection.mutable_uri);
}
let (mut current_supply, mut max_supply, mut total_minted_v2) =
(BigDecimal::zero(), None, None);
let (mut mutable_description, mut mutable_uri) = (None, None);
let mut collection_properties = serde_json::Value::Null;
let address = standardize_address(&write_resource.address);
if let Some(object_data) = object_metadatas.get(&address) {
// Getting supply data (prefer fixed supply over unlimited supply although they should never appear at the same time anyway)
let fixed_supply = object_data.fixed_supply.as_ref();
let unlimited_supply = object_data.unlimited_supply.as_ref();
if let Some(supply) = unlimited_supply {
(current_supply, max_supply, total_minted_v2) = (
supply.current_supply.clone(),
None,
Some(supply.total_minted.clone()),
);
}
if let Some(supply) = fixed_supply {
(current_supply, max_supply, total_minted_v2) = (
supply.current_supply.clone(),
Some(supply.max_supply.clone()),
Some(supply.total_minted.clone()),
);
}

collection_properties = object_data
.property_map
.as_ref()
.map(|m| m.inner.clone())
.unwrap_or(collection_properties);
} else {
// ObjectCore should not be missing, returning from entire function early
return Ok(None);
// Aggregator V2 enables a separate struct for supply
let concurrent_supply = object_data.concurrent_supply.as_ref();
if let Some(supply) = concurrent_supply {
(current_supply, max_supply, total_minted_v2) = (
supply.current_supply.value.clone(),
if supply.current_supply.max_value == u64::MAX.into() {
None
} else {
Some(supply.current_supply.max_value.clone())
},
Some(supply.total_minted.value.clone()),
);
}

let collection_id = resource.address.clone();
let creator_address = inner.get_creator_address();
let collection_name = inner.get_name_trunc();
let description = inner.description.clone();
let uri = inner.get_uri_trunc();
// Getting collection mutability config from AptosCollection
let collection = object_data.aptos_collection.as_ref();
if let Some(collection) = collection {
mutable_description = Some(collection.mutable_description);
mutable_uri = Some(collection.mutable_uri);
}

Ok(Some((
Self {
transaction_version: txn_version,
write_set_change_index,
collection_id: collection_id.clone(),
creator_address: creator_address.clone(),
collection_name: collection_name.clone(),
description: description.clone(),
uri: uri.clone(),
current_supply: current_supply.clone(),
max_supply: max_supply.clone(),
total_minted_v2: total_minted_v2.clone(),
mutable_description,
mutable_uri,
table_handle_v1: None,
collection_properties: Some(collection_properties.clone()),
token_standard: TokenStandard::V2.to_string(),
transaction_timestamp: txn_timestamp,
},
CurrentCollectionV2 {
collection_id,
creator_address,
collection_name,
description,
uri,
current_supply,
max_supply,
total_minted_v2,
mutable_description,
mutable_uri,
table_handle_v1: None,
token_standard: TokenStandard::V2.to_string(),
collection_properties: Some(collection_properties),
last_transaction_version: txn_version,
last_transaction_timestamp: txn_timestamp,
},
)))
collection_properties = object_data
.property_map
.as_ref()
.map(|m| m.inner.clone())
.unwrap_or(collection_properties);
} else {
Ok(None)
// ObjectCore should not be missing, returning from entire function early
return Ok(None);
}

let collection_id = address;
let creator_address = inner.get_creator_address();
let collection_name = inner.get_name_trunc();
let description = inner.description.clone();
let uri = inner.get_uri_trunc();

Ok(Some((
Self {
transaction_version: txn_version,
write_set_change_index,
collection_id: collection_id.clone(),
creator_address: creator_address.clone(),
collection_name: collection_name.clone(),
description: description.clone(),
uri: uri.clone(),
current_supply: current_supply.clone(),
max_supply: max_supply.clone(),
total_minted_v2: total_minted_v2.clone(),
mutable_description,
mutable_uri,
table_handle_v1: None,
collection_properties: Some(collection_properties.clone()),
token_standard: TokenStandard::V2.to_string(),
transaction_timestamp: txn_timestamp,
},
CurrentCollectionV2 {
collection_id,
creator_address,
collection_name,
description,
uri,
current_supply,
max_supply,
total_minted_v2,
mutable_description,
mutable_uri,
table_handle_v1: None,
token_standard: TokenStandard::V2.to_string(),
collection_properties: Some(collection_properties),
last_transaction_version: txn_version,
last_transaction_timestamp: txn_timestamp,
},
)))
}

pub async fn get_v1_from_write_table_item(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl TokenDataV2 {
txn_timestamp: chrono::NaiveDateTime,
object_metadatas: &ObjectAggregatedDataMapping,
) -> anyhow::Result<Option<(Self, CurrentTokenDataV2)>> {
if let Some(inner) = &TokenV2::from_write_resource(write_resource, txn_version)? {
if let Some(inner) = &TokenV2::from_write_resource(write_resource)? {
let token_data_id = standardize_address(&write_resource.address.to_string());
let mut token_name = inner.get_name_trunc();
let is_fungible_v2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,7 @@ impl TokenOwnershipV2 {
.get(&standardize_address(&token_data_id))
.is_some()
{
if let Some(object) =
&ObjectWithMetadata::from_write_resource(write_resource, txn_version)?
{
if let Some(object) = &ObjectWithMetadata::from_write_resource(write_resource)? {
let object_core = &object.object_core;
let owner_address = object_core.get_owner_address();
let storage_id = token_data_id.clone();
Expand Down
Loading

0 comments on commit 3cb1385

Please sign in to comment.