Skip to content

Commit

Permalink
fix storage_id
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenyang007 committed Jan 22, 2025
1 parent 1ca4c42 commit 2500429
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#![allow(clippy::extra_unused_lifetimes)]
#![allow(clippy::unused_unit)]

use super::raw_v2_fungible_asset_balances::{
get_paired_metadata_address, get_primary_fungible_store_address,
};
use crate::{
db::{
common::models::{
Expand All @@ -14,7 +17,7 @@ use crate::{
postgres::models::{
coin_models::{
coin_activities::CoinActivity,
coin_utils::{CoinEvent, CoinInfoType, EventGuidResource},
coin_utils::{CoinEvent, EventGuidResource},
},
fungible_asset_models::v2_fungible_asset_utils::{FeeStatement, FungibleAssetEvent},
},
Expand Down Expand Up @@ -194,9 +197,10 @@ impl RawFungibleAssetActivity {
}
};

let storage_id =
CoinInfoType::get_storage_id(coin_type.as_str(), owner_address.as_str());

// Storage id should be derived (for the FA migration)
let metadata_addr = get_paired_metadata_address(&coin_type);
let storage_id = get_primary_fungible_store_address(&owner_address, &metadata_addr)
.expect("calculate primary fungible store failed");
Ok(Some(Self {
transaction_version: txn_version,
event_index,
Expand Down Expand Up @@ -240,10 +244,11 @@ impl RawFungibleAssetActivity {
block_height,
fee_statement,
);
let storage_id = CoinInfoType::get_storage_id(
v1_activity.coin_type.as_str(),
v1_activity.owner_address.as_str(),
);
// Storage id should be derived (for the FA migration)
let metadata_addr = get_paired_metadata_address(&v1_activity.coin_type);
let storage_id =
get_primary_fungible_store_address(&v1_activity.owner_address, &metadata_addr)
.expect("calculate primary fungible store failed");
Self {
transaction_version,
event_index: v1_activity.event_index.unwrap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub trait CurrentUnifiedFungibleAssetBalanceConvertible {
fn from_raw(raw_item: RawCurrentUnifiedFungibleAssetBalance) -> Self;
}

fn get_paired_metadata_address(coin_type_name: &str) -> String {
pub fn get_paired_metadata_address(coin_type_name: &str) -> String {
if coin_type_name == APTOS_COIN_TYPE_STR {
APT_METADATA_ADDRESS_HEX.clone()
} else {
Expand All @@ -203,42 +203,60 @@ pub fn get_primary_fungible_store_address(

impl From<&RawCurrentFungibleAssetBalance> for RawCurrentUnifiedFungibleAssetBalance {
fn from(cfab: &RawCurrentFungibleAssetBalance) -> Self {
if cfab.token_standard.as_str() == V2_STANDARD.borrow().as_str() {
let asset_type_v2 = cfab.asset_type.clone();
Self {
storage_id: cfab.storage_id.clone(),
owner_address: cfab.owner_address.clone(),
asset_type_v2: Some(asset_type_v2.clone()),
asset_type_v1: METADATA_TO_COIN_TYPE_MAPPING
.get(asset_type_v2.as_str())
.map(|s| s.to_string()),
is_primary: cfab.is_primary,
is_frozen: cfab.is_frozen,
amount_v1: None,
amount_v2: Some(cfab.amount.clone()),
last_transaction_version_v1: None,
last_transaction_version_v2: Some(cfab.last_transaction_version),
last_transaction_timestamp_v1: None,
last_transaction_timestamp_v2: Some(cfab.last_transaction_timestamp),
}
// Determine if this is a V2 token standard
let is_v2 = cfab.token_standard.as_str() == V2_STANDARD.borrow().as_str();

// For V2 tokens, asset_type_v2 is the original asset type
// For V1 tokens, asset_type_v2 is None
let asset_type_v2 = is_v2.then(|| cfab.asset_type.clone());

// For V2 tokens, look up V1 equivalent in mapping
// For V1 tokens, use original asset type
let asset_type_v1 = if is_v2 {
METADATA_TO_COIN_TYPE_MAPPING
.get(cfab.asset_type.as_str())
.map(|s| s.to_string())
} else {
let metadata_addr = get_paired_metadata_address(&cfab.asset_type);
let pfs_addr = get_primary_fungible_store_address(&cfab.owner_address, &metadata_addr)
.expect("calculate pfs_address failed");
Self {
storage_id: pfs_addr,
owner_address: cfab.owner_address.clone(),
asset_type_v2: None,
asset_type_v1: Some(cfab.asset_type.clone()),
is_primary: true,
is_frozen: cfab.is_frozen,
amount_v1: Some(cfab.amount.clone()),
amount_v2: None,
last_transaction_version_v1: Some(cfab.last_transaction_version),
last_transaction_version_v2: None,
last_transaction_timestamp_v1: Some(cfab.last_transaction_timestamp),
last_transaction_timestamp_v2: None,
}
Some(cfab.asset_type.clone())
};

// V1 tokens are always primary, V2 tokens use the stored value
let is_primary = if is_v2 { cfab.is_primary } else { true };

// Amount and transaction details are stored in v1 or v2 fields based on token standard
let (amount_v1, amount_v2, version_v1, version_v2, timestamp_v1, timestamp_v2) = if is_v2 {
(
None,
Some(cfab.amount.clone()),
None,
Some(cfab.last_transaction_version),
None,
Some(cfab.last_transaction_timestamp),
)
} else {
(
Some(cfab.amount.clone()),
None,
Some(cfab.last_transaction_version),
None,
Some(cfab.last_transaction_timestamp),
None,
)
};

Self {
storage_id: cfab.storage_id.clone(),
owner_address: cfab.owner_address.clone(),
asset_type_v1,
asset_type_v2,
is_primary,
is_frozen: cfab.is_frozen,
amount_v1,
amount_v2,
last_transaction_version_v1: version_v1,
last_transaction_version_v2: version_v2,
last_transaction_timestamp_v1: timestamp_v1,
last_transaction_timestamp_v2: timestamp_v2,
}
}
}
Expand Down Expand Up @@ -318,8 +336,10 @@ impl RawFungibleAssetBalance {
);
if let Some(coin_type) = coin_info_type.get_coin_type_below_max() {
let owner_address = standardize_address(delete_resource.address.as_str());
let storage_id =
CoinInfoType::get_storage_id(coin_type.as_str(), owner_address.as_str());
// Storage id should be derived (for the FA migration)
let metadata_addr = get_paired_metadata_address(&coin_type);
let storage_id = get_primary_fungible_store_address(&owner_address, &metadata_addr)
.expect("calculate primary fungible store failed");

Check warning on line 342 in rust/processor/src/db/common/models/fungible_asset_models/raw_v2_fungible_asset_balances.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/common/models/fungible_asset_models/raw_v2_fungible_asset_balances.rs#L339-L342

Added lines #L339 - L342 were not covered by tests
let coin_balance = Self {
transaction_version: txn_version,
write_set_change_index,
Expand Down Expand Up @@ -372,8 +392,10 @@ impl RawFungibleAssetBalance {
);
if let Some(coin_type) = coin_info_type.get_coin_type_below_max() {
let owner_address = standardize_address(write_resource.address.as_str());
let storage_id =
CoinInfoType::get_storage_id(coin_type.as_str(), owner_address.as_str());
// Storage id should be derived (for the FA migration)
let metadata_addr = get_paired_metadata_address(&coin_type);
let storage_id = get_primary_fungible_store_address(&owner_address, &metadata_addr)
.expect("calculate primary fungible store failed");
let coin_balance = Self {
transaction_version: txn_version,
write_set_change_index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,6 @@ impl CoinInfoType {
hash_str(&self.coin_type.to_string())
}

/// This function gets the hash of the owner address and the coin type, similar to
/// how token v2 gets the named object address for the fungible asset store.
pub fn get_storage_id(coin_type: &str, owner_address: &str) -> String {
let key = format!("{}::{}", owner_address, coin_type);
format!("0x{}", hash_str(&key))
}

pub fn get_coin_type_trunc(&self) -> String {
truncate_str(&self.coin_type, COIN_TYPE_HASH_LENGTH)
}
Expand Down

0 comments on commit 2500429

Please sign in to comment.