Skip to content

Add GeneralIndexModule contract #59

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

Merged
merged 35 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5b416f4
Add GenrealIndexModule contract
0xSachinK Mar 27, 2021
834ba66
Fix bugs & Add UniswapV2ExchangeAdapterV3
0xSachinK Mar 28, 2021
45ebf67
Add BalancerV1ExchangeAdapter
0xSachinK Mar 29, 2021
c220c4f
Add initial tests & add fee collection to GeneralIndexModule
0xSachinK Mar 30, 2021
ef936bc
Add tests for trade function
0xSachinK Apr 2, 2021
b165c4a
Add javadocs to contract
0xSachinK Apr 2, 2021
adc3018
Fix javadoc bugs
0xSachinK Apr 2, 2021
e29e2c5
Add contract initalization test
0xSachinK Apr 3, 2021
816ce4a
Fix bug
0xSachinK Apr 3, 2021
0efc7e7
Add suggested changes
0xSachinK Apr 5, 2021
68d4317
Add raiseAssetTargets test
0xSachinK Apr 5, 2021
171c2ea
Add suggested changes & tests
0xSachinK Apr 5, 2021
189d740
modify raiseTargetPercentage
0xSachinK Apr 5, 2021
7a7afaa
Add tradeRemainingWETH tests
0xSachinK Apr 6, 2021
1db03b5
Add tests for SetToken with weth as component
0xSachinK Apr 6, 2021
276bb5e
Add more tests
0xSachinK Apr 7, 2021
85755e5
fix bug in tests
0xSachinK Apr 7, 2021
c2258ef
Add tests for empty arrays
0xSachinK Apr 8, 2021
3433ffa
Add min/max limit amounts to prevent MEV
0xSachinK Apr 8, 2021
9cff0a2
Add strict max/min limit amounts to tests
0xSachinK Apr 8, 2021
8f6f9ae
Fix max floating amounts
0xSachinK Apr 8, 2021
e38d02a
Add suggested changes to GeneralIndexModule.sol
0xSachinK Apr 9, 2021
2846a60
Refactor all tests, add suggested changes
0xSachinK Apr 10, 2021
dd69996
fix coverage bug
0xSachinK Apr 10, 2021
cfb2c94
Remove .skip from tests
0xSachinK Apr 10, 2021
eaa85ec
Add cacheBeforeEach blocks to speed up tests
0xSachinK Apr 10, 2021
3f0e0a2
Add test to revert when exchange adapter not found
0xSachinK Apr 10, 2021
bf10576
Added logic to be able to raiseAssetTargets when positionMultiplier <…
bweick Apr 12, 2021
6d7d91a
Complete coverage. Adding tests for getComponentTradeQuantityAndDirec…
bweick Apr 12, 2021
31ab0ed
Remove console.log
0xSachinK Apr 12, 2021
7699ae5
Removed unneeded totalSupply call and added test case.
bweick Apr 12, 2021
a31f37c
Standardize exchange adapter boolean
0xSachinK Apr 12, 2021
d830a0b
Merge branch 'alpha/general-index-module' of https://github.com/SetPr…
0xSachinK Apr 12, 2021
0d41b3a
Move to swapExactTokensForTokens as boolean standard for exchange ada…
0xSachinK Apr 12, 2021
34e2f0d
fix coverage bug
0xSachinK Apr 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions contracts/interfaces/IExchangeAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ interface IExchangeAdapter {
external
view
returns (address, uint256, bytes memory);
function generateDataParam(
address _sellComponent,
address _buyComponent,
bool _fixIn
) external view returns (bytes memory);
}
124 changes: 124 additions & 0 deletions contracts/protocol/integration/BalancerV1ExchangeAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Copyright 2021 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";

