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

Fix: FI swap correclty with asymmetric ratios #1746

Merged
merged 4 commits into from
Feb 28, 2024
Merged

Conversation

lemunozm
Copy link
Contributor

@lemunozm lemunozm commented Feb 27, 2024

Description

Description

The issue is described in this thread

When we decrease, and there is an increasing pending swap, we use the convert_by_market() to know upfront the amount we need to cancel to the inverse swap and distinguish from the amount we need to decrease the investment.
Nevertheless, later, inside the swap, convert_by_market() is called again in the inverse direction to perform the cancellation of the swap. This works fine. However, with asymmetric ratios (A -> B, ratio: 0.9; B -> A, ratio 0.95), it can lead to obtaining a different swap than expected. In the worst case, it would lead to a NoFund error if the amounts are very fit.

How it works

Now pallet-swaps offer a new method cancel_swap() which allows to cancel partially a pending amount. This way, we can cancel an inverse swap (in its currency out denomination) before applying the new swap (in the other currency denomination). Saving us from using convert_by_market() in the opposite direction.

@lemunozm lemunozm added I2-bug The code fails to follow expected behaviour. P7-asap Issue should be addressed in the next days. crcl-protocol Circle protocol related. labels Feb 27, 2024
@lemunozm lemunozm added this to the Centrifuge 1025 milestone Feb 27, 2024
@lemunozm lemunozm self-assigned this Feb 27, 2024
Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

I have a question about the new 2 FI tests. Code looks great as always!

libs/traits/src/swaps.rs Outdated Show resolved Hide resolved
Comment on lines +1115 to +1122
util::fulfill_last_swap(Action::Investment, foreign_to_pool(3 * AMOUNT / 4));

assert_eq!(
ForeignInvestmentInfo::<Runtime>::get(&USER, INVESTMENT_ID),
None,
);
assert_eq!(ForeignInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
assert_eq!(MockInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does the foreign investment vanish when we fulfill only 3/4 of the order? Shouldn't there be 1/4 left?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that we only fulfill the swap with the amount that was previously swapped in the opposite direction, which is 3/4. The 1/4 left has never been swapped, so when we decrease, such 1/4 is canceled.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes of course, thanks for the explanation!

Comment on lines +1170 to +1180
util::fulfill_last_swap(
Action::Investment,
foreign_to_pool(3 * AMOUNT / 4) * MULTIPLIER,
);

assert_eq!(
ForeignInvestmentInfo::<Runtime>::get(&USER, INVESTMENT_ID),
None,
);
assert_eq!(ForeignInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
assert_eq!(MockInvestment::investment(&USER, INVESTMENT_ID), Ok(0));
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same applies here

Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

Clear as a crystal ball!

@lemunozm lemunozm merged commit d0698c3 into main Feb 28, 2024
9 checks passed
@lemunozm lemunozm deleted the fix/asymmetric-ratios branch February 28, 2024 15:37
wischli pushed a commit that referenced this pull request Feb 29, 2024
* fix by cancellation

* noop if currencies are different

* fix typos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crcl-protocol Circle protocol related. I2-bug The code fails to follow expected behaviour. P7-asap Issue should be addressed in the next days.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants