Badger-Vested-Aura contest - TomJ's results

Bringing BTC to DeFi

General Information

Platform: Code4rena

Start Date: 15/06/2022

Pot Size: $30,000 USDC

Total HM: 5

Participants: 55

Period: 3 days

Judge: Jack the Pug

Id: 138

League: ETH

BadgerDAO

Findings Distribution

Researcher Performance

Rank: 53/55

Findings: 1

Award: $31.91

🌟 Selected for report: 0

🚀 Solo Findings: 0

Awards

31.9127 USDC - $31.91

Labels

bug
G (Gas Optimization)
sponsor acknowledged
valid

External Links

[G-01] Unnecessary variable initialization of default value

When variable is not initialized, it will have its default values. Example: 0 for uint, false for bool and address(0) for address By removing this default values you will save about 5 gas.

I suggest removing default value initialization for following variables.

MyStrategy.sol 118: for(uint i = 0; i < length; i++){ 300: for (uint256 i = 0; i < _claims.length; i++) { 317: for (uint256 i = 0; i < _claims.length; i++) {

For example these can change to:

  • for (uint256 i; i < _claims.length; i++) {

[G-02] Save Gas in For-Loops by storing array's length as a variable

3 gas per iteration can be saved by storing an array's length as a variable before the for-loop.

Issue found at:

MyStrategy.sol 300: for (uint256 i = 0; i < _claims.length; i++) { 317: for (uint256 i = 0; i < _claims.length; i++) {

For example, I suggest changing it to:

length = _claims.length for (uint i; i < length; i++) {

[G-03] ++i costs less gas than i++

It is better to use ++i than i++ when possible since it costs less gas. It saves about 5 gas per iteration.

Issue found at:

MyStrategy.sol 118: for(uint i = 0; i < length; i++){ 300: for (uint256 i = 0; i < _claims.length; i++) { 317: for (uint256 i = 0; i < _claims.length; i++) {

[G-04] Not Defining Variables to Reduce Gas

Certain variables is defined even though they are used only once. Remove these unnecessary variables to save gas. It saves about 10 gas for each variable (Total 100gas). For cases where it will reduce the readability, one can use comments to help describe what the code is doing.

Issues found at MyStrategy.sol

  1. Remove toSend variable of sweepRewardToken function
111: uint256 toSend = IERC20Upgradeable(token).balanceOf(address(this)); 112: _handleRewardTransfer(token, toSend);
  1. Remove balances variable of balanceOfPool function
143: IAuraLocker.Balances memory balances = LOCKER.balances(address(this)); 144: return balances.locked;
  1. Remove auraBalBalanceBefore variable of _harvest function
220: uint256 auraBalBalanceBefore = AURABAL.balanceOf(address(this)); 228: uint256 auraBalEarned = AURABAL.balanceOf(address(this)).sub(auraBalBalanceBefore);
  1. Remove balEthBptEarned variable of _harvest function
249: uint256 balEthBptEarned = BALANCER_VAULT.swap(singleSwap, fundManagement, 0, type(uint256).max); 260: userData: abi.encode(ExitKind.EXACT_BPT_IN_FOR_ONE_TOKEN_OUT, balEthBptEarned, BPT_WETH_INDEX),
  1. Remove wethBalanceBefore variable of _harvest function
252: uint256 wethBalanceBefore = WETH.balanceOf(address(this)); 266: uint256 wethEarned = WETH.balanceOf(address(this)).sub(wethBalanceBefore);
  1. Remove exitPoolRequest variable of _harvest function
257: IBalancerVault.ExitPoolRequest memory exitPoolRequest = IBalancerVault.ExitPoolRequest({ 263: BALANCER_VAULT.exitPool(BAL_ETH_POOL_ID, address(this), payable(address(this)), exitPoolRequest);
  1. Remove wethEarned variable of _harvest function
266: uint256 wethEarned = WETH.balanceOf(address(this)).sub(wethBalanceBefore); 272: amount: wethEarned,
  1. Remove beforeVaultBalance variable of claimBribesFromHiddenHand function
292: uint256 beforeVaultBalance = _getBalance(); 341: require(beforeVaultBalance == _getBalance(), "Balance can't change");
  1. Remove beforePricePerFullShare variable of claimBribesFromHiddenHand function
293: uint256 beforePricePerFullShare = _getPricePerFullShare(); 342: require(beforePricePerFullShare == _getPricePerFullShare(), "Ppfs can't change");
  1. Remove auraAmount variable of manualSendAuraToVault function
381: uint256 auraAmount = balanceOfWant(); 382: _transferToVault(auraAmount);

For mitigation, simply don't define variable that is used only once. Below is mitigation example of above 1.

_handleRewardTransfer(token, IERC20Upgradeable(token).balanceOf(address(this)));

[G-05] Reduce the long revert strings of error messages

By keeping the revert strings within 32 bytes will save you gas since each slot is 32 bytes and cost about 20k gas.

Following are revert strings that are more than 32 bytes.

MyStrategy.sol 184: require( 185: balanceOfPool() == 0 && LOCKER.balanceOf(address(this)) == 0, 186: "You have to wait for unlock or have to manually rebalance out of it"

[G-06] Use require instead of &&

When there are multiple conditions in require statement, break down the require statement into multiple require statements instead of using && can save gas. (saves about 50 gas)

Issue found at

184: require( 185: balanceOfPool() == 0 && LOCKER.balanceOf(address(this)) == 0, 186: "You have to wait for unlock or have to manually rebalance out of it" 187: );

For example these can be changed to

require(balanceOfPool() == 0); require(LOCKER.balanceOf(address(this)) == 0, "You have to wait for unlock or have to manually rebalance out of it");

#0 - GalloDaSballo

2022-06-19T01:38:47Z

Ack, note that assigning a value costs 3 gas, not 10

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