Skip to content

Commit

Permalink
fix SY allo/deallo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kinrezC committed Apr 26, 2024
1 parent bbeda19 commit 7012a90
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ number_underscore = "thousands"
[rpc_endpoints]
local = "http://localhost:8545"
optimism_sepolia = "${OPTIMISM_SEPOLIA_RPC_URL}"
mainnet = "${MAINNET_RPC_URL}"

[etherscan]
optimism_sepolia = { key = "${ETHERSCAN_API_KEY}" }
13 changes: 7 additions & 6 deletions src/DFMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
downscaleUp
} from "./lib/ScalingLib.sol";
import { LPToken } from "./LPToken.sol";
import "forge-std/console2.sol";

/**
* @title DFMM
Expand Down Expand Up @@ -218,7 +219,6 @@ contract DFMM is IDFMM {
bytes postSwapHookData;
}

/// @inheritdoc IDFMM
function swap(
uint256 poolId,
address recipient,
Expand All @@ -240,6 +240,8 @@ contract DFMM is IDFMM {
msg.sender, poolId, _pools[poolId], data
);

uint256 poolId = poolId;

if (!state.valid) revert InvalidInvariant(state.invariant);

if (_pools[poolId].controllerFee > 0) {
Expand All @@ -258,11 +260,6 @@ contract DFMM is IDFMM {
state.tokenIn = _pools[poolId].tokens[state.tokenInIndex];
state.tokenOut = _pools[poolId].tokens[state.tokenOutIndex];

address[] memory tokens = new address[](1);
tokens[0] = state.tokenIn;
uint256[] memory amounts = new uint256[](1);
amounts[0] = state.amountIn;

// Optimistically transfer the output tokens to the recipient.
_transfer(state.tokenOut, recipient, state.amountOut);

Expand All @@ -272,6 +269,10 @@ contract DFMM is IDFMM {

// If the callbackData is empty, do a regular `_transferFrom()` call, as in the other operations.
if (callbackData.length == 0) {
address[] memory tokens = new address[](1);
tokens[0] = state.tokenIn;
uint256[] memory amounts = new uint256[](1);
amounts[0] = state.amountIn;
_transferFrom(tokens, amounts);
} else {
// Otherwise, execute the callback and assert the input amount has been paid
Expand Down
17 changes: 12 additions & 5 deletions src/SYCoveredCall/SYCoveredCall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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";
import "forge-std/console2.sol";

enum UpdateCode {
Invalid,
Expand Down Expand Up @@ -125,11 +126,15 @@ contract SYCoveredCall is PairStrategy {

int256 tau = int256(computeTau(params));

if (PT.SY() != address(SY)) {
console2.log("got here1");
console2.log("pt.sy", params.PT.SY());
console2.log("sy", address(params.SY));
if (params.PT.SY() != address(params.SY)) {
revert InvalidPair();
}
console2.log("got here2");

if (PT.expiry() <= block.timestamp) {
if (params.PT.expiry() <= block.timestamp) {
revert InvalidMaturity();
}

Expand All @@ -141,9 +146,9 @@ contract SYCoveredCall is PairStrategy {
revert InvalidReservesLength();
}

internalParams[poolId].SY = SY;
internalParams[poolId].PT = PT;
internalParams[poolId].YT = IPYieldToken(PT.YT());
internalParams[poolId].SY = params.SY;
internalParams[poolId].PT = params.PT;
internalParams[poolId].YT = IPYieldToken(params.PT.YT());

internalParams[poolId].maturity = internalParams[poolId].PT.expiry();
internalParams[poolId].mean = params.mean;
Expand Down Expand Up @@ -217,6 +222,7 @@ contract SYCoveredCall is PairStrategy {
params = getPoolParams(poolId);
SYCoveredCallParams memory ccParams =
abi.decode(params, (SYCoveredCallParams));
console2.log("got here");

uint256 computedL;
uint256 swapTimestamp;
Expand All @@ -230,6 +236,7 @@ contract SYCoveredCall is PairStrategy {
) = abi.decode(
data, (uint256, uint256, uint256, uint256, uint256, uint256)
);
console2.log("got here2");

if (
swapTimestamp < internalParams[poolId].lastTimestamp
Expand Down
22 changes: 12 additions & 10 deletions src/SYCoveredCall/SYCoveredCallSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ contract SYCoveredCallSolver {
uint256 fees;
}

/// @dev Estimates a swap's reserves and adjustments and returns its validity.
function simulateSwap(
uint256 poolId,
bool swapXIn,
Expand All @@ -238,17 +237,21 @@ contract SYCoveredCallSolver {
poolParams.mean = computeKGivenLastPrice(
preReserves[0], preTotalLiquidity, poolParams
);

uint256 poolId = poolId;
uint256 swapAmountIn = amountIn;
bool swapXToY = swapXIn;
{
if (swapXIn) {
if (swapXToY) {
state.deltaLiquidity = computeDeltaLXIn(
amountIn,
swapAmountIn,
preReserves[0],
preReserves[1],
preTotalLiquidity,
poolParams
);

endReserves.rx = preReserves[0] + amountIn;
endReserves.rx = preReserves[0] + swapAmountIn;
endReserves.L = startComputedL + state.deltaLiquidity;
uint256 approxPrice =
getPriceGivenXL(poolId, endReserves.rx, endReserves.L);
Expand All @@ -264,14 +267,14 @@ contract SYCoveredCallSolver {
state.amountOut = preReserves[1] - endReserves.ry;
} else {
state.deltaLiquidity = computeDeltaLYIn(
amountIn,
swapAmountIn,
preReserves[0],
preReserves[1],
preTotalLiquidity,
poolParams
);

endReserves.ry = preReserves[1] + amountIn;
endReserves.ry = preReserves[1] + swapAmountIn;
endReserves.L = startComputedL + state.deltaLiquidity;
uint256 approxPrice =
getPriceGivenYL(poolId, endReserves.ry, endReserves.L);
Expand All @@ -294,15 +297,14 @@ contract SYCoveredCallSolver {

bytes memory swapData;

if (swapXIn) {
if (swapXToY) {
swapData =
abi.encode(0, 1, amountIn, state.amountOut, startComputedL);
abi.encode(0, 1, swapAmountIn, state.amountOut, startComputedL);
} else {
swapData =
abi.encode(1, 0, amountIn, state.amountOut, startComputedL);
abi.encode(1, 0, swapAmountIn, state.amountOut, startComputedL);
}

uint256 poolId = poolId;
(bool valid,,,,,,,) = IStrategy(strategy).validateSwap(
address(this), poolId, pool, swapData
);
Expand Down
5 changes: 1 addition & 4 deletions test/SYCoveredCall/unit/SetUp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ contract SYCoveredCallSetUp is SetUp {

function mintPtYt(uint256 amount) public {
SY.transfer(address(YT), amount);
uint256 pyOut = YT.mintPY(address(this), address(this));
uint256 ptBal = PT.balanceOf(address(this));
uint256 ytBal = YT.balanceOf(address(this));
uint256 syBal = SY.balanceOf(address(this));
YT.mintPY(address(this), address(this));
}

function setUp() public override {
Expand Down

0 comments on commit 7012a90

Please sign in to comment.