Merge branch 'refs/heads/main' into pm/eng-3406-add-swap-bytes-param

This commit is contained in:
PierreMkt
2024-08-05 10:44:55 -04:00
4 changed files with 24 additions and 23 deletions

View File

@@ -54,13 +54,10 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
if (reserveIn == 0 || reserveOut == 0) { if (reserveIn == 0 || reserveOut == 0) {
revert Unavailable("At least one reserve is zero!"); revert Unavailable("At least one reserve is zero!");
} }
uint256 amountInWithFee = amountIn * 997; uint256 amountOut = getAmountOut(amountIn, reserveIn, reserveOut);
uint256 numerator = amountInWithFee * reserveOut;
uint256 denominator = (reserveIn * 1000) + amountInWithFee;
uint256 amountOut = numerator / denominator;
uint256 newReserveOut = reserveOut - amountOut; uint256 newReserveOut = reserveOut - amountOut;
uint256 newReserveIn = reserveIn + amountIn; uint256 newReserveIn = reserveIn + amountIn;
return Fraction(newReserveOut * 1000, newReserveIn * 997); return Fraction(newReserveOut * 997, newReserveIn * 1000);
} }
/// @inheritdoc ISwapAdapter /// @inheritdoc ISwapAdapter
@@ -234,10 +231,11 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
override override
returns (Capability[] memory capabilities) returns (Capability[] memory capabilities)
{ {
capabilities = new Capability[](3); capabilities = new Capability[](4);
capabilities[0] = Capability.SellOrder; capabilities[0] = Capability.SellOrder;
capabilities[1] = Capability.BuyOrder; capabilities[1] = Capability.BuyOrder;
capabilities[2] = Capability.PriceFunction; capabilities[2] = Capability.PriceFunction;
capabilities[3] = Capability.MarginalPrice;
} }
/// @inheritdoc ISwapAdapter /// @inheritdoc ISwapAdapter

View File

@@ -20,9 +20,10 @@ contract AdapterTest is Test, ISwapAdapterTypes {
// sell amounts. Asserts that the prices behaves as expected. // sell amounts. Asserts that the prices behaves as expected.
// @param adapter The swap adapter to test // @param adapter The swap adapter to test
// @param poolIds The list of pool ids to test // @param poolIds The list of pool ids to test
function testPoolBehaviour(ISwapAdapter adapter, bytes32[] memory poolIds) function runPoolBehaviourTest(
public ISwapAdapter adapter,
{ bytes32[] memory poolIds
) public {
bool hasPriceImpact = !hasCapability( bool hasPriceImpact = !hasCapability(
adapter.getCapabilities(poolIds[0], address(0), address(0)), adapter.getCapabilities(poolIds[0], address(0), address(0)),
Capability.ConstantPrice Capability.ConstantPrice
@@ -84,6 +85,8 @@ contract AdapterTest is Test, ISwapAdapterTypes {
console2.log("TEST: Testing behavior for price at 0"); console2.log("TEST: Testing behavior for price at 0");
assertGt(prices[0].numerator, 0, "Nominator shouldn't be 0"); assertGt(prices[0].numerator, 0, "Nominator shouldn't be 0");
assertGt(prices[0].denominator, 0, "Denominator shouldn't be 0"); assertGt(prices[0].denominator, 0, "Denominator shouldn't be 0");
uint256 priceAtZero = fractionToInt(prices[0]);
console2.log("TEST: Price at 0: %d", priceAtZero);
Trade memory trade; Trade memory trade;
deal(tokenIn, address(this), 5 * amounts[amounts.length - 1]); deal(tokenIn, address(this), 5 * amounts[amounts.length - 1]);
@@ -105,14 +108,14 @@ contract AdapterTest is Test, ISwapAdapterTypes {
uint256 executedPrice = uint256 executedPrice =
trade.calculatedAmount * pricePrecision / amounts[j]; trade.calculatedAmount * pricePrecision / amounts[j];
uint256 priceAfterSwap = fractionToInt(trade.price); uint256 priceAfterSwap = fractionToInt(trade.price);
console2.log("TEST: - Pool price: %d", priceAtAmount);
console2.log("TEST: - Executed price: %d", executedPrice); console2.log("TEST: - Executed price: %d", executedPrice);
console2.log("TEST: - Price at amount: %d", priceAtAmount);
console2.log("TEST: - Price after swap: %d", priceAfterSwap); console2.log("TEST: - Price after swap: %d", priceAfterSwap);
if (hasPriceImpact) { if (hasPriceImpact) {
assertGe( assertGe(
priceAtAmount,
executedPrice, executedPrice,
priceAtAmount,
"Price should be greated than executed price." "Price should be greated than executed price."
); );
assertGt( assertGt(
@@ -121,24 +124,24 @@ contract AdapterTest is Test, ISwapAdapterTypes {
"Executed price should be greater than price after swap." "Executed price should be greater than price after swap."
); );
assertGt( assertGt(
priceAtAmount, priceAtZero,
priceAfterSwap, executedPrice,
"Price should be greated than price after swap." "Price should be greated than price after swap."
); );
} else { } else {
assertGe( assertGe(
priceAtAmount, priceAtZero,
executedPrice,
"Price should be greater or equal to executed price."
);
assertGe(
executedPrice,
priceAfterSwap, priceAfterSwap,
"Executed price should be or equal to price after swap." "Executed price should be or equal to price after swap."
); );
assertGe( assertGe(
priceAtZero,
priceAtAmount, priceAtAmount,
priceAfterSwap, "Executed price should be or equal to price after swap."
);
assertGe(
priceAtZero,
executedPrice,
"Price should be or equal to price after swap." "Price should be or equal to price after swap."
); );
} }

View File

@@ -243,6 +243,6 @@ contract BalancerV2SwapAdapterTest is AdapterTest {
function testBalancerV2PoolBehaviour() public { function testBalancerV2PoolBehaviour() public {
bytes32[] memory poolIds = new bytes32[](1); bytes32[] memory poolIds = new bytes32[](1);
poolIds[0] = B_80BAL_20WETH_POOL_ID; poolIds[0] = B_80BAL_20WETH_POOL_ID;
testPoolBehaviour(adapter, poolIds); runPoolBehaviourTest(adapter, poolIds);
} }
} }

View File

@@ -152,7 +152,7 @@ contract UniswapV2PairFunctionTest is AdapterTest {
function testGetCapabilities(bytes32 pair, address t0, address t1) public { function testGetCapabilities(bytes32 pair, address t0, address t1) public {
Capability[] memory res = adapter.getCapabilities(pair, t0, t1); Capability[] memory res = adapter.getCapabilities(pair, t0, t1);
assertEq(res.length, 3); assertEq(res.length, 4);
} }
function testGetLimits() public { function testGetLimits() public {
@@ -165,6 +165,6 @@ contract UniswapV2PairFunctionTest is AdapterTest {
function testUsv2PoolBehaviour() public { function testUsv2PoolBehaviour() public {
bytes32[] memory poolIds = new bytes32[](1); bytes32[] memory poolIds = new bytes32[](1);
poolIds[0] = bytes32(bytes20(USDC_WETH_PAIR)); poolIds[0] = bytes32(bytes20(USDC_WETH_PAIR));
testPoolBehaviour(adapter, poolIds); runPoolBehaviourTest(adapter, poolIds);
} }
} }