Skip to content

Commit

Permalink
chore: add startNewCycle function
Browse files Browse the repository at this point in the history
  • Loading branch information
bhargavaparoksham committed Jan 20, 2025
1 parent 162ae8c commit 1033581
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/interfaces/IAssetPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ interface IAssetPool {
*/
function rebalancePool(address lp, uint256 rebalancePrice, uint256 amount, bool isDeposit) external;

/**
* @notice When there is nothing to rebalance, start the new cycle
*/
function startNewCycle() external;

// --------------------------------------------------------------------------------
// GOVERNANCE ACTIONS
// --------------------------------------------------------------------------------
Expand Down
21 changes: 11 additions & 10 deletions src/protocol/AssetPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,25 @@ contract AssetPool is IAssetPool, Ownable, Pausable {

// If all LPs have rebalanced, start next cycle
if (rebalancedLPs == lpRegistry.getLPCount(address(this))) {
uint256 totalLiquidity = lpRegistry.getTotalLPLiquidity(address(this));
uint256 assetBalance = assetToken.balanceOf(address(this));
uint256 reserveBalanceInAssetToken = assetToken.reserveBalanceOf(address(this));
assetToken.burn(address(this), assetBalance, reserveBalanceInAssetToken);
cycleRebalancePrice[cycleIndex] = cycleWeightedSum[cycleIndex] / totalLiquidity;

_startNewCycle();
}
}

function updateCycle() external {
/**
* @notice If there is nothing to rebalance, start the next cycle.
*/
function startNewCycle() external {
if (cycleState != CycleState.REBALANCING) revert InvalidCycleState();
if (cycleTotalDepositRequests[cycleIndex] > 0) revert InvalidCycleRequest();
if (cycleTotalRedemptionRequests[cycleIndex] > 0) revert InvalidCycleRequest();

cycleIndex++;
cycleState = CycleState.ACTIVE;
nextRebalanceStartDate = block.timestamp + cycleTime;
nextRebalanceEndDate = nextRebalanceStartDate + rebalanceTime;
_startNewCycle();
}

// --------------------------------------------------------------------------------
Expand Down Expand Up @@ -473,11 +479,6 @@ contract AssetPool is IAssetPool, Ownable, Pausable {
* @notice Starts a new cycle after all LPs have rebalanced.
*/
function _startNewCycle() internal {
uint256 totalLiquidity = lpRegistry.getTotalLPLiquidity(address(this));
uint256 assetBalance = assetToken.balanceOf(address(this));
uint256 reserveBalanceInAssetToken = assetToken.reserveBalanceOf(address(this));
assetToken.burn(address(this), assetBalance, reserveBalanceInAssetToken);
cycleRebalancePrice[cycleIndex] = cycleWeightedSum[cycleIndex] / totalLiquidity;
cycleIndex++;
cycleState = CycleState.ACTIVE;
rebalancedLPs = 0;
Expand Down

0 comments on commit 1033581

Please sign in to comment.