Skip to content

Commit

Permalink
improve get_receiver_trade_fee for tendermint
Browse files Browse the repository at this point in the history
Signed-off-by: ozkanonur <[email protected]>
  • Loading branch information
onur-ozkan committed Apr 24, 2023
1 parent 2ac9679 commit 6f51bf8
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 43 deletions.
2 changes: 1 addition & 1 deletion mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4320,7 +4320,7 @@ impl MmCoin for EthCoin {
})
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
let coin = self.clone();
let fut = async move {
let gas_price = coin.get_gas_price().compat().await?;
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ fn get_receiver_trade_preimage() {
};

let actual = coin
.get_receiver_trade_fee(Default::default(), FeeApproxStage::WithoutApprox)
.get_receiver_trade_fee(FeeApproxStage::WithoutApprox)
.wait()
.expect("!get_sender_trade_fee");
assert_eq!(actual, expected_fee);
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ impl MmCoin 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 get_receiver_trade_fee(&self, _send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
Box::new(futures01::future::ok(TradeFee {
coin: self.ticker().to_owned(),
amount: Default::default(),
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 @@ -2190,7 +2190,7 @@ pub trait MmCoin:
) -> TradePreimageResult<TradeFee>;

/// Get fee to be paid by receiver per whole swap and check if the wallet has sufficient balance to pay the fee.
fn get_receiver_trade_fee(&self, send_amount: BigDecimal, stage: FeeApproxStage) -> TradePreimageFut<TradeFee>;
fn get_receiver_trade_fee(&self, stage: FeeApproxStage) -> TradePreimageFut<TradeFee>;

/// Get transaction fee the Taker has to pay to send a `TakerFee` transaction and check if the wallet has sufficient balance to pay the fee.
async fn get_fee_to_send_taker_fee(
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ impl MmCoin for Qrc20Coin {
})
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
let selfi = self.clone();
let fut = async move {
// pass the dummy params
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 @@ -916,7 +916,7 @@ fn test_receiver_trade_preimage() {
check_tx_fee(&coin, ActualTxFee::FixedPerKb(EXPECTED_TX_FEE as u64));

let actual = coin
.get_receiver_trade_fee(Default::default(), FeeApproxStage::WithoutApprox)
.get_receiver_trade_fee(FeeApproxStage::WithoutApprox)
.wait()
.expect("!get_receiver_trade_fee");
// only one contract call should be included into the expected trade fee
Expand Down
4 changes: 1 addition & 3 deletions mm2src/coins/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,7 @@ impl MmCoin for SolanaCoin {
unimplemented!()
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
unimplemented!()
}
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> { unimplemented!() }

async fn get_fee_to_send_taker_fee(
&self,
Expand Down
4 changes: 1 addition & 3 deletions mm2src/coins/solana/spl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,7 @@ impl MmCoin for SplToken {
unimplemented!()
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
unimplemented!()
}
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> { unimplemented!() }

async fn get_fee_to_send_taker_fee(
&self,
Expand Down
11 changes: 8 additions & 3 deletions mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,13 +2004,18 @@ impl MmCoin for TendermintCoin {
.await
}

fn get_receiver_trade_fee(&self, send_amount: BigDecimal, stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
let coin = self.clone();
let fut = async move {
// We can't simulate Claim Htlc without having information about broadcasted htlc tx.
// Since create and claim htlc fees are almost same, we can simply simulate create htlc tx.
coin.get_sender_trade_fee_for_denom(coin.ticker.clone(), coin.denom.clone(), coin.decimals, send_amount)
.await
coin.get_sender_trade_fee_for_denom(
coin.ticker.clone(),
coin.denom.clone(),
coin.decimals,
coin.min_tx_amount(),
)
.await
};
Box::new(fut.boxed().compat())
}
Expand Down
9 changes: 7 additions & 2 deletions mm2src/coins/tendermint/tendermint_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,14 +739,19 @@ impl MmCoin for TendermintToken {
.await
}

fn get_receiver_trade_fee(&self, send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
let token = self.clone();
let fut = async move {
// We can't simulate Claim Htlc without having information about broadcasted htlc tx.
// Since create and claim htlc fees are almost same, we can simply simulate create htlc tx.
token
.platform_coin
.get_sender_trade_fee_for_denom(token.ticker.clone(), token.denom.clone(), token.decimals, send_amount)
.get_sender_trade_fee_for_denom(
token.ticker.clone(),
token.denom.clone(),
token.decimals,
token.min_tx_amount(),
)
.await
};
Box::new(fut.boxed().compat())
Expand Down
4 changes: 1 addition & 3 deletions mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,7 @@ impl MmCoin for TestCoin {
unimplemented!()
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
unimplemented!()
}
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> { unimplemented!() }

async fn get_fee_to_send_taker_fee(
&self,
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/bch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ impl MmCoin for BchCoin {
utxo_common::get_sender_trade_fee(self, value, stage).await
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
utxo_common::get_receiver_trade_fee(self.clone())
}

Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ impl MmCoin for QtumCoin {
utxo_common::get_sender_trade_fee(self, value, stage).await
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
utxo_common::get_receiver_trade_fee(self.clone())
}

Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1788,7 +1788,7 @@ impl MmCoin for SlpToken {
})
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
let coin = self.clone();

let fut = async move {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/utxo_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ impl MmCoin for UtxoStandardCoin {
utxo_common::get_sender_trade_fee(self, value, stage).await
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
utxo_common::get_receiver_trade_fee(self.clone())
}

Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/z_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,7 @@ impl MmCoin for ZCoin {
})
}

fn get_receiver_trade_fee(&self, _send_amount: BigDecimal, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
fn get_receiver_trade_fee(&self, _stage: FeeApproxStage) -> TradePreimageFut<TradeFee> {
utxo_common::get_receiver_trade_fee(self.clone())
}

Expand Down
12 changes: 4 additions & 8 deletions mm2src/mm2_main/src/lp_ordermatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4545,13 +4545,9 @@ async fn cancel_orders_on_error<T, E>(ctx: &MmArc, req: &SetPriceReq, error: E)
Err(error)
}

pub async fn check_other_coin_balance_for_order_issue(
ctx: &MmArc,
other_coin: &MmCoinEnum,
my_coin_volume: BigDecimal,
) -> CheckBalanceResult<()> {
pub async fn check_other_coin_balance_for_order_issue(ctx: &MmArc, other_coin: &MmCoinEnum) -> CheckBalanceResult<()> {
let trade_fee = other_coin
.get_receiver_trade_fee(my_coin_volume, FeeApproxStage::OrderIssue)
.get_receiver_trade_fee(FeeApproxStage::OrderIssue)
.compat()
.await
.mm_err(|e| CheckBalanceError::from_trade_preimage_error(e, other_coin.ticker()))?;
Expand Down Expand Up @@ -4605,7 +4601,7 @@ pub async fn create_maker_order(ctx: &MmArc, req: SetPriceReq) -> Result<MakerOr
.or_else(|e| cancel_orders_on_error(ctx, &req, e))
.await
);
try_s!(check_other_coin_balance_for_order_issue(ctx, &rel_coin, volume.to_decimal()).await);
try_s!(check_other_coin_balance_for_order_issue(ctx, &rel_coin).await);
(volume, balance.to_decimal())
} else {
let balance = try_s!(
Expand Down Expand Up @@ -4779,7 +4775,7 @@ pub async fn update_maker_order(ctx: &MmArc, req: MakerOrderUpdateReq) -> Result
// Calculate order volume and add to update_msg if new_volume is found in the request
let new_volume = if req.max.unwrap_or(false) {
let max_volume = try_s!(get_max_maker_vol(ctx, &base_coin).await).volume + reserved_amount.clone();
try_s!(check_other_coin_balance_for_order_issue(ctx, &rel_coin, max_volume.to_decimal()).await);
try_s!(check_other_coin_balance_for_order_issue(ctx, &rel_coin).await);
update_msg.with_new_max_volume(max_volume.clone().into());
max_volume
} else if Option::is_some(&req.volume_delta) {
Expand Down
8 changes: 3 additions & 5 deletions mm2src/mm2_main/src/lp_swap/maker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,7 @@ impl MakerSwap {
)]))
},
};
let taker_payment_spend_trade_fee_fut = self
.taker_coin
.get_receiver_trade_fee(self.maker_amount.clone(), stage.clone());
let taker_payment_spend_trade_fee_fut = self.taker_coin.get_receiver_trade_fee(stage.clone());
let taker_payment_spend_trade_fee = match taker_payment_spend_trade_fee_fut.compat().await {
Ok(fee) => fee,
Err(e) => {
Expand Down Expand Up @@ -2149,7 +2147,7 @@ pub async fn check_balance_for_maker_swap(
.await
.mm_err(|e| CheckBalanceError::from_trade_preimage_error(e, my_coin.ticker()))?;
let taker_payment_spend_trade_fee = other_coin
.get_receiver_trade_fee(volume.to_decimal(), stage)
.get_receiver_trade_fee(stage)
.compat()
.await
.mm_err(|e| CheckBalanceError::from_trade_preimage_error(e, other_coin.ticker()))?;
Expand Down Expand Up @@ -2203,7 +2201,7 @@ pub async fn maker_swap_trade_preimage(
.await
.mm_err(|e| TradePreimageRpcError::from_trade_preimage_error(e, base_coin_ticker))?;
let rel_coin_fee = rel_coin
.get_receiver_trade_fee(volume.to_decimal(), FeeApproxStage::TradePreimage)
.get_receiver_trade_fee(FeeApproxStage::TradePreimage)
.compat()
.await
.mm_err(|e| TradePreimageRpcError::from_trade_preimage_error(e, rel_coin_ticker))?;
Expand Down
8 changes: 3 additions & 5 deletions mm2src/mm2_main/src/lp_swap/taker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,9 +971,7 @@ impl TakerSwap {
)]))
},
};
let maker_payment_spend_trade_fee_fut = self
.maker_coin
.get_receiver_trade_fee(self.taker_amount.to_decimal(), stage.clone());
let maker_payment_spend_trade_fee_fut = self.maker_coin.get_receiver_trade_fee(stage.clone());
let maker_payment_spend_trade_fee = match maker_payment_spend_trade_fee_fut.compat().await {
Ok(fee) => fee,
Err(e) => {
Expand Down Expand Up @@ -2257,7 +2255,7 @@ pub async fn check_balance_for_taker_swap(
.await
.mm_err(|e| CheckBalanceError::from_trade_preimage_error(e, my_coin.ticker()))?;
let maker_payment_spend_trade_fee = other_coin
.get_receiver_trade_fee(volume.to_decimal(), stage)
.get_receiver_trade_fee(stage)
.compat()
.await
.mm_err(|e| CheckBalanceError::from_trade_preimage_error(e, other_coin.ticker()))?;
Expand Down Expand Up @@ -2352,7 +2350,7 @@ pub async fn taker_swap_trade_preimage(
.await
.mm_err(|e| TradePreimageRpcError::from_trade_preimage_error(e, my_coin_ticker))?;
let other_coin_trade_fee = other_coin
.get_receiver_trade_fee(my_coin_volume.to_decimal(), stage.clone())
.get_receiver_trade_fee(stage.clone())
.compat()
.await
.mm_err(|e| TradePreimageRpcError::from_trade_preimage_error(e, other_coin_ticker))?;
Expand Down

0 comments on commit 6f51bf8

Please sign in to comment.