Skip to content

Commit

Permalink
fix get transfer method, use get_transfer_by_tx_hash_log_index_token_id
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Sep 3, 2024
1 parent e76ba0c commit a667af9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
7 changes: 6 additions & 1 deletion mm2src/coins/nft/nft_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,12 @@ cross_test!(test_add_get_transfers, {
.clone();
assert_eq!(transfer1.block_number, 28056721);
let transfer2 = storage
.get_transfer_by_tx_hash_and_log_index(&chain, TX_HASH.to_string(), LOG_INDEX)
.get_transfer_by_tx_hash_log_index_token_id(
&chain,
TX_HASH.to_string(),
LOG_INDEX,
BigUint::from_str("214300047253").unwrap(),
)
.await
.unwrap()
.unwrap();
Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/nft/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ pub trait NftTransferHistoryStorageOps {
token_id: BigUint,
) -> MmResult<Vec<NftTransferHistory>, Self::Error>;

async fn get_transfer_by_tx_hash_and_log_index(
async fn get_transfer_by_tx_hash_log_index_token_id(
&self,
chain: &Chain,
transaction_hash: String,
log_index: u32,
token_id: BigUint,
) -> MmResult<Option<NftTransferHistory>, Self::Error>;

/// Updates the metadata for NFT transfers identified by their token address and ID.
Expand Down
7 changes: 4 additions & 3 deletions mm2src/coins/nft/storage/sql_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,22 +1121,23 @@ impl NftTransferHistoryStorageOps for AsyncMutexGuard<'_, AsyncConnection> {
.map_to_mm(AsyncConnError::from)
}

async fn get_transfer_by_tx_hash_and_log_index(
async fn get_transfer_by_tx_hash_log_index_token_id(
&self,
chain: &Chain,
transaction_hash: String,
log_index: u32,
token_id: BigUint,
) -> MmResult<Option<NftTransferHistory>, Self::Error> {
let table_name = chain.transfer_history_table_name()?;
let sql = format!(
"SELECT * FROM {} WHERE transaction_hash=?1 AND log_index = ?2",
"SELECT * FROM {} WHERE transaction_hash=?1 AND log_index = ?2 AND token_id = ?3",
table_name.inner()
);
self.call(move |conn| {
let transfer = query_single_row(
conn,
&sql,
[transaction_hash, log_index.to_string()],
[transaction_hash, log_index.to_string(), token_id.to_string()],
transfer_history_from_row,
)?;
Ok(transfer)
Expand Down
27 changes: 16 additions & 11 deletions mm2src/coins/nft/storage/wasm/wasm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,18 +547,20 @@ impl NftTransferHistoryStorageOps for NftCacheIDBLocked<'_> {
.collect()
}

async fn get_transfer_by_tx_hash_and_log_index(
async fn get_transfer_by_tx_hash_log_index_token_id(
&self,
chain: &Chain,
transaction_hash: String,
log_index: u32,
token_id: BigUint,
) -> MmResult<Option<NftTransferHistory>, Self::Error> {
let db_transaction = self.get_inner().transaction().await?;
let table = db_transaction.table::<NftTransferHistoryTable>().await?;
let index_keys = MultiIndex::new(NftTransferHistoryTable::CHAIN_TX_HASH_LOG_INDEX_INDEX)
let index_keys = MultiIndex::new(NftTransferHistoryTable::CHAIN_TX_HASH_LOG_INDEX_TOKEN_ID_INDEX)
.with_value(chain.to_string())?
.with_value(&transaction_hash)?
.with_value(log_index)?;
.with_value(log_index)?
.with_value(BeBigUint::from(token_id))?;

if let Some((_item_id, item)) = table.get_item_by_unique_multi_index(index_keys).await? {
Ok(Some(transfer_details_from_item(item)?))
Expand Down Expand Up @@ -602,10 +604,11 @@ impl NftTransferHistoryStorageOps for NftCacheIDBLocked<'_> {
}
drop_mutability!(transfer);

let index_keys = MultiIndex::new(NftTransferHistoryTable::CHAIN_TX_HASH_LOG_INDEX_INDEX)
let index_keys = MultiIndex::new(NftTransferHistoryTable::CHAIN_TX_HASH_LOG_INDEX_TOKEN_ID_INDEX)
.with_value(&chain_str)?
.with_value(&transfer.common.transaction_hash)?
.with_value(transfer.common.log_index)?;
.with_value(transfer.common.log_index)?
.with_value(transfer.token_id.clone())?;

let item = NftTransferHistoryTable::from_transfer_history(&transfer)?;
table.replace_item_by_unique_multi_index(index_keys, &item).await?;
Expand Down Expand Up @@ -691,10 +694,11 @@ impl NftTransferHistoryStorageOps for NftCacheIDBLocked<'_> {
transfer.common.possible_spam = possible_spam;
drop_mutability!(transfer);

let index_keys = MultiIndex::new(NftTransferHistoryTable::CHAIN_TX_HASH_LOG_INDEX_INDEX)
let index_keys = MultiIndex::new(NftTransferHistoryTable::CHAIN_TX_HASH_LOG_INDEX_TOKEN_ID_INDEX)
.with_value(&chain_str)?
.with_value(&transfer.common.transaction_hash)?
.with_value(transfer.common.log_index)?;
.with_value(transfer.common.log_index)?
.with_value(transfer.token_id.clone())?;

let item = NftTransferHistoryTable::from_transfer_history(&transfer)?;
table.replace_item_by_unique_multi_index(index_keys, &item).await?;
Expand Down Expand Up @@ -777,10 +781,11 @@ async fn update_transfer_phishing_for_index(
transfer.possible_phishing = possible_phishing;
drop_mutability!(transfer);
let transfer_item = NftTransferHistoryTable::from_transfer_history(&transfer)?;
let index_keys = MultiIndex::new(NftTransferHistoryTable::CHAIN_TX_HASH_LOG_INDEX_INDEX)
let index_keys = MultiIndex::new(NftTransferHistoryTable::CHAIN_TX_HASH_LOG_INDEX_TOKEN_ID_INDEX)
.with_value(chain)?
.with_value(&transfer.common.transaction_hash)?
.with_value(transfer.common.log_index)?;
.with_value(transfer.common.log_index)?
.with_value(transfer.token_id)?;
table
.replace_item_by_unique_multi_index(index_keys, &transfer_item)
.await?;
Expand Down Expand Up @@ -951,7 +956,7 @@ pub(crate) struct NftTransferHistoryTable {
}

impl NftTransferHistoryTable {
const CHAIN_TX_HASH_LOG_INDEX_INDEX: &str = "chain_tx_hash_log_index_index";
const CHAIN_TX_HASH_LOG_INDEX_TOKEN_ID_INDEX: &str = "chain_tx_hash_log_index_token_idindex";

fn from_transfer_history(transfer: &NftTransferHistory) -> WasmNftCacheResult<NftTransferHistoryTable> {
let details_json =
Expand Down Expand Up @@ -992,7 +997,7 @@ impl TableSignature for NftTransferHistoryTable {
false,
)?;
table.create_multi_index(
Self::CHAIN_TX_HASH_LOG_INDEX_INDEX,
Self::CHAIN_TX_HASH_LOG_INDEX_TOKEN_ID_INDEX,
&["chain", "transaction_hash", "log_index", "token_id"],
true,
)?;
Expand Down

0 comments on commit a667af9

Please sign in to comment.