Skip to content
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

Different formulas to calculate tokenAmount #37

Open
code423n4 opened this issue Dec 14, 2021 · 0 comments
Open

Different formulas to calculate tokenAmount #37

code423n4 opened this issue Dec 14, 2021 · 0 comments
Labels
1 (Low Risk) Assets are not at risk. State handling, function incorrect as to spec, issues with comments bug Something isn't working

Comments

@code423n4
Copy link
Contributor

Handle

gpersoon

Vulnerability details

Impact

The function joinPool() and calcTokensForAmount() both calculate the tokenAmount , taking in account the entryFee.
However both use a different formula.
The result of the formulas is the same as far as I can see.
However for maintenance purposes it is better to use the same formula.

Note1: exitPool() and calcTokensForAmountExit() do use the same formula.
Note2: When using the same formula it is easier to optimize for gas, for example to take "_amount.add(feeAmount)" outside of the for loop.

Proof of Concept

https://github.com/code-423n4/2021-12-amun/blob/cf890dedf2e43ec787e8e5df65726316fda134a1/contracts/basket/contracts/facets/Basket/BasketFacet.sol#L143-L185

function joinPool(uint256 _amount, uint16 _referral)  external override noReentry {
       ...
        uint256 feeAmount = _amount.mul(bs.entryFee).div(10**18);
        for (uint256 i; i < bs.tokens.length; i++) {
            IERC20 token = bs.tokens[i];
            uint256 tokenAmount =  balance(address(token)).mul(_amount.add(feeAmount)).div( totalSupply );

https://github.com/code-423n4/2021-12-amun/blob/cf890dedf2e43ec787e8e5df65726316fda134a1/contracts/basket/contracts/facets/Basket/BasketFacet.sol#L332-L362

 function calcTokensForAmount(uint256 _amount)  external view  override returns (address[] memory tokens, uint256[] memory amounts) {
...
        for (uint256 i; i < bs.tokens.length; i++) {
            IERC20 token = bs.tokens[i];
            uint256 tokenBalance = balance(address(token));
            uint256 tokenAmount = tokenBalance.mul(_amount).div(totalSupply);
            tokenAmount = tokenAmount.add(  tokenAmount.mul(bs.entryFee).div(10**18)  );

Tools Used

Recommended Mitigation Steps

In function calcTokensForAmount() use the same formula to calculate tokenAmount as in joinPool()

@code423n4 code423n4 added 1 (Low Risk) Assets are not at risk. State handling, function incorrect as to spec, issues with comments bug Something isn't working labels Dec 14, 2021
code423n4 added a commit that referenced this issue Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 (Low Risk) Assets are not at risk. State handling, function incorrect as to spec, issues with comments bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant