Skip to content

Commit

Permalink
sy zap assertions, wip broken pt -> sy zap assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
kinrezC committed Jun 17, 2024
1 parent 2b541ac commit 1dae03d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/LiquidityManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {IStandardizedYield} from "pendle/interfaces/IStandardizedYield.sol";
import {PYIndexLib, PYIndex} from "pendle/core/StandardizedYield/PYIndex.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {SafeTransferLib, ERC20} from "solmate/utils/SafeTransferLib.sol";
import "forge-std/console2.sol";

import {RMM, IPYieldToken} from "./RMM.sol";
import {InvalidTokenIn, InsufficientSYMinted} from "./lib/RmmErrors.sol";
Expand Down Expand Up @@ -131,12 +132,12 @@ contract LiquidityManager {
uint256 epsilon;
}

function computePtToSyToAddLiquidity(ComputeArgs memory args) public view returns (uint256, uint256) {
function computePtToSyToAddLiquidity(ComputeArgs memory args) public view returns (uint256 guess, uint256 syOut) {
uint256 min = 0;
uint256 max = args.maxIn - 1;
for (uint256 iter = 0; iter < 256; ++iter) {
uint256 guess = args.initialGuess > 0 && iter == 0 ? args.initialGuess : (min + max) / 2;
(,, uint256 syOut,,) = RMM(payable(args.rmm)).prepareSwapPtIn(guess, args.blockTime, args.index);
guess = args.initialGuess > 0 && iter == 0 ? args.initialGuess : (min + max) / 2;
(,, syOut,,) = RMM(payable(args.rmm)).prepareSwapPtIn(guess, args.blockTime, args.index);

uint256 syNumerator = syOut * (args.rX + syOut);
uint256 ptNumerator = (args.maxIn - guess) * (args.rY - guess);
Expand Down
20 changes: 11 additions & 9 deletions test/unit/LiquidityManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {IPPrincipalToken} from "pendle/interfaces/IPPrincipalToken.sol";
import {IStandardizedYield} from "pendle/interfaces/IStandardizedYield.sol";
import {IPYieldToken} from "pendle/interfaces/IPYieldToken.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";

IPAllActionV3 constant router = IPAllActionV3(0x00000000005BBB0EF59571E58418F9a4357b68A0);
IPMarket constant market = IPMarket(0x9eC4c502D989F04FfA9312C9D6E3F872EC91A0F9);
Expand Down Expand Up @@ -186,6 +187,7 @@ contract ForkRMMTest is Test {
function test_zap_from_sy() public basic_sy {
PYIndex index = YT.newIndex();

// initial balances
uint256 rX = subject().reserveX();
uint256 rY = subject().reserveY();
uint256 maxSyToSwap = 1 ether;
Expand All @@ -202,15 +204,16 @@ contract ForkRMMTest is Test {
epsilon: 10_000
})
);
console2.log("ptOut", ptOut);
uint256 dx = maxSyToSwap - syToSwap;
uint256 dy = ptOut;

(,, uint256 minLiquidityDelta,) = subject().prepareAllocate(dx, dy, index);
uint256 liquidity = liquidityManager().allocateFromSy(
liquidityManager().allocateFromSy(
LiquidityManager.AllocateArgs(address(subject()), maxSyToSwap, ptOut, minLiquidityDelta, syToSwap, eps)
);
console2.log("liquidity", liquidity);

assertEq(subject().reserveX(), rX + maxSyToSwap, "unexpected rX balance after zap");
assertEq(subject().reserveY(), rY, "unexpected rY balance after zap");
}

function test_zap_from_pt() public basic_sy {
Expand All @@ -223,15 +226,14 @@ contract ForkRMMTest is Test {
(uint256 ptToSwap, uint256 syOut) = liquidityManager().computePtToSyToAddLiquidity(
LiquidityManager.ComputeArgs(address(subject()), rX, rY, index, maxPtToSwap, block.timestamp, 0, 10_000)
);
uint256 dx = maxPtToSwap - ptToSwap;
uint256 dy = syOut;
uint256 dx = syOut;
uint256 dy = maxPtToSwap - ptToSwap;

(,, uint256 minLiquidityDelta,) = subject().prepareAllocate(dx, dy, index);
uint256 liquidity = liquidityManager().allocateFromSy(
liquidityManager().allocateFromSy(
LiquidityManager.AllocateArgs(address(subject()), maxPtToSwap, syOut, minLiquidityDelta, ptToSwap, eps)
);
console2.log("liquidity", liquidity);
console2.log("ptToSwap", ptToSwap);
console2.log("syOut", syOut);
assertEq(subject().reserveY(), rY + maxPtToSwap, "unexpected rY balance after zap");
assertEq(subject().reserveX(), rX, "unexpected rX balance after zap");
}
}

0 comments on commit 1dae03d

Please sign in to comment.