Skip to content

Commit

Permalink
refactor(SwapOps): make send_maker_payment async (#2250)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamardy authored Oct 23, 2024
1 parent b90d4dc commit b35a818
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 115 deletions.
10 changes: 5 additions & 5 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,11 +1117,11 @@ impl SwapOps for EthCoin {
)
}

fn send_maker_payment(&self, maker_payment: SendPaymentArgs) -> TransactionFut {
Box::new(
self.send_hash_time_locked_payment(maker_payment)
.map(TransactionEnum::from),
)
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
self.send_hash_time_locked_payment(maker_payment_args)
.compat()
.await
.map(TransactionEnum::from)
}

fn send_taker_payment(&self, taker_payment: SendPaymentArgs) -> TransactionFut {
Expand Down
14 changes: 5 additions & 9 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,19 +615,15 @@ impl SwapOps for LightningCoin {
Box::new(fut.boxed().compat())
}

fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionFut {
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
let invoice = match maker_payment_args.payment_instructions.clone() {
Some(PaymentInstructions::Lightning(invoice)) => invoice,
_ => try_tx_fus!(ERR!("Invalid instructions, ligntning invoice is expected")),
_ => try_tx_s!(ERR!("Invalid instructions, ligntning invoice is expected")),
};

let coin = self.clone();
let fut = async move {
// No need for max_total_cltv_expiry_delta for lightning maker payment since the maker is the side that reveals the secret/preimage
let payment = try_tx_s!(coin.pay_invoice(invoice, None).await);
Ok(payment.payment_hash.into())
};
Box::new(fut.boxed().compat())
// No need for max_total_cltv_expiry_delta for lightning maker payment since the maker is the side that reveals the secret/preimage
let payment = try_tx_s!(self.pay_invoice(invoice, None).await);
Ok(payment.payment_hash.into())
}

fn send_taker_payment(&self, taker_payment_args: SendPaymentArgs<'_>) -> TransactionFut {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ pub enum WatcherRewardError {
pub trait SwapOps {
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], expire_at: u64) -> TransactionFut;

fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionFut;
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult;

fn send_taker_payment(&self, taker_payment_args: SendPaymentArgs<'_>) -> TransactionFut;

Expand Down
19 changes: 7 additions & 12 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,21 +771,16 @@ impl SwapOps for Qrc20Coin {
Box::new(fut.boxed().compat())
}

fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs) -> TransactionFut {
let time_lock = try_tx_fus!(maker_payment_args.time_lock.try_into());
let taker_addr = try_tx_fus!(self.contract_address_from_raw_pubkey(maker_payment_args.other_pubkey));
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
let time_lock = try_tx_s!(maker_payment_args.time_lock.try_into());
let taker_addr = try_tx_s!(self.contract_address_from_raw_pubkey(maker_payment_args.other_pubkey));
let id = qrc20_swap_id(time_lock, maker_payment_args.secret_hash);
let value = try_tx_fus!(wei_from_big_decimal(&maker_payment_args.amount, self.utxo.decimals));
let value = try_tx_s!(wei_from_big_decimal(&maker_payment_args.amount, self.utxo.decimals));
let secret_hash = Vec::from(maker_payment_args.secret_hash);
let swap_contract_address = try_tx_fus!(maker_payment_args.swap_contract_address.try_to_address());
let swap_contract_address = try_tx_s!(maker_payment_args.swap_contract_address.try_to_address());

let selfi = self.clone();
let fut = async move {
selfi
.send_hash_time_locked_payment(id, value, time_lock, secret_hash, taker_addr, swap_contract_address)
.await
};
Box::new(fut.boxed().compat())
self.send_hash_time_locked_payment(id, value, time_lock, secret_hash, taker_addr, swap_contract_address)
.await
}

#[inline]
Expand Down
4 changes: 3 additions & 1 deletion mm2src/coins/siacoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@ impl SwapOps for SiaCoin {
unimplemented!()
}

fn send_maker_payment(&self, _maker_payment_args: SendPaymentArgs) -> TransactionFut { unimplemented!() }
async fn send_maker_payment(&self, _maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
unimplemented!()
}

fn send_taker_payment(&self, _taker_payment_args: SendPaymentArgs) -> TransactionFut { unimplemented!() }

Expand Down
4 changes: 3 additions & 1 deletion mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2702,7 +2702,7 @@ impl SwapOps for TendermintCoin {
)
}

fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs) -> TransactionFut {
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
self.send_htlc_for_denom(
maker_payment_args.time_lock_duration,
maker_payment_args.other_pubkey,
Expand All @@ -2711,6 +2711,8 @@ impl SwapOps for TendermintCoin {
self.denom.clone(),
self.decimals,
)
.compat()
.await
}

fn send_taker_payment(&self, taker_payment_args: SendPaymentArgs) -> TransactionFut {
Expand Down
21 changes: 12 additions & 9 deletions mm2src/coins/tendermint/tendermint_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ impl SwapOps for TendermintToken {
)
}

fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs) -> TransactionFut {
self.platform_coin.send_htlc_for_denom(
maker_payment_args.time_lock_duration,
maker_payment_args.other_pubkey,
maker_payment_args.secret_hash,
maker_payment_args.amount,
self.denom.clone(),
self.decimals,
)
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
self.platform_coin
.send_htlc_for_denom(
maker_payment_args.time_lock_duration,
maker_payment_args.other_pubkey,
maker_payment_args.secret_hash,
maker_payment_args.amount,
self.denom.clone(),
self.decimals,
)
.compat()
.await
}

fn send_taker_payment(&self, taker_payment_args: SendPaymentArgs) -> TransactionFut {
Expand Down
4 changes: 3 additions & 1 deletion mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ impl SwapOps for TestCoin {
unimplemented!()
}

fn send_maker_payment(&self, _maker_payment_args: SendPaymentArgs) -> TransactionFut { unimplemented!() }
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
unimplemented!()
}

fn send_taker_payment(&self, _taker_payment_args: SendPaymentArgs) -> TransactionFut { unimplemented!() }

Expand Down
4 changes: 3 additions & 1 deletion mm2src/coins/utxo/bch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,10 @@ impl SwapOps for BchCoin {
}

#[inline]
fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs) -> TransactionFut {
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
utxo_common::send_maker_payment(self.clone(), maker_payment_args)
.compat()
.await
}

#[inline]
Expand Down
4 changes: 3 additions & 1 deletion mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,10 @@ impl SwapOps for QtumCoin {
}

#[inline]
fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs) -> TransactionFut {
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
utxo_common::send_maker_payment(self.clone(), maker_payment_args)
.compat()
.await
}

#[inline]
Expand Down
22 changes: 9 additions & 13 deletions mm2src/coins/utxo/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,22 +1238,18 @@ impl SwapOps for SlpToken {
Box::new(fut.boxed().compat().map(|tx| tx.into()))
}

fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs) -> TransactionFut {
let taker_pub = try_tx_fus!(Public::from_slice(maker_payment_args.other_pubkey));
let amount = try_tx_fus!(sat_from_big_decimal(&maker_payment_args.amount, self.decimals()));
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
let taker_pub = try_tx_s!(Public::from_slice(maker_payment_args.other_pubkey));
let amount = try_tx_s!(sat_from_big_decimal(&maker_payment_args.amount, self.decimals()));
let secret_hash = maker_payment_args.secret_hash.to_owned();
let maker_htlc_keypair = self.derive_htlc_key_pair(maker_payment_args.swap_unique_data);
let time_lock = try_tx_fus!(maker_payment_args.time_lock.try_into());
let time_lock = try_tx_s!(maker_payment_args.time_lock.try_into());

let coin = self.clone();
let fut = async move {
let tx = try_tx_s!(
coin.send_htlc(maker_htlc_keypair.public(), &taker_pub, time_lock, &secret_hash, amount)
.await
);
Ok(tx.into())
};
Box::new(fut.boxed().compat())
let tx = try_tx_s!(
self.send_htlc(maker_htlc_keypair.public(), &taker_pub, time_lock, &secret_hash, amount)
.await
);
Ok(tx.into())
}

