forked from abondarevrn/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompoundLens.sol
499 lines (454 loc) · 15.6 KB
/
CompoundLens.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
pragma solidity >=0.5.16;
pragma experimental ABIEncoderV2;
import "./CErc20.sol";
import "./CToken.sol";
import "./PriceOracle.sol";
import "./Comp.sol";
import "./GovernorAlpha.sol";
interface ComptrollerLensInterface {
function markets(address) external view returns (bool, uint256);
function oracle() external view returns (PriceOracle);
function getAccountLiquidity(address)
external
view
returns (
uint256,
uint256,
uint256
);
function getAssetsIn(address) external view returns (CToken[] memory);
function claimComp(address) external;
function compAccrued(address) external view returns (uint);
}
interface GovernorBravoInterface {
struct Receipt {
bool hasVoted;
uint8 support;
uint96 votes;
}
struct Proposal {
uint id;
address proposer;
uint eta;
uint startBlock;
uint endBlock;
uint forVotes;
uint againstVotes;
uint abstainVotes;
bool canceled;
bool executed;
}
function getActions(uint proposalId) external view returns (address[] memory targets, uint[] memory values, string[] memory signatures, bytes[] memory calldatas);
function proposals(uint proposalId) external view returns (Proposal memory);
function getReceipt(uint proposalId, address voter) external view returns (Receipt memory);
}
contract CompoundLens {
struct CTokenMetadata {
address cToken;
uint256 exchangeRateCurrent;
uint256 supplyRatePerBlock;
uint256 borrowRatePerBlock;
uint256 reserveFactorMantissa;
uint256 totalBorrows;
uint256 totalReserves;
uint256 totalSupply;
uint256 totalCash;
bool isListed;
uint256 collateralFactorMantissa;
address underlyingAssetAddress;
uint256 cTokenDecimals;
uint256 underlyingDecimals;
}
function cTokenMetadata(CToken cToken)
public
returns (CTokenMetadata memory)
{
uint256 exchangeRateCurrent = cToken.exchangeRateCurrent();
ComptrollerLensInterface comptroller =
ComptrollerLensInterface(address(cToken.comptroller()));
(bool isListed, uint256 collateralFactorMantissa) =
comptroller.markets(address(cToken));
address underlyingAssetAddress;
uint256 underlyingDecimals;
if (compareStrings(cToken.symbol(), "aMATIC")) {
underlyingAssetAddress = address(0);
underlyingDecimals = 18;
} else {
CErc20 cErc20 = CErc20(address(cToken));
underlyingAssetAddress = cErc20.underlying();
underlyingDecimals = EIP20Interface(cErc20.underlying()).decimals();
}
return
CTokenMetadata({
cToken: address(cToken),
exchangeRateCurrent: exchangeRateCurrent,
supplyRatePerBlock: cToken.supplyRatePerBlock(),
borrowRatePerBlock: cToken.borrowRatePerBlock(),
reserveFactorMantissa: cToken.reserveFactorMantissa(),
totalBorrows: cToken.totalBorrows(),
totalReserves: cToken.totalReserves(),
totalSupply: cToken.totalSupply(),
totalCash: cToken.getCash(),
isListed: isListed,
collateralFactorMantissa: collateralFactorMantissa,
underlyingAssetAddress: underlyingAssetAddress,
cTokenDecimals: cToken.decimals(),
underlyingDecimals: underlyingDecimals
});
}
function cTokenMetadataAll(CToken[] calldata cTokens)
external
returns (CTokenMetadata[] memory)
{
uint256 cTokenCount = cTokens.length;
CTokenMetadata[] memory res = new CTokenMetadata[](cTokenCount);
for (uint256 i = 0; i < cTokenCount; i++) {
res[i] = cTokenMetadata(cTokens[i]);
}
return res;
}
struct CTokenBalances {
address cToken;
uint256 balanceOf;
uint256 borrowBalanceCurrent;
uint256 balanceOfUnderlying;
uint256 tokenBalance;
uint256 tokenAllowance;
}
function cTokenBalances(CToken cToken, address payable account)
public
returns (CTokenBalances memory)
{
uint256 balanceOf = cToken.balanceOf(account);
uint256 borrowBalanceCurrent = cToken.borrowBalanceCurrent(account);
uint256 balanceOfUnderlying = cToken.balanceOfUnderlying(account);
uint256 tokenBalance;
uint256 tokenAllowance;
if (compareStrings(cToken.symbol(), "aMATIC")) {
tokenBalance = account.balance;
tokenAllowance = account.balance;
} else {
CErc20 cErc20 = CErc20(address(cToken));
EIP20Interface underlying = EIP20Interface(cErc20.underlying());
tokenBalance = underlying.balanceOf(account);
tokenAllowance = underlying.allowance(account, address(cToken));
}
return
CTokenBalances({
cToken: address(cToken),
balanceOf: balanceOf,
borrowBalanceCurrent: borrowBalanceCurrent,
balanceOfUnderlying: balanceOfUnderlying,
tokenBalance: tokenBalance,
tokenAllowance: tokenAllowance
});
}
function cTokenBalancesAll(
CToken[] calldata cTokens,
address payable account
) external returns (CTokenBalances[] memory) {
uint256 cTokenCount = cTokens.length;
CTokenBalances[] memory res = new CTokenBalances[](cTokenCount);
for (uint256 i = 0; i < cTokenCount; i++) {
res[i] = cTokenBalances(cTokens[i], account);
}
return res;
}
struct CTokenUnderlyingPrice {
address cToken;
uint256 underlyingPrice;
}
function cTokenUnderlyingPrice(CToken cToken)
public
returns (CTokenUnderlyingPrice memory)
{
ComptrollerLensInterface comptroller =
ComptrollerLensInterface(address(cToken.comptroller()));
PriceOracle priceOracle = comptroller.oracle();
return
CTokenUnderlyingPrice({
cToken: address(cToken),
underlyingPrice: priceOracle.getUnderlyingPrice(cToken)
});
}
function cTokenUnderlyingPriceAll(CToken[] calldata cTokens)
external
returns (CTokenUnderlyingPrice[] memory)
{
uint256 cTokenCount = cTokens.length;
CTokenUnderlyingPrice[] memory res =
new CTokenUnderlyingPrice[](cTokenCount);
for (uint256 i = 0; i < cTokenCount; i++) {
res[i] = cTokenUnderlyingPrice(cTokens[i]);
}
return res;
}
struct AccountLimits {
CToken[] markets;
uint256 liquidity;
uint256 shortfall;
}
function getAccountLimits(
ComptrollerLensInterface comptroller,
address account
) public returns (AccountLimits memory) {
(uint256 errorCode, uint256 liquidity, uint256 shortfall) =
comptroller.getAccountLiquidity(account);
require(errorCode == 0);
return
AccountLimits({
markets: comptroller.getAssetsIn(account),
liquidity: liquidity,
shortfall: shortfall
});
}
struct GovReceipt {
uint proposalId;
bool hasVoted;
bool support;
uint96 votes;
}
function getGovReceipts(GovernorAlpha governor, address voter, uint[] memory proposalIds) public view returns (GovReceipt[] memory) {
uint proposalCount = proposalIds.length;
GovReceipt[] memory res = new GovReceipt[](proposalCount);
for (uint i = 0; i < proposalCount; i++) {
GovernorAlpha.Receipt memory receipt = governor.getReceipt(proposalIds[i], voter);
res[i] = GovReceipt({
proposalId: proposalIds[i],
hasVoted: receipt.hasVoted,
support: receipt.support,
votes: receipt.votes
});
}
return res;
}
struct GovBravoReceipt {
uint proposalId;
bool hasVoted;
uint8 support;
uint96 votes;
}
function getGovBravoReceipts(GovernorBravoInterface governor, address voter, uint[] memory proposalIds) public view returns (GovBravoReceipt[] memory) {
uint proposalCount = proposalIds.length;
GovBravoReceipt[] memory res = new GovBravoReceipt[](proposalCount);
for (uint i = 0; i < proposalCount; i++) {
GovernorBravoInterface.Receipt memory receipt = governor.getReceipt(proposalIds[i], voter);
res[i] = GovBravoReceipt({
proposalId: proposalIds[i],
hasVoted: receipt.hasVoted,
support: receipt.support,
votes: receipt.votes
});
}
return res;
}
struct GovProposal {
uint proposalId;
address proposer;
uint eta;
address[] targets;
uint[] values;
string[] signatures;
bytes[] calldatas;
uint startBlock;
uint endBlock;
uint forVotes;
uint againstVotes;
bool canceled;
bool executed;
}
function setProposal(GovProposal memory res, GovernorAlpha governor, uint proposalId) internal view {
(
,
address proposer,
uint eta,
uint startBlock,
uint endBlock,
uint forVotes,
uint againstVotes,
bool canceled,
bool executed
) = governor.proposals(proposalId);
res.proposalId = proposalId;
res.proposer = proposer;
res.eta = eta;
res.startBlock = startBlock;
res.endBlock = endBlock;
res.forVotes = forVotes;
res.againstVotes = againstVotes;
res.canceled = canceled;
res.executed = executed;
}
function getGovProposals(GovernorAlpha governor, uint[] calldata proposalIds) external view returns (GovProposal[] memory) {
GovProposal[] memory res = new GovProposal[](proposalIds.length);
for (uint i = 0; i < proposalIds.length; i++) {
(
address[] memory targets,
uint[] memory values,
string[] memory signatures,
bytes[] memory calldatas
) = governor.getActions(proposalIds[i]);
res[i] = GovProposal({
proposalId: 0,
proposer: address(0),
eta: 0,
targets: targets,
values: values,
signatures: signatures,
calldatas: calldatas,
startBlock: 0,
endBlock: 0,
forVotes: 0,
againstVotes: 0,
canceled: false,
executed: false
});
setProposal(res[i], governor, proposalIds[i]);
}
return res;
}
struct GovBravoProposal {
uint proposalId;
address proposer;
uint eta;
address[] targets;
uint[] values;
string[] signatures;
bytes[] calldatas;
uint startBlock;
uint endBlock;
uint forVotes;
uint againstVotes;
uint abstainVotes;
bool canceled;
bool executed;
}
function setBravoProposal(GovBravoProposal memory res, GovernorBravoInterface governor, uint proposalId) internal view {
GovernorBravoInterface.Proposal memory p = governor.proposals(proposalId);
res.proposalId = proposalId;
res.proposer = p.proposer;
res.eta = p.eta;
res.startBlock = p.startBlock;
res.endBlock = p.endBlock;
res.forVotes = p.forVotes;
res.againstVotes = p.againstVotes;
res.abstainVotes = p.abstainVotes;
res.canceled = p.canceled;
res.executed = p.executed;
}
function getGovBravoProposals(GovernorBravoInterface governor, uint[] calldata proposalIds) external view returns (GovBravoProposal[] memory) {
GovBravoProposal[] memory res = new GovBravoProposal[](proposalIds.length);
for (uint i = 0; i < proposalIds.length; i++) {
(
address[] memory targets,
uint[] memory values,
string[] memory signatures,
bytes[] memory calldatas
) = governor.getActions(proposalIds[i]);
res[i] = GovBravoProposal({
proposalId: 0,
proposer: address(0),
eta: 0,
targets: targets,
values: values,
signatures: signatures,
calldatas: calldatas,
startBlock: 0,
endBlock: 0,
forVotes: 0,
againstVotes: 0,
abstainVotes: 0,
canceled: false,
executed: false
});
setBravoProposal(res[i], governor, proposalIds[i]);
}
return res;
}
struct CompBalanceMetadata {
uint256 balance;
uint256 votes;
address delegate;
}
function getCompBalanceMetadata(Comp lhb, address account)
external
view
returns (CompBalanceMetadata memory)
{
return
CompBalanceMetadata({
balance: lhb.balanceOf(account),
votes: uint256(lhb.getCurrentVotes(account)),
delegate: lhb.delegates(account)
});
}
struct CompBalanceMetadataExt {
uint256 balance;
uint256 votes;
address delegate;
uint256 allocated;
}
function getCompBalanceMetadataExt(
Comp lhb,
ComptrollerLensInterface comptroller,
address account
) external returns (CompBalanceMetadataExt memory) {
uint256 balance = lhb.balanceOf(account);
comptroller.claimComp(account);
uint256 newBalance = lhb.balanceOf(account);
uint256 accrued = comptroller.compAccrued(account);
uint256 total = add(accrued, newBalance, "sum LHB total");
uint256 allocated = sub(total, balance, "sub allocated");
return
CompBalanceMetadataExt({
balance: balance,
votes: uint256(lhb.getCurrentVotes(account)),
delegate: lhb.delegates(account),
allocated: allocated
});
}
struct CompVotes {
uint256 blockNumber;
uint256 votes;
}
function getCompVotes(
Comp lhb,
address account,
uint32[] calldata blockNumbers
) external view returns (CompVotes[] memory) {
CompVotes[] memory res = new CompVotes[](blockNumbers.length);
for (uint256 i = 0; i < blockNumbers.length; i++) {
res[i] = CompVotes({
blockNumber: uint256(blockNumbers[i]),
votes: uint256(lhb.getPriorVotes(account, blockNumbers[i]))
});
}
return res;
}
function compareStrings(string memory a, string memory b)
internal
pure
returns (bool)
{
return (keccak256(abi.encodePacked((a))) ==
keccak256(abi.encodePacked((b))));
}
function add(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, errorMessage);
return c;
}
function sub(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
}