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

Cache storage variables to local variables to save gas #75

Open
code423n4 opened this issue Jul 12, 2021 · 1 comment
Open

Cache storage variables to local variables to save gas #75

code423n4 opened this issue Jul 12, 2021 · 1 comment
Assignees
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Yes, this is a problem and we intend to fix it.

Comments

@code423n4
Copy link
Contributor

Handle

shw

Vulnerability details

Impact

In general, if a state variable is read more than once, caching its value to a local variable and reusing it will save gas since a storage read spends more gas than a memory write plus a memory read.

Proof of Concept

Referenced code:
TransactionManager.sol#L122-L125
TransactionManager.sol#L254-L260

Recommended Mitigation Steps

Rewrite #L122-L125 as follows:

uint256 balance = routerBalances[msg.sender][assetId];
require(balance >= amount, "removeLiquidity: INSUFFICIENT_FUNDS");

// Update router balances
routerBalances[msg.sender][assetId] = balance - amount;

Rewrite #L254-L260 as follows:

uint256 balance = routerBalances[invariantData.router][invariantData.receivingAssetId];
require(
    balance >= amount,
    "prepare: INSUFFICIENT_LIQUIDITY"
);

// Decrement the router liquidity
routerBalances[invariantData.router][invariantData.receivingAssetId] = balance - amount;
@sanchaymittal
Copy link
Collaborator

connext/monorepo#34

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 Yes, this is a problem and we intend to fix it.
Projects
None yet
Development

No branches or pull requests

3 participants