Skip to content

Commit

Permalink
review: replace BigDecimal by BigUint in NFT withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Oct 26, 2024
1 parent a5486e1 commit ef3dd1e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
29 changes: 12 additions & 17 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ 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;

Expand Down Expand Up @@ -914,24 +913,20 @@ pub async fn withdraw_erc1155(ctx: MmArc, withdraw_type: WithdrawErc1155) -> Wit
get_valid_nft_addr_to_withdraw(coin, &withdraw_type.to, &withdraw_type.token_address)?;

let token_id_str = &withdraw_type.token_id.to_string();
let wallet_amount = eth_coin.erc1155_balance(token_addr, token_id_str).await?;
let wallet_erc1155_amount = eth_coin.erc1155_balance(token_addr, token_id_str).await?;

let amount_dec = if withdraw_type.max {
wallet_amount.clone()
let amount_uint = if withdraw_type.max {
wallet_erc1155_amount.clone()
} else {
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)
withdraw_type.amount.unwrap_or_else(|| BigUint::from(1u32))
};

if amount_dec > wallet_amount {
if amount_uint > wallet_erc1155_amount {
return MmError::err(WithdrawError::NotEnoughNftsAmount {
token_address: withdraw_type.token_address,
token_id: withdraw_type.token_id.to_string(),
available: wallet_amount,
required: amount_dec,
available: wallet_erc1155_amount,
required: amount_uint,
});
}

Expand All @@ -942,7 +937,7 @@ pub async fn withdraw_erc1155(ctx: MmArc, withdraw_type: WithdrawErc1155) -> Wit
let token_id_u256 =
U256::from_dec_str(token_id_str).map_to_mm(|e| NumConversError::new(format!("{:?}", e)))?;
let amount_u256 =
U256::from_dec_str(&amount_dec.to_string()).map_to_mm(|e| NumConversError::new(format!("{:?}", e)))?;
U256::from_dec_str(&amount_uint.to_string()).map_to_mm(|e| NumConversError::new(format!("{:?}", e)))?;
let data = function.encode_input(&[
Token::Address(my_address),
Token::Address(to_addr),
Expand Down Expand Up @@ -1001,7 +996,7 @@ pub async fn withdraw_erc1155(ctx: MmArc, withdraw_type: WithdrawErc1155) -> Wit
contract_type: ContractType::Erc1155,
token_address: withdraw_type.token_address,
token_id: withdraw_type.token_id,
amount: amount_dec,
amount: amount_uint,
fee_details: Some(fee_details.into()),
coin: eth_coin.ticker.clone(),
block_height: 0,
Expand Down Expand Up @@ -1092,7 +1087,7 @@ pub async fn withdraw_erc721(ctx: MmArc, withdraw_type: WithdrawErc721) -> Withd
contract_type: ContractType::Erc721,
token_address: withdraw_type.token_address,
token_id: withdraw_type.token_id,
amount: 1.into(),
amount: BigUint::from(1u8),
fee_details: Some(fee_details.into()),
coin: eth_coin.ticker.clone(),
block_height: 0,
Expand Down Expand Up @@ -4418,7 +4413,7 @@ impl EthCoin {
self.get_token_balance_for_address(my_address, token_address).await
}

async fn erc1155_balance(&self, token_addr: Address, token_id: &str) -> MmResult<BigDecimal, BalanceError> {
async fn erc1155_balance(&self, token_addr: Address, token_id: &str) -> MmResult<BigUint, BalanceError> {
let wallet_amount_uint = match self.coin_type {
EthCoinType::Eth | EthCoinType::Nft { .. } => {
let function = ERC1155_CONTRACT.function("balanceOf")?;
Expand All @@ -4445,7 +4440,7 @@ impl EthCoin {
},
};
// The "balanceOf" function in ERC1155 standard returns the exact count of tokens held by address without any decimals or scaling factors
let wallet_amount = wallet_amount_uint.to_string().parse::<BigDecimal>()?;
let wallet_amount = wallet_amount_uint.to_string().parse::<BigUint>()?;
Ok(wallet_amount)
}

Expand Down
8 changes: 4 additions & 4 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use mm2_core::mm_ctx::{from_ctx, MmArc};
use mm2_err_handle::prelude::*;
use mm2_metrics::MetricsWeak;
use mm2_number::{bigdecimal::{BigDecimal, ParseBigDecimalError, Zero},
MmNumber};
BigUint, MmNumber, ParseBigIntError};
use mm2_rpc::data::legacy::{EnabledCoin, GetEnabledResponse, Mm2RpcResult};
use parking_lot::Mutex as PaMutex;
use rpc::v1::types::{Bytes as BytesJson, H256 as H256Json};
Expand Down Expand Up @@ -2642,7 +2642,7 @@ pub enum BalanceError {
UnexpectedDerivationMethod(UnexpectedDerivationMethod),
#[display(fmt = "Wallet storage error: {}", _0)]
WalletStorageError(String),
#[from_stringify("Bip32Error", "NumConversError", "ParseBigDecimalError")]
#[from_stringify("Bip32Error", "NumConversError", "ParseBigIntError")]
#[display(fmt = "Internal: {}", _0)]
Internal(String),
}
Expand Down Expand Up @@ -2994,8 +2994,8 @@ pub enum WithdrawError {
NotEnoughNftsAmount {
token_address: String,
token_id: String,
available: BigDecimal,
required: BigDecimal,
available: BigUint,
required: BigUint,
},
#[display(fmt = "DB error {}", _0)]
DbError(String),
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/nft/nft_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ pub struct TransactionNftDetails {
pub(crate) token_address: String,
#[serde(serialize_with = "serialize_token_id")]
pub(crate) token_id: BigUint,
pub(crate) amount: BigDecimal,
pub(crate) amount: BigUint,
pub(crate) fee_details: Option<TxFeeDetails>,
/// The coin transaction belongs to
pub(crate) coin: String,
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_number/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use num_bigint;
pub use num_rational;

pub use bigdecimal::BigDecimal;
pub use num_bigint::{BigInt, BigUint};
pub use num_bigint::{BigInt, BigUint, ParseBigIntError};
pub use num_rational::BigRational;
pub use paste::paste;

Expand Down

0 comments on commit ef3dd1e

Please sign in to comment.