Skip to content

Commit

Permalink
initial swap fuzz test
Browse files Browse the repository at this point in the history
  • Loading branch information
kyriediculous committed Aug 15, 2023
1 parent 6c35009 commit 0975f49
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ contract TenderSwap is TenderSwapStorage, Multicall, SelfPermit {
fee = f.mul(ud(amount)).unwrap();

// get the output amount
out = amount - fee;
unchecked {
out = amount - fee;
}
}

function _isValidAsset(address asset) internal view returns (bool) {
Expand Down
27 changes: 25 additions & 2 deletions test/Swap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

pragma solidity >=0.8.19;

import { Test, stdError, console } from "forge-std/Test.sol";
import { Test } from "forge-std/Test.sol";
import { MockERC20 } from "test/helpers/MockERC20.sol";
import { Registry } from "@tenderize/stake/registry/Registry.sol";
import { Tenderizer, TenderizerImmutableArgs } from "@tenderize/stake/tenderizer/Tenderizer.sol";

import { TenderSwap, Config, BASE_FEE, _encodeTokenId, _decodeTokenId } from "@tenderize/swap/Swap.sol";
import { LPToken } from "@tenderize/swap/LPToken.sol";

import { UD60x18, ud, ZERO, UNIT } from "@prb/math/UD60x18.sol";
import { UD60x18, ud, unwrap, ZERO, UNIT } from "@prb/math/UD60x18.sol";

contract TenderSwapTest is Test {
MockERC20 underlying;
Expand Down Expand Up @@ -110,4 +110,27 @@ contract TenderSwapTest is Test {
assertEq(out, amount - expFee, "swap out");
assertEq(swap.availableLiquidity(), 90 ether, "TenderSwap available liquidity");
}

function testFuzz_swap(uint256 liquidity, uint256 amount) public {
liquidity = bound(liquidity, 1e18, type(uint64).max);
amount = bound(amount, 1e9, liquidity);

underlying.mint(address(this), liquidity);
underlying.approve(address(swap), liquidity);
swap.deposit(liquidity);

vm.mockCall(address(tToken0), abi.encodeWithSelector(Tenderizer.unlock.selector, amount), abi.encode(0));
vm.mockCall(address(tToken0), abi.encodeWithSelector(Tenderizer.unlockMaturity.selector, 0), abi.encode(block.number + 100));

tToken0.mint(address(this), liquidity);
tToken0.approve(address(swap), amount);
(uint256 out, uint256 fee) = swap.swap(address(tToken0), amount, 0);

uint256 expFee = unwrap(ud(amount).mul((BASE_FEE + ud(amount).div(ud(liquidity)).pow(ud(3e18)))));
expFee = expFee >= amount ? amount : expFee;

assertEq(fee, expFee, "swap fee");
assertEq(out, amount - expFee, "swap out");
assertEq(swap.availableLiquidity(), liquidity - amount, "TenderSwap available liquidity");
}
}

0 comments on commit 0975f49

Please sign in to comment.