testing shift

This commit is contained in:
JulesCrown 2024-04-27 07:04:33 +02:00
parent 047d1967e1
commit 0edf05a32d
2 changed files with 108 additions and 48 deletions

View file

@ -7,6 +7,7 @@ import {TwabController} from "pt-v5-twab-controller/TwabController.sol";
import {PoolAddress, PoolKey} from "@aperture/uni-v3-lib/PoolAddress.sol";
import "@uniswap-v3-core/interfaces/IUniswapV3Factory.sol";
import "@uniswap-v3-core/interfaces/IUniswapV3Pool.sol";
import "@aperture/uni-v3-lib/TickMath.sol";
import "../src/interfaces/IWETH9.sol";
import {WETH} from "solmate/tokens/WETH.sol";
import "../src/Harb.sol";
@ -24,6 +25,8 @@ contract BaseLineLPTest is Test {
IUniswapV3Factory factory;
Stake stake;
BaseLineLP liquidityManager;
IUniswapV3Pool pool;
bool token0isWeth;
function sqrt(uint256 y) internal pure returns (uint256 z) {
if (y > 3) {
@ -39,9 +42,9 @@ contract BaseLineLPTest is Test {
// z is now the integer square root of y, or the closest integer to the square root of y.
}
function initializePoolFor1Cent(bool isEthToken0, address pool) public {
function initializePoolFor1Cent(address pool) public {
uint256 price;
if (isEthToken0) {
if (token0isWeth) {
// ETH as token0, so we are setting the price of 1 ETH in terms of token1 (USD cent)
price = 3000 * 10**20; // 1 ETH = 3700 USD, scaled by 10^18 for precision
} else {
@ -73,9 +76,10 @@ contract BaseLineLPTest is Test {
TwabController tc = new TwabController(60 * 60 * 24, uint32(block.timestamp));
harb = new Harb("HARB", "HARB", factoryAddress, address(weth), tc);
address pool = factory.createPool(address(weth), address(harb), FEE);
pool = IUniswapV3Pool(factory.createPool(address(weth), address(harb), FEE));
initializePoolFor1Cent(address(weth) < address(harb), pool);
token0isWeth = address(weth) < address(harb);
initializePoolFor1Cent(address(pool));
stake = new Stake(address(harb));
harb.setStakingPool(address(stake));
@ -87,14 +91,66 @@ contract BaseLineLPTest is Test {
vm.assume(account != address(0));
vm.assume(account != address(1)); // TWAB sponsorship address
vm.assume(account != address(2)); // tax pool address
vm.deal(account, 10 ether);
vm.deal(account, 15 ether);
vm.prank(account);
(bool sent, ) = address(liquidityManager).call{value: 10 ether}("");
require(sent, "Failed to send Ether");
// Try to shift liquidity manager's state, expect failure due to initial state
vm.expectRevert();
liquidityManager.shift();
liquidityManager.slide();
// Approve the pool to transfer WETH on behalf of the account
vm.prank(account);
weth.deposit{value: 5 ether}();
vm.prank(account);
weth.approve(address(this), 5 ether);
// Execute the swap
pool.swap(
account, // Recipient of the output tokens
token0isWeth,
int256(0.5 ether),
token0isWeth ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1,
abi.encode(account, int256(0.5 ether), true)
);
liquidityManager.shift();
}
/*//////////////////////////////////////////////////////////////
CALLBACKS
//////////////////////////////////////////////////////////////*/
function uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata _data
)
external
{
require(amount0Delta > 0 || amount1Delta > 0);
(address seller, uint256 harbIn, bool buy) = abi.decode(_data, (address, uint256, bool));
(bool isExactInput, uint256 amountToPay) = amount0Delta > 0
? (!token0isWeth, uint256(amount0Delta))
: (token0isWeth, uint256(amount1Delta));
if (buy) {
weth.transferFrom(seller, msg.sender, amountToPay);
return;
}
require(isExactInput, "reason 1");
// check input amount
require(amountToPay == harbIn, "reason 2");
// transfer eth to univ3 pool
require(harb.transferFrom(seller, msg.sender, amountToPay), "reason 3");
}
receive() external payable {}
}