diff --git a/mm2src/coins/qrc20.rs b/mm2src/coins/qrc20.rs index 79a803edd0..2b98e06263 100644 --- a/mm2src/coins/qrc20.rs +++ b/mm2src/coins/qrc20.rs @@ -584,6 +584,7 @@ impl Qrc20Coin { contract_outputs: Vec, stage: &FeeApproxStage, ) -> TradePreimageResult { + let decimals = self.as_ref().decimals; let mut gas_fee = 0; let mut outputs = Vec::with_capacity(contract_outputs.len()); for output in contract_outputs { @@ -594,7 +595,8 @@ impl Qrc20Coin { let miner_fee = UtxoCommonOps::preimage_trade_fee_required_to_send_outputs(self, outputs, fee_policy, Some(gas_fee), stage) .await?; - Ok(miner_fee) + let gas_fee = big_decimal_from_sat(gas_fee as i64, decimals); + Ok(miner_fee + gas_fee) } } diff --git a/mm2src/coins/qrc20/qrc20_tests.rs b/mm2src/coins/qrc20/qrc20_tests.rs index 6a12f21444..76412d814f 100644 --- a/mm2src/coins/qrc20/qrc20_tests.rs +++ b/mm2src/coins/qrc20/qrc20_tests.rs @@ -150,7 +150,7 @@ fn test_withdraw_impl_fee_details() { // 1000 from satoshi, // where decimals = 8, // 1000 is fixed fee - "miner_fee": "1.00001", + "miner_fee": "0.00001", "gas_limit": 2_500_000, "gas_price": 40, // (gas_limit * gas_price) from satoshi in Qtum diff --git a/mm2src/coins/utxo/utxo_common.rs b/mm2src/coins/utxo/utxo_common.rs index d88f607ec8..bd201bf48e 100644 --- a/mm2src/coins/utxo/utxo_common.rs +++ b/mm2src/coins/utxo/utxo_common.rs @@ -547,7 +547,7 @@ impl<'a, T: AsRef + UtxoTxGenerationOps> UtxoTxBuilder<'a, T> { .fold(0u64, |required, output| required + output.value); match self.fee_policy { FeePolicy::SendExact => { - sum_output += self.tx_fee(); + sum_output += self.total_tx_fee(); }, FeePolicy::DeductFromOutput(_) => {}, }; @@ -595,8 +595,8 @@ impl<'a, T: AsRef + UtxoTxGenerationOps> UtxoTxBuilder<'a, T> { /// Adds change output. /// Returns change value and dust change fn add_change(&mut self, change_script_pubkey: &Bytes) -> (u64, u64) { - if self.sum_inputs > self.sum_outputs + self.tx_fee() { - let change = self.sum_inputs - (self.sum_outputs + self.tx_fee()); + if self.sum_inputs > self.sum_outputs + self.total_tx_fee() { + let change = self.sum_inputs - (self.sum_outputs + self.total_tx_fee()); if change > self.dust() { self.tx.outputs.push({ TransactionOutput { @@ -634,7 +634,7 @@ impl<'a, T: AsRef + UtxoTxGenerationOps> UtxoTxBuilder<'a, T> { match self.fee_policy { FeePolicy::SendExact => Ok(0), FeePolicy::DeductFromOutput(i) => { - let tx_fee = self.tx_fee(); + let tx_fee = self.total_tx_fee(); let min_output = tx_fee + self.dust(); let val = self.tx.outputs[i].value; return_err_if_false!(val >= min_output, GenerateTxError::DeductFeeFromOutputFailed { @@ -678,7 +678,7 @@ impl<'a, T: AsRef + UtxoTxGenerationOps> UtxoTxBuilder<'a, T> { } } - fn tx_fee(&self) -> u64 { self.tx_fee + self.gas_fee.unwrap_or(0u64) } + fn total_tx_fee(&self) -> u64 { self.tx_fee + self.gas_fee.unwrap_or(0u64) } /// Generates unsigned transaction (TransactionInputSigner) from specified utxos and outputs. /// sends the change (inputs amount - outputs amount) to the [`UtxoTxBuilder::from`] address. @@ -738,13 +738,13 @@ impl<'a, T: AsRef + UtxoTxGenerationOps> UtxoTxBuilder<'a, T> { self.sum_outputs += change; unused_change = unused; self.update_tx_fee(from.addr_format(), &actual_fee_per_kb); // recalculate txfee with the change output, if added - if self.sum_inputs >= self.sum_outputs + self.tx_fee() { + if self.sum_inputs >= self.sum_outputs + self.total_tx_fee() { break; } } let data = AdditionalTxData { - fee_amount: self.tx_fee(), + fee_amount: self.tx_fee, // we return only txfee here (w/o gas_fee) received_by_me: self.sum_received_by_me(&change_script_pubkey), spent_by_me: self.sum_inputs, unused_change, diff --git a/mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs b/mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs index b7efcc925e..cfbd2df664 100644 --- a/mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs +++ b/mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs @@ -851,10 +851,10 @@ fn test_check_balance_on_order_post_base_coin_locked() { let alice_priv_key = random_secp256k1_secret(); let timeout = 30; // timeout if test takes more than 80 seconds to run - // fill the Bob address by 0.08001 Qtum (including tx_fee of 1000 and gas_fee of 400000) + // fill the Bob address by 0.05 Qtum let (_ctx, coin) = qrc20_coin_from_privkey("QICK", bob_priv_key); let my_address = coin.my_address().expect("!my_address"); - fill_address(&coin, &my_address, BigDecimal::try_from(0.08001).unwrap(), timeout); + fill_address(&coin, &my_address, BigDecimal::try_from(0.05).unwrap(), timeout); // fill the Bob address by 10 MYCOIN let (_ctx, coin) = utxo_coin_from_privkey("MYCOIN", bob_priv_key); let my_address = coin.my_address().expect("!my_address");