Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kmd): use kmd rewards for fees if change + interest is below dust #1944

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ impl Qrc20Coin {
self.utxo.conf.fork_id,
)?;

let miner_fee = data.fee_amount + data.unused_change.unwrap_or_default();
let miner_fee = data.fee_amount + data.unused_change;
Ok(GenerateQrc20TxResult {
signed,
miner_fee,
Expand Down Expand Up @@ -616,8 +616,9 @@ impl UtxoTxGenerationOps for Qrc20Coin {
unsigned: TransactionInputSigner,
data: AdditionalTxData,
my_script_pub: ScriptBytes,
dust: u64,
) -> UtxoRpcResult<(TransactionInputSigner, AdditionalTxData)> {
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub).await
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub, dust).await
}
}

Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ pub struct AdditionalTxData {
pub received_by_me: u64,
pub spent_by_me: u64,
pub fee_amount: u64,
pub unused_change: Option<u64>,
pub unused_change: u64,
pub kmd_rewards: Option<KmdRewardsDetails>,
}

Expand Down Expand Up @@ -838,6 +838,7 @@ pub trait UtxoTxGenerationOps {
mut unsigned: TransactionInputSigner,
mut data: AdditionalTxData,
my_script_pub: Bytes,
dust: u64,
) -> UtxoRpcResult<(TransactionInputSigner, AdditionalTxData)>;
}

Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/utxo/bch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,9 @@ impl UtxoTxGenerationOps for BchCoin {
unsigned: TransactionInputSigner,
data: AdditionalTxData,
my_script_pub: Bytes,
dust: u64,
) -> UtxoRpcResult<(TransactionInputSigner, AdditionalTxData)> {
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub).await
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub, dust).await
}
}

Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,9 @@ impl UtxoTxGenerationOps for QtumCoin {
unsigned: TransactionInputSigner,
data: AdditionalTxData,
my_script_pub: Bytes,
dust: u64,
) -> UtxoRpcResult<(TransactionInputSigner, AdditionalTxData)> {
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub).await
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub, dust).await
}
}

Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/qtum_delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl QtumCoin {
utxo.conf.fork_id,
)?;

let miner_fee = data.fee_amount + data.unused_change.unwrap_or_default();
let miner_fee = data.fee_amount + data.unused_change;
let generated_tx = GenerateQrc20TxResult {
signed,
miner_fee,
Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/utxo/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,9 +1072,10 @@ impl UtxoTxGenerationOps for SlpToken {
unsigned: TransactionInputSigner,
data: AdditionalTxData,
my_script_pub: Bytes,
dust: u64,
) -> UtxoRpcResult<(TransactionInputSigner, AdditionalTxData)> {
self.platform_coin
.calc_interest_if_required(unsigned, data, my_script_pub)
.calc_interest_if_required(unsigned, data, my_script_pub, dust)
.await
}
}
Expand Down
25 changes: 15 additions & 10 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,11 +1005,9 @@ impl<'a, T: AsRef<UtxoCoinFields> + UtxoTxGenerationOps> UtxoTxBuilder<'a, T> {
}
});
received_by_me += change;
None
} else if change > 0 {
Some(change)
0
} else {
None
change
};

let data = AdditionalTxData {
Expand All @@ -1022,7 +1020,7 @@ impl<'a, T: AsRef<UtxoCoinFields> + UtxoTxGenerationOps> UtxoTxBuilder<'a, T> {
};

Ok(coin
.calc_interest_if_required(self.tx, data, change_script_pubkey)
.calc_interest_if_required(self.tx, data, change_script_pubkey, dust)
.await?)
}
}
Expand All @@ -1035,6 +1033,7 @@ pub async fn calc_interest_if_required<T: UtxoCommonOps>(
mut unsigned: TransactionInputSigner,
mut data: AdditionalTxData,
my_script_pub: Bytes,
dust: u64,
) -> UtxoRpcResult<(TransactionInputSigner, AdditionalTxData)> {
if coin.as_ref().conf.ticker != "KMD" {
return Ok((unsigned, data));
Expand Down Expand Up @@ -1066,11 +1065,17 @@ pub async fn calc_interest_if_required<T: UtxoCommonOps>(
match output_to_me {
Some(ref mut output) => output.value += interest,
None => {
let interest_output = TransactionOutput {
script_pubkey: my_script_pub,
value: interest,
};
unsigned.outputs.push(interest_output);
let maybe_change_output_value = interest + data.unused_change;
if maybe_change_output_value > dust {
let change_output = TransactionOutput {
script_pubkey: my_script_pub,
value: maybe_change_output_value,
};
unsigned.outputs.push(change_output);
data.unused_change = 0;
} else {
data.unused_change += interest;
}
},
};
} else {
Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/utxo/utxo_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ impl UtxoTxGenerationOps for UtxoStandardCoin {
unsigned: TransactionInputSigner,
data: AdditionalTxData,
my_script_pub: Bytes,
dust: u64,
) -> UtxoRpcResult<(TransactionInputSigner, AdditionalTxData)> {
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub).await
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub, dust).await
}
}

