Fix Uniswap tests and work on balancer test
This commit is contained in:
@@ -44,9 +44,43 @@ contract BalancerV2SwapAdapter is ISwapAdapter {
|
||||
IVault.SwapKind.GIVEN_IN, swapSteps, assets, funds
|
||||
);
|
||||
|
||||
// TODO: the delta of buyToken is negative, so we need to flip the sign
|
||||
calculatedPrice = Fraction(uint256(assetDeltas[1]), sellAmount);
|
||||
}
|
||||
|
||||
function getSellAmount(
|
||||
bytes32 pairId,
|
||||
IERC20 sellToken,
|
||||
IERC20 buyToken,
|
||||
uint256 buyAmount
|
||||
) public returns (uint256 sellAmount) {
|
||||
IVault.BatchSwapStep[] memory swapSteps = new IVault.BatchSwapStep[](1);
|
||||
swapSteps[0] = IVault.BatchSwapStep({
|
||||
poolId: pairId,
|
||||
assetInIndex: 0,
|
||||
assetOutIndex: 1,
|
||||
amount: buyAmount,
|
||||
userData: ""
|
||||
});
|
||||
address[] memory assets = new address[](2);
|
||||
assets[0] = address(sellToken);
|
||||
assets[1] = address(buyToken);
|
||||
IVault.FundManagement memory funds = IVault.FundManagement({
|
||||
sender: msg.sender,
|
||||
fromInternalBalance: false,
|
||||
recipient: payable(msg.sender),
|
||||
toInternalBalance: false
|
||||
});
|
||||
|
||||
// assetDeltas correspond to the assets array
|
||||
int256[] memory assetDeltas = new int256[](2);
|
||||
assetDeltas = vault.queryBatchSwap(
|
||||
IVault.SwapKind.GIVEN_OUT, swapSteps, assets, funds
|
||||
);
|
||||
|
||||
sellAmount = uint256(assetDeltas[0]);
|
||||
}
|
||||
|
||||
function priceBatch(
|
||||
bytes32 pairId,
|
||||
IERC20 sellToken,
|
||||
@@ -75,31 +109,39 @@ contract BalancerV2SwapAdapter is ISwapAdapter {
|
||||
OrderSide side,
|
||||
uint256 specifiedAmount
|
||||
) external override returns (Trade memory trade) {
|
||||
uint256 sellAmount;
|
||||
IVault.SwapKind kind;
|
||||
uint256 limit; // TODO set this slippage limit properly
|
||||
if (side == OrderSide.Sell) {
|
||||
sellToken.approve(address(vault), specifiedAmount);
|
||||
kind = IVault.SwapKind.GIVEN_IN;
|
||||
sellAmount = specifiedAmount;
|
||||
limit = 0;
|
||||
} else {
|
||||
buyToken.approve(address(vault), type(uint256).max);
|
||||
kind = IVault.SwapKind.GIVEN_OUT;
|
||||
sellAmount = getSellAmount(pairId, sellToken, buyToken, specifiedAmount);
|
||||
limit = type(uint256).max;
|
||||
}
|
||||
|
||||
sellToken.transferFrom(msg.sender, address(this), sellAmount);
|
||||
sellToken.approve(address(vault), sellAmount);
|
||||
|
||||
uint256 gasBefore = gasleft();
|
||||
trade.receivedAmount = vault.swap(
|
||||
trade.calculatedAmount = vault.swap(
|
||||
IVault.SingleSwap({
|
||||
poolId: pairId,
|
||||
kind: side == OrderSide.Sell
|
||||
? IVault.SwapKind.GIVEN_IN
|
||||
: IVault.SwapKind.GIVEN_OUT,
|
||||
kind: kind,
|
||||
assetIn: address(sellToken),
|
||||
assetOut: address(buyToken),
|
||||
amount: specifiedAmount,
|
||||
userData: ""
|
||||
}),
|
||||
// This contract is not an approved relayer (yet), so the sender and recipient cannot be msg.sender
|
||||
IVault.FundManagement({
|
||||
sender: address(this),
|
||||
fromInternalBalance: false,
|
||||
recipient: payable(address(this)),
|
||||
recipient: msg.sender,
|
||||
toInternalBalance: false
|
||||
}),
|
||||
0,
|
||||
limit,
|
||||
block.timestamp + SWAP_DEADLINE_SEC
|
||||
);
|
||||
trade.gasUsed = gasBefore - gasleft();
|
||||
|
||||
@@ -46,7 +46,7 @@ interface ISwapAdapterTypes {
|
||||
struct Trade {
|
||||
// If the side is sell, it is the amount of tokens sold. If the side is
|
||||
// buy, it is the amount of tokens bought.
|
||||
uint256 receivedAmount;
|
||||
uint256 calculatedAmount;
|
||||
// The amount of gas used in the trade.
|
||||
uint256 gasUsed;
|
||||
// The price of the pair after the trade. For zero use Fraction(0, 1).
|
||||
|
||||
@@ -3,7 +3,7 @@ pragma solidity ^0.8.13;
|
||||
|
||||
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
|
||||
|
||||
uint256 constant RESERVE_LIMIT_FACTOR = 10; // TODO why is the factor so high?
|
||||
uint256 constant RESERVE_LIMIT_FACTOR = 2; // TODO why is the factor so high?
|
||||
|
||||
contract UniswapV2SwapAdapter is ISwapAdapter {
|
||||
IUniswapV2Factory immutable factory;
|
||||
@@ -73,10 +73,10 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
|
||||
}
|
||||
uint256 gasBefore = gasleft();
|
||||
if (side == OrderSide.Sell) {
|
||||
trade.receivedAmount =
|
||||
trade.calculatedAmount =
|
||||
sell(pair, sellToken, zero2one, r0, r1, specifiedAmount);
|
||||
} else {
|
||||
trade.receivedAmount =
|
||||
trade.calculatedAmount =
|
||||
buy(pair, sellToken, zero2one, r0, r1, specifiedAmount);
|
||||
}
|
||||
trade.gasUsed = gasBefore - gasleft();
|
||||
@@ -90,11 +90,12 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
|
||||
uint112 reserveIn,
|
||||
uint112 reserveOut,
|
||||
uint256 amount
|
||||
) internal returns (uint256 receivedAmount) {
|
||||
) internal returns (uint256 calculatedAmount) {
|
||||
address swapper = msg.sender;
|
||||
uint256 amountOut = getAmountOut(amount, reserveIn, reserveOut);
|
||||
|
||||
// TODO: use safeTransferFrom
|
||||
sellToken.transferFrom(swapper, address(pair), amount);
|
||||
uint256 amountOut = getAmountOut(amount, reserveIn, reserveOut);
|
||||
if (zero2one) {
|
||||
pair.swap(0, amountOut, swapper, "");
|
||||
} else {
|
||||
@@ -129,9 +130,10 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
|
||||
uint112 reserveIn,
|
||||
uint112 reserveOut,
|
||||
uint256 amountOut
|
||||
) internal returns (uint256 receivedAmount) {
|
||||
) internal returns (uint256 calculatedAmount) {
|
||||
address swapper = msg.sender;
|
||||
uint256 amount = getAmountIn(amountOut, reserveIn, reserveOut);
|
||||
|
||||
if (amount == 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -174,10 +176,13 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
|
||||
{
|
||||
IUniswapV2Pair pair = IUniswapV2Pair(address(bytes20(pairId)));
|
||||
limits = new uint256[](2);
|
||||
(uint256 r0, uint256 r1,) = pair.getReserves();
|
||||
if (sellToken < buyToken) {
|
||||
(limits[0], limits[1],) = pair.getReserves();
|
||||
limits[0] = r0 / RESERVE_LIMIT_FACTOR;
|
||||
limits[1] = r1 / RESERVE_LIMIT_FACTOR;
|
||||
} else {
|
||||
(limits[1], limits[0],) = pair.getReserves();
|
||||
limits[0] = r1 / RESERVE_LIMIT_FACTOR;
|
||||
limits[1] = r0 / RESERVE_LIMIT_FACTOR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user