Skip to content
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

Merged
merged 11 commits into from Jun 6, 2022

Conversation

borngraced
Copy link
Member

No description provided.

Copy link

@sergeyboyko0791 sergeyboyko0791 left a 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.

@@ -255,7 +255,7 @@ impl CheckBalanceError {
}
} else {
CheckBalanceError::NotSufficientBaseCoinBalance {
coin,
coin: ticker.to_string(),

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.

@@ -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()))?;

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.

@@ -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()))?;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@borngraced borngraced changed the title fixes incorrect coin type for rpc error fixes #1275 [wip] fixes incorrect coin type for rpc error fixes #1275 May 20, 2022
@borngraced borngraced changed the title [wip] fixes incorrect coin type for rpc error fixes #1275 [r2r] fixes incorrect coin type for rpc error fixes #1275 May 20, 2022
@borngraced borngraced linked an issue May 23, 2022 that may be closed by this pull request
Copy link
Collaborator

@shamardy shamardy left a 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?

@borngraced
Copy link
Member Author

LGTM 🔥! Can you please add a unit test for this case in qrc20_tests.rs?
yep!, thank you

Copy link

@sergeyboyko0791 sergeyboyko0791 left a 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

@@ -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,

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.

@@ -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>(

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

TradePreimageError::from_generate_tx_error(
e,
ticker,
coin.platform_ticker().to_string(),

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 🙂

Copy link
Member Author

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @sergeyboyko0791

Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one comment 🙂

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One change please :)

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(),

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.

@sergeyboyko0791
Copy link

sergeyboyko0791 commented May 26, 2022

Another issue related to this one...

Here we expect TradePreimageError::NotSufficientBaseCoinBalance instead of TradePreimageError::NotSufficientBalance https://github.com/KomodoPlatform/atomicDEX-API/blob/d3250f8d0c883eb377a87a56d67e1331460270c0/mm2src/coins/qrc20/qrc20_tests.rs#L742-L743

One of the possible solutions:

  • We try to generate a QTUM tx to send contract calls
  • We got GenerateTxError::NotSufficientBalance when tried to generate QTUM UTXO transaction
  • TradePreimageError::from_generate_tx_error must convert GenerateTxError::NotSufficientBalance to TradePreimageError::NotSufficientBaseCoinBalance when it's required like in QRC20/QTUM case

DelegationError::from_generate_tx_error, WithdrawError::from_generate_tx_error should handle this case the same way.

@borngraced borngraced changed the title [r2r] fixes incorrect coin type for rpc error fixes #1275 [r2r] fixes incorrect coin type for rpc error May 31, 2022
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One change please :)

Comment on lines 737 to 748
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());

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1289 (comment)

Yes, it would be great! Thank you 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@borngraced
Copy link
Member Author

borngraced commented Jun 1, 2022

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??

sergeyboyko0791
sergeyboyko0791 previously approved these changes Jun 2, 2022
Copy link

@sergeyboyko0791 sergeyboyko0791 left a 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 🙂

shamardy
shamardy previously approved these changes Jun 2, 2022
Copy link
Collaborator

@shamardy shamardy left a 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.

mm2src/coins/qrc20/qrc20_tests.rs Outdated Show resolved Hide resolved
@borngraced borngraced dismissed stale reviews from shamardy and sergeyboyko0791 via 614189a June 2, 2022 13:58
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Work 🔥!

@sergeyboyko0791 sergeyboyko0791 merged commit a499bce into dev Jun 6, 2022
@sergeyboyko0791 sergeyboyko0791 deleted the trade_preimage-rpc-bug branch June 6, 2022 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inaccurate error message returned by trade_preimage RPC
3 participants