Skip to content
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
86 changes: 64 additions & 22 deletions mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2164,29 +2164,23 @@ impl MmCoin for TendermintCoin {
BigDecimal::default()
};

let msg_payload = if is_ibc_transfer {
let channel_id = match req.ibc_source_channel {
Some(channel_id) => channel_id,
None => coin.detect_channel_id_for_ibc_transfer(&to_address).await?,
};

MsgTransfer::new_with_default_timeout(channel_id, account_id.clone(), to_address.clone(), Coin {
denom: coin.denom.clone(),
amount: amount_denom.into(),
})
.to_any()
} else {
MsgSend {
from_address: account_id.clone(),
to_address: to_address.clone(),
amount: vec![Coin {
denom: coin.denom.clone(),
amount: amount_denom.into(),
}],
let channel_id = if is_ibc_transfer {
match &req.ibc_source_channel {
Some(_) => req.ibc_source_channel,
None => Some(coin.detect_channel_id_for_ibc_transfer(&to_address).await?),
}
.to_any()
}
.map_to_mm(|e| WithdrawError::InternalError(e.to_string()))?;
} else {
None
};

let msg_payload = create_withdraw_msg_as_any(
account_id.clone(),
to_address.clone(),
&coin.denom,
amount_denom,
channel_id.clone(),
)
.await?;

let memo = req.memo.unwrap_or_else(|| TX_DEFAULT_MEMO.into());

Expand Down Expand Up @@ -2214,6 +2208,18 @@ impl MmCoin for TendermintCoin {
req.fee,
)
.await?;

let fee_amount_u64 = if coin.is_keplr_from_ledger {
// When using `SIGN_MODE_LEGACY_AMINO_JSON`, Keplr ignores the fee we calculated
// and calculates another one which is usually double what we calculate.
// To make sure the transaction doesn't fail on the Keplr side (because if Keplr
// calculates a higher fee than us, the withdrawal might fail), we use three times
// the actual fee.
fee_amount_u64 * 3
} else {
fee_amount_u64
};

let fee_amount_dec = big_decimal_from_sat_unsigned(fee_amount_u64, coin.decimals());

let fee_amount = Coin {
Expand Down Expand Up @@ -2246,6 +2252,15 @@ impl MmCoin for TendermintCoin {
(sat_from_big_decimal(&req.amount, coin.decimals)?, total)
};

let msg_payload = create_withdraw_msg_as_any(
account_id.clone(),
to_address.clone(),
&coin.denom,
amount_denom,
channel_id,
)
.await?;

let account_info = coin.account_info(&account_id).await?;

let tx = coin
Expand Down Expand Up @@ -3134,6 +3149,33 @@ pub(crate) fn chain_registry_name_from_account_prefix(ctx: &MmArc, prefix: &str)
None
}

pub(crate) async fn create_withdraw_msg_as_any(
sender: AccountId,
receiver: AccountId,
denom: &Denom,
amount: u64,
ibc_source_channel: Option<String>,
) -> Result<Any, MmError<WithdrawError>> {
if let Some(channel_id) = ibc_source_channel {
MsgTransfer::new_with_default_timeout(channel_id, sender, receiver, Coin {
denom: denom.clone(),
amount: amount.into(),
})
.to_any()
} else {
MsgSend {
from_address: sender,
to_address: receiver,
amount: vec![Coin {
denom: denom.clone(),
amount: amount.into(),
}],
}
.to_any()
}
.map_to_mm(|e| WithdrawError::InternalError(e.to_string()))
}

pub async fn get_ibc_transfer_channels(
source_registry_name: String,
destination_registry_name: String,
Expand Down
47 changes: 19 additions & 28 deletions mm2src/coins/tendermint/tendermint_token.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Module containing implementation for Tendermint Tokens. They include native assets + IBC

use super::ibc::transfer_v1::MsgTransfer;
use super::ibc::IBC_GAS_LIMIT_DEFAULT;
use super::{TendermintCoin, TendermintFeeDetails, GAS_LIMIT_DEFAULT, MIN_TX_SATOSHIS, TIMEOUT_HEIGHT_DELTA,
TX_DEFAULT_MEMO};
use super::{create_withdraw_msg_as_any, TendermintCoin, TendermintFeeDetails, GAS_LIMIT_DEFAULT, MIN_TX_SATOSHIS,
TIMEOUT_HEIGHT_DELTA, TX_DEFAULT_MEMO};
use crate::coin_errors::ValidatePaymentResult;
use crate::utxo::utxo_common::big_decimal_from_sat;
use crate::{big_decimal_from_sat_unsigned, utxo::sat_from_big_decimal, BalanceFut, BigDecimal,
Expand All @@ -26,9 +25,7 @@ use common::executor::abortable_queue::AbortableQueue;
use common::executor::{AbortableSystem, AbortedError};
use common::log::warn;
use common::Future01CompatExt;
use cosmrs::{bank::MsgSend,
tx::{Fee, Msg},
AccountId, Coin, Denom};
use cosmrs::{tx::Fee, AccountId, Coin, Denom};
use futures::{FutureExt, TryFutureExt};
use futures01::Future;
use keys::KeyPair;
Expand Down Expand Up @@ -538,29 +535,23 @@ impl MmCoin for TendermintToken {
BigDecimal::default()
};

let msg_payload = if is_ibc_transfer {
let channel_id = match req.ibc_source_channel {
Some(channel_id) => channel_id,
None => platform.detect_channel_id_for_ibc_transfer(&to_address).await?,
};

MsgTransfer::new_with_default_timeout(channel_id, account_id.clone(), to_address.clone(), Coin {
denom: token.denom.clone(),
amount: amount_denom.into(),
})
.to_any()
} else {
MsgSend {
from_address: account_id.clone(),
to_address: to_address.clone(),
amount: vec![Coin {
denom: token.denom.clone(),
amount: amount_denom.into(),
}],
let channel_id = if is_ibc_transfer {
match &req.ibc_source_channel {
Some(_) => req.ibc_source_channel,
None => Some(platform.detect_channel_id_for_ibc_transfer(&to_address).await?),
}
.to_any()
}
.map_to_mm(|e| WithdrawError::InternalError(e.to_string()))?;
} else {
None
};

let msg_payload = create_withdraw_msg_as_any(
account_id.clone(),
to_address.clone(),
&token.denom,
amount_denom,
channel_id.clone(),
)
.await?;

let memo = req.memo.unwrap_or_else(|| TX_DEFAULT_MEMO.into());
let current_block = token
Expand Down