Skip to content

Commit

Permalink
fix(nft): add log_index to history table and use in PK (#1926)
Browse files Browse the repository at this point in the history
- This commmit fixes transactions that transfer multiple NFT tokens in db. These transactions cause errors when adding due to the db constraint on tx hash uniqueness. To solve this, the PR uses log_index as part of the transfers history table primary key.
- nft_tx_history table is now called nft_transfer_history and tx/txs are renamed to transfer/transfers throughout the NFT code since what's added/retrieved from DB is NFT transfers not transactions (Multiple NFT transfers can be in one transaction). By renaming the table, there are no need for db migrations due to the addition of log_index column. Although NFT is not used in production yet, if anybody used it, transfers will be re-fetched and saved to the new DB table when using the related API methods.
  • Loading branch information
shamardy authored Aug 7, 2023
1 parent 54dce3c commit 92372cb
Show file tree
Hide file tree
Showing 8 changed files with 584 additions and 429 deletions.
231 changes: 124 additions & 107 deletions mm2src/coins/nft.rs

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions mm2src/coins/nft/nft_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub struct TransactionNftDetails {
#[derive(Debug, Deserialize)]
pub struct NftTransfersReq {
pub(crate) chains: Vec<Chain>,
pub(crate) filters: Option<NftTxHistoryFilters>,
pub(crate) filters: Option<NftTransferHistoryFilters>,
#[serde(default)]
pub(crate) max: bool,
#[serde(default = "ten")]
Expand Down Expand Up @@ -368,14 +368,14 @@ impl fmt::Display for TransferStatus {
}
}

/// [`NftTransferCommon`] structure contains common fields from [`NftTransferHistory`] and [`NftTxHistoryFromMoralis`]
/// [`NftTransferCommon`] structure contains common fields from [`NftTransferHistory`] and [`NftTransferHistoryFromMoralis`]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct NftTransferCommon {
pub(crate) block_hash: Option<String>,
/// Transaction hash in hexadecimal format
pub(crate) transaction_hash: String,
pub(crate) transaction_index: Option<u64>,
pub(crate) log_index: Option<u64>,
pub(crate) log_index: u32,
pub(crate) value: Option<BigDecimal>,
pub(crate) transaction_type: Option<String>,
pub(crate) token_address: Address,
Expand Down Expand Up @@ -404,9 +404,9 @@ pub struct NftTransferHistory {
pub(crate) status: TransferStatus,
}

/// This structure is for deserializing moralis NFT transaction json to struct.
/// This structure is for deserializing moralis NFT transfer json to struct.
#[derive(Debug, Deserialize)]
pub(crate) struct NftTxHistoryFromMoralis {
pub(crate) struct NftTransferHistoryFromMoralis {
#[serde(flatten)]
pub(crate) common: NftTransferCommon,
pub(crate) block_number: SerdeStringWrap<u64>,
Expand All @@ -422,7 +422,7 @@ pub struct NftsTransferHistoryList {
}

#[derive(Copy, Clone, Debug, Deserialize)]
pub struct NftTxHistoryFilters {
pub struct NftTransferHistoryFilters {
#[serde(default)]
pub receive: bool,
#[serde(default)]
Expand All @@ -444,7 +444,7 @@ pub struct NftTokenAddrId {
}

#[derive(Debug)]
pub struct TxMeta {
pub struct TransferMeta {
pub(crate) token_address: String,
pub(crate) token_id: BigDecimal,
pub(crate) token_uri: Option<String>,
Expand All @@ -453,9 +453,9 @@ pub struct TxMeta {
pub(crate) token_name: Option<String>,
}

impl From<Nft> for TxMeta {
impl From<Nft> for TransferMeta {
fn from(nft_db: Nft) -> Self {
TxMeta {
TransferMeta {
token_address: eth_addr_to_hex(&nft_db.common.token_address),
token_id: nft_db.common.token_id,
token_uri: nft_db.common.token_uri,
Expand Down
42 changes: 22 additions & 20 deletions mm2src/coins/nft/nft_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const TEST_WALLET_ADDR_EVM: &str = "0x394d86994f954ed931b86791b62fe64f4c5dac37";
#[cfg(all(test, not(target_arch = "wasm32")))]
mod native_tests {
use crate::eth::eth_addr_to_hex;
use crate::nft::nft_structs::{NftFromMoralis, NftTxHistoryFromMoralis, UriMeta};
use crate::nft::nft_structs::{NftFromMoralis, NftTransferHistoryFromMoralis, UriMeta};
use crate::nft::nft_tests::{NFT_HISTORY_URL_TEST, NFT_LIST_URL_TEST, NFT_METADATA_URL_TEST, TEST_WALLET_ADDR_EVM};
use crate::nft::storage::db_test_helpers::*;
use crate::nft::{check_and_redact_if_spam, check_moralis_ipfs_bafy, check_nft_metadata_for_spam,
Expand Down Expand Up @@ -70,11 +70,12 @@ mod native_tests {
assert_eq!(TEST_WALLET_ADDR_EVM, eth_addr_to_hex(&nft_moralis.common.owner_of));
}

let response_tx_history = block_on(send_request_to_uri(NFT_HISTORY_URL_TEST)).unwrap();
let mut transfer_list = response_tx_history["result"].as_array().unwrap().clone();
let response_transfer_history = block_on(send_request_to_uri(NFT_HISTORY_URL_TEST)).unwrap();
let mut transfer_list = response_transfer_history["result"].as_array().unwrap().clone();
assert!(!transfer_list.is_empty());
let first_tx = transfer_list.remove(transfer_list.len() - 1);
let transfer_moralis: NftTxHistoryFromMoralis = serde_json::from_str(&first_tx.to_string()).unwrap();
let first_transfer = transfer_list.remove(transfer_list.len() - 1);
let transfer_moralis: NftTransferHistoryFromMoralis =
serde_json::from_str(&first_transfer.to_string()).unwrap();
assert_eq!(
TEST_WALLET_ADDR_EVM,
eth_addr_to_hex(&transfer_moralis.common.to_address)
Expand Down Expand Up @@ -107,25 +108,25 @@ mod native_tests {
fn test_nft_amount() { block_on(test_nft_amount_impl()) }

#[test]
fn test_add_get_txs() { block_on(test_add_get_txs_impl()) }
fn test_add_get_transfers() { block_on(test_add_get_transfers_impl()) }

#[test]
fn test_last_tx_block() { block_on(test_last_tx_block_impl()) }
fn test_last_transfer_block() { block_on(test_last_transfer_block_impl()) }

#[test]
fn test_tx_history() { block_on(test_tx_history_impl()) }
fn test_transfer_history() { block_on(test_transfer_history_impl()) }

#[test]
fn test_tx_history_filters() { block_on(test_tx_history_filters_impl()) }
fn test_transfer_history_filters() { block_on(test_transfer_history_filters_impl()) }

#[test]
fn test_get_update_tx_meta() { block_on(test_get_update_tx_meta_impl()) }
fn test_get_update_transfer_meta() { block_on(test_get_update_transfer_meta_impl()) }
}

#[cfg(target_arch = "wasm32")]
mod wasm_tests {
use crate::eth::eth_addr_to_hex;
use crate::nft::nft_structs::{NftFromMoralis, NftTxHistoryFromMoralis};
use crate::nft::nft_structs::{NftFromMoralis, NftTransferHistoryFromMoralis};
use crate::nft::nft_tests::{NFT_HISTORY_URL_TEST, NFT_LIST_URL_TEST, NFT_METADATA_URL_TEST, TEST_WALLET_ADDR_EVM};
use crate::nft::send_request_to_uri;
use crate::nft::storage::db_test_helpers::*;
Expand All @@ -142,11 +143,12 @@ mod wasm_tests {
assert_eq!(TEST_WALLET_ADDR_EVM, eth_addr_to_hex(&nft_moralis.common.owner_of));
}

let response_tx_history = send_request_to_uri(NFT_HISTORY_URL_TEST).await.unwrap();
let mut transfer_list = response_tx_history["result"].as_array().unwrap().clone();
let response_transfer_history = send_request_to_uri(NFT_HISTORY_URL_TEST).await.unwrap();
let mut transfer_list = response_transfer_history["result"].as_array().unwrap().clone();
assert!(!transfer_list.is_empty());
let first_tx = transfer_list.remove(transfer_list.len() - 1);
let transfer_moralis: NftTxHistoryFromMoralis = serde_json::from_str(&first_tx.to_string()).unwrap();
let first_transfer = transfer_list.remove(transfer_list.len() - 1);
let transfer_moralis: NftTransferHistoryFromMoralis =
serde_json::from_str(&first_transfer.to_string()).unwrap();
assert_eq!(
TEST_WALLET_ADDR_EVM,
eth_addr_to_hex(&transfer_moralis.common.to_address)
Expand Down Expand Up @@ -176,17 +178,17 @@ mod wasm_tests {
async fn test_refresh_metadata() { test_refresh_metadata_impl().await }

#[wasm_bindgen_test]
async fn test_add_get_txs() { test_add_get_txs_impl().await }
async fn test_add_get_transfers() { test_add_get_transfers_impl().await }

#[wasm_bindgen_test]
async fn test_last_tx_block() { test_last_tx_block_impl().await }
async fn test_last_transfer_block() { test_last_transfer_block_impl().await }

#[wasm_bindgen_test]
async fn test_tx_history() { test_tx_history_impl().await }
async fn test_transfer_history() { test_transfer_history_impl().await }

#[wasm_bindgen_test]
async fn test_tx_history_filters() { test_tx_history_filters_impl().await }
async fn test_transfer_history_filters() { test_transfer_history_filters_impl().await }

#[wasm_bindgen_test]
async fn test_get_update_tx_meta() { test_get_update_tx_meta_impl().await }
async fn test_get_update_transfer_meta() { test_get_update_transfer_meta_impl().await }
}
Loading

0 comments on commit 92372cb

Please sign in to comment.