Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhweintraub committed Dec 24, 2024
1 parent 1d88712 commit 457ff5a
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 11 deletions.
9 changes: 7 additions & 2 deletions contracts/gas-snapshots/ccip.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,13 @@ Router_recoverTokens:test_RecoverTokens() (gas: 52668)
Router_routeMessage:test_routeMessage_AutoExec() (gas: 38071)
Router_routeMessage:test_routeMessage_ExecutionEvent() (gas: 153593)
Router_routeMessage:test_routeMessage_ManualExec() (gas: 31120)
SiloedLockReleaseTokenPool_lockOrBurn:testLockOrBurn_NonSiloedFunds_Success(uint256) (runs: 257, μ: 54330, ~: 54330)
SiloedLockReleaseTokenPool_lockOrBurn:test_LockOrBurn_SiloedFunds_Success(uint256) (runs: 257, μ: 75057, ~: 75057)
SiloedLockReleaseTokenPool_lockOrBurn:testLockOrBurn_NonSiloedFunds_Success(uint256) (runs: 256, μ: 54330, ~: 54330)
SiloedLockReleaseTokenPool_lockOrBurn:test_LockOrBurn_SiloedFunds_Success(uint256) (runs: 256, μ: 75057, ~: 75057)
SiloedLockReleaseTokenPool_provideLiqudity:test_ProvideLiquidity_ChainNotSiloed_Success() (gas: 60201)
SiloedLockReleaseTokenPool_provideLiqudity:test_ProvideLiquidity_ChainSiloed_Success() (gas: 80899)
SiloedLockReleaseTokenPool_releaseOrMint:test_ReleaseOrMint_SiloedFunds_Success() (gas: 261601)
SiloedLockReleaseTokenPool_setRebalancer:test_setRebalancer_Success() (gas: 29682)
SiloedLockReleaseTokenPool_withdrawLiqudity:test_withdrawLiquidity_Success() (gas: 69484)
TokenAdminRegistry_acceptAdminRole:test_acceptAdminRole() (gas: 44236)
TokenAdminRegistry_addRegistryModule:test_addRegistryModule() (gas: 67093)
TokenAdminRegistry_getAllConfiguredTokens:test_getAllConfiguredTokens_outOfBounds() (gas: 11363)
Expand Down
10 changes: 5 additions & 5 deletions contracts/src/v0.8/ccip/pools/SiloedLockReleaseTokenPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ contract SiloedLockReleaseTokenPool is TokenPool, ITypeAndVersion {
emit ChainSiloeDesignationUpdated(adds[i], true);
}
}
/// @notice Gets LiquidityManager, can be address(0) if none is configured.
/// @notice Gets the rebalancer able to provide liquidity for a remote chain selector
/// @param remoteChainSelector The CCIP specific selector for the remote chain being interacted with.
/// @return The current liquidity manager.
/// @return The current liquidity manager, owner if the chain's funds are not siloed.

function getRebalancerByChain(
uint64 remoteChainSelector
) external view returns (address) {
return s_rebalancerByChain[remoteChainSelector];
if (s_siloedChainSelectors[remoteChainSelector]) return s_rebalancerByChain[remoteChainSelector];
else return owner();
}

