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 #79

Open
code423n4 opened this issue Feb 23, 2022 · 0 comments
Open

Gas Optimizations #79

code423n4 opened this issue Feb 23, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Hubble contest Gas Optimization

1 use initial value to save gas for uint.

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/InsuranceFund.sol#L52

uint shares;

2 use cache for array length to save gas.

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L122
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L130
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L170
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L194
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L263
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L277

3 Delete unused variable. It seems that the following variable will be not used in this construct and other contracts. If it is not used, you can delete it to save gas.

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L22

4 Avoid extra mstore.

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L121
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L141
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L614

isNewPosition and isLongPosition are used only one time in

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L123
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L143

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L616
You can use position.size == 0 and position.size > 0 directly there to save gas.

if (position.size == 0 || (position.size > 0 ? Side.LONG : Side.SHORT) == side) {
if (position.size > 0) {

5 Use msg.sender instead of _msgSender() to save gas.

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L65
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L69
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L98
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L113
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L214

6 Delete unused import statements in AMM.sol

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L5

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L9

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Feb 23, 2022
code423n4 added a commit that referenced this issue Feb 23, 2022
@atvanguard atvanguard added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Feb 26, 2022
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) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

2 participants