chore: Remove old execution and encoding files (#221)
They are unused now. See https://github.com/propeller-heads/tycho-execution for the new workflow
This commit is contained in:
@@ -1,132 +0,0 @@
|
||||
// SPDX-License-Identifier: UNLICENCED
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../interfaces/ISwapExecutor.sol";
|
||||
|
||||
contract BalancerSwapExecutor is ISwapExecutor {
|
||||
address private constant vaultAddress =
|
||||
0xBA12222222228d8Ba445958a75a0704d566BF2C8;
|
||||
bytes32 private constant swapSelector =
|
||||
0x52bbbe2900000000000000000000000000000000000000000000000000000000;
|
||||
bytes32 private constant maxUint256 =
|
||||
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
|
||||
|
||||
/**
|
||||
* @dev Executes a Balancer swap.
|
||||
* @param givenAmount how much of to swap, depending on exactOut either in-
|
||||
* or outAmount.
|
||||
* @param data the parameters of the swap. This data is roughly the packed
|
||||
* encoding of
|
||||
* the struct below:
|
||||
* ```
|
||||
* struct Params {
|
||||
* // the token that the caller is selling
|
||||
* IERC20 tokenIn;
|
||||
*
|
||||
* // the token that the caller is receiving in exchange
|
||||
* IERC20 tokenOut;
|
||||
*
|
||||
* // the target pool id
|
||||
* bytes32 poolId;
|
||||
*
|
||||
* // the receiver of `tokenOut`
|
||||
* address receiver;
|
||||
*
|
||||
* // whether we want exactOut semantics
|
||||
* bool exactOut;
|
||||
*
|
||||
* // whether we need to approve the pool to spend `tokenIn`
|
||||
* bool tokenApprovalNeeded;
|
||||
*
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
function swap(uint256 givenAmount, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 calculatedAmount)
|
||||
{
|
||||
IERC20 tokenIn;
|
||||
IERC20 tokenOut;
|
||||
bytes32 poolId;
|
||||
address receiver;
|
||||
bool exactOut;
|
||||
bool tokenApprovalNeeded;
|
||||
assembly {
|
||||
tokenIn := shr(96, calldataload(data.offset))
|
||||
tokenOut := shr(96, calldataload(add(data.offset, 20)))
|
||||
poolId := calldataload(add(data.offset, 40))
|
||||
let dataLoad := calldataload(add(data.offset, 72))
|
||||
receiver := shr(96, dataLoad)
|
||||
exactOut := and(shr(88, dataLoad), 0xff)
|
||||
tokenApprovalNeeded := and(shr(80, dataLoad), 0xff)
|
||||
|
||||
// Check if token approval is needed and perform the approval
|
||||
if tokenApprovalNeeded {
|
||||
// Prepare approve call
|
||||
let approveCalldata := mload(0x40)
|
||||
mstore(
|
||||
approveCalldata,
|
||||
0x095ea7b300000000000000000000000000000000000000000000000000000000
|
||||
) // approve selector
|
||||
mstore(add(approveCalldata, 4), vaultAddress) // spender
|
||||
mstore(add(approveCalldata, 36), maxUint256) // value
|
||||
// (maxUint256)
|
||||
|
||||
let success :=
|
||||
call(gas(), tokenIn, 0, approveCalldata, 68, 0, 0)
|
||||
if iszero(success) {
|
||||
returndatacopy(0, 0, returndatasize())
|
||||
revert(0, returndatasize())
|
||||
}
|
||||
}
|
||||
|
||||
let ptr := mload(0x40)
|
||||
mstore(ptr, swapSelector)
|
||||
//limit: as it is always recalculated during the swap, we use the
|
||||
// extremums 0 tokenOut or max(uint256) tokenIn.
|
||||
let limit := 0
|
||||
if exactOut { limit := maxUint256 }
|
||||
// as the singleSwap struct contains a bytes, it's considered as
|
||||
// dynamic.
|
||||
// dynamic values are encoded at the end of the calldata and have a
|
||||
// corresponding offset at the beginning
|
||||
// we first need to encode the offset of the singleSwap struct
|
||||
mstore(add(ptr, 4), 0xe0)
|
||||
// fundManagement.sender: is always address(this)
|
||||
mstore(add(ptr, 36), address())
|
||||
// fundManagement.fromInternalBalance
|
||||
mstore(add(ptr, 68), 0)
|
||||
// fundManagement.receiver
|
||||
mstore(add(ptr, 100), receiver)
|
||||
// fundManagement.toInternalBalance
|
||||
mstore(add(ptr, 132), 0)
|
||||
// limit
|
||||
mstore(add(ptr, 164), limit)
|
||||
// deadline
|
||||
mstore(add(ptr, 196), timestamp())
|
||||
// singleSwap.poolId
|
||||
mstore(add(ptr, 228), poolId)
|
||||
// singleSwap.exactOut
|
||||
mstore(add(ptr, 260), exactOut)
|
||||
// singleSwap.assetIn
|
||||
mstore(add(ptr, 292), tokenIn)
|
||||
// singleSwap.assetOut
|
||||
mstore(add(ptr, 324), tokenOut)
|
||||
// singleSwap.amount
|
||||
mstore(add(ptr, 356), givenAmount)
|
||||
// singleSwap.userData offset
|
||||
mstore(add(ptr, 388), 0xc0)
|
||||
// singleSwap.userData lenght
|
||||
mstore(add(ptr, 420), 0)
|
||||
|
||||
let success := call(gas(), vaultAddress, 0, ptr, 452, ptr, 32)
|
||||
switch success
|
||||
case 0 {
|
||||
returndatacopy(0, 0, returndatasize())
|
||||
revert(0, returndatasize())
|
||||
}
|
||||
default { calculatedAmount := mload(ptr) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
// SPDX-License-Identifier: UNLICENCED
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../interfaces/ISwapExecutor.sol";
|
||||
import "./interfaces/ICurvePool.sol";
|
||||
import "./interfaces/ICurvePoolNoReturn.sol";
|
||||
import "./interfaces/ICurveCryptoPool.sol";
|
||||
import "./interfaces/ICurvePoolNoReturn.sol";
|
||||
import "./interfaces/ICurvePoolWithReturn.sol";
|
||||
import {
|
||||
IERC20,
|
||||
SafeERC20
|
||||
} from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import "src/libraries/EfficientERC20.sol";
|
||||
|
||||
interface IWETH is IERC20 {
|
||||
function deposit() external payable;
|
||||
|
||||
function withdraw(uint256) external;
|
||||
}
|
||||
|
||||
contract CurveSwapExecutor is ISwapExecutor, ISwapExecutorErrors {
|
||||
using EfficientERC20 for IERC20;
|
||||
|
||||
IWETH private constant weth =
|
||||
IWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
|
||||
address private constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||
|
||||
function _decodeParams(bytes calldata data)
|
||||
internal
|
||||
pure
|
||||
returns (
|
||||
IERC20 tokenOut,
|
||||
address target,
|
||||
address receiver,
|
||||
uint8 poolType,
|
||||
int128 i,
|
||||
int128 j,
|
||||
bool tokenApprovalNeeded
|
||||
)
|
||||
{
|
||||
tokenOut = IERC20(address(bytes20(data[0:20])));
|
||||
target = address(bytes20(data[20:40]));
|
||||
receiver = address(bytes20(data[40:60]));
|
||||
poolType = uint8(data[60]);
|
||||
i = int128(uint128(uint8(data[61])));
|
||||
j = int128(uint128(uint8(data[62])));
|
||||
tokenApprovalNeeded = data[63] != 0;
|
||||
}
|
||||
|
||||
function swap(uint256 amountIn, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 res)
|
||||
{
|
||||
(
|
||||
IERC20 tokenOut,
|
||||
address target,
|
||||
address receiver,
|
||||
uint8 poolType,
|
||||
int128 i,
|
||||
int128 j,
|
||||
bool tokenApprovalNeeded
|
||||
) = _decodeParams(data);
|
||||
|
||||
// Approve the token for the pool's address if `tokenApprovalNeeded` is
|
||||
// true
|
||||
if (tokenApprovalNeeded) {
|
||||
address tokenIn;
|
||||
// pool type 6 has a different function signature to get the coins
|
||||
if (poolType == 6) {
|
||||
tokenIn = ICurvePoolNoReturn(target).underlying_coins(int128(i));
|
||||
} else {
|
||||
tokenIn = ICurvePool(target).coins(uint256(uint128(i)));
|
||||
}
|
||||
IERC20(tokenIn).forceApprove(target, type(uint256).max);
|
||||
}
|
||||
if (poolType == 0) {
|
||||
// simple exchange with int128
|
||||
// e.g. AAVE, EURS
|
||||
res = ICurvePoolWithReturn(target).exchange(i, j, amountIn, 0);
|
||||
if (receiver != address(this)) {
|
||||
tokenOut.safeTransfer(receiver, res);
|
||||
}
|
||||
} else if (poolType == 1) {
|
||||
// simple exchange with int128 but no amountOut,
|
||||
// e.g. BUSD, HBTC, PAX, renBTC, sBTC, SUSD, USDT, Y, 3pool
|
||||
uint256 tokenOutBalanceBeforeSwap =
|
||||
tokenOut.balanceOf(address(this));
|
||||
ICurvePoolNoReturn(target).exchange(i, j, amountIn, 0);
|
||||
uint256 tokenOutBalanceAfterSwap = tokenOut.balanceOf(address(this));
|
||||
res = tokenOutBalanceAfterSwap - tokenOutBalanceBeforeSwap;
|
||||
if (receiver != address(this)) {
|
||||
tokenOut.safeTransfer(receiver, res);
|
||||
}
|
||||
} else if (poolType == 3) {
|
||||
// tricrypto case
|
||||
uint256 tokenOutBalanceBeforeSwap =
|
||||
tokenOut.balanceOf(address(this));
|
||||
ICurveCryptoPool(target).exchange(
|
||||
uint256(uint128(i)),
|
||||
uint256(uint128(j)),
|
||||
amountIn,
|
||||
0,
|
||||
false //TODO: Check if we can call the entrypoint without
|
||||
// 'use_eth' as it's false by default.
|
||||
);
|
||||
uint256 tokenOutBalanceAfterSwap = tokenOut.balanceOf(address(this));
|
||||
res = tokenOutBalanceAfterSwap - tokenOutBalanceBeforeSwap;
|
||||
if (receiver != address(this)) {
|
||||
tokenOut.safeTransfer(receiver, res);
|
||||
}
|
||||
} else if (poolType == 4) {
|
||||
// (payable) ether based stableswaps - so far no liquidity
|
||||
// e.g. sETH, stETH, rETH, etc
|
||||
ICurveCryptoPool pool = ICurveCryptoPool(target);
|
||||
if (pool.coins(uint256(uint128(i))) == ETH) {
|
||||
weth.withdraw(amountIn);
|
||||
res = pool.exchange{value: amountIn}(i, j, amountIn, 0);
|
||||
} else {
|
||||
res = pool.exchange(i, j, amountIn, 0);
|
||||
}
|
||||
|
||||
if (pool.coins(uint256(uint128(j))) == ETH) {
|
||||
weth.deposit{value: res}();
|
||||
}
|
||||
if (receiver != address(this)) {
|
||||
tokenOut.safeTransfer(receiver, res);
|
||||
}
|
||||
} else if (poolType == 5) {
|
||||
// metapool or lending pool interface using int128
|
||||
// e.g. AAVE
|
||||
res = ICurvePoolWithReturn(target).exchange_underlying(
|
||||
i, j, amountIn, 0
|
||||
);
|
||||
if (receiver != address(this)) {
|
||||
tokenOut.safeTransfer(receiver, res);
|
||||
}
|
||||
} else if (poolType == 6) {
|
||||
// metapool or lending pool interface using int128 no amountOut
|
||||
// returned
|
||||
// e.g. Y, Compound
|
||||
uint256 tokenOutBalanceBeforeSwap =
|
||||
tokenOut.balanceOf(address(this));
|
||||
ICurvePoolNoReturn(target).exchange_underlying(i, j, amountIn, 0);
|
||||
uint256 tokenOutBalanceAfterSwap = tokenOut.balanceOf(address(this));
|
||||
res = tokenOutBalanceAfterSwap - tokenOutBalanceBeforeSwap;
|
||||
if (receiver != address(this)) {
|
||||
tokenOut.safeTransfer(receiver, res);
|
||||
}
|
||||
} else if (poolType == 7) {
|
||||
// cryptov2 pool with two tokens
|
||||
// e.g. LDO/ETH
|
||||
res = ICurvePoolWithReturn(target).exchange(
|
||||
uint256(uint128(i)),
|
||||
uint256(uint128(j)),
|
||||
amountIn,
|
||||
0,
|
||||
false,
|
||||
receiver
|
||||
);
|
||||
} else if (poolType == 8) {
|
||||
// cryptov2 two tokens not factory pools ETH/CRV and ETH/CVX
|
||||
res = ICurvePoolWithReturn(target).exchange(
|
||||
uint256(uint128(i)), uint256(uint128(j)), amountIn, 0, false
|
||||
);
|
||||
if (receiver != address(this)) {
|
||||
tokenOut.safeTransfer(receiver, res);
|
||||
}
|
||||
} else {
|
||||
revert UnknownPoolType(poolType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity >=0.4.0;
|
||||
|
||||
interface ICurveCryptoPool {
|
||||
function get_dy(uint256 i, uint256 j, uint256 dx)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
|
||||
// tricrypto
|
||||
function exchange(
|
||||
uint256 i,
|
||||
uint256 j,
|
||||
uint256 dx,
|
||||
uint256 min_dy,
|
||||
bool use_eth
|
||||
) external payable;
|
||||
|
||||
// eth accepting pools
|
||||
function exchange(int128 i, int128 j, uint256 dx, uint256 min_dy)
|
||||
external
|
||||
payable
|
||||
returns (uint256);
|
||||
|
||||
function coins(uint256 i) external view returns (address);
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
pragma solidity >=0.4.0;
|
||||
|
||||
interface ICurvePool {
|
||||
function initialize(
|
||||
string memory _name,
|
||||
string memory _symbol,
|
||||
address _coin,
|
||||
uint256 _decimals,
|
||||
uint256 _A,
|
||||
uint256 _fee,
|
||||
address _admin
|
||||
) external;
|
||||
function decimals() external view returns (uint256);
|
||||
function transfer(address _to, uint256 _value) external returns (bool);
|
||||
function transferFrom(address _from, address _to, uint256 _value)
|
||||
external
|
||||
returns (bool);
|
||||
function approve(address _spender, uint256 _value)
|
||||
external
|
||||
returns (bool);
|
||||
function get_previous_balances()
|
||||
external
|
||||
view
|
||||
returns (uint256[2] memory);
|
||||
function get_balances() external view returns (uint256[2] memory);
|
||||
function get_twap_balances(
|
||||
uint256[2] memory _first_balances,
|
||||
uint256[2] memory _last_balances,
|
||||
uint256 _time_elapsed
|
||||
) external view returns (uint256[2] memory);
|
||||
function get_price_cumulative_last()
|
||||
external
|
||||
view
|
||||
returns (uint256[2] memory);
|
||||
function admin_fee() external view returns (uint256);
|
||||
function A() external view returns (uint256);
|
||||
function A_precise() external view returns (uint256);
|
||||
function get_virtual_price() external view returns (uint256);
|
||||
function calc_token_amount(uint256[2] memory _amounts, bool _is_deposit)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
function calc_token_amount(
|
||||
uint256[2] memory _amounts,
|
||||
bool _is_deposit,
|
||||
bool _previous
|
||||
) external view returns (uint256);
|
||||
function add_liquidity(uint256[2] memory _amounts, uint256 _min_mint_amount)
|
||||
external
|
||||
returns (uint256);
|
||||
function add_liquidity(
|
||||
uint256[2] memory _amounts,
|
||||
uint256 _min_mint_amount,
|
||||
address _receiver
|
||||
) external returns (uint256);
|
||||
function get_dy(int128 i, int128 j, uint256 dx)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
function get_dy(int128 i, int128 j, uint256 dx, uint256[2] memory _balances)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
function get_dy_underlying(int128 i, int128 j, uint256 dx)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
function get_dy_underlying(
|
||||
int128 i,
|
||||
int128 j,
|
||||
uint256 dx,
|
||||
uint256[2] memory _balances
|
||||
) external view returns (uint256);
|
||||
function exchange(uint256 i, uint256 j, uint256 dx, uint256 min_dy)
|
||||
external;
|
||||
function exchange(
|
||||
uint256 i,
|
||||
uint256 j,
|
||||
uint256 dx,
|
||||
uint256 min_dy,
|
||||
address _receiver
|
||||
) external;
|
||||
function exchange(int128 i, int128 j, uint256 dx, uint256 min_dy)
|
||||
external;
|
||||
function exchange(
|
||||
int128 i,
|
||||
int128 j,
|
||||
uint256 dx,
|
||||
uint256 min_dy,
|
||||
address _receiver
|
||||
) external;
|
||||
function exchange_underlying(
|
||||
uint256 i,
|
||||
uint256 j,
|
||||
uint256 dx,
|
||||
uint256 min_dy
|
||||
) external;
|
||||
function exchange_underlying(
|
||||
uint256 i,
|
||||
uint256 j,
|
||||
uint256 dx,
|
||||
uint256 min_dy,
|
||||
address _receiver
|
||||
) external;
|
||||
function exchange_underlying(int128 i, int128 j, uint256 dx, uint256 min_dy)
|
||||
external;
|
||||
function exchange_underlying(
|
||||
int128 i,
|
||||
int128 j,
|
||||
uint256 dx,
|
||||
uint256 min_dy,
|
||||
address _receiver
|
||||
) external;
|
||||
function remove_liquidity(
|
||||
uint256 _burn_amount,
|
||||
uint256[2] memory _min_amounts
|
||||
) external returns (uint256[2] memory);
|
||||
function remove_liquidity(
|
||||
uint256 _burn_amount,
|
||||
uint256[2] memory _min_amounts,
|
||||
address _receiver
|
||||
) external returns (uint256[2] memory);
|
||||
function remove_liquidity_imbalance(
|
||||
uint256[2] memory _amounts,
|
||||
uint256 _max_burn_amount
|
||||
) external returns (uint256);
|
||||
function remove_liquidity_imbalance(
|
||||
uint256[2] memory _amounts,
|
||||
uint256 _max_burn_amount,
|
||||
address _receiver
|
||||
) external returns (uint256);
|
||||
function calc_withdraw_one_coin(uint256 _burn_amount, int128 i)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
function calc_withdraw_one_coin(
|
||||
uint256 _burn_amount,
|
||||
int128 i,
|
||||
bool _previous
|
||||
) external view returns (uint256);
|
||||
function remove_liquidity_one_coin(
|
||||
uint256 _burn_amount,
|
||||
int128 i,
|
||||
uint256 _min_received
|
||||
) external returns (uint256);
|
||||
function remove_liquidity_one_coin(
|
||||
uint256 _burn_amount,
|
||||
int128 i,
|
||||
uint256 _min_received,
|
||||
address _receiver
|
||||
) external returns (uint256);
|
||||
function ramp_A(uint256 _future_A, uint256 _future_time) external;
|
||||
function stop_ramp_A() external;
|
||||
function admin_balances(uint256 i) external view returns (uint256);
|
||||
function withdraw_admin_fees() external;
|
||||
function admin() external view returns (address);
|
||||
function coins(uint256 arg0) external view returns (address);
|
||||
function balances(uint256 arg0) external view returns (uint256);
|
||||
function fee() external view returns (uint256);
|
||||
function block_timestamp_last() external view returns (uint256);
|
||||
function initial_A() external view returns (uint256);
|
||||
function future_A() external view returns (uint256);
|
||||
function initial_A_time() external view returns (uint256);
|
||||
function future_A_time() external view returns (uint256);
|
||||
function name() external view returns (string memory);
|
||||
function symbol() external view returns (string memory);
|
||||
function balanceOf(address arg0) external view returns (uint256);
|
||||
function allowance(address arg0, address arg1)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
function totalSupply() external view returns (uint256);
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity >=0.4.0;
|
||||
|
||||
interface ICurvePoolNoReturn {
|
||||
function get_dy(int128 i, int128 j, uint256 dx)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
|
||||
function get_dy_underlying(int128 i, int128 j, uint256 dx)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
|
||||
function exchange(int128 i, int128 j, uint256 dx, uint256 min_dy)
|
||||
external;
|
||||
|
||||
function exchange_underlying(int128 i, int128 j, uint256 dx, uint256 min_dy)
|
||||
external;
|
||||
function coins(int128 arg0) external view returns (address);
|
||||
function underlying_coins(int128 arg0) external view returns (address);
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity >=0.4.0;
|
||||
|
||||
interface ICurvePoolWithReturn {
|
||||
function get_dy(int128 i, int128 j, uint256 dx)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
|
||||
function get_dy_underlying(int128 i, int128 j, uint256 dx)
|
||||
external
|
||||
view
|
||||
returns (uint256);
|
||||
|
||||
function exchange(int128 i, int128 j, uint256 dx, uint256 min_dy)
|
||||
external
|
||||
returns (uint256);
|
||||
|
||||
function exchange_underlying(int128 i, int128 j, uint256 dx, uint256 min_dy)
|
||||
external
|
||||
returns (uint256);
|
||||
|
||||
function exchange(
|
||||
uint256 i,
|
||||
uint256 j,
|
||||
uint256 dx,
|
||||
uint256 min_dy,
|
||||
bool use_eth,
|
||||
address receiver
|
||||
) external returns (uint256);
|
||||
|
||||
function exchange(
|
||||
uint256 i,
|
||||
uint256 j,
|
||||
uint256 dx,
|
||||
uint256 min_dy,
|
||||
bool use_eth
|
||||
) external returns (uint256);
|
||||
}
|
||||
Reference in New Issue
Block a user