diff --git a/foundry.toml b/foundry.toml index 7fc5ad0f..ca26581c 100644 --- a/foundry.toml +++ b/foundry.toml @@ -2,6 +2,7 @@ remappings = [ "solmate/=lib/solstat/lib/solmate/src/", "solstat/=lib/solstat/src/", + "pendle/=lib/pendle-core-v2-public/contracts/" ] solc_version = "0.8.22" diff --git a/src/CoveredCall/CoveredCall.sol b/src/CoveredCall/CoveredCall.sol index 666591b0..198708dc 100644 --- a/src/CoveredCall/CoveredCall.sol +++ b/src/CoveredCall/CoveredCall.sol @@ -17,7 +17,7 @@ import { decodeControllerUpdate } from "src/CoveredCall/CoveredCallUtils.sol"; import { EPSILON } from "src/lib/StrategyLib.sol"; -import "forge-std/console2.sol"; +import { IStandardizedYield } from "pendle/interfaces/IStandardizedYield.sol"; enum UpdateCode { Invalid, @@ -191,8 +191,6 @@ contract CoveredCall is PairStrategy { int256 computedInvariant = tradingFunction(pool.reserves, computedL, params); - console2.log("Computed Invariant: {}", computedInvariant); - if (computedInvariant < 0 || computedInvariant > EPSILON) { revert InvalidComputedLiquidity(computedInvariant); } diff --git a/src/SYCoveredCall/README.md b/src/SYCoveredCall/README.md new file mode 100644 index 00000000..af6daa61 --- /dev/null +++ b/src/SYCoveredCall/README.md @@ -0,0 +1,172 @@ +# Log Normal Market Maker +This will be all the background needed to understand the `LogNormal` DFMM. + +## Conceptual Overview +The `LogNormal` DFMM provides the LP with a a log-normal shaped liquidity distribution centered around a price $\mu$ with a width given by $\sigma$. + +Note that this strategy can be made time-dependent by an additional $\tau$ parameter that is the time til the pool will "expire". +In this case, the LN trading function provides the LP with a payoff that is equivalent to a Black-Scholes covered call option with strike $K = \mu$, implied volatility $\sigma$, and time to expiration $\tau$. +We do not cover this explicitly here. + +## Core +We mark reserves as: +- $x \equiv \mathtt{rX}$ +- $y \equiv \mathtt{rY}$ + +`LogNormal` has two variable parameters: +- $\mu \equiv \mathtt{mean}$ +- $\sigma \equiv \mathtt{width}$ +- These parameters must satisfy: +$$\mu > 0\\ +\sigma > 0$$ + +The trading function for this DFMM is given by +$$\begin{equation} +\boxed{\varphi(x,y,L;\mu,\sigma) = \Phi^{-1}\left(\frac{x}{L}\right)+\Phi^{-1}\left(\frac{y}{\mu L}\right)+\sigma} +\end{equation}$$ +where $L$ is the **liquidity** of the pool. + +Given the domain of $\Phi^{-1}$ ([inverse Gaussian CDF](https://en.wikipedia.org/wiki/Normal_distribution)) we can see that $x\in [0,L]$ and $y\in [0,\mu L]$. +As the pool's liquidity increases, the maximal amount of each reserve increases and both are scaled by the same factor, which is also how we decide how to compute fees. + +## Useful Notation +We will use the following notation: +$$\begin{equation} +d_1(S;\mu,\sigma) = \frac{\ln\frac{S}{\mu}+\frac{1}{2}\sigma^2 }{\sigma} +\end{equation} +$$ +$$ +\begin{equation} +d_2(S;\mu,\sigma) = \frac{\ln\frac{S}{\mu}-\frac{1}{2}\sigma^2 }{\sigma} +\end{equation} +$$ + +## Price +We can provide the price of the pool given either of the reserves: +$$\begin{equation} +\boxed{P_X(x, L; \mu, \sigma) = \mu \exp\left(\Phi^{-1} \left(1 - \frac{x}{L}\right) \sigma - \frac{1}{2} \sigma^2 \right)} +\end{equation}$$ + +$$\begin{equation} +\boxed{P_Y(y, L; \mu, \sigma) = \mu \exp\left(\Phi^{-1} \left(\frac{y}{\mu L}\right) \sigma + \frac{1}{2} \sigma^2 \right)} +\end{equation}$$ + +Note that other DFMMs such as the `GeometricMean` have a price that can be determined from both reserves at once, so we typically do not write $P_X$ and $P_Y$. + +## Pool initialization +When the pool is initialized, we need to determine the value of $L$ and the other reserve. +The user will provide a price $S_0$ and an amount $x_0$ or an amount of $y_0$ that they wish to tender and we can get the other reserve and $L$ from the trading function. + +We can recall that get that: +$$\begin{equation} +\frac{x}{L} = 1-\Phi((d_1(S;\mu,\sigma)) +\end{equation}$$ +and +$$\begin{equation} +\frac{y}{\mu L} = \Phi(d_2(S;\mu,\sigma)) +\end{equation}$$ + +### Given $x$ and price +Suppose that the user specifies the amount $x_0$ they wish to allocate and they also choose a price $S_0$. +We first get $L_0$ using (6): +$$\begin{equation} +\boxed{L_0 = \frac{x}{1-\Phi(d_1(S;\mu,\sigma))}} +\end{equation}$$ +From this, we can get the amount $y_0$ +$$ +\boxed{y_0 = \mu L_0 \Phi(d_2(S;\mu,\sigma, \tau))} +$$ + + +### Given $y$ and price +The work here is basically a mirrored image of the above. +We get $L_0$: +$$\begin{equation} +\boxed{L_0 = \frac{y}{\mu\Phi(d_2(S;\mu,\sigma))}} +\end{equation}$$ +Suppose that the user specifies the amount $y$ they wish to allocate and they also choose a price $S$. +Now we need to get $x$: +$$\boxed{x_0 = L_0 \left(1-\Phi\left(d_1(S;\mu,\sigma)\right)\right)}$$ + +## Allocations and Deallocations +Allocations and deallocations should not change the price of a pool, and hence the ratio of reserves cannot change while increasing liquidity the correct amount. + +**Input $\Delta_X$:** If a user wants to allocate a specific amount of $\Delta_X$, then it must be that: +$$ +\frac{x}{L} = \frac{x+\Delta_X}{L+\Delta_L} +$$ +which yields: +$$ +\boxed{\Delta_L = L \frac{\Delta_X}{x}} +$$ +Then it must be that +$$ +\boxed{\Delta_Y = y\frac{\Delta_X}{x}} +$$ + +**Input $\Delta_Y$:** To allocate a specific amount of $\Delta_Y$, then it must be that: +$$ +\frac{y}{\mu L} = \frac{y+\Delta_Y}{\mu(L+\Delta_L)} +$$ +which yields: +$$ +\boxed{\Delta_L = L \frac{\Delta_Y}{y}} +$$ +and we likewise get +$$ +\boxed{\Delta_X = x\frac{\Delta_Y}{y}} +$$ + +## Swaps +We require that the trading function remain invariant when a swap is applied, that is: +$$\Phi^{-1}\left(\frac{x+\Delta_X}{L + \Delta_L}\right)+\Phi^{-1}\left(\frac{y}{\mu (L + \Delta_L)}\right)+\sigma = 0$$ +where either $\Delta_X$ or $\Delta_Y$ is given by user input and the $\Delta_L$ comes from fees. + +### Trade in $\Delta_X$ for $\Delta_Y$ +If we want to trade in $\Delta_X$ for $\Delta_Y$, +we first accumulate fees by taking +$$ +\textrm{Fees} = (1-\gamma) \Delta_X. +$$ +Then, we treat these fees as an allocation, therefore: +$$ +\boxed{\Delta_L = \frac{P}{Px +y}L\frac{(1-\gamma)\Delta_X}{x}} +$$ +where $P$ is the price of token $X$ quoted by the pool itself (i.e., using $P_X$ or $P_Y$ in Eq. (4) or (5) above). +Then we can use our invariant equation and solve for $\Delta_Y$ in terms of $\Delta_X$ to get: +$$\boxed{\Delta_Y = \mu (L+\Delta_L)\cdot\Phi\left(-\sigma-\Phi^{-1}\left(\frac{x+\Delta_X}{L+\Delta_L}\right)\right)-y}$$ + +### Trade in $\Delta_Y$ for $\Delta_X$ +If we want to trade in $\Delta_X$ for $\Delta_Y$, +we first accumulate fees by taking +$$ +\boxed{\Delta_L = L\frac{(1-\gamma)\Delta_X}{Px +y}} +$$ +Then we can use our invariant equation and solve for $\Delta_X$ in terms of $\Delta_Y$ to get: +$$ +\boxed{\Delta_X = (L+\Delta_L)\cdot\Phi\left(-\sigma-\Phi^{-1}\left(\frac{y+\Delta_Y}{\mu(L+\Delta_L)}\right)\right)-x} +$$ + +## Value Function on $L(S)$ +Relate to value on $V(L,S)$ and $V(x,y)$. +Then we can use this to tokenize. We have $L_X(x, S)$ and $L_Y(y, S)$. +We know that: +$$V = Sx + y$$ +We can get the following from the trading function: +$$ +x = LS\cdot\left(1-\Phi\left(\frac{\ln\frac{S}{\mu}+\frac{1}{2}\sigma^2}{\sigma}\right)\right)\\ +y = \mu\cdot L\cdot \Phi\left(\frac{\ln\frac{S}{\mu}-\frac{1}{2}\sigma^2}{\sigma}\right) +$$ +Therefore: +$$ +\boxed{V(L,S) = L\left( S\cdot\left(1-\Phi\left(\frac{\ln\frac{S}{\mu}+\frac{1}{2}\sigma^2}{\sigma}\right)\right) + \mu\cdot \Phi\left(\frac{\ln\frac{S}{\mu}-\frac{1}{2}\sigma^2}{\sigma}\right)\right)} +$$ + +### Time Dependence +Note that $L$ effectively changes as parameters of the trading function change. +To see this, note that the trading function must always satisfy: +$$\Phi^{-1}\left(\frac{x}{L}\right)+\Phi^{-1}\left(\frac{y}{ +\mu L}\right) + \sigma = 0.$$ +For new parameters $\mu'$ and $\sigma'$ we must find an $L'$ so that the trading function is satisfied: +$$\Phi^{-1}\left(\frac{x}{L'}\right)+\Phi^{-1}\left(\frac{y}{\mu'L'}\right) + \sigma' = 0.$$ +We can find this new $L'$ using a root finding algorithm. \ No newline at end of file diff --git a/src/SYCoveredCall/SYCoveredCall.sol b/src/SYCoveredCall/SYCoveredCall.sol new file mode 100644 index 00000000..e44370d9 --- /dev/null +++ b/src/SYCoveredCall/SYCoveredCall.sol @@ -0,0 +1,320 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pragma solidity 0.8.22; + +import { Pool } from "src/interfaces/IDFMM.sol"; +import { PairStrategy, IStrategy } from "src/PairStrategy.sol"; +import { IDFMM } from "src/interfaces/IDFMM.sol"; +import { DynamicParamLib, DynamicParam } from "src/lib/DynamicParamLib.sol"; +import { FixedPointMathLib } from "solmate/utils/FixedPointMathLib.sol"; +import { + computeTradingFunction, + computeDeltaGivenDeltaLRoundUp, + computeDeltaGivenDeltaLRoundDown, + computeDeltaLXIn, + computeDeltaLYIn, + computeTau +} from "src/SYCoveredCall/SYCoveredCallMath.sol"; +import { + decodeFeeUpdate, + decodeControllerUpdate +} from "src/SYCoveredCall/SYCoveredCallUtils.sol"; +import { EPSILON } from "src/lib/StrategyLib.sol"; +import { IPPrincipalToken } from "pendle/interfaces/IPPrincipalToken.sol"; +import { IStandardizedYield } from "pendle/interfaces/IStandardizedYield.sol"; +import { IPYieldToken } from "pendle/interfaces/IPYieldToken.sol"; + +enum UpdateCode { + Invalid, + SwapFee, + Controller +} + +struct InternalParams { + uint256 meanAnchor; + uint256 mean; + uint256 width; + uint256 maturity; + + uint256 swapFee; + address controller; + + IStandardizedYield SY; + IPPrincipalToken PT; + IPYieldToken YT; +} + +/// @dev Parameterization of the Log Normal curve. +struct SYCoveredCallParams { + uint256 meanAnchor; + uint256 mean; + uint256 width; + uint256 maturity; + + uint256 swapFee; + address controller; + + uint256 timestamp; + + IStandardizedYield SY; + IPPrincipalToken PT; + IPYieldToken YT; +} + +/// @dev Thrown when the mean parameter is not within the allowed bounds. +error InvalidMean(); + +/// @dev Thrown when the width parameter is not within the allowed bounds. +error InvalidWidth(); + +/// @dev Thrown when the maturity parameter is not later than the current block.timestamp. +error InvalidMaturity(); + +/// @dev Thrown when the pool SY token is not associated with the pool PT token. +error InvalidPair(); + +/// @dev Thrown when meanAnchor <= ONE. +error InvalidMeanAnchor(); + +/// @dev Thrown when the computedL passed to swap does not satisfy the invariant check +error InvalidComputedLiquidity(int256 invariant); + +uint256 constant MIN_WIDTH = 1; +uint256 constant MAX_WIDTH = uint256(type(int256).max); +uint256 constant MIN_MEAN = 1; +uint256 constant MAX_MEAN = uint256(type(int256).max); + +/** + * @title SYCoveredCall Strategy for DFMM. + * @author Primitive + */ +contract SYCoveredCall is PairStrategy { + using FixedPointMathLib for int256; + /// @inheritdoc IStrategy + string public constant override name = "SYCoveredCall"; + + mapping(uint256 => InternalParams) public internalParams; + + /// @param dfmm_ Address of the DFMM contract. + constructor(address dfmm_) PairStrategy(dfmm_) { } + + /// @inheritdoc IStrategy + function init( + address, + uint256 poolId, + Pool calldata pool, + bytes calldata data + ) + public + onlyDFMM + returns ( + bool valid, + int256 invariant, + uint256[] memory reserves, + uint256 totalLiquidity + ) + { + SYCoveredCallParams memory params; + + (reserves, totalLiquidity, params) = + abi.decode(data, (uint256[], uint256, SYCoveredCallParams)); + + IStandardizedYield SY = IStandardizedYield(pool.tokens[1]); + IPPrincipalToken PT = IPPrincipalToken(pool.tokens[1]); + params.timestamp = block.timestamp; + + int256 tau = int256(computeTau(params)); + + if (PT.SY() != address(SY)) { + revert InvalidPair(); + } + + if (PT.expiry() <= block.timestamp) { + revert InvalidMaturity(); + } + + if (params.meanAnchor <= 1 ether) { + revert InvalidMeanAnchor(); + } + + if (pool.reserves.length != 2 || reserves.length != 2) { + revert InvalidReservesLength(); + } + + internalParams[poolId].SY = SY; + internalParams[poolId].PT = PT; + internalParams[poolId].YT = IPYieldToken(PT.YT()); + + internalParams[poolId].maturity = internalParams[poolId].PT.expiry(); + internalParams[poolId].meanAnchor = params.meanAnchor; + internalParams[poolId].mean = uint256(int256(params.meanAnchor).powWad(tau)); + internalParams[poolId].width = params.width; + internalParams[poolId].swapFee = params.swapFee; + internalParams[poolId].controller = params.controller; + + invariant = + tradingFunction(reserves, totalLiquidity, abi.encode(params)); + valid = invariant >= 0 && invariant <= EPSILON; + } + + /// @inheritdoc IStrategy + function update( + address sender, + uint256 poolId, + Pool calldata, + bytes calldata data + ) external onlyDFMM { + if (sender != internalParams[poolId].controller) revert InvalidSender(); + UpdateCode updateCode = abi.decode(data, (UpdateCode)); + if (updateCode == UpdateCode.SwapFee) { + internalParams[poolId].swapFee = decodeFeeUpdate(data); + } else if (updateCode == UpdateCode.Controller) { + internalParams[poolId].controller = decodeControllerUpdate(data); + } else { + revert InvalidUpdateCode(); + } + } + + /// @inheritdoc IStrategy + function getPoolParams(uint256 poolId) + public + view + override + returns (bytes memory) + { + SYCoveredCallParams memory params; + + params.width = internalParams[poolId].width; + params.mean = internalParams[poolId].mean; + params.swapFee = internalParams[poolId].swapFee; + params.maturity = internalParams[poolId].maturity; + params.timestamp = IDFMM(dfmm).pools(poolId).lastSwapTimestamp; + + return abi.encode(params); + } + + /// @inheritdoc IStrategy + function validateSwap( + address, + uint256 poolId, + Pool memory pool, + bytes memory data + ) + external + view + override + returns ( + bool valid, + int256 invariant, + uint256 tokenInIndex, + uint256 tokenOutIndex, + uint256 amountIn, + uint256 amountOut, + uint256 deltaLiquidity + ) + { + bytes memory params = getPoolParams(poolId); + uint256 computedL; + (tokenInIndex, tokenOutIndex, amountIn, amountOut, computedL) = + abi.decode(data, (uint256, uint256, uint256, uint256, uint256)); + + int256 computedInvariant = + tradingFunction(pool.reserves, computedL, params); + + if (computedInvariant < 0 || computedInvariant > EPSILON) { + revert InvalidComputedLiquidity(computedInvariant); + } + + deltaLiquidity = _computeSwapDeltaLiquidity( + pool, params, tokenInIndex, tokenOutIndex, amountIn, amountOut + ); + + pool.reserves[tokenInIndex] += amountIn; + pool.reserves[tokenOutIndex] -= amountOut; + + invariant = + tradingFunction(pool.reserves, computedL + deltaLiquidity, params); + + valid = invariant >= 0; + //valid = invariant >= 0 && invariant <= EPSILON; + } + + /// @inheritdoc IStrategy + function tradingFunction( + uint256[] memory reserves, + uint256 totalLiquidity, + bytes memory params + ) public pure override returns (int256) { + SYCoveredCallParams memory poolParams = + abi.decode(params, (SYCoveredCallParams)); + return computeTradingFunction( + reserves[0], reserves[1], totalLiquidity, poolParams + ); + } + + /// @inheritdoc PairStrategy + function _computeAllocateDeltasGivenDeltaL( + uint256 deltaLiquidity, + Pool memory pool, + bytes memory + ) internal pure override returns (uint256[] memory) { + uint256[] memory deltas = new uint256[](2); + + deltas[0] = computeDeltaGivenDeltaLRoundUp( + pool.reserves[0], deltaLiquidity, pool.totalLiquidity + ); + + deltas[1] = computeDeltaGivenDeltaLRoundUp( + pool.reserves[1], deltaLiquidity, pool.totalLiquidity + ); + + return deltas; + } + + /// @inheritdoc PairStrategy + function _computeDeallocateDeltasGivenDeltaL( + uint256 deltaLiquidity, + Pool memory pool, + bytes memory + ) internal pure override returns (uint256[] memory) { + uint256[] memory deltas = new uint256[](2); + + deltas[0] = computeDeltaGivenDeltaLRoundDown( + pool.reserves[0], deltaLiquidity, pool.totalLiquidity + ); + + deltas[1] = computeDeltaGivenDeltaLRoundDown( + pool.reserves[1], deltaLiquidity, pool.totalLiquidity + ); + return deltas; + } + + function _computeSwapDeltaLiquidity( + Pool memory pool, + bytes memory params, + uint256 tokenInIndex, + uint256, + uint256 amountIn, + uint256 + ) internal pure override returns (uint256) { + SYCoveredCallParams memory poolParams = + abi.decode(params, (SYCoveredCallParams)); + + if (tokenInIndex == 0) { + return computeDeltaLXIn( + amountIn, + pool.reserves[0], + pool.reserves[1], + pool.totalLiquidity, + poolParams + ); + } + + return computeDeltaLYIn( + amountIn, + pool.reserves[0], + pool.reserves[1], + pool.totalLiquidity, + poolParams + ); + } +} diff --git a/src/SYCoveredCall/SYCoveredCallMath.sol b/src/SYCoveredCall/SYCoveredCallMath.sol new file mode 100644 index 00000000..9894d5e5 --- /dev/null +++ b/src/SYCoveredCall/SYCoveredCallMath.sol @@ -0,0 +1,491 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pragma solidity ^0.8.13; + +import { FixedPointMathLib } from "solmate/utils/FixedPointMathLib.sol"; +import { SignedWadMathLib } from "src/lib/SignedWadMath.sol"; +import { ONE, HALF } from "src/lib/StrategyLib.sol"; +import { SYCoveredCallParams } from "src/SYCoveredCall/SYCoveredCall.sol"; +import { Gaussian } from "solstat/Gaussian.sol"; +import { toUint } from "src/SYCoveredCall/SYCoveredCallUtils.sol"; +import { bisection } from "src/lib/BisectionLib.sol"; +import "forge-std/console2.sol"; + +using FixedPointMathLib for uint256; +using FixedPointMathLib for int256; +using SignedWadMathLib for int256; + +uint256 constant MAX_ITER = 256; +uint256 constant YEAR = 31_536_000; + +function computeTradingFunction( + uint256 rX, + uint256 rY, + uint256 L, + SYCoveredCallParams memory params +) pure returns (int256) { + int256 a = Gaussian.ppf(int256(rX.divWadUp(L))); + int256 b = Gaussian.ppf(int256(rY.divWadUp(L.mulWadUp(params.mean)))); + uint256 tau = computeTau(params); + int256 c = int256(computeSigmaSqrtTau(params.width, tau)); + return a + b + c; +} + +function computeTau(SYCoveredCallParams memory params) pure returns (uint256) { + if (params.timestamp >= params.maturity) { + return 0; + } else { + return ONE * (params.maturity - params.timestamp) / YEAR; + } +} +function computeDeltaGivenDeltaLRoundUp( + uint256 reserve, + uint256 deltaLiquidity, + uint256 totalLiquidity +) pure returns (uint256) { + return reserve.mulDivUp(deltaLiquidity, totalLiquidity); +} + +function computeDeltaGivenDeltaLRoundDown( + uint256 reserve, + uint256 deltaLiquidity, + uint256 totalLiquidity +) pure returns (uint256) { + return reserve.mulDivDown(deltaLiquidity, totalLiquidity); +} + +function computeLnSDivMean( + uint256 S, + uint256 mean +) pure returns (int256 lnSDivMean) { + lnSDivMean = int256(S.divWadUp(mean)).lnWad(); +} + +/** + * @dev Computes the half of the square of sigma. + * + * $$\frac{1}{2}\sigma^2$$ + * + */ +function computeHalfSigmaSquaredTau( + uint256 sigma, + uint256 tau +) pure returns (uint256) { + uint256 innerTerm = sigma.mulWadDown(sigma).mulWadDown(tau); + return HALF.mulWadDown(innerTerm); +} + +function computeSigmaSqrtTau( + uint256 sigma, + uint256 tau +) pure returns (uint256 sigmaSqrtTau) { + uint256 sqrtTau = FixedPointMathLib.sqrt(tau) * 10 ** 9; + sigmaSqrtTau = sigma.mulWadUp(sqrtTau); +} + +/** + * @dev Computes reserves L given rx, S. + * + * $$L_0 = \frac{x}{1-\Phi(d_1(S;\mu,\sigma))}$$ + * + * @param rx The reserve of x. + * @param S The price of X in Y, in WAD units. + * @param params LogNormParameters of the Log Normal distribution. + * @return L The liquidity given rx, S + */ +function computeLGivenX( + uint256 rx, + uint256 S, + SYCoveredCallParams memory params +) pure returns (uint256 L) { + int256 d1 = computeD1({ S: S, params: params }); + uint256 cdf = toUint(Gaussian.cdf(d1)); + + L = rx.divWadUp(ONE - cdf); +} + +function computeLGivenY( + uint256 ry, + uint256 S, + SYCoveredCallParams memory params +) pure returns (uint256 L) { + int256 d2 = computeD2({ S: S, params: params }); + uint256 cdf = toUint(Gaussian.cdf(d2)); + + L = ry.divWadUp(params.mean.mulWadUp(cdf)); +} + +/// @dev Computes reserves y given L(x, S). +/// @return ry The reserve y computed as y(x, s) = K * L_x(x, S) * cdf[d2(S, K, sigma, tau)] +function computeYGivenL( + uint256 L, + uint256 S, + SYCoveredCallParams memory params +) pure returns (uint256 ry) { + int256 d2 = computeD2({ S: S, params: params }); + uint256 cdf = toUint(Gaussian.cdf(d2)); + + ry = params.mean.mulWadUp(L).mulWadUp(cdf); +} + +/// @dev Computes reserves x given L(y, S). +/// @return rx The reserve x computed as x(y, s) = L_y(y, S) * (WAD - cdf[d1(S, K, sigma, tau)]) +function computeXGivenL( + uint256 L, + uint256 S, + SYCoveredCallParams memory params +) pure returns (uint256 rx) { + int256 d1 = computeD1({ S: S, params: params }); + uint256 cdf = toUint(Gaussian.cdf(d1)); + rx = L.mulWadUp(ONE - cdf); +} + +/** + * @dev Computes the d1 parameter for the Black-Scholes formula. + * + * $$d_1(S;\mu,\sigma) = \frac{\ln\frac{S}{\mu}+\frac{1}{2}\sigma^2 }{\sigma}$$ + * + * @param S The price of X in Y, in WAD units. + * @param params LogNormParameters of the Log Normal distribution. + */ +function computeD1( + uint256 S, + SYCoveredCallParams memory params +) pure returns (int256 d1) { + uint256 tau = computeTau(params); + if (tau == 0) { + d1 = 0; + } else { + int256 lnSDivMean = computeLnSDivMean(S, params.mean); + uint256 halfSigmaSquaredTau = + computeHalfSigmaSquaredTau(params.width, tau); + uint256 sigmaSqrtTau = computeSigmaSqrtTau(params.width, tau); + d1 = (lnSDivMean + int256(halfSigmaSquaredTau)).wadDiv( + int256(sigmaSqrtTau) + ); + } +} + +/// @dev Computes the d2 parameter for the Black-Scholes formula. +/// $$d_2(S;\mu,\sigma) = \frac{\ln\frac{S}{K}-\frac{1}{2}\sigma^2 }{\sigma}$$ +/// @param S The price of X in Y, in WAD units. +/// @param params LogNormParameters of the Log Normal distribution. +/// @return d2 = d1 - sigma * sqrt(tau), alternatively d2 = (ln(S/K) - tau * sigma^2 / 2) / (sigma * sqrt(tau)) +function computeD2( + uint256 S, + SYCoveredCallParams memory params +) pure returns (int256 d2) { + uint256 tau = computeTau(params); + if (tau == 0) { + d2 = 0; + } else { + int256 lnSDivMean = computeLnSDivMean(S, params.mean); + uint256 halfSigmaSquaredTau = + computeHalfSigmaSquaredTau(params.width, tau); + uint256 sigmaSqrtTau = computeSigmaSqrtTau(params.width, tau); + d2 = (lnSDivMean - int256(halfSigmaSquaredTau)).wadDiv( + int256(sigmaSqrtTau) + ); + } +} + +/** + * @dev Computes the price using the reserve of token X. + * + * $$P_X(x, L; \mu, \sigma) = \mu \exp (\Phi^{-1} (1 - \frac{x}{L} ) \sigma - \frac{1}{2} \sigma^2 )$$ + * + */ +function computePriceGivenX( + uint256 rX, + uint256 L, + SYCoveredCallParams memory params +) pure returns (uint256) { + uint256 tau = computeTau(params); + uint256 a = computeHalfSigmaSquaredTau(params.width, tau); + // $$\Phi^{-1} (1 - \frac{x}{L})$$ + int256 b = Gaussian.ppf(int256(ONE - rX.divWadDown(L))); + + // $$\exp(\Phi^{-1} (1 - \frac{x}{L} ) \sigma - \frac{1}{2} \sigma^2 )$$ + int256 exp = ( + b.wadMul(int256(computeSigmaSqrtTau(params.width, tau))) - int256(a) + ).expWad(); + + // $$\mu \exp (\Phi^{-1} (1 - \frac{x}{L} ) \sigma - \frac{1}{2} \sigma^2 )$$ + return params.mean.mulWadUp(uint256(exp)); +} + +// K = P1(x) / exp[ni(x/L)√(L + (1/2)v²t)] +function computeKGivenLastPrice(uint256 rX, uint256 L, SYCoveredCallParams memory params) pure returns (uint256 K) { + uint256 price = computePriceGivenX(rX, L, params); + + uint256 tau = computeTau(params); + uint256 a = computeHalfSigmaSquaredTau(params.width, tau); + // $$\Phi^{-1} (1 - \frac{x}{L})$$ + int256 b = Gaussian.ppf(int256(ONE - rX.divWadDown(L))); + int256 exp = ( + b.wadMul(int256(computeSigmaSqrtTau(params.width, tau))) - int256(a) + ).expWad(); + + K = price.divWadDown(exp); + + +} + + +function computePriceGivenY( + uint256 rY, + uint256 L, + SYCoveredCallParams memory params +) pure returns (uint256) { + uint256 tau = computeTau(params); + uint256 a = computeHalfSigmaSquaredTau(params.width, tau); + + // $$\Phi^{-1} (\frac{y}{\mu L})$$ + int256 b = Gaussian.ppf(int256(rY.divWadDown(params.mean.mulWadDown(L)))); + + // $$\exp (\Phi^{-1} (\frac{y}{\mu L}) \sigma + \frac{1}{2} \sigma^2 )$$ + int256 exp = ( + b.wadMul(int256(computeSigmaSqrtTau(params.width, tau))) + int256(a) + ).expWad(); + + // $$\mu \exp (\Phi^{-1} (\frac{y}{\mu L}) \sigma + \frac{1}{2} \sigma^2 )$$ + return params.mean.mulWadUp(uint256(exp)); +} + +function computeDeltaLXIn( + uint256 amountIn, + uint256 rx, + uint256 ry, + uint256 L, + SYCoveredCallParams memory params +) pure returns (uint256 deltaL) { + uint256 fees = params.swapFee.mulWadUp(amountIn); + uint256 px = computePriceGivenX(rx, L, params); + deltaL = px.mulWadUp(L).mulWadUp(fees).divWadDown(px.mulWadDown(rx) + ry); +} + +function computeDeltaLYIn( + uint256 amountIn, + uint256 rx, + uint256 ry, + uint256 L, + SYCoveredCallParams memory params +) pure returns (uint256 deltaL) { + uint256 fees = params.swapFee.mulWadUp(amountIn); + uint256 px = computePriceGivenX(rx, L, params); + deltaL = L.mulWadUp(fees).divWadDown(px.mulWadDown(rx) + ry); +} + +function computeAllocationGivenDeltaX( + uint256 deltaX, + uint256 rX, + uint256 rY, + uint256 totalLiquidity +) pure returns (uint256 deltaY, uint256 deltaL) { + uint256 a = deltaX.divWadUp(rX); + deltaY = a.mulWadUp(rY); + deltaL = a.mulWadUp(totalLiquidity); +} + +function computeAllocationGivenDeltaY( + uint256 deltaY, + uint256 rX, + uint256 rY, + uint256 totalLiquidity +) pure returns (uint256 deltaX, uint256 deltaL) { + uint256 a = deltaY.divWadUp(rY); + deltaX = a.mulWadUp(rX); + deltaL = a.mulWadUp(totalLiquidity); +} + +function computeDeallocationGivenDeltaX( + uint256 deltaX, + uint256 rX, + uint256 rY, + uint256 totalLiquidity +) pure returns (uint256 deltaY, uint256 deltaL) { + uint256 a = deltaX.divWadDown(rX); + deltaY = a.mulWadDown(rY); + deltaL = a.mulWadDown(totalLiquidity); +} + +function computeDeallocationGivenDeltaY( + uint256 deltaY, + uint256 rX, + uint256 rY, + uint256 totalLiquidity +) pure returns (uint256 deltaX, uint256 deltaL) { + uint256 a = deltaY.divWadDown(rY); + deltaX = a.mulWadDown(rX); + deltaL = a.mulWadDown(totalLiquidity); +} + +/// @dev This is a pure anonymous function defined at the file level, which allows +/// it to be passed as an argument to another function. BisectionLib.sol takes this +/// function as an argument to find the root of the trading function given the reserveYWad. +function findRootY(bytes memory data, uint256 ry) pure returns (int256) { + (uint256 rx, uint256 L, SYCoveredCallParams memory params) = + abi.decode(data, (uint256, uint256, SYCoveredCallParams)); + return computeTradingFunction(rx, ry, L, params); +} + +/// @dev This is a pure anonymous function defined at the file level, which allows +/// it to be passed as an argument to another function. BisectionLib.sol takes this +/// function as an argument to find the root of the trading function given the reserveXWad. +function findRootX(bytes memory data, uint256 rx) pure returns (int256) { + (uint256 ry, uint256 L, SYCoveredCallParams memory params) = + abi.decode(data, (uint256, uint256, SYCoveredCallParams)); + return computeTradingFunction(rx, ry, L, params); +} + +/// @dev This is a pure anonymous function defined at the file level, which allows +/// it to be passed as an argument to another function. BisectionLib.sol takes this +/// function as an argument to find the root of the trading function given the liquidity. +function findRootLiquidity( + bytes memory data, + uint256 L +) pure returns (int256) { + (uint256 rx, uint256 ry, SYCoveredCallParams memory params) = + abi.decode(data, (uint256, uint256, SYCoveredCallParams)); + return computeTradingFunction(rx, ry, L, params); +} + +function computeNextLiquidity( + uint256 rX, + uint256 rY, + int256 invariant, + uint256 approximatedL, + SYCoveredCallParams memory params +) pure returns (uint256 L) { + uint256 upper = approximatedL; + uint256 lower = approximatedL; + int256 computedInvariant = invariant; + if (computedInvariant < 0) { + while (computedInvariant < 0) { + lower = lower.mulDivDown(999, 1000); + uint256 min = rX > rY.divWadDown(params.mean) + ? rX + 1000 + : rY.divWadDown(params.mean) + 1000; + lower = lower < rX ? min : lower; + computedInvariant = computeTradingFunction({ + rX: rX, + rY: rY, + L: lower, + params: params + }); + } + } else { + while (computedInvariant > 0) { + upper = upper.mulDivUp(1001, 1000); + computedInvariant = computeTradingFunction({ + rX: rX, + rY: rY, + L: upper, + params: params + }); + } + } + (uint256 rootInput,, uint256 lowerInput) = bisection( + abi.encode(rX, rY, params), lower, upper, 1, MAX_ITER, findRootLiquidity + ); + + if ( + computeTradingFunction({ rX: rX, rY: rY, L: rootInput, params: params }) + == 0 + ) { + L = rootInput; + } else { + L = lowerInput; + } +} + +function computeNextRx( + uint256 rY, + uint256 L, + int256 invariant, + uint256 approximatedRx, + SYCoveredCallParams memory params +) pure returns (uint256 rX) { + uint256 upper = approximatedRx; + uint256 lower = approximatedRx; + int256 computedInvariant = invariant; + if (computedInvariant < 0) { + while (computedInvariant < 0) { + upper = upper.mulDivUp(1001, 1000); + upper = upper > L ? L : upper; + computedInvariant = computeTradingFunction({ + rX: upper, + rY: rY, + L: L, + params: params + }); + } + } else { + while (computedInvariant > 0) { + lower = lower.mulDivDown(999, 1000); + lower = lower > L ? L : lower; + computedInvariant = computeTradingFunction({ + rX: lower, + rY: rY, + L: L, + params: params + }); + } + } + (uint256 rootInput, uint256 upperInput,) = bisection( + abi.encode(rY, L, params), lower, upper, 0, MAX_ITER, findRootX + ); + // `upperInput` should be positive, so if root is < 0 return upperInput instead + if ( + computeTradingFunction({ rX: rootInput, rY: rY, L: L, params: params }) + == 0 + ) { + rX = rootInput; + } else { + rX = upperInput; + } +} + +function computeNextRy( + uint256 rX, + uint256 L, + int256 invariant, + uint256 approximatedRy, + SYCoveredCallParams memory params +) pure returns (uint256 rY) { + uint256 upper = approximatedRy; + uint256 lower = approximatedRy; + int256 computedInvariant = invariant; + if (computedInvariant < 0) { + while (computedInvariant < 0) { + upper = upper.mulDivUp(1001, 1000); + computedInvariant = computeTradingFunction({ + rX: rX, + rY: upper, + L: L, + params: params + }); + } + } else { + while (computedInvariant > 0) { + lower = lower.mulDivDown(999, 1000); + computedInvariant = computeTradingFunction({ + rX: rX, + rY: lower, + L: L, + params: params + }); + } + } + (uint256 rootInput, uint256 upperInput,) = bisection( + abi.encode(rX, L, params), lower, upper, 0, MAX_ITER, findRootY + ); + // `upperInput` should be positive, so if root is < 0 return upperInput instead + if ( + computeTradingFunction({ rX: rX, rY: rootInput, L: L, params: params }) + == 0 + ) { + rY = rootInput; + } else { + rY = upperInput; + } +} diff --git a/src/SYCoveredCall/SYCoveredCallSolver.sol b/src/SYCoveredCall/SYCoveredCallSolver.sol new file mode 100644 index 00000000..65d1ac4d --- /dev/null +++ b/src/SYCoveredCall/SYCoveredCallSolver.sol @@ -0,0 +1,356 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pragma solidity 0.8.22; + +import { FixedPointMathLib } from "solmate/utils/FixedPointMathLib.sol"; +import { IStrategy } from "src/interfaces/IStrategy.sol"; +import { Pool, IDFMM } from "src/interfaces/IDFMM.sol"; +import { SignedWadMathLib } from "src/lib/SignedWadMath.sol"; +import { + computeAllocationGivenX, + computeAllocationGivenY +} from "src/lib/StrategyLib.sol"; +import { + encodeFeeUpdate, + encodeControllerUpdate, + computeInitialPoolData, + computeInitialPoolDataGivenY +} from "src/SYCoveredCall/SYCoveredCallUtils.sol"; +import { SYCoveredCallParams } from "src/SYCoveredCall/SYCoveredCall.sol"; +import { + computeTradingFunction, + computeNextLiquidity, + computeXGivenL, + computeNextRx, + computeYGivenL, + computeNextRy, + computePriceGivenX, + computePriceGivenY, + computeDeltaLXIn, + computeDeltaLYIn, + computeAllocationGivenDeltaX, + computeAllocationGivenDeltaY, + computeDeallocationGivenDeltaX, + computeDeallocationGivenDeltaY, + YEAR, + ONE +} from "src/SYCoveredCall/SYCoveredCallMath.sol"; +import "forge-std/console2.sol"; + +contract SYCoveredCallSolver { + using FixedPointMathLib for uint256; + using FixedPointMathLib for int256; + using SignedWadMathLib for int256; + + /// @dev Structure to hold reserve information + struct Reserves { + uint256 rx; + uint256 ry; + uint256 L; + } + + uint256 public constant BISECTION_EPSILON = 0; + uint256 public constant MAX_BISECTION_ITERS = 120; + + address public strategy; + + constructor(address _strategy) { + strategy = _strategy; + } + + function getPoolParams(uint256 poolId) + public + view + returns (SYCoveredCallParams memory) + { + return abi.decode( + IStrategy(strategy).getPoolParams(poolId), (SYCoveredCallParams) + ); + } + + function getPoolParamsCustomTimestamp( + uint256 poolId, + uint256 timestamp + ) public view returns (SYCoveredCallParams memory) { + SYCoveredCallParams memory params = getPoolParams(poolId); + params.timestamp = timestamp; + return params; + } + + function prepareFeeUpdate(uint256 swapFee) + external + pure + returns (bytes memory) + { + return encodeFeeUpdate(swapFee); + } + + function prepareControllerUpdate(address controller) + external + pure + returns (bytes memory) + { + return encodeControllerUpdate(controller); + } + + function getReservesAndLiquidity(uint256 poolId) + public + view + returns (uint256[] memory, uint256) + { + Pool memory pool = IDFMM(IStrategy(strategy).dfmm()).pools(poolId); + return (pool.reserves, pool.totalLiquidity); + } + + function getInitialPoolDataGivenX( + uint256 rX, + uint256 S, + SYCoveredCallParams memory params + ) public pure returns (bytes memory) { + return computeInitialPoolData(rX, S, params); + } + + function getInitialPoolDataGivenY( + uint256 rY, + uint256 S, + SYCoveredCallParams memory params + ) public pure returns (bytes memory) { + return computeInitialPoolDataGivenY(rY, S, params); + } + + function prepareInitialPoolDataGivenY( + uint256 rY, + uint256 S, + SYCoveredCallParams memory params + ) public pure returns (bytes memory) { + return computeInitialPoolDataGivenY(rY, S, params); + } + + function allocateGivenDeltaX( + uint256 poolId, + uint256 deltaX + ) public view returns (uint256 deltaY, uint256 deltaLiquidity) { + (uint256[] memory reserves, uint256 liquidity) = + getReservesAndLiquidity(poolId); + (deltaY, deltaLiquidity) = computeAllocationGivenDeltaX( + deltaX, reserves[0], reserves[1], liquidity + ); + } + + function allocateGivenDeltaY( + uint256 poolId, + uint256 deltaY + ) public view returns (uint256 deltaX, uint256 deltaLiquidity) { + (uint256[] memory reserves, uint256 liquidity) = + getReservesAndLiquidity(poolId); + (deltaX, deltaLiquidity) = computeAllocationGivenDeltaY( + deltaY, reserves[0], reserves[1], liquidity + ); + } + + function deallocateGivenDeltaX( + uint256 poolId, + uint256 deltaX + ) public view returns (uint256 deltaY, uint256 deltaLiquidity) { + (uint256[] memory reserves, uint256 liquidity) = + getReservesAndLiquidity(poolId); + (deltaY, deltaLiquidity) = computeDeallocationGivenDeltaX( + deltaX, reserves[0], reserves[1], liquidity + ); + } + + function deallocateGivenDeltaY( + uint256 poolId, + uint256 deltaY + ) public view returns (uint256 deltaX, uint256 deltaLiquidity) { + (uint256[] memory reserves, uint256 liquidity) = + getReservesAndLiquidity(poolId); + (deltaX, deltaLiquidity) = computeDeallocationGivenDeltaY( + deltaY, reserves[0], reserves[1], liquidity + ); + } + + function getNextLiquidity( + uint256 poolId, + uint256 rx, + uint256 ry, + uint256 L + ) public view returns (uint256) { + SYCoveredCallParams memory poolParams = + getPoolParamsCustomTimestamp(poolId, block.timestamp); + + int256 invariant = computeTradingFunction(rx, ry, L, poolParams); + return computeNextLiquidity(rx, ry, invariant, L, poolParams); + } + + function getNextReserveX( + uint256 poolId, + uint256 ry, + uint256 L, + uint256 S + ) public view returns (uint256) { + SYCoveredCallParams memory poolParams = + getPoolParamsCustomTimestamp(poolId, block.timestamp); + uint256 approximatedRx = computeXGivenL(L, S, poolParams); + int256 invariant = + computeTradingFunction(approximatedRx, ry, L, poolParams); + return computeNextRx(ry, L, invariant, approximatedRx, poolParams); + } + + function getNextReserveY( + uint256 poolId, + uint256 rx, + uint256 L, + uint256 S + ) public view returns (uint256) { + SYCoveredCallParams memory poolParams = + getPoolParamsCustomTimestamp(poolId, block.timestamp); + uint256 approximatedRy = computeYGivenL(L, S, poolParams); + int256 invariant = + computeTradingFunction(rx, approximatedRy, L, poolParams); + return computeNextRy(rx, L, invariant, approximatedRy, poolParams); + } + + struct SimulateSwapState { + uint256 amountOut; + uint256 deltaLiquidity; + uint256 fees; + } + + /// @dev Estimates a swap's reserves and adjustments and returns its validity. + function simulateSwap( + uint256 poolId, + bool swapXIn, + uint256 amountIn + ) public view returns (bool, uint256, uint256, bytes memory) { + Reserves memory endReserves; + (uint256[] memory preReserves, uint256 preTotalLiquidity) = + getReservesAndLiquidity(poolId); + SYCoveredCallParams memory poolParams = + getPoolParamsCustomTimestamp(poolId, block.timestamp); + + SimulateSwapState memory state; + + uint256 startComputedL = getNextLiquidity( + poolId, preReserves[0], preReserves[1], preTotalLiquidity + ); + { + console2.log("startComputedL", startComputedL); + + if (swapXIn) { + state.deltaLiquidity = computeDeltaLXIn( + amountIn, + preReserves[0], + preReserves[1], + preTotalLiquidity, + poolParams + ); + console2.log("state.deltaLiquidity", state.deltaLiquidity); + + endReserves.rx = preReserves[0] + amountIn; + endReserves.L = startComputedL + state.deltaLiquidity; + console2.log("endReserves.rx", endReserves.rx); + console2.log("endReserves.L", endReserves.L); + uint256 approxPrice = + getPriceGivenXL(poolId, endReserves.rx, endReserves.L); + console2.log("approxPrice", approxPrice); + + endReserves.ry = getNextReserveY( + poolId, endReserves.rx, endReserves.L, approxPrice + ); + console2.log("endReserves.ry", endReserves.ry); + + require( + endReserves.ry < preReserves[1], + "invalid swap: y reserve increased!" + ); + state.amountOut = preReserves[1] - endReserves.ry; + } else { + state.deltaLiquidity = computeDeltaLYIn( + amountIn, + preReserves[0], + preReserves[1], + preTotalLiquidity, + poolParams + ); + + endReserves.ry = preReserves[1] + amountIn; + endReserves.L = startComputedL + state.deltaLiquidity; + uint256 approxPrice = + getPriceGivenYL(poolId, endReserves.ry, endReserves.L); + + endReserves.rx = getNextReserveX( + poolId, endReserves.ry, endReserves.L, approxPrice + ); + + require( + endReserves.rx < preReserves[0], + "invalid swap: x reserve increased!" + ); + state.amountOut = preReserves[0] - endReserves.rx; + } + } + + Pool memory pool; + pool.reserves = preReserves; + pool.totalLiquidity = preTotalLiquidity; + + bytes memory swapData; + + if (swapXIn) { + swapData = + abi.encode(0, 1, amountIn, state.amountOut, startComputedL); + } else { + swapData = + abi.encode(1, 0, amountIn, state.amountOut, startComputedL); + } + + uint256 poolId = poolId; + (bool valid,,,,,,) = IStrategy(strategy).validateSwap( + address(this), poolId, pool, swapData + ); + + return ( + valid, + state.amountOut, + computePriceGivenX(endReserves.rx, endReserves.L, poolParams), + swapData + ); + } + + function getPriceGivenYL( + uint256 poolId, + uint256 ry, + uint256 L + ) public view returns (uint256 price) { + SYCoveredCallParams memory params = + getPoolParamsCustomTimestamp(poolId, block.timestamp); + price = computePriceGivenY(ry, L, params); + } + + function getPriceGivenXL( + uint256 poolId, + uint256 rx, + uint256 L + ) public view returns (uint256 price) { + SYCoveredCallParams memory params = + getPoolParamsCustomTimestamp(poolId, block.timestamp); + price = computePriceGivenX(rx, L, params); + } + + /// @dev Computes the internal price using this strategie's slot parameters. + function internalPrice(uint256 poolId) + public + view + returns (uint256 price) + { + (uint256[] memory reserves, uint256 L) = getReservesAndLiquidity(poolId); + price = computePriceGivenX(reserves[0], L, getPoolParams(poolId)); + } + + function getInvariant(uint256 poolId) public view returns (int256) { + (uint256[] memory reserves, uint256 L) = getReservesAndLiquidity(poolId); + return computeTradingFunction( + reserves[0], reserves[1], L, getPoolParams(poolId) + ); + } +} diff --git a/src/SYCoveredCall/SYCoveredCallUtils.sol b/src/SYCoveredCall/SYCoveredCallUtils.sol new file mode 100644 index 00000000..2fee37fb --- /dev/null +++ b/src/SYCoveredCall/SYCoveredCallUtils.sol @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pragma solidity ^0.8.13; + +import { SYCoveredCallParams, UpdateCode } from "src/SYCoveredCall/SYCoveredCall.sol"; +import { + computeLGivenX, + computeLGivenY, + computeXGivenL, + computeYGivenL, + computeTradingFunction, + computeNextLiquidity +} from "./SYCoveredCallMath.sol"; + +function encodeFeeUpdate(uint256 swapFee) pure returns (bytes memory) { + return abi.encode(UpdateCode.SwapFee, uint256(swapFee)); +} + +function encodeControllerUpdate(address controller) + pure + returns (bytes memory data) +{ + return abi.encode(UpdateCode.Controller, controller); +} + +function decodeFeeUpdate(bytes memory data) pure returns (uint256) { + (, uint256 swapFee) = abi.decode(data, (UpdateCode, uint256)); + return swapFee; +} + +function decodeControllerUpdate(bytes memory data) + pure + returns (address controller) +{ + (, controller) = abi.decode(data, (UpdateCode, address)); +} + +function computeInitialPoolData( + uint256 amountX, + uint256 initialPrice, + SYCoveredCallParams memory params +) pure returns (bytes memory) { + uint256 L = computeLGivenX(amountX, initialPrice, params); + uint256 ry = computeYGivenL(L, initialPrice, params); + int256 invariant = computeTradingFunction(amountX, ry, L, params); + L = computeNextLiquidity(amountX, ry, invariant, L, params); + uint256[] memory reserves = new uint256[](2); + reserves[0] = amountX; + reserves[1] = ry; + return abi.encode(reserves, L, params); +} + +function computeInitialPoolDataGivenY( + uint256 amountY, + uint256 initialPrice, + SYCoveredCallParams memory params +) pure returns (bytes memory) { + uint256 L = computeLGivenY(amountY, initialPrice, params); + uint256 rX = computeXGivenL(L, initialPrice, params); + int256 invariant = computeTradingFunction(rX, amountY, L, params); + L = computeNextLiquidity(rX, amountY, invariant, L, params); + uint256[] memory reserves = new uint256[](2); + reserves[0] = rX; + reserves[1] = amountY; + return abi.encode(reserves, L, params); +} + +/// @dev Casts a positived signed integer to an unsigned integer, reverting if `x` is negative. +function toUint(int256 x) pure returns (uint256) { + require(x >= 0, "toUint: negative"); + return uint256(x); +} diff --git a/src/SYCoveredCall/covered_call.nb b/src/SYCoveredCall/covered_call.nb new file mode 100644 index 00000000..329ffa88 --- /dev/null +++ b/src/SYCoveredCall/covered_call.nb @@ -0,0 +1,2334 @@ +(*CacheID: 234*) +(* Internal cache information: +NotebookFileLineBreakTest +NotebookFileLineBreakTest +NotebookDataPosition[ 0, 0] +NotebookDataLength[ 86695, 2333] +NotebookOptionsPosition[ 78138, 2161] +NotebookOutlinePosition[ 78697, 2180] +CellTagsIndexPosition[ 78654, 2177] +WindowFrame->Normal*) + +(* Beginning of Notebook Content *) +Notebook[{ + +Cell[CellGroupData[{ +Cell["Log Normal Trading Function Calculations", "Title", + CellChangeTimes->{{3.911382811596325*^9, + 3.9113828340058823`*^9}},ExpressionUUID->"2003d08a-fff7-4f74-8623-\ +7a0823c9cafa"], + +Cell[CellGroupData[{ + +Cell["\<\ +First, we set up the basic functions we need throughout the notebook.\ +\>", "Section", + CellChangeTimes->{{3.911382862311339*^9, + 3.91138289581577*^9}},ExpressionUUID->"514be430-48c5-4dc6-92af-\ +6b1c3a5b8586"], + +Cell[CellGroupData[{ + +Cell["\<\ +Before anything, we should set some environment level variables.\ +\>", "Subsection", + CellChangeTimes->{{3.911387263997834*^9, + 3.9113872765136137`*^9}},ExpressionUUID->"f16f1652-ed41-4414-8c9b-\ +6b6d5d8061ac"], + +Cell[BoxData[ + RowBox[{ + RowBox[{ + RowBox[{"On", "[", "Assert", "]"}], ";"}], " ", + RowBox[{"(*", " ", + RowBox[{ + RowBox[{ + "Asserts", " ", "will", " ", "show", " ", "a", " ", "failure", " ", "if", + " ", "they", " ", "fail"}], ",", " ", + RowBox[{"and", " ", "nothing", " ", "if", " ", "they", " ", + RowBox[{"don", "'"}], "t"}]}], " ", "*)"}]}]], "Code", + CellChangeTimes->{{3.91138727840687*^9, 3.911387281430051*^9}, { + 3.911387543969853*^9, 3.911387555514419*^9}}, + CellLabel-> + "In[3266]:=",ExpressionUUID->"8255c47c-fa0b-4fdd-8638-752453aca613"] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["First are the CDF and inverse CDF (PPF) functions.", "Subsection", + CellChangeTimes->{{3.9113829761574574`*^9, + 3.9113829863941193`*^9}},ExpressionUUID->"b3dd161e-0b53-4183-b30c-\ +be7844f26477"], + +Cell[BoxData[{ + RowBox[{ + RowBox[{"\[CapitalPhi]", "[", "x_", "]"}], " ", ":=", " ", + RowBox[{"CDF", "[", + RowBox[{ + RowBox[{"NormalDistribution", "[", + RowBox[{"0", ",", "1"}], "]"}], ",", " ", "x"}], "]"}]}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalPhi]", "inv"], "[", "y_", "]"}], " ", ":=", " ", + RowBox[{"Quantile", "[", + RowBox[{ + RowBox[{"NormalDistribution", "[", + RowBox[{"0", ",", " ", "1"}], "]"}], ",", " ", "y"}], "]"}]}]}], "Code", + CellChangeTimes->{{3.911382903714142*^9, 3.911383006799996*^9}, { + 3.911385117889493*^9, 3.911385119663499*^9}, {3.91738309829743*^9, + 3.917383102823773*^9}, {3.9173831552741337`*^9, 3.917383158507695*^9}, + 3.917383532443891*^9}, + CellLabel-> + "In[3833]:=",ExpressionUUID->"25d6c1c4-f902-41e0-8746-c63f6b03d94e"] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["\<\ +Next let\[CloseCurlyQuote]s define some helper functions. These will appear \ +often in calculations.\ +\>", "Subsection", + CellChangeTimes->{{3.911383043072701*^9, 3.911383082172174*^9}, { + 3.911383316418652*^9, + 3.9113833317783127`*^9}},ExpressionUUID->"f601d02f-f91e-4780-a166-\ +a78097a54f48"], + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + SubscriptBox["d", "1"], "[", + RowBox[{"S_", ",", "\[Mu]_", ",", "\[Sigma]_"}], "]"}], " ", ":=", " ", + FractionBox[ + RowBox[{ + RowBox[{"Log", "[", + FractionBox["S", "\[Mu]"], "]"}], " ", "+", " ", + RowBox[{ + FractionBox["1", "2"], + SuperscriptBox["\[Sigma]", "2"]}]}], "\[Sigma]"]}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["d", "2"], "[", + RowBox[{"S_", ",", "\[Mu]_", ",", "\[Sigma]_"}], "]"}], " ", ":=", " ", + FractionBox[ + RowBox[{ + RowBox[{"Log", "[", + FractionBox["S", "\[Mu]"], "]"}], " ", "-", " ", + RowBox[{ + FractionBox["1", "2"], + SuperscriptBox["\[Sigma]", "2"]}]}], "\[Sigma]"]}]}], "Code", + CellChangeTimes->{{3.911383086202894*^9, 3.911383096527341*^9}, { + 3.911383144055451*^9, 3.911383310823001*^9}, {3.9113851030677443`*^9, + 3.91138511600043*^9}, 3.9185709112502613`*^9}, + CellLabel-> + "In[3835]:=",ExpressionUUID->"d5d16a82-3e60-44ff-affc-b50eb9144304"] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["\<\ +Now let\[CloseCurlyQuote]s define functions that are more explicitly used for \ +the DFMM.\ +\>", "Subsection", + CellChangeTimes->{{3.911383368894425*^9, 3.9113833696600657`*^9}, { + 3.911383542720358*^9, + 3.911383554344432*^9}},ExpressionUUID->"009a24ad-ebe5-4d73-bdda-\ +a7839592332a"], + +Cell[CellGroupData[{ + +Cell["\<\ +These are functions used to get initial liquidity given a token amount and a \ +price.\ +\>", "Subsubsection", + CellChangeTimes->{{3.911383821691424*^9, + 3.911383842953394*^9}},ExpressionUUID->"3b15f5e3-f420-4095-899a-\ +506c7286cc40"], + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + SubscriptBox["L", "X"], "[", + RowBox[{"x_", ",", "S_", ",", "\[Mu]_", ",", "\[Sigma]_"}], "]"}], " ", ":=", + " ", + FractionBox["x", + RowBox[{"1", " ", "-", " ", + RowBox[{"\[CapitalPhi]", "[", + RowBox[{ + SubscriptBox["d", "1"], "[", + RowBox[{"S", ",", "\[Mu]", ",", "\[Sigma]"}], "]"}], "]"}]}]]}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["L", "Y"], "[", + RowBox[{"y_", ",", "S_", ",", "\[Mu]_", ",", "\[Sigma]_"}], "]"}], " ", ":=", + " ", + FractionBox["y", + RowBox[{"\[Mu]", " ", + RowBox[{"\[CapitalPhi]", "[", + RowBox[{ + SubscriptBox["d", "2"], "[", + RowBox[{"S", ",", "\[Mu]", ",", "\[Sigma]"}], "]"}], "]"}]}]]}], "\n", + RowBox[{ + RowBox[{"X", "[", + RowBox[{"y_", ",", "S_", ",", "\[Mu]_", ",", "\[Sigma]_"}], "]"}], " ", ":=", + " ", + RowBox[{ + RowBox[{ + SubscriptBox["L", "Y"], "[", + RowBox[{"y", ",", "S", ",", "\[Mu]", ",", "\[Sigma]"}], "]"}], " ", + RowBox[{"(", + RowBox[{"1", " ", "-", " ", + RowBox[{"\[CapitalPhi]", "[", + RowBox[{ + SubscriptBox["d", "1"], "[", + RowBox[{"S", ",", "\[Mu]", ",", "\[Sigma]"}], "]"}], "]"}]}], + ")"}]}]}], "\n", + RowBox[{ + RowBox[{"Y", "[", + RowBox[{"x_", ",", "S_", ",", "\[Mu]_", ",", "\[Sigma]_"}], "]"}], " ", ":=", + " ", + RowBox[{"\[Mu]", " ", + RowBox[{ + SubscriptBox["L", "X"], "[", + RowBox[{"x", ",", "S", ",", "\[Mu]", ",", "\[Sigma]"}], "]"}], " ", + RowBox[{"\[CapitalPhi]", "[", + RowBox[{ + SubscriptBox["d", "2"], "[", + RowBox[{"S", ",", "\[Mu]", ",", "\[Sigma]"}], "]"}], "]"}]}]}]}], "Code",\ + + CellChangeTimes->{{3.911383688783895*^9, 3.911383741794456*^9}, { + 3.911383797950727*^9, 3.911383809912835*^9}, {3.9113838491740713`*^9, + 3.911383864952888*^9}, {3.9113841779644413`*^9, 3.911384322151863*^9}, { + 3.911384433609087*^9, 3.911384448745434*^9}, {3.9113850554248533`*^9, + 3.911385099649076*^9}, {3.91138525707533*^9, 3.911385263363533*^9}, { + 3.911385324670476*^9, 3.911385325035862*^9}, {3.911409187909778*^9, + 3.9114091880800257`*^9}, 3.9185709187863417`*^9}, + CellLabel-> + "In[3837]:=",ExpressionUUID->"3950a1e9-9c32-45c4-b5ef-404c37d6ef65"] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["\<\ +These are functions that are used to get prices from either a balance in X or \ +a balance in Y.\ +\>", "Subsubsection", + CellChangeTimes->{{3.91138394332069*^9, + 3.911383960427863*^9}},ExpressionUUID->"6228385e-cfd2-4bd0-97f4-\ +58c98a5a994e"], + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + SubscriptBox["P", "X"], "[", + RowBox[{"x_", ",", "L_", ",", "\[Mu]_", ",", "\[Sigma]_"}], "]"}], " ", ":=", + " ", + RowBox[{"\[Mu]", " ", + RowBox[{"Exp", "[", + RowBox[{ + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalPhi]", "inv"], "[", + RowBox[{"1", " ", "-", " ", + FractionBox["x", "L"]}], "]"}], "\[Sigma]"}], " ", "-", " ", + RowBox[{ + FractionBox["1", "2"], + SuperscriptBox["\[Sigma]", "2"]}]}], "]"}]}]}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["P", "Y"], "[", + RowBox[{"y_", ",", "L_", ",", "\[Mu]_", ",", "\[Sigma]_"}], "]"}], " ", ":=", + " ", + RowBox[{"\[Mu]", " ", + RowBox[{"Exp", "[", + RowBox[{ + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalPhi]", "inv"], "[", + FractionBox["y", + RowBox[{"\[Mu]", " ", "L"}]], "]"}], "\[Sigma]"}], " ", "+", " ", + RowBox[{ + FractionBox["1", "2"], + SuperscriptBox["\[Sigma]", "2"]}]}], "]"}]}]}]}], "Code", + CellChangeTimes->{{3.9113839769604807`*^9, 3.911384029460125*^9}, { + 3.911385062781126*^9, 3.911385091502931*^9}, 3.91857093005863*^9}, + CellLabel-> + "In[3841]:=",ExpressionUUID->"cdbca2c9-2426-4adf-8516-6c22e3b352b1"] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["Then we have the trading function", "Subsubsection", + CellChangeTimes->{{3.9114107980754547`*^9, + 3.911410804009096*^9}},ExpressionUUID->"18015876-38da-4fa9-82b7-\ +a417d745ef90"], + +Cell[BoxData[ + RowBox[{ + RowBox[{"\[CurlyPhi]", "[", + RowBox[{"x_", ",", "y_", ",", "L_", ",", "\[Mu]_", ",", "\[Sigma]_"}], + "]"}], " ", ":=", " ", + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalPhi]", "inv"], "[", + FractionBox["x", "L"], "]"}], "+", + RowBox[{ + SubscriptBox["\[CapitalPhi]", "inv"], "[", + FractionBox["y", + RowBox[{"\[Mu]", " ", "L"}]], "]"}], "+", "\[Sigma]"}]}]], "Code", + CellChangeTimes->{{3.911410806554799*^9, 3.911410882453505*^9}, { + 3.9114109468550673`*^9, 3.9114109866059113`*^9}, 3.9185709352254477`*^9}, + CellLabel-> + "In[3843]:=",ExpressionUUID->"a65cb5ec-cdac-40e0-bb49-f3d8157592d9"] +}, Open ]] +}, Open ]] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["\<\ +Let\[CloseCurlyQuote]s initialize a pool with some constants and some \ +liquidity. \ +\>", "Section", + CellChangeTimes->{{3.911384055956565*^9, 3.911384066810919*^9}, { + 3.911384711690135*^9, + 3.911384714277852*^9}},ExpressionUUID->"da815218-0c74-4720-a5c9-\ +76f45781c5e2"], + +Cell[CellGroupData[{ + +Cell["\<\ +First, let\[CloseCurlyQuote]s set the parameters for our curve, including the \ +fee parameter \[Gamma]\ +\>", "Subsection", + CellChangeTimes->{{3.911384725040826*^9, 3.911384731518064*^9}, { + 3.9113851982356*^9, + 3.911385205023809*^9}},ExpressionUUID->"d461a415-44ca-4804-8248-\ +6137a0f9449f"], + +Cell[CellGroupData[{ + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + RowBox[{ + RowBox[{"{", + RowBox[{ + SubscriptBox["\[Mu]", "0"], ",", " ", + SubscriptBox["\[Sigma]", "0"], ",", " ", + SubscriptBox["\[Gamma]", "0"]}], "}"}], " ", "=", " ", + RowBox[{"{", + RowBox[{"1", ",", " ", + FractionBox["1", "4"], ",", " ", "0.995"}], "}"}]}], ";"}], " "}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + SubscriptBox["K", "0"], ",", " ", + "\"\<\!\(\*SubscriptBox[\(K\), \(0\)]\) = \>\""}], "]"}], ";", " ", + RowBox[{"Echo", "[", + RowBox[{ + SubscriptBox["\[Sigma]", "0"], ",", " ", + "\"\<\!\(\*SubscriptBox[\(\[Sigma]\), \(0\)]\) = \>\""}], "]"}], ";", " ", + RowBox[{"Echo", "[", + RowBox[{ + SubscriptBox["\[Gamma]", "0"], ",", " ", + "\"\<\!\(\*SubscriptBox[\(\[Gamma]\), \(0\)]\) = \>\""}], "]"}], + ";"}]}], "Code", + CellChangeTimes->{ + 3.91138448247676*^9, {3.9113845136096563`*^9, 3.911384540517331*^9}, { + 3.911384579083336*^9, 3.911384646274679*^9}, {3.911384706054633*^9, + 3.911384732869401*^9}, {3.911385194438079*^9, 3.9113852384442177`*^9}, + 3.911387336640081*^9, {3.911408996136958*^9, 3.9114090092659817`*^9}, { + 3.9169341546962433`*^9, 3.916934155785771*^9}, {3.916934920973572*^9, + 3.916934921366069*^9}, {3.916935004865656*^9, 3.916935006671085*^9}, { + 3.916935097711054*^9, 3.916935099175394*^9}, {3.91693537100875*^9, + 3.916935393268579*^9}, {3.916935561687649*^9, 3.916935563633135*^9}, { + 3.91738367920582*^9, 3.917383680576692*^9}, {3.917384081819539*^9, + 3.917384085426984*^9}, {3.9173856229323063`*^9, 3.917385639341588*^9}, { + 3.91751517656183*^9, 3.917515189435231*^9}, {3.918570943201333*^9, + 3.918570984185614*^9}, {3.918571110139764*^9, 3.9185711113502274`*^9}, { + 3.9185720081337433`*^9, 3.918572008408592*^9}, {3.918575751023251*^9, + 3.918575751913149*^9}, {3.918576073288947*^9, 3.9185760735774183`*^9}, { + 3.918576642017666*^9, 3.918576642103425*^9}, {3.918582513027935*^9, + 3.918582513467471*^9}, {3.918582809143463*^9, 3.918582809217847*^9}, { + 3.918582876514236*^9, 3.918582923985523*^9}, {3.918583425222728*^9, + 3.918583425376048*^9}, {3.918583546078206*^9, 3.918583546573629*^9}, { + 3.918584395359364*^9, 3.918584395834972*^9}, {3.9185864064676332`*^9, + 3.918586406580247*^9}, {3.918587050341199*^9, 3.918587075189999*^9}, { + 3.918588451693397*^9, 3.918588451829137*^9}, {3.918630585921973*^9, + 3.918630585996049*^9}, {3.9186306784955587`*^9, 3.918630678602592*^9}, { + 3.918631219716024*^9, 3.918631219811838*^9}, {3.918631276745323*^9, + 3.9186312768635263`*^9}}, + CellLabel-> + "In[3844]:=",ExpressionUUID->"8d262a91-37a1-41fb-b1b3-ae8191b1ccda"], + +Cell[CellGroupData[{ + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"\\!\\(\\*SubscriptBox[\\(K\\), \\(0\\)]\\) = \"\>", + "EchoLabel"], " ", "1"}]], "Echo", + CellChangeTimes->{ + 3.9186459576704597`*^9},ExpressionUUID->"63fa2685-ead2-42c3-8894-\ +aa3d0f759278"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"\\!\\(\\*SubscriptBox[\\(\[Sigma]\\), \\(0\\)]\\) = \"\>", + "EchoLabel"], " ", + FractionBox["1", "4"]}]], "Echo", + CellChangeTimes->{ + 3.918645957703246*^9},ExpressionUUID->"ab767d72-08f9-45a3-8d7a-\ +62a93aac3a75"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"\\!\\(\\*SubscriptBox[\\(\[Gamma]\\), \\(0\\)]\\) = \"\>", + "EchoLabel"], " ", "0.995`"}]], "Echo", + CellChangeTimes->{ + 3.918645957713688*^9},ExpressionUUID->"865ae55a-ba52-403c-9134-\ +dd94b3a70090"] +}, Open ]] +}, Open ]] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["\<\ +Now, let\[CloseCurlyQuote]s set the initial liquidity by providing an amount \ +of X and a price S.\ +\>", "Subsection", + CellChangeTimes->{{3.9113847363117323`*^9, 3.911384755116337*^9}, { + 3.911384849366685*^9, + 3.9113848505774117`*^9}},ExpressionUUID->"a6874bc0-590c-4cb3-9d20-\ +f010a014a154"], + +Cell[CellGroupData[{ + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + RowBox[{ + RowBox[{"{", + RowBox[{ + SubscriptBox["x", "0"], ",", + SubscriptBox["S", "0"]}], "}"}], " ", "=", " ", + RowBox[{"{", + RowBox[{"1000000000", ",", " ", "0.75"}], "}"}]}], ";"}], " "}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + SubscriptBox["x", "0"], ",", " ", + "\"\\""}], "]"}], ";", " ", + RowBox[{"Echo", "[", + RowBox[{ + SubscriptBox["S", "0"], ",", " ", + "\"\<\!\(\*SubscriptBox[\(S\), \(0\)]\) = \>\""}], "]"}], ";"}]}], "Code",\ + + CellChangeTimes->{{3.911384757671178*^9, 3.911384806225191*^9}, { + 3.911386982132729*^9, 3.911386989059497*^9}, 3.91138734111685*^9, { + 3.9114090196649647`*^9, 3.9114090339697657`*^9}, {3.917383705204445*^9, + 3.917383705323718*^9}, {3.917383899939695*^9, 3.9173839018597183`*^9}, { + 3.917383976339484*^9, 3.917383976688408*^9}, 3.917384041262576*^9, { + 3.9173840921075373`*^9, 3.9173840922911777`*^9}, 3.917384525229738*^9, { + 3.917515198755231*^9, 3.917515199530878*^9}, {3.918575982190796*^9, + 3.918575982584321*^9}, {3.918582811121216*^9, 3.9185828112296124`*^9}, { + 3.918582880689728*^9, 3.9185828808310337`*^9}, {3.918582926936624*^9, + 3.9185829296473217`*^9}, {3.91858299719833*^9, 3.918582997406849*^9}, { + 3.918583291020159*^9, 3.918583291177719*^9}, {3.918583428982098*^9, + 3.9185834291087303`*^9}, {3.918583548048421*^9, 3.918583551706524*^9}, { + 3.918584158439363*^9, 3.918584158785482*^9}, {3.918584216906336*^9, + 3.9185842170754147`*^9}, {3.918584354834117*^9, 3.918584355139624*^9}, { + 3.918584446835289*^9, 3.918584447252163*^9}, {3.918585317499875*^9, + 3.918585320001732*^9}, {3.918585959285797*^9, 3.918585960380043*^9}, { + 3.918587011890151*^9, 3.9185870122509117`*^9}, {3.91858707864706*^9, + 3.918587079216874*^9}, {3.9186305834832478`*^9, 3.918630583772887*^9}, { + 3.918630681419763*^9, 3.918630681553543*^9}, {3.9186312811761227`*^9, + 3.9186312818709307`*^9}, 3.918631349127054*^9, {3.918631421837872*^9, + 3.918631422168438*^9}, {3.918631663100456*^9, 3.9186316633223667`*^9}, { + 3.918643343448288*^9, 3.918643343752068*^9}, {3.918646106269537*^9, + 3.918646106440174*^9}, {3.918646277573381*^9, 3.918646277908587*^9}, { + 3.9186465675649567`*^9, 3.918646568407236*^9}, {3.918647114579299*^9, + 3.9186471322198763`*^9}},ExpressionUUID->"085ca656-cc94-43cf-a8c6-\ +c116e23e4910"], + +Cell[CellGroupData[{ + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The initial X-reserve balance is: \ +\\!\\(\\*SubscriptBox[\\(x\\), \\(0\\)]\\) = \"\>", + "EchoLabel"], " ", "1000000000"}]], "Echo", + CellChangeTimes->{ + 3.918646568794178*^9},ExpressionUUID->"d46f0b65-6c97-4fd7-a377-\ +fd814b863b4a"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"\\!\\(\\*SubscriptBox[\\(S\\), \\(0\\)]\\) = \"\>", + "EchoLabel"], " ", "0.75`"}]], "Echo", + CellChangeTimes->{ + 3.918646568829261*^9},ExpressionUUID->"d830d4bf-eeeb-4bb6-a004-\ +32188869cd46"] +}, Open ]] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["\<\ +From this, let\[CloseCurlyQuote]s see what we will get for the initial amount \ +of Y and L.\ +\>", "Subsubsection", + CellChangeTimes->{{3.9113848345992517`*^9, + 3.91138485967836*^9}},ExpressionUUID->"d0f44536-b00f-4974-8c75-\ +dd65dd319645"], + +Cell[CellGroupData[{ + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + RowBox[{ + RowBox[{"{", + RowBox[{ + SubscriptBox["L", "0"], ",", " ", + SubscriptBox["y", "0"]}], "}"}], " ", "=", " ", + RowBox[{"{", + RowBox[{ + RowBox[{ + SubscriptBox["L", "X"], "[", + RowBox[{ + SubscriptBox["x", "0"], ",", + SubscriptBox["S", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"]}], "]"}], ",", " ", + RowBox[{"Y", "[", + RowBox[{ + SubscriptBox["x", "0"], ",", + SubscriptBox["S", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"]}], "]"}]}], "}"}]}], ";"}], " "}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"N", "[", + RowBox[{ + SubscriptBox["L", "0"], ",", " ", "18"}], "]"}], ",", " ", + "\"\\""}], "]"}], ";", " ", + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"N", "[", + RowBox[{ + SubscriptBox["y", "0"], ",", " ", "18"}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}]}], "Code", + CellChangeTimes->{{3.911384864489366*^9, 3.911384942301722*^9}, { + 3.911384992602735*^9, 3.9113850128379593`*^9}, {3.911385047347066*^9, + 3.9113850518014383`*^9}, {3.9113851385254107`*^9, 3.9113851753238697`*^9}, { + 3.91138696634296*^9, 3.911386992951726*^9}, {3.911387349621842*^9, + 3.911387398394403*^9}, {3.911912845565853*^9, 3.9119128460491123`*^9}, { + 3.918570990618823*^9, 3.918570994101475*^9}, {3.9185711059481163`*^9, + 3.9185711082874823`*^9}}, + CellLabel-> + "In[4060]:=",ExpressionUUID->"57112165-db6f-447c-9713-f9c85c6d4828"], + +Cell[CellGroupData[{ + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The initial liquidity is: \\!\\(\\*SubscriptBox[\\(L\\), \ +\\(0\\)]\\) = \"\>", + "EchoLabel"], " ", "1.1799546999753058`*^9"}]], "Echo", + CellChangeTimes->{ + 3.918646570423168*^9},ExpressionUUID->"d45d2202-bcd3-4d72-9040-\ +061ec9d3b3fd"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The initial Y-reserve balance is: \ +\\!\\(\\*SubscriptBox[\\(y\\), \\(0\\)]\\) = \"\>", + "EchoLabel"], " ", "1.1920585842721254`*^8"}]], "Echo", + CellChangeTimes->{ + 3.918646570454266*^9},ExpressionUUID->"7f4698d3-1c35-46e4-b14b-\ +549da3ca9c05"] +}, Open ]] +}, Open ]] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["Let\[CloseCurlyQuote]s check that the prices are correct after the \ +fact.", "Subsubsection", + CellChangeTimes->{{3.911385351666885*^9, + 3.91138536037985*^9}},ExpressionUUID->"0dabba71-0908-48dd-b11c-\ +9fc06ac2d79b"], + +Cell[CellGroupData[{ + +Cell[BoxData[{ + RowBox[{"Assert", "[", + RowBox[{ + RowBox[{ + SubscriptBox["P", "X"], "[", + RowBox[{ + SubscriptBox["x", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"]}], "]"}], " ", "==", " ", + RowBox[{ + SubscriptBox["P", "Y"], "[", + RowBox[{ + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"]}], "]"}]}], "]"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{ + SubscriptBox["P", "X"], "[", + RowBox[{ + SubscriptBox["x", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"]}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}], " "}]}], "Code", + CellChangeTimes->{{3.9113853639105387`*^9, 3.9113854185634947`*^9}, { + 3.9113869483534203`*^9, 3.911386962492029*^9}, {3.911386994813387*^9, + 3.911387177122032*^9}, {3.911387291516371*^9, 3.911387309281919*^9}, + 3.911912928324705*^9, {3.9185711812613907`*^9, 3.918571185390046*^9}, { + 3.91857169317227*^9, 3.918571699558569*^9}}, + CellLabel-> + "In[4062]:=",ExpressionUUID->"05df5500-1e66-49a9-afd7-256365f8b6dc"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The initial price is: P = \"\>", + "EchoLabel"], " ", "0.75`"}]], "Echo", + CellChangeTimes->{ + 3.9186465723885307`*^9},ExpressionUUID->"2780423d-6264-4b5d-a5a5-\ +68bad0b82ef8"] +}, Open ]] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["\<\ +Just to verify that we could have done this the other way, and show the flow, \ +let\[CloseCurlyQuote]s do that real fast.\ +\>", "Subsubsection", + CellChangeTimes->{{3.9113853013290577`*^9, + 3.9113853161239634`*^9}},ExpressionUUID->"6c78f375-83c5-40c5-a26f-\ +9dfafb83427b"], + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + RowBox[{"{", + RowBox[{ + SubscriptBox["L", + SubscriptBox["0", "y"]], ",", " ", + SubscriptBox["x", + SubscriptBox["0", "y"]]}], "}"}], " ", "=", " ", + RowBox[{"{", + RowBox[{ + RowBox[{ + SubscriptBox["L", "Y"], "[", + RowBox[{ + SubscriptBox["y", "0"], ",", + SubscriptBox["S", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"]}], "]"}], ",", " ", + RowBox[{"X", "[", + RowBox[{ + SubscriptBox["y", "0"], ",", + SubscriptBox["S", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"]}], "]"}]}], "}"}]}], ";"}], "\n", + RowBox[{ + RowBox[{"Assert", "[", + RowBox[{ + RowBox[{"{", + RowBox[{ + SubscriptBox["L", + SubscriptBox["0", "y"]], ",", + SubscriptBox["x", + SubscriptBox["0", "y"]]}], "}"}], " ", "==", " ", + RowBox[{"{", + RowBox[{ + SubscriptBox["L", "0"], ",", + SubscriptBox["x", "0"]}], "}"}]}], "]"}], ";"}]}], "Code", + CellChangeTimes->{{3.911387447754344*^9, 3.911387533259612*^9}, { + 3.911409113951961*^9, 3.911409136342054*^9}, {3.911409206127439*^9, + 3.911409206365666*^9}, {3.918571188362344*^9, 3.918571190424653*^9}, { + 3.9185717026957493`*^9, 3.918571706012089*^9}}, + CellLabel-> + "In[4064]:=",ExpressionUUID->"05cdeb1a-e1b0-40a7-af51-51ab9b75cc2e"] +}, Open ]] +}, Open ]] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["Swapping", "Section", + CellChangeTimes->{{3.911387861088531*^9, + 3.9113878701923523`*^9}},ExpressionUUID->"e0558e89-2c12-471a-af64-\ +7b6a1457eb4d"], + +Cell[CellGroupData[{ + +Cell["\<\ +Now we need to set up the swap logic. We will use R to denote an arbitrary \ +reserve.\ +\>", "Subsection", + CellChangeTimes->{{3.911387873703059*^9, 3.911387883191267*^9}, { + 3.911388058682213*^9, + 3.911388067027336*^9}},ExpressionUUID->"2f7348b3-443c-4a22-943e-\ +943b7962999e"], + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + RowBox[{"fees", "[", + RowBox[{"\[CapitalDelta]_", ",", "\[Gamma]_"}], "]"}], " ", ":=", " ", + RowBox[{ + RowBox[{"(", + RowBox[{"1", "-", "\[Gamma]"}], ")"}], "\[CapitalDelta]"}]}], + " "}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["\[Delta]", "LiqX"], "[", + RowBox[{ + "\[CapitalDelta]_", ",", "x_", ",", "y_", ",", "L_", ",", "\[Mu]_", ",", + "\[Sigma]_", ",", "\[Gamma]_"}], "]"}], " ", ":=", " ", + RowBox[{ + RowBox[{ + SubscriptBox["P", "X"], "[", + RowBox[{"x", ",", "L", ",", "\[Mu]", ",", "\[Sigma]"}], "]"}], " ", "L", + " ", + FractionBox[ + RowBox[{"fees", "[", + RowBox[{"\[CapitalDelta]", ",", "\[Gamma]"}], "]"}], + RowBox[{ + RowBox[{ + RowBox[{ + SubscriptBox["P", "X"], "[", + RowBox[{"x", ",", "L", ",", "\[Mu]", ",", "\[Sigma]"}], "]"}], " ", + "x"}], " ", "+", " ", "y"}]]}]}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["\[Delta]", "LiqY"], "[", + RowBox[{ + "\[CapitalDelta]_", ",", "x_", ",", "y_", ",", "L_", ",", "\[Mu]_", ",", + "\[Sigma]_", ",", "\[Gamma]_"}], "]"}], " ", ":=", " ", + RowBox[{"L", " ", + FractionBox[ + RowBox[{"fees", "[", + RowBox[{"\[CapitalDelta]", ",", "\[Gamma]"}], "]"}], + RowBox[{ + RowBox[{ + RowBox[{ + SubscriptBox["P", "Y"], "[", + RowBox[{"y", ",", "L", ",", "\[Mu]", ",", "\[Sigma]"}], "]"}], " ", + "x"}], " ", "+", " ", "y"}]]}]}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalDelta]", "X"], "[", + RowBox[{ + "\[CapitalDelta]_", ",", "x_", ",", "y_", ",", "L_", ",", "\[Mu]_", ",", + "\[Sigma]_", ",", "\[Gamma]_"}], "]"}], " ", ":=", " ", + RowBox[{ + RowBox[{ + RowBox[{"(", + RowBox[{"L", "+", + RowBox[{ + SubscriptBox["\[Delta]", "LiqY"], "[", + RowBox[{ + "\[CapitalDelta]", ",", "x", ",", "y", ",", "L", ",", "\[Mu]", ",", + "\[Sigma]", ",", "\[Gamma]"}], "]"}]}], ")"}], + RowBox[{"\[CapitalPhi]", "[", + RowBox[{ + RowBox[{"-", "\[Sigma]"}], " ", "-", " ", + RowBox[{ + SubscriptBox["\[CapitalPhi]", "inv"], "[", + FractionBox[ + RowBox[{"y", "+", "\[CapitalDelta]"}], + RowBox[{"\[Mu]", + RowBox[{"(", + RowBox[{"L", "+", + RowBox[{ + SubscriptBox["\[Delta]", "LiqY"], "[", + RowBox[{ + "\[CapitalDelta]", ",", "x", ",", "y", ",", "L", ",", "\[Mu]", + ",", "\[Sigma]", ",", "\[Gamma]"}], "]"}]}], ")"}]}]], "]"}]}], + "]"}]}], "-", "x"}]}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalDelta]", "Y"], "[", + RowBox[{ + "\[CapitalDelta]_", ",", "x_", ",", "y_", ",", "L_", ",", "\[Mu]_", ",", + "\[Sigma]_", ",", "\[Gamma]_"}], "]"}], " ", ":=", " ", + RowBox[{ + RowBox[{"\[Mu]", + RowBox[{"(", + RowBox[{"L", "+", + RowBox[{ + SubscriptBox["\[Delta]", "LiqX"], "[", + RowBox[{ + "\[CapitalDelta]", ",", "x", ",", "y", ",", "L", ",", "\[Mu]", ",", + "\[Sigma]", ",", "\[Gamma]"}], "]"}]}], ")"}], + RowBox[{"\[CapitalPhi]", "[", + RowBox[{ + RowBox[{"-", "\[Sigma]"}], " ", "-", " ", + RowBox[{ + SubscriptBox["\[CapitalPhi]", "inv"], "[", + FractionBox[ + RowBox[{"x", "+", "\[CapitalDelta]"}], + RowBox[{"L", "+", + RowBox[{ + SubscriptBox["\[Delta]", "LiqX"], "[", + RowBox[{ + "\[CapitalDelta]", ",", "x", ",", "y", ",", "L", ",", "\[Mu]", ",", + "\[Sigma]", ",", "\[Gamma]"}], "]"}]}]], "]"}]}], "]"}]}], "-", + "y"}]}]}], "Code", + CellChangeTimes->{{3.9113879019426517`*^9, 3.91138810608572*^9}, { + 3.911388182414874*^9, 3.911388482660475*^9}, {3.911388787674255*^9, + 3.911388979350903*^9}, {3.911389141187207*^9, 3.911389159323999*^9}, { + 3.911390630736039*^9, 3.911390698837728*^9}, {3.911408926773992*^9, + 3.911408926935534*^9}, {3.916935312155489*^9, 3.916935312305293*^9}, { + 3.9173824759156227`*^9, 3.917382476601667*^9}, {3.91738376350004*^9, + 3.917383784452558*^9}, {3.917383943744508*^9, 3.917383954282363*^9}, { + 3.9173839922487173`*^9, 3.9173839928360653`*^9}, {3.9173844534077587`*^9, + 3.91738450986141*^9}, {3.917384708602298*^9, 3.917384711107472*^9}, { + 3.917384741504918*^9, 3.917384745117815*^9}, {3.917384781098084*^9, + 3.917384801570612*^9}, {3.917384922013281*^9, 3.917384925581356*^9}, { + 3.9173850860945387`*^9, 3.917385090363779*^9}, 3.917385467785697*^9, { + 3.91857109514256*^9, 3.9185711020502167`*^9}, {3.91857136201642*^9, + 3.918571379355769*^9}, {3.9185719508515673`*^9, 3.9185719564304523`*^9}, { + 3.918572032513034*^9, 3.9185720372819643`*^9}, {3.918575843943832*^9, + 3.918575846003868*^9}, {3.918576114999403*^9, 3.9185761615915833`*^9}, { + 3.918576659877521*^9, 3.918576669588545*^9}, {3.918576871025722*^9, + 3.918576980060952*^9}, {3.9185799351979227`*^9, 3.918580034351376*^9}, { + 3.918580066820713*^9, 3.918580088205531*^9}, {3.91858024962215*^9, + 3.918580262523444*^9}, {3.918580294205887*^9, 3.918580298495079*^9}, { + 3.918580365884131*^9, 3.9185803726739683`*^9}, {3.9185815280229187`*^9, + 3.918581595304048*^9}, {3.918581637999057*^9, 3.9185817030183477`*^9}, { + 3.9185818429293957`*^9, 3.9185818876898746`*^9}, {3.9185823204024343`*^9, + 3.918582435530633*^9}, {3.918582488030525*^9, 3.918582492481691*^9}, { + 3.918582676185212*^9, 3.918582684377091*^9}, {3.9185827212029133`*^9, + 3.918582726305999*^9}, {3.918582845001643*^9, 3.918582856707839*^9}, { + 3.91858350972567*^9, 3.918583524907681*^9}, {3.9185836349151993`*^9, + 3.918583689775346*^9}, {3.918583814649831*^9, 3.918583887826089*^9}, + 3.918584019823391*^9, {3.918584115383397*^9, 3.918584125997015*^9}, { + 3.918584188749694*^9, 3.918584207807349*^9}, {3.918584252596579*^9, + 3.918584283333911*^9}, {3.9185843240947323`*^9, 3.9185843419430447`*^9}, { + 3.918584596408697*^9, 3.918584605431155*^9}, {3.918584698005369*^9, + 3.9185847022659883`*^9}, {3.918584733166923*^9, 3.9185847359196243`*^9}, { + 3.918584835029223*^9, 3.918584914920583*^9}, {3.918585923120076*^9, + 3.918585949059307*^9}, {3.918585981929215*^9, 3.918586212982409*^9}, { + 3.9185862713877707`*^9, 3.918586283220223*^9}, {3.918586430146008*^9, + 3.918586438229198*^9}, {3.91858647808181*^9, 3.9185865516402187`*^9}, { + 3.9185869770915956`*^9, 3.918586998684773*^9}, {3.9185871705919724`*^9, + 3.918587233694872*^9}, {3.918587688250499*^9, 3.918587732617826*^9}, { + 3.918587822943163*^9, 3.918587895921957*^9}, {3.918630539218589*^9, + 3.9186305548276157`*^9}, {3.918630656016925*^9, 3.918630658939457*^9}, { + 3.918630702505906*^9, 3.918630723206217*^9}, {3.918630766067203*^9, + 3.9186307800116253`*^9}, {3.918630857798958*^9, 3.918630861330348*^9}, { + 3.918630944696974*^9, 3.9186309775688953`*^9}, {3.918631062524468*^9, + 3.918631190734248*^9}, {3.9186313271535597`*^9, 3.91863134257788*^9}, { + 3.918631387338098*^9, 3.918631390137829*^9}, {3.918631437297716*^9, + 3.918631438126658*^9}, {3.918631612143648*^9, 3.9186316513861513`*^9}, { + 3.918631686728327*^9, 3.918631830046607*^9}, {3.918631896508091*^9, + 3.9186320069107533`*^9}, {3.91863303862149*^9, 3.9186330635458193`*^9}, { + 3.91863310072386*^9, 3.918633119499754*^9}, {3.918633166361033*^9, + 3.918633196016712*^9}, {3.9186332409230433`*^9, 3.918633311599324*^9}, { + 3.918633342234268*^9, 3.918633383646895*^9}, {3.918633455651833*^9, + 3.9186334734597473`*^9}, {3.9186432538684263`*^9, 3.918643296703499*^9}, { + 3.9186433525370207`*^9, 3.918643372417371*^9}, 3.918643647132059*^9, { + 3.9186456605015583`*^9, 3.918645704439562*^9}, {3.918645736019648*^9, + 3.9186457417525*^9}, {3.9186458580141783`*^9, 3.9186458880641613`*^9}, { + 3.918646011039798*^9, 3.9186460131439123`*^9}, {3.918646045288979*^9, + 3.918646059600195*^9}, {3.91864617045295*^9, 3.918646267932062*^9}, { + 3.9186464192975893`*^9, 3.91864641960671*^9}, {3.918646608483427*^9, + 3.91864684851731*^9}, {3.9186469185057583`*^9, 3.918647050242887*^9}, { + 3.918647093303738*^9, 3.918647093440069*^9}, {3.918647162357498*^9, + 3.9186471626747093`*^9}}, + CellLabel-> + "In[4174]:=",ExpressionUUID->"0716117c-5381-46be-8b57-f2cefa727879"], + +Cell[CellGroupData[{ + +Cell[BoxData[{ + RowBox[{ + RowBox[{"YIn", " ", "=", " ", "1"}], ";"}], "\n", + RowBox[{ + RowBox[{"XOut", " ", "=", " ", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "X"], "[", + RowBox[{"YIn", ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], ";"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{"XOut", ",", " ", "\"\\""}], "]"}], ";"}], " ", + RowBox[{"(*", " ", + RowBox[{"Should", " ", "be", " ", ".796"}], " ", "*)"}]}], "\n", + RowBox[{ + RowBox[{"DeltaL", " ", "=", " ", + RowBox[{ + SubscriptBox["\[Delta]", "LiqY"], "[", + RowBox[{"YIn", ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], ";"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"\[CurlyPhi]", "[", + RowBox[{ + RowBox[{ + SubscriptBox["x", "0"], "+", "XOut"}], ",", + RowBox[{ + SubscriptBox["y", "0"], "+", "YIn"}], ",", + RowBox[{ + SubscriptBox["L", "0"], "+", "DeltaL"}], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"]}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}], "\n"}], "\n", + RowBox[{ + RowBox[{"XIn", " ", "=", " ", "1"}], ";"}], "\n", + RowBox[{ + RowBox[{"YOut", " ", "=", " ", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "Y"], "[", + RowBox[{"XIn", ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], ";"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{"YOut", ",", " ", "\"\\""}], "]"}], ";"}], " ", + RowBox[{"(*", " ", + RowBox[{"Should", " ", "be", " ", "1.24375"}], " ", "*)"}]}], "\n", + RowBox[{ + RowBox[{"DeltaL", " ", "=", " ", + RowBox[{ + SubscriptBox["\[Delta]", "LiqX"], "[", + RowBox[{"XIn", ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], ";"}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"\[CurlyPhi]", "[", + RowBox[{ + RowBox[{ + SubscriptBox["x", "0"], "+", "XIn"}], ",", + RowBox[{ + SubscriptBox["y", "0"], "+", "YOut"}], ",", + RowBox[{ + SubscriptBox["L", "0"], "+", "DeltaL"}], ",", + SubscriptBox["\[Mu]", "0"], ",", + SubscriptBox["\[Sigma]", "0"]}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}]}], "Code", + CellChangeTimes->{{3.9173824881246443`*^9, 3.917382640107789*^9}, { + 3.917382703512905*^9, 3.917382797160448*^9}, {3.917382957143201*^9, + 3.9173829787289667`*^9}, {3.917383009866159*^9, 3.91738301413713*^9}, { + 3.9173830595966473`*^9, 3.9173830608297663`*^9}, {3.917383133177814*^9, + 3.917383139655548*^9}, {3.91738319090906*^9, 3.9173832152140627`*^9}, { + 3.917383329695533*^9, 3.9173833441047373`*^9}, {3.917383592136142*^9, + 3.917383601902347*^9}, {3.917383788771337*^9, 3.9173838368765507`*^9}, { + 3.917383998912529*^9, 3.917384002782464*^9}, {3.917384361112628*^9, + 3.917384432792912*^9}, {3.917384584058999*^9, 3.917384619281192*^9}, { + 3.9173846522116756`*^9, 3.9173846873539762`*^9}, {3.9173849883607492`*^9, + 3.917384990884163*^9}, {3.917385341574011*^9, 3.917385423584772*^9}, { + 3.918571382615984*^9, 3.918571412216847*^9}, {3.918571717856246*^9, + 3.918571718925044*^9}, {3.918571975672353*^9, 3.9185719781640577`*^9}, { + 3.918575850485878*^9, 3.918575871098755*^9}, {3.918580315169186*^9, + 3.9185803368583097`*^9}, {3.918582534732585*^9, 3.9185825771962633`*^9}, { + 3.918582688010583*^9, 3.9185826955344*^9}, {3.918582731539423*^9, + 3.9185827367343893`*^9}, {3.918582864604357*^9, 3.918582868781686*^9}, { + 3.918583482655109*^9, 3.918583500351995*^9}, 3.9185843702651167`*^9, { + 3.918584645054994*^9, 3.9185846751565027`*^9}, {3.918584720161016*^9, + 3.918584725365926*^9}, {3.918587956963675*^9, 3.918587962241294*^9}, + 3.918589138602861*^9, 3.9186320307912188`*^9, {3.9186331433380632`*^9, + 3.9186331543138247`*^9}, {3.9186468606918497`*^9, + 3.9186469067855988`*^9}, {3.918647072837221*^9, 3.918647073727294*^9}, { + 3.918647266809247*^9, 3.918647298407833*^9}}, + CellLabel-> + "In[4199]:=",ExpressionUUID->"2687c350-1150-4951-bcf0-28364fa5bf73"], + +Cell[CellGroupData[{ + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"XOut = \"\>", + "EchoLabel"], " ", + RowBox[{"-", "1.3266667127609253`"}]}]], "Echo", + CellChangeTimes->{ + 3.9186472988237343`*^9},ExpressionUUID->"27baf199-5392-4622-872b-\ +c04b2522ee2d"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"Validation = \"\>", + "EchoLabel"], " ", + RowBox[{"-", "2.220446049250313`*^-16"}]}]], "Echo", + CellChangeTimes->{ + 3.91864729885005*^9},ExpressionUUID->"eecd11fc-1f1d-4de9-9e4a-07d53c8e0fbb"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"YOut = \"\>", + "EchoLabel"], " ", + RowBox[{"-", "0.7462499886751175`"}]}]], "Echo", + CellChangeTimes->{ + 3.91864729885777*^9},ExpressionUUID->"292d880d-854e-49ac-80b7-b67aa90a72e5"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"Validation = \"\>", + "EchoLabel"], " ", + RowBox[{"-", "2.220446049250313`*^-16"}]}]], "Echo", + CellChangeTimes->{ + 3.9186472988780203`*^9},ExpressionUUID->"38d83234-0ac4-4733-b57c-\ +347036548a09"] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["Arbitrage", "Section", + CellChangeTimes->{{3.91138759286382*^9, 3.9113876060914783`*^9}, { + 3.911387637205267*^9, + 3.911387638219921*^9}},ExpressionUUID->"65d2b88d-22b2-481a-935c-\ +71bdce9f21f1"], + +Cell[TextData[{ + "We will assume there is some external price ", + Cell[BoxData[ + FormBox[ + SubscriptBox["S", "ext"], TraditionalForm]],ExpressionUUID-> + "cd8c38a7-d91f-4b2c-a735-67538238c600"], + "that we are given and decide whether or not to perform an arbitrage and, if \ +so, to get the optimal trade size. That is, the trade that gives the \ +arbitrageur maximal profit." +}], "Text", + CellChangeTimes->{{3.9113876414292507`*^9, 3.9113877131079063`*^9}, { + 3.911388488238481*^9, + 3.911388489476202*^9}},ExpressionUUID->"f20cba79-bbb2-4d1b-9349-\ +a349e8b0b7a4"], + +Cell[CellGroupData[{ + +Cell[TextData[{ + "We will need the marginal price ", + Cell[BoxData[ + FormBox[ + SubscriptBox["P", "M"], TraditionalForm]],ExpressionUUID-> + "17089434-c32f-45ea-a26f-0d3ed9139695"], + " of a swap to compute optimal arbitrages and a profit calculation ", + Cell[BoxData[ + FormBox[ + SubscriptBox["V", "A"], TraditionalForm]],ExpressionUUID-> + "72aada08-6991-4530-9774-3dd0a302c916"] +}], "Subsubsection", + CellChangeTimes->{{3.9113884927083406`*^9, 3.911388517540121*^9}, { + 3.911388583492565*^9, 3.911388585552403*^9}, {3.9113886383414087`*^9, + 3.911388651317487*^9}},ExpressionUUID->"d7131855-64d1-451f-98f3-\ +152b4aa9f3a5"], + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + SubscriptBox["P", "M"], "[", + RowBox[{"dX_", ",", "dY_"}], "]"}], " ", ":=", " ", + FractionBox[ + RowBox[{"-", "dY"}], "dX"]}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["V", "A"], "[", + RowBox[{"Pm_", ",", "Pext_", ",", "\[CapitalDelta]_"}], "]"}], " ", ":=", + " ", + RowBox[{ + RowBox[{"(", + RowBox[{"Pm", " ", "-", " ", "Pext"}], ")"}], + "\[CapitalDelta]"}]}]}], "Code", + CellChangeTimes->{{3.911388520734275*^9, 3.911388754553933*^9}, { + 3.911408038142099*^9, 3.9114080663589067`*^9}, {3.9114081240293818`*^9, + 3.91140812426967*^9}}, + CellLabel-> + "In[2448]:=",ExpressionUUID->"fadd5b7c-a968-401d-9e42-d6ca30b3a2a6"] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["Lower External Price:", "Subsection", + CellChangeTimes->{{3.911387716836122*^9, 3.9113877303598022`*^9}, { + 3.911389282458209*^9, 3.911389285697858*^9}, {3.9119143659239197`*^9, + 3.911914366787615*^9}},ExpressionUUID->"eed8b815-af5d-458f-8082-\ +a956f0fb88b9"], + +Cell[CellGroupData[{ + +Cell[TextData[{ + "We\[CloseCurlyQuote]ll let ", + Cell[BoxData[ + FormBox[ + SubscriptBox["O", "X"], TraditionalForm]],ExpressionUUID-> + "bea8f527-9f1f-453d-b766-3832796355bb"], + " be the optimal amount of X token to tender to achieve maximal arbitrage \ +profit." +}], "Subsubsection", + CellChangeTimes->{{3.911389287836197*^9, 3.911389310066874*^9}, { + 3.911389351175222*^9, + 3.911389351682972*^9}},ExpressionUUID->"b20dcf41-6dfe-404c-8167-\ +50b070efca5f"], + +Cell[CellGroupData[{ + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + SubscriptBox["S", "ext"], " ", "=", " ", ".70"}], ";", " ", + RowBox[{"Assert", "[", + RowBox[{ + SubscriptBox["S", "ext"], " ", "<", " ", + SubscriptBox["S", "0"]}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["Prof", "Lower"], "[", "in_", "]"}], " ", ":=", " ", + RowBox[{ + SubscriptBox["V", "A"], "[", + RowBox[{ + RowBox[{ + SubscriptBox["P", "M"], "[", + RowBox[{"in", ",", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "Y"], "[", + RowBox[{"in", ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], "]"}], ",", " ", + SubscriptBox["S", "ext"], ",", " ", "in"}], "]"}]}], "\n", + RowBox[{"Plot", "[", + RowBox[{ + RowBox[{ + SubscriptBox["Prof", "Lower"], "[", "v", "]"}], ",", " ", + RowBox[{"{", + RowBox[{"v", ",", "0", ",", "0.2"}], "}"}]}], "]"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{ + SubscriptBox["O", "X"], " ", "=", " ", + RowBox[{"ArgMax", "[", + RowBox[{ + RowBox[{"{", + RowBox[{ + RowBox[{ + SubscriptBox["Prof", "Lower"], "[", "x", "]"}], ",", " ", + RowBox[{"0", "<=", "x", "<=", + RowBox[{ + SubscriptBox["L", "0"], "-", + SubscriptBox["x", "0"]}]}]}], "}"}], ",", " ", "x"}], "]"}]}], + ";"}], " "}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + SubscriptBox["O", "X"], ",", " ", + "\"\\""}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"N", "[", + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalDelta]", "Y"], "[", + RowBox[{ + SubscriptBox["O", "X"], ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}], ",", "18"}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"{", + RowBox[{ + RowBox[{ + SubscriptBox["x", "0"], " ", "+", " ", + SubscriptBox["O", "X"]}], ",", " ", + RowBox[{ + SubscriptBox["y", "0"], " ", "+", " ", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "Y"], "[", + RowBox[{ + SubscriptBox["O", "X"], ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}]}], "}"}], ",", " ", + "\"\\""}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["P", "F"], " ", "=", " ", + RowBox[{ + SubscriptBox["P", "X"], "[", + RowBox[{ + RowBox[{ + SubscriptBox["x", "0"], " ", "+", " ", + SubscriptBox["O", "X"]}], ",", " ", + RowBox[{ + SubscriptBox["L", "0"], " ", "+", " ", + RowBox[{ + SubscriptBox["\[Delta]", "Liq"], "[", + RowBox[{ + SubscriptBox["O", "X"], ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], ",", " ", + SubscriptBox["K", "0"], ",", " ", + SubscriptBox["\[Sigma]", "0"], ",", " ", + SubscriptBox["\[Tau]", "0"]}], "]"}]}], ";", " ", + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"N", "[", + RowBox[{ + SubscriptBox["P", "F"], ",", "18"}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"FullSimplify", "[", + RowBox[{"D", "[", + RowBox[{ + RowBox[{ + SubscriptBox["V", "A"], "[", + RowBox[{ + RowBox[{ + SubscriptBox["P", "M"], "[", + RowBox[{"v", ",", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "Y"], "[", + RowBox[{ + "v", ",", "x", ",", "y", ",", "L", ",", "K", ",", "\[Sigma]", + ",", "\[Tau]", ",", "\[Gamma]"}], "]"}]}], "]"}], ",", " ", "S", + ",", " ", "v"}], "]"}], ",", "v"}], "]"}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}], + "\n", + RowBox[{"(*", " ", + RowBox[{ + "Check", " ", "that", " ", "the", " ", "trading", " ", "function", " ", + "is", " ", "invariant", " ", "under", " ", "the", " ", "swap"}], " ", + "*)"}]}], "\n", + RowBox[{ + RowBox[{"Assert", "[", + RowBox[{ + RowBox[{"Abs", "[", + RowBox[{"\[CurlyPhi]", "[", + RowBox[{ + RowBox[{ + SubscriptBox["x", "0"], " ", "+", " ", + SubscriptBox["O", "X"]}], ",", " ", + RowBox[{ + SubscriptBox["y", "0"], " ", "+", " ", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "Y"], "[", + RowBox[{ + SubscriptBox["O", "X"], ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], ",", " ", + RowBox[{ + SubscriptBox["L", "0"], " ", "+", " ", + RowBox[{ + SubscriptBox["\[Delta]", "in"], "[", + RowBox[{ + SubscriptBox["O", "X"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], ",", " ", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"]}], "]"}], "]"}], " ", "<", " ", + SuperscriptBox["10", + RowBox[{"-", "15"}]]}], "]"}], "\n"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"checkLower", "[", + RowBox[{ + "v_", ",", "x_", ",", "y_", ",", "L_", ",", "S_", ",", "K_", ",", + "\[Sigma]_", ",", "\[Tau]_", ",", "\[Gamma]_"}], "]"}], ":=", + RowBox[{ + RowBox[{"-", "S"}], "+", + FractionBox[ + RowBox[{ + SuperscriptBox["\[ExponentialE]", + RowBox[{ + RowBox[{"-", + FractionBox[ + RowBox[{ + SuperscriptBox["\[Sigma]", "2"], " ", "\[Tau]"}], "2"]}], "+", + RowBox[{ + SqrtBox["2"], " ", "\[Sigma]", " ", + SqrtBox["\[Tau]"], " ", + RowBox[{"InverseErfc", "[", + FractionBox[ + RowBox[{"2", " ", "x", " ", + RowBox[{"(", + RowBox[{"v", "+", "x"}], ")"}]}], + RowBox[{"L", " ", + RowBox[{"(", + RowBox[{"v", "+", "x", "-", + RowBox[{"v", " ", "\[Gamma]"}]}], ")"}]}]], "]"}]}]}]], " ", + "K", " ", "x", " ", "\[Gamma]"}], + RowBox[{"v", "+", "x", "-", + RowBox[{"v", " ", "\[Gamma]"}]}]], "+", + FractionBox[ + RowBox[{"K", " ", "L", " ", + RowBox[{"(", + RowBox[{ + RowBox[{"-", "1"}], "+", "\[Gamma]"}], ")"}], " ", + RowBox[{"Erfc", "[", + RowBox[{ + FractionBox[ + RowBox[{"\[Sigma]", " ", + SqrtBox["\[Tau]"]}], + SqrtBox["2"]], "-", + RowBox[{"InverseErfc", "[", + FractionBox[ + RowBox[{"2", " ", "x", " ", + RowBox[{"(", + RowBox[{"v", "+", "x"}], ")"}]}], + RowBox[{"L", " ", + RowBox[{"(", + RowBox[{"v", "+", "x", "-", + RowBox[{"v", " ", "\[Gamma]"}]}], ")"}]}]], "]"}]}], "]"}]}], + RowBox[{"2", " ", "x"}]]}]}], ";"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"numOne", "[", + RowBox[{ + "v_", ",", "x_", ",", "y_", ",", "L_", ",", "S_", ",", "K_", ",", + "\[Sigma]_", ",", "\[Tau]_", ",", "\[Gamma]_"}], "]"}], ":=", + RowBox[{ + SuperscriptBox["\[ExponentialE]", + RowBox[{ + RowBox[{"-", + FractionBox[ + RowBox[{ + SuperscriptBox["\[Sigma]", "2"], " ", "\[Tau]"}], "2"]}], "+", + RowBox[{ + SqrtBox["2"], " ", "\[Sigma]", " ", + SqrtBox["\[Tau]"], " ", + RowBox[{"InverseErfc", "[", + FractionBox[ + RowBox[{"2", " ", "x", " ", + RowBox[{"(", + RowBox[{"v", "+", "x"}], ")"}]}], + RowBox[{"L", " ", + RowBox[{"(", + RowBox[{"v", "+", "x", "-", + RowBox[{"v", " ", "\[Gamma]"}]}], ")"}]}]], "]"}]}]}]], " ", "K", + " ", "x", " ", "\[Gamma]"}]}], ";"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"denomOne", "[", + RowBox[{ + "v_", ",", "x_", ",", "y_", ",", "L_", ",", "S_", ",", "K_", ",", + "\[Sigma]_", ",", "\[Tau]_", ",", "\[Gamma]_"}], "]"}], ":=", + RowBox[{"v", "+", "x", "-", + RowBox[{"v", " ", "\[Gamma]"}]}]}], ";"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"numTwo", "[", + RowBox[{ + "v_", ",", "x_", ",", "y_", ",", "L_", ",", "S_", ",", "K_", ",", + "\[Sigma]_", ",", "\[Tau]_", ",", "\[Gamma]_"}], "]"}], ":=", + RowBox[{"K", " ", "L", " ", + RowBox[{"(", + RowBox[{ + RowBox[{"-", "1"}], "+", "\[Gamma]"}], ")"}], " ", + RowBox[{"Erfc", "[", + RowBox[{ + FractionBox[ + RowBox[{"\[Sigma]", " ", + SqrtBox["\[Tau]"]}], + SqrtBox["2"]], "-", + RowBox[{"InverseErfc", "[", + FractionBox[ + RowBox[{"2", " ", "x", " ", + RowBox[{"(", + RowBox[{"v", "+", "x"}], ")"}]}], + RowBox[{"L", " ", + RowBox[{"(", + RowBox[{"v", "+", "x", "-", + RowBox[{"v", " ", "\[Gamma]"}]}], ")"}]}]], "]"}]}], "]"}]}]}], + ";"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"denomTwo", "[", + RowBox[{ + "v_", ",", "x_", ",", "y_", ",", "L_", ",", "S_", ",", "K_", ",", + "\[Sigma]_", ",", "\[Tau]_", ",", "\[Gamma]_"}], "]"}], ":=", " ", + RowBox[{"2", " ", "x"}]}], ";"}], "\n"}], "Code", + CellChangeTimes->{{3.9113877530057907`*^9, 3.911387759870617*^9}, { + 3.911387791900612*^9, 3.911387835019555*^9}, {3.911389040938622*^9, + 3.911389111156786*^9}, {3.911389254777446*^9, 3.9113892749054213`*^9}, { + 3.911389313552825*^9, 3.9113896479421587`*^9}, {3.911389684738289*^9, + 3.911389710567501*^9}, {3.911389764399426*^9, 3.911390120184866*^9}, { + 3.911390172200013*^9, 3.9113903097104073`*^9}, {3.9113903414495697`*^9, + 3.911390422476118*^9}, {3.9113904688729887`*^9, 3.911390469654806*^9}, { + 3.911390530287198*^9, 3.9113905408414717`*^9}, {3.9113907054945307`*^9, + 3.91139073520191*^9}, {3.91139090146146*^9, 3.911390906904234*^9}, { + 3.911390946256706*^9, 3.911391064802693*^9}, {3.911391222082168*^9, + 3.9113912438598137`*^9}, {3.9114061934461813`*^9, 3.911406197897942*^9}, { + 3.911409265595838*^9, 3.911409282641177*^9}, {3.9114093384435053`*^9, + 3.911409346040655*^9}, {3.911409904932541*^9, 3.911409910625359*^9}, + 3.911410010310408*^9, {3.9114111732761717`*^9, 3.911411210958044*^9}, { + 3.911411248268466*^9, 3.911411313752475*^9}, {3.911413086350491*^9, + 3.9114131282498713`*^9}, {3.91141319914225*^9, 3.911413480385025*^9}, { + 3.91675072235946*^9, 3.916750736241868*^9}, {3.916750835562142*^9, + 3.916750853604291*^9}, {3.916933983207768*^9, 3.916933985584363*^9}, { + 3.916934589170532*^9, 3.916934742188787*^9}, {3.91738501952264*^9, + 3.917385025286654*^9}, {3.918585613443736*^9, 3.918585616419739*^9}}, + CellLabel-> + "In[2450]:=",ExpressionUUID->"6bec84e7-f813-4bb5-9a1e-8a3d86e897da"], + +Cell[BoxData[ + GraphicsBox[{{{}, {}, + TagBox[ + {RGBColor[0.368417, 0.506779, 0.709798], AbsoluteThickness[1.6], Opacity[ + 1.], LineBox[CompressedData[" +1:eJwV0Ws01AkABXDklayUKWOijaOTOB5bK1ZxPUqiFCrPQliPEqMhMV7/VVGE +Etswq8Wiklc1ixAG46+EVDImJBKSsKi8Vh/uud/uh99VPhVg6yUiJCR0aCU/ +Onu0gtF6+Rej4nC9wE9X1Gr8RDbUllFkobFYnz4z0mw0Sw0rSaJsgZKozYz4 +oS6jPbmFapEUHaRJUHOTPgwb9Y00egZTjLE/PnzermfOqDThhQidcgQalprb +OG0SoPW+6ThLccMVdXHD4V55PBd3qfCnBOI5a4q33L0NlHeHHBProiEYvqkd +90of9B0Z78WG4qD0VJceG2SKzYyr420ByZDo8mK7u1jgO919uZCbit/9C/kC +njWKtstQM1r/BL+Jt/Wuuy3sRTvNFbTYMMps+PAw6xgy4uQ3Uqpvgyqtuk6m +zQFzYzrSqjXZeFFrc0yX54znbsd/Yo7mor1SKnt++SRSdgynCCbzMLxgIUoV +dkcgm0sXmyhA9IjhGcewU2hRKHQJkb8HXmioA0vOE7LTyy0TtPu4qM6yqU/x +Qsdb172lB4oxGincdkvbG10lFxOU/i2B/xZaTGiTD/Yop5R/4JTCeVDB+KSJ +H06wVEbtnpVBzsr1j3uC05ip8z2xr+0Btr+82gkXf1hN7XrNH36IgeXF/J75 +s9CauMM8Ov8InlXJDnqWgbjEeuFpu8DBVqk0jypJOjKtY2z/W18Oje01Okpd +dKiVDsXQ5CvA7HaMnUkPgmn/RK/qxkq46lgHDR09BwVqtEmn+mNccvBSmVRm +wHupcUhMswoD6744+Lxj4Nx4SpJgbzWyMtk2+SXBkL6zyjvdvgZTrTmDhswQ +0OQ0ol/bPwEzWWlJzuA8pk3PtAl21cJJtbJbVzIUqZ3sqOTyWjAPtOx2bg4F +0zLbnKNXB+0NO/Vfxl4At9FXtqiuDkplNbNuNmHYR5We9zath09/9W4x2XBs +DaB/nG6th3B/n+libziM2aWzJke4eBYY3K54m4ns0oZNIu1cGBR4fA04EQGP +FpZZqlMDPpUwBmrUI+FaaZXwsacBI5bfbC+MRqKy6EmXjE8jtDm3L9pVRsEl +Xtwgf7IRyx5H7lDMovHbbrVq4aAmXNKVsrPKjgZjUtpMa7EJdQGv3h5eHbPy +67MRxSgebgTxNVi+McifOpkfscRDRfOa/ZLNMeD6PfQuiWtG1GVrs7NaBGiV +gxb/rCbRL/NUZCmRwGKEKG1oDYkpJkuQl0Sg30R1TFWGxOoxX45zCoE80iMx +dz2JzTxJvwepBHZ2D3TkbCJxOsq8wzaDwMGv/U7ZmiTGJuqyHhcQiNj19kyW +DYn59vI9XlwCbt8XDPvsVvYQt/FjA4G9NYprfz5OQvK+/RdOEwEpc5fSv5xI +aMbP5ki2EEg71jPD9iARYvLrmsJ2AkWM7sjMYBJCZcXd4r0EUvS/HRacJ0HZ +EvXg7z4CjAWqsmIYCZVr1omn3hEwiHXgZkSS2O83blw+SIB3o0si4zIJQkWj +wHmMwF37uTf8eBJJyd9jRMcJXNskf5eWQIK9RDpXfCZwNOf4QVYyicd8n7Vz +kwT0vEOU+NdJvLbQHymeXvHTSPuscJPEe44E129mxe/zoyeO6SQWVbsyV82t ++JW9Sr51i8TS9byQqq8E/gd1j1xk + "]]}, + Annotation[#, "Charting`Private`Tag$18998#1"]& ]}, {}}, + AspectRatio->NCache[GoldenRatio^(-1), 0.6180339887498948], + Axes->{True, True}, + AxesLabel->{None, None}, + AxesOrigin->{0, 0}, + DisplayFunction->Identity, + Frame->{{False, False}, {False, False}}, + FrameLabel->{{None, None}, {None, None}}, + FrameTicks->{{Automatic, Automatic}, {Automatic, Automatic}}, + GridLines->{None, None}, + GridLinesStyle->Directive[ + GrayLevel[0.5, 0.4]], + ImagePadding->All, + Method->{ + "DefaultBoundaryStyle" -> Automatic, + "DefaultGraphicsInteraction" -> { + "Version" -> 1.2, "TrackMousePosition" -> {True, False}, + "Effects" -> { + "Highlight" -> {"ratio" -> 2}, "HighlightPoint" -> {"ratio" -> 2}, + "Droplines" -> { + "freeformCursorMode" -> True, + "placement" -> {"x" -> "All", "y" -> "None"}}}}, "DefaultMeshStyle" -> + AbsolutePointSize[6], "ScalingFunctions" -> None, + "CoordinatesToolOptions" -> {"DisplayFunction" -> ({ + (Identity[#]& )[ + Part[#, 1]], + (Identity[#]& )[ + Part[#, 2]]}& ), "CopiedValueFunction" -> ({ + (Identity[#]& )[ + Part[#, 1]], + (Identity[#]& )[ + Part[#, 2]]}& )}}, + PlotRange->{{0, 0.2}, {-2.8571428571428568`*^-9, 0.009263489123279008}}, + PlotRangeClipping->True, + PlotRangePadding->{{ + Scaled[0.02], + Scaled[0.02]}, { + Scaled[0.05], + Scaled[0.05]}}, + Ticks->{Automatic, Automatic}]], "Output", + CellChangeTimes->{ + 3.917383563255979*^9, 3.917384872931774*^9, {3.9173849757089787`*^9, + 3.9173850261946993`*^9}, 3.917385114645734*^9, 3.918585617114811*^9}, + CellLabel-> + "Out[2452]=",ExpressionUUID->"ff2c3a54-a3b1-4be6-afbb-32094a16572f"], + +Cell[CellGroupData[{ + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The optimal amount of X to tender is: \\!\\(\\*SubscriptBox[\\(\ +\[CapitalDelta]\\), \\(X\\)]\\) = \"\>", + "EchoLabel"], " ", "6.230446865679352`*^7"}]], "Echo", + CellChangeTimes->{ + 3.918585617388137*^9},ExpressionUUID->"6c3d9d24-049c-4137-b7e3-\ +6cf83d94b59c"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The amount out is: \\!\\(\\*SubscriptBox[\\(\[CapitalDelta]\\), \ +\\(Y\\)]\\) = \"\>", + "EchoLabel"], " ", + RowBox[{"-", "4.51112226444927`*^7"}]}]], "Echo", + CellChangeTimes->{ + 3.918585617396303*^9},ExpressionUUID->"255c6473-c036-4f07-8655-\ +af783da13368"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The resulting reserves are: (\\!\\(\\*SubscriptBox[\\(x\\), \ +\\(1\\)]\\),\\!\\(\\*SubscriptBox[\\(y\\), \\(1\\)]\\)) = \"\>", + "EchoLabel"], " ", + RowBox[{"{", + RowBox[{"1.0623044686567935`*^9", ",", "7.409463578271984`*^7"}], + "}"}]}]], "Echo", + CellChangeTimes->{ + 3.9185856174259367`*^9},ExpressionUUID->"a6e336c8-67e3-4548-aec6-\ +53634d7b4e40"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The final price of the pool is: P = \"\>", + "EchoLabel"], " ", "0.7033820512261256`"}]], "Echo", + CellChangeTimes->{ + 3.918585617433468*^9},ExpressionUUID->"5f8a3663-d1c0-4227-93e2-\ +6e4110936aef"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The equation to do root finding with is: \"\>", + "EchoLabel"], " ", + TemplateBox[{ + RowBox[{ + RowBox[{"-", "S"}], "+", + FractionBox[ + RowBox[{ + SuperscriptBox["\[ExponentialE]", + RowBox[{ + RowBox[{"-", + FractionBox["1", "2"]}], " ", "\[Sigma]", " ", + RowBox[{"(", + RowBox[{"\[Sigma]", "-", + RowBox[{"2", " ", + SqrtBox["2"], " ", + RowBox[{"InverseErfc", "[", + FractionBox[ + RowBox[{"2", " ", + RowBox[{"(", + RowBox[{"v", "+", "x"}], ")"}]}], + RowBox[{"L", "+", "v", "-", + RowBox[{"v", " ", "\[Gamma]"}]}]], "]"}]}]}], ")"}]}]], + " ", "K", " ", + RowBox[{"(", + RowBox[{"L", "+", + RowBox[{"x", " ", + RowBox[{"(", + RowBox[{ + RowBox[{"-", "1"}], "+", "\[Gamma]"}], ")"}]}]}], ")"}]}], + + RowBox[{"L", "+", "v", "-", + RowBox[{"v", " ", "\[Gamma]"}]}]], "+", + RowBox[{ + FractionBox["1", "2"], " ", "K", " ", + RowBox[{"(", + RowBox[{ + RowBox[{"-", "1"}], "+", "\[Gamma]"}], ")"}], " ", + RowBox[{"Erfc", "[", + RowBox[{ + FractionBox["\[Sigma]", + SqrtBox["2"]], "-", + RowBox[{"InverseErfc", "[", + FractionBox[ + RowBox[{"2", " ", + RowBox[{"(", + RowBox[{"v", "+", "x"}], ")"}]}], + RowBox[{"L", "+", "v", "-", + RowBox[{"v", " ", "\[Gamma]"}]}]], "]"}]}], "]"}]}]}], + RowBox[{"0", "\[LessEqual]", + FractionBox[ + RowBox[{"v", "+", "x"}], + RowBox[{"L", "+", "v", "-", + RowBox[{"v", " ", "\[Gamma]"}]}]], "\[LessEqual]", "1"}]}, + "ConditionalExpression"]}]], "Echo", + CellChangeTimes->{ + 3.918585617718585*^9},ExpressionUUID->"8b3d5769-45e4-45af-b1b7-\ +284acb1d88ab"] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["Raise External Price:", "Subsection", + CellChangeTimes->{{3.9114058960816593`*^9, + 3.911405902149222*^9}},ExpressionUUID->"d44a8cc6-b4f0-4204-b250-\ +e68ad4e19d99"], + +Cell[CellGroupData[{ + +Cell[TextData[{ + "Let ", + Cell[BoxData[ + FormBox[ + SubscriptBox["O", "Y"], TraditionalForm]],ExpressionUUID-> + "daf8aba3-0522-4fdc-b0f6-095c37f44dc7"], + " be the optimal amount of Y token to tender to get max arbitrage profit." +}], "Subsubsection", + CellChangeTimes->{{3.9114059041043043`*^9, + 3.9114059228094807`*^9}},ExpressionUUID->"800e047e-b69c-4509-bda4-\ +30799fa9a63e"], + +Cell[CellGroupData[{ + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + SubscriptBox["S", "ext"], " ", "=", " ", "2.2"}], ";", " ", + RowBox[{"Assert", "[", + RowBox[{ + SubscriptBox["S", "ext"], " ", ">", " ", + SubscriptBox["S", "0"]}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["Prof", "Raise"], "[", "in_", "]"}], " ", ":=", " ", + RowBox[{ + SubscriptBox["V", "A"], "[", + RowBox[{ + RowBox[{ + SubscriptBox["P", "M"], "[", + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalDelta]", "X"], "[", + RowBox[{"in", ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}], ",", "in"}], "]"}], ",", " ", + + SubscriptBox["S", "ext"], ",", " ", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "X"], "[", + RowBox[{"in", ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], "]"}]}], "\n", + RowBox[{"Plot", "[", + RowBox[{ + RowBox[{ + SubscriptBox["Prof", "Raise"], "[", "v", "]"}], ",", " ", + RowBox[{"{", + RowBox[{"v", ",", "0", ",", "0.2"}], "}"}]}], "]"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{ + SubscriptBox["O", "Y"], " ", "=", " ", + RowBox[{"ArgMax", "[", + RowBox[{ + RowBox[{"{", + RowBox[{ + RowBox[{ + SubscriptBox["Prof", "Raise"], "[", "y", "]"}], ",", " ", + RowBox[{"0", "<=", "y", "<=", + RowBox[{ + RowBox[{ + SubscriptBox["K", "0"], + SubscriptBox["L", "0"]}], "-", + SubscriptBox["y", "0"]}]}]}], "}"}], ",", " ", "y"}], "]"}]}], + ";"}], " "}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + SubscriptBox["O", "Y"], ",", " ", + "\"\\""}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"N", "[", + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalDelta]", "X"], "[", + RowBox[{ + SubscriptBox["O", "Y"], ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}], ",", "18"}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"{", + RowBox[{ + RowBox[{ + SubscriptBox["x", "0"], " ", "+", " ", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "X"], "[", + RowBox[{ + SubscriptBox["O", "Y"], ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], ",", " ", + RowBox[{ + SubscriptBox["y", "0"], " ", "+", " ", + SubscriptBox["O", "Y"]}]}], "}"}], ",", " ", + "\"\\""}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["P", "F"], " ", "=", " ", + RowBox[{ + SubscriptBox["P", "Y"], "[", + RowBox[{ + RowBox[{ + SubscriptBox["y", "0"], " ", "+", " ", + SubscriptBox["O", "Y"]}], ",", " ", + RowBox[{ + SubscriptBox["L", "0"], " ", "+", " ", + RowBox[{ + FractionBox["1", + SubscriptBox["K", "0"]], + RowBox[{ + SubscriptBox["\[Delta]", "in"], "[", + RowBox[{ + SubscriptBox["O", "Y"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}]}], ",", " ", + SubscriptBox["K", "0"], ",", " ", + SubscriptBox["\[Sigma]", "0"], ",", " ", + SubscriptBox["\[Tau]", "0"]}], "]"}]}], ";", " ", + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"N", "[", + RowBox[{ + SubscriptBox["P", "F"], ",", " ", "18"}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"FullSimplify", "[", + RowBox[{"D", "[", + RowBox[{ + RowBox[{ + SubscriptBox["V", "A"], "[", + RowBox[{ + RowBox[{ + SubscriptBox["P", "M"], "[", + RowBox[{ + RowBox[{ + SubscriptBox["\[CapitalDelta]", "X"], "[", + RowBox[{ + "v", ",", "x", ",", "y", ",", "L", ",", "K", ",", "\[Sigma]", + ",", "\[Tau]", ",", "\[Gamma]"}], "]"}], ",", "v"}], "]"}], ",", + " ", "S", ",", " ", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "X"], "[", + RowBox[{ + "v", ",", "x", ",", "y", ",", "L", ",", "K", ",", "\[Sigma]", ",", + "\[Tau]", ",", "\[Gamma]"}], "]"}]}], "]"}], ",", "v"}], "]"}], + "]"}], ",", " ", "\"\\""}], + "]"}], ";"}], "\n", + RowBox[{"(*", " ", + RowBox[{ + "Check", " ", "that", " ", "the", " ", "trading", " ", "function", " ", + "is", " ", "invariant", " ", "under", " ", "the", " ", "swap"}], " ", + "*)"}]}], "\n", + RowBox[{"Assert", "[", + RowBox[{ + RowBox[{"Abs", "[", + RowBox[{"\[CurlyPhi]", "[", + RowBox[{ + RowBox[{ + SubscriptBox["x", "0"], " ", "+", " ", + RowBox[{ + SubscriptBox["\[CapitalDelta]", "X"], "[", + RowBox[{ + SubscriptBox["O", "Y"], ",", + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", + SubscriptBox["L", "0"], ",", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}], ",", " ", + RowBox[{ + SubscriptBox["y", "0"], " ", "+", " ", + SubscriptBox["O", "Y"]}], ",", " ", + RowBox[{ + SubscriptBox["L", "0"], " ", "+", " ", + RowBox[{ + FractionBox["1", + SubscriptBox["K", "0"]], + RowBox[{ + SubscriptBox["\[Delta]", "in"], "[", + RowBox[{ + SubscriptBox["O", "Y"], ",", + SubscriptBox["\[Gamma]", "0"]}], "]"}]}]}], ",", " ", + SubscriptBox["K", "0"], ",", + SubscriptBox["\[Sigma]", "0"], ",", + SubscriptBox["\[Tau]", "0"]}], "]"}], "]"}], " ", "<", " ", + SuperscriptBox["10", + RowBox[{"-", "14"}]]}], "]"}]}], "Code", + CellChangeTimes->CompressedData[" +1:eJwdxU0oQwEAB/D3nsumRlKGcVhtIsVFESPPmNmBfFxmiqxZjWFoDcUFS5NW +aj7bYUQ+pmjJVsJuyhSJ5OsiankjbT5Ww/u/w6+fuL2nQUcRBJHBwr1iQZ5B +wtA+5bQCS3yaOnznza3H/kBRC247W+B+Jt19eCRqHOBeGq/oZNe8CuSYP1da +jQvMWiWmnVVNODz43Yq3RZdarJ+eMODQvH0K1+g8DuyaTL3FjvDMPfY+lr/g +DtnaF1YcrsRw9/AH0cWefUVc4LHfo2scv296NLLv2mxPuKycTNdKGdoel8Yd +pJngYh5Dq+SWN2xeLiGX2BWhUQqH1L+mCDujPO/DTuGbFYcbrVP4L0DNYnKH +z12Zs+HEN7xt7hRV4ip2FWdy77nM5Ce79NRN4droQQoeSxIJMWHTF+KwLVmG +Le/rm+f5DM2LqLew/6ffg5mEB27LUPMxDsWyTvA//Rnfcg== + "], + CellLabel-> + "In[683]:=",ExpressionUUID->"7ca95c98-841f-4a4d-8646-7ee5087c0cbe"], + +Cell[BoxData[ + GraphicsBox[{{{}, {}, + TagBox[ + {RGBColor[0.368417, 0.506779, 0.709798], AbsoluteThickness[1.6], Opacity[ + 1.], LineBox[CompressedData[" +1:eJwV0nk41AkAxnH3VjOsh5GZIWrXdklbrVLE60oyZUWRUmzaBmUZDZWwM+Ws +IeNnKdKhlhw7a2yE1tWMaX9K0uEYR5aQI0fpcmTtH+/z/f/zvMsOB7n+rKSg +oLBrfv83e6ic2xC33sogIXXggPO0ZYCSTk0xTRMZ1N48O2qb1Qd6eNFF2lJQ +W6QG6tRBq623CldG0dZBJ0GJO0H5bPVysO5IKM0aomlzs17KQogFT5U4NBdI +FY02tlMYYHa1Nv1C84H58JegF5RVeKzmVR5IC8YH70KVRsoW0P7d5ZlYy8OB ++GHbQIYTOBsye1X74lFwjTHG8nWDAffCm8agZHhNDAcsyPDEFOenuUJJKkyD +uTmksQ9EqzTomQ2XMEVX7fzk4gsPlWcOjLVZcO2Ma0gqP4rMeN3FtMrr8Az7 +dsVmrQB8HF5HNarKhnJueytrfSAe+7irRwzdwrT3XHPPjmAINwwIOyZyMNKk +pb23i4PgLAlHdew2DEWmzk0BJ1DPKPQK0y2A00PZbgVGKDTfzdWPMf/AaAN9 +9Eh+GJo6ve3FO/7Eq5OhiyMOnkJLUYxgyd0i+Ff3Lc9aEI6ty4Rl/aVinDJk +MCbTzuBgxjdDbo+KobWCvXPWIRLva/0Pbmv8C+PaN2K3yqPAerupWT5wBwWB +BT1p23lYO5YXsWe6BBuXWkXTDPiIzXh6xHWmFC3Gs7tM+vm44sx3ndQqQ7WR +9aRB9VmsFPfxmbrleJ8X+KIg4Rxsu8e6jBZXIDHdosvSJRoMOs/m2ep7UKbW +Xr29IgbsL3V9qiZ/wzdnpFI0HoMTb4QXO+wr8XwqNyyxPhbUPGV2ukcVRHe5 +LM71ODC1jXnNHtUgFUJujrPj8c72eGPHphpsNvMLcrROQOqzrF+Ty2ow8RlN +FovOI8Ip26HUrBYO+45zTZ+ch6TOX1NUWwvdlpGjqrkXsI1OnWbb3odezxut +kCABvgvivH7XcB9V6ozxUsdEWGeJP9i4SLBwTfjldO0kZIulekpPJNBkICVS +ngTf+gy71P1SFC0/mm5RfBHeFSzB63YprsnsZPWWyagQVbdo+NXBr3Lgbm/+ +/K8S1MxzJ+qwvITDmtMRYovFykrFEBmuC5wHX58WgjtBtVs7K8NM4rFjVv1C +DEU9GtT/9QGGLdYoHmalIPftodzILw+gZhDLihelQBJwh10U/w88kmvYTCYB +ZsUrx98XkvA9fCYvPIrAbKQKs49CghdW027OI9BtYzRspEHiyiIFjWk+gRzS +N/GWFglThl/ImRgCP7T1NN3UI7FFedI8QkBg56fu/dkmJFqDJ+sjMwhEbuo8 +fm03iahifj+vlIDP1IzlSzcSV2UUuk0ZAfsq/a8N3UkUyM/vUKwgsMjBS3x1 +PwlH/5OF/EoCaXvb32f5ktjjphtyVkpAxG2LuhJKIlW+eubcUwLCzZ9/7DhJ +Qmevu4n9cwLcGfoy/XASpNLpQyrNBMyj90kyo0gIpYU10W0EHhAtX2XGkbgk +ehgd000g3+NjqzyBhHZeXcm2HgJJerr5TAEJSXNpv+orAntuuu/MSCZxg57o +GDtAwIwdtkSeQmK7LPC0w+C8n3HaKOO3eR91p3y14Xm/0ZJqz3QSrvcM22Uj +837FL5IvXybxvf5bStwogf8AM+pSBQ== + "]]}, + Annotation[#, "Charting`Private`Tag$100831#1"]& ]}, {}}, + AspectRatio->NCache[GoldenRatio^(-1), 0.6180339887498948], + Axes->{True, True}, + AxesLabel->{None, None}, + AxesOrigin->{0, 0}, + DisplayFunction->Identity, + Frame->{{False, False}, {False, False}}, + FrameLabel->{{None, None}, {None, None}}, + FrameTicks->{{Automatic, Automatic}, {Automatic, Automatic}}, + GridLines->{None, None}, + GridLinesStyle->Directive[ + GrayLevel[0.5, 0.4]], + ImagePadding->All, + Method->{ + "DefaultBoundaryStyle" -> Automatic, + "DefaultGraphicsInteraction" -> { + "Version" -> 1.2, "TrackMousePosition" -> {True, False}, + "Effects" -> { + "Highlight" -> {"ratio" -> 2}, "HighlightPoint" -> {"ratio" -> 2}, + "Droplines" -> { + "freeformCursorMode" -> True, + "placement" -> {"x" -> "All", "y" -> "None"}}}}, "DefaultMeshStyle" -> + AbsolutePointSize[6], "ScalingFunctions" -> None, + "CoordinatesToolOptions" -> {"DisplayFunction" -> ({ + (Identity[#]& )[ + Part[#, 1]], + (Identity[#]& )[ + Part[#, 2]]}& ), "CopiedValueFunction" -> ({ + (Identity[#]& )[ + Part[#, 1]], + (Identity[#]& )[ + Part[#, 2]]}& )}}, + PlotRange->{{0, 0.2}, {0., 0.01946459780131365}}, + PlotRangeClipping->True, + PlotRangePadding->{{ + Scaled[0.02], + Scaled[0.02]}, { + Scaled[0.05], + Scaled[0.05]}}, + Ticks->{Automatic, Automatic}]], "Output", + CellChangeTimes->{ + 3.9173835690887947`*^9, 3.917385059548512*^9, {3.9173851196395884`*^9, + 3.917385128134555*^9}, 3.9173852324926853`*^9, 3.917385290081284*^9}, + CellLabel-> + "Out[685]=",ExpressionUUID->"1f3d487e-7d60-4563-a2b3-f3aa65c1d146"], + +Cell[CellGroupData[{ + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The optimal amount of Y to tender is: \\!\\(\\*SubscriptBox[\\(\ +\[CapitalDelta]\\), \\(Y\\)]\\) = \"\>", + "EchoLabel"], " ", "364.4731544001954`"}]], "Echo", + CellChangeTimes->{ + 3.917385290172243*^9},ExpressionUUID->"20a8471e-d48d-4dc1-8a0f-\ +2b45a51698ee"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The amount out is: \\!\\(\\*SubscriptBox[\\(\[CapitalDelta]\\), \ +\\(X\\)]\\) = \"\>", + "EchoLabel"], " ", + RowBox[{"-", "173.56889322451593`"}]}]], "Echo", + CellChangeTimes->{ + 3.917385290180708*^9},ExpressionUUID->"5cd69ac1-3ee9-4f3c-b5cf-\ +95d07eb0ff1c"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The resulting reserves are: (\\!\\(\\*SubscriptBox[\\(x\\), \ +\\(1\\)]\\),\\!\\(\\*SubscriptBox[\\(y\\), \\(1\\)]\\)) = \"\>", + "EchoLabel"], " ", + RowBox[{"{", + RowBox[{"826.4311067754841`", ",", "2364.4731544001957`"}], + "}"}]}]], "Echo", + CellChangeTimes->{ + 3.917385290202286*^9},ExpressionUUID->"0834851c-ee77-4202-aa72-\ +ad7a0f04c869"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The final price of the pool is: P = \"\>", + "EchoLabel"], " ", "2.194468360784738`"}]], "Echo", + CellChangeTimes->{ + 3.917385290210438*^9},ExpressionUUID->"e88a31f0-9ce3-43a8-a4fe-\ +062f673969b4"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The equation to do root finding with is: \"\>", + "EchoLabel"], " ", + TemplateBox[{ + RowBox[{ + RowBox[{"-", "1"}], "+", + FractionBox[ + RowBox[{ + SuperscriptBox["\[ExponentialE]", + RowBox[{ + RowBox[{"-", + FractionBox[ + RowBox[{ + SuperscriptBox["\[Sigma]", "2"], " ", "\[Tau]"}], "2"]}], + "+", + RowBox[{ + SqrtBox["2"], " ", "\[Sigma]", " ", + SqrtBox["\[Tau]"], " ", + RowBox[{"InverseErfc", "[", + FractionBox[ + RowBox[{"2", " ", + RowBox[{"(", + RowBox[{"v", "+", "y"}], ")"}]}], + RowBox[{ + RowBox[{"K", " ", "L"}], "+", "v", "-", + RowBox[{"v", " ", "\[Gamma]"}]}]], "]"}]}]}]], " ", "S", + " ", + RowBox[{"(", + RowBox[{ + RowBox[{"K", " ", "L"}], "+", + RowBox[{"y", " ", + RowBox[{"(", + RowBox[{ + RowBox[{"-", "1"}], "+", "\[Gamma]"}], ")"}]}]}], ")"}]}], + + RowBox[{"K", " ", + RowBox[{"(", + RowBox[{ + RowBox[{"K", " ", "L"}], "+", "v", "-", + RowBox[{"v", " ", "\[Gamma]"}]}], ")"}]}]], "+", + FractionBox[ + RowBox[{"S", " ", + RowBox[{"(", + RowBox[{ + RowBox[{"-", "1"}], "+", "\[Gamma]"}], ")"}], " ", + RowBox[{"Erfc", "[", + RowBox[{ + FractionBox[ + RowBox[{"\[Sigma]", " ", + SqrtBox["\[Tau]"]}], + SqrtBox["2"]], "-", + RowBox[{"InverseErfc", "[", + FractionBox[ + RowBox[{"2", " ", + RowBox[{"(", + RowBox[{"v", "+", "y"}], ")"}]}], + RowBox[{ + RowBox[{"K", " ", "L"}], "+", "v", "-", + RowBox[{"v", " ", "\[Gamma]"}]}]], "]"}]}], "]"}]}], + RowBox[{"2", " ", "K"}]]}], + RowBox[{"0", "\[LessEqual]", + FractionBox[ + RowBox[{"v", "+", "y"}], + RowBox[{ + RowBox[{"K", " ", "L"}], "+", "v", "-", + RowBox[{"v", " ", "\[Gamma]"}]}]], "\[LessEqual]", "1"}]}, + "ConditionalExpression"]}]], "Echo", + CellChangeTimes->{ + 3.917385290654476*^9},ExpressionUUID->"24b12cbb-7a22-45fd-bb9e-\ +d5824114ff9b"] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]], + +Cell[CellGroupData[{ + +Cell["Parameter Updates", "Section", + CellChangeTimes->{{3.9114106614664173`*^9, + 3.911410663603334*^9}},ExpressionUUID->"b9760121-93f2-4105-bc65-\ +e6b3dbe04aec"], + +Cell[CellGroupData[{ + +Cell["\<\ +We want to let parameters change, then determine the new L from them.\ +\>", "Subsection", + CellChangeTimes->{{3.9114115124928417`*^9, + 3.911411520281617*^9}},ExpressionUUID->"bf3ecf5a-6ea2-4f9a-a236-\ +2d3956e9c134"], + +Cell[CellGroupData[{ + +Cell[BoxData[{ + RowBox[{ + RowBox[{ + RowBox[{"{", + RowBox[{ + SubscriptBox["K", "1"], ",", + SubscriptBox["\[Sigma]", "1"], ",", + SubscriptBox["\[Tau]", "1"]}], "}"}], " ", "=", " ", + RowBox[{"{", + RowBox[{ + RowBox[{ + SubscriptBox["K", "0"], " ", "+", " ", + FractionBox["1", "10"]}], ",", " ", + RowBox[{ + SubscriptBox["\[Sigma]", "0"], " ", "-", " ", + FractionBox["1", "20"]}], ",", " ", + SubscriptBox["\[Tau]", "0"]}], "}"}]}], ";"}], "\n", + RowBox[{ + RowBox[{ + SubscriptBox["L", "1"], " ", "=", " ", + RowBox[{"L", " ", "/.", " ", + RowBox[{"FindRoot", "[", + RowBox[{ + RowBox[{"\[CurlyPhi]", "[", + RowBox[{ + SubscriptBox["x", "0"], ",", + SubscriptBox["y", "0"], ",", "L", ",", + SubscriptBox["K", "1"], ",", + SubscriptBox["\[Sigma]", "1"], ",", + SubscriptBox["\[Tau]", "1"]}], "]"}], ",", " ", + RowBox[{"{", + RowBox[{"L", ",", + SubscriptBox["L", "0"]}], "}"}]}], "]"}]}]}], ";"}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"N", "[", + RowBox[{ + SubscriptBox["L", "0"], ",", "18"}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}], "\n", + RowBox[{ + RowBox[{"Echo", "[", + RowBox[{ + RowBox[{"N", "[", + RowBox[{ + SubscriptBox["L", "1"], ",", "18"}], "]"}], ",", " ", + "\"\\""}], "]"}], ";"}]}], "Code", + CellChangeTimes->{{3.911411526467024*^9, 3.911411558889496*^9}, { + 3.911411603270329*^9, 3.911411726104031*^9}, {3.911411854447959*^9, + 3.911411871785728*^9}, {3.911412038711419*^9, 3.9114121340202703`*^9}, { + 3.911412179397016*^9, 3.91141220168312*^9}, {3.911412234164196*^9, + 3.9114122397709723`*^9}, {3.911412325036952*^9, 3.9114123356623907`*^9}, { + 3.91141236997156*^9, 3.911412427698132*^9}, {3.911412458150606*^9, + 3.911412466280922*^9}, {3.9114126475927057`*^9, 3.9114126927861547`*^9}}, + CellLabel-> + "In[477]:=",ExpressionUUID->"88153533-d3ba-42e3-8671-5c87d6eba466"], + +Cell[CellGroupData[{ + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The original liquidity was: \\!\\(\\*SubscriptBox[\\(L\\), \\(0\ +\\)]\\) = \"\>", + "EchoLabel"], " ", + "1.07205816303780375296378578465914117862`18."}]], "Echo", + CellChangeTimes->{ + 3.911412693179192*^9},ExpressionUUID->"9c86a598-6ad7-4cdc-b7d3-\ +f17754fbb4f8"], + +Cell[BoxData[ + RowBox[{ + TagBox["\<\"The new liquidity after parameter changes is: \ +\\!\\(\\*SubscriptBox[\\(L\\), \\(1\\)]\\) = \"\>", + "EchoLabel"], " ", "1.0633573081332175`"}]], "Echo", + CellChangeTimes->{ + 3.9114126932060432`*^9},ExpressionUUID->"edb1d49d-7246-436e-85d1-\ +f3028622cb8d"] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]] +}, +WindowSize->{2125, 2083}, +WindowMargins->{{Automatic, 1157}, {Automatic, -819}}, +PrintingCopies->1, +PrintingPageRange->{1, Automatic}, +FrontEndVersion->"13.2 for Mac OS X ARM (64-bit) (January 31, 2023)", +StyleDefinitions->FrontEnd`FileName[{$RootDirectory, "Users", "colin", + "Documents"}, "DarkMode.nb", CharacterEncoding -> "UTF-8"], +ExpressionUUID->"13daad44-0d0d-4117-a120-aa865447a233" +] +(* End of Notebook Content *) + +(* Internal cache information *) +(*CellTagsOutline +CellTagsIndex->{} +*) +(*CellTagsIndex +CellTagsIndex->{} +*) +(*NotebookFileOutline +Notebook[{ +Cell[CellGroupData[{ +Cell[422, 15, 185, 3, 194, "Title",ExpressionUUID->"2003d08a-fff7-4f74-8623-7a0823c9cafa"], +Cell[CellGroupData[{ +Cell[632, 22, 221, 5, 134, "Section",ExpressionUUID->"514be430-48c5-4dc6-92af-6b1c3a5b8586"], +Cell[CellGroupData[{ +Cell[878, 31, 222, 5, 107, "Subsection",ExpressionUUID->"f16f1652-ed41-4414-8c9b-6b6d5d8061ac"], +Cell[1103, 38, 579, 14, 69, "Code",ExpressionUUID->"8255c47c-fa0b-4fdd-8638-752453aca613"] +}, Open ]], +Cell[CellGroupData[{ +Cell[1719, 57, 202, 3, 107, "Subsection",ExpressionUUID->"b3dd161e-0b53-4183-b30c-be7844f26477"], +Cell[1924, 62, 809, 19, 111, "Code",ExpressionUUID->"25d6c1c4-f902-41e0-8746-c63f6b03d94e"] +}, Open ]], +Cell[CellGroupData[{ +Cell[2770, 86, 307, 7, 107, "Subsection",ExpressionUUID->"f601d02f-f91e-4780-a166-a78097a54f48"], +Cell[3080, 95, 978, 27, 205, "Code",ExpressionUUID->"d5d16a82-3e60-44ff-affc-b50eb9144304"] +}, Open ]], +Cell[CellGroupData[{ +Cell[4095, 127, 296, 7, 107, "Subsection",ExpressionUUID->"009a24ad-ebe5-4d73-bdda-a7839592332a"], +Cell[CellGroupData[{ +Cell[4416, 138, 244, 6, 89, "Subsubsection",ExpressionUUID->"3b15f5e3-f420-4095-899a-506c7286cc40"], +Cell[4663, 146, 2208, 59, 244, "Code",ExpressionUUID->"3950a1e9-9c32-45c4-b5ef-404c37d6ef65"] +}, Open ]], +Cell[CellGroupData[{ +Cell[6908, 210, 253, 6, 89, "Subsubsection",ExpressionUUID->"6228385e-cfd2-4bd0-97f4-58c98a5a994e"], +Cell[7164, 218, 1228, 36, 159, "Code",ExpressionUUID->"cdbca2c9-2426-4adf-8516-6c22e3b352b1"] +}, Open ]], +Cell[CellGroupData[{ +Cell[8429, 259, 186, 3, 89, "Subsubsection",ExpressionUUID->"18015876-38da-4fa9-82b7-a417d745ef90"], +Cell[8618, 264, 649, 16, 90, "Code",ExpressionUUID->"a65cb5ec-cdac-40e0-bb49-f3d8157592d9"] +}, Open ]] +}, Open ]] +}, Open ]], +Cell[CellGroupData[{ +Cell[9328, 287, 285, 7, 134, "Section",ExpressionUUID->"da815218-0c74-4720-a5c9-76f45781c5e2"], +Cell[CellGroupData[{ +Cell[9638, 298, 305, 7, 107, "Subsection",ExpressionUUID->"d461a415-44ca-4804-8248-6137a0f9449f"], +Cell[CellGroupData[{ +Cell[9968, 309, 2677, 52, 134, "Code",ExpressionUUID->"8d262a91-37a1-41fb-b1b3-ae8191b1ccda"], +Cell[CellGroupData[{ +Cell[12670, 365, 232, 6, 50, "Echo",ExpressionUUID->"63fa2685-ead2-42c3-8894-aa3d0f759278"], +Cell[12905, 373, 258, 7, 68, "Echo",ExpressionUUID->"ab767d72-08f9-45a3-8d7a-62a93aac3a75"], +Cell[13166, 382, 242, 6, 50, "Echo",ExpressionUUID->"865ae55a-ba52-403c-9134-dd94b3a70090"] +}, Open ]] +}, Open ]] +}, Open ]], +Cell[CellGroupData[{ +Cell[13469, 395, 307, 7, 107, "Subsection",ExpressionUUID->"a6874bc0-590c-4cb3-9d20-f010a014a154"], +Cell[CellGroupData[{ +Cell[13801, 406, 2471, 46, 111, "Code",ExpressionUUID->"085ca656-cc94-43cf-a8c6-c116e23e4910"], +Cell[CellGroupData[{ +Cell[16297, 456, 275, 7, 50, "Echo",ExpressionUUID->"d46f0b65-6c97-4fd7-a377-fd814b863b4a"], +Cell[16575, 465, 234, 6, 50, "Echo",ExpressionUUID->"d830d4bf-eeeb-4bb6-a004-32188869cd46"] +}, Open ]] +}, Open ]], +Cell[CellGroupData[{ +Cell[16858, 477, 251, 6, 89, "Subsubsection",ExpressionUUID->"d0f44536-b00f-4974-8c75-dd65dd319645"], +Cell[CellGroupData[{ +Cell[17134, 487, 1744, 46, 111, "Code",ExpressionUUID->"57112165-db6f-447c-9713-f9c85c6d4828"], +Cell[CellGroupData[{ +Cell[18903, 537, 279, 7, 50, "Echo",ExpressionUUID->"d45d2202-bcd3-4d72-9040-061ec9d3b3fd"], +Cell[19185, 546, 287, 7, 50, "Echo",ExpressionUUID->"7f4698d3-1c35-46e4-b14b-549da3ca9c05"] +}, Open ]] +}, Open ]] +}, Open ]], +Cell[CellGroupData[{ +Cell[19533, 560, 224, 4, 89, "Subsubsection",ExpressionUUID->"0dabba71-0908-48dd-b11c-9fc06ac2d79b"], +Cell[CellGroupData[{ +Cell[19782, 568, 1307, 35, 111, "Code",ExpressionUUID->"05df5500-1e66-49a9-afd7-256365f8b6dc"], +Cell[21092, 605, 217, 6, 50, "Echo",ExpressionUUID->"2780423d-6264-4b5d-a5a5-68bad0b82ef8"] +}, Open ]] +}, Open ]], +Cell[CellGroupData[{ +Cell[21358, 617, 284, 6, 89, "Subsubsection",ExpressionUUID->"6c78f375-83c5-40c5-a26f-9dfafb83427b"], +Cell[21645, 625, 1409, 42, 128, "Code",ExpressionUUID->"05cdeb1a-e1b0-40a7-af51-51ab9b75cc2e"] +}, Open ]] +}, Open ]] +}, Open ]], +Cell[CellGroupData[{ +Cell[23115, 674, 155, 3, 134, "Section",ExpressionUUID->"e0558e89-2c12-471a-af64-7b6a1457eb4d"], +Cell[CellGroupData[{ +Cell[23295, 681, 290, 7, 107, "Subsection",ExpressionUUID->"2f7348b3-443c-4a22-943e-943b7962999e"], +Cell[23588, 690, 8190, 168, 362, "Code",ExpressionUUID->"0716117c-5381-46be-8b57-f2cefa727879"], +Cell[CellGroupData[{ +Cell[31803, 862, 4800, 115, 486, "Code",ExpressionUUID->"2687c350-1150-4951-bcf0-28364fa5bf73"], +Cell[CellGroupData[{ +Cell[36628, 981, 230, 7, 50, "Echo",ExpressionUUID->"27baf199-5392-4622-872b-c04b2522ee2d"], +Cell[36861, 990, 235, 6, 50, "Echo",ExpressionUUID->"eecd11fc-1f1d-4de9-9e4a-07d53c8e0fbb"], +Cell[37099, 998, 225, 6, 50, "Echo",ExpressionUUID->"292d880d-854e-49ac-80b7-b67aa90a72e5"], +Cell[37327, 1006, 240, 7, 50, "Echo",ExpressionUUID->"38d83234-0ac4-4733-b57c-347036548a09"] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]], +Cell[CellGroupData[{ +Cell[37640, 1021, 204, 4, 134, "Section",ExpressionUUID->"65d2b88d-22b2-481a-935c-71bdce9f21f1"], +Cell[37847, 1027, 568, 13, 107, "Text",ExpressionUUID->"f20cba79-bbb2-4d1b-9349-a349e8b0b7a4"], +Cell[CellGroupData[{ +Cell[38440, 1044, 630, 15, 89, "Subsubsection",ExpressionUUID->"d7131855-64d1-451f-98f3-152b4aa9f3a5"], +Cell[39073, 1061, 685, 20, 137, "Code",ExpressionUUID->"fadd5b7c-a968-401d-9e42-d6ca30b3a2a6"] +}, Open ]], +Cell[CellGroupData[{ +Cell[39795, 1086, 268, 4, 107, "Subsection",ExpressionUUID->"eed8b815-af5d-458f-8082-a956f0fb88b9"], +Cell[CellGroupData[{ +Cell[40088, 1094, 460, 12, 89, "Subsubsection",ExpressionUUID->"b20dcf41-6dfe-404c-8167-50b070efca5f"], +Cell[CellGroupData[{ +Cell[40573, 1110, 11768, 323, 907, "Code",ExpressionUUID->"6bec84e7-f813-4bb5-9a1e-8a3d86e897da"], +Cell[52344, 1435, 3570, 78, 471, "Output",ExpressionUUID->"ff2c3a54-a3b1-4be6-afbb-32094a16572f"], +Cell[CellGroupData[{ +Cell[55939, 1517, 304, 7, 50, "Echo",ExpressionUUID->"6c3d9d24-049c-4137-b7e3-6cf83d94b59c"], +Cell[56246, 1526, 302, 8, 50, "Echo",ExpressionUUID->"255c6473-c036-4f07-8655-af783da13368"], +Cell[56551, 1536, 399, 10, 52, "Echo",ExpressionUUID->"a6e336c8-67e3-4548-aec6-53634d7b4e40"], +Cell[56953, 1548, 239, 6, 50, "Echo",ExpressionUUID->"5f8a3663-d1c0-4227-93e2-6e4110936aef"], +Cell[57195, 1556, 2090, 58, 241, "Echo",ExpressionUUID->"8b3d5769-45e4-45af-b1b7-284acb1d88ab"] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]], +Cell[CellGroupData[{ +Cell[59358, 1622, 171, 3, 107, "Subsection",ExpressionUUID->"d44a8cc6-b4f0-4204-b250-e68ad4e19d99"], +Cell[CellGroupData[{ +Cell[59554, 1629, 382, 10, 89, "Subsubsection",ExpressionUUID->"800e047e-b69c-4509-bda4-30799fa9a63e"], +Cell[CellGroupData[{ +Cell[59961, 1643, 7526, 215, 540, "Code",ExpressionUUID->"7ca95c98-841f-4a4d-8646-7ee5087c0cbe"], +Cell[67490, 1860, 3553, 78, 450, "Output",ExpressionUUID->"1f3d487e-7d60-4563-a2b3-f3aa65c1d146"], +Cell[CellGroupData[{ +Cell[71068, 1942, 301, 7, 50, "Echo",ExpressionUUID->"20a8471e-d48d-4dc1-8a0f-2b45a51698ee"], +Cell[71372, 1951, 301, 8, 50, "Echo",ExpressionUUID->"5cd69ac1-3ee9-4f3c-b5cf-95d07eb0ff1c"], +Cell[71676, 1961, 391, 10, 50, "Echo",ExpressionUUID->"0834851c-ee77-4202-aa72-ad7a0f04c869"], +Cell[72070, 1973, 238, 6, 50, "Echo",ExpressionUUID->"e88a31f0-9ce3-43a8-a4fe-062f673969b4"], +Cell[72311, 1981, 2460, 70, 173, "Echo",ExpressionUUID->"24b12cbb-7a22-45fd-bb9e-d5824114ff9b"] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]], +Cell[CellGroupData[{ +Cell[74856, 2060, 164, 3, 134, "Section",ExpressionUUID->"b9760121-93f2-4105-bc65-e6b3dbe04aec"], +Cell[CellGroupData[{ +Cell[75045, 2067, 227, 5, 108, "Subsection",ExpressionUUID->"bf3ecf5a-6ea2-4f9a-a236-2d3956e9c134"], +Cell[CellGroupData[{ +Cell[75297, 2076, 2145, 57, 224, "Code",ExpressionUUID->"88153533-d3ba-42e3-8671-5c87d6eba466"], +Cell[CellGroupData[{ +Cell[77467, 2137, 306, 8, 50, "Echo",ExpressionUUID->"9c86a598-6ad7-4cdc-b7d3-f17754fbb4f8"], +Cell[77776, 2147, 298, 7, 50, "Echo",ExpressionUUID->"edb1d49d-7246-436e-85d1-f3028622cb8d"] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]] +}, Open ]] +} +] +*) +