Pieces Protocol

First Flight #32
Beginner FriendlyFoundrySolidityNFT
100 EXP
View results
Submission Details
Severity: low
Invalid

Public Getter Functions Not Used Internally Should Be Marked External to Optimize Gas

Summary

Public Getter Functions Not Used Internally Should Be Marked External to Optimize Gas

Vulnerability Details

In the TokenDivider contract, several getter functions are declared as public but are never used internally within the contract. These functions include TokenDivider::getBalanceOf, TokenDivider::getErc20TotalMintedAmount, TokenDivider::getErc20InfoFromNft, and TokenDivider::getOrderPrice. Following Solidity best practices, functions that are only called externally should be marked as external instead of public to save gas.

Proof of Concept

The affected functions in the contract:

function getBalanceOf(address user, address token) public view returns(uint256) {
return balances[user][token];
}
function getErc20TotalMintedAmount(address erc20) public view returns(uint256) {
return erc20ToMintedAmount[erc20];
}
function getErc20InfoFromNft(address nft) public view returns(ERC20Info memory) {
return nftToErc20Info[nft];
}
function getOrderPrice(address seller, uint256 index) public view returns(uint256 price) {
price = s_userToSellOrders[seller][index].price;
}

Impact

Using public instead of external for these functions results in:

  • Slightly higher gas costs as public functions copy function arguments to memory

  • external functions can directly read arguments from calldata, which is more gas efficient

  • While the gas savings per call is small, it accumulates across all external calls to these functions

Tools Used

Foundry

Recommendations

Change the visibility specifier from public to external for these getter functions. Here's the diff of the changes:

- function getBalanceOf(address user, address token) public view returns(uint256) {
+ function getBalanceOf(address user, address token) external view returns(uint256) {
return balances[user][token];
}
- function getErc20TotalMintedAmount(address erc20) public view returns(uint256) {
+ function getErc20TotalMintedAmount(address erc20) external view returns(uint256) {
return erc20ToMintedAmount[erc20];
}
- function getErc20InfoFromNft(address nft) public view returns(ERC20Info memory) {
+ function getErc20InfoFromNft(address nft) external view returns(ERC20Info memory) {
return nftToErc20Info[nft];
}
- function getOrderPrice(address seller, uint256 index) public view returns(uint256 price) {
+ function getOrderPrice(address seller, uint256 index) external view returns(uint256 price) {
price = s_userToSellOrders[seller][index].price;
}
Updates

Lead Judging Commences

fishy Lead Judge 5 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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