Implement adapter and test templates

This commit is contained in:
pistomat
2023-12-09 19:46:02 +01:00
parent 8af168a774
commit 1118502162
8 changed files with 105 additions and 17 deletions

View File

@@ -3,14 +3,13 @@ pragma experimental ABIEncoderV2;
pragma solidity ^0.8.13;
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
import "forge-std/Test.sol";
// Maximum Swap In/Out Ratio - 0.3
// https://balancer.gitbook.io/balancer/core-concepts/protocol/limitations#v2-limits
uint256 constant RESERVE_LIMIT_FACTOR = 4;
uint256 constant SWAP_DEADLINE_SEC = 1000;
contract BalancerV2SwapAdapter is ISwapAdapter, Test {
contract BalancerV2SwapAdapter is ISwapAdapter {
IVault immutable vault;
constructor(address payable vault_) {

View File

@@ -68,7 +68,7 @@ interface ISwapAdapter is ISwapAdapterTypes {
/// @notice Retrieves the limits for each token.
/// @dev Retrieve the maximum limits of a token that can be traded. The
/// limit is reached when the change in the received amounts is zero or
/// close to zero or when the swap fails because of the pools restrictions.
/// close to zero or when the swap fails because of the pools restrictions.
/// Overestimate if in doubt rather than underestimate. The
/// swap function should not error with `LimitExceeded` if called with
/// amounts below the limit.

View File

@@ -0,0 +1,58 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma experimental ABIEncoderV2;
pragma solidity ^0.8.13;
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
/// @title TemplateSwapAdapter
/// @dev This is a template for a swap adapter.
/// Rename it to your own protocol's name and implement it according to the
/// specification.
contract TemplateSwapAdapter is ISwapAdapter {
function price(
bytes32 _poolId,
IERC20 _sellToken,
IERC20 _buyToken,
uint256[] memory _specifiedAmounts
) external view override returns (Fraction[] memory _prices) {
revert NotImplemented("TemplateSwapAdapter.price");
}
function swap(
bytes32 poolId,
IERC20 sellToken,
IERC20 buyToken,
OrderSide side,
uint256 specifiedAmount
) external returns (Trade memory trade) {
revert NotImplemented("TemplateSwapAdapter.swap");
}
function getLimits(bytes32 poolId, IERC20 sellToken, IERC20 buyToken)
external
returns (uint256[] memory limits)
{
revert NotImplemented("TemplateSwapAdapter.getLimits");
}
function getCapabilities(bytes32 poolId, IERC20 sellToken, IERC20 buyToken)
external
returns (Capability[] memory capabilities)
{
revert NotImplemented("TemplateSwapAdapter.getCapabilities");
}
function getTokens(bytes32 poolId)
external
returns (IERC20[] memory tokens)
{
revert NotImplemented("TemplateSwapAdapter.getTokens");
}
function getPoolIds(uint256 offset, uint256 limit)
external
returns (bytes32[] memory ids)
{
revert NotImplemented("TemplateSwapAdapter.getPoolIds");
}
}

View File

@@ -3,7 +3,7 @@ pragma solidity ^0.8.13;
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
// Uniswap handles arbirary amounts, but we limit the amount to 10x just in case
// Uniswap handles arbirary amounts, but we limit the amount to 10x just in case
uint256 constant RESERVE_LIMIT_FACTOR = 10;
contract UniswapV2SwapAdapter is ISwapAdapter {
@@ -119,7 +119,8 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
return amountOut;
}
/// @notice Given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
/// @notice Given an input amount of an asset and pair reserves, returns the
/// maximum output amount of the other asset
/// @param amountIn The amount of the token being sold.
/// @param reserveIn The reserve of the token being sold.
/// @param reserveOut The reserve of the token being bought.
@@ -173,7 +174,8 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
return amount;
}
/// @notice Given an output amount of an asset and pair reserves, returns a required input amount of the other asset
/// @notice Given an output amount of an asset and pair reserves, returns a
/// required input amount of the other asset
/// @param amountOut The amount of the token being bought.
/// @param reserveIn The reserve of the token being sold.
/// @param reserveOut The reserve of the token being bought.