/// @notice Sets the LiquidityManager address.
Expand Down Expand Up @@ -167,11 +168,10 @@ contract SiloedLockReleaseTokenPool is TokenPool, ITypeAndVersion {
/// @param amount The amount of liquidity to remove.
function withdrawLiquidity(uint64 remoteChainSelector, uint256 amount) external onlyOwner {
// If funds are siloed by chain, prevent more than has been locked from being removed from the token pool.
if (s_siloedChains.contains(remoteChainSelector)) {
if (s_siloedChainSelectors[remoteChainSelector]) {
s_lockedTokensByChainSelector[remoteChainSelector] -= amount;
}

if (i_token.balanceOf(address(this)) < amount) revert InsufficientLiquidity();
i_token.safeTransfer(msg.sender, amount);
emit LiquidityRemoved(remoteChainSelector, msg.sender, amount);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.24;

import {SiloedLockReleaseTokenPoolSetup} from "./SiloedLockReleaseTokenPoolSetup.t.sol";

import {SiloedLockReleaseTokenPool} from "../../../pools/SiloedLockReleaseTokenPool.sol";
import {TokenPool} from "../../../pools/TokenPool.sol";

contract SiloedLockReleaseTokenPool_provideLiqudity is SiloedLockReleaseTokenPoolSetup {
address public UNAUTHORIZED_ADDRESS = address(0xdeadbeef);

function setUp() public override {
super.setUp();

s_siloedLockReleaseTokenPool.setRebalancer(SILOED_CHAIN_SELECTOR, OWNER);
}

function test_ProvideLiquidity_ChainNotSiloed_Success() public {
uint256 amount = 1e24;

vm.expectEmit();
emit SiloedLockReleaseTokenPool.LiquidityAdded(DEST_CHAIN_SELECTOR, OWNER, amount);

s_siloedLockReleaseTokenPool.provideLiquidity(DEST_CHAIN_SELECTOR, amount);

assertEq(s_token.balanceOf(address(s_siloedLockReleaseTokenPool)), amount);

// Since the funds for the destination chain are not siloed,
// the locked token amount should not be increased
assertEq(s_siloedLockReleaseTokenPool.getLockedTokensByChain(DEST_CHAIN_SELECTOR), 0);
}

function test_ProvideLiquidity_ChainSiloed_Success() public {
uint256 amount = 1e24;

vm.expectEmit();
emit SiloedLockReleaseTokenPool.LiquidityAdded(SILOED_CHAIN_SELECTOR, OWNER, amount);

s_siloedLockReleaseTokenPool.provideLiquidity(SILOED_CHAIN_SELECTOR, amount);

assertEq(s_token.balanceOf(address(s_siloedLockReleaseTokenPool)), amount);

// Since the funds for the destination chain are not siloed,
// the locked token amount should not be increased
assertEq(s_siloedLockReleaseTokenPool.getLockedTokensByChain(SILOED_CHAIN_SELECTOR), amount);
}

// Reverts

function test_ProvideLiquidity_RevertWhen_UnauthorizedForSiloedChain() public {
vm.startPrank(UNAUTHORIZED_ADDRESS);

vm.expectRevert(abi.encodeWithSelector(TokenPool.Unauthorized.selector, UNAUTHORIZED_ADDRESS));

s_siloedLockReleaseTokenPool.provideLiquidity(SILOED_CHAIN_SELECTOR, 1);
}

function test_ProvideLiquidity_RevertWhen_UnauthorizedForNotSiloedChain() public {
vm.startPrank(UNAUTHORIZED_ADDRESS);

vm.expectRevert(abi.encodeWithSelector(TokenPool.Unauthorized.selector, UNAUTHORIZED_ADDRESS));

s_siloedLockReleaseTokenPool.provideLiquidity(DEST_CHAIN_SELECTOR, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {Pool} from "../../../libraries/Pool.sol";
import {SiloedLockReleaseTokenPoolSetup} from "./SiloedLockReleaseTokenPoolSetup.t.sol";

contract SiloedLockReleaseTokenPool_releaseOrMint is SiloedLockReleaseTokenPoolSetup {
function testFuzz_ReleaseOrMint_SiloedFunds_Success(
uint256 amount
) public {
amount = bound(amount, 1, _getOutboundRateLimiterConfig().capacity);
function test_ReleaseOrMint_SiloedFunds_Success() public {
uint256 amount = 10e18;

deal(address(s_token), address(s_siloedLockReleaseTokenPool), amount);
vm.startPrank(s_allowedOnRamp);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.24;

import {SiloedLockReleaseTokenPool} from "../../../pools/SiloedLockReleaseTokenPool.sol";
import {SiloedLockReleaseTokenPoolSetup} from "./SiloedLockReleaseTokenPoolSetup.t.sol";

contract SiloedLockReleaseTokenPool_setRebalancer is SiloedLockReleaseTokenPoolSetup {
address public REBALANCER_ADDRESS = address(0xdeadbeef);

function test_setRebalancer_Success() public {
vm.expectEmit();
emit SiloedLockReleaseTokenPool.RebalancerSet(SILOED_CHAIN_SELECTOR, REBALANCER_ADDRESS, OWNER);

s_siloedLockReleaseTokenPool.setRebalancer(SILOED_CHAIN_SELECTOR, REBALANCER_ADDRESS);

assertEq(s_siloedLockReleaseTokenPool.getRebalancerByChain(SILOED_CHAIN_SELECTOR), REBALANCER_ADDRESS);
assertEq(s_siloedLockReleaseTokenPool.getRebalancerByChain(DEST_CHAIN_SELECTOR), OWNER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.24;

import {SiloedLockReleaseTokenPoolSetup} from "./SiloedLockReleaseTokenPoolSetup.t.sol";

import {SiloedLockReleaseTokenPool} from "../../../pools/SiloedLockReleaseTokenPool.sol";

contract SiloedLockReleaseTokenPool_withdrawLiqudity is SiloedLockReleaseTokenPoolSetup {
address public UNAUTHORIZED_ADDRESS = address(0xdeadbeef);

function setUp() public override {
super.setUp();

s_siloedLockReleaseTokenPool.setRebalancer(SILOED_CHAIN_SELECTOR, OWNER);
}

function test_withdrawLiquidity_Success() public {
uint256 amount = 1e24;

uint256 balanceBefore = s_token.balanceOf(OWNER);

// Provide the Liquidity first
s_siloedLockReleaseTokenPool.provideLiquidity(SILOED_CHAIN_SELECTOR, amount);

vm.expectEmit();
emit SiloedLockReleaseTokenPool.LiquidityRemoved(SILOED_CHAIN_SELECTOR, OWNER, amount);

// Remove the Liquidity
s_siloedLockReleaseTokenPool.withdrawLiquidity(SILOED_CHAIN_SELECTOR, amount);

assertEq(s_token.balanceOf(OWNER), balanceBefore);
assertEq(s_token.balanceOf(address(s_siloedLockReleaseTokenPool)), 0);
}

// Reverts

function test_withdrawLiquidity_RevertWhen_NotEnoughLiquidity() public {
uint256 amount = 1e24;

s_siloedLockReleaseTokenPool.provideLiquidity(SILOED_CHAIN_SELECTOR, amount);

// Call should revert due to underflow error due to trying to burn more tokens than are locked via CCIP.
vm.expectRevert(abi.encodeWithSignature("Panic(uint256)", 0x11));

s_siloedLockReleaseTokenPool.withdrawLiquidity(SILOED_CHAIN_SELECTOR, amount + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ contract SiloedLockReleaseTokenPoolSetup is BaseTest {
s_token, DEFAULT_TOKEN_DECIMALS, new address[](0), address(s_mockRMNRemote), address(s_sourceRouter)
);

s_token.approve(address(s_siloedLockReleaseTokenPool), type(uint256).max);

bytes[] memory remotePoolAddresses = new bytes[](2);
remotePoolAddresses[0] = abi.encode(s_destPoolAddress);
remotePoolAddresses[1] = abi.encode(s_siloedDestPoolAddress);
Expand Down

0 comments on commit 457ff5a

Please sign in to comment.