Skip to content

Commit 7657f9a

Browse files
authored
Added an exchangeData param on the GIM (#76)
Added an exchangeData param on the GIM. Allows passing in arbitrary parameter or execution info to an exchange. Made all GIM adapters specific to GIM and not compatible with TradeModule.
1 parent 944c015 commit 7657f9a

File tree

12 files changed

+492
-198
lines changed

12 files changed

+492
-198
lines changed

contracts/interfaces/IExchangeAdapter.sol

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,4 @@ interface IExchangeAdapter {
3030
external
3131
view
3232
returns (address, uint256, bytes memory);
33-
function generateDataParam(
34-
address _sellComponent,
35-
address _buyComponent,
36-
bool _fixIn
37-
) external view returns (bytes memory);
3833
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2021 Set Labs Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache License, Version 2.0
17+
*/
18+
pragma solidity 0.6.10;
19+
20+
interface IIndexExchangeAdapter {
21+
function getSpender() external view returns(address);
22+
23+
/**
24+
* Returns calldata for executing trade on given adapter's exchange when using the GeneralIndexModule.
25+
*
26+
* @param _sourceToken Address of source token to be sold
27+
* @param _destinationToken Address of destination token to buy
28+
* @param _destinationAddress Address that assets should be transferred to
29+
* @param _isSendTokenFixed Boolean indicating if the send quantity is fixed, used to determine correct trade interface
30+
* @param _sourceQuantity Fixed/Max amount of source token to sell
31+
* @param _destinationQuantity Min/Fixed amount of destination tokens to receive
32+
* @param _data Arbitrary bytes that can be used to store exchange specific parameters or logic
33+
*
34+
* @return address Target contract address
35+
* @return uint256 Call value
36+
* @return bytes Trade calldata
37+
*/
38+
function getTradeCalldata(
39+
address _sourceToken,
40+
address _destinationToken,
41+
address _destinationAddress,
42+
bool _isSendTokenFixed,
43+
uint256 _sourceQuantity,
44+
uint256 _destinationQuantity,
45+
bytes memory _data
46+
)
47+
external
48+
view
49+
returns (address, uint256, bytes memory);
50+
}

contracts/protocol/integration/exchange/BalancerV1ExchangeAdapter.sol renamed to contracts/protocol/integration/index-exchange/BalancerV1IndexExchangeAdapter.sol

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
pragma solidity 0.6.10;
2020
pragma experimental "ABIEncoderV2";
2121

22+
import { IIndexExchangeAdapter } from "../../../interfaces/IIndexExchangeAdapter.sol";
23+
2224
/**
23-
* @title BalancerV1ExchangeAdapter
25+
* @title BalancerV1IndexExchangeAdapter
2426
* @author Set Protocol
2527
*
26-
* A Balancer exchange adapter that returns calldata for trading.
28+
* A Balancer exchange adapter that returns calldata for trading with GeneralIndexModule, allows trading a fixed input amount or for a fixed
29+
* output amount.
2730
*/
28-
contract BalancerV1ExchangeAdapter {
31+
contract BalancerV1IndexExchangeAdapter is IIndexExchangeAdapter {
2932

3033
/* ============ Constants ============ */
3134

@@ -55,14 +58,17 @@ contract BalancerV1ExchangeAdapter {
5558
/* ============ External Getter Functions ============ */
5659

5760
/**
58-
* Return calldata for Balancer Proxy. Bool to select trade function is encoded in the arbitrary data parameter.
61+
* Return calldata for Balancer Proxy, _isSendTokenFixed indicates whether a fixed amount of token should be sold for an unfixed amount, or
62+
* if an unfixed amount of token should be spent for a fixed amount.
63+
*
64+
* Note: When _isSendTokenFixed is false, _sourceQuantity is defined as the max token quantity you are willing to trade, and
65+
* _destinationQuantity is the exact quantity of token you are receiving.
5966
*
6067
* @param _sourceToken Address of source token to be sold
6168
* @param _destinationToken Address of destination token to buy
62-
* @param _destinationAddress Address that assets should be transferred to
69+
* @param _isSendTokenFixed Boolean indicating if the send quantity is fixed, used to determine correct trade interface
6370
* @param _sourceQuantity Fixed/Max amount of source token to sell
6471
* @param _destinationQuantity Min/Fixed amount of destination tokens to receive
65-
* @param _data Arbitrary bytes containing bool to determine function string
6672
*
6773
* @return address Target contract address
6874
* @return uint256 Call value
@@ -71,63 +77,35 @@ contract BalancerV1ExchangeAdapter {
7177
function getTradeCalldata(
7278
address _sourceToken,
7379
address _destinationToken,
74-
address _destinationAddress,
80+
address /*_destinationAddress*/,
81+
bool _isSendTokenFixed,
7582
uint256 _sourceQuantity,
7683
uint256 _destinationQuantity,
77-
bytes memory _data
84+
bytes memory /*_data*/
7885
)
7986
external
8087
view
88+
override
8189
returns (address, uint256, bytes memory)
8290
{
83-
(
84-
bool shouldSwapFixedInputAmount
85-
) = abi.decode(_data, (bool));
86-
8791
bytes memory callData = abi.encodeWithSignature(
88-
shouldSwapFixedInputAmount ? EXACT_IN : EXACT_OUT,
92+
_isSendTokenFixed ? EXACT_IN : EXACT_OUT,
8993
_sourceToken,
9094
_destinationToken,
91-
shouldSwapFixedInputAmount ? _sourceQuantity : _destinationQuantity,
92-
shouldSwapFixedInputAmount ? _destinationQuantity : _sourceQuantity,
95+
_isSendTokenFixed ? _sourceQuantity : _destinationQuantity,
96+
_isSendTokenFixed ? _destinationQuantity : _sourceQuantity,
9397
BALANCER_POOL_LIMIT
9498
);
9599

96100
return (balancerProxy, 0, callData);
97101
}
98102

99-
/**
100-
* Generate data parameter to be passed to `getTradeCallData`. Returns encoded bool to select trade function.
101-
*
102-
* @param _sourceToken Address of the source token to be sold
103-
* @param _destinationToken Address of the destination token to buy
104-
* @param _fixIn Boolean representing if input tokens amount is fixed
105-
*
106-
* @return bytes Data parameter to be passed to `getTradeCallData`
107-
*/
108-
function generateDataParam(address _sourceToken, address _destinationToken, bool _fixIn)
109-
external
110-
pure
111-
returns (bytes memory)
112-
{
113-
return abi.encode(_fixIn);
114-
}
115-
116103
/**
117104
* Returns the address to approve source tokens to for trading. This is the Balancer proxy address
118105
*
119106
* @return address Address of the contract to approve tokens to
120107
*/
121-
function getSpender() external view returns (address) {
108+
function getSpender() external view override returns (address) {
122109
return balancerProxy;
123110
}
124-
125-
/**
126-
* Helper that returns the encoded data of boolean indicating the Balancer function to use
127-
*
128-
* @return bytes Encoded data used for trading on Balancer
129-
*/
130-
function getBalancerExchangeData(bool _shouldSwapFixedInputAmount) external pure returns (bytes memory) {
131-
return abi.encode(_shouldSwapFixedInputAmount);
132-
}
133111
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Copyright 2021 Set Labs Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache License, Version 2.0
17+
*/
18+
19+
pragma solidity 0.6.10;
20+
pragma experimental "ABIEncoderV2";
21+
22+
import { IIndexExchangeAdapter } from "../../../interfaces/IIndexExchangeAdapter.sol";
23+
24+
/**
25+
* @title UniswapV2IndexExchangeAdapter
26+
* @author Set Protocol
27+
*
28+
* A Uniswap Router02 exchange adapter that returns calldata for trading with GeneralIndexModule, allows encoding a trade with a fixed input quantity or
29+
* a fixed output quantity.
30+
*/
31+
contract UniswapV2IndexExchangeAdapter is IIndexExchangeAdapter {
32+
33+
/* ============ State Variables ============ */
34+
35+
// Address of Uniswap V2 Router02 contract
36+
address public immutable router;
37+
// Uniswap router function string for swapping exact tokens for a minimum of receive tokens
38+
string internal constant SWAP_EXACT_TOKENS_FOR_TOKENS = "swapExactTokensForTokens(uint256,uint256,address[],address,uint256)";
39+
// Uniswap router function string for swapping tokens for an exact amount of receive tokens
40+
string internal constant SWAP_TOKENS_FOR_EXACT_TOKENS = "swapTokensForExactTokens(uint256,uint256,address[],address,uint256)";
41+
42+
/* ============ Constructor ============ */
43+
44+
/**
45+
* Set state variables
46+
*
47+
* @param _router Address of Uniswap V2 Router02 contract
48+
*/
49+
constructor(address _router) public {
50+
router = _router;
51+
}
52+
53+
/* ============ External Getter Functions ============ */
54+
55+
/**
56+
* Return calldata for trading Uniswap V2 Router02. Trade paths are created from input and output tokens, _isSendTokenFixed indicates whether
57+
* a fixed amount of token should be sold or an unfixed amount.
58+
*
59+
* Note: When _isSendTokenFixed is false, _sourceQuantity is defined as the max token quantity you are willing to trade, and
60+
* _destinationQuantity is the exact quantity of token you are receiving.
61+
*
62+
* @param _sourceToken Address of source token to be sold
63+
* @param _destinationToken Address of destination token to buy
64+
* @param _destinationAddress Address that assets should be transferred to
65+
* @param _isSendTokenFixed Boolean indicating if the send quantity is fixed, used to determine correct trade interface
66+
* @param _sourceQuantity Fixed/Max amount of source token to sell
67+
* @param _destinationQuantity Min/Fixed amount of destination token to buy
68+
*
69+
* @return address Target contract address
70+
* @return uint256 Call value
71+
* @return bytes Trade calldata
72+
*/
73+
function getTradeCalldata(
74+
address _sourceToken,
75+
address _destinationToken,
76+
address _destinationAddress,
77+
bool _isSendTokenFixed,
78+
uint256 _sourceQuantity,
79+
uint256 _destinationQuantity,
80+
bytes memory /*_data*/
81+
)
82+
external
83+
view
84+
override
85+
returns (address, uint256, bytes memory)
86+
{
87+
address[] memory path = new address[](2);
88+
path[0] = _sourceToken;
89+
path[1] = _destinationToken;
90+
91+
bytes memory callData = abi.encodeWithSignature(
92+
_isSendTokenFixed ? SWAP_EXACT_TOKENS_FOR_TOKENS : SWAP_TOKENS_FOR_EXACT_TOKENS,
93+
_isSendTokenFixed ? _sourceQuantity : _destinationQuantity,
94+
_isSendTokenFixed ? _destinationQuantity : _sourceQuantity,
95+
path,
96+
_destinationAddress,
97+
block.timestamp
98+
);
99+
return (router, 0, callData);
100+
}
101+
102+
/**
103+
* Returns the address to approve source tokens to for trading. This is the Uniswap router address
104+
*
105+
* @return address Address of the contract to approve tokens to
106+
*/
107+
function getSpender() external view override returns (address) {
108+
return router;
109+
}
110+
}

0 commit comments

Comments
 (0)