Skip to content

Commit

Permalink
prepareInit
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexangelj committed May 6, 2024
1 parent add319e commit 9a995d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/RMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ contract RMM is ERC20 {

receive() external payable {}

function prepareInit(uint256 priceX, uint256 amountX, uint256 strike_, uint256 sigma_, uint256 maturity_)
public
view
returns (uint256 totalLiquidity_)
{
uint256 tau_ = computeTauWadYears(maturity_ - block.timestamp);
uint256 initialLiquidity =
computeLGivenX({reserveX_: amountX, S: priceX, strike_: strike_, sigma_: sigma_, tau_: tau_});

console2.log("initialLiquidity", initialLiquidity);
uint256 amountY =
solveY({reserveX_: amountX, liquidity: initialLiquidity, strike_: strike_, sigma_: sigma_, tau_: tau_});
totalLiquidity_ = solveL(initialLiquidity, reserveX, amountY, strike_, sigma_, tau_, tau_);
}

/// todo: need a way to compute initial liquidity based on price user input!
/// @dev Initializes the pool with an implied price via the desired reserves, liquidity, and parameters.
function init(
Expand Down Expand Up @@ -324,7 +339,7 @@ contract RMM is ERC20 {
view
returns (uint256 deltaXWad, uint256 deltaYWad, uint256 lptBurned)
{
uint liquidity = totalLiquidity;
uint256 liquidity = totalLiquidity;
deltaXWad = deltaLiquidity.mulDivDown(reserveX, liquidity);
deltaYWad = deltaLiquidity.mulDivDown(reserveY, liquidity);
lptBurned = deltaLiquidity.mulDivUp(totalSupply, liquidity);
Expand All @@ -338,7 +353,7 @@ contract RMM is ERC20 {
lock
returns (uint256 deltaX, uint256 deltaY)
{
(uint256 deltaXWad, uint256 deltaYWad, uint lptBurned) = prepareDeallocate(deltaLiquidity);
(uint256 deltaXWad, uint256 deltaYWad, uint256 lptBurned) = prepareDeallocate(deltaLiquidity);
(deltaX, deltaY) = (downscaleDown(deltaXWad, scalar(tokenX)), downscaleDown(deltaYWad, scalar(tokenY)));

if (minDeltaXOut > deltaX) {
Expand Down
12 changes: 10 additions & 2 deletions test/RMM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,20 @@ contract RMMTest is Test {
tokenX.approve(address(subject()), basicParams.reserveX);
tokenY.approve(address(subject()), basicParams.reserveY);

uint256 totalLiquidity = subject().prepareInit({
priceX: 1 ether,
amountX: basicParams.reserveX,
strike_: basicParams.strike,
sigma_: basicParams.sigma,
maturity_: basicParams.maturity
});

subject().init(
address(tokenX),
address(tokenY),
basicParams.reserveX,
basicParams.reserveY,
basicParams.totalLiquidity,
totalLiquidity,
basicParams.strike,
basicParams.sigma,
basicParams.fee,
Expand All @@ -499,7 +507,7 @@ contract RMMTest is Test {
assertEq(subject().tokenY(), address(tokenY), "Token Y address is not correct.");
assertEq(subject().reserveX(), basicParams.reserveX, "Reserve X is not correct.");
assertEq(subject().reserveY(), basicParams.reserveY, "Reserve Y is not correct.");
assertEq(subject().totalLiquidity(), basicParams.totalLiquidity, "Total liquidity is not correct.");
assertEq(subject().totalLiquidity(), totalLiquidity, "Total liquidity is not correct.");
assertEq(subject().strike(), basicParams.strike, "Strike is not correct.");
assertEq(subject().sigma(), basicParams.sigma, "Sigma is not correct.");
assertEq(subject().fee(), basicParams.fee, "Fee is not correct.");
Expand Down

0 comments on commit 9a995d9

Please sign in to comment.