-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[parquet-sdk][token_v2] migrate CurrentTokenV2Metadata (#633)
- Loading branch information
Showing
11 changed files
with
242 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod raw_token_claims; | ||
pub mod raw_v1_token_royalty; | ||
pub mod raw_v2_token_metadata; |
96 changes: 96 additions & 0 deletions
96
rust/processor/src/db/common/models/token_v2_models/raw_v2_token_metadata.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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::{ | ||
db::postgres::models::{ | ||
default_models::move_resources::MoveResource, | ||
object_models::v2_object_utils::ObjectAggregatedDataMapping, | ||
resources::{COIN_ADDR, TOKEN_ADDR, TOKEN_V2_ADDR}, | ||
token_models::token_utils::NAME_LENGTH, | ||
}, | ||
utils::util::{standardize_address, truncate_str}, | ||
}; | ||
use anyhow::Context; | ||
use aptos_protos::transaction::v1::WriteResource; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
|
||
// PK of current_objects, i.e. object_address, resource_type | ||
pub type CurrentTokenV2MetadataPK = (String, String); | ||
|
||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] | ||
pub struct RawCurrentTokenV2Metadata { | ||
pub object_address: String, | ||
pub resource_type: String, | ||
pub data: Value, | ||
pub state_key_hash: String, | ||
pub last_transaction_version: i64, | ||
pub last_transaction_timestamp: chrono::NaiveDateTime, | ||
} | ||
|
||
impl Ord for RawCurrentTokenV2Metadata { | ||
fn cmp(&self, other: &Self) -> std::cmp::Ordering { | ||
self.object_address | ||
.cmp(&other.object_address) | ||
.then(self.resource_type.cmp(&other.resource_type)) | ||
} | ||
} | ||
impl PartialOrd for RawCurrentTokenV2Metadata { | ||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl RawCurrentTokenV2Metadata { | ||
/// Parsing unknown resources with 0x4::token::Token | ||
pub fn from_write_resource( | ||
write_resource: &WriteResource, | ||
txn_version: i64, | ||
object_metadatas: &ObjectAggregatedDataMapping, | ||
txn_timestamp: chrono::NaiveDateTime, | ||
) -> anyhow::Result<Option<Self>> { | ||
let object_address = standardize_address(&write_resource.address.to_string()); | ||
if let Some(object_data) = object_metadatas.get(&object_address) { | ||
// checking if token_v2 | ||
if object_data.token.is_some() { | ||
let move_tag = | ||
MoveResource::convert_move_struct_tag(write_resource.r#type.as_ref().unwrap()); | ||
let resource_type_addr = move_tag.get_address(); | ||
if matches!( | ||
resource_type_addr.as_str(), | ||
COIN_ADDR | TOKEN_ADDR | TOKEN_V2_ADDR | ||
) { | ||
return Ok(None); | ||
} | ||
|
||
let resource = MoveResource::from_write_resource(write_resource, 0, txn_version, 0); | ||
|
||
let state_key_hash = object_data.object.get_state_key_hash(); | ||
if state_key_hash != resource.state_key_hash { | ||
return Ok(None); | ||
} | ||
|
||
let resource_type = truncate_str(&resource.type_, NAME_LENGTH); | ||
return Ok(Some(RawCurrentTokenV2Metadata { | ||
object_address, | ||
resource_type, | ||
data: resource | ||
.data | ||
.context("data must be present in write resource")?, | ||
state_key_hash: resource.state_key_hash, | ||
last_transaction_version: txn_version, | ||
last_transaction_timestamp: txn_timestamp, | ||
})); | ||
} | ||
} | ||
Ok(None) | ||
} | ||
} | ||
|
||
pub trait CurrentTokenV2MetadataConvertible { | ||
fn from_raw(raw_item: RawCurrentTokenV2Metadata) -> Self; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod token_claims; | ||
pub mod v1_token_royalty; | ||
pub mod v2_token_metadata; |
59 changes: 59 additions & 0 deletions
59
rust/processor/src/db/parquet/models/token_v2_models/v2_token_metadata.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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::{ | ||
bq_analytics::generic_parquet_processor::{GetTimeStamp, HasVersion, NamedTable}, | ||
db::common::models::token_v2_models::raw_v2_token_metadata::{ | ||
CurrentTokenV2MetadataConvertible, RawCurrentTokenV2Metadata, | ||
}, | ||
}; | ||
use allocative_derive::Allocative; | ||
use field_count::FieldCount; | ||
use parquet_derive::ParquetRecordWriter; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive( | ||
Allocative, Clone, Debug, Default, Deserialize, FieldCount, ParquetRecordWriter, Serialize, | ||
)] | ||
pub struct CurrentTokenV2Metadata { | ||
pub object_address: String, | ||
pub resource_type: String, | ||
pub data: String, | ||
pub state_key_hash: String, | ||
pub last_transaction_version: i64, | ||
#[allocative(skip)] | ||
pub last_transaction_timestamp: chrono::NaiveDateTime, | ||
} | ||
impl NamedTable for CurrentTokenV2Metadata { | ||
const TABLE_NAME: &'static str = "current_token_v2_metadata"; | ||
} | ||
|
||
impl HasVersion for CurrentTokenV2Metadata { | ||
fn version(&self) -> i64 { | ||
self.last_transaction_version | ||
} | ||
} | ||
|
||
impl GetTimeStamp for CurrentTokenV2Metadata { | ||
fn get_timestamp(&self) -> chrono::NaiveDateTime { | ||
self.last_transaction_timestamp | ||
} | ||
} | ||
|
||
impl CurrentTokenV2MetadataConvertible for CurrentTokenV2Metadata { | ||
// TODO: consider returning a Result | ||
fn from_raw(raw_item: RawCurrentTokenV2Metadata) -> Self { | ||
Self { | ||
object_address: raw_item.object_address, | ||
resource_type: raw_item.resource_type, | ||
data: canonical_json::to_string(&raw_item.data).unwrap(), // TODO: handle better | ||
state_key_hash: raw_item.state_key_hash, | ||
last_transaction_version: raw_item.last_transaction_version, | ||
last_transaction_timestamp: raw_item.last_transaction_timestamp, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.