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

SIP-399: Perps V3 Flash Loan Utility #2039

Closed
wants to merge 3 commits into from

Conversation

barrasso
Copy link
Member

This SIP proposes to deploy a new contract, PerpsFlashLoanUtil, which will allow users to close their Perps V3 positions by utilizing flash loans.

Copy link

vercel bot commented Jul 16, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sips ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 16, 2024 8:12pm

content/sips/sip-399.md Show resolved Hide resolved
* @param params Additional parameters for the flash loan operation.
* @return success True if the operation was successful, false otherwise.
*/
function executeOperation(
Copy link
Member

Choose a reason for hiding this comment

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

Can you please tell me which argument is given to ensure that sandwishes are constrained?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you please tell me which argument is given to ensure that sandwishes are constrained?

amountOutMinimum: https://github.com/Synthetixio/synthetix-v3/blob/b8f01cdaa829f7630c2064ed5717c86d7c3c3dc3/auxiliary/PerpsFlashLoanUtil/contracts/PerpsFlashLoanUtil.sol#L119

which is derived from uniswap's IQuoter.quoteExactInput:
https://github.com/Uniswap/v3-periphery/blob/main/contracts/interfaces/IQuoter.sol

Copy link
Member

Choose a reason for hiding this comment

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

ohhh this amountOutMinimum is included in calldata?

Copy link
Member Author

Choose a reason for hiding this comment

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

ohhh this amountOutMinimum is included in calldata?

No, we fetch it from the IQuoter at L119 in executeOperation:

            uint256 amountOutMinimum = quoter.quoteExactInput(
                abi.encodePacked(collateralType, poolAddress, USDC),
                unwrappedAmount
            );

And then we use it to do the swap:

            ISwapRouter.ExactInputParams memory swapParams = ISwapRouter.ExactInputParams({
               path: abi.encodePacked(collateralType, poolAddress, USDC),
               recipient: address(this),
               deadline: block.timestamp,
               amountIn: unwrappedAmount,
               amountOutMinimum: amountOutMinimum
           });
           unwrappedAmount = router.exactInput(swapParams);

Copy link
Member

@kaleb-keny kaleb-keny Jul 17, 2024

Choose a reason for hiding this comment

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

Maybe i'm not clear, but how does the front-end is able to specify a minimum amount of USDC that he expects to receive for selling his wETH/wBTC margin so that he doesn't get rugged with a sandwish... Like to protect against this, the front-end gives a user a interface to specify a maximum amount of slippage against a centralized exchange price, that he is willing to accept.. If the return on the uniswap trade yields fewer USDC against the ETH that was to be sold all of it reverts... The uni/flash quoter, which uses on-chain data, I think ensures that the transaction goes through, but does not protect the trader against a sandwish attack which involves pushing uniswap prices around.

Copy link
Member Author

@barrasso barrasso Jul 19, 2024

Choose a reason for hiding this comment

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

Maybe i'm not clear, but how does the front-end is able to specify a minimum amount of USDC that he expects to receive for selling his wETH/wBTC margin so that he doesn't get rugged with a sandwish

Ah, I see what you mean. Can we mitigate this by quoting the min amount of USDC in requestFlashLoan, and pass it to params for execution? The user can be shown the amount of USDC expected to close their position at the time of requesting.

This way, the user requests a flash loan with a quoted amount in the first block. And then if the price moves down by the following block when the execution is processed, it can compare the initially quoted amount to a new quote and revert if outside the slippage range.

Copy link
Member

@kaleb-keny kaleb-keny Jul 22, 2024

Choose a reason for hiding this comment

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

Like let's do an example:

  • User requests a 1000 usdc flash loan to repay usdc of debt, having 1 ETH margin on hand (3k$)
  • Users flash 1000 usdc,
  • Swaps ETH to 2000 usdc,
  • Repays debt
  • Repays flash

At no point in this set of transaction is the rate of swap ETH to USDC is specified. Uness we can configure the maximum amount of ETH margin is allowed to be spent somewhere, or the rate of ETH to USDC,

content/sips/sip-399.md Outdated Show resolved Hide resolved
@kaleb-keny
Copy link
Member

as per author's request

@kaleb-keny kaleb-keny closed this Jul 26, 2024
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.

2 participants