Skip to content

Commit

Permalink
LM-117-enforce-min-liquidity-on-chain-during-transfers (#1006)
Browse files Browse the repository at this point in the history
## Motivation
We don't want the DON transferring funds out of token pools beyond the
configured minimum balance

## Solution
Enforce the min liquidity config
  • Loading branch information
RyanRHall authored Jun 24, 2024
1 parent 20169bd commit 1652778
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
18 changes: 9 additions & 9 deletions contracts/gas-snapshots/liquiditymanager.gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
LiquidityManager__report:test_EmptyReportReverts() (gas: 11159)
LiquidityManager_addLiquidity:test_addLiquiditySuccess() (gas: 279154)
LiquidityManager_rebalanceLiquidity:test_InsufficientLiquidityReverts() (gas: 19585)
LiquidityManager_rebalanceLiquidity:test_InvalidRemoteChainReverts() (gas: 190304)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPoolsSuccess() (gas: 8831482)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPoolsSuccess_AlreadyFinalized() (gas: 8742448)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPools_MultiStageFinalization() (gas: 8737656)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPools_NativeRewrap() (gas: 8665799)
LiquidityManager_rebalanceLiquidity:test_rebalanceLiquiditySuccess() (gas: 379308)
LiquidityManager_rebalanceLiquidity:test_InsufficientLiquidityReverts() (gas: 204896)
LiquidityManager_rebalanceLiquidity:test_InvalidRemoteChainReverts() (gas: 192069)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPoolsSuccess() (gas: 8785965)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPoolsSuccess_AlreadyFinalized() (gas: 8719591)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPools_MultiStageFinalization() (gas: 8714799)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPools_NativeRewrap() (gas: 8642942)
LiquidityManager_rebalanceLiquidity:test_rebalanceLiquiditySuccess() (gas: 381075)
LiquidityManager_receive:test_receive_success() (gas: 21182)
LiquidityManager_removeLiquidity:test_InsufficientLiquidityReverts() (gas: 184481)
LiquidityManager_removeLiquidity:test_InsufficientLiquidityReverts() (gas: 184602)
LiquidityManager_removeLiquidity:test_OnlyOwnerReverts() (gas: 10967)
LiquidityManager_removeLiquidity:test_removeLiquiditySuccess() (gas: 236047)
LiquidityManager_removeLiquidity:test_removeLiquiditySuccess() (gas: 236075)
LiquidityManager_setCrossChainRebalancer:test_OnlyOwnerReverts() (gas: 17027)
LiquidityManager_setCrossChainRebalancer:test_ZeroAddressReverts() (gas: 21630)
LiquidityManager_setCrossChainRebalancer:test_ZeroChainSelectorReverts() (gas: 13105)
Expand Down
11 changes: 6 additions & 5 deletions contracts/src/v0.8/liquiditymanager/LiquidityManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract LiquidityManager is ILiquidityManager, OCR3Base {
error ZeroAddress();
error InvalidRemoteChain(uint64 chainSelector);
error ZeroChainSelector();
error InsufficientLiquidity(uint256 requested, uint256 available);
error InsufficientLiquidity(uint256 requested, uint256 available, uint256 reserve);
error EmptyReport();
error TransferFailed();

Expand Down Expand Up @@ -213,9 +213,9 @@ contract LiquidityManager is ILiquidityManager, OCR3Base {
/// @notice Removes liquidity from the system and sends it to the caller, so the owner.
/// @dev Only the owner can call this function.
function removeLiquidity(uint256 amount) external onlyOwner {
uint256 currentBalance = i_localToken.balanceOf(address(s_localLiquidityContainer));
uint256 currentBalance = getLiquidity();
if (currentBalance < amount) {
revert InsufficientLiquidity(amount, currentBalance);
revert InsufficientLiquidity(amount, currentBalance, 0);
}

s_localLiquidityContainer.withdrawLiquidity(amount);
Expand Down Expand Up @@ -263,8 +263,9 @@ contract LiquidityManager is ILiquidityManager, OCR3Base {
bytes memory bridgeSpecificPayload
) internal {
uint256 currentBalance = getLiquidity();
if (currentBalance < tokenAmount) {
revert InsufficientLiquidity(tokenAmount, currentBalance);
uint256 minBalance = s_minimumLiquidity;
if (currentBalance < minBalance || currentBalance - minBalance < tokenAmount) {
revert InsufficientLiquidity(tokenAmount, currentBalance, minBalance);
}

CrossChainRebalancer memory remoteLiqManager = s_crossChainRebalancer[chainSelector];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {LiquidityManagerHelper} from "./helpers/LiquidityManagerHelper.sol";

import {IERC20} from "../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol";

// FOUNDRY_PROFILE=liquiditymanager forge test --match-path src/v0.8/liquiditymanager/test/LiquidityManager.t.sol

contract LiquidityManagerSetup is LiquidityManagerBaseTest {
event FinalizationStepCompleted(
uint64 indexed ocrSeqNum,
Expand Down Expand Up @@ -116,7 +118,7 @@ contract LiquidityManager_removeLiquidity is LiquidityManagerSetup {

deal(address(s_l1Token), address(s_lockReleaseTokenPool), balance);

vm.expectRevert(abi.encodeWithSelector(LiquidityManager.InsufficientLiquidity.selector, requested, balance));
vm.expectRevert(abi.encodeWithSelector(LiquidityManager.InsufficientLiquidity.selector, requested, balance, 0));

s_liquidityManager.removeLiquidity(requested);
}
Expand Down Expand Up @@ -639,7 +641,9 @@ contract LiquidityManager_rebalanceLiquidity is LiquidityManagerSetup {
// Reverts

function test_InsufficientLiquidityReverts() external {
vm.expectRevert(abi.encodeWithSelector(LiquidityManager.InsufficientLiquidity.selector, AMOUNT, 0));
s_liquidityManager.setMinimumLiquidity(3);
deal(address(s_l1Token), address(s_lockReleaseTokenPool), AMOUNT);
vm.expectRevert(abi.encodeWithSelector(LiquidityManager.InsufficientLiquidity.selector, AMOUNT, AMOUNT, 3));

s_liquidityManager.rebalanceLiquidity(0, AMOUNT, 0, bytes(""));
}
Expand Down
Loading

0 comments on commit 1652778

Please sign in to comment.