Skip to content

Commit

Permalink
Merge pull request #32 from zeriontech/contracts-fixes
Browse files Browse the repository at this point in the history
Fresh updates for new adapters [fixed]
  • Loading branch information
sobolev-igor authored Apr 7, 2020
2 parents d598340 + 2ea43ab commit dd57f00
Show file tree
Hide file tree
Showing 67 changed files with 1,048 additions and 844 deletions.
5 changes: 4 additions & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
],
"compiler-version": [
"error",
"0.6.4"
"0.6.5"
],
"private-vars-leading-underscore": [
"off"
],
"const-name-snakecase": [
"off"
]
}
}
5 changes: 4 additions & 1 deletion .soliumignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ node_modules
./contracts/ProtocolManager.sol
./contracts/Ownable.sol
./contracts/adapters/maker/MKRAdapter.sol
./contracts/adapters/uniswap/UniswapTokenAdapter.sol
./contracts/adapters/uniswap/UniswapV1TokenAdapter.sol
./contracts/adapters/balancer/BalancerTokenAdapter.sol
./contracts/adapters/dydx
./contracts/adapters
./contracts/mock
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
[![Coverage status](https://github.com/zeriontech/protocol-wrappers/workflows/coverage/badge.svg)](https://github.com/zeriontech/defi-sdk/actions?query=workflow:coverage)
[![Lint status](https://github.com/zeriontech/protocol-wrappers/workflows/lint/badge.svg)](https://github.com/zeriontech/defi-sdk/actions?query=workflow:lint)
[![License](https://img.shields.io/github/license/zeriontech/defi-sdk)](https://www.gnu.org/licenses/lgpl-3.0.en.html)
[![Discord](https://img.shields.io/discord/544761450724458498?label=discord)](https://go.zerion.io/discord)
[![Twitter Follow](https://img.shields.io/twitter/follow/zerion_io.svg)](https://twitter.com/intent/follow?screen_name=zerion_io)
[![Discord](https://badgen.net/badge/zerion/Zerion?icon=discord&label=discord)](https://go.zerion.io/discord)
[![Twitter Follow](https://badgen.net/twitter/follow/zerion_io)](https://twitter.com/intent/follow?screen_name=zerion_io)

**DeFi SDK** is an open-source system of smart contracts designed for precise DeFi portfolio accounting. To put it simply, DeFi SDK is the on-chain *balanceOf* for DeFi protocols.

Expand Down Expand Up @@ -140,7 +140,7 @@ and obtain all balances for a given account. The response from the smart-contrac
...
```

## DeFi SDK architecture
## DeFi SDK Architecture

- **ProtocolAdapter** is a special contract for every protocol. Its main purpose is to wrap all the protocol interactions.
There are different types of protocol adapters: "Asset" adapter returns the amount of the account's tokens held on the protocol and the "Debt" adapter returns the amount of the account's debt to the protocol. Some protocols do not use "simple" ERC20 tokens but instead have complex derivatives, for example the Compound protocol has CTokens. The **ProtocolAdapter** contract also provides information about the type of tokens used within it.
Expand All @@ -150,11 +150,12 @@ Its main purpose is to provide ERC20-style token metadata as well as information

More detailed documentation about contracts can be found in [adapters](../../wiki/Adapters) and [AdapterRegistry](../../wiki/AdapterRegistry) documentation.

## Supported protocols
## Supported Protocols

| Protocol Name | Description | Protocol Adapters | Token Adapters |
| :-----------: | :---------: | :---------------: | :------------: |
| [Aave](./contracts/adapters/aave) | Decentralized lending & borrowing protocol. | [Asset adapter](./contracts/adapters/aave/AaveAssetAdapter.sol) <br> [Debt adapter](contracts/adapters/aave/AaveDebtAdapter.sol) | ["AToken"](./contracts/adapters/aave/AaveTokenAdapter.sol) |
| [Balancer](./contracts/adapters/balancer) | Non-custodial portfolio manager, liquidity provider, and price sensor. | [Asset adapter](./contracts/adapters/balancer/BalancerAdapter.sol) supports all Balancer pools | ["Balancer pool token"](./contracts/adapters/aave/BalancerTokenAdapter.sol) |
| [Compound](./contracts/adapters/compound) | Decentralized lending & borrowing protocol. | [Asset adapter](./contracts/adapters/compound/CompoundAssetAdapter.sol) <br> [Debt adapter](./contracts/adapters/compound/CompoundDebtAdapter.sol) | ["CToken"](./contracts/adapters/compound/CompoundTokenAdapter.sol) |
| [Curve](./contracts/adapters/curve) | Exchange liquidity pool for stablecoin trading. Supports Compound, Y, and BUSD pools. | [Asset adapter](./contracts/adapters/curve/CurveAdapter.sol) | ["Curve pool token"](contracts/adapters/curve/CurveTokenAdapter.sol) |
| [dYdX](./contracts/adapters/dydx) | Decentralized trading platform. All 4 markets (WETH, SAI, USDC, DAI) are supported. | [Asset adapter](./contracts/adapters/dydx/DyDxAssetAdapter.sol) <br> [Debt adapter](./contracts/adapters/dydx/DyDxDebtAdapter.sol) ||
Expand All @@ -168,22 +169,22 @@ More detailed documentation about contracts can be found in [adapters](../../wik
| [Uniswap V1](./contracts/adapters/uniswap) | Automated liquidity protocol. Top 30 pools are added to the **AdapterRegistry** contract, however adapter supports all Uniswap pools. | [Asset adapter](./contracts/adapters/uniswap/UniswapV1Adapter.sol) supports all Uniswap pools | ["Uniswap V1 pool token"](./contracts/adapters/uniswap/UniswapV1TokenAdapter.sol) |
| [0x Staking](./contracts/adapters/zrx) | Liquidity rewards for staking ZRX. | [Asset adapter](./contracts/adapters/zrx/ZrxAdapter.sol) ||

## How to add your adapter
## How to Add Your Adapter

The full instructions on how to add a custom adapter to the **AdapterRegistry** contract may be found in our [wiki](../../wiki/Adding-new-adapters).

If you have questions and/or want to add your adapter to Zerion reach out to us on our [Discord server](https://go.zerion.io/discord).


## What’s next for DeFi SDK? 🚀
## What’s Next for DeFi SDK? 🚀

This first version of DeFi SDK is for read-only accounting purposes. Our next step is to introduce Interactive Adapters that allow users to make cross-protocol transactions from a single interface. We are incredibly excited to work with developers, users and the wider DeFi community to make these integrations as secure and accessible as possible. Watch this space, because the “De” in DeFi is about to get a whole lot more user-friendly!

## Security Vulnerabilities 🛡

If you discover a security vulnerability within DeFi SDK, please send us an e-mail at [email protected]. All security vulnerabilities will be promptly addressed.

## Dev notes
## Dev Notes

This project uses Truffle and web3js for all Ethereum interactions and testing.

Expand Down Expand Up @@ -217,4 +218,4 @@ Currently, unsupported files are ignored.

## License

All smart contracts are released under LGPL v.3.
All smart contracts are released under GNU LGPLv3.
137 changes: 91 additions & 46 deletions contracts/AdapterRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@
// 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.4;
pragma solidity 0.6.5;
pragma experimental ABIEncoderV2;

import { Ownable } from "./Ownable.sol";
import { ProtocolManager } from "./ProtocolManager.sol";
import { TokenAdapterManager } from "./TokenAdapterManager.sol";
import { ProtocolAdapter } from "./adapters/ProtocolAdapter.sol";
import { TokenAdapter } from "./adapters/TokenAdapter.sol";
import { Strings } from "./Strings.sol";
import {
ProtocolBalance,
ProtocolMetadata,
Expand All @@ -32,6 +26,12 @@ import {
TokenMetadata,
Component
} from "./Structs.sol";
import { Strings } from "./Strings.sol";
import { Ownable } from "./Ownable.sol";
import { ProtocolManager } from "./ProtocolManager.sol";
import { TokenAdapterManager } from "./TokenAdapterManager.sol";
import { ProtocolAdapter } from "./adapters/ProtocolAdapter.sol";
import { TokenAdapter } from "./adapters/TokenAdapter.sol";


/**
Expand Down Expand Up @@ -107,15 +107,29 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
returns (ProtocolBalance[] memory)
{
ProtocolBalance[] memory protocolBalances = new ProtocolBalance[](protocolNames.length);
uint256 counter = 0;

for (uint256 i = 0; i < protocolNames.length; i++) {
protocolBalances[i] = ProtocolBalance({
metadata: protocolMetadata[protocolNames[i]],
adapterBalances: getAdapterBalances(account, protocolAdapters[protocolNames[i]])
});
if (protocolBalances[i].adapterBalances.length > 0) {
counter++;
}
}

return protocolBalances;
ProtocolBalance[] memory nonZeroProtocolBalances = new ProtocolBalance[](counter);
counter = 0;

for (uint256 i = 0; i < protocolNames.length; i++) {
if (protocolBalances[i].adapterBalances.length > 0) {
nonZeroProtocolBalances[counter] = protocolBalances[i];
counter++;
}
}

return nonZeroProtocolBalances;
}

/**
Expand All @@ -132,16 +146,30 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
returns (AdapterBalance[] memory)
{
AdapterBalance[] memory adapterBalances = new AdapterBalance[](adapters.length);
uint256 counter = 0;

for (uint256 i = 0; i < adapterBalances.length; i++) {
adapterBalances[i] = getAdapterBalance(
account,
adapters[i],
supportedTokens[adapters[i]]
);
if (adapterBalances[i].balances.length > 0) {
counter++;
}
}

AdapterBalance[] memory nonZeroAdapterBalances = new AdapterBalance[](counter);
counter = 0;

for (uint256 i = 0; i < adapterBalances.length; i++) {
if (adapterBalances[i].balances.length > 0) {
nonZeroAdapterBalances[counter] = adapterBalances[i];
counter++;
}
}

return adapterBalances;
return nonZeroAdapterBalances;
}

/**
Expand All @@ -159,25 +187,34 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
view
returns (AdapterBalance memory)
{
FullTokenBalance[] memory finalFullTokenBalances = new FullTokenBalance[](tokens.length);
uint256 amount;
string memory tokenType;
string memory tokenType = ProtocolAdapter(adapter).tokenType();
uint256[] memory amounts = new uint256[](tokens.length);
uint256 counter;

for (uint256 i = 0; i < tokens.length; i++) {
for (uint256 i = 0; i < amounts.length; i++) {
try ProtocolAdapter(adapter).getBalance(tokens[i], account) returns (uint256 result) {
amount = result;
amounts[i] = result;
} catch {
amount = 0;
amounts[i] = 0;
}
if (amounts[i] > 0) {
counter++;
}
}

tokenType = ProtocolAdapter(adapter).tokenType();

finalFullTokenBalances[i] = getFullTokenBalance(
tokenType,
tokens[i],
amount,
getFinalComponents(tokenType, tokens[i], 1e18)
);
FullTokenBalance[] memory finalFullTokenBalances = new FullTokenBalance[](counter);
counter = 0;

for (uint256 i = 0; i < amounts.length; i++) {
if (amounts[i] > 0) {
finalFullTokenBalances[counter] = getFullTokenBalance(
tokenType,
tokens[i],
amounts[i],
getFinalComponents(tokenType, tokens[i], amounts[i])
);
counter++;
}
}

return AdapterBalance({
Expand Down Expand Up @@ -212,7 +249,7 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
componentTokenBalances[i] = getTokenBalance(
components[i].tokenType,
components[i].token,
components[i].rate * amount / 1e18
components[i].rate
);
}

Expand All @@ -237,10 +274,7 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
view
returns (Component[] memory)
{
uint256 totalLength;

totalLength = getFinalComponentsNumber(tokenType, token, true);

uint256 totalLength = getFinalComponentsNumber(tokenType, token, true);
Component[] memory finalTokens = new Component[](totalLength);
uint256 length;
uint256 init = 0;
Expand Down Expand Up @@ -287,13 +321,13 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
view
returns (uint256)
{
if (tokenType.isEqualTo("ERC20")) {
return initial ? uint256(0) : uint256(1);
}

uint256 totalLength = 0;
Component[] memory components = getComponents(tokenType, token, 1e18);

if (components.length == 0) {
return initial ? uint256(0) : uint256(1);
}

for (uint256 i = 0; i < components.length; i++) {
totalLength = totalLength + getFinalComponentsNumber(
components[i].tokenType,
Expand Down Expand Up @@ -323,9 +357,13 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
TokenAdapter adapter = TokenAdapter(tokenAdapter[tokenType]);
Component[] memory components;

try adapter.getComponents(token) returns (Component[] memory result) {
components = result;
} catch {
if (address(adapter) != address(0)) {
try adapter.getComponents(token) returns (Component[] memory result) {
components = result;
} catch {
components = new Component[](0);
}
} else {
components = new Component[](0);
}

Expand Down Expand Up @@ -353,22 +391,29 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
returns (TokenBalance memory)
{
TokenAdapter adapter = TokenAdapter(tokenAdapter[tokenType]);
TokenBalance memory tokenBalance;
tokenBalance.amount = amount;

try adapter.getMetadata(token) returns (TokenMetadata memory result) {
return TokenBalance({
metadata: result,
amount: amount
});
} catch {
return TokenBalance({
metadata: TokenMetadata({
if (address(adapter) != address(0)) {
try adapter.getMetadata(token) returns (TokenMetadata memory result) {
tokenBalance.metadata = result;
} catch {
tokenBalance.metadata = TokenMetadata({
token: token,
name: "Not available",
symbol: "N/A",
decimals: 18
}),
amount: amount
decimals: 0
});
}
} else {
tokenBalance.metadata = TokenMetadata({
token: token,
name: "Not available",
symbol: "N/A",
decimals: 0
});
}

return tokenBalance;
}
}
10 changes: 5 additions & 5 deletions contracts/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
// 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.4;
pragma solidity 0.6.5;
pragma experimental ABIEncoderV2;


interface ERC20 {
function approve(address, uint256) external returns (bool);
function transfer(address, uint256) external returns (bool);
function transferFrom(address, address, uint256) external returns (bool);
function balanceOf(address) external view returns (uint256);
function totalSupply() external view returns (uint256);
function decimals() external view returns (uint8);
function symbol() external view returns (string memory);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint256);
function balanceOf(address) external view returns (uint256);
}
2 changes: 1 addition & 1 deletion contracts/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// 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.4;
pragma solidity 0.6.5;
pragma experimental ABIEncoderV2;


Expand Down
Loading

0 comments on commit dd57f00

Please sign in to comment.