fn send_taker_payment(&self, taker_payment_args: SendPaymentArgs) -> TransactionFut {
Expand Down
4 changes: 3 additions & 1 deletion mm2src/coins/utxo/utxo_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,10 @@ impl SwapOps for UtxoStandardCoin {
}

#[inline]
fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs) -> TransactionFut {
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
utxo_common::send_maker_payment(self.clone(), maker_payment_args)
.compat()
.await
}

#[inline]
Expand Down
34 changes: 15 additions & 19 deletions mm2src/coins/z_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,28 +1210,24 @@ impl SwapOps for ZCoin {
Box::new(fut.boxed().compat())
}

fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionFut {
let selfi = self.clone();
async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
let maker_key_pair = self.derive_htlc_key_pair(maker_payment_args.swap_unique_data);
let taker_pub = try_tx_fus!(Public::from_slice(maker_payment_args.other_pubkey));
let taker_pub = try_tx_s!(Public::from_slice(maker_payment_args.other_pubkey));
let secret_hash = maker_payment_args.secret_hash.to_vec();
let time_lock = try_tx_fus!(maker_payment_args.time_lock.try_into());
let time_lock = try_tx_s!(maker_payment_args.time_lock.try_into());
let amount = maker_payment_args.amount;
let fut = async move {
let utxo_tx = try_tx_s!(
z_send_htlc(
&selfi,
time_lock,
maker_key_pair.public(),
&taker_pub,
&secret_hash,
amount
)
.await
);
Ok(utxo_tx.into())
};
Box::new(fut.boxed().compat())
let utxo_tx = try_tx_s!(
z_send_htlc(
self,
time_lock,
maker_key_pair.public(),
&taker_pub,
&secret_hash,
amount
)
.await
);
Ok(utxo_tx.into())
}

