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
+ /**
23
+ * @title BalancerV1ExchangeAdapter
24
+ * @author Set Protocol
25
+ *
26
+ * A Balancer exchange adapter that returns calldata for trading.
27
+ */
28
+ contract BalancerV1ExchangeAdapter {
29
+
30
+ /* ============ Constants ============ */
31
+
32
+ // Amount of pools examined when fetching quote
33
+ uint256 private constant BALANCER_POOL_LIMIT = 3 ;
34
+
35
+ /* ============ State Variables ============ */
36
+
37
+ // Address of Uniswap V2 Router02 contract
38
+ address public immutable balancerProxy;
39
+ // Balancer proxy function string for swapping exact tokens for a minimum of receive tokens
40
+ string internal constant EXACT_IN = "smartSwapExactIn(address,address,uint256,uint256,uint256) " ;
41
+ // Balancer proxy function string for swapping tokens for an exact amount of receive tokens
42
+ string internal constant EXACT_OUT = "smartSwapExactOut(address,address,uint256,uint256,uint256) " ;
43
+
44
+ /* ============ Constructor ============ */
45
+
46
+ /**
47
+ * Set state variables
48
+ *
49
+ * @param _balancerProxy Balancer exchange proxy address
50
+ */
51
+ constructor (address _balancerProxy ) public {
52
+ balancerProxy = _balancerProxy;
53
+ }
54
+
55
+ /* ============ External Getter Functions ============ */
56
+
57
+ /**
58
+ * Return calldata for Balancer Proxy. Bool to select trade function is encoded in the arbitrary data parameter.
59
+ *
60
+ * @param _sourceToken Address of source token to be sold
61
+ * @param _destinationToken Address of destination token to buy
62
+ * @param _destinationAddress Address that assets should be transferred to
63
+ * @param _sourceQuantity Fixed/Max amount of source token to sell
64
+ * @param _destinationQuantity Min/Fixed amount of destination tokens to receive
65
+ * @param _data Arbitrary bytes containing bool to determine function string
66
+ *
67
+ * @return address Target contract address
68
+ * @return uint256 Call value
69
+ * @return bytes Trade calldata
70
+ */
71
+ function getTradeCalldata (
72
+ address _sourceToken ,
73
+ address _destinationToken ,
74
+ address _destinationAddress ,
75
+ uint256 _sourceQuantity ,
76
+ uint256 _destinationQuantity ,
77
+ bytes memory _data
78
+ )
79
+ external
80
+ view
81
+ returns (address , uint256 , bytes memory )
82
+ {
83
+ (
84
+ bool shouldSwapFixedInputAmount
85
+ ) = abi.decode (_data, (bool ));
86
+
87
+ bytes memory callData = abi.encodeWithSignature (
88
+ shouldSwapFixedInputAmount ? EXACT_IN : EXACT_OUT,
89
+ _sourceToken,
90
+ _destinationToken,
91
+ shouldSwapFixedInputAmount ? _sourceQuantity : _destinationQuantity,
92
+ shouldSwapFixedInputAmount ? _destinationQuantity : _sourceQuantity,
93
+ BALANCER_POOL_LIMIT
94
+ );
95
+
96
+ return (balancerProxy, 0 , callData);
97
+ }
98
+
99
+ /**
100
+ * Generate data parameter to be passed to `getTradeCallData`. Returns encoded bool to select trade function.
101
+ *
102
+ * @param _sellComponent Address of the token to be sold
103
+ * @param _buyComponent Address of the token to be bought
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 _sellComponent , address _buyComponent , bool _fixIn )
109
+ external
110
+ view
111
+ returns (bytes memory )
112
+ {
113
+ return abi.encode (_fixIn);
114
+ }
115
+
116
+ /**
117
+ * Returns the address to approve source tokens to for trading. This is the Balancer proxy address
118
+ *
119
+ * @return address Address of the contract to approve tokens to
120
+ */
121
+ function getSpender () external view returns (address ) {
122
+ return balancerProxy;
123
+ }
124
+ }
0 commit comments