From 24e1edabe2d67e775a0e5283ca47e1bd15b5f0f9 Mon Sep 17 00:00:00 2001 From: teddy Date: Tue, 23 Jul 2024 15:26:08 -0300 Subject: [PATCH] chore: finally remove bpool.t.sol --- test/unit/BPool.t.sol | 246 ------------------------------------------ 1 file changed, 246 deletions(-) delete mode 100644 test/unit/BPool.t.sol diff --git a/test/unit/BPool.t.sol b/test/unit/BPool.t.sol deleted file mode 100644 index 6be576c4..00000000 --- a/test/unit/BPool.t.sol +++ /dev/null @@ -1,246 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.25; - -import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; -import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol'; - -import {BPool} from 'contracts/BPool.sol'; -import {IBPool} from 'interfaces/IBPool.sol'; -import {MockBPool} from 'test/smock/MockBPool.sol'; - -import {BConst} from 'contracts/BConst.sol'; -import {BMath} from 'contracts/BMath.sol'; -import {Test} from 'forge-std/Test.sol'; -import {Pow} from 'test/utils/Pow.sol'; -import {Utils} from 'test/utils/Utils.sol'; - -abstract contract BasePoolTest is Test, BConst, Utils, BMath { - MockBPool public bPool; - - // Deploy this external contract to perform a try-catch when calling bpow. - // If the call fails, it means that the function overflowed, then we reject the fuzzed inputs - Pow public pow = new Pow(); - - function setUp() public virtual { - bPool = new MockBPool(); - - // Create fake tokens - address[] memory _tokensToAdd = _getDeterministicTokenArray(TOKENS_AMOUNT); - for (uint256 i = 0; i < _tokensToAdd.length; i++) { - tokens.push(_tokensToAdd[i]); - } - } - - function _setRandomTokens(uint256 _length) internal returns (address[] memory _tokensToAdd) { - _tokensToAdd = _getDeterministicTokenArray(_length); - for (uint256 i = 0; i < _length; i++) { - bPool.set__records(_tokensToAdd[i], IBPool.Record({bound: true, index: i, denorm: 0})); - } - bPool.set__tokens(_tokensToAdd); - } - - function _mockTransfer(address _token) internal { - // TODO: add amount to transfer to check that it's called with the right amount - vm.mockCall(_token, abi.encodeWithSelector(IERC20.transfer.selector), abi.encode(true)); - } - - function _mockTransferFrom(address _token) internal { - // TODO: add from and amount to transfer to check that it's called with the right params - vm.mockCall(_token, abi.encodeWithSelector(IERC20.transferFrom.selector), abi.encode(true)); - } - - function _mockPoolBalance(address _token, uint256 _balance) internal { - vm.mockCall(_token, abi.encodeWithSelector(IERC20.balanceOf.selector, address(bPool)), abi.encode(_balance)); - } - - function _setSwapFee(uint256 _swapFee) internal { - bPool.set__swapFee(_swapFee); - } - - function _setFinalize(bool _isFinalized) internal { - bPool.set__finalized(_isFinalized); - } - - function _setPoolBalance(address _user, uint256 _balance) internal { - deal(address(bPool), _user, _balance, true); - } - - function _setTotalSupply(uint256 _totalSupply) internal { - _setPoolBalance(address(0), _totalSupply); - } - - function _setTotalWeight(uint256 _totalWeight) internal { - bPool.set__totalWeight(_totalWeight); - } - - function _expectRevertByReentrancy() internal { - // Assert that the contract is accessible - assertEq(bPool.call__getLock(), _MUTEX_FREE); - // Simulate ongoing call to the contract - bPool.call__setLock(_MUTEX_TAKEN); - - vm.expectRevert(IBPool.BPool_Reentrancy.selector); - } - - function _expectSetReentrancyLock() internal { - // Assert that the contract is accessible - assertEq(bPool.call__getLock(), _MUTEX_FREE); - // Expect reentrancy lock to be set - bPool.expectCall__setLock(_MUTEX_TAKEN); - } - - function _assumeCalcSpotPrice( - uint256 _tokenInBalance, - uint256 _tokenInDenorm, - uint256 _tokenOutBalance, - uint256 _tokenOutDenorm, - uint256 _swapFee - ) internal pure { - vm.assume(_tokenInDenorm > 0); - vm.assume(_tokenInBalance < type(uint256).max / BONE); - vm.assume(_tokenInBalance * BONE < type(uint256).max - (_tokenInDenorm / 2)); - - uint256 _numer = bdiv(_tokenInBalance, _tokenInDenorm); - vm.assume(_tokenOutDenorm > 0); - vm.assume(_tokenOutBalance < type(uint256).max / BONE); - vm.assume(_tokenOutBalance * BONE < type(uint256).max - (_tokenOutDenorm / 2)); - - uint256 _denom = bdiv(_tokenOutBalance, _tokenOutDenorm); - vm.assume(_denom > 0); - vm.assume(_numer < type(uint256).max / BONE); - vm.assume(_numer * BONE < type(uint256).max - (_denom / 2)); - vm.assume(_swapFee <= BONE); - - uint256 _ratio = bdiv(_numer, _denom); - vm.assume(bsub(BONE, _swapFee) > 0); - - uint256 _scale = bdiv(BONE, bsub(BONE, _swapFee)); - vm.assume(_ratio < type(uint256).max / _scale); - } - - function _assumeCalcInGivenOut( - uint256 _tokenOutDenorm, - uint256 _tokenInDenorm, - uint256 _tokenOutBalance, - uint256 _tokenAmountOut, - uint256 _tokenInBalance - ) internal pure { - uint256 _weightRatio = bdiv(_tokenOutDenorm, _tokenInDenorm); - uint256 _diff = bsub(_tokenOutBalance, _tokenAmountOut); - uint256 _y = bdiv(_tokenOutBalance, _diff); - uint256 _foo = bpow(_y, _weightRatio); - vm.assume(bsub(_foo, BONE) < type(uint256).max / _tokenInBalance); - } - - function _assumeCalcOutGivenIn(uint256 _tokenInBalance, uint256 _tokenAmountIn, uint256 _swapFee) internal pure { - uint256 _adjustedIn = bsub(BONE, _swapFee); - _adjustedIn = bmul(_tokenAmountIn, _adjustedIn); - vm.assume(_tokenInBalance < type(uint256).max / BONE); - vm.assume(_tokenInBalance * BONE < type(uint256).max - (badd(_tokenInBalance, _adjustedIn) / 2)); - } - - function _assumeCalcPoolOutGivenSingleIn( - uint256 _tokenInDenorm, - uint256 _tokenInBalance, - uint256 _tokenAmountIn, - uint256 _swapFee, - uint256 _totalWeight, - uint256 _totalSupply - ) internal pure { - uint256 _normalizedWeight = bdiv(_tokenInDenorm, _totalWeight); - vm.assume(_normalizedWeight < bdiv(MAX_WEIGHT, MAX_TOTAL_WEIGHT)); - - uint256 _zaz = bmul(bsub(BONE, _normalizedWeight), _swapFee); - uint256 _tokenAmountInAfterFee = bmul(_tokenAmountIn, bsub(BONE, _zaz)); - uint256 _newTokenBalanceIn = badd(_tokenInBalance, _tokenAmountInAfterFee); - vm.assume(_newTokenBalanceIn < type(uint256).max / BONE); - vm.assume(_newTokenBalanceIn > _tokenInBalance); - - uint256 _tokenInRatio = bdiv(_newTokenBalanceIn, _tokenInBalance); - uint256 _poolRatio = bpow(_tokenInRatio, _normalizedWeight); - vm.assume(_poolRatio < type(uint256).max / _totalSupply); - } - - function _assumeCalcSingleInGivenPoolOut( - uint256 _tokenInBalance, - uint256 _tokenInDenorm, - uint256 _poolSupply, - uint256 _totalWeight, - uint256 _poolAmountOut - ) internal view { - uint256 _normalizedWeight = bdiv(_tokenInDenorm, _totalWeight); - uint256 _newPoolSupply = badd(_poolSupply, _poolAmountOut); - vm.assume(_newPoolSupply < type(uint256).max / BONE); - vm.assume(_newPoolSupply * BONE < type(uint256).max - (_poolSupply / 2)); // bdiv require - - uint256 _poolRatio = bdiv(_newPoolSupply, _poolSupply); - vm.assume(_poolRatio < MAX_BPOW_BASE); - vm.assume(BONE > _normalizedWeight); - - uint256 _boo = bdiv(BONE, _normalizedWeight); - uint256 _tokenRatio; - try pow.pow(_poolRatio, _boo) returns (uint256 _result) { - // pow didn't overflow - _tokenRatio = _result; - } catch { - // pow did an overflow. Reject this inputs - vm.assume(false); - } - - vm.assume(_tokenRatio < type(uint256).max / _tokenInBalance); - } - - function _assumeCalcSingleOutGivenPoolIn( - uint256 _tokenOutBalance, - uint256 _tokenOutDenorm, - uint256 _poolSupply, - uint256 _totalWeight, - uint256 _poolAmountIn, - uint256 _swapFee - ) internal pure { - uint256 _normalizedWeight = bdiv(_tokenOutDenorm, _totalWeight); - uint256 _exitFee = bsub(BONE, EXIT_FEE); - vm.assume(_poolAmountIn < type(uint256).max / _exitFee); - - uint256 _poolAmountInAfterExitFee = bmul(_poolAmountIn, _exitFee); - uint256 _newPoolSupply = bsub(_poolSupply, _poolAmountInAfterExitFee); - vm.assume(_newPoolSupply < type(uint256).max / BONE); - vm.assume(_newPoolSupply * BONE < type(uint256).max - (_poolSupply / 2)); // bdiv require - - uint256 _poolRatio = bdiv(_newPoolSupply, _poolSupply); - vm.assume(_poolRatio < MAX_BPOW_BASE); - vm.assume(_poolRatio > MIN_BPOW_BASE); - vm.assume(BONE > _normalizedWeight); - - uint256 _tokenOutRatio = bpow(_poolRatio, bdiv(BONE, _normalizedWeight)); - vm.assume(_tokenOutRatio < type(uint256).max / _tokenOutBalance); - - uint256 _newTokenOutBalance = bmul(_tokenOutRatio, _tokenOutBalance); - uint256 _tokenAmountOutBeforeSwapFee = bsub(_tokenOutBalance, _newTokenOutBalance); - uint256 _zaz = bmul(bsub(BONE, _normalizedWeight), _swapFee); - vm.assume(_tokenAmountOutBeforeSwapFee < type(uint256).max / bsub(BONE, _zaz)); - } - - function _assumeCalcPoolInGivenSingleOut( - uint256 _tokenOutBalance, - uint256 _tokenOutDenorm, - uint256 _poolSupply, - uint256 _totalWeight, - uint256 _tokenAmountOut, - uint256 _swapFee - ) internal pure { - uint256 _normalizedWeight = bdiv(_tokenOutDenorm, _totalWeight); - vm.assume(BONE > _normalizedWeight); - - uint256 _zoo = bsub(BONE, _normalizedWeight); - uint256 _zar = bmul(_zoo, _swapFee); - uint256 _tokenAmountOutBeforeSwapFee = bdiv(_tokenAmountOut, bsub(BONE, _zar)); - vm.assume(_tokenOutBalance >= _tokenAmountOutBeforeSwapFee); - uint256 _newTokenOutBalance = bsub(_tokenOutBalance, _tokenAmountOutBeforeSwapFee); - vm.assume(_newTokenOutBalance < type(uint256).max / _tokenOutBalance); - - uint256 _tokenOutRatio = bdiv(_newTokenOutBalance, _tokenOutBalance); - uint256 _poolRatio = bpow(_tokenOutRatio, _normalizedWeight); - vm.assume(_poolRatio < type(uint256).max / _poolSupply); - } -}