-
Notifications
You must be signed in to change notification settings - Fork 21
/
jug.sol
142 lines (129 loc) · 5.1 KB
/
jug.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: AGPL-3.0-or-later
/// jug.sol -- Dai Lending Rate
// Copyright (C) 2018 Rain <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity >=0.5.12;
import "./lib.sol";
interface VatLike {
function ilks(bytes32) external returns (
uint256 Art, // [wad]
uint256 rate // [ray]
);
function fold(bytes32,address,int) external;
}
//Jug智能合约的主要功能是在调用drip()方法时为抵押品计算累计稳定费用。
//有效地更新了所有金库的累计债务,以及由Vat(全球)跟踪的累计债务总额和Dai盈余金额(以vow所拥有的Dai金额表示)。
contract Jug is LibNote {
// --- Auth ---
mapping (address => uint) public wards;
function rely(address usr) external note auth { wards[usr] = 1; }
function deny(address usr) external note auth { wards[usr] = 0; }
modifier auth {
require(wards[msg.sender] == 1, "Jug/not-authorized");
_;
}
// --- Data ---
struct Ilk {
//抵押物的风险溢价
uint256 duty; // Collateral-specific, per-second stability fee contribution [ray]
//drip最后更新时的时间戳
uint256 rho; // Time of last drip [unix epoch time]
}
//每个抵押物创建一个结构
mapping (bytes32 => Ilk) public ilks;
VatLike public vat; // CDP Engine
address public vow; // Debt Engine
//使用于所有抵押物的费用
uint256 public base; // Global, per-second stability fee contribution [ray]
// --- Init ---
constructor(address vat_) public {
wards[msg.sender] = 1;
vat = VatLike(vat_);
}
// --- Math ---
function rpow(uint x, uint n, uint b) internal pure returns (uint z) {
assembly {
switch x case 0 {switch n case 0 {z := b} default {z := 0}}
default {
switch mod(n, 2) case 0 { z := b } default { z := x }
let half := div(b, 2) // for rounding.
for { n := div(n, 2) } n { n := div(n,2) } {
let xx := mul(x, x)
if iszero(eq(div(xx, x), x)) { revert(0,0) }
let xxRound := add(xx, half)
if lt(xxRound, xx) { revert(0,0) }
x := div(xxRound, b)
if mod(n,2) {
let zx := mul(z, x)
if and(iszero(iszero(x)), iszero(eq(div(zx, x), z))) { revert(0,0) }
let zxRound := add(zx, half)
if lt(zxRound, zx) { revert(0,0) }
z := div(zxRound, b)
}
}
}
}
}
uint256 constant ONE = 10 ** 27;
function add(uint x, uint y) internal pure returns (uint z) {
z = x + y;
require(z >= x);
}
function diff(uint x, uint y) internal pure returns (int z) {
z = int(x) - int(y);
require(int(x) >= 0 && int(y) >= 0);
}
function rmul(uint x, uint y) internal pure returns (uint z) {
z = x * y;
require(y == 0 || z / y == x);
z = z / ONE;
}
// --- Administration ---
//开始对特定抵押物收取稳定费
//添加新的抵押品的调用
function init(bytes32 ilk) external note auth {
Ilk storage i = ilks[ilk];
require(i.duty == 0, "Jug/ilk-already-init");
i.duty = ONE;
i.rho = now;
}
//设置抵押物的风险溢价
function file(bytes32 ilk, bytes32 what, uint data) external note auth {
require(now == ilks[ilk].rho, "Jug/rho-not-updated");
if (what == "duty") ilks[ilk].duty = data;
else revert("Jug/file-unrecognized-param");
}
function file(bytes32 what, uint data) external note auth {
if (what == "base") base = data;
else revert("Jug/file-unrecognized-param");
}
function file(bytes32 what, address data) external note auth {
if (what == "vow") vow = data;
else revert("Jug/file-unrecognized-param");
}
// --- Stability Fee Collection ---
//给抵押物收取稳定费
//任何人可以调用
//计算抵押品的稳定率变化根据上次更新时间的base和duty
//更新抵押品的抵押率、总的债务和盈余
//更新时间戳
function drip(bytes32 ilk) external note returns (uint rate) {
require(now >= ilks[ilk].rho, "Jug/invalid-now");
(, uint prev) = vat.ilks(ilk);
rate = rmul(rpow(add(base, ilks[ilk].duty), now - ilks[ilk].rho, ONE), prev);
vat.fold(ilk, vow, diff(rate, prev));
ilks[ilk].rho = now;
}
}