forked from zeriontech/defi-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCDDebtAdapter.sol
executable file
·98 lines (82 loc) · 3.36 KB
/
MCDDebtAdapter.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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 { ProtocolAdapter } from "../ProtocolAdapter.sol";
import { MKRAdapter } from "./MKRAdapter.sol";
/**
* @dev Vat contract interface.
* Only the functions required for MCDDebtAdapter contract are added.
* The Vat contract is available here
* github.com/makerdao/dss/blob/master/src/vat.sol.
*/
interface Vat {
function urns(bytes32, address) external view returns (uint256, uint256);
function ilks(bytes32) external view returns (uint256, uint256);
}
/**
* @dev Jug contract interface.
* Only the functions required for MCDDebtAdapter contract are added.
* The Jug contract is available here
* github.com/makerdao/dss/blob/master/src/jug.sol.
*/
interface Jug {
function ilks(bytes32) external view returns (uint256, uint256);
function base() external view returns (uint256);
}
/**
* @dev DssCdpManager contract interface.
* Only the functions required for MCDDebtAdapter contract are added.
* The DssCdpManager contract is available here
* github.com/makerdao/dss-cdp-manager/blob/master/src/DssCdpManager.sol.
*/
interface DssCdpManager {
function first(address) external view returns (uint256);
function list(uint256) external view returns (uint256, uint256);
function urns(uint256) external view returns (address);
function ilks(uint256) external view returns (bytes32);
}
/**
* @title Debt adapter for MCD protocol.
* @dev Implementation of ProtocolAdapter interface.
* @author Igor Sobolev <[email protected]>
*/
contract MCDDebtAdapter is ProtocolAdapter, MKRAdapter {
string public constant override adapterType = "Debt";
string public constant override tokenType = "ERC20";
/**
* @return Amount of debt of the given account for the protocol.
* @dev Implementation of ProtocolAdapter interface function.
*/
function getBalance(address, address account) external view override returns (uint256) {
DssCdpManager manager = DssCdpManager(MANAGER);
Vat vat = Vat(VAT);
Jug jug = Jug(JUG);
uint256 id = manager.first(account);
uint256 totalValue = 0;
while (id > 0) {
bytes32 ilk = manager.ilks(id);
(, id) = manager.list(id);
(, uint256 art) = vat.urns(ilk, manager.urns(id));
(, uint256 storedRate) = vat.ilks(ilk);
(uint256 duty, uint256 rho) = jug.ilks(ilk);
uint256 base = jug.base();
// solhint-disable-next-line not-rely-on-time
uint256 currentRate = mkrRmul(mkrRpow(mkrAdd(base, duty), now - rho, ONE), storedRate);
totalValue = totalValue + mkrRmul(art, currentRate);
}
return totalValue;
}
}