From a493f3f5321abff8e1dd39837441686f2e0fc015 Mon Sep 17 00:00:00 2001 From: laruh Date: Thu, 12 Sep 2024 13:00:15 +0700 Subject: [PATCH] require amount: Option in WithdrawErc1155 --- mm2src/coins/eth.rs | 8 +++++++- mm2src/coins/nft/nft_structs.rs | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/mm2src/coins/eth.rs b/mm2src/coins/eth.rs index ef1d57968c..deb2d27ab9 100644 --- a/mm2src/coins/eth.rs +++ b/mm2src/coins/eth.rs @@ -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 @@ -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) }; if amount_dec > wallet_amount { diff --git a/mm2src/coins/nft/nft_structs.rs b/mm2src/coins/nft/nft_structs.rs index f772b92f56..52febc5270 100644 --- a/mm2src/coins/nft/nft_structs.rs +++ b/mm2src/coins/nft/nft_structs.rs @@ -438,7 +438,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, + #[serde(deserialize_with = "deserialize_opt_biguint")] + pub(crate) amount: Option, /// If set to `true`, withdraws the maximum amount available. Overrides the `amount` field. #[serde(default)] pub(crate) max: bool, @@ -806,6 +807,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, D::Error> +where + D: Deserializer<'de>, +{ + let opt: Option = 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 {