Skip to content

Conversation

@adhusson
Copy link
Contributor

@adhusson adhusson commented Sep 26, 2024

Add ParaswapModule. It has the following functions:
-buy: buy exact amount & sell up to limit amount
-sell: sell exact amount & buy at least limit amount

Context

The Paraswap module calls Paraswap's Augustus contract, which executes the trade. There is a small integration guide here.

Behavior of the Paraswap module

sell function
If the sellEntireBalance boolean is true, the sell amount will be adjusted to the current balance of the ParaswapModule. The minDestAmount will be adjusted accordingly.

buyfunction
If the loanToken of the marketParams parameter is not address(0), the buy amount will be adjusted to the current debt of the initiator on this market. The maxSrcAmount will be adjusted accordingly. This is necessary for debt swap and repay with collateral.

In-place calldata modification
The Paraswap module calls Augustus with callData. When the exact sell/buy amounts are adjusted, callData is modified in-place.

Up to 3 values are used by Paraswap:

  • The exact sell/buy amount
  • A limit on the variable amount (minimum buy amount when selling, maximum sell amount when buying)
  • A quoted amount, which is an estimate by Paraswap of the variable amount (quoted buy amount when selling, quoted sell amount when buying). Not used on every route.

All 3 values (exact amount, limit amount, and quote amount) are adjusted in callData before they are sent to Augustus. Their offsets are given by an Offsets structs that contains 3 memory offsets (see offsets). Some Paraswap routes do not use a quote amount. In that case, its offset should be 0.

Note that:

  • When selling, Augustus will require received >= limit, and take half of received - quoted (the surplus) up to .5% of the quoted amount.
  • When buying, Augustus will require received >= exact and quoted <= limit, and take half of quoted - spent (the surplus) up to .5% of the quoted amount.

Tested bundles

The tests simulate constructing and running the following bundles:

  • Withdraw collateral & swap
  • Withdraw loan token & swap
  • Supply swap (withdraw -> swap -> supply)
  • Collateral swap (withdraw collateral -> swap -> supply collateral, move debt to new market)
    • Limitation: fails if Morpho does not hold 2 * collateral to swap
  • Debt swap (borrow -> swap -> repay, move collateral to new market)
  • Repay with collateral (withdraw collateral -> swap -> repay)
    • Limitation: fails if Morpho does not hold 2 * initiator collateral

@adhusson adhusson marked this pull request as ready for review September 29, 2024 22:20
@adhusson
Copy link
Contributor Author

adhusson commented Dec 1, 2024

Merged main. I left the flashloan module action in the merge because some paraswap bundles need morpho flashloans.

…raswap-module

refactor: move buy morpho debt action to paraswap module
Co-authored-by: MathisGD <[email protected]>
Signed-off-by: Adrien Husson <[email protected]>
@adhusson adhusson requested review from MathisGD and QGarchery and removed request for MathisGD and QGarchery December 2, 2024 22:45
@adhusson adhusson merged commit 095a8f8 into main Dec 2, 2024
8 checks passed
@adhusson adhusson deleted the feat/paraswap branch December 2, 2024 22:46
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.

4 participants