New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[r2r] fixes incorrect coin type for rpc error #1289
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the error occurs somewhere TradePreimageError::NotSufficientBalance
is constructed. Please try to figure out where exactly it happens.
mm2src/lp_swap/check_balance.rs
Outdated
@@ -255,7 +255,7 @@ impl CheckBalanceError { | |||
} | |||
} else { | |||
CheckBalanceError::NotSufficientBaseCoinBalance { | |||
coin, | |||
coin: ticker.to_string(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TradePreimageError::NotSufficientBalance::coin
is the coin, the balance of which is not enough.
So I think the problem is in the place where the TradePreimageError
error is constructed.
For example, if coin is QRC20 and there are no QTUM enough, TradePreimageError::NotSufficientBalance::coin
has to be QTUM
.
mm2src/lp_swap/maker_swap.rs
Outdated
@@ -1865,7 +1865,7 @@ pub async fn maker_swap_trade_preimage( | |||
let base_coin_fee = base_coin | |||
.get_sender_trade_fee(preimage_value, FeeApproxStage::TradePreimage) | |||
.await | |||
.mm_err(|e| TradePreimageRpcError::from_trade_preimage_error(e, base_coin_ticker))?; | |||
.mm_err(|e| TradePreimageRpcError::from_trade_preimage_error(e, base_coin.platform_ticker()))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you can see, TradePreimageRpcError::from_trade_preimage_error
takes the coin ticker at this case only:
TradePreimageError::AmountIsTooSmall { amount, threshold } => CheckBalanceError::VolumeTooLow {
coin: ticker.to_owned(),
volume: amount,
threshold,
},
The ticker is needed here because TradePreimageError::AmountIsTooSmall
doesn't provide it.
base_coin.platform_ticker()
is not correct since we called base_coin.get_sender_trade_fee
for base_coin
, not for a platform coin of base_coin
.
mm2src/lp_swap/maker_swap.rs
Outdated
@@ -1934,7 +1934,7 @@ pub async fn calc_max_maker_vol( | |||
let trade_fee = coin | |||
.get_sender_trade_fee(preimage_value, stage) | |||
.await | |||
.mm_err(|e| CheckBalanceError::from_trade_preimage_error(e, ticker))?; | |||
.mm_err(|e| CheckBalanceError::from_trade_preimage_error(e, coin.platform_ticker()))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🔥! Can you please add a unit test for this case in qrc20_tests.rs
?
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider my solution
mm2src/coins/lp_coins.rs
Outdated
@@ -1127,7 +1128,7 @@ impl TradePreimageError { | |||
let available = big_decimal_from_sat_unsigned(sum_utxos, decimals); | |||
let required = big_decimal_from_sat_unsigned(required, decimals); | |||
TradePreimageError::NotSufficientBalance { | |||
coin, | |||
coin: platform_ticker, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, platform_ticker
needs to be used instead of coin
in all cases, not in GenerateTxError::NotEnoughUtxos
only.
Also I think it's not really common case when a UTXO coin is a platform coin. Currently it's true for QRC20
and SLP
tokens.
Summing up, I think we don't need to take platform_ticker
and coin
both. But we need to pass coin.platform_ticker()
to the TradePreimageError:: from_generate_tx_error
function instead of coin.ticker()
when it's required, i.e. for QRC20
and SLP
tokens.
mm2src/coins/utxo/utxo_common.rs
Outdated
@@ -2635,7 +2635,7 @@ pub fn get_trade_fee<T: UtxoCommonOps>(coin: T) -> Box<dyn Future<Item = TradeFe | |||
/// | |||
/// To sum up, `get_sender_trade_fee(TradePreimageValue::Exact(9000)) > get_sender_trade_fee(TradePreimageValue::Exact(10000))`. | |||
/// So we should always return a fee as if a transaction includes the change output. | |||
pub async fn preimage_trade_fee_required_to_send_outputs<T>( | |||
pub async fn preimage_trade_fee_required_to_send_outputs<T: MarketCoinOps>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First, we already have trait bounds for the generic T
type:
where
T: UtxoCommonOps + GetUtxoListOps,
It had to be there.
Secondly, T: MarketCoinOps
is not required due to the next comment
mm2src/coins/utxo/utxo_common.rs
Outdated
TradePreimageError::from_generate_tx_error( | ||
e, | ||
ticker, | ||
coin.platform_ticker().to_string(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said in the first comment, TradePreimageError::from_generate_tx_error
doesn't need to take platform_coin
.
But we need to pass a correct ticker here. So this is what I consider as a solution:
I'd add the ticker
additional argument to the utxo_common:: preimage_trade_fee_required_to_send_outputs
function. The ticker
argument should be used instead of
https://github.com/KomodoPlatform/atomicDEX-API/blob/4366141c8a7a5d6d3abac87df8827f7cae53d11c/mm2src/coins/utxo/utxo_common.rs#L2648
So you need to find all places where we need to pass coin.platform_ticker()
instead of coin.ticker()
to the utxo_common:: preimage_trade_fee_required_to_send_outputs
function. And this will be a solution.
If you have other ideas, please feel free to share them to discuss 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes cool, this is also a very good solution from my side ...thoughts @shamardy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @sergeyboyko0791
…into trade_preimage-rpc-bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only one comment 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One change please :)
mm2src/coins/utxo/bch.rs
Outdated
utxo_common::preimage_trade_fee_required_to_send_outputs(self, outputs, fee_policy, gas_fee, stage).await | ||
utxo_common::preimage_trade_fee_required_to_send_outputs( | ||
self, | ||
&self.ticker().to_string(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to take &str
instead of &String
. Then you can avoid casting self.ticker(): &str
to String
.
Another issue related to this one... Here we expect One of the possible solutions:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One change please :)
mm2src/coins/qrc20/qrc20_tests.rs
Outdated
let actual = block_on(coin.get_sender_trade_fee(TradePreimageValue::Exact(0.into()), FeeApproxStage::OrderIssue)) | ||
.err() | ||
.unwrap() | ||
.get_inner() | ||
.to_string(); | ||
// expecting TradePreimageError::NotSufficientBalance | ||
let expected = TradePreimageError::NotSufficientBalance { | ||
coin: "tQTUM".to_string(), | ||
available: BigDecimal::from_str("0").unwrap(), | ||
required: BigDecimal::from_str("0.08").unwrap(), | ||
}; | ||
assert_eq!(expected.to_string(), actual.to_string()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest comparing two TradePreimageError
errors instead of strings.
In order to do it, you need to add PartialEq
to the derivings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it would be great! Thank you 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
For the tests, I always try to follow the code style..(if similar test already exits) Currently there's a similar test that compares PreimageError too so what if I also refactor that so they both follow a similar pattern instead of having different pattern with same outputs?? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Thank you for the fixes!
@shamardy please review it 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Work 🚀 ! Only one non-blocker comment/change needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Work 🔥!
No description provided.