Skip to content

Commit

Permalink
require amount: Option<BigUint> in WithdrawErc1155
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Sep 12, 2024
1 parent 416b23c commit a493f3f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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)
};

if amount_dec > wallet_amount {
Expand Down
16 changes: 15 additions & 1 deletion mm2src/coins/nft/nft_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -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<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

0 comments on commit a493f3f

Please sign in to comment.