Skip to content

Commit

Permalink
fix: swap example with new price change
Browse files Browse the repository at this point in the history
  • Loading branch information
TokenTitan committed Nov 12, 2024
1 parent b143185 commit d0c1ffe
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
6 changes: 3 additions & 3 deletions test/FlashLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ contract FlashLiquidityTest is Test, FlashLiquidityLib {
uint256 laminatorSequenceNumber;

vm.startPrank(pusher, pusher);
laminatorSequenceNumber = userLand(100000000000000000000, 10, 2);
laminatorSequenceNumber = userLand(10000 * 1e18, 10000, 2);
vm.stopPrank();

// go forward in time
vm.roll(block.number + 1);

vm.startPrank(filler, filler);
solverLand(1000, 100, laminatorSequenceNumber, 2, filler);
solverLand(1000000000, 10000, laminatorSequenceNumber, 2, filler);
vm.stopPrank();

assertFalse(callbreaker.isPortalOpen());
Expand All @@ -56,7 +56,7 @@ contract FlashLiquidityTest is Test, FlashLiquidityLib {
uint256 laminatorSequenceNumber;

vm.startPrank(pusher, pusher);
laminatorSequenceNumber = userLand(100000000000000000000, 80, 1);
laminatorSequenceNumber = userLand(10000 * 1e18, 80000, 2);
vm.stopPrank();

// go forward in time
Expand Down
4 changes: 2 additions & 2 deletions test/FlashLoan.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ contract FlashLoanTest is Test, FlashLoanLib {
uint256 laminatorSequenceNumber;

vm.startPrank(pusher, pusher);
laminatorSequenceNumber = userLand(100000000000000000000, 10, 2);
laminatorSequenceNumber = userLand(10000 * 1e18, 10000, 2);
vm.stopPrank();

// go forward in time
vm.roll(block.number + 1);

vm.startPrank(filler, filler);
solverLand(1000, 100, laminatorSequenceNumber, 2, filler);
solverLand(1000000000, 10000, laminatorSequenceNumber, 2, filler);
vm.stopPrank();

assertFalse(callbreaker.isPortalOpen());
Expand Down
11 changes: 6 additions & 5 deletions test/examples/DeFi/MockDaiWethPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ contract MockDaiWethPool is SmarterContract {

uint256 private _balanceOfWeth;
uint256 private _balanceOfDai;
uint256 expectedPrice;

address public owner;
IMintableERC20 public weth;
Expand All @@ -25,10 +26,11 @@ contract MockDaiWethPool is SmarterContract {
}

function mintInitialLiquidity() external returns (uint256, uint256) {
dai.mint(address(this), 100 * DECIMAL);
dai.mint(address(this), 100000 * DECIMAL);
weth.mint(address(this), 10 * DECIMAL);
_balanceOfDai = 100000 * DECIMAL;
_balanceOfWeth = 10 * DECIMAL;
_balanceOfDai = 100 * DECIMAL;
expectedPrice = _balanceOfDai * DECIMAL / _balanceOfWeth;
return (_balanceOfDai, _balanceOfWeth);
}

Expand Down Expand Up @@ -61,6 +63,7 @@ contract MockDaiWethPool is SmarterContract {

_balanceOfDai += amount0Desired;
_balanceOfWeth += amount1Desired;
expectedPrice = _balanceOfDai * DECIMAL / _balanceOfWeth;
}

function withdrawLiquidityFromDAIETHPool(address provider, uint256 _amount0Out, uint256 _amount1Out) external {
Expand Down Expand Up @@ -100,6 +103,7 @@ contract MockDaiWethPool is SmarterContract {

_balanceOfDai = dai.balanceOf(address(this));
_balanceOfWeth = weth.balanceOf(address(this));
expectedPrice = _balanceOfDai * DECIMAL / _balanceOfWeth;

emit LiquiditySetForPriceTest();
}
Expand All @@ -108,9 +112,6 @@ contract MockDaiWethPool is SmarterContract {
// Calculate the current price (ratio) of DAI to WETH
uint256 currentPrice = (_balanceOfDai * DECIMAL) / _balanceOfWeth;

// Assume initial expected price of 10 due to initial DAI and WETH ratio
uint256 expectedPrice = 10 * DECIMAL;

// Calculate the absolute deviation in percentage
uint256 slippage = (
currentPrice > expectedPrice ? (currentPrice - expectedPrice) : (expectedPrice - currentPrice)
Expand Down
11 changes: 4 additions & 7 deletions test/solve-lib/DeFi/FlashLiquidityLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,21 @@ contract FlashLiquidityLib {
CallBreaker public callbreaker;
uint256 _tipWei = 33;

uint256 public balanceOfWeth;
uint256 public balanceOfDai;

function deployerLand(address pusher) public {
// Initializing contracts
callbreaker = new CallBreaker();
laminator = new Laminator(address(callbreaker));
dai = new MockERC20Token("Dai", "DAI");
weth = new MockERC20Token("Weth", "WETH");
daiWethPool = new MockDaiWethPool(address(callbreaker), address(dai), address(weth));
(balanceOfDai, balanceOfWeth) = daiWethPool.mintInitialLiquidity();
daiWethPool.mintInitialLiquidity();

pusherLaminated = payable(laminator.computeProxyAddress(pusher));
dai.mint(pusherLaminated, 100000000000000000000);
dai.mint(pusherLaminated, 10000 * 1e18);

liquidityProvider = new MockLiquidityProvider(dai, weth);
dai.mint(address(liquidityProvider), 1009000000000000000000);
weth.mint(address(liquidityProvider), 1000000000000000000000);
dai.mint(address(liquidityProvider), 1000000000 * 1e18);
weth.mint(address(liquidityProvider), 10000 * 1e18);
}

function userLand(uint256 tokenToApprove, uint256 amountIn, uint256 slippagePercent) public returns (uint256) {
Expand Down
8 changes: 4 additions & 4 deletions test/solve-lib/DeFi/FlashLoanLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ contract FlashLoanLib {
daiWethPool = new MockDaiWethPool(address(callbreaker), address(dai), address(weth));
daiWethPool.mintInitialLiquidity();
pusherLaminated = payable(laminator.computeProxyAddress(pusher));
dai.mint(pusherLaminated, 100000000000000000000);
dai.mint(pusherLaminated, 10000 * 1e18);

flashLoan = new MockFlashLoan(address(dai), address(weth));
dai.mint(address(flashLoan), 1009000000000000000000);
weth.mint(address(flashLoan), 1000000000000000000000);
dai.mint(address(flashLoan), 1000000000 * 1e18);
weth.mint(address(flashLoan), 10000 * 1e18);
}

function userLand(uint256 tokenToApprove, uint256 amountIn, uint256 slippagePercent) public returns (uint256) {
Expand Down Expand Up @@ -157,7 +157,7 @@ contract FlashLoanLib {

function generateFlashLoanData(address _flashLoan) public pure returns (FlashLoanData memory) {
FlashLoanData memory flashLoanData =
FlashLoanData({provider: _flashLoan, amountA: 1000 * 1e18, amountB: 100 * 1e18});
FlashLoanData({provider: _flashLoan, amountA: 1000000000 * 1e18, amountB: 10000 * 1e18});
return flashLoanData;
}
}
8 changes: 4 additions & 4 deletions test/solve-lib/DeFi/SlippageProtectionLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ contract SlippageProtectionLib {
daiWethPool = new MockDaiWethPool(address(callbreaker), address(dai), address(weth));
daiWethPool.mintInitialLiquidity();
pusherLaminated = payable(laminator.computeProxyAddress(pusher));
dai.mint(pusherLaminated, 100e18);
weth.mint(address(callbreaker), 100e18);
dai.mint(pusherLaminated, 1000000000 * 1e18);
weth.mint(address(callbreaker), 10000 * 1e18);
}

function userLand(uint256 maxSlippage) public returns (uint256) {
Expand All @@ -43,13 +43,13 @@ contract SlippageProtectionLib {
amount: 0,
addr: address(dai),
gas: 1000000,
callvalue: abi.encodeWithSignature("approve(address,uint256)", daiWethPool, 100e18)
callvalue: abi.encodeWithSignature("approve(address,uint256)", daiWethPool, 80000e18)
});
pusherCallObjs[1] = CallObject({
amount: 0,
addr: address(daiWethPool),
gas: 1000000,
callvalue: abi.encodeWithSignature("swapDAIForWETH(uint256,uint256)", 100, maxSlippage)
callvalue: abi.encodeWithSignature("swapDAIForWETH(uint256,uint256)", 80000, maxSlippage)
});
pusherCallObjs[2] = CallObject({amount: _tipWei, addr: address(callbreaker), gas: 10000000, callvalue: ""});
SolverData[] memory dataValues = Constants.emptyDataValues();
Expand Down

0 comments on commit d0c1ffe

Please sign in to comment.