Skip to content

Commit

Permalink
Merge pull request #4 from zeriontech/additional-adapters
Browse files Browse the repository at this point in the history
Adapters for Compound, PoolTogether, and others
  • Loading branch information
sobolev-igor authored Feb 20, 2020
2 parents 3f0bdd7 + b802609 commit bde61ed
Show file tree
Hide file tree
Showing 34 changed files with 1,810 additions and 2,004 deletions.
1 change: 1 addition & 0 deletions .solcover.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
'ERC20.sol',
'Logic.sol',
'adapters/Adapter.sol',
'adapters/MKRAdapter.sol',
'interactiveAdapters',
'mock',
],
Expand Down
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"compiler-version": [
"error",
"0.6.1"
"0.6.2"
]
}
}
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ This is a project with Zerion Smart Contracts interacting with different DeFi pr
- [AdapterRegistry](#adapterregistry-is-AdapterAssetsManager)
- [AdapterAssetsManager](#AdapterAssetsManager-is-ownable)
- [Ownable](#ownable)
- [adapters/AaveAdapter](#aaveadapter-is-Adapter)
- [adapters/CompoundAdapter](#compoundadapter-is-Adapter)
- [adapters/CurveAdapter](#curveadapter-is-Adapter)
- [adapters/DSRAdapter](#dsradapter-is-Adapter)
- [adapters/MCDAdapter](#mcdadapter-is-Adapter)
- [adapters/MKRAdapter (abstract contract)](#mkradapter-abstract-contract)
- [adapters/PoolTogetherAdapter](#pooltogetheradapter-is-Adapter)
- [adapters/SynthetixAdapter](#synthetixadapter-is-Adapter)
- [adapters/CurveAdapter](#curveadapter-is-Adapter)
- [adapters/ZrxAdapter](#zrxadapter-is-Adapter)
- [adapters/Adapter (abstract contract)](#Adapter-abstract-contract)
- [Logic](#logic)
- [TokenSpender](#tokenspender)
Expand Down Expand Up @@ -96,11 +102,35 @@ Base contract for `AdapterAssetsManager` and `Logic` contracts.
Implements `Ownable` logic.
Includes `onlyOwner` modifier, `transferOwnership()` function, and public state variable `owner`.

## AaveAdapter is [Adapter](#Adapter-abstract-contract)

Adapter for Aave protocol.

## CompoundAdapter is [Adapter](#Adapter-abstract-contract)

Adapter for Compound protocol.

## CurveAdapter is [Adapter](#Adapter-abstract-contract)

Adapter for [curve.fi](https://compound.curve.fi/) protocol.
Currently, there is the only pool with cDAI/cUSDC locked on it.

## DSRAdapter is [Adapter](#Adapter-abstract-contract)

Adapter for DSR protocol.

Interactive adapter will not be implemented.
## MCDAdapter is [Adapter](#Adapter-abstract-contract)

Adapter for MCD vaults.

## MKRAdapter (abstract contract)

Base contract for Maker adapters.
It includes all the required constants and `pure` functions with calculations.

## PoolTogetherAdapter is [Adapter](#Adapter-abstract-contract)

Adapter for PoolTogether protocol.

## SynthetixAdapter is [Adapter](#Adapter-abstract-contract)

Expand All @@ -110,12 +140,9 @@ Adapter for Synthetix protocol.
- amount of SNX tokens locked by minting sUSD tokens (positive);
- amount of sUSD that should be burned to unlock SNX tokens (negative).

Interactive adapter will not be implemented.

## CurveAdapter is [Adapter](#Adapter-abstract-contract)
## ZrxAdapter is [Adapter](#Adapter-abstract-contract)

Adapter for [curve.fi](https://compound.curve.fi/) protocol.
Currently, there is the only pool with cDAI/cUSDC locked on it.
Adapter for Zrx protocol.

## Adapter (abstract contract)

Expand Down
2 changes: 1 addition & 1 deletion contracts/AdapterAssetsManager.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity 0.6.1;
pragma solidity 0.6.2;
pragma experimental ABIEncoderV2;

import { Ownable } from "./Ownable.sol";
Expand Down
25 changes: 19 additions & 6 deletions contracts/AdapterRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity 0.6.1;
pragma solidity 0.6.2;
pragma experimental ABIEncoderV2;

import { Adapter } from "./adapters/Adapter.sol";
Expand All @@ -15,11 +15,13 @@ import {

/**
* @title Registry for protocol adapters.
* @notice balance() and exchangeRates() functions
* @notice getBalances() and getRates() functions
* with different arguments implement the main functionality.
*/
contract AdapterRegistry is AdapterAssetsManager {

address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;

constructor(
address[] memory _adapters,
address[][] memory _assets
Expand All @@ -42,7 +44,7 @@ contract AdapterRegistry is AdapterAssetsManager {
address[] memory adapters = getAdapters();
ProtocolDetail[] memory protocolDetails = new ProtocolDetail[](adapters.length);

for (uint i = 0; i < adapters.length; i++) {
for (uint256 i = 0; i < adapters.length; i++) {
protocolDetails[i] = ProtocolDetail({
name: Adapter(adapters[i]).getProtocolName(),
balances: getBalances(user, adapters[i]),
Expand All @@ -67,7 +69,7 @@ contract AdapterRegistry is AdapterAssetsManager {
address[] memory adapters = getAdapters();
ProtocolBalance[] memory protocolBalances = new ProtocolBalance[](adapters.length);

for (uint i = 0; i < adapters.length; i++) {
for (uint256 i = 0; i < adapters.length; i++) {
protocolBalances[i] = ProtocolBalance({
name: Adapter(adapters[i]).getProtocolName(),
balances: getBalances(user, adapters[i])
Expand All @@ -89,7 +91,7 @@ contract AdapterRegistry is AdapterAssetsManager {
address[] memory adapters = getAdapters();
ProtocolRate[] memory protocolRates = new ProtocolRate[](adapters.length);

for (uint i = 0; i < adapters.length; i++) {
for (uint256 i = 0; i < adapters.length; i++) {
protocolRates[i] = ProtocolRate({
name: Adapter(adapters[i]).getProtocolName(),
rates: getRates(adapters[i])
Expand Down Expand Up @@ -137,7 +139,7 @@ contract AdapterRegistry is AdapterAssetsManager {
assetBalances[i] = AssetBalance({
asset: asset,
amount: Adapter(adapter).getAssetAmount(asset, user),
decimals: ERC20(asset).decimals()
decimals: getAssetDecimals(asset)
});
}

Expand Down Expand Up @@ -185,4 +187,15 @@ contract AdapterRegistry is AdapterAssetsManager {

return rates;
}

function getAssetDecimals(
address asset
)
internal
view
returns (uint8)
{
return asset == ETH ? uint8(18) : ERC20(asset).decimals();
}

}
2 changes: 1 addition & 1 deletion contracts/ERC20.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity 0.6.1;
pragma solidity 0.6.2;
pragma experimental ABIEncoderV2;


Expand Down
2 changes: 1 addition & 1 deletion contracts/Ownable.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity 0.6.1;
pragma solidity 0.6.2;
pragma experimental ABIEncoderV2;


Expand Down
4 changes: 2 additions & 2 deletions contracts/Structs.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity 0.6.1;
pragma solidity 0.6.2;
pragma experimental ABIEncoderV2;


Expand All @@ -23,7 +23,7 @@ struct ProtocolRate {

struct AssetBalance {
address asset;
int128 amount;
int256 amount;
uint8 decimals;
}

Expand Down
87 changes: 87 additions & 0 deletions contracts/adapters/AaveAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
pragma solidity 0.6.2;
pragma experimental ABIEncoderV2;

import { Adapter } from "./Adapter.sol";
import { Component } from "../Structs.sol";


/**
* @dev LendingPoolAddressesProvider contract interface.
* Only the functions required for AaveAdapter contract are added.
* The LendingPoolAddressesProvider contract is available here
* https://github.com/aave/aave-protocol/blob/master/contracts/configuration/LendingPoolAddressesProvider.sol.
*/
interface LendingPoolAddressesProvider {
function getLendingPool() external view returns (LendingPool);
}


/**
* @dev LendingPool contract interface.
* Only the functions required for AaveAdapter contract are added.
* The LendingPool contract is available here
* https://github.com/aave/aave-protocol/blob/master/contracts/lendingpool/LendingPool.sol.
*/
interface LendingPool {
function getUserReserveData(address, address)
external
view
returns (
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
bool
);
}


/**
* @title Adapter for Aave protocol.
* @dev Implementation of Adapter interface.
*/
contract AaveAdapter is Adapter {

address internal constant PROVIDER = 0x24a42fD28C976A61Df5D00D0599C34c4f90748c8;

/**
* @return Name of the protocol.
* @dev Implementation of Adapter function.
*/
function getProtocolName() external pure override returns (string memory) {
return("Aave");
}

/**
* @return Amount of asset locked on the protocol by the given user.
* @dev Implementation of Adapter function.
*/
function getAssetAmount(address asset, address user) external view override returns (int256) {
LendingPool pool = LendingPoolAddressesProvider(PROVIDER).getLendingPool();

(uint256 depositAmount, uint256 debtAmount, , , , , , , , ) =
pool.getUserReserveData(asset, user);

return depositAmount > 0 ? int256(depositAmount) : -int256(debtAmount);
}

/**
* @return Struct with underlying assets rates for the given asset.
* @dev Implementation of Adapter function.
*/
function getUnderlyingRates(address asset) external view override returns (Component[] memory) {
Component[] memory components = new Component[](1);

components[0] = Component({
underlying: asset,
rate: uint256(1e18)
});

return components;
}
}
10 changes: 5 additions & 5 deletions contracts/adapters/Adapter.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity 0.6.1;
pragma solidity 0.6.2;
pragma experimental ABIEncoderV2;

import { Component } from "../Structs.sol";
Expand All @@ -8,20 +8,20 @@ import { Component } from "../Structs.sol";
* @title Base contract for protocol adapters.
* @dev getProtocolName(), getAssetAmount(), and getUnderlyingRates() functions MUST be implemented.
*/
abstract contract Adapter {
interface Adapter {

/**
* @dev MUST return name of the protocol.
*/
function getProtocolName() external pure virtual returns (string memory);
function getProtocolName() external pure returns (string memory);

/**
* @dev MUST return amount of the given asset locked on the protocol by the given user.
*/
function getAssetAmount(address asset, address user) external view virtual returns (int128);
function getAssetAmount(address asset, address user) external view returns (int256);

/**
* @dev MUST return struct with underlying assets rates for the given asset.
*/
function getUnderlyingRates(address asset) external view virtual returns (Component[] memory);
function getUnderlyingRates(address asset) external view returns (Component[] memory);
}
Loading

0 comments on commit bde61ed

Please sign in to comment.