/**
* @title BalancerV1ExchangeAdapter
* @author Set Protocol
*
* A Balancer exchange adapter that returns calldata for trading.
*/
contract BalancerV1ExchangeAdapter {

/* ============ Constants ============ */

// Amount of pools examined when fetching quote
uint256 private constant BALANCER_POOL_LIMIT = 3;

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

// Address of Uniswap V2 Router02 contract
address public immutable balancerProxy;
// Balancer proxy function string for swapping exact tokens for a minimum of receive tokens
string internal constant EXACT_IN = "smartSwapExactIn(address,address,uint256,uint256,uint256)";
// Balancer proxy function string for swapping tokens for an exact amount of receive tokens
string internal constant EXACT_OUT = "smartSwapExactOut(address,address,uint256,uint256,uint256)";

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

/**
* Set state variables
*
* @param _balancerProxy Balancer exchange proxy address
*/
constructor(address _balancerProxy) public {
balancerProxy = _balancerProxy;
}

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

/**
* Return calldata for Balancer Proxy. Bool to select trade function is encoded in the arbitrary data parameter.
*
* @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 Fixed/Max amount of source token to sell
* @param _destinationQuantity Min/Fixed amount of destination tokens to receive
* @param _data Arbitrary bytes containing bool to determine function string
*
* @return address Target contract address
* @return uint256 Call value
* @return bytes Trade calldata
*/
function getTradeCalldata(
address _sourceToken,
address _destinationToken,
address _destinationAddress,
uint256 _sourceQuantity,
uint256 _destinationQuantity,
bytes memory _data
)
external
view
returns (address, uint256, bytes memory)
{
(
bool shouldSwapFixedInputAmount
) = abi.decode(_data, (bool));

bytes memory callData = abi.encodeWithSignature(
shouldSwapFixedInputAmount ? EXACT_IN : EXACT_OUT,
_sourceToken,
_destinationToken,
shouldSwapFixedInputAmount ? _sourceQuantity : _destinationQuantity,
shouldSwapFixedInputAmount ? _destinationQuantity : _sourceQuantity,
BALANCER_POOL_LIMIT
);

return (balancerProxy, 0, callData);
}

/**
* Generate data parameter to be passed to `getTradeCallData`. Returns encoded bool to select trade function.
*
* @param _sellComponent Address of the token to be sold
* @param _buyComponent Address of the token to be bought
* @param _fixIn Boolean representing if input tokens amount is fixed
*
* @return bytes Data parameter to be passed to `getTradeCallData`
*/
function generateDataParam(address _sellComponent, address _buyComponent, bool _fixIn)
external
view
returns (bytes memory)
{
return abi.encode(_fixIn);
}

/**
* Returns the address to approve source tokens to for trading. This is the Balancer proxy address
*
* @return address Address of the contract to approve tokens to
*/
function getSpender() external view returns (address) {
return balancerProxy;
}
}
43 changes: 31 additions & 12 deletions contracts/protocol/integration/UniswapV2ExchangeAdapterV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ pragma experimental "ABIEncoderV2";
* @title UniswapV2ExchangeAdapterV2
* @author Set Protocol
*
* A Uniswap Router02 exchange adapter that returns calldata for trading. Includes option for 2 different trade types on Uniswap
* A Uniswap Router02 exchange adapter that returns calldata for trading. Includes option for 2 different trade types on Uniswap.
*
* CHANGE LOG:
* - Add helper that encodes path and boolean into bytes
* - Generalized ability to choose whether to swap an exact amount of source token for a min amount of receive token or swap a max amount of source token for
* an exact amount of receive token
* - Add helper to generate data parameter for `getTradeCallData`
*
*/
contract UniswapV2ExchangeAdapterV2 {
Expand Down Expand Up @@ -64,8 +65,8 @@ contract UniswapV2ExchangeAdapterV2 {
* @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 _sourceQuantity Fixed/Max amount of source token to sell
* @param _destinationQuantity Min/Fixed amount of destination token to buy
* @param _data Arbitrary bytes containing trade path and bool to determine function string
*
* @return address Target contract address
Expand All @@ -77,7 +78,7 @@ contract UniswapV2ExchangeAdapterV2 {
address _destinationToken,
address _destinationAddress,
uint256 _sourceQuantity,
uint256 _minDestinationQuantity,
uint256 _destinationQuantity,
bytes memory _data
)
external
Expand All @@ -86,22 +87,40 @@ contract UniswapV2ExchangeAdapterV2 {
{
(
address[] memory path,
bool shouldSwapTokensForExactTokens
bool shouldSwapExactTokensForTokens
) = abi.decode(_data, (address[],bool));

// If shouldSwapTokensForExactTokens, use appropriate function string and flip source and destination quantities to conform with Uniswap interface
bytes memory callData = abi.encodeWithSignature(
shouldSwapTokensForExactTokens ? SWAP_TOKENS_FOR_EXACT_TOKENS : SWAP_EXACT_TOKENS_FOR_TOKENS,
shouldSwapTokensForExactTokens ? _minDestinationQuantity : _sourceQuantity,
shouldSwapTokensForExactTokens ? _sourceQuantity : _minDestinationQuantity,
shouldSwapExactTokensForTokens ? SWAP_EXACT_TOKENS_FOR_TOKENS : SWAP_TOKENS_FOR_EXACT_TOKENS,
shouldSwapExactTokensForTokens ? _sourceQuantity : _destinationQuantity,
shouldSwapExactTokensForTokens ? _destinationQuantity : _sourceQuantity,
path,
_destinationAddress,
block.timestamp
);

return (router, 0, callData);
}

/**
* Generate data parameter to be passed to `getTradeCallData`. Returns encoded trade paths and bool to select trade function.
*
* @param _sellComponent Address of the token to be sold
* @param _buyComponent Address of the token to be bought
* @param _fixIn Boolean representing if input tokens amount is fixed
*
* @return bytes Data parameter to be passed to `getTradeCallData`
*/
function generateDataParam(address _sellComponent, address _buyComponent, bool _fixIn)
external
view
returns (bytes memory)
{
address[] memory path = new address[](2);
path[0] = _sellComponent;
path[1] = _buyComponent;
return abi.encode(path, _fixIn);
}

/**
* Returns the address to approve source tokens to for trading. This is the Uniswap router address
*
Expand All @@ -116,7 +135,7 @@ contract UniswapV2ExchangeAdapterV2 {
*
* @return bytes Encoded data used for trading on Uniswap
*/
function getUniswapExchangeData(address[] memory _path, bool _shouldSwapTokensForExactTokens) external view returns (bytes memory) {
return abi.encode(_path, _shouldSwapTokensForExactTokens);
function getUniswapExchangeData(address[] memory _path, bool _shouldSwapExactTokensForTokens) external view returns (bytes memory) {
return abi.encode(_path, _shouldSwapExactTokensForTokens);
}
}
Loading