Skip to content

Latest commit

 

History

History
84 lines (57 loc) · 3.32 KB

006.md

File metadata and controls

84 lines (57 loc) · 3.32 KB

lemonr

medium

Gas Optimazation

Summary

Gas Optimazation is reducing the amount of gas required to compute and execute transactions; In this smartcontract codebase i have identified ways required to reduce the gas used for transactions: gas-1: usage of !=0 instead of >0 to check for conditions; gas-2: usage of long reverts statements; gas-3 : += Costs More Gas Than = + gas-4: ++i Costs Less Gas Than i++, Especially When It’s Used In For-loops (--i/i-- Too)

Vulnerability Detail

gas-1 && gas-2: Before for !>0 and long Reverts statements [PASS] test_depositAsset_ERC721_double_collateral_overwrite_prevention() (gas: 254412) gas-1 && gas-2: After for !=0 and short Reverts statements

[PASS] test_depositAsset_ERC721_double_collateral_overwrite_prevention() (gas: 254370) Amount of gas saved (254412 - 254370) = 42

gas-3 : Before [PASS] test_withdrawAsset_ERC1155() (gas: 154612) [PASS] test_withdrawAsset_ERC20() (gas: 116988)

gas-3 : After [PASS] test_withdrawAsset_ERC1155() (gas: 154602) [PASS] test_withdrawAsset_ERC20() (gas: 116980)

Amount of gas Saved for the total withdraw ((gas: 154612) + (gas: 116988) ) - ((gas: 154602)+(gas: 116980) ) = 18

gas-4 Before ++i Costs Less Gas Than i++ [PASS] test_commit_collateral_array() (gas: 25543) [PASS] test_deployAndDeposit_backed() (gas: 257879) [PASS] test_getCollateralInfo() (gas: 131426) [PASS] test_withdraw_internal() (gas: 218991) [PASS] test_checkBalances_internal_invalid() (gas: 19650) [PASS] test_checkBalances_internal_valid() (gas: 21709)

gas-4 After ++i Costs Less Gas Than i++ [PASS] test_commit_collateral_array() (gas: 25538) [PASS] test_deployAndDeposit_backed() (gas: 257874) [PASS] test_getCollateralInfo() (gas: 131421) [PASS] test_withdraw_internal() (gas: 218986) [PASS] test_checkBalances_internal_invalid() (gas: 19645) [PASS] test_checkBalances_internal_valid() (gas: 21699)

Amount of gas saved: (gas: 25543) - (gas: 25538) = 35

Impact

  1. Increase Transaction costs
  2. it can impact the scalability of the Ethereum blockchain. This is because the blockchain can only process a limited number of transactions per second, and high gas costs can slow down the processing of transactions.
  3. Take longer to execute. This can impact the user experience, especially if the contract is used in a time-sensitive application.

Code Snippet

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/escrow/CollateralEscrowV1.sol#L50-L77

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/escrow/CollateralEscrowV1.sol#L84-L103

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/CollateralManager.sol#L117-L131

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/CollateralManager.sol#L179-L200

https://github.com/sherlock-audit/2023-03-teller/blob/main/teller-protocol-v2/packages/contracts/contracts/CollateralManager.sol#L215-L229

Tool used

Foundry Hardhart Ganache

Recommendation

gas-1: usage of !=0 instead of >0 to check for conditions; gas-2: usage of long reverts statements; gas-3 : += Costs More Gas Than = + gas-4: ++i Costs Less Gas Than i++, Especially When It’s Used In For-loops (--i/i-- Too)