Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #68

Open
code423n4 opened this issue Apr 2, 2022 · 1 comment
Open

Gas Optimizations #68

code423n4 opened this issue Apr 2, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

  1. !=0 more effective than < 0

Proof of Concept:
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L229
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L385
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L1078

Recommended Mitigation Steps:
Change > 0 or < 0 to != 0

========================================================================

  1. Can save gas by using -- instead of - 1

Proof of Concept:
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L515

Recommended Mitigation Steps:
Change to:

 uint256 high = --nbCheckpoints;

========================================================================

  1. unnecessary value set. the default value of uint is zero.

Proof of Concept:
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L516
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L796
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L807

Recommended Mitigation Steps:
remove 0

========================================================================

  1. unnecessary value set. the default value of bool is false

Proof of Concept:
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L103

Recommended Mitigation Steps:
remove false

========================================================================

  1. more efficient gas usage by removing && operator

oof of Concept:
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L1271

Recommended Mitigation Steps:

require(user != address(0)):
require(kicker != address(0), "hPAL: Address Zero");

========================================================================

  1. Using > is cheaper than >=

Proof of Concept:
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L1194-L1195
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L1141-L1143

Recommended Mitigation Steps:
Change from >= to >

========================================================================

  1. the best way to use SafeERC20.function for gas opt

Proof of Concept:
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/PaladinRewardReserve.sol#L12

Recommended Mitigation Steps:
by not declaring:

using SafeERC20 for IERC20;

and use:

SafeERC20.safeTransfer(IERC20(token), receiver, amount);

========================================================================

  1. Using calldata to store struct data type can save gas

Proof of Concept:
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L451
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L466
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L482
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/HolyPaladinToken.sol#L492

Recommended Mitigation Steps:
Change from memory to calldata

========================================================================

  1. caching proof.length for loop can save gas

Proof of Concept:
https://github.com/code-423n4/2022-03-paladin/blob/main/contracts/open-zeppelin/utils/MerkleProof.sol#L28

Recommended Mitigation Steps:

uint256 saveLengt = proof.length
for (uint256 i; i < saveLengt; ++i)

========================================================================

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Apr 2, 2022
code423n4 added a commit that referenced this issue Apr 2, 2022
@Kogaroshi
Copy link
Collaborator

QA & gas optimizations changes are done in the PR: PaladinFinance/Paladin-Tokenomics#6
(some changes/tips were implemented, others are noted but won't be applied)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

2 participants