Skip to content

Commit

Permalink
test for convert() flow
Browse files Browse the repository at this point in the history
  • Loading branch information
omnifient committed Aug 3, 2023
1 parent 13ed589 commit ca9f020
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 11 deletions.
6 changes: 6 additions & 0 deletions test/Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ contract Base is Test {
uint32 depositCount
);

// copy of NativeConverterImpl.Convert
event Convert(address indexed from, address indexed to, uint256 amount);

// copy of L1EscrowImpl.Deposit
event Deposit(address indexed from, address indexed to, uint256 amount);

// copy of NativeConverterImpl.Migrate
event Migrate(uint256 amount);

// copy of ZkMinterBurner.Withdraw
event Withdraw(address indexed from, address indexed to, uint256 amount);

Expand Down
11 changes: 0 additions & 11 deletions test/Convert.t.sol

This file was deleted.

118 changes: 118 additions & 0 deletions test/ConvertFlows.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;

import {Base} from "./Base.sol";

contract Convert is Base {
/// @notice Alice converts 1000 L2_WUSDC to L2_USDC for herself.
function testConvertsWrappedUsdcToNativeUsdc() public {
vm.selectFork(_l2Fork);
vm.startPrank(_alice);

// setup
uint256 wrappedBalance1 = _erc20L2Wusdc.balanceOf(_alice);

uint256 amount = _toUSDC(1000);
_erc20L2Wusdc.approve(address(_nativeConverter), amount);

// check that our convert event is emitted
vm.expectEmit(address(_nativeConverter));
emit Convert(_alice, _alice, amount);

// call convert
_nativeConverter.convert(_alice, amount);

// alice's L2_WUSDC balance decreased
uint256 wrappedBalance2 = _erc20L2Wusdc.balanceOf(_alice);
assertEq(wrappedBalance1 - wrappedBalance2, amount);

// alice's L2_USDC balance increased
assertEq(_erc20L2Usdc.balanceOf(_alice), amount);

// converter's L2_BWUSDC balance increased
assertEq(_erc20L2Wusdc.balanceOf(address(_nativeConverter)), amount);

_assertUsdcSupplyAndBalancesMatch();
}

/// @notice Alice converts 1000 L2_WUSDC to L2_USDC for Bob.
function testConvertsWrappedUsdcToNativeUsdcForAnotherAddress() public {
vm.selectFork(_l2Fork);
vm.startPrank(_alice);

// setup
uint256 wrappedBalance1 = _erc20L2Wusdc.balanceOf(_alice);

uint256 amount = _toUSDC(1000);
_erc20L2Wusdc.approve(address(_nativeConverter), amount);

// check that our convert event is emitted
vm.expectEmit(address(_nativeConverter));
emit Convert(_alice, _bob, amount);

// call convert
_nativeConverter.convert(_bob, amount);

// alice's L2_WUSDC balance decreased
uint256 wrappedBalance2 = _erc20L2Wusdc.balanceOf(_alice);
assertEq(wrappedBalance1 - wrappedBalance2, amount);

// bob's L2_USDC balance increased
assertEq(_erc20L2Usdc.balanceOf(_bob), amount);

// converter's L2_BWUSDC balance increased
assertEq(_erc20L2Wusdc.balanceOf(address(_nativeConverter)), amount);

_assertUsdcSupplyAndBalancesMatch();
}

/// @notice Alice approves a 500 L2_WUSDC spend but tries to convert 1000 L2_WUSDC.
function testRevertConvertWithInsufficientApproval() public {
vm.selectFork(_l2Fork);
vm.startPrank(_alice);

// setup
uint256 wrappedBalance1 = _erc20L2Wusdc.balanceOf(_alice);

uint256 approveAmount = _toUSDC(500);
uint256 convertAmount = _toUSDC(1000);
_erc20L2Wusdc.approve(address(_nativeConverter), approveAmount);

// call convert
vm.expectRevert();
_nativeConverter.convert(_alice, convertAmount);

// alice's L2_WUSDC balance didn't change
uint256 wrappedBalance2 = _erc20L2Wusdc.balanceOf(_alice);
assertEq(wrappedBalance1, wrappedBalance2);

// alice's L2_USDC balance didn't change
assertEq(_erc20L2Usdc.balanceOf(_alice), 0);

_assertUsdcSupplyAndBalancesMatch();
}

/// @notice Alice tries to convert 0 L2_WUSDC.
function testRevertConvertingZero() public {
vm.selectFork(_l2Fork);
vm.startPrank(_alice);

// try to convert 0 L2_WUSDC to L2_USDC
vm.expectRevert();
_nativeConverter.convert(_alice, 0);

_assertUsdcSupplyAndBalancesMatch();
}

/// @notice Alice tries to deposit 1000 L1_USDC to L1Escrow for address zero.
function testRevertConvertingForAddressZero() public {
vm.selectFork(_l2Fork);
vm.startPrank(_alice);

// try to convert 1000 L2_WUSDC for address 0
vm.expectRevert();
_nativeConverter.convert(address(0), _toUSDC(1000));

_assertUsdcSupplyAndBalancesMatch();
}
}

0 comments on commit ca9f020

Please sign in to comment.