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 improvement in withdraw & withdrawAll #18

Open
code423n4 opened this issue Aug 7, 2021 · 1 comment
Open

gas improvement in withdraw & withdrawAll #18

code423n4 opened this issue Aug 7, 2021 · 1 comment
Labels
bug Something isn't working fixed-in-upstream-repo This task has been implemented in the upstream repo G (Gas Optimization) resolved sponsor acknowledged

Comments

@code423n4
Copy link
Contributor

Handle

gpersoon

Vulnerability details

Impact

The functions withdraw and withdrawAll retrieve the value of marketIndexOfToken[token] twice.
The second time its stored in the variable marketIndex.
This could be optimized to save a bit of gas

Proof of Concept

// https://github.com/code-423n4/2021-08-floatcapital/blob/main/contracts/contracts/Staker.sol#L948

function withdraw(address token, uint256 amount) external {
ILongShort(longShort).updateSystemState(marketIndexOfToken[token]);
uint32 marketIndex = marketIndexOfToken[token];

function withdrawAll(address token) external {
ILongShort(longShort).updateSystemState(marketIndexOfToken[token]);
uint32 marketIndex = marketIndexOfToken[token];

Tools Used

Recommended Mitigation Steps

Replace:
ILongShort(longShort).updateSystemState(marketIndexOfToken[token]);
uint32 marketIndex = marketIndexOfToken[token];

With:
uint32 marketIndex = marketIndexOfToken[token];
ILongShort(longShort).updateSystemState(marketIndex);

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Aug 7, 2021
code423n4 added a commit that referenced this issue Aug 7, 2021
@JasoonS
Copy link
Collaborator

JasoonS commented Aug 10, 2021

image

This is our final fix. (so not relevant in our final code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed-in-upstream-repo This task has been implemented in the upstream repo G (Gas Optimization) resolved sponsor acknowledged
Projects
None yet
Development

No branches or pull requests

2 participants