forked from balancer/balancer-core
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adding btt test for joinswap extern amount in #164
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4076a08
refactor: explicitly set weights in every scenario, remove it from ba…
0xteddybear 5bd8bab
test: btt tests for joinswapExternAmountIn
0xteddybear 64cafaa
chore: remove preexisting unit tests replaced by ones in this pr
0xteddybear d755adf
test: query tokenIn balance
0xteddybear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.25; | ||
|
||
import {BPoolBase} from './BPoolBase.sol'; | ||
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; | ||
|
||
import {BNum} from 'contracts/BNum.sol'; | ||
import {IBPool} from 'interfaces/IBPool.sol'; | ||
|
||
contract BPoolJoinswapExternAmountIn is BPoolBase, BNum { | ||
address public tokenIn; | ||
|
||
// Valid scenario | ||
uint256 public tokenAmountIn = 1e18; | ||
uint256 public tokenInWeight = 2e18; | ||
uint256 public totalWeight = 10e18; | ||
uint256 public tokenInBalance = 50e18; | ||
|
||
// (((tokenAmountIn*(1-(1-tokenInWeight/totalWeight)*MIN_FEE)+tokenInBalance)/tokenInBalance)^(tokenInWeight/totalWeight))*INIT_POOL_SUPPLY - INIT_POOL_SUPPLY | ||
// (((1*(1-(1-2/10)*(10^-6))+50)/50)^(2/10))*100 - 100 | ||
// 0.396837555601045600 | ||
uint256 public expectedPoolOut = 0.3968375556010456e18; | ||
|
||
function setUp() public virtual override { | ||
super.setUp(); | ||
tokenIn = tokens[0]; | ||
bPool.set__finalized(true); | ||
// mint an initial amount of pool shares (expected to happen at _finalize) | ||
bPool.call__mintPoolShare(INIT_POOL_SUPPLY); | ||
bPool.set__tokens(_tokensToMemory()); | ||
bPool.set__totalWeight(totalWeight); | ||
_setRecord(tokenIn, IBPool.Record({bound: true, index: 0, denorm: tokenInWeight})); | ||
vm.mockCall(tokenIn, abi.encodePacked(IERC20.balanceOf.selector), abi.encode(uint256(tokenInBalance))); | ||
} | ||
|
||
function test_RevertWhen_ReentrancyLockIsSet() external { | ||
bPool.call__setLock(_MUTEX_TAKEN); | ||
// it should revert | ||
vm.expectRevert(IBPool.BPool_Reentrancy.selector); | ||
bPool.joinswapExternAmountIn(tokenIn, tokenAmountIn, expectedPoolOut); | ||
} | ||
|
||
function test_RevertWhen_PoolIsNotFinalized() external { | ||
bPool.set__finalized(false); | ||
// it should revert | ||
vm.expectRevert(IBPool.BPool_PoolNotFinalized.selector); | ||
bPool.joinswapExternAmountIn(tokenIn, tokenAmountIn, expectedPoolOut); | ||
} | ||
|
||
function test_RevertWhen_TokenIsNotBound() external { | ||
// it should revert | ||
vm.expectRevert(IBPool.BPool_TokenNotBound.selector); | ||
bPool.joinswapExternAmountIn(makeAddr('unknown token'), tokenAmountIn, expectedPoolOut); | ||
} | ||
|
||
function test_RevertWhen_TokenAmountInExceedsMaxRatio(uint256 amountIn) external { | ||
amountIn = bound(amountIn, bdiv(INIT_POOL_SUPPLY, MAX_IN_RATIO) + 1, type(uint256).max); | ||
// it should revert | ||
vm.expectRevert(IBPool.BPool_TokenAmountInAboveMaxRatio.selector); | ||
bPool.joinswapExternAmountIn(tokenIn, amountIn, expectedPoolOut); | ||
} | ||
|
||
function test_RevertWhen_CalculatedPoolAmountOutIsLessThanExpected(uint256 expectedPoolOut_) external { | ||
expectedPoolOut_ = bound(expectedPoolOut_, expectedPoolOut + 1, type(uint256).max); | ||
// it should revert | ||
vm.expectRevert(IBPool.BPool_PoolAmountOutBelowMinPoolAmountOut.selector); | ||
bPool.joinswapExternAmountIn(tokenIn, tokenAmountIn, expectedPoolOut_); | ||
} | ||
|
||
function test_WhenPreconditionsAreMet() external { | ||
// it sets reentrancy lock | ||
bPool.expectCall__setLock(_MUTEX_TAKEN); | ||
// it queries the contracts token in balance | ||
vm.expectCall(tokenIn, abi.encodeCall(IERC20.balanceOf, (address(bPool)))); | ||
// it calls _pullUnderlying for token | ||
bPool.mock_call__pullUnderlying(tokenIn, address(this), tokenAmountIn); | ||
bPool.expectCall__pullUnderlying(tokenIn, address(this), tokenAmountIn); | ||
// it mints the pool shares | ||
bPool.expectCall__mintPoolShare(expectedPoolOut); | ||
// it sends pool shares to caller | ||
bPool.expectCall__pushPoolShare(address(this), expectedPoolOut); | ||
// it emits LOG_CALL event | ||
bytes memory _data = | ||
abi.encodeWithSelector(IBPool.joinswapExternAmountIn.selector, tokenIn, tokenAmountIn, expectedPoolOut); | ||
vm.expectEmit(); | ||
emit IBPool.LOG_CALL(IBPool.joinswapExternAmountIn.selector, address(this), _data); | ||
// it emits LOG_JOIN event for token | ||
vm.expectEmit(); | ||
emit IBPool.LOG_JOIN(address(this), tokenIn, tokenAmountIn); | ||
bPool.joinswapExternAmountIn(tokenIn, tokenAmountIn, expectedPoolOut); | ||
|
||
// it clears the reentrancy lock | ||
assertEq(_MUTEX_FREE, bPool.call__getLock()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
BPool::JoinswapExternAmountIn | ||
├── when reentrancy lock is set | ||
│ └── it should revert | ||
├── when pool is not finalized | ||
│ └── it should revert | ||
├── when token is not bound | ||
│ └── it should revert | ||
├── when token amount in exceeds max ratio | ||
│ └── it should revert | ||
├── when calculated pool amount out is less than expected | ||
│ └── it should revert | ||
└── when preconditions are met | ||
├── it emits LOG_CALL event | ||
├── it sets the reentrancy lock | ||
├── it queries the contracts token in balance | ||
├── it emits LOG_JOIN event for token | ||
├── it calls _pullUnderlying for token | ||
├── it mints the pool shares | ||
├── it sends pool shares to caller | ||
└── it clears the reentrancy lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we reduce this formula to what's described in BMath? like, using
wi
orwT
instead of full variable names, perhaps we can do this accross all testsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could, but for that we could reference bmath directly. In these comments I explicitly implement the formulae as defined by bmath in a notation that's easily executable, using the same variable names that'll be found in the code, as both a sanity check and the way to independently compute them (this one got me searching for alternatives to
bc
tho, because of the non-integer exponent)