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

Tcr 11 gas optimisations #157

Merged
merged 12 commits into from Sep 12, 2021
Merged

Tcr 11 gas optimisations #157

merged 12 commits into from Sep 12, 2021

Conversation

CalabashSquash
Copy link
Contributor

@CalabashSquash CalabashSquash commented Sep 11, 2021

Motivation

TCR-11 In the Sigma Prime audit report listed numerous gas saving changes that should be made.

Changes

LeveragedPool

  • Remove unnecessary uint256( cast
  • Add function getUpkeepInformation() external view override returns (int256 _latestPrice, uint256 _lastPriceTimestamp, uint256 _updateInterval)
    • This is used by PoolKeeper to avoid three separate external calls
  • Make getOraclePrice external
    PoolCommitter
  • Cache LeveragedPool.tokens locally, to save external calls on these.
    • Since these don't change for a given pool, it is safe to set them when setQuoteAndPool is called.
  • In executeAllCommitments, nextEarliestCommitUnexecuted was always being set to equal i at the start of each iteration. May as well just remove i.
  • There were unnecessary SLOAD of latestCommitUnexecuted each loop (it is checked inside the for statement, and used inside the block). This was removed and just SLOAD'ed once at the start of the function.
  • Make getCommit external
    PoolFactory
  • Make the addresses for pairTokenBase and poolBase immutable.
    • These are read from in deployment, but never change.
  • Move pool.initialize(initialization); above poolCommitter.setQuoteAndPool so that LeveragedPool.tokens is initialised before setQuoteAndPool, allowing this function to save LeveragedPool.tokens.
    • See PoolCommitter
      PoolKeeper
  • Use LeveragedPool.getOraclePrice in newPool instead of 1) fetching oracleWrapper then 2) fetching price from oracleWrapper
  • Use getUpkeepInformation in performUpkeepSinglePool.
    • See LeveragedPool. But this is to save from 3 external calls.
      PoolSwapLibrary
  • Make getBalancesAfterFees and calculatePriceChange external.
  • In getLossMultiplier, a Ternary can be used to save from confusing logic and extra mathematical operations.
  • In calculatePriceChange, A line can be removed which sets totalFeeAmount right before setting it again two lines below.
    hardhat.config.ts
  • Add optimizer

@CalabashSquash CalabashSquash marked this pull request as draft September 11, 2021 04:20
@CalabashSquash CalabashSquash marked this pull request as ready for review September 11, 2021 07:00
@CalabashSquash CalabashSquash merged commit 76a91e4 into develop Sep 12, 2021
@CalabashSquash CalabashSquash deleted the tcr-11-gas-optimisations branch September 12, 2021 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants