-
Notifications
You must be signed in to change notification settings - Fork 41
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
AMM-CDA-4: Integrate Hybrid Router #1277
Conversation
if !amm_amount_in.is_zero() { | ||
match tx_type { | ||
TxType::Buy => { | ||
T::Amm::buy(&who, market_id, asset, amm_amount_in, BalanceOf::<T>::zero())?; | ||
} | ||
TxType::Sell => { | ||
T::Amm::sell( | ||
&who, | ||
market_id, | ||
asset, | ||
amm_amount_in, | ||
BalanceOf::<T>::zero(), | ||
)?; | ||
} | ||
} | ||
remaining = remaining.checked_sub_res(&amm_amount_in)?; | ||
} |
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'm afraid there's a bit of a problem here which isn't covered in the ZIP. The buy
and sell
calls could fail for a variety of reasons. I think we should cover these using more detailed error handling that just question marking the result. In detail:
NumericalLimits
: Some calculation was not done due to numerical concerns. In this case, you should returnOk(remaining)
and basically "continue". This will happen if the pre-execution price is very close to the price of the first order on the book. Basically, theamount_in
will be too small for the AMM to handle. Situations like these should not keep the user from executing their trade. We could also build that into thecalculate_*_amount_until
function, but I think that would just cause more confusion.- Everything else is unreachable, provided you check that the market is active before calling this.
if !amm_amount_in.is_zero() { | |
match tx_type { | |
TxType::Buy => { | |
T::Amm::buy(&who, market_id, asset, amm_amount_in, BalanceOf::<T>::zero())?; | |
} | |
TxType::Sell => { | |
T::Amm::sell( | |
&who, | |
market_id, | |
asset, | |
amm_amount_in, | |
BalanceOf::<T>::zero(), | |
)?; | |
} | |
} | |
remaining = remaining.checked_sub_res(&amm_amount_in)?; | |
} | |
if !amm_amount_in.is_zero() { | |
let result = match tx_type { | |
TxType::Buy => { | |
T::Amm::buy(&who, market_id, asset, amm_amount_in, BalanceOf::<T>::zero())?; | |
} | |
TxType::Sell => { | |
T::Amm::sell( | |
&who, | |
market_id, | |
asset, | |
amm_amount_in, | |
BalanceOf::<T>::zero(), | |
)?; | |
} | |
}; | |
let remaining = match result { | |
Ok(_) => remaining.checked_sub_res(&amm_amount_in)?, | |
Err(AmmApiError::SoftFailure) => remaining, | |
Err(AmmApiError::HardFailure) => | |
} | |
remaining = remaining.checked_sub_res(&amm_amount_in)?; | |
} |
This requires you to add the AmmApiError
type to the AmmApi
(has two variants, hard and soft failure) and then map the neo-swaps pallet errors to hard and soft failure. Numerical failure is soft in the sense that it could happen during normal operations, whereas everything else is unexpected/"panic".
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.
Yeah, seems legit. This is a bigger suggestion. I will do this as soon as I have looked into the other review comments.
match strategy { | ||
Strategy::ImmediateOrCancel => { | ||
return Err(Error::<T>::CancelStrategyApplied.into()); | ||
} | ||
Strategy::LimitOrder => { | ||
T::OrderBook::place_order( | ||
who, | ||
market_id, | ||
maker_asset, | ||
maker_amount, | ||
taker_asset, | ||
taker_amount, | ||
)?; | ||
} | ||
} | ||
|
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.
We have a similar problem here where a user might end up with a very small remaining
amount. If their strategy is LimitOrder
, this call will then fail due to the minimum order size set in zrml-orderbook.
@maltekliemann and I decided to defer the missing review suggestion to another PR. This decision was made in order to update the APIs needed that are not part of this PR. |
Changes incorporated here. This PR was not merged as usual, because a normal merge would have caused a revert of some changes. |
What does it do?
What important points should reviewers know?
It isn't required to be compiled. Clippy is also irrelevant. Clippy will be fixed in the final result.
Is there something left for follow-up PRs?
What alternative implementations were considered?
Are there relevant PRs or issues?
References