Merge pull request #24 from propeller-heads/mp/use-safe-erc20

Remove IERC20 from ISwapAdapter and use SafeERC20 for IERC20
This commit is contained in:
pistomat
2024-04-13 10:11:46 +00:00
committed by GitHub
9 changed files with 173 additions and 165 deletions

View File

@@ -17,8 +17,8 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
IVault(payable(0xBA12222222228d8Ba445958a75a0704d566BF2C8));
BalancerV2SwapAdapter adapter;
IERC20 constant WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
IERC20 constant BAL = IERC20(0xba100000625a3754423978a60c9317c58a424e3D);
address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address constant BAL = 0xba100000625a3754423978a60c9317c58a424e3D;
address constant B_80BAL_20WETH = 0x5c6Ee304399DBdB9C8Ef030aB642B10820DB8F56;
bytes32 constant B_80BAL_20WETH_POOL_ID =
0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014;
@@ -34,7 +34,7 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
vm.label(address(balancerV2Vault), "IVault");
vm.label(address(adapter), "BalancerV2SwapAdapter");
vm.label(address(WETH), "WETH");
vm.label(address(BAL), "BAL");
vm.label(BAL, "BAL");
vm.label(address(B_80BAL_20WETH), "B_80BAL_20WETH");
}
@@ -97,17 +97,17 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
// TODO calculate the amountIn by using price function as in
// testPriceDecreasing
deal(address(BAL), address(this), type(uint256).max);
BAL.approve(address(adapter), type(uint256).max);
deal(BAL, address(this), type(uint256).max);
IERC20(BAL).approve(address(adapter), type(uint256).max);
} else {
vm.assume(specifiedAmount < limits[0]);
deal(address(BAL), address(this), specifiedAmount);
BAL.approve(address(adapter), specifiedAmount);
deal(BAL, address(this), specifiedAmount);
IERC20(BAL).approve(address(adapter), specifiedAmount);
}
uint256 bal_balance = BAL.balanceOf(address(this));
uint256 weth_balance = WETH.balanceOf(address(this));
uint256 bal_balance = IERC20(BAL).balanceOf(address(this));
uint256 weth_balance = IERC20(WETH).balanceOf(address(this));
Trade memory trade = adapter.swap(
B_80BAL_20WETH_POOL_ID, BAL, WETH, side, specifiedAmount
@@ -117,19 +117,20 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
if (side == OrderSide.Buy) {
assertEq(
specifiedAmount,
WETH.balanceOf(address(this)) - weth_balance
IERC20(WETH).balanceOf(address(this)) - weth_balance
);
assertEq(
trade.calculatedAmount,
bal_balance - BAL.balanceOf(address(this))
bal_balance - IERC20(BAL).balanceOf(address(this))
);
} else {
assertEq(
specifiedAmount, bal_balance - BAL.balanceOf(address(this))
specifiedAmount,
bal_balance - IERC20(BAL).balanceOf(address(this))
);
assertEq(
trade.calculatedAmount,
WETH.balanceOf(address(this)) - weth_balance
IERC20(WETH).balanceOf(address(this)) - weth_balance
);
}
}
@@ -144,8 +145,8 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
uint256 beforeSwap = vm.snapshot();
deal(address(BAL), address(this), amounts[i]);
BAL.approve(address(adapter), amounts[i]);
deal(BAL, address(this), amounts[i]);
IERC20(BAL).approve(address(adapter), amounts[i]);
trades[i] = adapter.swap(
B_80BAL_20WETH_POOL_ID, BAL, WETH, OrderSide.Sell, amounts[i]
);
@@ -175,8 +176,8 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
uint256 amountIn =
(amounts[i] * price.denominator / price.numerator) * 2;
deal(address(BAL), address(this), amountIn);
BAL.approve(address(adapter), amountIn);
deal(BAL, address(this), amountIn);
IERC20(BAL).approve(address(adapter), amountIn);
trades[i] = adapter.swap(
B_80BAL_20WETH_POOL_ID, BAL, WETH, OrderSide.Buy, amounts[i]
);
@@ -203,8 +204,7 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
function testGetCapabilitiesFuzz(bytes32 pool, address t0, address t1)
public
{
Capability[] memory res =
adapter.getCapabilities(pool, IERC20(t0), IERC20(t1));
Capability[] memory res = adapter.getCapabilities(pool, t0, t1);
assertEq(res.length, 2);
assertEq(uint256(res[0]), uint256(Capability.SellOrder));
@@ -212,10 +212,10 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
}
function testGetTokens() public {
IERC20[] memory tokens = adapter.getTokens(B_80BAL_20WETH_POOL_ID);
address[] memory tokens = adapter.getTokens(B_80BAL_20WETH_POOL_ID);
assertEq(address(tokens[0]), address(BAL));
assertEq(address(tokens[1]), address(WETH));
assertEq(tokens[0], BAL);
assertEq(tokens[1], address(WETH));
}
function testGetPoolIds() public {

View File

@@ -12,8 +12,8 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
IntegralSwapAdapter adapter;
ITwapRelayer relayer;
IERC20 constant WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
IERC20 constant USDC = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address constant USDC_WETH_PAIR = 0x2fe16Dd18bba26e457B7dD2080d5674312b026a2;
address constant relayerAddress = 0xd17b3c9784510E33cD5B87b490E79253BcD81e2E;
@@ -26,7 +26,7 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
relayer = ITwapRelayer(relayerAddress);
vm.label(address(WETH), "WETH");
vm.label(address(USDC), "USDC");
vm.label(USDC, "USDC");
vm.label(address(USDC_WETH_PAIR), "USDC_WETH_PAIR");
}
@@ -66,8 +66,8 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
limitsMin = getMinLimits(USDC, WETH);
vm.assume(specifiedAmount > limitsMin[1] * 115 / 100);
deal(address(USDC), address(this), type(uint256).max);
USDC.approve(address(adapter), type(uint256).max);
deal(USDC, address(this), type(uint256).max);
IERC20(USDC).approve(address(adapter), type(uint256).max);
} else {
limits = adapter.getLimits(pair, USDC, WETH);
vm.assume(specifiedAmount < limits[0]);
@@ -75,12 +75,12 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
limitsMin = getMinLimits(USDC, WETH);
vm.assume(specifiedAmount > limitsMin[0] * 115 / 100);
deal(address(USDC), address(this), type(uint256).max);
USDC.approve(address(adapter), specifiedAmount);
deal(USDC, address(this), type(uint256).max);
IERC20(USDC).approve(address(adapter), specifiedAmount);
}
uint256 usdc_balance_before = USDC.balanceOf(address(this));
uint256 weth_balance_before = WETH.balanceOf(address(this));
uint256 usdc_balance_before = IERC20(USDC).balanceOf(address(this));
uint256 weth_balance_before = IERC20(WETH).balanceOf(address(this));
Trade memory trade =
adapter.swap(pair, USDC, WETH, side, specifiedAmount);
@@ -89,22 +89,22 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
if (side == OrderSide.Buy) {
assertEq(
specifiedAmount,
WETH.balanceOf(address(this)) - weth_balance_before
IERC20(WETH).balanceOf(address(this)) - weth_balance_before
);
assertEq(
trade.calculatedAmount,
usdc_balance_before - USDC.balanceOf(address(this))
usdc_balance_before - IERC20(USDC).balanceOf(address(this))
);
} else {
assertEq(
specifiedAmount,
usdc_balance_before - USDC.balanceOf(address(this))
usdc_balance_before - IERC20(USDC).balanceOf(address(this))
);
assertEq(
trade.calculatedAmount,
WETH.balanceOf(address(this)) - weth_balance_before
IERC20(WETH).balanceOf(address(this)) - weth_balance_before
);
}
}
@@ -135,8 +135,8 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
for (uint256 i = 1; i < TEST_ITERATIONS; i++) {
beforeSwap = vm.snapshot();
deal(address(USDC), address(this), amounts[i]);
USDC.approve(address(adapter), amounts[i]);
deal(USDC, address(this), amounts[i]);
IERC20(USDC).approve(address(adapter), amounts[i]);
trades[i] = adapter.swap(pair, USDC, WETH, side, amounts[i]);
vm.revertTo(beforeSwap);
@@ -152,15 +152,14 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
function testGetCapabilitiesIntegral(bytes32 pair, address t0, address t1)
public
{
Capability[] memory res =
adapter.getCapabilities(pair, IERC20(t0), IERC20(t1));
Capability[] memory res = adapter.getCapabilities(pair, t0, t1);
assertEq(res.length, 4);
}
function testGetTokensIntegral() public {
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
IERC20[] memory tokens = adapter.getTokens(pair);
address[] memory tokens = adapter.getTokens(pair);
assertEq(tokens.length, 2);
}
@@ -172,7 +171,7 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
assertEq(limits.length, 2);
}
function getMinLimits(IERC20 sellToken, IERC20 buyToken)
function getMinLimits(address sellToken, address buyToken)
public
view
returns (uint256[] memory limits)

