Skip to content

Commit

Permalink
revert qtum gas_fee calc in trade preimage,
Browse files Browse the repository at this point in the history
fix refactored UtxoTxBuilder::build(): return only txfee (w/o gas fee) with tx as it used to be
  • Loading branch information
dimxy committed Jan 9, 2025
1 parent 8d2fe79 commit bc69a42
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ impl Qrc20Coin {
contract_outputs: Vec<ContractCallOutput>,
stage: &FeeApproxStage,
) -> TradePreimageResult<BigDecimal> {
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 {
Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/qrc20/qrc20_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ impl<'a, T: AsRef<UtxoCoinFields> + 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(_) => {},
};
Expand Down Expand Up @@ -595,8 +595,8 @@ impl<'a, T: AsRef<UtxoCoinFields> + 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 {
Expand Down Expand Up @@ -634,7 +634,7 @@ impl<'a, T: AsRef<UtxoCoinFields> + 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 {
Expand Down Expand Up @@ -678,7 +678,7 @@ impl<'a, T: AsRef<UtxoCoinFields> + 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.
Expand Down Expand Up @@ -738,13 +738,13 @@ impl<'a, T: AsRef<UtxoCoinFields> + 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,
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit bc69a42

Please sign in to comment.