Skip to content

PieDAO Pie Tokens #36

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 5 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions contracts/adapters/pieDAO/PieDAOPieAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 { ProtocolAdapter } from "../ProtocolAdapter.sol";


/**
* @title Adapter for PieDAO pools.
* @dev Implementation of ProtocolAdapter interface.
* @author Mick de Graaf <mick@dexlab.io>
*/
contract PieDAOPieAdapter is ProtocolAdapter {

string public constant override adapterType = "Asset";

string public constant override tokenType = "PieDAO Pie Token";

/**
* @return Amount of PieDAO pool tokens held by the given account.
* @param token Address of the pool!
* @dev Implementation of ProtocolAdapter interface function.
*/
function getBalance(address token, address account) external view override returns (uint256) {
return ERC20(token).balanceOf(account);
}
}
93 changes: 93 additions & 0 deletions contracts/adapters/pieDAO/PieDAOPieTokenAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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 PieSmartPool contract interface.
* Only the functions required for UniswapAdapter contract are added.
* The BPool contract is available here
* github.com/balancer-labs/balancer-core/blob/master/contracts/BPool.sol.
*/
interface IPieSmartPool {
function getTokens() external view returns (address[] memory);
function getBPool() external view returns (address);
}


/**
* @dev BPool contract interface.
* Only the functions required for UniswapAdapter 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 Pie pool tokens.
* @dev Implementation of TokenAdapter interface.
* @author Mick de Graaf <mick@dexlab.io>
*/
contract PieDAOPieTokenAdapter 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: ERC20(token).name(),
symbol: ERC20(token).symbol(),
decimals: ERC20(token).decimals()
});
}

/**
* @return Array of Component structs with underlying tokens rates for the given asset.
* @dev Implementation of TokenAdapter interface function.
*/
function getComponents(address token) external view override returns (Component[] memory) {
address[] memory underlyingTokensAddresses = IPieSmartPool(token).getTokens();
uint256 totalSupply = ERC20(token).totalSupply();
BPool bPool = BPool(IPieSmartPool(token).getBPool());

Component[] memory underlyingTokens = new Component[](underlyingTokensAddresses.length);
address underlyingToken;

for (uint256 i = 0; i < underlyingTokens.length; i++) {
underlyingToken = underlyingTokensAddresses[i];
underlyingTokens[i] = Component({
token: underlyingToken,
tokenType: "ERC20",
rate: bPool.getBalance(underlyingToken) * 1e18 / totalSupply
});
}

return underlyingTokens;
}

}
118 changes: 118 additions & 0 deletions test/PieDAOPieAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import displayToken from './helpers/displayToken';

const AdapterRegistry = artifacts.require('./AdapterRegistry');
const ProtocolAdapter = artifacts.require('./PieDAOPieAdapter');
const TokenAdapter = artifacts.require('./PieDAOPieTokenAdapter');
const ERC20TokenAdapter = artifacts.require('./ERC20TokenAdapter');

contract('PieDAOPieAdapter', () => {
const BTCPPAddress = '0x0327112423F3A68efdF1fcF402F6c5CB9f7C33fd';
const wbtcAddress = '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599';
const pbtcAddress = '0x5228a22e72ccC52d415EcFd199F99D0665E7733b';
const imbtcAddress = '0x3212b29E33587A00FB1C83346f5dBFA69A458923';
const sbtcAddress = '0xfE18be6b3Bd88A2D2A7f928d00292E7a9963CfC6';

const testAddress = '0xd4DBF96Db2FDf8ED40296d8d104b371aDF7dEE12';

let accounts;
let adapterRegistry;
let protocolAdapterAddress;
let tokenAdapterAddress;
let erc20TokenAdapterAddress;

const btcpp = [
BTCPPAddress,
'PieDAO BTC++',
'BTC++',
'18',
];
const wbtc = [
wbtcAddress,
'Wrapped BTC',
'WBTC',
'8',
];
const pbtc = [
pbtcAddress,
'pTokens BTC',
'pBTC',
'18',
];
const imbtc = [
imbtcAddress,
'The Tokenized Bitcoin',
'imBTC',
'8',
];
const sbtc = [
sbtcAddress,
'Synth sBTC',
'sBTC',
'18',
];

beforeEach(async () => {
accounts = await web3.eth.getAccounts();
await ProtocolAdapter.new({ from: accounts[0] })
.then((result) => {
protocolAdapterAddress = result.address;
});
await TokenAdapter.new({ from: accounts[0] })
.then((result) => {
tokenAdapterAddress = result.address;
});
await ERC20TokenAdapter.new({ from: accounts[0] })
.then((result) => {
erc20TokenAdapterAddress = result.address;
});
await AdapterRegistry.new({ from: accounts[0] })
.then((result) => {
adapterRegistry = result.contract;
});
await adapterRegistry.methods.addProtocols(
['PieDAOPie'],
[[
'Mock Protocol Name',
'Mock protocol description',
'Mock website',
'Mock icon',
'0',
]],
[[
protocolAdapterAddress,
]],
[[[
BTCPPAddress
]]],
)
.send({
from: accounts[0],
gas: '1000000',
});
await adapterRegistry.methods.addTokenAdapters(
['ERC20', 'PieDAO Pie Token'],
[erc20TokenAdapterAddress, tokenAdapterAddress],
)
.send({
from: accounts[0],
gas: '1000000',
});
});

it('should return correct balances', async () => {
await adapterRegistry.methods['getBalances(address)'](testAddress)
.call()
.then((result) => {
displayToken(result[0].adapterBalances[0].balances[0].underlying[0]);
displayToken(result[0].adapterBalances[0].balances[0].underlying[1]);
displayToken(result[0].adapterBalances[0].balances[0].underlying[2]);
displayToken(result[0].adapterBalances[0].balances[0].underlying[3]);
displayToken(result[0].adapterBalances[0].balances[0].base);
assert.deepEqual(result[0].adapterBalances[0].balances[0].base.metadata, btcpp);
assert.deepEqual(result[0].adapterBalances[0].balances[0].underlying[0].metadata, wbtc);
assert.deepEqual(result[0].adapterBalances[0].balances[0].underlying[1].metadata, pbtc);
assert.deepEqual(result[0].adapterBalances[0].balances[0].underlying[2].metadata, imbtc);
assert.deepEqual(result[0].adapterBalances[0].balances[0].underlying[3].metadata, sbtc);
});
});
});