PaalSwap

Smart Contract Audit Report

Audit Summary

PaalSwap Audit Report PaalSwap is building a new platform where users can buy and sell tokens.

For this audit, we reviewed the PaalAISwap contract provided to us by the project team.

Audit Findings

All findings have been resolved, though some centralized aspects are present.
Date: October 10th, 2023.
Updated: October 11th, 2023 to reflect updates made to the contract by the project team that resolves all Findings.
Updated: October 17th, 2023 to reflect updates made to the contract to remove the ability to swap any tokens for any other tokens.

Finding #1 - PaalSwap - High (Resolved)

Description: The sell() function enables users to sell any token, including fee-on-transfer tokens, but the function does not contain logic for fee-on-transfer support.
Risk/Impact: If a user attempts to sell a fee-on-transfer token, the txAmount local variable will be set to a value higher than the number of tokens transferred to the contract in the transaction. As a result, the transaction will either fail if the contract lacks a sufficient token balance or a portion of the tokens to be swapped will be funded by fees from previous transactions.
Recommendation: The team should implement fee-on-transfer support in the sell() function by recording the contract's original token balance before the transfer occurs. After the transfer occurs, the txAmount value should be set to the difference between the contract's new balance and the initial one (subtracting the swapping fee). This change could be implemented as follows:
uint256 initialBalance = token.balanceOf(address(this));

token.transferFrom(sender, address(this), amountIn);

uint256 actualReceived = token.balanceOf(address(this)) - initialBalance;

uint256 fee = (actualReceived * swappingFee) / 1000000;
uint256 txAmount = actualReceived - fee;

token.approve(address(router), txAmount);
Resolution: The team has implemented the above recommendation.

Finding #2 - PaalSwap - High (Resolved)

Description: The swapTokensForTokens() function enables users to initiate a swap with any token, including fee-on-transfer tokens, but the function does not properly contain logic for fee-on-transfer support.
Risk/Impact: If a user attempts to swap a fee-on-transfer token, the amountIn parameter passed into the swapExactTokensForTokensSupportingFeeOnTransferTokens() function will be greater than the number of tokens transferred to the contract in the transaction. As a result, the transaction will either fail if the contract lacks a sufficient token balance or a portion of the tokens to be swapped will be funded by fees from previous transactions.
Recommendation: The team should implement fee-on-transfer support in the swapTokensForTokens() function by recording the contract's original "from" token balance before the transfer occurs. After the transfer occurs, the first parameter passed into the swapExactTokensForTokensSupportingFeeOnTransferTokens() function should be the difference between the contract's new balance and the initial one. This change could be implemented as follows:
uint256 initialBalanceFromToken = fromToken.balanceOf(address(this));

fromToken.transferFrom(sender, address(this), amountIn);

uint256 actualReceived = fromToken.balanceOf(address(this)) - initialBalanceFromToken;

fromToken.approve(address(router), actualReceived);

address[] memory path = new address[](2);
path[0] = fromTokenAddr;
path[1] = toTokenAddr;

uint256 initialBalanceToToken = toToken.balanceOf(address(this));

router.swapExactTokensForTokensSupportingFeeOnTransferTokens(
    actualReceived,
    amountOutMin,
    path,
    address(this),
    block.timestamp + 300
);
Resolution: The team has implemented the above recommendation.

Finding #3 - PaalSwap - Low (Resolved)

Description: The swapTokensForTokens() function calculates the fee local variable based on the amountOutMin parameter rather than the actual number of the toTokenAddr received after the swap occurs.
Risk/Impact: The calculated fee value may be inaccurate as it is based on the arbitrary expectedReceived value passed in by the caller.
Recommendation: The fee calculation should be performed after the swap occurs using the exact number of tokens received. This could be implemented as follows:
uint256 initialBalanceToToken = toToken.balanceOf(address(this));

    router.swapExactTokensForTokensSupportingFeeOnTransferTokens(
    actualReceived,
    amountOutMin,
    path,
    address(this),
    block.timestamp + 300
);

uint256 actualOut = toToken.balanceOf(address(this)) - initialBalanceToToken;
require(actualOut >= amountOutMin, "Received less than the expected amount");

uint256 fee = (actualOut * swappingFee) / 1000000;
uint256 userAmountOut = actualOut - fee;

toToken.transfer(sender, userAmountOut);

emit SwapTokens(sender, fromTokenAddr, toTokenAddr, amountIn, userAmountOut);
Resolution: The team has implemented the above recommendation.

Contract Overview

  • Any user can initiate a purchase by specifying the address of the token to purchase, an amount of ETH for the purchase, and a minimum amount of tokens that must be received in order for the transaction to successfully occur.
  • There is a Swapping fee (set by the owner) that is deducted from the caller's provided ETH. This fee remains in the contract.
  • The remaining ETH is swapped for the specified token and sent to the caller via the Router contract set by the team on deployment. The Router contract was out of scope for this audit so our team is unable to provide an assessment with regard to its security.
  • Any user can initiate a sell by specifying the address of a token to sell, the number of tokens to sell, and a minimum amount of ETH that must be received in exchange for the token in order for the transaction to successfully occur.
  • The specified number of tokens are initially transferred from the caller to the contract. The caller must grant the contract a sufficient allowance in order for the transaction to successfully occur.
  • There is a Swapping fee that is deducted from the number of tokens to sell. This fee remains in the contract.
  • The remaining tokens are swapped for ETH and sent to the caller via the Router contract.
  • The owner can set the Swapping fee to any percentage at any time.
  • The owner can withdraw any tokens or ETH from the contract at any time.

Audit Results

Vulnerability Category Notes Result
Arbitrary Jump/Storage Write N/A PASS
Centralization of Control The owner can set the Swapping fee to any percentage at any time. WARNING
Compiler Issues N/A PASS
Delegate Call to Untrusted Contract N/A PASS
Dependence on Predictable Variables N/A PASS
Ether/Token Theft N/A PASS
Flash Loans N/A PASS
Front Running N/A PASS
Improper Events N/A PASS
Improper Authorization Scheme N/A PASS
Integer Over/Underflow N/A PASS
Logical Issues N/A PASS
Oracle Issues N/A PASS
Outdated Compiler Version N/A PASS
Race Conditions N/A PASS
Reentrancy N/A PASS
Signature Issues N/A PASS
Sybil Attack N/A PASS
Unbounded Loops N/A PASS
Unused Code N/A PASS
Overall Contract Safety   PASS

Inheritance Chart

Smart Contract Audit - Inheritance

Function Graph

Smart Contract Audit - Graph

Functions Overview


 ($) = payable function
 # = non-constant function
 
 Int = Internal
 Ext = External
 Pub = Public

 + [Int] IERC20 
    - [Ext] totalSupply
    - [Ext] balanceOf
    - [Ext] transfer #
    - [Ext] allowance
    - [Ext] approve #
    - [Ext] transferFrom #

 + [Int] IUniswapV2Router01 
    - [Ext] factory
    - [Ext] WETH
    - [Ext] addLiquidity #
    - [Ext] addLiquidityETH ($)
    - [Ext] removeLiquidity #
    - [Ext] removeLiquidityETH #
    - [Ext] removeLiquidityWithPermit #
    - [Ext] removeLiquidityETHWithPermit #
    - [Ext] swapExactTokensForTokens #
    - [Ext] swapTokensForExactTokens #
    - [Ext] swapExactETHForTokens ($)
    - [Ext] swapTokensForExactETH #
    - [Ext] swapExactTokensForETH #
    - [Ext] swapETHForExactTokens ($)
    - [Ext] quote
    - [Ext] getAmountOut
    - [Ext] getAmountIn
    - [Ext] getAmountsOut
    - [Ext] getAmountsIn

 + [Int] IUniswapV2Router02 (IUniswapV2Router01)
    - [Ext] removeLiquidityETHSupportingFeeOnTransferTokens #
    - [Ext] removeLiquidityETHWithPermitSupportingFeeOnTransferTokens #
    - [Ext] swapExactTokensForTokensSupportingFeeOnTransferTokens #
    - [Ext] swapExactETHForTokensSupportingFeeOnTransferTokens ($)
    - [Ext] swapExactTokensForETHSupportingFeeOnTransferTokens #

 +  Context 
    - [Int] _msgSender
    - [Int] _msgData

 +  Ownable (Context)
    - [Pub]  #
    - [Pub] owner
    - [Int] _checkOwner
    - [Pub] renounceOwnership #
       - modifiers: onlyOwner
    - [Pub] transferOwnership #
       - modifiers: onlyOwner
    - [Int] _transferOwnership #

 +  PaalAISwap (Context, Ownable)
    - [Pub]  #
    - [Ext] buy ($)
    - [Ext] sell #
    - [Ext] withdraw #
       - modifiers: onlyOwner
    - [Ext] withdrawToken #
       - modifiers: onlyOwner
    - [Ext] setSwappingFee #
       - modifiers: onlyOwner

About SourceHat

SourceHat has quickly grown to have one of the most experienced and well-equipped smart contract auditing teams in the industry. Our team has conducted 1800+ 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.