View File

@@ -11,8 +11,8 @@ contract UniswapV2PairFunctionTest is Test, ISwapAdapterTypes {
using FractionMath for Fraction;
UniswapV2SwapAdapter adapter;
IERC20 constant WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
IERC20 constant USDC = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address constant USDC_WETH_PAIR = 0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc;
uint256 constant TEST_ITERATIONS = 100;
@@ -24,9 +24,9 @@ contract UniswapV2PairFunctionTest is Test, ISwapAdapterTypes {
new UniswapV2SwapAdapter(0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f);
vm.label(address(adapter), "UniswapV2SwapAdapter");
vm.label(address(WETH), "WETH");
vm.label(address(USDC), "USDC");
vm.label(address(USDC_WETH_PAIR), "USDC_WETH_PAIR");
vm.label(WETH, "WETH");
vm.label(USDC, "USDC");
vm.label(USDC_WETH_PAIR, "USDC_WETH_PAIR");
}
function testPriceFuzz(uint256 amount0, uint256 amount1) public {
@@ -75,17 +75,17 @@ contract UniswapV2PairFunctionTest is Test, ISwapAdapterTypes {
// TODO calculate the amountIn by using price function as in
// BalancerV2 testPriceDecreasing
deal(address(USDC), address(this), type(uint256).max);
USDC.approve(address(adapter), type(uint256).max);
deal(USDC, address(this), type(uint256).max);
IERC20(USDC).approve(address(adapter), type(uint256).max);
} else {
vm.assume(specifiedAmount < limits[0]);
deal(address(USDC), address(this), specifiedAmount);
USDC.approve(address(adapter), specifiedAmount);
deal(USDC, address(this), specifiedAmount);
IERC20(USDC).approve(address(adapter), specifiedAmount);
}
uint256 usdc_balance = USDC.balanceOf(address(this));
uint256 weth_balance = WETH.balanceOf(address(this));
uint256 usdc_balance = IERC20(USDC).balanceOf(address(this));
uint256 weth_balance = IERC20(WETH).balanceOf(address(this));
Trade memory trade =
adapter.swap(pair, USDC, WETH, side, specifiedAmount);
@@ -94,20 +94,20 @@ contract UniswapV2PairFunctionTest is Test, ISwapAdapterTypes {
if (side == OrderSide.Buy) {
assertEq(
specifiedAmount,
WETH.balanceOf(address(this)) - weth_balance
IERC20(WETH).balanceOf(address(this)) - weth_balance
);
assertEq(
trade.calculatedAmount,
usdc_balance - USDC.balanceOf(address(this))
usdc_balance - IERC20(USDC).balanceOf(address(this))
);
} else {
assertEq(
specifiedAmount,
usdc_balance - USDC.balanceOf(address(this))
usdc_balance - IERC20(USDC).balanceOf(address(this))
);
assertEq(
trade.calculatedAmount,
WETH.balanceOf(address(this)) - weth_balance
IERC20(WETH).balanceOf(address(this)) - weth_balance
);
}
}
@@ -130,8 +130,8 @@ contract UniswapV2PairFunctionTest is Test, ISwapAdapterTypes {
for (uint256 i = 0; i < TEST_ITERATIONS; i++) {
beforeSwap = vm.snapshot();
deal(address(USDC), address(this), amounts[i]);
USDC.approve(address(adapter), amounts[i]);
deal(USDC, address(this), amounts[i]);
IERC20(USDC).approve(address(adapter), amounts[i]);
trades[i] = adapter.swap(pair, USDC, WETH, side, amounts[i]);
vm.revertTo(beforeSwap);
@@ -149,8 +149,7 @@ contract UniswapV2PairFunctionTest is Test, ISwapAdapterTypes {
}
function testGetCapabilities(bytes32 pair, address t0, address t1) public {
Capability[] memory res =
adapter.getCapabilities(pair, IERC20(t0), IERC20(t1));
Capability[] memory res = adapter.getCapabilities(pair, t0, t1);
assertEq(res.length, 3);
}