Finish tests
This commit is contained in:
@@ -8,8 +8,11 @@ import {
|
||||
IVault
|
||||
} from "src/balancer-v2/BalancerV2SwapAdapter.sol";
|
||||
import {ISwapAdapterTypes} from "src/interfaces/ISwapAdapterTypes.sol";
|
||||
import {FractionMath} from "src/libraries/FractionMath.sol";
|
||||
|
||||
contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
|
||||
using FractionMath for Fraction;
|
||||
|
||||
IVault constant balancerV2Vault =
|
||||
IVault(payable(0xBA12222222228d8Ba445958a75a0704d566BF2C8));
|
||||
BalancerV2SwapAdapter adapter;
|
||||
@@ -20,6 +23,8 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
|
||||
bytes32 constant B_80BAL_20WETH_POOL_ID =
|
||||
0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014;
|
||||
|
||||
uint256 constant TEST_ITERATIONS = 100;
|
||||
|
||||
function setUp() public {
|
||||
uint256 forkBlock = 18710000;
|
||||
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
||||
@@ -45,28 +50,50 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
|
||||
adapter.price(B_80BAL_20WETH_POOL_ID, BAL, WETH, amounts);
|
||||
}
|
||||
|
||||
// function testPriceSingleFuzz(uint256 amount) public {
|
||||
// uint256[] memory limits = adapter.getLimits(B_80BAL_20WETH_POOL_ID, BAL, WETH);
|
||||
// vm.assume(amount < limits[0]);
|
||||
// vm.assume(amount > 100);
|
||||
function testPriceSingleFuzz() public {
|
||||
uint256 specifiedAmount = 100 * 10 ** 18;
|
||||
// Assume OrderSide.Sell
|
||||
uint256[] memory limits =
|
||||
adapter.getLimits(B_80BAL_20WETH_POOL_ID, BAL, WETH);
|
||||
|
||||
// uint256[] memory amounts = new uint256[](1);
|
||||
// amounts[0] = amount;
|
||||
vm.assume(specifiedAmount > 0);
|
||||
vm.assume(specifiedAmount < limits[0]);
|
||||
|
||||
// Fraction memory price =
|
||||
// adapter.priceSingle(B_80BAL_20WETH_POOL_ID, BAL, WETH, amount);
|
||||
Fraction memory price = adapter.priceSingle(
|
||||
B_80BAL_20WETH_POOL_ID, BAL, WETH, specifiedAmount
|
||||
);
|
||||
|
||||
// console.log("price.numerator: ", price.numerator);
|
||||
// console.log("price.denominator: ", price.denominator);
|
||||
assertGt(price.numerator, 0);
|
||||
assertGt(price.denominator, 0);
|
||||
}
|
||||
|
||||
// assertGt(price.numerator, 0);
|
||||
// }
|
||||
function testPriceDecreasing() public {
|
||||
uint256[] memory amounts = new uint256[](TEST_ITERATIONS);
|
||||
Fraction[] memory prices = new Fraction[](TEST_ITERATIONS);
|
||||
|
||||
for (uint256 i = 0; i < TEST_ITERATIONS; i++) {
|
||||
amounts[i] = 1000 * (i + 1) * 10 ** 18;
|
||||
console.log("i = ", i);
|
||||
console.log("amounts[i] = ", amounts[i]);
|
||||
prices[i] = adapter.priceSingle(
|
||||
B_80BAL_20WETH_POOL_ID, BAL, WETH, amounts[i]
|
||||
);
|
||||
console.log("prices = ", prices[i].numerator, prices[i].denominator);
|
||||
}
|
||||
|
||||
for (uint256 i = 0; i < TEST_ITERATIONS - 1; i++) {
|
||||
assertEq(prices[i].compareFractions(prices[i + 1]), 1);
|
||||
assertGt(prices[i].denominator, 0);
|
||||
assertGt(prices[i + 1].denominator, 0);
|
||||
}
|
||||
}
|
||||
|
||||
function testSwapFuzz(uint256 specifiedAmount, bool isBuy) public {
|
||||
OrderSide side = isBuy ? OrderSide.Buy : OrderSide.Sell;
|
||||
vm.assume(specifiedAmount > 0);
|
||||
|
||||
uint256[] memory limits = adapter.getLimits(B_80BAL_20WETH_POOL_ID, BAL, WETH);
|
||||
uint256[] memory limits =
|
||||
adapter.getLimits(B_80BAL_20WETH_POOL_ID, BAL, WETH);
|
||||
|
||||
if (side == OrderSide.Buy) {
|
||||
vm.assume(specifiedAmount < limits[1]);
|
||||
@@ -74,30 +101,98 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
|
||||
// sellAmount is not specified for buy orders
|
||||
deal(address(BAL), address(this), type(uint256).max);
|
||||
BAL.approve(address(adapter), type(uint256).max);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
vm.assume(specifiedAmount < limits[0]);
|
||||
|
||||
deal(address(BAL), address(this), specifiedAmount);
|
||||
BAL.approve(address(adapter), specifiedAmount);
|
||||
}
|
||||
|
||||
|
||||
uint256 bal_balance = BAL.balanceOf(address(this));
|
||||
uint256 weth_balance = WETH.balanceOf(address(this));
|
||||
|
||||
Trade memory trade = adapter.swap(B_80BAL_20WETH_POOL_ID, BAL, WETH, side, specifiedAmount);
|
||||
Trade memory trade = adapter.swap(
|
||||
B_80BAL_20WETH_POOL_ID, BAL, WETH, side, specifiedAmount
|
||||
);
|
||||
|
||||
if (trade.calculatedAmount > 0) {
|
||||
if (side == OrderSide.Buy) {
|
||||
assertEq(specifiedAmount, WETH.balanceOf(address(this)) - weth_balance);
|
||||
assertEq(trade.calculatedAmount, bal_balance - BAL.balanceOf(address(this)));
|
||||
assertEq(
|
||||
specifiedAmount,
|
||||
WETH.balanceOf(address(this)) - weth_balance
|
||||
);
|
||||
assertEq(
|
||||
trade.calculatedAmount,
|
||||
bal_balance - BAL.balanceOf(address(this))
|
||||
);
|
||||
} else {
|
||||
assertEq(specifiedAmount, bal_balance - BAL.balanceOf(address(this)));
|
||||
assertEq(trade.calculatedAmount, WETH.balanceOf(address(this)) - weth_balance);
|
||||
assertEq(
|
||||
specifiedAmount, bal_balance - BAL.balanceOf(address(this))
|
||||
);
|
||||
assertEq(
|
||||
trade.calculatedAmount,
|
||||
WETH.balanceOf(address(this)) - weth_balance
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function testSwapSellIncreasing() public {
|
||||
uint256[] memory amounts = new uint256[](TEST_ITERATIONS);
|
||||
Trade[] memory trades = new Trade[](TEST_ITERATIONS);
|
||||
|
||||
for (uint256 i = 0; i < TEST_ITERATIONS; i++) {
|
||||
amounts[i] = 1000 * (i + 1) * 10 ** 18;
|
||||
|
||||
uint256 beforeSwap = vm.snapshot();
|
||||
|
||||
deal(address(BAL), address(this), amounts[i]);
|
||||
BAL.approve(address(adapter), amounts[i]);
|
||||
trades[i] = adapter.swap(
|
||||
B_80BAL_20WETH_POOL_ID, BAL, WETH, OrderSide.Sell, amounts[i]
|
||||
);
|
||||
|
||||
vm.revertTo(beforeSwap);
|
||||
}
|
||||
|
||||
for (uint256 i = 0; i < TEST_ITERATIONS - 1; i++) {
|
||||
assertLe(trades[i].calculatedAmount, trades[i + 1].calculatedAmount);
|
||||
assertLe(trades[i].gasUsed, trades[i + 1].gasUsed);
|
||||
assertEq(trades[i].price.compareFractions(trades[i + 1].price), 1);
|
||||
}
|
||||
}
|
||||
|
||||
function testSwapBuyIncreasing() public {
|
||||
uint256[] memory amounts = new uint256[](TEST_ITERATIONS);
|
||||
Trade[] memory trades = new Trade[](TEST_ITERATIONS);
|
||||
|
||||
for (uint256 i = 0; i < TEST_ITERATIONS; i++) {
|
||||
amounts[i] = 10 * (i + 1) * 10 ** 18;
|
||||
|
||||
uint256 beforeSwap = vm.snapshot();
|
||||
|
||||
Fraction memory price = adapter.priceSingle(
|
||||
B_80BAL_20WETH_POOL_ID, BAL, WETH, amounts[i]
|
||||
);
|
||||
uint256 amountIn =
|
||||
(amounts[i] * price.denominator / price.numerator) * 2;
|
||||
|
||||
deal(address(BAL), address(this), amountIn);
|
||||
BAL.approve(address(adapter), amountIn);
|
||||
trades[i] = adapter.swap(
|
||||
B_80BAL_20WETH_POOL_ID, BAL, WETH, OrderSide.Buy, amounts[i]
|
||||
);
|
||||
|
||||
vm.revertTo(beforeSwap);
|
||||
}
|
||||
|
||||
for (uint256 i = 0; i < TEST_ITERATIONS - 1; i++) {
|
||||
assertLe(trades[i].calculatedAmount, trades[i + 1].calculatedAmount);
|
||||
assertLe(trades[i].gasUsed, trades[i + 1].gasUsed);
|
||||
assertEq(trades[i].price.compareFractions(trades[i + 1].price), 1);
|
||||
}
|
||||
}
|
||||
|
||||
function testGetLimits() public view {
|
||||
uint256[] memory limits =
|
||||
adapter.getLimits(B_80BAL_20WETH_POOL_ID, BAL, WETH);
|
||||
@@ -107,7 +202,9 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
|
||||
assert(limits[1] > 0);
|
||||
}
|
||||
|
||||
function testGetCapabilitiesFuzz(bytes32 pair, address t0, address t1) public {
|
||||
function testGetCapabilitiesFuzz(bytes32 pair, address t0, address t1)
|
||||
public
|
||||
{
|
||||
Capability[] memory res =
|
||||
adapter.getCapabilities(pair, IERC20(t0), IERC20(t1));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user