-
Notifications
You must be signed in to change notification settings - Fork 103
VelodromeExchangeAdapter #257
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
Open
andris-balodis
wants to merge
10
commits into
SetProtocol:master
Choose a base branch
from
andris-balodis:velodrome-adapter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
da28e64
add velodrome exchange adapter
andris-balodis bbdf241
add unit tests for VelodromeExchangeAdapter
andris-balodis 699e744
update unit test to use velodrome fixture
andris-balodis b07c422
skip velodrome exchange trade module integration test
andris-balodis a8b7f72
fork optimism for velodrome integration test
andris-balodis e8a83a5
use deadline as parameter
andris-balodis d2e52cc
update imports from typechain
andris-balodis cd5d9e2
fix comments
andris-balodis 4d9ed79
fix comments
andris-balodis 5d637c0
fix comments
andris-balodis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
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); | ||
} |
126 changes: 126 additions & 0 deletions
126
contracts/protocol/integration/exchange/VelodromeExchangeAdapter.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
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, | ||
uint256 deadline | ||
) = abi.decode(_data, (IVelodromeRouter.route[], uint256)); | ||
|
||
require(routes.length > 0, "empty routes"); | ||
require(_sourceToken == routes[0].from, "Source token path mismatch"); | ||
require(_destinationToken == routes[routes.length - 1].to, "Destination token path mismatch"); | ||
require(deadline >= block.timestamp, "invalid deadline"); | ||
|
||
bytes memory callData = abi.encodeWithSelector( | ||
IVelodromeRouter.swapExactTokensForTokens.selector, | ||
_sourceQuantity, | ||
_minDestinationQuantity, | ||
routes, | ||
_destinationAddress, | ||
deadline | ||
); | ||
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 | ||
* @param _deadline block timestamp of trade deadline | ||
* @return bytes Data parameter to be passed to `getTradeCallData` | ||
*/ | ||
function generateDataParam(IVelodromeRouter.route[] calldata _routes, uint256 _deadline) | ||
external | ||
view | ||
returns (bytes memory) | ||
{ | ||
andris-balodis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
require(_routes.length > 0, "empty routes"); | ||
andris-balodis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
require(_deadline >= block.timestamp, "invalid deadline"); | ||
return abi.encode(_routes, _deadline); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.