Skip to content

Commit

Permalink
Feat/sy without k pin (#140)
Browse files Browse the repository at this point in the history
* add pendle deps

* add SYCoveredCall

* add postSwapHooks

* fix: covered call solver implements ISolver

* fix: sy covered call implements ISolver
  • Loading branch information
kinrezC authored Apr 26, 2024
1 parent 0edfaa9 commit fbfb58d
Show file tree
Hide file tree
Showing 33 changed files with 4,594 additions and 236 deletions.
15 changes: 14 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,17 @@

[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
url = https://github.com/foundry-rs/forge-std

[submodule "lib/pendle-core-v2-public"]
path = lib/pendle-core-v2-public
url = https://github.com/pendle-finance/pendle-core-v2-public

[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
branch = v4.9.3

[submodule "lib/openzeppelin-contracts-upgradeable"]
path = lib/openzeppelin-contracts-upgradeable
url = https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable
2 changes: 2 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -23,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}" }
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts-upgradeable
1 change: 1 addition & 0 deletions lib/pendle-core-v2-public
Submodule pendle-core-v2-public added at bc27b1
5 changes: 3 additions & 2 deletions src/ConstantSum/ConstantSumSolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ contract ConstantSumSolver is ISolver {
swapData = abi.encode(1, 0, amountIn, amountOut);
}

(bool valid,,,,,,) =
strategy.validateSwap(address(this), poolId, pool, swapData);
(bool valid,,,,,,,) = IStrategy(strategy).validateSwap(
address(this), poolId, pool, swapData
);
return (valid, amountOut, swapData);
}

Expand Down
32 changes: 24 additions & 8 deletions src/CoveredCall/CoveredCall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,6 +33,7 @@ struct InternalParams {
uint256 maturity;
uint256 swapFee;
address controller;
uint256 lastTimestamp;
}

/// @dev Parameterization of the Log Normal curve.
Expand All @@ -42,7 +43,7 @@ struct CoveredCallParams {
uint256 maturity;
uint256 swapFee;
address controller;
uint256 timestamp;
uint256 lastTimestamp;
}

/// @dev Thrown when the mean parameter is not within the allowed bounds.
Expand Down Expand Up @@ -98,7 +99,7 @@ contract CoveredCall is PairStrategy {
(reserves, totalLiquidity, params) =
abi.decode(data, (uint256[], uint256, CoveredCallParams));

params.timestamp = block.timestamp;
params.lastTimestamp = block.timestamp;

if (params.mean < MIN_WIDTH || params.mean > MAX_MEAN) {
revert InvalidMean();
Expand Down Expand Up @@ -158,7 +159,7 @@ contract CoveredCall is PairStrategy {
params.mean = internalParams[poolId].mean;
params.swapFee = internalParams[poolId].swapFee;
params.maturity = internalParams[poolId].maturity;
params.timestamp = IDFMM(dfmm).pools(poolId).lastSwapTimestamp;
params.lastTimestamp = internalParams[poolId].lastTimestamp;

return abi.encode(params);
}
Expand All @@ -180,19 +181,25 @@ contract CoveredCall is PairStrategy {
uint256 tokenOutIndex,
uint256 amountIn,
uint256 amountOut,
uint256 deltaLiquidity
uint256 deltaLiquidity,
bytes memory params
)
{
bytes memory params = getPoolParams(poolId);
params = getPoolParams(poolId);

CoveredCallParams memory ccParams =
abi.decode(params, (CoveredCallParams));
ccParams.lastTimestamp = block.timestamp;

params = abi.encode(ccParams);

uint256 computedL;
(tokenInIndex, tokenOutIndex, amountIn, amountOut, computedL) =
abi.decode(data, (uint256, uint256, uint256, uint256, uint256));

int256 computedInvariant =
tradingFunction(pool.reserves, computedL, params);

console2.log("Computed Invariant: {}", computedInvariant);

if (computedInvariant < 0 || computedInvariant > EPSILON) {
revert InvalidComputedLiquidity(computedInvariant);
}
Expand All @@ -211,6 +218,15 @@ contract CoveredCall is PairStrategy {
//valid = invariant >= 0 && invariant <= EPSILON;
}

function postSwapHook(
address,
uint256 poolId,
Pool calldata pool,
bytes calldata
) external override onlyDFMM {
internalParams[poolId].lastTimestamp = block.timestamp;
}

/// @inheritdoc IStrategy
function tradingFunction(
uint256[] memory reserves,
Expand Down
4 changes: 2 additions & 2 deletions src/CoveredCall/CoveredCallMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ function computeTradingFunction(
}

function computeTau(CoveredCallParams memory params) pure returns (uint256) {
if (params.timestamp >= params.maturity) {
if (params.lastTimestamp >= params.maturity) {
return 0;
} else {
return ONE * (params.maturity - params.timestamp) / YEAR;
return ONE * (params.maturity - params.lastTimestamp) / YEAR;
}
}

Expand Down
Loading

0 comments on commit fbfb58d

Please sign in to comment.