-
when I run "forge test --mt statefulFuzz_constantProductFormulaStayTheSame" the handler contract is not fuzzing. i tried everything but i don't know where is the issue !!! Functions in Invariant contract: ``
} `` Functions in handler contract: `
} This is my output after run "forge test --mt statefulFuzz_constantProductFormulaStayTheSame -vvvv'' ` [13192] Invariant::statefulFuzz_constantProductFormulaStayTheSame() Test result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 10.10ms Failing tests: Encountered a total of 1 failing tests, 0 tests succeeded` i have no idea why functions in my handler contract not fuzzing ? (i'm still learning and any help will be so much appreciated) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import { Test } from "forge-std/Test.sol";
import { StdInvariant } from "forge-std/StdInvariant.sol";
import { ERC20Mock } from "../Mock/ERC20Mock.sol";
import { PoolFactory } from "../../../src/PoolFactory.sol";
import { TSwapPool } from "../../../src/TSwapPool.sol";
import { Handler } from "./Handler.t.sol";
contract Invariant is StdInvariant, Test {
// these pool have 2 assets
ERC20Mock poolToken;
ERC20Mock weth;
//We need contracts
PoolFactory factory;
TSwapPool pool; // poolToken /Weth
Handler handler;
int256 constant STARTING_X = 100e18;
int256 constant STARTING_Y = 50e18;
function setUp() public {
poolToken = new ERC20Mock();
weth = new ERC20Mock();
factory = new PoolFactory(address(weth));
pool = TSwapPool(factory.createPool(address(poolToken)));
// create those intila X and Y balances
poolToken.mint(address(this), uint256(STARTING_X));
weth.mint(address(this), uint256(STARTING_Y));
poolToken.approve(address(pool), type(uint256).max);
weth.approve(address(pool), type(uint256).max);
pool.deposit(uint256(STARTING_Y), uint256(STARTING_Y), uint256(STARTING_X), uint64(block.timestamp));
handler = new Handler(pool);
bytes4[] memory selectors = new bytes4[](2);
selectors[0] = handler.deposit.selector;
selectors[1] = handler.swapPoolTokenForWethBasedOnOutputweth.selector;
targetSelector(FuzzSelector({ addr: address(handler), selectors: selectors }));
targetContract(address(handler));
}
function statefulFuzz_constantProductFormulaStayTheSame() public {
assertEq(handler.actualDeltaX(), handler.expectedDeltaX());
}
} |
Beta Was this translation helpful? Give feedback.
-
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import { Test, console2 } from "forge-std/Test.sol";
import { TSwapPool } from "../../../src/TSwapPool.sol";
import { ERC20Mock } from "../Mock/ERC20Mock.sol";
contract Handler is Test {
TSwapPool pool;
ERC20Mock weth;
ERC20Mock poolToken;
//Gost variables
int256 public startingY;
int256 public startingX;
int256 public expectedDeltaY;
int256 public expectedDeltaX;
int256 public actualDeltaX;
int256 public actualDeltaY;
address liquidityProvider = makeAddr("lp");
address swapper = makeAddr("swapper");
constructor(TSwapPool _pool) {
pool = _pool;
weth = ERC20Mock(_pool.getWeth());
poolToken = ERC20Mock(_pool.getPoolToken());
}
function swapPoolTokenForWethBasedOnOutputweth(uint256 outputweth) public {
outputweth = bound(outputweth, pool.getMinimumWethDepositAmount(), type(uint64).max);
if (outputweth >= weth.balanceOf(address(pool))) {
return;
}
// ∆x = (β/(1-β)) * x
uint256 poolTokenAmount = pool.getInputAmountBasedOnOutput(
outputweth, poolToken.balanceOf(address(pool)), weth.balanceOf(address(pool))
);
if (poolTokenAmount > type(uint64).max) {
return;
}
startingY = int256(weth.balanceOf(address(this)));
startingX = int256(poolToken.balanceOf(address(this)));
expectedDeltaY = int256(-1) * int256(outputweth);
expectedDeltaX = int256(pool.getPoolTokensToDepositBasedOnWeth(poolTokenAmount));
if (poolToken.balanceOf(swapper) < poolTokenAmount) {
poolToken.mint(swapper, poolTokenAmount - poolToken.balanceOf(swapper) + 1);
}
//swap
vm.startPrank(swapper);
poolToken.approve(address(pool), type(uint256).max);
pool.swapExactOutput(poolToken, weth, outputweth, uint64(block.timestamp));
vm.stopPrank();
uint256 endingY = weth.balanceOf(address(this));
uint256 endingX = poolToken.balanceOf(address(this));
actualDeltaY = int256(endingY) - int256(startingY);
actualDeltaX = int256(endingX) - int256(startingX);
}
function deposit(uint256 wethAmount) public {
// let's make sure to deposit reasonable amount
// avoid overflow
wethAmount = bound(wethAmount, 0, type(uint64).max);
//18.446744073709551615
startingY = int256(weth.balanceOf(address(this)));
startingX = int256(poolToken.balanceOf(address(this)));
expectedDeltaY = int256(wethAmount);
expectedDeltaX = int256(pool.getPoolTokensToDepositBasedOnWeth(wethAmount));
//deposit
vm.startPrank(liquidityProvider);
weth.mint(liquidityProvider, uint256(wethAmount));
poolToken.mint(liquidityProvider, uint256(expectedDeltaX));
weth.approve(address(pool), type(uint256).max);
poolToken.approve(address(pool), type(uint256).max);
pool.deposit(wethAmount, 0, uint256(expectedDeltaX), uint64(block.timestamp));
vm.stopPrank();
//actual
uint256 endingY = weth.balanceOf(address(this));
uint256 endingX = poolToken.balanceOf(address(this));
actualDeltaY = int256(endingY) - int256(startingY);
actualDeltaX = int256(endingX) - int256(startingX);
}
} |
Beta Was this translation helpful? Give feedback.
-
Hello @Loko0125, Please run |
Beta Was this translation helpful? Give feedback.
Hello @Loko0125, Please run
foundryup
, then retry running the test and show yourfoundry.toml
. The image below shows mine.