# Strategy contract walk-through ## Principle Minimize the number of sload/sstore needed. Since positions need to be iterated (to some extent), this minimisation is the easiest gas saving strategy. ## Definitions ### `MIN_POSITIONS_RIGHT` The minimum number of position which should be on the right of the current tick. If there are less than this amount of position, then the strategy needs to mint new ones. ex: 3 ### `MIN_NUMBER_TO_ADD` The number of positions to add to the right of the current tick, if there are less than MIN_POSITIONS_RIGHT ex:5 -> "If there are less than 3 positions to the right, mint to have at least 5 positions" ### `highestTick` and `lowestTick` The current (ie before running the strategy) highest and lowest lower tick. Positions are refered by their lower bounds (by convention). Example: 3 positions, covering the ticks 100 to 200 (position A), 200 to 300 (B) and 300 to 400 ('C), will have a lowestTick of 100 (A.lowerTick) and an highestTick of 300 (C.lowerTick) ## Code markdowns The gist of the logic is just 6 conditional part, marked down in the contract: ![](https://i.imgur.com/Q1HhoJE.png) ### A current tick is under the lowestTick minus MIN_NUMBER_TO_ADD -> we mint MIN_NUMBER_TO_ADD without reading any positions (and update the lowestTick). ![](https://i.imgur.com/MVEx3tZ.png) (in light blue, existing positions; in red, the minting without check) ### B current tick is between lowestTick minus MIN_NUMBER_TO_ADD and lowestTick minus MIN_POSITIONS_RIGHT -> we need to mint to have MIN_NUMBER_TO_ADD positions: - we can mint without checking until the lowestTick (since these are empty) - lowestTick cannot be empty -> we don't check it (TODO) - we check and mint if empty, from lowestTick to currentTick + MIN_NUMBER_TO_ADD ![](https://i.imgur.com/SVpBrgt.png) (in red, minting, in orange, every sload operations) ### C current tick is less than lowestTick minus MIN_POSITIONS_RIGHT and before the lowest tick -> we first need to check the next MIN_POSITIONS_RIGHT, starting at the lowestTick (since all positions are empty before, by definition). If one is empty, we know we'll have to mint (before lowestTick) or check if empty & mint if needed (after lowestTick). ![](https://i.imgur.com/sqPOMUN.png) ### D current tick is less than highestTick minus MIN_POSITIONS_RIGHT -> no choice, we need to check each positions between current tick and current tick + MIN_POSITIONS_RIGHT. If there are any empty position in that range, then we need to start minting at that empty one, then check and mint, up until currentTick + MIN_NUMBER_TO_ADD ![](https://i.imgur.com/OCU5ZG3.png) ### E current tick is between highestTick minus MIN_POSITIONS_RIGHT and highest tick -> we'll have to mint to have MIN_NUMBER_TO_ADD (highestTick will increase). - we first check and mint if needed before reaching highestTick - then we just mint, without reading, from highestTick to currentTick+MIN_NUMBER_TO_ADD (these positions are empty by definition) ![](https://i.imgur.com/nVkW8WC.png) ### F current tick is above the highest tick -> we don't read anything and mint MIN_NUMBER_TO_ADD positions. ![](https://i.imgur.com/PUiXrGh.png)