Pieces Protocol

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

`storage` variable is used in `TokenDivider::claimNft` function

Description

In `TokenDivider::claimNft` function there is a `storage` variable is defined and used to update the state variable in contract.
```javascript
function claimNft(address nftAddress) external {
if(nftAddress == address(0)) {
revert TokenDivider__NftAddressIsZero();
}
@> ERC20Info storage tokenInfo = nftToErc20Info[nftAddress];
if(balances[msg.sender][tokenInfo.erc20Address] < erc20ToMintedAmount[tokenInfo.erc20Address]) {
revert TokenDivider__NotEnoughErc20Balance();
}
```
However the `storage` variables are used to store data `permanently` in blockchain, but we are just updating the state variable. Using `storage` variable for that will require `more gas` then using `memory` veriable, as it reads directly storage variable.

Impact

Updating the state variables will require more gas.

Proof of Concept

Gas used in both storage and memory:
1. Gas used in `storage` variable: `18709`.
2. Gas used in `memory` variable: `18493`.
There is extra `216` gas is used while using storage variable in `TokenDivider::claimNft` function, Which can be ignored but it could be `memory`.

Recommended Mitigation

Use `memory` keyword instead of `storage` keyword suggested below.
```diff
function claimNft(address nftAddress) external {
if(nftAddress == address(0)) {
revert TokenDivider__NftAddressIsZero();
}
- ERC20Info storage tokenInfo = nftToErc20Info[nftAddress];
+ ERC20Info memory tokenInfo = nftToErc20Info[nftAddress];
if(balances[msg.sender][tokenInfo.erc20Address] < erc20ToMintedAmount[tokenInfo.erc20Address]) {
revert TokenDivider__NotEnoughErc20Balance();
}
```
Updates

Lead Judging Commences

fishy Lead Judge 5 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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