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 external call results can save gas #236

Open
code423n4 opened this issue Jan 27, 2022 · 1 comment
Open

Cache external call results can save gas #236

code423n4 opened this issue Jan 27, 2022 · 1 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")

Comments

@code423n4
Copy link
Contributor

Handle

WatchPug

Vulnerability details

Every call to an external contract costs a decent amount of gas. For optimization of gas usage, external call results should be cached if they are being used for more than one time.

For example:

factory.getPair(wavaxAddress, tokenAddress) and factory.getPair(tokenAddress, wavaxAddress) in LaunchEvent#createPair()

https://github.com/code-423n4/2022-01-trader-joe/blob/a1579f6453bc4bf9fb0db9c627beaa41135438ed/contracts/LaunchEvent.sol#L377-L435

function createPair() external isStopped(false) atPhase(Phase.PhaseThree) {
    // ...
    require(
        factory.getPair(wavaxAddress, tokenAddress) == address(0) ||
            IJoePair(
                IJoeFactory(factory).getPair(wavaxAddress, tokenAddress)
            ).totalSupply() ==
            0,
        "LaunchEvent: liquid pair already exists"
    );
    // ...
    pair = IJoePair(factory.getPair(tokenAddress, wavaxAddress));
    // ...
}

note: factory.getPair(a, b)factory.getPair(b, a) 相同, see
code at github
or
code at avascan

getPair[token0][token1] = pair;
getPair[token1][token0] = pair; // populate mapping in the reverse direction

IJoeFactory(factory).getPair(_token, wavax) in RocketJoeFactory#createRJLaunchEvent()

https://github.com/code-423n4/2022-01-trader-joe/blob/a1579f6453bc4bf9fb0db9c627beaa41135438ed/contracts/RocketJoeFactory.sol#L122-L128

require(
    IJoeFactory(factory).getPair(_token, wavax) == address(0) ||
        IJoePair(IJoeFactory(factory).getPair(_token, wavax))
            .totalSupply() ==
        0,
    "RJFactory: liquid pair already exists"
);

token.decimals() in LaunchEvent#createPair()

https://github.com/code-423n4/2022-01-trader-joe/blob/a1579f6453bc4bf9fb0db9c627beaa41135438ed/contracts/LaunchEvent.sol#L395-L405

if (
    floorPrice > (wavaxReserve * 10**token.decimals()) / tokenAllocated
) {
    tokenAllocated = (wavaxReserve * 10**token.decimals()) / floorPrice;
    // ...
}
@cryptofish7
Copy link
Collaborator

Fix: traderjoe-xyz/rocket-joe#144

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