fn send_taker_payment(&self, taker_payment_args: SendPaymentArgs<'_>) -> TransactionFut {
Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/z_coin/z_coin_native_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn zombie_coin_send_and_refund_maker_payment() {
watcher_reward: None,
wait_for_confirmation_until: 0,
};
let tx = block_on_f01(coin.send_maker_payment(args)).unwrap();
let tx = block_on(coin.send_maker_payment(args)).unwrap();
log!("swap tx {}", hex::encode(tx.tx_hash_as_bytes().0));

let refund_args = RefundPaymentArgs {
Expand Down Expand Up @@ -115,7 +115,7 @@ fn zombie_coin_send_and_spend_maker_payment() {
wait_for_confirmation_until: 0,
};

let tx = block_on_f01(coin.send_maker_payment(maker_payment_args)).unwrap();
let tx = block_on(coin.send_maker_payment(maker_payment_args)).unwrap();
log!("swap tx {}", hex::encode(tx.tx_hash_as_bytes().0));

let maker_pub = taker_pub;
Expand Down
49 changes: 28 additions & 21 deletions mm2src/mm2_main/src/lp_swap/maker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,32 +794,36 @@ impl MakerSwap {
}

async fn maker_payment(&self) -> Result<(Option<MakerSwapCommand>, Vec<MakerSwapEvent>), String> {
let timeout = self.r().data.started_at + self.r().data.lock_duration / 3;
let lock_duration = self.r().data.lock_duration;
let timeout = self.r().data.started_at + lock_duration / 3;
let now = now_sec();
if now > timeout {
return Ok((Some(MakerSwapCommand::Finish), vec![
MakerSwapEvent::MakerPaymentTransactionFailed(ERRL!("Timeout {} > {}", now, timeout).into()),
]));
}

let maker_payment_lock = self.r().data.maker_payment_lock;
let other_maker_coin_htlc_pub = self.r().other_maker_coin_htlc_pub;
let secret_hash = self.secret_hash();
let maker_coin_swap_contract_address = self.r().data.maker_coin_swap_contract_address.clone();
let unique_data = self.unique_swap_data();
let payment_instructions = self.r().payment_instructions.clone();
let transaction_f = self
.maker_coin
.check_if_my_payment_sent(CheckIfMyPaymentSentArgs {
time_lock: self.r().data.maker_payment_lock,
other_pub: &*self.r().other_maker_coin_htlc_pub,
time_lock: maker_payment_lock,
other_pub: &*other_maker_coin_htlc_pub,
secret_hash: secret_hash.as_slice(),
search_from_block: self.r().data.maker_coin_start_block,
swap_contract_address: &self.r().data.maker_coin_swap_contract_address,
swap_contract_address: &maker_coin_swap_contract_address,
swap_unique_data: &unique_data,
amount: &self.maker_amount,
payment_instructions: &self.r().payment_instructions,
payment_instructions: &payment_instructions,
})
.compat();

let wait_maker_payment_until =
wait_for_maker_payment_conf_until(self.r().data.started_at, self.r().data.lock_duration);
let wait_maker_payment_until = wait_for_maker_payment_conf_until(self.r().data.started_at, lock_duration);
let watcher_reward = if self.r().watcher_reward {
match self
.maker_coin
Expand All @@ -841,20 +845,23 @@ impl MakerSwap {
Ok(res) => match res {
Some(tx) => tx,
None => {
let payment_fut = self.maker_coin.send_maker_payment(SendPaymentArgs {
time_lock_duration: self.r().data.lock_duration,
time_lock: self.r().data.maker_payment_lock,
other_pubkey: &*self.r().other_maker_coin_htlc_pub,
secret_hash: secret_hash.as_slice(),
amount: self.maker_amount.clone(),
swap_contract_address: &self.r().data.maker_coin_swap_contract_address,
swap_unique_data: &unique_data,
payment_instructions: &self.r().payment_instructions,
watcher_reward,
wait_for_confirmation_until: wait_maker_payment_until,
});

match payment_fut.compat().await {
let payment = self
.maker_coin
.send_maker_payment(SendPaymentArgs {
time_lock_duration: lock_duration,
time_lock: maker_payment_lock,
other_pubkey: &*other_maker_coin_htlc_pub,
secret_hash: secret_hash.as_slice(),
amount: self.maker_amount.clone(),
swap_contract_address: &maker_coin_swap_contract_address,
swap_unique_data: &unique_data,
payment_instructions: &payment_instructions,
watcher_reward,
wait_for_confirmation_until: wait_maker_payment_until,
})
.await;

match payment {
Ok(t) => t,
Err(err) => {
return Ok((Some(MakerSwapCommand::Finish), vec![
Expand Down
6 changes: 3 additions & 3 deletions mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn test_search_for_swap_tx_spend_native_was_refunded_maker() {
watcher_reward: None,
wait_for_confirmation_until: 0,
};
let tx = block_on_f01(coin.send_maker_payment(maker_payment_args)).unwrap();
let tx = block_on(coin.send_maker_payment(maker_payment_args)).unwrap();

let confirm_payment_input = ConfirmPaymentInput {
payment_tx: tx.tx_hex(),
Expand Down Expand Up @@ -276,7 +276,7 @@ fn test_search_for_maker_swap_tx_spend_native_was_spent_by_taker() {
watcher_reward: None,
wait_for_confirmation_until: 0,
};
let tx = block_on_f01(coin.send_maker_payment(maker_payment_args)).unwrap();
let tx = block_on(coin.send_maker_payment(maker_payment_args)).unwrap();

let confirm_payment_input = ConfirmPaymentInput {
payment_tx: tx.tx_hex(),
Expand Down Expand Up @@ -347,7 +347,7 @@ fn test_one_hundred_maker_payments_in_a_row_native() {
watcher_reward: None,
wait_for_confirmation_until: 0,
};
let tx = block_on_f01(coin.send_maker_payment(maker_payment_args)).unwrap();
let tx = block_on(coin.send_maker_payment(maker_payment_args)).unwrap();
if let TransactionEnum::UtxoTx(tx) = tx {
unspents.push(UnspentInfo {
outpoint: OutPoint {
Expand Down
Loading

0 comments on commit b35a818

Please sign in to comment.