Balancer swap test

This commit is contained in:
pistomat
2023-12-04 16:30:57 +01:00
parent 7ffee9ac2e
commit 6c6525be2b
5 changed files with 220 additions and 43 deletions

View File

@@ -4,6 +4,9 @@ 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 SWAP_DEADLINE_SEC = 1000;
contract BalancerV2SwapAdapter is ISwapAdapter {
IVault immutable vault;
@@ -11,12 +14,57 @@ contract BalancerV2SwapAdapter is ISwapAdapter {
vault = IVault(vault_);
}
function price(
function priceSingle(
bytes32 pairId,
IERC20 sellToken,
IERC20 buyToken,
uint256[] memory sellAmounts
) external view override returns (Fraction[] memory prices) {
uint256 sellAmount
) public returns (Fraction memory calculatedPrice) {
IVault.BatchSwapStep[] memory swapSteps = new IVault.BatchSwapStep[](1);
swapSteps[0] = IVault.BatchSwapStep({
poolId: pairId,
assetInIndex: 0,
assetOutIndex: 1,
amount: sellAmount,
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_IN, swapSteps, assets, funds
);
calculatedPrice = Fraction(uint256(assetDeltas[1]), sellAmount);
}
function priceBatch(
bytes32 pairId,
IERC20 sellToken,
IERC20 buyToken,
uint256[] memory specifiedAmounts
) external returns (Fraction[] memory calculatedPrices) {
for (uint256 i = 0; i < specifiedAmounts.length; i++) {
calculatedPrices[i] =
priceSingle(pairId, sellToken, buyToken, specifiedAmounts[i]);
}
}
function price(bytes32, IERC20, IERC20, uint256[] memory)
external
pure
override
returns (Fraction[] memory)
{
revert NotImplemented("BalancerV2SwapAdapter.price");
}
@@ -44,14 +92,15 @@ contract BalancerV2SwapAdapter is ISwapAdapter {
amount: specifiedAmount,
userData: ""
}),
// This contract is not an approved relayer (yet), so the sender and recipient cannot be msg.sender
IVault.FundManagement({
sender: msg.sender,
sender: address(this),
fromInternalBalance: false,
recipient: payable(msg.sender),
recipient: payable(address(this)),
toInternalBalance: false
}),
0,
block.number
block.timestamp + SWAP_DEADLINE_SEC
);
trade.gasUsed = gasBefore - gasleft();
trade.price = Fraction(0, 1); // Without the price function return 0.
@@ -63,14 +112,16 @@ contract BalancerV2SwapAdapter is ISwapAdapter {
override
returns (uint256[] memory limits)
{
limits = new uint256[](2);
(IERC20[] memory tokens, uint256[] memory balances,) =
vault.getPoolTokens(pairId);
for (uint256 i = 0; i < tokens.length; i++) {
if (tokens[i] == sellToken) {
limits[0] = balances[i];
limits[0] = balances[i] * RESERVE_LIMIT_FACTOR;
}
if (tokens[i] == buyToken) {
limits[1] = balances[i];
limits[1] = balances[i] * RESERVE_LIMIT_FACTOR;
}
}
}
@@ -95,12 +146,13 @@ contract BalancerV2SwapAdapter is ISwapAdapter {
(tokens,,) = vault.getPoolTokens(pairId);
}
/// @dev Balancer V2 does not support enumerating pools, they have to be indexed off-chain.
function getPoolIds(uint256 offset, uint256 limit)
/// @dev Balancer V2 does not support enumerating pools, they have to be
/// indexed off-chain.
function getPoolIds(uint256, uint256)
external
view
pure
override
returns (bytes32[] memory ids)
returns (bytes32[] memory)
{
revert NotImplemented("BalancerV2SwapAdapter.getPoolIds");
}
@@ -174,9 +226,7 @@ interface IVault {
uint256 feeAmount
);
event InternalBalanceChanged(
address indexed user,
address indexed token,
int256 delta
address indexed user, address indexed token, int256 delta
);
event PausedStateChanged(bool paused);
event PoolBalanceChanged(
@@ -199,9 +249,7 @@ interface IVault {
uint8 specialization
);
event RelayerApprovalChanged(
address indexed relayer,
address indexed sender,
bool approved
address indexed relayer, address indexed sender, bool approved
);
event Swap(
bytes32 indexed poolId,
@@ -212,9 +260,7 @@ interface IVault {
);
event TokensDeregistered(bytes32 indexed poolId, address[] tokens);
event TokensRegistered(
bytes32 indexed poolId,
address[] tokens,
address[] assetManagers
bytes32 indexed poolId, address[] tokens, address[] assetManagers
);
function WETH() external view returns (address);
@@ -228,7 +274,8 @@ interface IVault {
uint256 deadline
) external payable returns (int256[] memory assetDeltas);
function deregisterTokens(bytes32 poolId, address[] memory tokens) external;
function deregisterTokens(bytes32 poolId, address[] memory tokens)
external;
function exitPool(
bytes32 poolId,
@@ -296,6 +343,28 @@ interface IVault {
function manageUserBalance(UserBalanceOp[] memory ops) external payable;
/**
* @dev Simulates a call to `batchSwap`, returning an array of Vault asset
* deltas. Calls to `swap` cannot be
* simulated directly, but an equivalent `batchSwap` call can and will yield
* the exact same result.
*
* Each element in the array corresponds to the asset at the same index, and
* indicates the number of tokens (or ETH)
* the Vault would take from the sender (if positive) or send to the
* recipient (if negative). The arguments it
* receives are the same that an equivalent `batchSwap` call would receive.
*
* Unlike `batchSwap`, this function performs no checks on the sender or
* recipient field in the `funds` struct.
* This makes it suitable to be called by off-chain applications via
* eth_call without needing to hold tokens,
* approve them for the Vault, or even know a user's address.
*
* Note that this function is not 'view' (due to implementation details):
* the client code must explicitly execute
* eth_call instead of eth_sendTransaction.
*/
function queryBatchSwap(
SwapKind kind,
BatchSwapStep[] memory swaps,
@@ -315,11 +384,8 @@ interface IVault {
function setPaused(bool paused) external;
function setRelayerApproval(
address sender,
address relayer,
bool approved
) external;
function setRelayerApproval(address sender, address relayer, bool approved)
external;
/**
* @dev Performs a swap with a single Pool.
@@ -345,7 +411,7 @@ interface IVault {
) external payable returns (uint256);
receive() external payable;
function getPoolTokens(bytes32 poolId)
external
view
@@ -356,8 +422,8 @@ interface IVault {
);
enum SwapKind
/// The number of tokens to send to the Pool is known
{
/// The number of tokens to send to the Pool is known
GIVEN_IN,
/// The number of tokens to take from the Pool is known
GIVEN_OUT