8
.github/workflows/evm.yml
vendored
8
.github/workflows/evm.yml
vendored
@@ -32,10 +32,16 @@ jobs:
|
|||||||
forge build --sizes
|
forge build --sizes
|
||||||
id: build
|
id: build
|
||||||
|
|
||||||
|
- name: Run Forge format check
|
||||||
|
run: |
|
||||||
|
forge --version
|
||||||
|
forge fmt --check
|
||||||
|
id: format
|
||||||
|
|
||||||
- name: Run Forge tests
|
- name: Run Forge tests
|
||||||
run: |
|
run: |
|
||||||
cd evm
|
cd evm
|
||||||
forge test -vvv
|
forge test -vvv
|
||||||
id: test
|
id: test
|
||||||
env:
|
env:
|
||||||
ETH_RPC_URL: ${{ secrets.ETH_RPC_URL }}
|
ETH_RPC_URL: ${{ secrets.ETH_RPC_URL }}
|
||||||
@@ -43,7 +43,7 @@ contract:
|
|||||||
instances:
|
instances:
|
||||||
- chain:
|
- chain:
|
||||||
name: mainnet
|
name: mainnet
|
||||||
id: 0
|
id: 1
|
||||||
# Arguments passed to the constructor when building the contract
|
# Arguments passed to the constructor when building the contract
|
||||||
arguments:
|
arguments:
|
||||||
- "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"
|
- "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"
|
||||||
@@ -57,8 +57,8 @@ tests:
|
|||||||
buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
|
buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
|
||||||
block: 17000000
|
block: 17000000
|
||||||
chain:
|
chain:
|
||||||
id: 0
|
|
||||||
name: mainnet
|
name: mainnet
|
||||||
|
id: 1
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Price (optional)
|
#### Price (optional)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
pragma experimental ABIEncoderV2;
|
|
||||||
pragma solidity ^0.8.13;
|
pragma solidity ^0.8.13;
|
||||||
|
|
||||||
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
|
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
|
||||||
@@ -478,9 +477,8 @@ interface IVault {
|
|||||||
uint256 lastChangeBlock
|
uint256 lastChangeBlock
|
||||||
);
|
);
|
||||||
|
|
||||||
enum SwapKind
|
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,
|
GIVEN_IN,
|
||||||
/// The number of tokens to take from the Pool is known
|
/// The number of tokens to take from the Pool is known
|
||||||
GIVEN_OUT
|
GIVEN_OUT
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# information about the author helps us reach out in case of issues.
|
# information about the author helps us reach out in case of issues.
|
||||||
author:
|
author:
|
||||||
name: Propellerheads.xyz
|
name: Matt
|
||||||
email: pistomat@propellerheads.xyz
|
email: pistomat@propellerheads.xyz
|
||||||
|
|
||||||
# Protocol Constants
|
# Protocol Constants
|
||||||
@@ -19,7 +19,7 @@ contract: BalancerV2SwapAdapter.sol
|
|||||||
instances:
|
instances:
|
||||||
- chain:
|
- chain:
|
||||||
name: mainnet
|
name: mainnet
|
||||||
id: 0
|
id: 1
|
||||||
arguments:
|
arguments:
|
||||||
- "0xBA12222222228d8Ba445958a75a0704d566BF2C8"
|
- "0xBA12222222228d8Ba445958a75a0704d566BF2C8"
|
||||||
|
|
||||||
@@ -27,10 +27,10 @@ instances:
|
|||||||
# getTokens are not implemented.
|
# getTokens are not implemented.
|
||||||
tests:
|
tests:
|
||||||
instances:
|
instances:
|
||||||
- pool_id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"
|
- pool_id: "0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014"
|
||||||
sell_token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
sell_token: "0xba100000625a3754423978a60c9317c58a424e3D"
|
||||||
buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
|
buy_token: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
|
||||||
block: 17000000
|
block: 17000000
|
||||||
chain:
|
chain:
|
||||||
id: 0
|
|
||||||
name: mainnet
|
name: mainnet
|
||||||
|
id: 1
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ pragma solidity ^0.8.13;
|
|||||||
|
|
||||||
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
|
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
|
||||||
import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
|
import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
|
||||||
import {SafeERC20} from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
|
import {SafeERC20} from
|
||||||
|
"openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||||
|
|
||||||
/// @dev Integral submitted deadline of 3600 seconds (1 hour) to Paraswap, but it is not strictly necessary to be this long
|
/// @dev Integral submitted deadline of 3600 seconds (1 hour) to Paraswap, but
|
||||||
/// as the contract allows less durations, we use 1000 seconds (15 minutes) as a deadline
|
/// it is not strictly necessary to be this long
|
||||||
|
/// as the contract allows less durations, we use 1000 seconds (15 minutes) as a
|
||||||
|
/// deadline
|
||||||
uint256 constant SWAP_DEADLINE_SEC = 1000;
|
uint256 constant SWAP_DEADLINE_SEC = 1000;
|
||||||
uint256 constant STANDARD_TOKEN_DECIMALS = 10**18;
|
uint256 constant STANDARD_TOKEN_DECIMALS = 10 ** 18;
|
||||||
|
|
||||||
/// @title Integral Swap Adapter
|
/// @title Integral Swap Adapter
|
||||||
contract IntegralSwapAdapter is ISwapAdapter {
|
contract IntegralSwapAdapter is ISwapAdapter {
|
||||||
@@ -21,12 +24,18 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// @inheritdoc ISwapAdapter
|
/// @inheritdoc ISwapAdapter
|
||||||
/// @dev Integral always relies on a single pool linked to the factory to map two pairs, and does not use routing
|
/// @dev Integral always relies on a single pool linked to the factory to
|
||||||
/// we can then use getPriceByTokenAddresses() instead of getPriceByPairAddresses()
|
/// map two pairs, and does not use routing
|
||||||
/// as they both return the same value and the first also handles the order of tokens inside.
|
/// we can then use getPriceByTokenAddresses() instead of
|
||||||
/// @dev Since the price of a token is determined externally by Integral Oracles and not by reserves
|
/// getPriceByPairAddresses()
|
||||||
/// it will always be the same (pre and post trade) and independent of the amounts swapped,
|
/// as they both return the same value and the first also handles the order
|
||||||
/// but we still return an array of length=specifiedAmounts.length with same values to make sure the return value is the expected from caller.
|
/// of tokens inside.
|
||||||
|
/// @dev Since the price of a token is determined externally by Integral
|
||||||
|
/// Oracles and not by reserves
|
||||||
|
/// it will always be the same (pre and post trade) and independent of the
|
||||||
|
/// amounts swapped,
|
||||||
|
/// but we still return an array of length=specifiedAmounts.length with same
|
||||||
|
/// values to make sure the return value is the expected from caller.
|
||||||
function price(
|
function price(
|
||||||
bytes32,
|
bytes32,
|
||||||
IERC20 _sellToken,
|
IERC20 _sellToken,
|
||||||
@@ -34,8 +43,9 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
uint256[] memory _specifiedAmounts
|
uint256[] memory _specifiedAmounts
|
||||||
) external view override returns (Fraction[] memory _prices) {
|
) external view override returns (Fraction[] memory _prices) {
|
||||||
_prices = new Fraction[](_specifiedAmounts.length);
|
_prices = new Fraction[](_specifiedAmounts.length);
|
||||||
Fraction memory price = getPriceAt(address(_sellToken), address(_buyToken));
|
Fraction memory price =
|
||||||
|
getPriceAt(address(_sellToken), address(_buyToken));
|
||||||
|
|
||||||
for (uint256 i = 0; i < _specifiedAmounts.length; i++) {
|
for (uint256 i = 0; i < _specifiedAmounts.length; i++) {
|
||||||
_prices[i] = price;
|
_prices[i] = price;
|
||||||
}
|
}
|
||||||
@@ -54,12 +64,12 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint256 gasBefore = gasleft();
|
uint256 gasBefore = gasleft();
|
||||||
if (side == OrderSide.Sell) { // sell
|
if (side == OrderSide.Sell) {
|
||||||
trade.calculatedAmount =
|
// sell
|
||||||
sell(sellToken, buyToken, specifiedAmount);
|
trade.calculatedAmount = sell(sellToken, buyToken, specifiedAmount);
|
||||||
} else { // buy
|
} else {
|
||||||
trade.calculatedAmount =
|
// buy
|
||||||
buy(sellToken, buyToken, specifiedAmount);
|
trade.calculatedAmount = buy(sellToken, buyToken, specifiedAmount);
|
||||||
}
|
}
|
||||||
trade.gasUsed = gasBefore - gasleft();
|
trade.gasUsed = gasBefore - gasleft();
|
||||||
trade.price = getPriceAt(address(sellToken), address(buyToken));
|
trade.price = getPriceAt(address(sellToken), address(buyToken));
|
||||||
@@ -72,21 +82,17 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
override
|
override
|
||||||
returns (uint256[] memory limits)
|
returns (uint256[] memory limits)
|
||||||
{
|
{
|
||||||
(
|
(,,, uint256 limitMax0,, uint256 limitMax1) =
|
||||||
,
|
relayer.getPoolState(address(sellToken), address(buyToken));
|
||||||
,
|
|
||||||
,
|
|
||||||
uint256 limitMax0,
|
|
||||||
,
|
|
||||||
uint256 limitMax1
|
|
||||||
) = relayer.getPoolState(address(sellToken), address(buyToken));
|
|
||||||
|
|
||||||
limits = new uint256[](2);
|
limits = new uint256[](2);
|
||||||
limits[0] = limitMax0;
|
limits[0] = limitMax0;
|
||||||
limits[1] = limitMax1;
|
limits[1] = limitMax1;
|
||||||
/**
|
/**
|
||||||
* @dev minLimits in integral are the args: 2(for sellToken, the one before limitMax0)
|
* @dev minLimits in integral are the args: 2(for sellToken, the one
|
||||||
* and 4(for buyToken, the one before limitMax1) of the function relayer.getPoolState(sellToken, buyToken);
|
* before limitMax0)
|
||||||
|
* and 4(for buyToken, the one before limitMax1) of the function
|
||||||
|
* relayer.getPoolState(sellToken, buyToken);
|
||||||
* an implementation of them can be found in the test of this adapter
|
* an implementation of them can be found in the test of this adapter
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
@@ -141,12 +147,12 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
/// @param buyToken The address of the token being bought.
|
/// @param buyToken The address of the token being bought.
|
||||||
/// @param amount The amount to be traded.
|
/// @param amount The amount to be traded.
|
||||||
/// @return uint256 The amount of tokens received.
|
/// @return uint256 The amount of tokens received.
|
||||||
function sell(
|
function sell(IERC20 sellToken, IERC20 buyToken, uint256 amount)
|
||||||
IERC20 sellToken,
|
internal
|
||||||
IERC20 buyToken,
|
returns (uint256)
|
||||||
uint256 amount
|
{
|
||||||
) internal returns (uint256) {
|
uint256 amountOut =
|
||||||
uint256 amountOut = relayer.quoteSell(address(sellToken), address(buyToken), amount);
|
relayer.quoteSell(address(sellToken), address(buyToken), amount);
|
||||||
if (amountOut == 0) {
|
if (amountOut == 0) {
|
||||||
revert Unavailable("AmountOut is zero!");
|
revert Unavailable("AmountOut is zero!");
|
||||||
}
|
}
|
||||||
@@ -154,15 +160,17 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
sellToken.safeTransferFrom(msg.sender, address(this), amount);
|
sellToken.safeTransferFrom(msg.sender, address(this), amount);
|
||||||
sellToken.safeIncreaseAllowance(address(relayer), amount);
|
sellToken.safeIncreaseAllowance(address(relayer), amount);
|
||||||
|
|
||||||
relayer.sell(ITwapRelayer.SellParams({
|
relayer.sell(
|
||||||
tokenIn: address(sellToken),
|
ITwapRelayer.SellParams({
|
||||||
tokenOut: address(buyToken),
|
tokenIn: address(sellToken),
|
||||||
wrapUnwrap: false,
|
tokenOut: address(buyToken),
|
||||||
to: msg.sender,
|
wrapUnwrap: false,
|
||||||
submitDeadline: uint32(block.timestamp + SWAP_DEADLINE_SEC),
|
to: msg.sender,
|
||||||
amountIn: amount,
|
submitDeadline: uint32(block.timestamp + SWAP_DEADLINE_SEC),
|
||||||
amountOutMin: amountOut
|
amountIn: amount,
|
||||||
}));
|
amountOutMin: amountOut
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
return amountOut;
|
return amountOut;
|
||||||
}
|
}
|
||||||
@@ -172,12 +180,13 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
/// @param buyToken The address of the token being bought.
|
/// @param buyToken The address of the token being bought.
|
||||||
/// @param amountBought The amount of buyToken tokens to buy.
|
/// @param amountBought The amount of buyToken tokens to buy.
|
||||||
/// @return uint256 The amount of tokens received.
|
/// @return uint256 The amount of tokens received.
|
||||||
function buy(
|
function buy(IERC20 sellToken, IERC20 buyToken, uint256 amountBought)
|
||||||
IERC20 sellToken,
|
internal
|
||||||
IERC20 buyToken,
|
returns (uint256)
|
||||||
uint256 amountBought
|
{
|
||||||
) internal returns (uint256) {
|
uint256 amountIn = relayer.quoteBuy(
|
||||||
uint256 amountIn = relayer.quoteBuy(address(sellToken), address(buyToken), amountBought);
|
address(sellToken), address(buyToken), amountBought
|
||||||
|
);
|
||||||
if (amountIn == 0) {
|
if (amountIn == 0) {
|
||||||
revert Unavailable("AmountIn is zero!");
|
revert Unavailable("AmountIn is zero!");
|
||||||
}
|
}
|
||||||
@@ -185,15 +194,17 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
sellToken.safeTransferFrom(msg.sender, address(this), amountIn);
|
sellToken.safeTransferFrom(msg.sender, address(this), amountIn);
|
||||||
sellToken.safeIncreaseAllowance(address(relayer), amountIn);
|
sellToken.safeIncreaseAllowance(address(relayer), amountIn);
|
||||||
|
|
||||||
relayer.buy(ITwapRelayer.BuyParams({
|
relayer.buy(
|
||||||
tokenIn: address(sellToken),
|
ITwapRelayer.BuyParams({
|
||||||
tokenOut: address(buyToken),
|
tokenIn: address(sellToken),
|
||||||
wrapUnwrap: false,
|
tokenOut: address(buyToken),
|
||||||
to: msg.sender,
|
wrapUnwrap: false,
|
||||||
submitDeadline: uint32(block.timestamp + SWAP_DEADLINE_SEC),
|
to: msg.sender,
|
||||||
amountInMax: amountIn,
|
submitDeadline: uint32(block.timestamp + SWAP_DEADLINE_SEC),
|
||||||
amountOut: amountBought
|
amountInMax: amountIn,
|
||||||
}));
|
amountOut: amountBought
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
return amountIn;
|
return amountIn;
|
||||||
}
|
}
|
||||||
@@ -201,30 +212,43 @@ contract IntegralSwapAdapter is ISwapAdapter {
|
|||||||
/// @notice Get swap price including fee
|
/// @notice Get swap price including fee
|
||||||
/// @param sellToken token to sell
|
/// @param sellToken token to sell
|
||||||
/// @param buyToken token to buy
|
/// @param buyToken token to buy
|
||||||
function getPriceAt(address sellToken, address buyToken) internal view returns(Fraction memory) {
|
function getPriceAt(address sellToken, address buyToken)
|
||||||
uint256 priceWithoutFee = relayer.getPriceByTokenAddresses(address(sellToken), address(buyToken));
|
internal
|
||||||
|
view
|
||||||
|
returns (Fraction memory)
|
||||||
|
{
|
||||||
|
uint256 priceWithoutFee = relayer.getPriceByTokenAddresses(
|
||||||
|
address(sellToken), address(buyToken)
|
||||||
|
);
|
||||||
ITwapFactory factory = ITwapFactory(relayer.factory());
|
ITwapFactory factory = ITwapFactory(relayer.factory());
|
||||||
address pairAddress = factory.getPair(address(sellToken), address(buyToken));
|
address pairAddress =
|
||||||
|
factory.getPair(address(sellToken), address(buyToken));
|
||||||
|
|
||||||
// get swapFee formatted; swapFee is a constant
|
// get swapFee formatted; swapFee is a constant
|
||||||
uint256 swapFeeFormatted = (STANDARD_TOKEN_DECIMALS - relayer.swapFee(pairAddress));
|
uint256 swapFeeFormatted =
|
||||||
|
(STANDARD_TOKEN_DECIMALS - relayer.swapFee(pairAddress));
|
||||||
|
|
||||||
// get token decimals
|
// get token decimals
|
||||||
uint256 sellTokenDecimals = 10**ERC20(sellToken).decimals();
|
uint256 sellTokenDecimals = 10 ** ERC20(sellToken).decimals();
|
||||||
uint256 buyTokenDecimals = 10**ERC20(buyToken).decimals();
|
uint256 buyTokenDecimals = 10 ** ERC20(buyToken).decimals();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev
|
* @dev
|
||||||
* Denominator works as a "standardizer" for the price rather than a reserve value
|
* Denominator works as a "standardizer" for the price rather than a
|
||||||
* as Integral takes prices from oracles and do not operate with reserves;
|
* reserve value
|
||||||
|
* as Integral takes prices from oracles and do not operate with
|
||||||
|
* reserves;
|
||||||
* it is therefore used to maintain integrity for the Fraction division,
|
* it is therefore used to maintain integrity for the Fraction division,
|
||||||
* as numerator and denominator could have different token decimals(es. ETH(18)-USDC(6)).
|
* as numerator and denominator could have different token decimals(es.
|
||||||
* Both numerator and denominator are also multiplied by STANDARD_TOKEN_DECIMALS
|
* ETH(18)-USDC(6)).
|
||||||
|
* Both numerator and denominator are also multiplied by
|
||||||
|
* STANDARD_TOKEN_DECIMALS
|
||||||
* to ensure that precision losses are minimized or null.
|
* to ensure that precision losses are minimized or null.
|
||||||
*/
|
*/
|
||||||
return Fraction(
|
return Fraction(
|
||||||
priceWithoutFee * STANDARD_TOKEN_DECIMALS,
|
priceWithoutFee * STANDARD_TOKEN_DECIMALS,
|
||||||
STANDARD_TOKEN_DECIMALS * sellTokenDecimals * swapFeeFormatted / buyTokenDecimals
|
STANDARD_TOKEN_DECIMALS * sellTokenDecimals * swapFeeFormatted
|
||||||
|
/ buyTokenDecimals
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -276,8 +300,12 @@ interface ITwapRelayer {
|
|||||||
uint256 amountIn,
|
uint256 amountIn,
|
||||||
uint256 indexed delayOrderId
|
uint256 indexed delayOrderId
|
||||||
);
|
);
|
||||||
event RebalanceSellWithOneInch(address indexed oneInchRouter, uint256 gas, bytes data);
|
event RebalanceSellWithOneInch(
|
||||||
event OneInchRouterWhitelisted(address indexed oneInchRouter, bool whitelisted);
|
address indexed oneInchRouter, uint256 gas, bytes data
|
||||||
|
);
|
||||||
|
event OneInchRouterWhitelisted(
|
||||||
|
address indexed oneInchRouter, bool whitelisted
|
||||||
|
);
|
||||||
|
|
||||||
function factory() external pure returns (address);
|
function factory() external pure returns (address);
|
||||||
|
|
||||||
@@ -289,7 +317,10 @@ interface ITwapRelayer {
|
|||||||
|
|
||||||
function rebalancer() external view returns (address);
|
function rebalancer() external view returns (address);
|
||||||
|
|
||||||
function isOneInchRouterWhitelisted(address oneInchRouter) external view returns (bool);
|
function isOneInchRouterWhitelisted(address oneInchRouter)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (bool);
|
||||||
|
|
||||||
function setOwner(address _owner) external;
|
function setOwner(address _owner) external;
|
||||||
|
|
||||||
@@ -309,19 +340,26 @@ interface ITwapRelayer {
|
|||||||
|
|
||||||
function tokenLimitMin(address token) external pure returns (uint256);
|
function tokenLimitMin(address token) external pure returns (uint256);
|
||||||
|
|
||||||
function tokenLimitMaxMultiplier(address token) external pure returns (uint256);
|
function tokenLimitMaxMultiplier(address token)
|
||||||
|
external
|
||||||
|
pure
|
||||||
|
returns (uint256);
|
||||||
|
|
||||||
function tolerance(address pair) external pure returns (uint16);
|
function tolerance(address pair) external pure returns (uint16);
|
||||||
|
|
||||||
function setRebalancer(address _rebalancer) external;
|
function setRebalancer(address _rebalancer) external;
|
||||||
|
|
||||||
function whitelistOneInchRouter(address oneInchRouter, bool whitelisted) external;
|
function whitelistOneInchRouter(address oneInchRouter, bool whitelisted)
|
||||||
|
external;
|
||||||
|
|
||||||
function getTolerance(address pair) external pure returns (uint16);
|
function getTolerance(address pair) external pure returns (uint16);
|
||||||
|
|
||||||
function getTokenLimitMin(address token) external pure returns (uint256);
|
function getTokenLimitMin(address token) external pure returns (uint256);
|
||||||
|
|
||||||
function getTokenLimitMaxMultiplier(address token) external pure returns (uint256);
|
function getTokenLimitMaxMultiplier(address token)
|
||||||
|
external
|
||||||
|
pure
|
||||||
|
returns (uint256);
|
||||||
|
|
||||||
function getTwapInterval(address pair) external pure returns (uint32);
|
function getTwapInterval(address pair) external pure returns (uint32);
|
||||||
|
|
||||||
@@ -335,7 +373,10 @@ interface ITwapRelayer {
|
|||||||
uint32 submitDeadline;
|
uint32 submitDeadline;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sell(SellParams memory sellParams) external payable returns (uint256 orderId);
|
function sell(SellParams memory sellParams)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (uint256 orderId);
|
||||||
|
|
||||||
struct BuyParams {
|
struct BuyParams {
|
||||||
address tokenIn;
|
address tokenIn;
|
||||||
@@ -347,18 +388,20 @@ interface ITwapRelayer {
|
|||||||
uint32 submitDeadline;
|
uint32 submitDeadline;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buy(BuyParams memory buyParams) external payable returns (uint256 orderId);
|
function buy(BuyParams memory buyParams)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (uint256 orderId);
|
||||||
|
|
||||||
function getPriceByPairAddress(address pair, bool inverted)
|
function getPriceByPairAddress(address pair, bool inverted)
|
||||||
external
|
external
|
||||||
view
|
view
|
||||||
returns (
|
returns (uint8 xDecimals, uint8 yDecimals, uint256 price);
|
||||||
uint8 xDecimals,
|
|
||||||
uint8 yDecimals,
|
|
||||||
uint256 price
|
|
||||||
);
|
|
||||||
|
|
||||||
function getPriceByTokenAddresses(address tokenIn, address tokenOut) external view returns (uint256 price);
|
function getPriceByTokenAddresses(address tokenIn, address tokenOut)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (uint256 price);
|
||||||
|
|
||||||
function getPoolState(address token0, address token1)
|
function getPoolState(address token0, address token1)
|
||||||
external
|
external
|
||||||
@@ -372,29 +415,19 @@ interface ITwapRelayer {
|
|||||||
uint256 limitMax1
|
uint256 limitMax1
|
||||||
);
|
);
|
||||||
|
|
||||||
function quoteSell(
|
function quoteSell(address tokenIn, address tokenOut, uint256 amountIn)
|
||||||
address tokenIn,
|
external
|
||||||
address tokenOut,
|
view
|
||||||
uint256 amountIn
|
returns (uint256 amountOut);
|
||||||
) external view returns (uint256 amountOut);
|
|
||||||
|
|
||||||
function quoteBuy(
|
function quoteBuy(address tokenIn, address tokenOut, uint256 amountOut)
|
||||||
address tokenIn,
|
external
|
||||||
address tokenOut,
|
view
|
||||||
uint256 amountOut
|
returns (uint256 amountIn);
|
||||||
) external view returns (uint256 amountIn);
|
|
||||||
|
|
||||||
function approve(
|
function approve(address token, uint256 amount, address to) external;
|
||||||
address token,
|
|
||||||
uint256 amount,
|
|
||||||
address to
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function withdraw(
|
function withdraw(address token, uint256 amount, address to) external;
|
||||||
address token,
|
|
||||||
uint256 amount,
|
|
||||||
address to
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function rebalanceSellWithDelay(
|
function rebalanceSellWithDelay(
|
||||||
address tokenIn,
|
address tokenIn,
|
||||||
@@ -412,12 +445,17 @@ interface ITwapRelayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface ITwapFactory {
|
interface ITwapFactory {
|
||||||
event PairCreated(address indexed token0, address indexed token1, address pair, uint256);
|
event PairCreated(
|
||||||
|
address indexed token0, address indexed token1, address pair, uint256
|
||||||
|
);
|
||||||
event OwnerSet(address owner);
|
event OwnerSet(address owner);
|
||||||
|
|
||||||
function owner() external view returns (address);
|
function owner() external view returns (address);
|
||||||
|
|
||||||
function getPair(address tokenA, address tokenB) external view returns (address pair);
|
function getPair(address tokenA, address tokenB)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (address pair);
|
||||||
|
|
||||||
function allPairs(uint256) external view returns (address pair);
|
function allPairs(uint256) external view returns (address pair);
|
||||||
|
|
||||||
@@ -432,41 +470,19 @@ interface ITwapFactory {
|
|||||||
|
|
||||||
function setOwner(address) external;
|
function setOwner(address) external;
|
||||||
|
|
||||||
function setMintFee(
|
function setMintFee(address tokenA, address tokenB, uint256 fee) external;
|
||||||
address tokenA,
|
|
||||||
address tokenB,
|
|
||||||
uint256 fee
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function setBurnFee(
|
function setBurnFee(address tokenA, address tokenB, uint256 fee) external;
|
||||||
address tokenA,
|
|
||||||
address tokenB,
|
|
||||||
uint256 fee
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function setSwapFee(
|
function setSwapFee(address tokenA, address tokenB, uint256 fee) external;
|
||||||
address tokenA,
|
|
||||||
address tokenB,
|
|
||||||
uint256 fee
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function setOracle(
|
function setOracle(address tokenA, address tokenB, address oracle)
|
||||||
address tokenA,
|
external;
|
||||||
address tokenB,
|
|
||||||
address oracle
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function setTrader(
|
function setTrader(address tokenA, address tokenB, address trader)
|
||||||
address tokenA,
|
external;
|
||||||
address tokenB,
|
|
||||||
address trader
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function collect(
|
function collect(address tokenA, address tokenB, address to) external;
|
||||||
address tokenA,
|
|
||||||
address tokenB,
|
|
||||||
address to
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function withdraw(
|
function withdraw(
|
||||||
address tokenA,
|
address tokenA,
|
||||||
@@ -491,20 +507,39 @@ interface ITwapERC20 is IERC20 {
|
|||||||
bytes32 s
|
bytes32 s
|
||||||
) external;
|
) external;
|
||||||
|
|
||||||
function increaseAllowance(address spender, uint256 addedValue) external returns (bool);
|
function increaseAllowance(address spender, uint256 addedValue)
|
||||||
|
external
|
||||||
|
returns (bool);
|
||||||
|
|
||||||
function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool);
|
function decreaseAllowance(address spender, uint256 subtractedValue)
|
||||||
|
external
|
||||||
|
returns (bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IReserves {
|
interface IReserves {
|
||||||
function getReserves() external view returns (uint112 reserve0, uint112 reserve1);
|
function getReserves()
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (uint112 reserve0, uint112 reserve1);
|
||||||
|
|
||||||
function getFees() external view returns (uint256 fee0, uint256 fee1);
|
function getFees() external view returns (uint256 fee0, uint256 fee1);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ITwapPair is ITwapERC20, IReserves {
|
interface ITwapPair is ITwapERC20, IReserves {
|
||||||
event Mint(address indexed sender, uint256 amount0In, uint256 amount1In, uint256 liquidityOut, address indexed to);
|
event Mint(
|
||||||
event Burn(address indexed sender, uint256 amount0Out, uint256 amount1Out, uint256 liquidityIn, address indexed to);
|
address indexed sender,
|
||||||
|
uint256 amount0In,
|
||||||
|
uint256 amount1In,
|
||||||
|
uint256 liquidityOut,
|
||||||
|
address indexed to
|
||||||
|
);
|
||||||
|
event Burn(
|
||||||
|
address indexed sender,
|
||||||
|
uint256 amount0Out,
|
||||||
|
uint256 amount1Out,
|
||||||
|
uint256 liquidityIn,
|
||||||
|
address indexed to
|
||||||
|
);
|
||||||
event Swap(
|
event Swap(
|
||||||
address indexed sender,
|
address indexed sender,
|
||||||
uint256 amount0In,
|
uint256 amount0In,
|
||||||
@@ -541,7 +576,9 @@ interface ITwapPair is ITwapERC20, IReserves {
|
|||||||
|
|
||||||
function setBurnFee(uint256 fee) external;
|
function setBurnFee(uint256 fee) external;
|
||||||
|
|
||||||
function burn(address to) external returns (uint256 amount0, uint256 amount1);
|
function burn(address to)
|
||||||
|
external
|
||||||
|
returns (uint256 amount0, uint256 amount1);
|
||||||
|
|
||||||
function swapFee() external view returns (uint256);
|
function swapFee() external view returns (uint256);
|
||||||
|
|
||||||
@@ -569,15 +606,33 @@ interface ITwapPair is ITwapERC20, IReserves {
|
|||||||
address _trader
|
address _trader
|
||||||
) external;
|
) external;
|
||||||
|
|
||||||
function getSwapAmount0In(uint256 amount1Out, bytes calldata data) external view returns (uint256 swapAmount0In);
|
function getSwapAmount0In(uint256 amount1Out, bytes calldata data)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (uint256 swapAmount0In);
|
||||||
|
|
||||||
function getSwapAmount1In(uint256 amount0Out, bytes calldata data) external view returns (uint256 swapAmount1In);
|
function getSwapAmount1In(uint256 amount0Out, bytes calldata data)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (uint256 swapAmount1In);
|
||||||
|
|
||||||
function getSwapAmount0Out(uint256 amount1In, bytes calldata data) external view returns (uint256 swapAmount0Out);
|
function getSwapAmount0Out(uint256 amount1In, bytes calldata data)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (uint256 swapAmount0Out);
|
||||||
|
|
||||||
function getSwapAmount1Out(uint256 amount0In, bytes calldata data) external view returns (uint256 swapAmount1Out);
|
function getSwapAmount1Out(uint256 amount0In, bytes calldata data)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (uint256 swapAmount1Out);
|
||||||
|
|
||||||
function getDepositAmount0In(uint256 amount0, bytes calldata data) external view returns (uint256 depositAmount0In);
|
function getDepositAmount0In(uint256 amount0, bytes calldata data)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (uint256 depositAmount0In);
|
||||||
|
|
||||||
function getDepositAmount1In(uint256 amount1, bytes calldata data) external view returns (uint256 depositAmount1In);
|
function getDepositAmount1In(uint256 amount1, bytes calldata data)
|
||||||
|
external
|
||||||
|
view
|
||||||
|
returns (uint256 depositAmount1In);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
pragma experimental ABIEncoderV2;
|
|
||||||
pragma solidity ^0.8.13;
|
pragma solidity ^0.8.13;
|
||||||
|
|
||||||
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
|
import {IERC20, ISwapAdapter} from "src/interfaces/ISwapAdapter.sol";
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ contract: TemplateSwapAdapter.sol
|
|||||||
instances:
|
instances:
|
||||||
- chain:
|
- chain:
|
||||||
name: mainnet
|
name: mainnet
|
||||||
id: 0
|
id: 1
|
||||||
arguments:
|
arguments:
|
||||||
- "0xBA12222222228d8Ba445958a75a0704d566BF2C8"
|
- "0xBA12222222228d8Ba445958a75a0704d566BF2C8"
|
||||||
|
|
||||||
@@ -32,5 +32,5 @@ tests:
|
|||||||
buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
|
buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
|
||||||
block: 17000000
|
block: 17000000
|
||||||
chain:
|
chain:
|
||||||
id: 0
|
|
||||||
name: mainnet
|
name: mainnet
|
||||||
|
id: 1
|
||||||
|
|||||||
@@ -87,10 +87,9 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
|
|||||||
buy(pair, sellToken, zero2one, r0, r1, specifiedAmount);
|
buy(pair, sellToken, zero2one, r0, r1, specifiedAmount);
|
||||||
}
|
}
|
||||||
trade.gasUsed = gasBefore - gasleft();
|
trade.gasUsed = gasBefore - gasleft();
|
||||||
if(side == OrderSide.Sell) {
|
if (side == OrderSide.Sell) {
|
||||||
trade.price = getPriceAt(specifiedAmount, r0, r1);
|
trade.price = getPriceAt(specifiedAmount, r0, r1);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
trade.price = getPriceAt(trade.calculatedAmount, r0, r1);
|
trade.price = getPriceAt(trade.calculatedAmount, r0, r1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# information about the author helps us reach out in case of issues.
|
# information about the author helps us reach out in case of issues.
|
||||||
author:
|
author:
|
||||||
name: Propellerheads.xyz
|
name: Alan
|
||||||
email: alan@propellerheads.xyz
|
email: alan@propellerheads.xyz
|
||||||
|
|
||||||
# Protocol Constants
|
# Protocol Constants
|
||||||
@@ -19,18 +19,6 @@ contract: UniswapV2SwapAdapter.sol
|
|||||||
instances:
|
instances:
|
||||||
- chain:
|
- chain:
|
||||||
name: mainnet
|
name: mainnet
|
||||||
id: 0
|
id: 1
|
||||||
arguments:
|
arguments:
|
||||||
- "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"
|
- "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"
|
||||||
|
|
||||||
# Specify some automatic test cases in case getPoolIds and
|
|
||||||
# getTokens are not implemented.
|
|
||||||
tests:
|
|
||||||
instances:
|
|
||||||
- pool_id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"
|
|
||||||
sell_token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
|
||||||
buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
|
|
||||||
block: 17000000
|
|
||||||
chain:
|
|
||||||
id: 0
|
|
||||||
name: mainnet
|
|
||||||
|
|||||||
@@ -1,103 +1,97 @@
|
|||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
pragma solidity ^0.8.13;
|
pragma solidity ^0.8.13;
|
||||||
|
|
||||||
import "forge-std/Test.sol";
|
import "forge-std/Test.sol";
|
||||||
import "openzeppelin-contracts/contracts/interfaces/IERC20.sol";
|
import "openzeppelin-contracts/contracts/interfaces/IERC20.sol";
|
||||||
import "src/interfaces/ISwapAdapterTypes.sol";
|
import "src/interfaces/ISwapAdapterTypes.sol";
|
||||||
import "src/libraries/FractionMath.sol";
|
import "src/libraries/FractionMath.sol";
|
||||||
import "src/integral/IntegralSwapAdapter.sol";
|
import "src/integral/IntegralSwapAdapter.sol";
|
||||||
|
|
||||||
contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
|
contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
|
||||||
using FractionMath for Fraction;
|
using FractionMath for Fraction;
|
||||||
|
|
||||||
IntegralSwapAdapter adapter;
|
IntegralSwapAdapter adapter;
|
||||||
ITwapRelayer relayer;
|
ITwapRelayer relayer;
|
||||||
IERC20 constant WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
|
IERC20 constant WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
|
||||||
IERC20 constant USDC = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
|
IERC20 constant USDC = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
|
||||||
address constant USDC_WETH_PAIR =
|
address constant USDC_WETH_PAIR = 0x2fe16Dd18bba26e457B7dD2080d5674312b026a2;
|
||||||
0x2fe16Dd18bba26e457B7dD2080d5674312b026a2;
|
address constant relayerAddress = 0xd17b3c9784510E33cD5B87b490E79253BcD81e2E;
|
||||||
address constant relayerAddress =
|
|
||||||
0xd17b3c9784510E33cD5B87b490E79253BcD81e2E;
|
|
||||||
|
|
||||||
uint256 constant TEST_ITERATIONS = 100;
|
uint256 constant TEST_ITERATIONS = 100;
|
||||||
|
|
||||||
function setUp() public {
|
function setUp() public {
|
||||||
uint256 forkBlock = 18835309;
|
uint256 forkBlock = 18835309;
|
||||||
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
||||||
adapter = new IntegralSwapAdapter(relayerAddress);
|
adapter = new IntegralSwapAdapter(relayerAddress);
|
||||||
relayer = ITwapRelayer(relayerAddress);
|
relayer = ITwapRelayer(relayerAddress);
|
||||||
|
|
||||||
vm.label(address(WETH), "WETH");
|
vm.label(address(WETH), "WETH");
|
||||||
vm.label(address(USDC), "USDC");
|
vm.label(address(USDC), "USDC");
|
||||||
vm.label(address(USDC_WETH_PAIR), "USDC_WETH_PAIR");
|
vm.label(address(USDC_WETH_PAIR), "USDC_WETH_PAIR");
|
||||||
}
|
}
|
||||||
|
|
||||||
function testPriceFuzzIntegral(uint256 amount0, uint256 amount1) public {
|
function testPriceFuzzIntegral(uint256 amount0, uint256 amount1) public {
|
||||||
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
||||||
uint256[] memory limits = adapter.getLimits(pair, USDC, WETH);
|
uint256[] memory limits = adapter.getLimits(pair, USDC, WETH);
|
||||||
vm.assume(amount0 < limits[0]);
|
vm.assume(amount0 < limits[0]);
|
||||||
vm.assume(amount1 < limits[1]);
|
vm.assume(amount1 < limits[1]);
|
||||||
|
|
||||||
uint256[] memory amounts = new uint256[](2);
|
uint256[] memory amounts = new uint256[](2);
|
||||||
amounts[0] = amount0;
|
amounts[0] = amount0;
|
||||||
amounts[1] = amount1;
|
amounts[1] = amount1;
|
||||||
|
|
||||||
Fraction[] memory prices = adapter.price(pair, WETH, USDC, amounts);
|
Fraction[] memory prices = adapter.price(pair, WETH, USDC, amounts);
|
||||||
|
|
||||||
for (uint256 i = 0; i < prices.length; i++) {
|
for (uint256 i = 0; i < prices.length; i++) {
|
||||||
assertGt(prices[i].numerator, 0);
|
assertGt(prices[i].numerator, 0);
|
||||||
assertGt(prices[i].denominator, 0);
|
assertGt(prices[i].denominator, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @dev Since TwapRelayer's calculateAmountOut function is internal, and using quoteSell would
|
/// @dev Since TwapRelayer's calculateAmountOut function is internal, and
|
||||||
|
/// using quoteSell would
|
||||||
/// revert the transaction if calculateAmountOut is not enough,
|
/// revert the transaction if calculateAmountOut is not enough,
|
||||||
/// we need a threshold to cover this internal amount, applied to
|
/// we need a threshold to cover this internal amount, applied to
|
||||||
function testSwapFuzzIntegral(uint256 specifiedAmount, bool isBuy) public {
|
function testSwapFuzzIntegral(uint256 specifiedAmount, bool isBuy) public {
|
||||||
OrderSide side = isBuy ? OrderSide.Buy : OrderSide.Sell;
|
OrderSide side = isBuy ? OrderSide.Buy : OrderSide.Sell;
|
||||||
|
|
||||||
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
||||||
uint256[] memory limits = new uint256[](2);
|
uint256[] memory limits = new uint256[](2);
|
||||||
uint256[] memory limitsMin = new uint256[](2);
|
uint256[] memory limitsMin = new uint256[](2);
|
||||||
|
|
||||||
if (side == OrderSide.Buy) {
|
if (side == OrderSide.Buy) {
|
||||||
limits = adapter.getLimits(pair, USDC, WETH);
|
limits = adapter.getLimits(pair, USDC, WETH);
|
||||||
vm.assume(specifiedAmount < limits[1]);
|
vm.assume(specifiedAmount < limits[1]);
|
||||||
|
|
||||||
limitsMin = getMinLimits(USDC, WETH);
|
limitsMin = getMinLimits(USDC, WETH);
|
||||||
vm.assume(specifiedAmount > limitsMin[1] * 115 / 100);
|
vm.assume(specifiedAmount > limitsMin[1] * 115 / 100);
|
||||||
|
|
||||||
deal(address(USDC), address(this), type(uint256).max);
|
deal(address(USDC), address(this), type(uint256).max);
|
||||||
USDC.approve(address(adapter), type(uint256).max);
|
USDC.approve(address(adapter), type(uint256).max);
|
||||||
} else {
|
} else {
|
||||||
limits = adapter.getLimits(pair, USDC, WETH);
|
limits = adapter.getLimits(pair, USDC, WETH);
|
||||||
vm.assume(specifiedAmount < limits[0]);
|
vm.assume(specifiedAmount < limits[0]);
|
||||||
|
|
||||||
limitsMin = getMinLimits(USDC, WETH);
|
limitsMin = getMinLimits(USDC, WETH);
|
||||||
vm.assume(specifiedAmount > limitsMin[0] * 115 / 100);
|
vm.assume(specifiedAmount > limitsMin[0] * 115 / 100);
|
||||||
|
|
||||||
deal(address(USDC), address(this), type(uint256).max);
|
deal(address(USDC), address(this), type(uint256).max);
|
||||||
USDC.approve(address(adapter), specifiedAmount);
|
USDC.approve(address(adapter), specifiedAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint256 usdc_balance_before = USDC.balanceOf(address(this));
|
uint256 usdc_balance_before = USDC.balanceOf(address(this));
|
||||||
uint256 weth_balance_before = WETH.balanceOf(address(this));
|
uint256 weth_balance_before = WETH.balanceOf(address(this));
|
||||||
|
|
||||||
Trade memory trade = adapter.swap(
|
Trade memory trade =
|
||||||
pair,
|
adapter.swap(pair, USDC, WETH, side, specifiedAmount);
|
||||||
USDC,
|
|
||||||
WETH,
|
|
||||||
side,
|
|
||||||
specifiedAmount
|
|
||||||
);
|
|
||||||
|
|
||||||
if (trade.calculatedAmount > 0) {
|
if (trade.calculatedAmount > 0) {
|
||||||
if (side == OrderSide.Buy) {
|
if (side == OrderSide.Buy) {
|
||||||
assertEq(
|
assertEq(
|
||||||
specifiedAmount,
|
specifiedAmount,
|
||||||
WETH.balanceOf(address(this)) - weth_balance_before
|
WETH.balanceOf(address(this)) - weth_balance_before
|
||||||
);
|
);
|
||||||
|
|
||||||
assertEq(
|
assertEq(
|
||||||
trade.calculatedAmount,
|
trade.calculatedAmount,
|
||||||
usdc_balance_before - USDC.balanceOf(address(this))
|
usdc_balance_before - USDC.balanceOf(address(this))
|
||||||
@@ -107,7 +101,7 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
|
|||||||
specifiedAmount,
|
specifiedAmount,
|
||||||
usdc_balance_before - USDC.balanceOf(address(this))
|
usdc_balance_before - USDC.balanceOf(address(this))
|
||||||
);
|
);
|
||||||
|
|
||||||
assertEq(
|
assertEq(
|
||||||
trade.calculatedAmount,
|
trade.calculatedAmount,
|
||||||
WETH.balanceOf(address(this)) - weth_balance_before
|
WETH.balanceOf(address(this)) - weth_balance_before
|
||||||
@@ -127,76 +121,67 @@ contract IntegralSwapAdapterTest is Test, ISwapAdapterTypes {
|
|||||||
function executeIncreasingSwapsIntegral(OrderSide side) internal {
|
function executeIncreasingSwapsIntegral(OrderSide side) internal {
|
||||||
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
||||||
|
|
||||||
uint256 amountConstant_ = side == OrderSide.Sell ? 1000 * 10**6 : 10**17;
|
uint256 amountConstant_ =
|
||||||
|
side == OrderSide.Sell ? 1000 * 10 ** 6 : 10 ** 17;
|
||||||
|
|
||||||
uint256[] memory amounts = new uint256[](TEST_ITERATIONS);
|
uint256[] memory amounts = new uint256[](TEST_ITERATIONS);
|
||||||
amounts[0] = amountConstant_;
|
amounts[0] = amountConstant_;
|
||||||
for (uint256 i = 1; i < TEST_ITERATIONS; i++) {
|
for (uint256 i = 1; i < TEST_ITERATIONS; i++) {
|
||||||
amounts[i] = amountConstant_ * i;
|
amounts[i] = amountConstant_ * i;
|
||||||
}
|
}
|
||||||
|
|
||||||
Trade[] memory trades = new Trade[](TEST_ITERATIONS);
|
Trade[] memory trades = new Trade[](TEST_ITERATIONS);
|
||||||
uint256 beforeSwap;
|
uint256 beforeSwap;
|
||||||
for (uint256 i = 1; i < TEST_ITERATIONS; i++) {
|
for (uint256 i = 1; i < TEST_ITERATIONS; i++) {
|
||||||
beforeSwap = vm.snapshot();
|
beforeSwap = vm.snapshot();
|
||||||
|
|
||||||
deal(address(USDC), address(this), amounts[i]);
|
deal(address(USDC), address(this), amounts[i]);
|
||||||
USDC.approve(address(adapter), amounts[i]);
|
USDC.approve(address(adapter), amounts[i]);
|
||||||
|
|
||||||
trades[i] = adapter.swap(pair, USDC, WETH, side, amounts[i]);
|
trades[i] = adapter.swap(pair, USDC, WETH, side, amounts[i]);
|
||||||
vm.revertTo(beforeSwap);
|
vm.revertTo(beforeSwap);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint256 i = 1; i < TEST_ITERATIONS - 1; i++) {
|
for (uint256 i = 1; i < TEST_ITERATIONS - 1; i++) {
|
||||||
assertLe(
|
assertLe(trades[i].calculatedAmount, trades[i + 1].calculatedAmount);
|
||||||
trades[i].calculatedAmount,
|
|
||||||
trades[i + 1].calculatedAmount
|
|
||||||
);
|
|
||||||
assertLe(trades[i].gasUsed, trades[i + 1].gasUsed);
|
assertLe(trades[i].gasUsed, trades[i + 1].gasUsed);
|
||||||
assertEq(trades[i].price.compareFractions(trades[i + 1].price), 0);
|
assertEq(trades[i].price.compareFractions(trades[i + 1].price), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function testGetCapabilitiesIntegral(
|
function testGetCapabilitiesIntegral(bytes32 pair, address t0, address t1)
|
||||||
bytes32 pair,
|
public
|
||||||
address t0,
|
{
|
||||||
address t1
|
Capability[] memory res =
|
||||||
) public {
|
adapter.getCapabilities(pair, IERC20(t0), IERC20(t1));
|
||||||
Capability[] memory res = adapter.getCapabilities(
|
|
||||||
pair,
|
|
||||||
IERC20(t0),
|
|
||||||
IERC20(t1)
|
|
||||||
);
|
|
||||||
|
|
||||||
assertEq(res.length, 4);
|
assertEq(res.length, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testGetTokensIntegral() public {
|
function testGetTokensIntegral() public {
|
||||||
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
||||||
IERC20[] memory tokens = adapter.getTokens(pair);
|
IERC20[] memory tokens = adapter.getTokens(pair);
|
||||||
|
|
||||||
assertEq(tokens.length, 2);
|
assertEq(tokens.length, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testGetLimitsIntegral() public {
|
function testGetLimitsIntegral() public {
|
||||||
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
bytes32 pair = bytes32(bytes20(USDC_WETH_PAIR));
|
||||||
uint256[] memory limits = adapter.getLimits(pair, USDC, WETH);
|
uint256[] memory limits = adapter.getLimits(pair, USDC, WETH);
|
||||||
|
|
||||||
assertEq(limits.length, 2);
|
assertEq(limits.length, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMinLimits(IERC20 sellToken, IERC20 buyToken) public view returns (uint256[] memory limits) {
|
function getMinLimits(IERC20 sellToken, IERC20 buyToken)
|
||||||
(
|
public
|
||||||
,
|
view
|
||||||
,
|
returns (uint256[] memory limits)
|
||||||
uint256 limitMin0,
|
{
|
||||||
,
|
(,, uint256 limitMin0,, uint256 limitMin1,) =
|
||||||
uint256 limitMin1
|
relayer.getPoolState(address(sellToken), address(buyToken));
|
||||||
,
|
|
||||||
) = relayer.getPoolState(address(sellToken), address(buyToken));
|
|
||||||
|
|
||||||
limits = new uint256[](2);
|
limits = new uint256[](2);
|
||||||
limits[0] = limitMin0;
|
limits[0] = limitMin0;
|
||||||
limits[1] = limitMin1;
|
limits[1] = limitMin1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ import "src/libraries/FractionMath.sol";
|
|||||||
|
|
||||||
/// @title TemplateSwapAdapterTest
|
/// @title TemplateSwapAdapterTest
|
||||||
/// @dev This is a template for a swap adapter test.
|
/// @dev This is a template for a swap adapter test.
|
||||||
/// Test all functions that are implemented in your swap adapter, the two test included here are just an example.
|
/// Test all functions that are implemented in your swap adapter, the two test
|
||||||
/// Feel free to use UniswapV2SwapAdapterTest and BalancerV2SwapAdapterTest as a reference.
|
/// included here are just an example.
|
||||||
|
/// Feel free to use UniswapV2SwapAdapterTest and BalancerV2SwapAdapterTest as a
|
||||||
|
/// reference.
|
||||||
contract TemplateSwapAdapterTest is Test, ISwapAdapterTypes {
|
contract TemplateSwapAdapterTest is Test, ISwapAdapterTypes {
|
||||||
using FractionMath for Fraction;
|
using FractionMath for Fraction;
|
||||||
|
|
||||||
function testPriceFuzz(uint256 amount0, uint256 amount1) public {}
|
function testPriceFuzz(uint256 amount0, uint256 amount1) public {}
|
||||||
|
|
||||||
function testSwapFuzz(uint256 specifiedAmount) public {}
|
function testSwapFuzz(uint256 specifiedAmount) public {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ contract UniswapV2PairFunctionTest is Test, ISwapAdapterTypes {
|
|||||||
function setUp() public {
|
function setUp() public {
|
||||||
uint256 forkBlock = 17000000;
|
uint256 forkBlock = 17000000;
|
||||||
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
||||||
adapter = new
|
adapter =
|
||||||
UniswapV2SwapAdapter(0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f);
|
new UniswapV2SwapAdapter(0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f);
|
||||||
|
|
||||||
vm.label(address(adapter), "UniswapV2SwapAdapter");
|
vm.label(address(adapter), "UniswapV2SwapAdapter");
|
||||||
vm.label(address(WETH), "WETH");
|
vm.label(address(WETH), "WETH");
|
||||||
|
|||||||
Reference in New Issue
Block a user