Refactoring NFTMarketCreators.sol can reduce decent amounts of gas and size #67
Labels
bug
Something isn't working
disagree with severity
Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments)
duplicate
This issue or pull request already exists
G (Gas Optimization)
sponsor acknowledged
Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Lines of code
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L91-L102
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L154-L162
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L190-L201
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L71-L72
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L134-L135
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L225-L226
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L240-L241
Vulnerability details
Impact
Refactoring NFTMarketCreators.sol can reduce decent amounts of gas and size. Given that the contract size is close to the max(24.576 KB), I will report this as mid pri.
Proof of Concept
_getCreatorPaymentInfo
function has some duplicated logic which can be put into function.Refactoring opportunity #1
Following codes which contain looop are duplicated.
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L91-L102
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L154-L162
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L190-L201
Refactoring opportunity #2
Following codes which resets recipients are duplicated.
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L71-L72
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L134-L135
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L225-L226
https://github.com/code-423n4/2022-02-foundation/blob/main/contracts/mixins/NFTMarketCreators.sol#L240-L241
Tools Used
yarn test
yarn size-contracts
Recommended Mitigation Steps
Refactoring opportunity #1
Here is an example function to use at the refactoring opportunity #1.
Here is an example code how to use
getHasRecipientAndShouldReturn
function.Refactoring opportunity #2
Here is an example function to use at the refactoring opportunity #2.
Here is an example code how to use
_resetAndGetRecipients
function.Results
Following results are before or after implementing the above two refactoring.
Gas cost
Before the change,
$ yarn test
returns gas cost for FNDNFTMarket:Min=5236255
Max=5236291
Avg=5236287
After the change,
$ yarn test
returns gas cost for FNDNFTMarket:Min=5179631
Max=5179667
Avg=5179663
The above change can reduce 56624 gas cost on average.
Code size
Before the change,
$ yarn size-contracts
returns23.877 KB
for FNDNFTMarket:After the change,
$ yarn size-contracts
returns23.615 KB
for FNDNFTMarket:The above change can reduce
0.262 KB
.The text was updated successfully, but these errors were encountered: