|
| 1 | +// Copyright © Aptos Foundation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// This is required because a diesel macro makes clippy sad |
| 5 | +#![allow(clippy::extra_unused_lifetimes)] |
| 6 | +#![allow(clippy::unused_unit)] |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + bq_analytics::generic_parquet_processor::{GetTimeStamp, HasVersion, NamedTable}, |
| 10 | + db::common::models::{ |
| 11 | + ans_models::{ |
| 12 | + ans_lookup::{AnsPrimaryName, CurrentAnsPrimaryName}, |
| 13 | + ans_utils::SetReverseLookupEvent, |
| 14 | + }, |
| 15 | + token_v2_models::v2_token_utils::TokenStandard, |
| 16 | + }, |
| 17 | +}; |
| 18 | +use allocative_derive::Allocative; |
| 19 | +use aptos_protos::transaction::v1::Event; |
| 20 | +use field_count::FieldCount; |
| 21 | +use parquet_derive::ParquetRecordWriter; |
| 22 | +use serde::{Deserialize, Serialize}; |
| 23 | + |
| 24 | +#[derive( |
| 25 | + Allocative, Clone, Default, Debug, Deserialize, FieldCount, ParquetRecordWriter, Serialize, |
| 26 | +)] |
| 27 | +pub struct AnsPrimaryNameV2 { |
| 28 | + pub txn_version: i64, |
| 29 | + pub write_set_change_index: i64, |
| 30 | + pub registered_address: String, |
| 31 | + pub token_standard: String, |
| 32 | + pub domain: Option<String>, |
| 33 | + pub subdomain: Option<String>, |
| 34 | + pub token_name: Option<String>, |
| 35 | + pub is_deleted: bool, |
| 36 | + #[allocative(skip)] |
| 37 | + pub block_timestamp: chrono::NaiveDateTime, |
| 38 | +} |
| 39 | + |
| 40 | +impl NamedTable for AnsPrimaryNameV2 { |
| 41 | + const TABLE_NAME: &'static str = "ans_primary_name_v2"; |
| 42 | +} |
| 43 | + |
| 44 | +impl HasVersion for AnsPrimaryNameV2 { |
| 45 | + fn version(&self) -> i64 { |
| 46 | + self.txn_version |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl GetTimeStamp for AnsPrimaryNameV2 { |
| 51 | + fn get_timestamp(&self) -> chrono::NaiveDateTime { |
| 52 | + self.block_timestamp |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Clone, Default, Debug, Deserialize, Serialize, PartialEq, Eq)] |
| 57 | +pub struct CurrentAnsPrimaryNameV2 { |
| 58 | + pub registered_address: String, |
| 59 | + pub token_standard: String, |
| 60 | + pub domain: Option<String>, |
| 61 | + pub subdomain: Option<String>, |
| 62 | + pub token_name: Option<String>, |
| 63 | + pub is_deleted: bool, |
| 64 | + pub last_transaction_version: i64, |
| 65 | +} |
| 66 | + |
| 67 | +impl Ord for CurrentAnsPrimaryNameV2 { |
| 68 | + fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
| 69 | + self.registered_address.cmp(&other.registered_address) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl PartialOrd for CurrentAnsPrimaryNameV2 { |
| 74 | + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 75 | + Some(self.cmp(other)) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl CurrentAnsPrimaryNameV2 { |
| 80 | + pub fn get_v2_from_v1( |
| 81 | + v1_current_primary_name: CurrentAnsPrimaryName, |
| 82 | + v1_primary_name: AnsPrimaryName, |
| 83 | + block_timestamp: chrono::NaiveDateTime, |
| 84 | + ) -> (Self, AnsPrimaryNameV2) { |
| 85 | + ( |
| 86 | + Self { |
| 87 | + registered_address: v1_current_primary_name.registered_address, |
| 88 | + token_standard: TokenStandard::V1.to_string(), |
| 89 | + domain: v1_current_primary_name.domain, |
| 90 | + subdomain: v1_current_primary_name.subdomain, |
| 91 | + token_name: v1_current_primary_name.token_name, |
| 92 | + is_deleted: v1_current_primary_name.is_deleted, |
| 93 | + last_transaction_version: v1_current_primary_name.last_transaction_version, |
| 94 | + }, |
| 95 | + AnsPrimaryNameV2 { |
| 96 | + txn_version: v1_primary_name.transaction_version, |
| 97 | + write_set_change_index: v1_primary_name.write_set_change_index, |
| 98 | + registered_address: v1_primary_name.registered_address, |
| 99 | + token_standard: TokenStandard::V1.to_string(), |
| 100 | + domain: v1_primary_name.domain, |
| 101 | + subdomain: v1_primary_name.subdomain, |
| 102 | + token_name: v1_primary_name.token_name, |
| 103 | + is_deleted: v1_primary_name.is_deleted, |
| 104 | + block_timestamp, |
| 105 | + }, |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + // Parse v2 primary name record from SetReverseLookupEvent |
| 110 | + pub fn parse_v2_primary_name_record_from_event( |
| 111 | + event: &Event, |
| 112 | + txn_version: i64, |
| 113 | + event_index: i64, |
| 114 | + ans_v2_contract_address: &str, |
| 115 | + block_timestamp: chrono::NaiveDateTime, |
| 116 | + ) -> anyhow::Result<Option<(Self, AnsPrimaryNameV2)>> { |
| 117 | + if let Some(set_reverse_lookup_event) = |
| 118 | + SetReverseLookupEvent::from_event(event, ans_v2_contract_address, txn_version).unwrap() |
| 119 | + { |
| 120 | + if set_reverse_lookup_event.get_curr_domain_trunc().is_empty() { |
| 121 | + // Handle case where the address's primary name is unset |
| 122 | + return Ok(Some(( |
| 123 | + Self { |
| 124 | + registered_address: set_reverse_lookup_event.get_account_addr().clone(), |
| 125 | + token_standard: TokenStandard::V2.to_string(), |
| 126 | + domain: None, |
| 127 | + subdomain: None, |
| 128 | + token_name: None, |
| 129 | + last_transaction_version: txn_version, |
| 130 | + is_deleted: true, |
| 131 | + }, |
| 132 | + AnsPrimaryNameV2 { |
| 133 | + txn_version, |
| 134 | + write_set_change_index: -(event_index + 1), |
| 135 | + registered_address: set_reverse_lookup_event.get_account_addr().clone(), |
| 136 | + token_standard: TokenStandard::V2.to_string(), |
| 137 | + domain: None, |
| 138 | + subdomain: None, |
| 139 | + token_name: None, |
| 140 | + is_deleted: true, |
| 141 | + block_timestamp, |
| 142 | + }, |
| 143 | + ))); |
| 144 | + } else { |
| 145 | + // Handle case where the address is set to a new primary name |
| 146 | + return Ok(Some(( |
| 147 | + Self { |
| 148 | + registered_address: set_reverse_lookup_event.get_account_addr().clone(), |
| 149 | + token_standard: TokenStandard::V2.to_string(), |
| 150 | + domain: Some(set_reverse_lookup_event.get_curr_domain_trunc()), |
| 151 | + subdomain: Some(set_reverse_lookup_event.get_curr_subdomain_trunc()), |
| 152 | + token_name: Some(set_reverse_lookup_event.get_curr_token_name()), |
| 153 | + last_transaction_version: txn_version, |
| 154 | + is_deleted: false, |
| 155 | + }, |
| 156 | + AnsPrimaryNameV2 { |
| 157 | + txn_version, |
| 158 | + write_set_change_index: -(event_index + 1), |
| 159 | + registered_address: set_reverse_lookup_event.get_account_addr().clone(), |
| 160 | + token_standard: TokenStandard::V2.to_string(), |
| 161 | + domain: Some(set_reverse_lookup_event.get_curr_domain_trunc()), |
| 162 | + subdomain: Some(set_reverse_lookup_event.get_curr_subdomain_trunc()), |
| 163 | + token_name: Some(set_reverse_lookup_event.get_curr_token_name()), |
| 164 | + is_deleted: false, |
| 165 | + block_timestamp, |
| 166 | + }, |
| 167 | + ))); |
| 168 | + } |
| 169 | + } |
| 170 | + Ok(None) |
| 171 | + } |
| 172 | +} |
0 commit comments