Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VelodromeExchangeAdapter #257

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
131 changes: 131 additions & 0 deletions contracts/interfaces/external/IVelodromeRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright 2020 Set Labs Inc.
andris-balodis marked this conversation as resolved.
Show resolved Hide resolved

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache License, Version 2.0
*/

pragma solidity 0.6.10;
pragma experimental "ABIEncoderV2";

interface IVelodromeRouter {
struct route {
address from;
address to;
bool stable;
}

function factory() external pure returns (address);
function weth() external pure returns (address);

function addLiquidity(
address tokenA,
address tokenB,
bool stable,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
address token,
bool stable,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function removeLiquidity(
address tokenA,
address tokenB,
bool stable,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB);
function removeLiquidityETH(
address token,
bool stable,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountETH);
function removeLiquidityWithPermit(
address tokenA,
address tokenB,
bool stable,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountA, uint amountB);
function removeLiquidityETHWithPermit(
address token,
bool stable,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountETH);
function swapExactTokensForTokensSimple(
uint amountIn,
uint amountOutMin,
address tokenFrom,
address tokenTo,
bool stable,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
route[] calldata routes,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactETHForTokens(
uint amountOutMin,
route[] calldata routes,
address to,
uint deadline
) external payable returns (uint[] memory amounts);

function getReserves(address tokenA, address tokenB, bool stable) external view returns (uint reserveA, uint reserveB);
function getAmountOut(uint amountIn, address tokenIn, address tokenOut) external view returns (uint amount, bool stable);
function getAmountsOut(uint amountIn, route[] memory routes) external view returns (uint[] memory amounts);
function quoteAddLiquidity(
address tokenA,
address tokenB,
bool stable,
uint amountADesired,
uint amountBDesired
) external view returns (uint amountA, uint amountB, uint liquidity);
function quoteRemoveLiquidity(
address tokenA,
address tokenB,
bool stable,
uint liquidity
) external view returns (uint amountA, uint amountB);
}
120 changes: 120 additions & 0 deletions contracts/protocol/integration/exchange/VelodromeExchangeAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright 2022 Set Labs Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache License, Version 2.0
*/

pragma solidity 0.6.10;
pragma experimental "ABIEncoderV2";

import { IVelodromeRouter } from "../../../interfaces/external/IVelodromeRouter.sol";

/**
* @title VelodromeExchangeAdapter
* @author deephil
*
* Exchange adapter for Velodrome Exchange Router that encodes trade data
*/
contract VelodromeExchangeAdapter {

/* ============ State Variables ============ */

// Address of Velodrome Exchange Router contract
address public immutable router;

/* ============ Constructor ============ */

/**
* Set state variables
*
* @param _router Address of Velodrome Exchange Router contract
*/
constructor(address _router) public {
router = _router;
}

/* ============ External Getter Functions ============ */

/**
* Return calldata for Velodrome Exchange Router
*
* @param _sourceToken Address of source token to be sold
* @param _destinationToken Address of destination token to buy
* @param _destinationAddress Address that assets should be transferred to
* @param _sourceQuantity Amount of source token to sell
* @param _minDestinationQuantity Min amount of destination token to buy
* @param _data Arbitrary bytes containing trade call data
*
* @return address Target contract address
* @return uint256 Call value
* @return bytes Trade calldata
*/
function getTradeCalldata(
address _sourceToken,
address _destinationToken,
address _destinationAddress,
uint256 _sourceQuantity,
uint256 _minDestinationQuantity,
bytes memory _data
)
external
view
returns (address, uint256, bytes memory)
{
(
IVelodromeRouter.route[] memory routes
) = abi.decode(_data, (IVelodromeRouter.route[]));

andris-balodis marked this conversation as resolved.
Show resolved Hide resolved
require(_sourceToken == routes[0].from, "Source token path mismatch");
require(_destinationToken == routes[routes.length - 1].to, "Destination token path mismatch");

bytes memory callData = abi.encodeWithSelector(
IVelodromeRouter.swapExactTokensForTokens.selector,
_sourceQuantity,
_minDestinationQuantity,
routes,
_destinationAddress,
block.timestamp
);
return (router, 0, callData);
}

/**
* Returns the address to approve source tokens to for trading. This is the Velodrome Exchange Router address
*
* @return address Address of the contract to approve tokens to
*/
function getSpender()
external
view
returns (address)
{
return router;
}

/**
* Generate data parameter to be passed to `getTradeCallData`. Returns encoded trade routes.
*
* @param _routes array of routes for Velodrome Router
andris-balodis marked this conversation as resolved.
Show resolved Hide resolved
* @return bytes Data parameter to be passed to `getTradeCallData`
*/
function generateDataParam(IVelodromeRouter.route[] calldata _routes)
external
pure
returns (bytes memory)
{
andris-balodis marked this conversation as resolved.
Show resolved Hide resolved
return abi.encode(_routes);
}
}
Loading