Skip to content

Commit

Permalink
test: deposit limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlagonia committed Aug 21, 2024
1 parent 15e78c8 commit dc7488e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/L1YearnEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ contract L1YearnEscrow is L1Escrow {
return $.minimumBuffer;
}

function depositLimit() public view returns (uint256) {
VaultStorage storage $ = _getVaultStorage();
return $.depositLimit;
}

// ****************************
// * Initializer *
// ****************************
Expand Down Expand Up @@ -129,10 +134,12 @@ contract L1YearnEscrow is L1Escrow {
function _receiveTokens(
uint256 amount
) internal virtual override whenNotPaused {
VaultStorage storage $ = _getVaultStorage();
require($.deposited + amount <= $.depositLimit, "deposit limit");

IERC20 originToken = originTokenAddress();
originToken.safeTransferFrom(msg.sender, address(this), amount);

VaultStorage storage $ = _getVaultStorage();
unchecked {
$.deposited += amount;
}
Expand Down Expand Up @@ -290,7 +297,7 @@ contract L1YearnEscrow is L1Escrow {

/**
* @dev Update the minimum buffer to keep in the escrow.
* uint128 max would be the max buffer.
* uint128 max would be the max buffer.
* @param _minimumBuffer The new minimum buffer to enforce.
*/
function updateMinimumBuffer(
Expand Down
87 changes: 86 additions & 1 deletion test/L1Escrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,92 @@ contract EscrowTest is Setup {
assertEq(vault.balanceOf(address(mockEscrow)), 0);
}

function test_bridgeAsset_maxDepositLimit(uint256 _amount) public {
function test_bridgeAsset_escrowDepositLimit(uint256 _amount) public {
_amount = bound(_amount, minFuzzAmount, maxFuzzAmount);
address counterPart = l1Deployer.getL2EscrowAddress(
l2RollupID,
address(asset)
);
mockEscrow = deployMockL1Escrow();

vm.expectRevert();
mockEscrow.updateDepositLimit(0);

vm.prank(governator);
mockEscrow.updateDepositLimit(0);

// Simulate a bridge txn
airdrop(asset, user, _amount);

vm.prank(user);
asset.approve(address(mockEscrow), _amount);

vm.expectRevert("deposit limit");
vm.prank(user);
mockEscrow.bridgeToken(user, _amount, true);

vm.prank(governator);
mockEscrow.updateDepositLimit(_amount);

bytes memory data = abi.encode(user, _amount);
uint256 depositCount = polygonZkEVMBridge.depositCount();
vm.expectEmit(true, true, true, true, address(polygonZkEVMBridge));
emit BridgeEvent(
1,
l1RollupID,
address(mockEscrow),
l2RollupID,
counterPart,
0,
data,
uint32(depositCount)
);
vm.prank(user);
mockEscrow.bridgeToken(user, _amount, true);

assertEq(vault.totalAssets(), _amount);
assertEq(mockEscrow.deposited(), _amount);
assertEq(asset.balanceOf(user), 0);
assertEq(asset.balanceOf(address(mockEscrow)), 0);
assertEq(vault.balanceOf(address(mockEscrow)), _amount);

airdrop(asset, user, _amount);

vm.prank(user);
asset.approve(address(mockEscrow), _amount);

vm.expectRevert("deposit limit");
vm.prank(user);
mockEscrow.bridgeToken(user, _amount, true);

vm.prank(governator);
mockEscrow.updateDepositLimit(_amount * 2);

vm.prank(user);
mockEscrow.bridgeToken(user, _amount, true);

assertEq(vault.totalAssets(), _amount * 2);
assertEq(mockEscrow.deposited(), _amount * 2);
assertEq(asset.balanceOf(user), 0);
assertEq(asset.balanceOf(address(mockEscrow)), 0);
assertEq(vault.balanceOf(address(mockEscrow)), _amount * 2);

// Withdraw half
uint256 toWithdraw = _amount + 10;

data = abi.encode(user, toWithdraw);

vm.prank(address(polygonZkEVMBridge));
mockEscrow.onMessageReceived(counterPart, l2RollupID, data);

assertEq(vault.totalAssets(), _amount - 10);
assertEq(mockEscrow.deposited(), _amount * 2 - toWithdraw);
assertEq(asset.balanceOf(user), toWithdraw);
assertEq(asset.balanceOf(address(mockEscrow)), 0);
assertEq(vault.balanceOf(address(mockEscrow)), _amount - 10);
}

function test_bridgeAsset_vaultDepositLimit(uint256 _amount) public {
_amount = bound(_amount, minFuzzAmount, maxFuzzAmount);
address counterPart = l1Deployer.getL2EscrowAddress(
l2RollupID,
Expand Down

0 comments on commit dc7488e

Please sign in to comment.