Expand Down
10 changes: 5 additions & 5 deletions mm2src/coins/utxo/utxo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn test_generate_transaction() {
assert_eq!(generated.0.outputs.len(), 1);

assert_eq!(generated.1.fee_amount, 1000);
assert_eq!(generated.1.unused_change, Some(999));
assert_eq!(generated.1.unused_change, 999);
assert_eq!(generated.1.received_by_me, 0);
assert_eq!(generated.1.spent_by_me, 100000);

Expand All @@ -251,7 +251,7 @@ fn test_generate_transaction() {
assert_eq!(generated.0.outputs.len(), 1);

assert_eq!(generated.1.fee_amount, 1000);
assert_eq!(generated.1.unused_change, None);
assert_eq!(generated.1.unused_change, 0);
assert_eq!(generated.1.received_by_me, 99000);
assert_eq!(generated.1.spent_by_me, 100000);
assert_eq!(generated.0.outputs[0].value, 99000);
Expand Down Expand Up @@ -1156,7 +1156,7 @@ fn test_generate_transaction_relay_fee_is_used_when_dynamic_fee_is_lower() {

// generated transaction fee must be equal to relay fee if calculated dynamic fee is lower than relay
assert_eq!(generated.1.fee_amount, 100000000);
assert_eq!(generated.1.unused_change, None);
assert_eq!(generated.1.unused_change, 0);
assert_eq!(generated.1.received_by_me, 0);
assert_eq!(generated.1.spent_by_me, 1000000000);
assert!(unsafe { GET_RELAY_FEE_CALLED });
Expand Down Expand Up @@ -1201,7 +1201,7 @@ fn test_generate_transaction_relay_fee_is_used_when_dynamic_fee_is_lower_and_ded

// generated transaction fee must be equal to relay fee if calculated dynamic fee is lower than relay
assert_eq!(generated.1.fee_amount, 100000000);
assert_eq!(generated.1.unused_change, None);
assert_eq!(generated.1.unused_change, 0);
assert_eq!(generated.1.received_by_me, 0);
assert_eq!(generated.1.spent_by_me, 1000000000);
assert!(unsafe { GET_RELAY_FEE_CALLED });
Expand Down Expand Up @@ -1248,7 +1248,7 @@ fn test_generate_tx_fee_is_correct_when_dynamic_fee_is_larger_than_relay() {

// resulting signed transaction size would be 3032 bytes so fee is 3032 sat
assert_eq!(generated.1.fee_amount, 3032);
assert_eq!(generated.1.unused_change, None);
assert_eq!(generated.1.unused_change, 0);
assert_eq!(generated.1.received_by_me, 999996968);
assert_eq!(generated.1.spent_by_me, 20000000000);
assert!(unsafe { GET_RELAY_FEE_CALLED });
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/utxo_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ where
// Finish by generating `TransactionDetails` from the signed transaction.
self.on_finishing()?;

let fee_amount = data.fee_amount + data.unused_change.unwrap_or_default();
let fee_amount = data.fee_amount + data.unused_change;
let fee_details = UtxoFeeDetails {
coin: Some(ticker.clone()),
amount: big_decimal_from_sat(fee_amount as i64, decimals),
Expand Down
5 changes: 3 additions & 2 deletions mm2src/coins/z_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ impl ZCoin {
received_by_me,
spent_by_me: sat_from_big_decimal(&total_input_amount, self.decimals())?,
fee_amount: sat_from_big_decimal(&tx_fee, self.decimals())?,
unused_change: None,
unused_change: 0,
kmd_rewards: None,
};
Ok((tx, additional_data, sync_guard))
Expand Down Expand Up @@ -1753,8 +1753,9 @@ impl UtxoTxGenerationOps for ZCoin {
unsigned: TransactionInputSigner,
data: AdditionalTxData,
my_script_pub: Bytes,
dust: u64,
) -> UtxoRpcResult<(TransactionInputSigner, AdditionalTxData)> {
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub).await
utxo_common::calc_interest_if_required(self, unsigned, data, my_script_pub, dust).await
}
}

Expand Down
Loading