Smart Contract Audit Report
BrainDex is building a Router contract to facilitate their new decentralized exchange aggregator.
For this audit, we reviewed the project team's BrainDexRouterV2 contract at commit b907a51fd8fd009759c6cd636eb368ed37116d77 on the team's GitHub repository.
All findings have been resolved, though some centralized aspects are present.
Date: December 28th, 2022.
Updated: January 4th, 2022 to reflect updates from commit 9c92848656d63ab700348cb0d91b03cfe3d9e6ec to commit b907a51fd8fd009759c6cd636eb368ed37116d77.
Finding #1 - BrainDexRouterV2 - Low (Resolved)Description: In the
multiSwapTokensForTokens()functions, 1 wei is deducted from the output amount after executing
executeSplitSwap()leading to an incorrect
netTokensamount being used for further calculations in the function.
Risk/Impact: When determining if enough tokens were received from the swap, the function will unexpectedly revert if exactly the
uint256 netTokens = IERC20(tokenOut).balanceOf(address(this)) - 1;
amountOutMintokens were recevied. In addition, there will always be residual tokens left in the contract on each swap.
Recommendation: The team should modify the
netTokenscalculation so that it will be exactly equal to the amount of tokens in the contract after the swap.Resolution: The team has implemented the above recommendation and has opted to retain a balance of 1 wei in the contract taken out of the admin fee in order to leverage a gas-saving mechanism which requires that storage slots used to store the token balance of the contract are never cleared.
uint256 netTokens = IERC20(tokenOut).balanceOf(address(this));
Finding #2 - BrainDexRouterV2 - Informational (Resolved)Description: The
_feeDepositstate variable is not utilized in the contract.
_feeDepositstate variable and
setFeeDeposit()function should either be removed to reduce contract size and deployment costs or utilized in a way that fits the project team's intended functionality.
Resolution: The team has decided to utilize the _feeDeposit address to direct fees from users' swaps.
This contract is intended to be used to perform swaps from one token asset to any other token asset supported by the platform. The contract relies on the BrainDexExecutor contract to perform all swaps; this contract was not provided in the scope of the audit so our team is unable to provide an assessment with regards to security and functionality. This contract contains the fee logic that is applied on each swap. When fees are enabled, a portion of the output amount from each swap is calculated and transferred to the fee deposit wallet. Fees are calculated based on the difference between the user's specified minimum output amount and the actual output amount received from the swap. A larger discrepancy between the minimum output amount and the actual output amount will incur a larger fee. The fee will be at least the minimum fee percentage and at most the maximum fee percentage; these values are determined by the team. The receiver of any swap cannot be the 0 address; if no receiver address is specified, the output tokens will be sent to the caller. The owner can set the fee deposit address to any value at any time. The owner can set the minimum and maximum fee percentages to any value at or below 2% at any time. The owner can enable or disable the fee at any time. The owner can pause or unpause swaps at any time. The owner can withdraw any residual tokens or ETH from the contract at any time. As the contract is implemented with Solidity v0.8.15, it is safe from any possible overflows/underflows.
|Arbitrary Jump/Storage Write||N/A||PASS|
|Centralization of Control||N/A||PASS|
|Delegate Call to Untrusted Contract||N/A||PASS|
|Dependence on Predictable Variables||N/A||PASS|
|Improper Authorization Scheme||N/A||PASS|
|Outdated Compiler Version||N/A||PASS|
|Overall Contract Safety||PASS|
($) = payable function # = non-constant function Int = Internal Ext = External Pub = Public + Context - [Int] _msgSender - [Int] _msgData + Ownable (Context) - [Pub]
# - [Pub] owner - [Int] _checkOwner - [Pub] renounceOwnership # - modifiers: onlyOwner - [Pub] transferOwnership # - modifiers: onlyOwner - [Int] _transferOwnership # + [Int] IKPool - [Ext] swap # - [Ext] swap # - [Ext] getReserves + [Int] ISaddleStableSwap - [Ext] calculateSwap - [Ext] swap # + [Int] IWETH - [Ext] deposit ($) - [Ext] transfer # - [Ext] withdraw # + [Int] IERC20 - [Ext] transferFrom # - [Ext] transfer # - [Ext] balanceOf - [Ext] approve # - [Ext] totalSupply + [Int] IBrainDexExecutor - [Ext] executeSplitSwap # + [Lib] TransferHelper - [Int] safeApprove # - [Int] safeTransfer # - [Int] safeTransferFrom # - [Int] safeTransferETH # + BrainDexRouterV2 (Ownable) - [Pub] # - [Ext] multiSwapEthForTokens ($) - modifiers: notPaused,ensureDeadline - [Ext] multiSwapTokensForEth # - modifiers: notPaused,ensureDeadline - [Ext] multiSwapTokensForTokens # - modifiers: notPaused,ensureDeadline - [Int] _sendAdminFee # - [Pub] getFee - [Int] _getFee - [Int] _sendEth # - [Ext] setFeeDeposit # - modifiers: onlyOwner - [Ext] setFees # - modifiers: onlyOwner - [Ext] setFeeOn # - modifiers: onlyOwner - [Ext] setPaused # - modifiers: onlyOwner - [Ext] isPaused - [Pub] minFee - [Pub] maxFee - [Pub] executor - [Pub] feeOn - [Ext] rescueTokens # - modifiers: onlyOwner - [Ext] rescueEth # - modifiers: onlyOwner - [Ext] ($)
SourceHat (formerly Solidity Finance - founded in 2020) has quickly grown to have one of the most experienced and well-equipped smart contract auditing teams in the industry. Our team has conducted 1700+ solidity smart contract audits covering all major project types and protocols, securing a total of over $50 billion U.S. dollars in on-chain value!
Our firm is well-reputed in the community and is trusted as a top smart contract auditing company for the review of solidity code, no matter how complex. Our team of experienced solidity smart contract auditors performs audits for tokens, NFTs, crowdsales, marketplaces, gambling games, financial protocols, and more!
Contact us today to get a free quote for a smart contract audit of your project!
What is a SourceHat Audit?
Typically, a smart contract audit is a comprehensive review process designed to discover logical errors, security vulnerabilities, and optimization opportunities within code. A SourceHat Audit takes this a step further by verifying economic logic to ensure the stability of smart contracts and highlighting privileged functionality to create a report that is easy to understand for developers and community members alike.
How Do I Interpret the Findings?
Each of our Findings will be labeled with a Severity level. We always recommend the team resolve High, Medium, and Low severity findings prior to deploying the code to the mainnet. Here is a breakdown on what each Severity level means for the project:
- High severity indicates that the issue puts a large number of users' funds at risk and has a high probability of exploitation, or the smart contract contains serious logical issues which can prevent the code from operating as intended.
- Medium severity issues are those which place at least some users' funds at risk and has a medium to high probability of exploitation.
- Low severity issues have a relatively minor risk association; these issues have a low probability of occurring or may have a minimal impact.
- Informational issues pose no immediate risk, but inform the project team of opportunities for gas optimizations and following smart contract security best practices.