forked from zeriontech/defi-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BalancerTokenAdapter.sol
executable file
·189 lines (158 loc) · 6.15 KB
/
BalancerTokenAdapter.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (C) 2020 Zerion Inc. <https://zerion.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity 0.6.5;
pragma experimental ABIEncoderV2;
import { ERC20 } from "../../ERC20.sol";
import { TokenMetadata, Component } from "../../Structs.sol";
import { TokenAdapter } from "../TokenAdapter.sol";
/**
* @dev CToken contract interface.
* Only the functions required for BalancerTokenAdapter contract are added.
* The CToken contract is available here
* github.com/compound-finance/compound-protocol/blob/master/contracts/CToken.sol.
*/
interface CToken {
function isCToken() external view returns (bool);
}
/**
* @dev BPool contract interface.
* Only the functions required for BalancerTokenAdapter contract are added.
* The BPool contract is available here
* github.com/balancer-labs/balancer-core/blob/master/contracts/BPool.sol.
*/
interface BPool {
function getFinalTokens() external view returns (address[] memory);
function getBalance(address) external view returns (uint256);
function getNormalizedWeight(address) external view returns (uint256);
}
/**
* @title Token adapter for Balancer pool tokens.
* @dev Implementation of TokenAdapter interface.
* @author Igor Sobolev <sobolev@zerion.io>
*/
contract BalancerTokenAdapter is TokenAdapter {
/**
* @return TokenMetadata struct with ERC20-style token info.
* @dev Implementation of TokenAdapter interface function.
*/
function getMetadata(address token) external view override returns (TokenMetadata memory) {
return TokenMetadata({
token: token,
name: getPoolName(token),
symbol: ERC20(token).symbol(),
decimals: ERC20(token).decimals()
});
}
/**
* @return Array of Component structs with underlying tokens rates for the given token.
* @dev Implementation of TokenAdapter interface function.
*/
function getComponents(address token) external view override returns (Component[] memory) {
address[] memory underlyingTokensAddresses;
try BPool(token).getFinalTokens() returns (address[] memory result) {
underlyingTokensAddresses = result;
} catch {
underlyingTokensAddresses = new address[](0);
}
uint256 totalSupply = ERC20(token).totalSupply();
Component[] memory underlyingTokens = new Component[](underlyingTokensAddresses.length);
address underlyingToken;
string memory underlyingTokenType;
for (uint256 i = 0; i < underlyingTokens.length; i++) {
underlyingToken = underlyingTokensAddresses[i];
try CToken(underlyingToken).isCToken{gas: 2000}() returns (bool) {
underlyingTokenType = "CToken";
} catch {
underlyingTokenType = "ERC20";
}
underlyingTokens[i] = Component({
token: underlyingToken,
tokenType: underlyingTokenType,
rate: BPool(token).getBalance(underlyingToken) * 1e18 / totalSupply
});
}
return underlyingTokens;
}
function getPoolName(address token) internal view returns (string memory) {
address[] memory underlyingTokensAddresses;
try BPool(token).getFinalTokens() returns (address[] memory result) {
underlyingTokensAddresses = result;
} catch {
return "Unknown pool";
}
string memory poolName = "";
uint256 lastIndex = underlyingTokensAddresses.length - 1;
for (uint256 i = 0; i < underlyingTokensAddresses.length; i++) {
poolName = string(abi.encodePacked(
poolName,
getPoolElement(token, underlyingTokensAddresses[i]),
i == lastIndex ? " pool" : " + "
));
}
return poolName;
}
function getPoolElement(address pool, address token) internal view returns (string memory) {
return string(abi.encodePacked(
convertToString(BPool(pool).getNormalizedWeight(token) / 1e16),
"% ",
getSymbol(token)
));
}
function getSymbol(address token) internal view returns (string memory) {
(, bytes memory returnData) = token.staticcall(
abi.encodeWithSelector(ERC20(token).symbol.selector)
);
if (returnData.length == 32) {
return convertToString(abi.decode(returnData, (bytes32)));
} else {
return abi.decode(returnData, (string));
}
}
/**
* @dev Internal function to convert bytes32 to string and trim zeroes.
*/
function convertToString(bytes32 data) internal pure returns (string memory) {
uint256 length = 0;
bytes memory result;
for (uint256 i = 0; i < 32; i++) {
if (data[i] != bytes1(0)) {
length++;
}
}
result = new bytes(length);
for (uint256 i = 0; i < length; i++) {
result[i] = data[i];
}
return string(result);
}
/**
* @dev Internal function to convert uint256 to string and trim zeroes.
*/
function convertToString(uint256 data) internal pure returns (string memory) {
uint256 length = 0;
uint256 dataCopy = data;
while (dataCopy != 0){
length++;
dataCopy /= 10;
}
bytes memory result = new bytes(length);
dataCopy = data;
for (uint256 i = length - 1; i < length; i--) {
result[i] = bytes1(uint8(48 + dataCopy % 10));
dataCopy /= 10;
}
return string(result);
}
}