Skip to content

Instantly share code, notes, and snippets.

@tonisives
Created December 9, 2023 03:16
Show Gist options
  • Save tonisives/25844567462a5454da0645c7cace65a9 to your computer and use it in GitHub Desktop.
Save tonisives/25844567462a5454da0645c7cace65a9 to your computer and use it in GitHub Desktop.
hide all comments fold lines
regex:
"hideComments.regex": [
{
"regex": "^(\\s+|)\/\/.*\\n",
"flags": "igm",
},
{
"regex": "\/\\*(.|\\n)+?\\*\/",
"flags": "igm"
}
],
sample code
/*//////////////////////////////////////////////////////////////
AMM INTERACTION AND POSITION UPDATE HELPERS
//////////////////////////////////////////////////////////////*/
/// @notice Helper that checks the proposed option position and size and forwards the minting and potential swapping tasks.
/// @notice This helper function checks:
/// @notice - that the `tokenId` is valid
/// @notice - confirms that the Uniswap pool exists
/// @notice - retrieves Uniswap pool info
/// @notice and then forwards the minting/burning/swapping to another internal helper functions which perform the AMM pool actions.
/// ┌───────────────────────┐
/// │mintTokenizedPosition()├───┐
/// └───────────────────────┘ │
/// │
/// ┌───────────────────────┐ │ ┌───────────────────────────────┐
/// │burnTokenizedPosition()├───────► _validateAndForwardToAMM(...) ├─ (...) --> (mint/burn in AMM)
/// └───────────────────────┘ └───────────────────────────────┘
/// @param tokenId the option position
/// @param positionSize the size of the position to create
/// @param tickLimitLow lower limits on potential slippage
/// @param tickLimitHigh upper limits on potential slippage
/// @param isBurn is equal to false for mints and true for burns
/// @return totalCollectedFromAMM the total amount of funds collected from Uniswap
/// @return totalMoved the total amount of funds swapped in Uniswap as part of building potential ITM positions
/// @return newTick the tick *after* the mint+swap
function _validateAndForwardToAMM(
uint256 tokenId,
uint128 positionSize,
int24 tickLimitLow,
int24 tickLimitHigh,
bool isBurn
) internal returns (int256 totalCollectedFromAMM, int256 totalMoved, int24 newTick) {
// Reverts if positionSize is 0 and user did not own the position before minting/burning
if (positionSize == 0) revert Errors.OptionsBalanceZero();
/// @dev the flipToBurnToken() function flips the isLong bits
if (isBurn) {
tokenId = tokenId.flipToBurnToken();
}
// Validate tokenId
// Extract univ3pool from the poolId map to Uniswap Pool
IUniswapV3Pool univ3pool = s_poolContext[tokenId.validate()].pool;
// Revert if the pool not been previously initialized
if (univ3pool == IUniswapV3Pool(address(0))) revert Errors.UniswapPoolNotInitialized();
bool swapAtMint;
{
if (tickLimitLow > tickLimitHigh) {
swapAtMint = true;
(tickLimitLow, tickLimitHigh) = (tickLimitHigh, tickLimitLow);
}
}
// initialize some variables returned by the _createPositionInAMM function
int256 itmAmounts;
{
// calls a function that loops through each leg of tokenId and mints/burns liquidity in Uni v3 pool
(totalMoved, totalCollectedFromAMM, itmAmounts) = _createPositionInAMM(
univ3pool,
tokenId,
positionSize,
isBurn
);
}
// if the in-the-money amount is not zero (i.e. positions were minted ITM) and the user did provide tick limits LOW > HIGH, then swap necessary amounts
if ((itmAmounts != 0) && (swapAtMint)) {
totalMoved = swapInAMM(univ3pool, itmAmounts).add(totalMoved);
}
// Get the current tick of the Uniswap pool, check slippage
(, newTick, , , , , ) = univ3pool.slot0();
if ((newTick >= tickLimitHigh) || (newTick <= tickLimitLow)) revert Errors.PriceBoundFail();
return (totalCollectedFromAMM, totalMoved, newTick);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment