20,000 USDC
View results
Submission Details
Severity: medium

Insufficient balance check in `Staking.sol` contract's `withdraw` function

Summary

The withdraw() function in the Staking contract don't have a check that ensures the contract has sufficient tokens to distribute before transferring tokens to a user. This could potentially lead to a failed transaction if the contract's token balance is insufficient to fulfill a user's withdrawal request.

Vulnerability Details

The vulnerability arises from the lack of a balance check in the withdraw() function of the Staking.sol contract. When a user calls the withdraw() function to remove their stake, the contract deducts the withdrawal amount from the user's balance and attempts to transfer the corresponding amount of tokens to the user. However, the contract does not check if it has enough tokens to fulfill the withdrawal request before initiating the transfer.

Code Snippet

function withdraw(uint _amount) external {
updateFor(msg.sender);
balances[msg.sender] -= _amount;
TKN.transfer(msg.sender, _amount);
}

Impact

In the case of an insufficient contract balance, the execution of the withdraw() function could result in a failed transaction. This could disrupt the normal functioning of the staking protocol and could potentially lock user funds. This presents a considerable usability and functional risk to the contract.

Tools Used

Manual Code Analysis

Recommendations

To mitigate this potential vulnerability, it is recommended to include a check that ensures the contract has a sufficient token balance before initiating a transfer. This could look something like this:

function withdraw(uint _amount) external {
require(TKN.balanceOf(address(this)) >= _amount, "Insufficient contract balance");
updateFor(msg.sender);
balances[msg.sender] -= _amount;
TKN.transfer(msg.sender, _amount);
}

This addition ensures that the contract has enough tokens to fulfill the withdrawal request. If the contract balance is insufficient, the transaction will fail with an "Insufficient contract balance" error, which alerts users to the issue in a clear manner.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.