Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WstEthBundler tests #149

Merged
merged 20 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/Configured.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract contract Configured is StdChains {
address internal USDC;
address internal USDT;
address internal LINK;
address internal ST_ETH;
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
address internal WBTC;
address internal WETH;
address internal CB_ETH;
Expand Down Expand Up @@ -56,13 +57,14 @@ abstract contract Configured is StdChains {
USDC = CONFIG.getAddress("USDC");
USDT = CONFIG.getAddress("USDT");
LINK = CONFIG.getAddress("LINK");
ST_ETH = CONFIG.getAddress("stETH");
WBTC = CONFIG.getAddress("WBTC");
WETH = CONFIG.getAddress("WETH");
CB_ETH = CONFIG.getAddress("cbETH");
WNATIVE = CONFIG.getWrappedNative();

lsdNatives = CONFIG.getLsdNatives();
allAssets = [DAI, USDC, USDT, LINK, WBTC, WETH];
allAssets = [DAI, USDC, USDT, LINK, ST_ETH, WBTC, WETH];

ConfigMarket[] memory allConfigMarkets = CONFIG.getMarkets();
for (uint256 i; i < allConfigMarkets.length; ++i) {
Expand Down
1 change: 1 addition & 0 deletions config/ethereum-mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"USDC": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"USDT": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
"LINK": "0x514910771AF9Ca656af840dff83E8264EcF986CA",
"stETH": "0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84",
"WBTC": "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
"WETH": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"wstETH": "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0",
Expand Down
4 changes: 3 additions & 1 deletion contracts/ethereum-mainnet/interfaces/IStEth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pragma solidity >=0.5.0;
interface IStEth {
function transferShares(address _recipient, uint256 _sharesAmount) external returns (uint256);

function transferSharesFrom(address _sender, address _recipient, uint256 _sharesAmount) external returns (uint256);
function transferSharesFrom(address _sender, address _recipient, uint256 _sharesAmount)
external
returns (uint256);

function getPooledEthByShares(uint256 _sharesAmount) external view returns (uint256);

Expand Down
165 changes: 165 additions & 0 deletions test/ethereum-mainnet/StEthBundlerEthereumTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import {SigUtils} from "test/helpers/SigUtils.sol";
import {ErrorsLib as BulkerErrorsLib} from "contracts/libraries/ErrorsLib.sol";

import {IStEth} from "contracts/ethereum-mainnet/interfaces/IStEth.sol";
import {IWStEth} from "contracts/ethereum-mainnet/interfaces/IWStEth.sol";

import "../helpers/ForkTest.sol";

import "../mocks/StEthBundlerMock.sol";

contract StEthBundlerEthereumTest is ForkTest {
address public constant WST_ETH = 0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;
StEthBundlerMock private bundler;

function _network() internal pure override returns (string memory) {
return "ethereum-mainnet";
}

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

bundler = new StEthBundlerMock();

vm.startPrank(USER);
ERC20(ST_ETH).approve(address(bundler), type(uint256).max);
ERC20(WST_ETH).approve(address(bundler), type(uint256).max);
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
vm.stopPrank();
}

function testTransferStEthSharesFromZeroAmount() public {
bytes[] memory data = new bytes[](1);
data[0] = abi.encodeCall(StEthBundler.transferSharesFrom, (0));

vm.prank(USER);
vm.expectRevert(bytes(BulkerErrorsLib.ZERO_AMOUNT));
bundler.multicall(block.timestamp, data);
}

function testTransferStEthSharesFrom(uint256 amount) public {
amount = bound(amount, MIN_AMOUNT, MAX_AMOUNT);

uint256 stEthAmount = _mintStEth(amount);
uint256 stEthUserBalanceBeforeTransfer = ERC20(ST_ETH).balanceOf(USER);

bytes[] memory data = new bytes[](1);
data[0] = abi.encodeCall(StEthBundler.transferSharesFrom, (stEthAmount));

vm.prank(USER);
bundler.multicall(block.timestamp, data);

assertEq(ERC20(ST_ETH).balanceOf(USER), 0, "User's StEth Balance");
assertEq(ERC20(ST_ETH).balanceOf(address(bundler)), stEthUserBalanceBeforeTransfer, "Receiver's StEth Balance");
}

function testWrapZeroAddress(uint256 amount) public {
vm.assume(amount != 0);

bytes[] memory data = new bytes[](1);
data[0] = abi.encodeCall(StEthBundler.wrapStEth, (amount, address(0)));

vm.expectRevert(bytes(BulkerErrorsLib.ZERO_ADDRESS));
vm.prank(USER);
bundler.multicall(block.timestamp, data);
}

function testWrapZeroAmount(address receiver) public {
vm.assume(receiver != address(bundler) && receiver != address(0));

bytes[] memory data = new bytes[](1);
data[0] = abi.encodeCall(StEthBundler.wrapStEth, (0, receiver));

vm.expectRevert(bytes(BulkerErrorsLib.ZERO_AMOUNT));
vm.prank(USER);
bundler.multicall(block.timestamp, data);
}

function testWrapStEth(uint256 amount) public {
amount = bound(amount, MIN_AMOUNT, MAX_AMOUNT);

uint256 stEthAmount = _mintStEth(amount);

bytes[] memory data = new bytes[](2);
data[0] = abi.encodeCall(StEthBundler.transferSharesFrom, (stEthAmount));
data[1] = abi.encodeCall(StEthBundler.wrapStEth, (amount, RECEIVER));

uint256 wstEthExpectedAmount = IStEth(ST_ETH).getSharesByPooledEth(amount);

vm.prank(USER);
bundler.multicall(block.timestamp, data);

assertEq(ERC20(WST_ETH).balanceOf(address(bundler)), 0, "Bundler's wrapped stEth balance");
assertEq(ERC20(WST_ETH).balanceOf(USER), 0, "User's wrapped stEth balance");
assertEq(ERC20(WST_ETH).balanceOf(RECEIVER), wstEthExpectedAmount, "Receiver's wrapped stEth balance");

assertEq(ERC20(ST_ETH).balanceOf(address(bundler)), 0, "Bundler's stEth balance");
assertEq(ERC20(ST_ETH).balanceOf(USER), 0, "User's stEth balance");
assertEq(ERC20(ST_ETH).balanceOf(RECEIVER), 0, "Receiver's stEth balance");
}

function testUnwrapZeroAddress(uint256 amount) public {
vm.assume(amount != 0);

bytes[] memory data = new bytes[](1);
data[0] = abi.encodeCall(StEthBundler.unwrapStEth, (amount, address(0)));

vm.expectRevert(bytes(BulkerErrorsLib.ZERO_ADDRESS));
vm.prank(USER);
bundler.multicall(block.timestamp, data);
}

function testUnwrapBundlerAddress(uint256 amount) public {
vm.assume(amount != 0);

bytes[] memory data = new bytes[](1);
data[0] = abi.encodeCall(StEthBundler.unwrapStEth, (amount, address(bundler)));

vm.expectRevert(bytes(BulkerErrorsLib.BUNDLER_ADDRESS));
vm.prank(USER);
bundler.multicall(block.timestamp, data);
}

function testUnwrapZeroAmount(address receiver) public {
vm.assume(receiver != address(bundler) && receiver != address(0));

bytes[] memory data = new bytes[](1);
data[0] = abi.encodeCall(StEthBundler.unwrapStEth, (0, receiver));

vm.expectRevert(bytes(BulkerErrorsLib.ZERO_AMOUNT));
vm.prank(USER);
bundler.multicall(block.timestamp, data);
}

function testUnwrapWstEth(uint256 amount) public {
amount = bound(amount, MIN_AMOUNT, MAX_AMOUNT);

bytes[] memory data = new bytes[](2);
data[0] = abi.encodeCall(ERC20Bundler.transferFrom2, (WST_ETH, amount));
data[1] = abi.encodeCall(StEthBundler.unwrapStEth, (amount, RECEIVER));

deal(WST_ETH, USER, amount);
console2.log(amount, "amount", ERC20(WST_ETH).balanceOf(USER), "User's WST_ETH balance");
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
vm.prank(USER);
bundler.multicall(block.timestamp, data);

uint256 expectedUnwrappedAmount = IWStEth(WST_ETH).getStETHByWstETH(amount);

assertEq(ERC20(WST_ETH).balanceOf(address(bundler)), 0, "Bundler's wrapped stEth balance");
assertEq(ERC20(WST_ETH).balanceOf(USER), 0, "User's wrapped stEth balance");
assertEq(ERC20(WST_ETH).balanceOf(RECEIVER), 0, "Receiver's wrapped stEth balance");

assertEq(ERC20(ST_ETH).balanceOf(address(bundler)), 0, "Bundler's stEth balance");
assertEq(ERC20(ST_ETH).balanceOf(USER), 0, "User's stEth balance");
assertApproxEqAbs(ERC20(ST_ETH).balanceOf(RECEIVER), expectedUnwrappedAmount, 2, "Receiver's stEth balance");
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
}

function _mintStEth(uint256 amount) internal returns (uint256 stEthAmount) {
vm.deal(USER, amount);
vm.prank(USER);
stEthAmount = IStEth(ST_ETH).submit{value: amount}(address(0));
vm.assume(stEthAmount != 0);
}
}
1 change: 1 addition & 0 deletions test/helpers/ForkTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ abstract contract ForkTest is BaseTest, Configured {
for (uint256 i; i < allAssets.length; ++i) {
address asset = allAssets[i];

if (asset == ST_ETH) return;
deal(asset, user, balance / (10 ** (18 - ERC20(asset).decimals())));
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/mocks/StEthBundlerMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pragma solidity ^0.8.21;

import "contracts/ethereum-mainnet/StEthBundler.sol";
import "contracts/ERC20Bundler.sol";

contract StEthBundlerMock is StEthBundler, ERC20Bundler {}
Loading