Badger Citadel contest - Jujic's results

Bringing BTC to DeFi

General Information

Platform: Code4rena

Start Date: 04/02/2022

Pot Size: $30,000 USDC

Total HM: 3

Participants: 37

Period: 3 days

Judge: leastwood

Id: 84

League: ETH

BadgerDAO

Findings Distribution

Researcher Performance

Rank: 32/37

Findings: 1

Award: $40.51

🌟 Selected for report: 0

🚀 Solo Findings: 0

Findings Information

Awards

40.5129 USDC - $40.51

Labels

bug
G (Gas Optimization)

External Links

Handle

Jujic

Vulnerability details

1. Long Revert Strings

Impact

Shortening revert strings to fit in 32 bytes will decrease deployment time gas and will decrease runtime gas when the revert condition has been met. ##Proof of Concept

require( _saleStart >= block.timestamp, "TokenSale: start date may not be in the past" );

https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L108

https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L112

https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L114

https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L117

https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L274

https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L290

https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L304

https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L318

Tools

https://planetcalc.com/9029/

Shorten the revert strings to fit in 32 bytes.

2. Adding unchecked directive can save gas

Impact

For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.

Proof of Concept

https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L232

function getTokenInLimitLeft() external view returns (uint256 limitLeft_) { if (totalTokenIn < tokenInLimit) { limitLeft_ = tokenInLimit - totalTokenIn; } }

Tools

Remix

Consider using 'unchecked' where it is safe to do so. Example:

unchecked { limitLeft_ = tokenInLimit - totalTokenIn; }

3. Caching variables

Impact

Some of the variable can be cached to slightly reduce gas usage.

Proof of Concept

saleStart and guestlist can be cached. https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L144-L186

Tools

Remix

Consider caching those variable for read and make sure write back to storage.

4. Use Minimal Interface for gas optimizations

Vulnerability details

Impact

ERC20Upgradeable is a superset of ERC20 and in this case, only ERC20 functions are required to be called on, so IERC20 can be used. ##Proof of Concept https://github.com/code-423n4/2022-02-badger-citadel/blob/08d43b5662f6d6c3707b720eadc773b913f79f2c/contracts/TokenSaleUpgradeable.sol#L19-L21

ERC20Upgradeable public tokenOut; ERC20Upgradeable public tokenIn;

Tools

Remix

#0 - GalloDaSballo

2022-02-14T13:58:28Z

Agree with all findings except 4. Use Minimal Interface for gas optimizations, code that is not used doesn't join the final bytecode, the compiler removes it for you

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter