Skip to content

Commit dd57f00

Browse files
authored
Merge pull request #32 from zeriontech/contracts-fixes
Fresh updates for new adapters [fixed]
2 parents d598340 + 2ea43ab commit dd57f00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1048
-844
lines changed

.solhint.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
],
1414
"compiler-version": [
1515
"error",
16-
"0.6.4"
16+
"0.6.5"
1717
],
1818
"private-vars-leading-underscore": [
1919
"off"
20+
],
21+
"const-name-snakecase": [
22+
"off"
2023
]
2124
}
2225
}

.soliumignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ node_modules
77
./contracts/ProtocolManager.sol
88
./contracts/Ownable.sol
99
./contracts/adapters/maker/MKRAdapter.sol
10-
./contracts/adapters/uniswap/UniswapTokenAdapter.sol
10+
./contracts/adapters/uniswap/UniswapV1TokenAdapter.sol
11+
./contracts/adapters/balancer/BalancerTokenAdapter.sol
1112
./contracts/adapters/dydx
13+
./contracts/adapters
14+
./contracts/mock

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
[![Coverage status](https://github.com/zeriontech/protocol-wrappers/workflows/coverage/badge.svg)](https://github.com/zeriontech/defi-sdk/actions?query=workflow:coverage)
88
[![Lint status](https://github.com/zeriontech/protocol-wrappers/workflows/lint/badge.svg)](https://github.com/zeriontech/defi-sdk/actions?query=workflow:lint)
99
[![License](https://img.shields.io/github/license/zeriontech/defi-sdk)](https://www.gnu.org/licenses/lgpl-3.0.en.html)
10-
[![Discord](https://img.shields.io/discord/544761450724458498?label=discord)](https://go.zerion.io/discord)
11-
[![Twitter Follow](https://img.shields.io/twitter/follow/zerion_io.svg)](https://twitter.com/intent/follow?screen_name=zerion_io)
10+
[![Discord](https://badgen.net/badge/zerion/Zerion?icon=discord&label=discord)](https://go.zerion.io/discord)
11+
[![Twitter Follow](https://badgen.net/twitter/follow/zerion_io)](https://twitter.com/intent/follow?screen_name=zerion_io)
1212

1313
**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.
1414

@@ -140,7 +140,7 @@ and obtain all balances for a given account. The response from the smart-contrac
140140
...
141141
```
142142

143-
## DeFi SDK architecture
143+
## DeFi SDK Architecture
144144

145145
- **ProtocolAdapter** is a special contract for every protocol. Its main purpose is to wrap all the protocol interactions.
146146
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.
@@ -150,11 +150,12 @@ Its main purpose is to provide ERC20-style token metadata as well as information
150150

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

153-
## Supported protocols
153+
## Supported Protocols
154154

155155
| Protocol Name | Description | Protocol Adapters | Token Adapters |
156156
| :-----------: | :---------: | :---------------: | :------------: |
157157
| [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) |
158+
| [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) |
158159
| [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) |
159160
| [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) |
160161
| [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) ||
@@ -168,22 +169,22 @@ More detailed documentation about contracts can be found in [adapters](../../wik
168169
| [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) |
169170
| [0x Staking](./contracts/adapters/zrx) | Liquidity rewards for staking ZRX. | [Asset adapter](./contracts/adapters/zrx/ZrxAdapter.sol) ||
170171

171-
## How to add your adapter
172+
## How to Add Your Adapter
172173

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

175176
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).
176177

177178

178-
## What’s next for DeFi SDK? 🚀
179+
## What’s Next for DeFi SDK? 🚀
179180

180181
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!
181182

182183
## Security Vulnerabilities 🛡
183184

184185
If you discover a security vulnerability within DeFi SDK, please send us an e-mail at inbox@zerion.io. All security vulnerabilities will be promptly addressed.
185186

186-
## Dev notes
187+
## Dev Notes
187188

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

@@ -217,4 +218,4 @@ Currently, unsupported files are ignored.
217218

218219
## License
219220

220-
All smart contracts are released under LGPL v.3.
221+
All smart contracts are released under GNU LGPLv3.

contracts/AdapterRegistry.sol

+91-46
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@
1313
// You should have received a copy of the GNU General Public License
1414
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1515

16-
pragma solidity 0.6.4;
16+
pragma solidity 0.6.5;
1717
pragma experimental ABIEncoderV2;
1818

19-
import { Ownable } from "./Ownable.sol";
20-
import { ProtocolManager } from "./ProtocolManager.sol";
21-
import { TokenAdapterManager } from "./TokenAdapterManager.sol";
22-
import { ProtocolAdapter } from "./adapters/ProtocolAdapter.sol";
23-
import { TokenAdapter } from "./adapters/TokenAdapter.sol";
24-
import { Strings } from "./Strings.sol";
2519
import {
2620
ProtocolBalance,
2721
ProtocolMetadata,
@@ -32,6 +26,12 @@ import {
3226
TokenMetadata,
3327
Component
3428
} from "./Structs.sol";
29+
import { Strings } from "./Strings.sol";
30+
import { Ownable } from "./Ownable.sol";
31+
import { ProtocolManager } from "./ProtocolManager.sol";
32+
import { TokenAdapterManager } from "./TokenAdapterManager.sol";
33+
import { ProtocolAdapter } from "./adapters/ProtocolAdapter.sol";
34+
import { TokenAdapter } from "./adapters/TokenAdapter.sol";
3535

3636

3737
/**
@@ -107,15 +107,29 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
107107
returns (ProtocolBalance[] memory)
108108
{
109109
ProtocolBalance[] memory protocolBalances = new ProtocolBalance[](protocolNames.length);
110+
uint256 counter = 0;
110111

111112
for (uint256 i = 0; i < protocolNames.length; i++) {
112113
protocolBalances[i] = ProtocolBalance({
113114
metadata: protocolMetadata[protocolNames[i]],
114115
adapterBalances: getAdapterBalances(account, protocolAdapters[protocolNames[i]])
115116
});
117+
if (protocolBalances[i].adapterBalances.length > 0) {
118+
counter++;
119+
}
116120
}
117121

118-
return protocolBalances;
122+
ProtocolBalance[] memory nonZeroProtocolBalances = new ProtocolBalance[](counter);
123+
counter = 0;
124+
125+
for (uint256 i = 0; i < protocolNames.length; i++) {
126+
if (protocolBalances[i].adapterBalances.length > 0) {
127+
nonZeroProtocolBalances[counter] = protocolBalances[i];
128+
counter++;
129+
}
130+
}
131+
132+
return nonZeroProtocolBalances;
119133
}
120134

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

136151
for (uint256 i = 0; i < adapterBalances.length; i++) {
137152
adapterBalances[i] = getAdapterBalance(
138153
account,
139154
adapters[i],
140155
supportedTokens[adapters[i]]
141156
);
157+
if (adapterBalances[i].balances.length > 0) {
158+
counter++;
159+
}
160+
}
161+
162+
AdapterBalance[] memory nonZeroAdapterBalances = new AdapterBalance[](counter);
163+
counter = 0;
164+
165+
for (uint256 i = 0; i < adapterBalances.length; i++) {
166+
if (adapterBalances[i].balances.length > 0) {
167+
nonZeroAdapterBalances[counter] = adapterBalances[i];
168+
counter++;
169+
}
142170
}
143171

144-
return adapterBalances;
172+
return nonZeroAdapterBalances;
145173
}
146174

147175
/**
@@ -159,25 +187,34 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
159187
view
160188
returns (AdapterBalance memory)
161189
{
162-
FullTokenBalance[] memory finalFullTokenBalances = new FullTokenBalance[](tokens.length);
163-
uint256 amount;
164-
string memory tokenType;
190+
string memory tokenType = ProtocolAdapter(adapter).tokenType();
191+
uint256[] memory amounts = new uint256[](tokens.length);
192+
uint256 counter;
165193

166-
for (uint256 i = 0; i < tokens.length; i++) {
194+
for (uint256 i = 0; i < amounts.length; i++) {
167195
try ProtocolAdapter(adapter).getBalance(tokens[i], account) returns (uint256 result) {
168-
amount = result;
196+
amounts[i] = result;
169197
} catch {
170-
amount = 0;
198+
amounts[i] = 0;
171199
}
200+
if (amounts[i] > 0) {
201+
counter++;
202+
}
203+
}
172204

173-
tokenType = ProtocolAdapter(adapter).tokenType();
174-
175-
finalFullTokenBalances[i] = getFullTokenBalance(
176-
tokenType,
177-
tokens[i],
178-
amount,
179-
getFinalComponents(tokenType, tokens[i], 1e18)
180-
);
205+
FullTokenBalance[] memory finalFullTokenBalances = new FullTokenBalance[](counter);
206+
counter = 0;
207+
208+
for (uint256 i = 0; i < amounts.length; i++) {
209+
if (amounts[i] > 0) {
210+
finalFullTokenBalances[counter] = getFullTokenBalance(
211+
tokenType,
212+
tokens[i],
213+
amounts[i],
214+
getFinalComponents(tokenType, tokens[i], amounts[i])
215+
);
216+
counter++;
217+
}
181218
}
182219

183220
return AdapterBalance({
@@ -212,7 +249,7 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
212249
componentTokenBalances[i] = getTokenBalance(
213250
components[i].tokenType,
214251
components[i].token,
215-
components[i].rate * amount / 1e18
252+
components[i].rate
216253
);
217254
}
218255

@@ -237,10 +274,7 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
237274
view
238275
returns (Component[] memory)
239276
{
240-
uint256 totalLength;
241-
242-
totalLength = getFinalComponentsNumber(tokenType, token, true);
243-
277+
uint256 totalLength = getFinalComponentsNumber(tokenType, token, true);
244278
Component[] memory finalTokens = new Component[](totalLength);
245279
uint256 length;
246280
uint256 init = 0;
@@ -287,13 +321,13 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
287321
view
288322
returns (uint256)
289323
{
290-
if (tokenType.isEqualTo("ERC20")) {
291-
return initial ? uint256(0) : uint256(1);
292-
}
293-
294324
uint256 totalLength = 0;
295325
Component[] memory components = getComponents(tokenType, token, 1e18);
296326

327+
if (components.length == 0) {
328+
return initial ? uint256(0) : uint256(1);
329+
}
330+
297331
for (uint256 i = 0; i < components.length; i++) {
298332
totalLength = totalLength + getFinalComponentsNumber(
299333
components[i].tokenType,
@@ -323,9 +357,13 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
323357
TokenAdapter adapter = TokenAdapter(tokenAdapter[tokenType]);
324358
Component[] memory components;
325359

326-
try adapter.getComponents(token) returns (Component[] memory result) {
327-
components = result;
328-
} catch {
360+
if (address(adapter) != address(0)) {
361+
try adapter.getComponents(token) returns (Component[] memory result) {
362+
components = result;
363+
} catch {
364+
components = new Component[](0);
365+
}
366+
} else {
329367
components = new Component[](0);
330368
}
331369

@@ -353,22 +391,29 @@ contract AdapterRegistry is Ownable, ProtocolManager, TokenAdapterManager {
353391
returns (TokenBalance memory)
354392
{
355393
TokenAdapter adapter = TokenAdapter(tokenAdapter[tokenType]);
394+
TokenBalance memory tokenBalance;
395+
tokenBalance.amount = amount;
356396

357-
try adapter.getMetadata(token) returns (TokenMetadata memory result) {
358-
return TokenBalance({
359-
metadata: result,
360-
amount: amount
361-
});
362-
} catch {
363-
return TokenBalance({
364-
metadata: TokenMetadata({
397+
if (address(adapter) != address(0)) {
398+
try adapter.getMetadata(token) returns (TokenMetadata memory result) {
399+
tokenBalance.metadata = result;
400+
} catch {
401+
tokenBalance.metadata = TokenMetadata({
365402
token: token,
366403
name: "Not available",
367404
symbol: "N/A",
368-
decimals: 18
369-
}),
370-
amount: amount
405+
decimals: 0
406+
});
407+
}
408+
} else {
409+
tokenBalance.metadata = TokenMetadata({
410+
token: token,
411+
name: "Not available",
412+
symbol: "N/A",
413+
decimals: 0
371414
});
372415
}
416+
417+
return tokenBalance;
373418
}
374419
}

contracts/ERC20.sol

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
// You should have received a copy of the GNU General Public License
1414
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1515

16-
pragma solidity 0.6.4;
16+
pragma solidity 0.6.5;
1717
pragma experimental ABIEncoderV2;
1818

1919

2020
interface ERC20 {
2121
function approve(address, uint256) external returns (bool);
2222
function transfer(address, uint256) external returns (bool);
2323
function transferFrom(address, address, uint256) external returns (bool);
24-
function balanceOf(address) external view returns (uint256);
25-
function totalSupply() external view returns (uint256);
26-
function decimals() external view returns (uint8);
27-
function symbol() external view returns (string memory);
2824
function name() external view returns (string memory);
25+
function symbol() external view returns (string memory);
26+
function decimals() external view returns (uint8);
27+
function totalSupply() external view returns (uint256);
28+
function balanceOf(address) external view returns (uint256);
2929
}

contracts/Ownable.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// You should have received a copy of the GNU General Public License
1414
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1515

16-
pragma solidity 0.6.4;
16+
pragma solidity 0.6.5;
1717
pragma experimental ABIEncoderV2;
1818

1919

0 commit comments

Comments
 (0)