Skip to content

Commit

Permalink
Merge pull request #28 from primitivefinance/test/more
Browse files Browse the repository at this point in the history
Test/more
  • Loading branch information
kinrezC authored Jul 15, 2024
2 parents d89b753 + 52aba25 commit e124db8
Show file tree
Hide file tree
Showing 34 changed files with 945 additions and 1,639 deletions.
2 changes: 1 addition & 1 deletion script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract Deploy is Script {

address sender = vm.addr(pk);
console2.log("Deploying RMM from", sender);
RMM rmm = FACTORY.createRMM("RMM", "RMM");
// RMM rmm = FACTORY.createRMM("RMM", "RMM");

vm.stopBroadcast();
}
Expand Down
12 changes: 2 additions & 10 deletions script/DeployPool.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ contract DeployPool is Script {
uint256 pk = vm.envUint(ENV_PRIVATE_KEY);
vm.startBroadcast(pk);
sender = vm.addr(pk);
rmm = FACTORY.createRMM("Lido Staked ETH 24 Dec 2025", "stETH-24DEC25");
rmm = FACTORY.createRMM("Lido Staked ETH 24 Dec 2025", "stETH-24DEC25", address(PT), 0.02 ether, fee);
console2.log("rmm address: ", address(rmm));

mintSY(10_000 ether);
Expand All @@ -107,15 +107,7 @@ contract DeployPool is Script {
PYIndex index = YT.newIndex();
(MarketState memory ms, MarketPreCompute memory mp) = getPendleMarketData(index);
uint256 price = uint256(getPtExchangeRate(index));
rmm.init({
PT_: address(PT),
priceX: price,
amountX: uint256(ms.totalSy),
strike_: uint256(mp.rateAnchor),
sigma_: 0.02 ether,
fee_: fee,
curator_: address(0x55)
});
rmm.init({priceX: price, amountX: uint256(ms.totalSy), strike_: uint256(mp.rateAnchor)});

vm.stopBroadcast();
}
Expand Down
7 changes: 5 additions & 2 deletions src/Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ contract Factory {
WETH = weth_;
}

function createRMM(string memory poolName, string memory poolSymbol) external returns (RMM) {
RMM rmm = new RMM(WETH, poolName, poolSymbol);
function createRMM(string memory poolName, string memory poolSymbol, address PT, uint256 sigma, uint256 fee)
external
returns (RMM)
{
RMM rmm = new RMM(poolName, poolSymbol, PT, sigma, fee);
emit NewPool(msg.sender, address(rmm), poolName, poolSymbol);
pools.push(address(rmm));
return rmm;
Expand Down
31 changes: 24 additions & 7 deletions src/LiquidityManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 {RMM, IPYieldToken} from "./RMM.sol";
import {RMM, IPYieldToken, Gaussian, computeTradingFunction} from "./RMM.sol";
import {InvalidTokenIn, InsufficientSYMinted} from "./lib/RmmErrors.sol";

contract LiquidityManager {
Expand Down Expand Up @@ -78,10 +78,11 @@ contract LiquidityManager {
// swap syToSwap for pt
rmm.swapExactSyForPt(syToSwap, args.minOut, address(this));
uint256 syBal = sy.balanceOf(address(this));
sy.approve(address(args.rmm), syBal);
uint256 ptBal = pt.balanceOf(address(this));

pt.approve(address(args.rmm), ptBal);
liquidity = rmm.allocate(syBal, ptBal, args.minLiquidityDelta, msg.sender);
liquidity = rmm.allocate(true, syBal, args.minLiquidityDelta, msg.sender);
}

function allocateFromPt(AllocateArgs calldata args) external returns (uint256 liquidity) {
Expand Down Expand Up @@ -112,12 +113,10 @@ contract LiquidityManager {
pt.approve(address(rmm), args.amountIn);

// swap ptToSwap for sy
rmm.swapExactPtForSy(ptToSwap, args.minOut, address(this));
uint256 syBal = sy.balanceOf(address(this));
uint256 ptBal = pt.balanceOf(address(this));
(uint256 syOut,) = rmm.swapExactPtForSy(ptToSwap, args.minOut, address(this));

sy.approve(address(rmm), syBal);
liquidity = rmm.allocate(syBal, ptBal, args.minLiquidityDelta, msg.sender);
sy.approve(address(rmm), type(uint256).max);
liquidity = rmm.allocate(false, pt.balanceOf(address(this)), args.minLiquidityDelta, msg.sender);
}

struct ComputeArgs {
Expand Down Expand Up @@ -179,4 +178,22 @@ contract LiquidityManager {
function isAApproxB(uint256 a, uint256 b, uint256 eps) internal pure returns (bool) {
return b.mulWadDown(1 ether - eps) <= a && a <= b.mulWadDown(1 ether + eps);
}

function calcMaxPtOut(
uint256 reserveX_,
uint256 reserveY_,
uint256 totalLiquidity_,
uint256 strike_,
uint256 sigma_,
uint256 tau_
) internal pure returns (uint256) {
int256 currentTF = computeTradingFunction(reserveX_, reserveY_, totalLiquidity_, strike_, sigma_, tau_);

uint256 maxProportion = uint256(int256(1e18) - currentTF) * 1e18 / (2 * 1e18);

uint256 maxPtOut = reserveY_ * maxProportion / 1e18;

return (maxPtOut * 999) / 1000;
}

}
Loading

0 comments on commit e124db8

Please sign in to comment.