refactor: don't use mutable var
@@ -290,19 +290,23 @@ impl<T: Config> Module<T> {
Error::<T>::CommitPeriodExpired
);
- let mut total_amount = issue.amount + issue.fee;
ext::btc_relay::verify_transaction_inclusion::<T>(tx_id, merkle_proof)?;
let (refund_address, amount_transferred) =
ext::btc_relay::validate_transaction::<T>(raw_tx, None, issue.btc_address, None)?;
+ let expected_total_amount = issue
+ .amount
+ .checked_add(&issue.fee)
+ .ok_or(Error::<T>::ArithmeticOverflow)?;
let amount_transferred = Self::u128_to_btc(amount_transferred as u128)?;
- if amount_transferred < total_amount {
+ // check for unexpected bitcoin amounts, and update the issue struct
+ if amount_transferred < expected_total_amount {
// only the requester of the issue can execute payments with insufficient amounts
ensure!(requester == executor, Error::<T>::InvalidExecutor);
// decrease the to-be-issued tokens that will not be issued after all
- let deficit = total_amount
+ let deficit = expected_total_amount
.checked_sub(&amount_transferred)
.ok_or(Error::<T>::ArithmeticUnderflow)?;
ext::vault_registry::decrease_to_be_issued_tokens::<T>(&issue.vault, deficit)?;
@@ -311,7 +315,7 @@ impl<T: Config> Module<T> {
let released_collateral = ext::vault_registry::calculate_collateral::<T>(
issue.griefing_collateral,
amount_transferred,
- total_amount,
+ expected_total_amount,
)?;
ext::collateral::release_collateral::<T>(&requester, released_collateral)?;
let slashed_collateral = issue
@@ -325,17 +329,15 @@ impl<T: Config> Module<T> {
)?;
Self::update_issue_amount(&issue_id, &mut issue, amount_transferred)?;
-
- total_amount = amount_transferred;
} else {
// release griefing collateral
ext::collateral::release_collateral::<T>(&requester, issue.griefing_collateral)?;
- if amount_transferred > total_amount
+ if amount_transferred > expected_total_amount
&& !ext::vault_registry::is_vault_liquidated::<T>(&issue.vault)?
{
let surplus_btc = amount_transferred
- .checked_sub(&total_amount)
+ .checked_sub(&expected_total_amount)
.ok_or(Error::<T>::ArithmeticUnderflow)?;
match ext::vault_registry::try_increase_to_be_issued_tokens::<T>(
@@ -345,7 +347,6 @@ impl<T: Config> Module<T> {
Ok(_) => {
// Current vault can handle the surplus; update the issue request
Self::update_issue_amount(&issue_id, &mut issue, amount_transferred)?;
- total_amount = amount_transferred;
}
Err(_) => {
// vault does not have enough collateral to accept the over payment, so refund.
@@ -359,9 +360,14 @@ impl<T: Config> Module<T> {
}
}
}
- }
+ };
- ext::vault_registry::issue_tokens::<T>(&issue.vault, total_amount)?;
+ // issue struct may have been update above; recalculate the total
+ let total = issue
+ .amount
+ .checked_add(&issue.fee)
+ .ok_or(Error::<T>::ArithmeticOverflow)?;
+ ext::vault_registry::issue_tokens::<T>(&issue.vault, total)?;
// mint polkabtc amount
ext::treasury::mint::<T>(requester.clone(), issue.amount);
@@ -395,7 +401,7 @@ impl<T: Config> Module<T> {
Self::deposit_event(<Event<T>>::ExecuteIssue(
issue_id,
requester,
- total_amount,
+ expected_total_amount,
issue.vault,
));
Ok(())
