Skip to content

Commit

Permalink
make send_taker_fee async
Browse files Browse the repository at this point in the history
  • Loading branch information
shamardy committed Oct 23, 2024
1 parent b35a818 commit 689de89
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 70 deletions.
23 changes: 14 additions & 9 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,16 +1105,21 @@ impl Deref for EthCoin {

#[async_trait]
impl SwapOps for EthCoin {
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
let address = try_tx_fus!(addr_from_raw_pubkey(fee_addr));

Box::new(
self.send_to_address(
address,
try_tx_fus!(wei_from_big_decimal(&dex_fee.fee_amount().into(), self.decimals)),
)
.map(TransactionEnum::from),
async fn send_taker_fee(
&self,
fee_addr: &[u8],
dex_fee: DexFee,
_uuid: &[u8],
_expire_at: u64,
) -> TransactionResult {
let address = try_tx_s!(addr_from_raw_pubkey(fee_addr));
self.send_to_address(
address,
try_tx_s!(wei_from_big_decimal(&dex_fee.fee_amount().into(), self.decimals)),
)
.map(TransactionEnum::from)
.compat()
.await
}

async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
Expand Down
11 changes: 8 additions & 3 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,14 @@ impl LightningCoin {
#[async_trait]
impl SwapOps for LightningCoin {
// Todo: This uses dummy data for now for the sake of swap P.O.C., this should be implemented probably after agreeing on how fees will work for lightning
fn send_taker_fee(&self, _fee_addr: &[u8], _dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
let fut = async move { Ok(TransactionEnum::LightningPayment(PaymentHash([1; 32]))) };
Box::new(fut.boxed().compat())
async fn send_taker_fee(
&self,
_fee_addr: &[u8],
_dex_fee: DexFee,
_uuid: &[u8],
_expire_at: u64,
) -> TransactionResult {
Ok(TransactionEnum::LightningPayment(PaymentHash([1; 32])))
}

async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
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 @@ -1069,7 +1069,7 @@ pub enum WatcherRewardError {
/// Swap operations (mostly based on the Hash/Time locked transactions implemented by coin wallets).
#[async_trait]
pub trait SwapOps {
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], expire_at: u64) -> TransactionFut;
async fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], expire_at: u64) -> TransactionResult;

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

Expand Down
21 changes: 11 additions & 10 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,17 +758,18 @@ impl UtxoCommonOps for Qrc20Coin {

#[async_trait]
impl SwapOps for Qrc20Coin {
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
let to_address = try_tx_fus!(self.contract_address_from_raw_pubkey(fee_addr));
let amount = try_tx_fus!(wei_from_big_decimal(&dex_fee.fee_amount().into(), self.utxo.decimals));
async fn send_taker_fee(
&self,
fee_addr: &[u8],
dex_fee: DexFee,
_uuid: &[u8],
_expire_at: u64,
) -> TransactionResult {
let to_address = try_tx_s!(self.contract_address_from_raw_pubkey(fee_addr));
let amount = try_tx_s!(wei_from_big_decimal(&dex_fee.fee_amount().into(), self.utxo.decimals));
let transfer_output =
try_tx_fus!(self.transfer_output(to_address, amount, QRC20_GAS_LIMIT_DEFAULT, QRC20_GAS_PRICE_DEFAULT));
let outputs = vec![transfer_output];

let selfi = self.clone();
let fut = async move { selfi.send_contract_calls(outputs).await };

Box::new(fut.boxed().compat())
try_tx_s!(self.transfer_output(to_address, amount, QRC20_GAS_LIMIT_DEFAULT, QRC20_GAS_PRICE_DEFAULT));
self.send_contract_calls(vec![transfer_output]).await
}

async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
Expand Down
8 changes: 7 additions & 1 deletion mm2src/coins/siacoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,13 @@ impl MarketCoinOps for SiaCoin {

#[async_trait]
impl SwapOps for SiaCoin {
fn send_taker_fee(&self, _fee_addr: &[u8], _dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
async fn send_taker_fee(
&self,
_fee_addr: &[u8],
_dex_fee: DexFee,
_uuid: &[u8],
_expire_at: u64,
) -> TransactionResult {
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 @@ -2691,7 +2691,7 @@ impl MarketCoinOps for TendermintCoin {
#[async_trait]
#[allow(unused_variables)]
impl SwapOps for TendermintCoin {
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], expire_at: u64) -> TransactionFut {
async fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], expire_at: u64) -> TransactionResult {
self.send_taker_fee_for_denom(
fee_addr,
dex_fee.fee_amount().into(),
Expand All @@ -2700,6 +2700,8 @@ impl SwapOps for TendermintCoin {
uuid,
expire_at,
)
.compat()
.await
}

async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
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 @@ -104,15 +104,18 @@ impl TendermintToken {
#[async_trait]
#[allow(unused_variables)]
impl SwapOps for TendermintToken {
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], expire_at: u64) -> TransactionFut {
self.platform_coin.send_taker_fee_for_denom(
fee_addr,
dex_fee.fee_amount().into(),
self.denom.clone(),
self.decimals,
uuid,
expire_at,
)
async fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], expire_at: u64) -> TransactionResult {
self.platform_coin
.send_taker_fee_for_denom(
fee_addr,
dex_fee.fee_amount().into(),
self.denom.clone(),
self.decimals,
uuid,
expire_at,
)
.compat()
.await
}

async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl MarketCoinOps for TestCoin {
#[async_trait]
#[mockable]
impl SwapOps for TestCoin {
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], _expire_at: u64) -> TransactionFut {
async fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], expire_at: u64) -> TransactionResult {
unimplemented!()
}

Expand Down
10 changes: 9 additions & 1 deletion mm2src/coins/utxo/bch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,16 @@ impl UtxoCommonOps for BchCoin {
#[async_trait]
impl SwapOps for BchCoin {
#[inline]
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
async fn send_taker_fee(
&self,
fee_addr: &[u8],
dex_fee: DexFee,
_uuid: &[u8],
_expire_at: u64,
) -> TransactionResult {
utxo_common::send_taker_fee(self.clone(), fee_addr, dex_fee)
.compat()
.await
}

#[inline]
Expand Down
10 changes: 9 additions & 1 deletion mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,16 @@ impl UtxoStandardOps for QtumCoin {
#[async_trait]
impl SwapOps for QtumCoin {
#[inline]
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
async fn send_taker_fee(
&self,
fee_addr: &[u8],
dex_fee: DexFee,
_uuid: &[u8],
_expire_at: u64,
) -> TransactionResult {
utxo_common::send_taker_fee(self.clone(), fee_addr, dex_fee)
.compat()
.await
}

#[inline]
Expand Down
39 changes: 21 additions & 18 deletions mm2src/coins/utxo/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,26 +1216,29 @@ impl MarketCoinOps for SlpToken {

#[async_trait]
impl SwapOps for SlpToken {
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
let coin = self.clone();
let fee_pubkey = try_tx_fus!(Public::from_slice(fee_addr));
async fn send_taker_fee(
&self,
fee_addr: &[u8],
dex_fee: DexFee,
_uuid: &[u8],
_expire_at: u64,
) -> TransactionResult {
let fee_pubkey = try_tx_s!(Public::from_slice(fee_addr));
let script_pubkey = ScriptBuilder::build_p2pkh(&fee_pubkey.address_hash().into()).into();
let amount = try_tx_fus!(dex_fee.fee_uamount(self.decimals()));
let amount = try_tx_s!(dex_fee.fee_uamount(self.decimals()));
let slp_out = SlpOutput { amount, script_pubkey };
let (preimage, recently_spent) = try_tx_s!(self.generate_slp_tx_preimage(vec![slp_out]).await);

let fut = async move {
let slp_out = SlpOutput { amount, script_pubkey };
let (preimage, recently_spent) = try_tx_s!(coin.generate_slp_tx_preimage(vec![slp_out]).await);
generate_and_send_tx(
&coin,
preimage.available_bch_inputs,
Some(preimage.slp_inputs.into_iter().map(|slp| slp.bch_unspent).collect()),
FeePolicy::SendExact,
recently_spent,
preimage.outputs,
)
.await
};
Box::new(fut.boxed().compat().map(|tx| tx.into()))
generate_and_send_tx(
self,
preimage.available_bch_inputs,
Some(preimage.slp_inputs.into_iter().map(|slp| slp.bch_unspent).collect()),
FeePolicy::SendExact,
recently_spent,
preimage.outputs,
)
.await
.map(|tx| tx.into())
}

async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
Expand Down
10 changes: 9 additions & 1 deletion mm2src/coins/utxo/utxo_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,16 @@ impl UtxoStandardOps for UtxoStandardCoin {
#[async_trait]
impl SwapOps for UtxoStandardCoin {
#[inline]
fn send_taker_fee(&self, fee_addr: &[u8], dex_fee: DexFee, _uuid: &[u8], _expire_at: u64) -> TransactionFut {
async fn send_taker_fee(
&self,
fee_addr: &[u8],
dex_fee: DexFee,
_uuid: &[u8],
_expire_at: u64,
) -> TransactionResult {
utxo_common::send_taker_fee(self.clone(), fee_addr, dex_fee)
.compat()
.await
}

#[inline]
Expand Down
16 changes: 9 additions & 7 deletions mm2src/coins/z_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,14 +1200,16 @@ impl MarketCoinOps for ZCoin {

#[async_trait]
impl SwapOps for ZCoin {
fn send_taker_fee(&self, _fee_addr: &[u8], dex_fee: DexFee, uuid: &[u8], _expire_at: u64) -> TransactionFut {
let selfi = self.clone();
async fn send_taker_fee(
&self,
_fee_addr: &[u8],
dex_fee: DexFee,
uuid: &[u8],
_expire_at: u64,
) -> TransactionResult {
let uuid = uuid.to_owned();
let fut = async move {
let tx = try_tx_s!(z_send_dex_fee(&selfi, dex_fee.fee_amount().into(), &uuid).await);
Ok(tx.into())
};
Box::new(fut.boxed().compat())
let tx = try_tx_s!(z_send_dex_fee(self, dex_fee.fee_amount().into(), &uuid).await);
Ok(tx.into())
}

async fn send_maker_payment(&self, maker_payment_args: SendPaymentArgs<'_>) -> TransactionResult {
Expand Down
1 change: 0 additions & 1 deletion mm2src/mm2_main/src/lp_swap/taker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,6 @@ impl TakerSwap {
let fee_tx = self
.taker_coin
.send_taker_fee(&DEX_FEE_ADDR_RAW_PUBKEY, fee_amount, self.uuid.as_bytes(), expire_at)
.compat()
.await;
let transaction = match fee_tx {
Ok(t) => t,
Expand Down
6 changes: 3 additions & 3 deletions mm2src/mm2_main/tests/docker_tests/qrc20_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ fn test_get_max_taker_vol_and_trade_with_dynamic_trade_fee(coin: QtumCoin, priv_

let dex_fee = dex_fee_amount("QTUM", "MYCOIN", &expected_max_taker_vol, &qtum_min_tx_amount);
let _taker_fee_tx =
block_on_f01(coin.send_taker_fee(&DEX_FEE_ADDR_RAW_PUBKEY, dex_fee, &[], timelock)).expect("!send_taker_fee");
block_on(coin.send_taker_fee(&DEX_FEE_ADDR_RAW_PUBKEY, dex_fee, &[], timelock)).expect("!send_taker_fee");
let taker_payment_args = SendPaymentArgs {
time_lock_duration: 0,
time_lock: timelock,
Expand Down Expand Up @@ -1705,7 +1705,7 @@ fn test_send_taker_fee_qtum() {
generate_segwit_qtum_coin_with_random_privkey("QTUM", BigDecimal::try_from(0.5).unwrap(), Some(0));

let amount = BigDecimal::from_str("0.01").unwrap();
let tx = block_on_f01(coin.send_taker_fee(
let tx = block_on(coin.send_taker_fee(
&DEX_FEE_ADDR_RAW_PUBKEY,
DexFee::Standard(amount.clone().into()),
&[],
Expand Down Expand Up @@ -1734,7 +1734,7 @@ fn test_send_taker_fee_qrc20() {
);

let amount = BigDecimal::from_str("0.01").unwrap();
let tx = block_on_f01(coin.send_taker_fee(
let tx = block_on(coin.send_taker_fee(
&DEX_FEE_ADDR_RAW_PUBKEY,
DexFee::Standard(amount.clone().into()),
&[],
Expand Down
6 changes: 3 additions & 3 deletions mm2src/mm2_main/tests/docker_tests/swap_watcher_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ fn test_watcher_validate_taker_fee_utxo() {
let taker_amount = MmNumber::from((10, 1));
let fee_amount = dex_fee_amount_from_taker_coin(&taker_coin, maker_coin.ticker(), &taker_amount);

let taker_fee = block_on_f01(taker_coin.send_taker_fee(
let taker_fee = block_on(taker_coin.send_taker_fee(
&DEX_FEE_ADDR_RAW_PUBKEY,
fee_amount,
Uuid::new_v4().as_bytes(),
Expand Down Expand Up @@ -1323,7 +1323,7 @@ fn test_watcher_validate_taker_fee_eth() {

let taker_amount = MmNumber::from((1, 1));
let fee_amount = dex_fee_amount_from_taker_coin(&taker_coin, "ETH", &taker_amount);
let taker_fee = block_on_f01(taker_coin.send_taker_fee(
let taker_fee = block_on(taker_coin.send_taker_fee(
&DEX_FEE_ADDR_RAW_PUBKEY,
fee_amount,
Uuid::new_v4().as_bytes(),
Expand Down Expand Up @@ -1420,7 +1420,7 @@ fn test_watcher_validate_taker_fee_erc20() {

let taker_amount = MmNumber::from((1, 1));
let fee_amount = dex_fee_amount_from_taker_coin(&taker_coin, "ETH", &taker_amount);
let taker_fee = block_on_f01(taker_coin.send_taker_fee(
let taker_fee = block_on(taker_coin.send_taker_fee(
&DEX_FEE_ADDR_RAW_PUBKEY,
fee_amount,
Uuid::new_v4().as_bytes(),
Expand Down

0 comments on commit 689de89

Please sign in to comment.