-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDBR.sol
393 lines (363 loc) · 15.4 KB
/
DBR.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
/**
@title Dola Borrow Rights
@notice The DolaBorrowRights contract is a non-standard ERC20 token, that gives the right of holders to borrow DOLA at 0% interest.
As a borrower takes on DOLA debt, their DBR balance will be exhausted at 1 DBR per 1 DOLA borrowed per year.
*/
contract DolaBorrowingRights {
string public name;
string public symbol;
uint8 public constant decimals = 18;
uint256 public _totalSupply;
address public operator;
address public pendingOperator;
uint public totalDueTokensAccrued;
uint public replenishmentPriceBps;
mapping(address => uint256) public balances;
mapping(address => mapping(address => uint256)) public allowance;
uint256 internal immutable INITIAL_CHAIN_ID;
bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;
mapping(address => uint256) public nonces;
mapping (address => bool) public minters;
mapping (address => bool) public markets;
mapping (address => uint) public debts; // user => debt across all tracked markets
mapping (address => uint) public dueTokensAccrued; // user => amount of due tokens accrued
mapping (address => uint) public lastUpdated; // user => last update timestamp
constructor(
uint _replenishmentPriceBps,
string memory _name,
string memory _symbol,
address _operator
) {
replenishmentPriceBps = _replenishmentPriceBps;
name = _name;
symbol = _symbol;
operator = _operator;
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}
modifier onlyOperator {
require(msg.sender == operator, "ONLY OPERATOR");
_;
}
/**
@notice Sets pending operator of the contract. Operator role must be claimed by the new oprator. Only callable by Operator.
@param newOperator_ The address of the newOperator
*/
function setPendingOperator(address newOperator_) public onlyOperator {
pendingOperator = newOperator_;
}
/**
@notice Sets the replenishment price in basis points. Replenishment price denotes the increase in DOLA debt upon forced replenishments.
At 10000, the cost of replenishing 1 DBR is 1 DOLA in debt. Only callable by Operator.
@param newReplenishmentPriceBps_ The new replen
*/
function setReplenishmentPriceBps(uint newReplenishmentPriceBps_) public onlyOperator {
require(newReplenishmentPriceBps_ > 0, "replenishment price must be over 0");
require(newReplenishmentPriceBps_ <= 1_000_000, "Replenishment price cannot exceed 100 DOLA per DBR");
replenishmentPriceBps = newReplenishmentPriceBps_;
}
/**
@notice claims the Operator role if set as pending operator.
*/
function claimOperator() public {
require(msg.sender == pendingOperator, "ONLY PENDING OPERATOR");
operator = pendingOperator;
pendingOperator = address(0);
emit ChangeOperator(operator);
}
/**
@notice Add a minter to the set of addresses allowed to mint DBR tokens. Only callable by Operator.
@param minter_ The address of the new minter.
*/
function addMinter(address minter_) public onlyOperator {
minters[minter_] = true;
emit AddMinter(minter_);
}
/**
@notice Removes a minter from the set of addresses allowe to mint DBR tokens. Only callable by Operator.
@param minter_ The address to be removed from the minter set.
*/
function removeMinter(address minter_) public onlyOperator {
minters[minter_] = false;
emit RemoveMinter(minter_);
}
/**
@notice Adds a market to the set of active markets. Only callable by Operator.
@dev markets can be added but cannot be removed. A removed market would result in unrepayable debt for some users.
@param market_ The address of the new market contract to be added.
*/
function addMarket(address market_) public onlyOperator {
markets[market_] = true;
emit AddMarket(market_);
}
/**
@notice Get the total supply of DBR tokens.
@dev The total supply is calculated as the difference between total DBR minted and total DBR accrued.
@return uint representing the total supply of DBR.
*/
function totalSupply() public view returns (uint) {
if(totalDueTokensAccrued > _totalSupply) return 0;
return _totalSupply - totalDueTokensAccrued;
}
/**
@notice Get the DBR balance of an address. Will return 0 if the user has zero DBR or a deficit.
@dev The balance of a user is calculated as the difference between the user's balance and the user's accrued DBR debt + due DBR debt.
@param user Address of the user.
@return uint representing the balance of the user.
*/
function balanceOf(address user) public view returns (uint) {
uint debt = debts[user];
uint accrued = (block.timestamp - lastUpdated[user]) * debt / 365 days;
if(dueTokensAccrued[user] + accrued > balances[user]) return 0;
return balances[user] - dueTokensAccrued[user] - accrued;
}
/**
@notice Get the DBR deficit of an address. Will return 0 if th user has zero DBR or more.
@dev The deficit of a user is calculated as the difference between the user's accrued DBR deb + due DBR debt and their balance.
@param user Address of the user.
@return uint representing the deficit of the user.
*/
function deficitOf(address user) public view returns (uint) {
uint debt = debts[user];
uint accrued = (block.timestamp - lastUpdated[user]) * debt / 365 days;
if(dueTokensAccrued[user] + accrued < balances[user]) return 0;
return dueTokensAccrued[user] + accrued - balances[user];
}
/**
@notice Get the signed DBR balance of an address.
@dev This function will revert if a user has a balance of more than 2^255-1 DBR
@param user Address of the user.
@return Returns a signed int of the user's balance
*/
function signedBalanceOf(address user) public view returns (int) {
uint debt = debts[user];
uint accrued = (block.timestamp - lastUpdated[user]) * debt / 365 days;
return int(balances[user]) - int(dueTokensAccrued[user]) - int(accrued);
}
/**
@notice Approves spender to spend amount of DBR on behalf of the message sender.
@param spender Address of the spender to be approved
@param amount Amount to be approved to spend
@return Always returns true, will revert if not successful.
*/
function approve(address spender, uint256 amount) public virtual returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
/**
@notice Transfers amount to address to from message sender.
@param to The address to transfer to
@param amount The amount of DBR to transfer
@return Always returns true, will revert if not successful.
*/
function transfer(address to, uint256 amount) public virtual returns (bool) {
require(balanceOf(msg.sender) >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
unchecked {
balances[to] += amount;
}
emit Transfer(msg.sender, to, amount);
return true;
}
/**
@notice Transfer amount of DBR on behalf of address from to address to. Message sender must have a sufficient allowance from the from address.
@dev Allowance is reduced by the amount transferred.
@param from Address to transfer from.
@param to Address to transfer to.
@param amount Amount of DBR to transfer.
@return Always returns true, will revert if not successful.
*/
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
uint256 allowed = allowance[from][msg.sender];
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;
require(balanceOf(from) >= amount, "Insufficient balance");
balances[from] -= amount;
unchecked {
balances[to] += amount;
}
emit Transfer(from, to, amount);
return true;
}
/**
@notice Permits an address to spend on behalf of another address via a signed message.
@dev Can be bundled with a transferFrom call, to reduce transaction load on users.
@param owner Address of the owner permitting the spending
@param spender Address allowed to spend on behalf of owner.
@param value Amount to be allowed to spend.
@param deadline Timestamp after which the signed message is no longer valid.
@param v The v param of the ECDSA signature
@param r The r param of the ECDSA signature
@param s The s param of the ECDSA signature
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");
unchecked {
address recoveredAddress = ecrecover(
keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
),
owner,
spender,
value,
nonces[owner]++,
deadline
)
)
)
),
v,
r,
s
);
require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");
allowance[recoveredAddress][spender] = value;
}
emit Approval(owner, spender, value);
}
/**
@notice Function for invalidating the nonce of a signed message.
*/
function invalidateNonce() public {
nonces[msg.sender]++;
}
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}
function computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256("1"),
block.chainid,
address(this)
)
);
}
/**
@notice Accrue due DBR debt of user
@dev DBR debt is accrued at a rate of 1 DBR per 1 DOLA of debt per year.
@param user The address of the user to accrue DBR debt to.
*/
function accrueDueTokens(address user) public {
uint debt = debts[user];
if(lastUpdated[user] == block.timestamp) return;
uint accrued = (block.timestamp - lastUpdated[user]) * debt / 365 days;
if(accrued > 0 || lastUpdated[user] == 0){
dueTokensAccrued[user] += accrued;
totalDueTokensAccrued += accrued;
lastUpdated[user] = block.timestamp;
emit Transfer(user, address(0), accrued);
}
}
/**
@notice Function to be called by markets when a borrow occurs.
@dev Accrues due tokens on behalf of the user, before increasing their debt.
@param user The address of the borrower
@param additionalDebt The additional amount of DOLA the user is borrowing
*/
function onBorrow(address user, uint additionalDebt) public {
require(markets[msg.sender], "Only markets can call onBorrow");
accrueDueTokens(user);
require(deficitOf(user) == 0, "DBR Deficit");
debts[user] += additionalDebt;
}
/**
@notice Function to be called by markets when a repayment occurs.
@dev Accrues due tokens on behalf of the user, before reducing their debt.
@param user The address of the borrower having their debt repaid
@param repaidDebt The amount of DOLA repaid
*/
function onRepay(address user, uint repaidDebt) public {
require(markets[msg.sender], "Only markets can call onRepay");
accrueDueTokens(user);
debts[user] -= repaidDebt;
}
/**
@notice Function to be called by markets when a force replenish occurs. This function can only be called if the user has a DBR deficit.
@dev Accrues due tokens on behalf of the user, before increasing their debt by the replenishment price and minting them new DBR.
@param user The user to be force replenished.
@param amount The amount of DBR the user will be force replenished.
*/
function onForceReplenish(address user, address replenisher, uint amount, uint replenisherReward) public {
require(markets[msg.sender], "Only markets can call onForceReplenish");
uint deficit = deficitOf(user);
require(deficit > 0, "No deficit");
require(deficit >= amount, "Amount > deficit");
uint replenishmentCost = amount * replenishmentPriceBps / 10000;
accrueDueTokens(user);
debts[user] += replenishmentCost;
_mint(user, amount);
emit ForceReplenish(user, replenisher, msg.sender, amount, replenishmentCost, replenisherReward);
}
/**
@notice Function for burning DBR from message sender, reducing supply.
@param amount Amount to be burned
*/
function burn(uint amount) public {
_burn(msg.sender, amount);
}
/**
@notice Function for minting new DBR, increasing supply. Only callable by minters and the operator.
@param to Address to mint DBR to.
@param amount Amount of DBR to mint.
*/
function mint(address to, uint amount) public {
require(minters[msg.sender] == true || msg.sender == operator, "ONLY MINTERS OR OPERATOR");
_mint(to, amount);
}
/**
@notice Internal function for minting DBR.
@param to Address to mint DBR to.
@param amount Amount of DBR to mint.
*/
function _mint(address to, uint256 amount) internal virtual {
_totalSupply += amount;
unchecked {
balances[to] += amount;
}
emit Transfer(address(0), to, amount);
}
/**
@notice Internal function for burning DBR.
@param from Address to burn DBR from.
@param amount Amount of DBR to be burned.
*/
function _burn(address from, uint256 amount) internal virtual {
require(balanceOf(from) >= amount, "Insufficient balance");
balances[from] -= amount;
unchecked {
_totalSupply -= amount;
}
emit Transfer(from, address(0), amount);
}
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
event AddMinter(address indexed minter);
event RemoveMinter(address indexed minter);
event AddMarket(address indexed market);
event ChangeOperator(address indexed newOperator);
event ForceReplenish(address indexed account, address indexed replenisher, address indexed market, uint deficit, uint replenishmentCost, uint replenisherReward);
}