11# SaturationAndGeometricTWAPState
2- [ Git Source] ( https://github.com/Ammalgam-Protocol/core-v1/blob/82dff11576b9df76b675736dba889653cf737de9 /contracts/SaturationAndGeometricTWAPState.sol )
2+ [ Git Source] ( https://github.com/Ammalgam-Protocol/core-v1/blob/50c2020998f5d2b0b343c9061ad14779312d3011 /contracts/SaturationAndGeometricTWAPState.sol )
33
44** Inherits:**
5- [ ISaturationAndGeometricTWAPState] ( /docs/developer-guide/contracts/interfaces/ISaturationAndGeometricTWAPState.sol/interface.ISaturationAndGeometricTWAPState.md ) , Ownable
5+ Initializable, [ ISaturationAndGeometricTWAPState] ( /docs/developer-guide/contracts/interfaces/ISaturationAndGeometricTWAPState.sol/interface.ISaturationAndGeometricTWAPState.md ) , OwnableUpgradeable
66
77
88## State Variables
99### midTermIntervalConfig
1010
1111``` solidity
12- uint24 public immutable midTermIntervalConfig;
12+ uint24 public midTermIntervalConfig;
1313```
1414
1515
1616### longTermIntervalConfig
1717
1818``` solidity
19- uint24 public immutable longTermIntervalConfig;
19+ uint24 public longTermIntervalConfig;
2020```
2121
2222
@@ -41,6 +41,13 @@ mapping(address => mapping(address => uint256)) maxNewPositionSaturationInMAG2;
4141```
4242
4343
44+ ### lastUsedActiveLiquidityInLAssets
45+
46+ ``` solidity
47+ mapping(address => mapping(address => uint256)) lastUsedActiveLiquidityInLAssets;
48+ ```
49+
50+
4451### isPairInitialized
4552
4653``` solidity
@@ -53,7 +60,14 @@ mapping(address => bool) internal isPairInitialized;
5360
5461
5562``` solidity
56- constructor(uint24 _midTermIntervalConfig, uint24 _longTermIntervalConfig) Ownable(msg.sender);
63+ constructor();
64+ ```
65+
66+ ### initialize
67+
68+
69+ ``` solidity
70+ function initialize(uint24 _midTermIntervalConfig, uint24 _longTermIntervalConfig, address _owner) public initializer;
5771```
5872
5973### isInitialized
@@ -78,9 +92,7 @@ initializes the sat and TWAP struct
7892
7993
8094``` solidity
81- function init(
82- int16 firstTick
83- ) external;
95+ function init(uint256 reserveXAssets, uint256 reserveYAssets) external;
8496```
8597
8698### setNewPositionSaturation
@@ -90,28 +102,18 @@ function init(
90102function setNewPositionSaturation(address pair, uint256 maxDesiredSaturationMag2) external;
91103```
92104
93- ### getNewPositionSaturation
94-
95-
96- ``` solidity
97- function getNewPositionSaturation(
98- address pair,
99- address account
100- ) internal view returns (uint256 maxDesiredSaturationInMAG2);
101- ```
102-
103105### getTree
104106
105107
106108``` solidity
107109function getTree(address pairAddress, bool netDebtX) private view returns (Saturation.Tree storage);
108110```
109111
110- ### getLeafDetails
112+ ### getTreeLeafDetails
111113
112114
113115``` solidity
114- function getLeafDetails (
116+ function getTreeLeafDetails (
115117 address pairAddress,
116118 bool netDebtX,
117119 uint256 leafIndex
@@ -121,17 +123,12 @@ function getLeafDetails(
121123 returns (
122124 Saturation.SaturationPair memory saturation,
123125 uint256 currentPenaltyInBorrowLSharesPerSatInQ72,
126+ uint128 totalSatInLAssets,
127+ uint16 highestSetLeaf,
124128 uint16[] memory tranches
125129 );
126130```
127131
128- ### getTreeDetails
129-
130-
131- ``` solidity
132- function getTreeDetails(address pairAddress, bool netDebtX) external view returns (uint16, uint128);
133- ```
134-
135132### getTrancheDetails
136133
137134
@@ -156,28 +153,62 @@ function getAccount(
156153
157154### update
158155
159- update the borrow position of an account and potentially check (and revert) if the resulting sat is too high
156+ update the borrow position of an account and potentially check (and revert) if the
157+ resulting sat is too high
160158
161159* run accruePenalties before running this function*
162160
163161
164162``` solidity
165- function update(Validation.InputParams memory inputParams, address account) external virtual;
163+ function update(
164+ Validation.InputParams memory inputParams,
165+ address account,
166+ bool skipMinOrMaxTickCheck
167+ ) public virtual isInitialized;
166168```
167169** Parameters**
168170
169171| Name| Type| Description|
170172| ----| ----| -----------|
171173| ` inputParams ` | ` Validation.InputParams ` | contains the position and pair params, like account borrows/deposits, current price and active liquidity|
172174| ` account ` | ` address ` | for which is position is being updated|
175+ | ` skipMinOrMaxTickCheck ` | ` bool ` ||
176+
177+
178+ ### scaleDesiredSaturation
173179
180+ Scales the desired saturation threshold based on changes in Active Liquidity Assets (ALA).
174181
175- ### _ update
182+ * When liquidity is burned from the pool, ALA decreases. Without scaling, this would cause
183+ existing positions to appear more saturated (since saturation = borrows / ALA), potentially
184+ triggering unwarranted liquidation premiums. This function scales the desired saturation
185+ proportionally to ALA changes to maintain the position's relative health.
186+ The scaling formula: scaled = lastUsedALA * desiredSat / currentALA*
176187
177188
178189``` solidity
179- function _update(Validation.InputParams memory inputParams, address account) internal isInitialized;
190+ function scaleDesiredSaturation(
191+ address pair,
192+ address account,
193+ uint256 currentALA,
194+ bool capAtPenaltyStart
195+ ) internal view returns (uint256 desiredSaturationInMAG2);
180196```
197+ ** Parameters**
198+
199+ | Name| Type| Description|
200+ | ----| ----| -----------|
201+ | ` pair ` | ` address ` | The address of the pair contract.|
202+ | ` account ` | ` address ` | The account whose saturation threshold is being scaled.|
203+ | ` currentALA ` | ` uint256 ` | The current active liquidity assets in the pool.|
204+ | ` capAtPenaltyStart ` | ` bool ` | If ` true ` , caps the scaled value at START_SATURATION_PENALTY_RATIO_IN_MAG2. Used in _ update() to prevent excessive. Set to ` false ` in calcSatChangeRatioBips() for accurate premium calculations.|
205+
206+ ** Returns**
207+
208+ | Name| Type| Description|
209+ | ----| ----| -----------|
210+ | ` desiredSaturationInMAG2 ` | ` uint256 ` | The scaled desired saturation threshold.|
211+
181212
182213### accruePenalties
183214
@@ -218,7 +249,7 @@ function calcSatChangeRatioBips(
218249 uint256 liqSqrtPriceInYInQ72,
219250 address pairAddress,
220251 address account
221- ) external view virtual isInitialized returns (uint256 ratioNetXBips, uint256 ratioNetYBips );
252+ ) external view virtual isInitialized returns (uint256 ratioBips );
222253```
223254** Parameters**
224255
@@ -234,8 +265,7 @@ function calcSatChangeRatioBips(
234265
235266| Name| Type| Description|
236267| ----| ----| -----------|
237- | ` ratioNetXBips ` | ` uint256 ` | The ratio representing the change in netX saturation for account.|
238- | ` ratioNetYBips ` | ` uint256 ` | The ratio representing the change in netY saturation for account.|
268+ | ` ratioBips ` | ` uint256 ` | The ratio representing the change saturation for account.|
239269
240270
241271### getObservations
@@ -278,7 +308,7 @@ provided block timestamp is less than or equal to the last recorded timestamp.*
278308
279309
280310``` solidity
281- function recordObservation(int16 newTick, uint32 timeElapsed) external isInitialized returns (bool);
311+ function recordObservation(int16 newTick, uint32 timeElapsed) public virtual isInitialized returns (bool);
282312```
283313** Parameters**
284314
@@ -305,7 +335,8 @@ long-term tick, mid-term tick, and current tick.*
305335``` solidity
306336function getTickRange(
307337 address pair,
308- int16 currentTick,
338+ uint256 reserveXAssets,
339+ uint256 reserveYAssets,
309340 bool includeLongTermTick
310341) external view virtual returns (int16, int16);
311342```
@@ -314,7 +345,8 @@ function getTickRange(
314345| Name| Type| Description|
315346| ----| ----| -----------|
316347| ` pair ` | ` address ` | The address of the pair for which the tick range is being calculated.|
317- | ` currentTick ` | ` int16 ` | The current (most recent) tick based on the current reserves.|
348+ | ` reserveXAssets ` | ` uint256 ` | The current pair reserves of asset X.|
349+ | ` reserveYAssets ` | ` uint256 ` | The current pair reserves of asset Y.|
318350| ` includeLongTermTick ` | ` bool ` | Boolean value indicating whether to include the long-term tick in the range.|
319351
320352** Returns**
@@ -325,17 +357,6 @@ function getTickRange(
325357| ` <none> ` | ` int16 ` | maxTick The maximum tick value among the three observed ticks.|
326358
327359
328- ### _ getTickRange
329-
330-
331- ``` solidity
332- function _getTickRange(
333- address pair,
334- int16 currentTick,
335- bool includeLongTermTick
336- ) internal view returns (int16, int16);
337- ```
338-
339360### getLendingStateTickAndCheckpoint
340361
341362Gets the tick value representing the TWAP since the last
@@ -390,30 +411,6 @@ function getObservedMidTermTick(
390411| ` <none> ` | ` int16 ` | midTermTick The mid-term tick value.|
391412
392413
393- ### boundTick
394-
395- * The function ensures that ` newTick ` stays within the bounds
396- determined by ` lastTick ` and a dynamically calculated factor.*
397-
398-
399- ``` solidity
400- function boundTick(
401- int16 newTick
402- ) external view returns (int16);
403- ```
404- ** Parameters**
405-
406- | Name| Type| Description|
407- | ----| ----| -----------|
408- | ` newTick ` | ` int16 ` | The proposed new tick value to be adjusted within valid bounds.|
409-
410- ** Returns**
411-
412- | Name| Type| Description|
413- | ----| ----| -----------|
414- | ` <none> ` | ` int16 ` | The adjusted tick value constrained within the allowable range.|
415-
416-
417414### getLendingStateTick
418415
419416Gets the tick value representing the TWAP since the last lending update.
@@ -442,15 +439,3 @@ function getLendingStateTick(
442439| ` maxSatInWads ` | ` uint256 ` | The maximum saturation in WADs.|
443440
444441
445- ### liquidationCheckHardPremiums
446-
447-
448- ``` solidity
449- function liquidationCheckHardPremiums(
450- Validation.InputParams memory inputParams,
451- address borrower,
452- Liquidation.HardLiquidationParams memory hardLiquidationParams,
453- uint256 actualRepaidLiquidityAssets
454- ) external view returns (bool badDebt);
455- ```
456-
0 commit comments