Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nft): add token_id field to the tx history primary key, fix balance #2209

Merged
merged 23 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
51eb543
update multi_index in wasm NftTransferHistoryTable and PRIMARY KEY in…
laruh Sep 3, 2024
2bdee4f
fix erc1155_balance, check that "balanceOf" ERC11155 returns the exac…
laruh Sep 3, 2024
416b23c
Merge remote-tracking branch 'origin/dev' into fix-nft-tx-history-pri…
laruh Sep 12, 2024
a493f3f
require amount: Option<BigUint> in WithdrawErc1155
laruh Sep 12, 2024
48a7f5b
cover sql nft tx history migration
laruh Sep 12, 2024
1da9cb8
Merge remote-tracking branch 'origin/dev' into fix-nft-tx-history-pri…
laruh Sep 13, 2024
5cae2d4
make migration function name clearer
laruh Sep 13, 2024
9cebf62
wasm migration wip
laruh Sep 15, 2024
df164d4
copy_store_data_sync for NftTransferHistoryTable migration
laruh Sep 15, 2024
041f99c
change version check and add logs
laruh Sep 15, 2024
e8be556
use "else if old_version == 1 && new_version == 2" check
laruh Sep 16, 2024
c42e941
Merge remote-tracking branch 'origin/dev' into fix-nft-tx-history-pri…
laruh Sep 30, 2024
12bdb27
WIP: update on_upgrade_needed for wasm nft tables
laruh Oct 10, 2024
7511d94
Merge remote-tracking branch 'origin/dev' into fix-nft-tx-history-pri…
laruh Oct 11, 2024
14bef0d
update old/new versions handle, remove unused code
laruh Oct 11, 2024
a3ba820
avoid duplication when we crate tx history sql str, migrate_tx_histor…
laruh Oct 13, 2024
5c14c09
update comment in migrate_tx_history_table_to_schema_v2
laruh Oct 13, 2024
38ac974
cover schema_table deletion in RPC clear_nft_db
laruh Oct 13, 2024
43d7ba2
fix sql migration bug
laruh Oct 14, 2024
a5486e1
Merge remote-tracking branch 'origin/dev' into fix-nft-tx-history-pri…
laruh Oct 26, 2024
ef3dd1e
review: replace BigDecimal by BigUint in NFT withdraw
laruh Oct 26, 2024
7636464
review: use progressive upgrade pattern for IDB and sql schemas
laruh Oct 27, 2024
f44bc38
review: remove break to allow straightforward version-by-version upg…
laruh Nov 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ mod eip1559_gas_fee;
pub(crate) use eip1559_gas_fee::FeePerGasEstimated;
use eip1559_gas_fee::{BlocknativeGasApiCaller, FeePerGasSimpleEstimator, GasApiConfig, GasApiProvider,
InfuraGasApiCaller};
use mm2_number::num_bigint::ToBigInt;

pub(crate) mod eth_swap_v2;

/// https://github.com/artemii235/etomic-swap/blob/master/contracts/EtomicSwap.sol
Expand Down Expand Up @@ -917,7 +919,11 @@ pub async fn withdraw_erc1155(ctx: MmArc, withdraw_type: WithdrawErc1155) -> Wit
let amount_dec = if withdraw_type.max {
wallet_amount.clone()
} else {
withdraw_type.amount.unwrap_or_else(|| 1.into())
let amount = withdraw_type.amount.unwrap_or_else(|| BigUint::from(1u32));
let bigint = amount
.to_bigint()
.ok_or_else(|| WithdrawError::InternalError("Failed to convert BigUint to BigInt".to_string()))?;
BigDecimal::from(bigint)
shamardy marked this conversation as resolved.
Show resolved Hide resolved
};

if amount_dec > wallet_amount {
Expand Down
7 changes: 7 additions & 0 deletions mm2src/coins/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::nft::nft_errors::{ClearNftDbError, MetaFromUrlError, ProtectFromSpamE
use crate::nft::nft_structs::{build_nft_with_empty_meta, BuildNftFields, ClearNftDbReq, NftCommon, NftCtx, NftInfo,
NftTransferCommon, PhishingDomainReq, PhishingDomainRes, RefreshMetadataReq,
SpamContractReq, SpamContractRes, TransferMeta, TransferStatus, UriMeta};
#[cfg(not(target_arch = "wasm32"))]
use crate::nft::storage::NftMigrationOps;
use crate::nft::storage::{NftListStorageOps, NftTransferHistoryStorageOps};
use common::log::error;
use common::parse_rfc3339_to_timestamp;
Expand Down Expand Up @@ -155,6 +157,9 @@ pub async fn get_nft_transfers(ctx: MmArc, req: NftTransfersReq) -> MmResult<Nft
for chain in req.chains.iter() {
if !NftTransferHistoryStorageOps::is_initialized(&storage, chain).await? {
NftTransferHistoryStorageOps::init(&storage, chain).await?;
} else {
#[cfg(not(target_arch = "wasm32"))]
NftMigrationOps::migrate_tx_history_if_needed(&storage, chain).await?;
}
}
let mut transfer_history_list = storage
Expand Down Expand Up @@ -224,6 +229,8 @@ pub async fn update_nft(ctx: MmArc, req: UpdateNftReq) -> MmResult<(), UpdateNft
let transfer_history_initialized = NftTransferHistoryStorageOps::is_initialized(&storage, chain).await?;

let from_block = if transfer_history_initialized {
#[cfg(not(target_arch = "wasm32"))]
NftMigrationOps::migrate_tx_history_if_needed(&storage, chain).await?;
let last_transfer_block = NftTransferHistoryStorageOps::get_last_block_number(&storage, chain).await?;
last_transfer_block.map(|b| b + 1)
} else {
Expand Down
21 changes: 19 additions & 2 deletions mm2src/coins/nft/nft_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use crate::nft::nft_errors::{LockDBError, ParseChainTypeError, ParseContractType
use crate::nft::storage::{NftListStorageOps, NftTransferHistoryStorageOps};
use crate::{TransactionType, TxFeeDetails, WithdrawFee};

#[cfg(not(target_arch = "wasm32"))]
use crate::nft::storage::NftMigrationOps;

cfg_native! {
use db_common::async_sql_conn::AsyncConnection;
use futures::lock::Mutex as AsyncMutex;
Expand Down Expand Up @@ -438,7 +441,8 @@ pub struct WithdrawErc1155 {
#[serde(deserialize_with = "deserialize_token_id")]
pub(crate) token_id: BigUint,
/// Optional amount of the token to withdraw. Defaults to 1 if not specified.
pub(crate) amount: Option<BigDecimal>,
#[serde(deserialize_with = "deserialize_opt_biguint")]
pub(crate) amount: Option<BigUint>,
/// If set to `true`, withdraws the maximum amount available. Overrides the `amount` field.
#[serde(default)]
pub(crate) max: bool,
Expand Down Expand Up @@ -753,7 +757,7 @@ impl NftCtx {
#[cfg(not(target_arch = "wasm32"))]
pub(crate) async fn lock_db(
&self,
) -> MmResult<impl NftListStorageOps + NftTransferHistoryStorageOps + '_, LockDBError> {
) -> MmResult<impl NftListStorageOps + NftTransferHistoryStorageOps + NftMigrationOps + '_, LockDBError> {
Ok(self.nft_cache_db.lock().await)
}

Expand Down Expand Up @@ -806,6 +810,19 @@ where
BigUint::from_str(&s).map_err(serde::de::Error::custom)
}

/// Custom deserialization function for optional BigUint.
fn deserialize_opt_biguint<'de, D>(deserializer: D) -> Result<Option<BigUint>, D::Error>
where
D: Deserializer<'de>,
{
let opt: Option<String> = Option::deserialize(deserializer)?;
if let Some(s) = opt {
BigUint::from_str(&s).map(Some).map_err(serde::de::Error::custom)
} else {
Ok(None)
}
}

/// Request parameters for clearing NFT data from the database.
#[derive(Debug, Deserialize)]
pub struct ClearNftDbReq {
Expand Down
7 changes: 7 additions & 0 deletions mm2src/coins/nft/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,10 @@ pub(crate) struct TransferDetailsJson {
pub(crate) to_address: Address,
pub(crate) fee_details: Option<EthTxFeeDetails>,
}

#[async_trait]
pub trait NftMigrationOps {
type Error: NftStorageError;

async fn migrate_tx_history_if_needed(&self, chain: &Chain) -> MmResult<(), Self::Error>;
}
Loading
Loading