-
Notifications
You must be signed in to change notification settings - Fork 107
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
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
5b416f4
Add GenrealIndexModule contract
0xSachinK 834ba66
Fix bugs & Add UniswapV2ExchangeAdapterV3
0xSachinK 45ebf67
Add BalancerV1ExchangeAdapter
0xSachinK c220c4f
Add initial tests & add fee collection to GeneralIndexModule
0xSachinK ef936bc
Add tests for trade function
0xSachinK b165c4a
Add javadocs to contract
0xSachinK adc3018
Fix javadoc bugs
0xSachinK e29e2c5
Add contract initalization test
0xSachinK 816ce4a
Fix bug
0xSachinK 0efc7e7
Add suggested changes
0xSachinK 68d4317
Add raiseAssetTargets test
0xSachinK 171c2ea
Add suggested changes & tests
0xSachinK 189d740
modify raiseTargetPercentage
0xSachinK 7a7afaa
Add tradeRemainingWETH tests
0xSachinK 1db03b5
Add tests for SetToken with weth as component
0xSachinK 276bb5e
Add more tests
0xSachinK 85755e5
fix bug in tests
0xSachinK c2258ef
Add tests for empty arrays
0xSachinK 3433ffa
Add min/max limit amounts to prevent MEV
0xSachinK 9cff0a2
Add strict max/min limit amounts to tests
0xSachinK 8f6f9ae
Fix max floating amounts
0xSachinK e38d02a
Add suggested changes to GeneralIndexModule.sol
0xSachinK 2846a60
Refactor all tests, add suggested changes
0xSachinK dd69996
fix coverage bug
0xSachinK cfb2c94
Remove .skip from tests
0xSachinK eaa85ec
Add cacheBeforeEach blocks to speed up tests
0xSachinK 3f0e0a2
Add test to revert when exchange adapter not found
0xSachinK bf10576
Added logic to be able to raiseAssetTargets when positionMultiplier <…
bweick 6d7d91a
Complete coverage. Adding tests for getComponentTradeQuantityAndDirec…
bweick 31ab0ed
Remove console.log
0xSachinK 7699ae5
Removed unneeded totalSupply call and added test case.
bweick a31f37c
Standardize exchange adapter boolean
0xSachinK d830a0b
Merge branch 'alpha/general-index-module' of https://github.com/SetPr…
0xSachinK 0d41b3a
Move to swapExactTokensForTokens as boolean standard for exchange ada…
0xSachinK 34e2f0d
fix coverage bug
0xSachinK 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
133 changes: 133 additions & 0 deletions
133
contracts/protocol/integration/BalancerV1ExchangeAdapter.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,133 @@ | ||
| /* | ||
| 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; | ||
| } | ||
|
|
||
| /** | ||
| * Helper that returns the encoded data of boolean indicating the Balancer swap function to use. | ||
| * | ||
| * @return bytes Encoded data used for trading on Balancer | ||
| */ | ||
| function getBalancerExchangeData(bool _shouldSwarFixeInputtAmount) external view returns (bytes memory) { | ||
| return abi.encode(_shouldSwarFixeInputtAmount); | ||
| } | ||
| } | ||
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
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.