|
| 1 | +// Copyright (C) 2020 Zerion Inc. <https://zerion.io> |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +pragma solidity 0.6.5; |
| 17 | +pragma experimental ABIEncoderV2; |
| 18 | + |
| 19 | +import { ERC20 } from "../../ERC20.sol"; |
| 20 | +import { TokenMetadata, Component } from "../../Structs.sol"; |
| 21 | +import { TokenAdapter } from "../TokenAdapter.sol"; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * @dev PieSmartPool contract interface. |
| 26 | + * Only the functions required for UniswapAdapter contract are added. |
| 27 | + * The BPool contract is available here |
| 28 | + * github.com/balancer-labs/balancer-core/blob/master/contracts/BPool.sol. |
| 29 | + */ |
| 30 | +interface IPieSmartPool { |
| 31 | + function getTokens() external view returns (address[] memory); |
| 32 | + function getBPool() external view returns (address); |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * @dev BPool contract interface. |
| 38 | + * Only the functions required for UniswapAdapter contract are added. |
| 39 | + * The BPool contract is available here |
| 40 | + * github.com/balancer-labs/balancer-core/blob/master/contracts/BPool.sol. |
| 41 | + */ |
| 42 | +interface BPool { |
| 43 | + function getFinalTokens() external view returns (address[] memory); |
| 44 | + function getBalance(address) external view returns (uint256); |
| 45 | + function getNormalizedWeight(address) external view returns (uint256); |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +/** |
| 50 | + * @title Token adapter for Pie pool tokens. |
| 51 | + * @dev Implementation of TokenAdapter interface. |
| 52 | + * @author Mick de Graaf <mick@dexlab.io> |
| 53 | + */ |
| 54 | +contract PieDAOPieTokenAdapter is TokenAdapter { |
| 55 | + |
| 56 | + /** |
| 57 | + * @return TokenMetadata struct with ERC20-style token info. |
| 58 | + * @dev Implementation of TokenAdapter interface function. |
| 59 | + */ |
| 60 | + function getMetadata(address token) external view override returns (TokenMetadata memory) { |
| 61 | + return TokenMetadata({ |
| 62 | + token: token, |
| 63 | + name: ERC20(token).name(), |
| 64 | + symbol: ERC20(token).symbol(), |
| 65 | + decimals: ERC20(token).decimals() |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return Array of Component structs with underlying tokens rates for the given asset. |
| 71 | + * @dev Implementation of TokenAdapter interface function. |
| 72 | + */ |
| 73 | + function getComponents(address token) external view override returns (Component[] memory) { |
| 74 | + address[] memory underlyingTokensAddresses = IPieSmartPool(token).getTokens(); |
| 75 | + uint256 totalSupply = ERC20(token).totalSupply(); |
| 76 | + BPool bPool = BPool(IPieSmartPool(token).getBPool()); |
| 77 | + |
| 78 | + Component[] memory underlyingTokens = new Component[](underlyingTokensAddresses.length); |
| 79 | + address underlyingToken; |
| 80 | + |
| 81 | + for (uint256 i = 0; i < underlyingTokens.length; i++) { |
| 82 | + underlyingToken = underlyingTokensAddresses[i]; |
| 83 | + underlyingTokens[i] = Component({ |
| 84 | + token: underlyingToken, |
| 85 | + tokenType: "ERC20", |
| 86 | + rate: bPool.getBalance(underlyingToken) * 1e18 / totalSupply |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + return underlyingTokens; |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments