-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CompoundLens.sol
500 lines (454 loc) · 17.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
500
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity ^0.8.10;
import "../CErc20.sol";
import "../CToken.sol";
import "../PriceOracle.sol";
import "../EIP20Interface.sol";
import "../Governance/GovernorAlpha.sol";
import "../Governance/Comp.sol";
interface ComptrollerLensInterface {
function markets(address) external view returns (bool, uint);
function oracle() external view returns (PriceOracle);
function getAccountLiquidity(address) external view returns (uint, uint, uint);
function getAssetsIn(address) external view returns (CToken[] memory);
function claimComp(address) external;
function compAccrued(address) external view returns (uint);
function compSpeeds(address) external view returns (uint);
function compSupplySpeeds(address) external view returns (uint);
function compBorrowSpeeds(address) external view returns (uint);
function borrowCaps(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;
uint exchangeRateCurrent;
uint supplyRatePerBlock;
uint borrowRatePerBlock;
uint reserveFactorMantissa;
uint totalBorrows;
uint totalReserves;
uint totalSupply;
uint totalCash;
bool isListed;
uint collateralFactorMantissa;
address underlyingAssetAddress;
uint cTokenDecimals;
uint underlyingDecimals;
uint compSupplySpeed;
uint compBorrowSpeed;
uint borrowCap;
}
function getCompSpeeds(ComptrollerLensInterface comptroller, CToken cToken) internal returns (uint, uint) {
// Getting comp speeds is gnarly due to not every network having the
// split comp speeds from Proposal 62 and other networks don't even
// have comp speeds.
uint compSupplySpeed = 0;
(bool compSupplySpeedSuccess, bytes memory compSupplySpeedReturnData) =
address(comptroller).call(
abi.encodePacked(
comptroller.compSupplySpeeds.selector,
abi.encode(address(cToken))
)
);
if (compSupplySpeedSuccess) {
compSupplySpeed = abi.decode(compSupplySpeedReturnData, (uint));
}
uint compBorrowSpeed = 0;
(bool compBorrowSpeedSuccess, bytes memory compBorrowSpeedReturnData) =
address(comptroller).call(
abi.encodePacked(
comptroller.compBorrowSpeeds.selector,
abi.encode(address(cToken))
)
);
if (compBorrowSpeedSuccess) {
compBorrowSpeed = abi.decode(compBorrowSpeedReturnData, (uint));
}
// If the split comp speeds call doesn't work, try the oldest non-spit version.
if (!compSupplySpeedSuccess || !compBorrowSpeedSuccess) {
(bool compSpeedSuccess, bytes memory compSpeedReturnData) =
address(comptroller).call(
abi.encodePacked(
comptroller.compSpeeds.selector,
abi.encode(address(cToken))
)
);
if (compSpeedSuccess) {
compSupplySpeed = compBorrowSpeed = abi.decode(compSpeedReturnData, (uint));
}
}
return (compSupplySpeed, compBorrowSpeed);
}
function cTokenMetadata(CToken cToken) public returns (CTokenMetadata memory) {
uint exchangeRateCurrent = cToken.exchangeRateCurrent();
ComptrollerLensInterface comptroller = ComptrollerLensInterface(address(cToken.comptroller()));
(bool isListed, uint collateralFactorMantissa) = comptroller.markets(address(cToken));
address underlyingAssetAddress;
uint underlyingDecimals;
if (compareStrings(cToken.symbol(), "cETH")) {
underlyingAssetAddress = address(0);
underlyingDecimals = 18;
} else {
CErc20 cErc20 = CErc20(address(cToken));
underlyingAssetAddress = cErc20.underlying();
underlyingDecimals = EIP20Interface(cErc20.underlying()).decimals();
}
(uint compSupplySpeed, uint compBorrowSpeed) = getCompSpeeds(comptroller, cToken);
uint borrowCap = 0;
(bool borrowCapSuccess, bytes memory borrowCapReturnData) =
address(comptroller).call(
abi.encodePacked(
comptroller.borrowCaps.selector,
abi.encode(address(cToken))
)
);
if (borrowCapSuccess) {
borrowCap = abi.decode(borrowCapReturnData, (uint));
}
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,
compSupplySpeed: compSupplySpeed,
compBorrowSpeed: compBorrowSpeed,
borrowCap: borrowCap
});
}
function cTokenMetadataAll(CToken[] calldata cTokens) external returns (CTokenMetadata[] memory) {
uint cTokenCount = cTokens.length;
CTokenMetadata[] memory res = new CTokenMetadata[](cTokenCount);
for (uint i = 0; i < cTokenCount; i++) {
res[i] = cTokenMetadata(cTokens[i]);
}
return res;
}
struct CTokenBalances {
address cToken;
uint balanceOf;
uint borrowBalanceCurrent;
uint balanceOfUnderlying;
uint tokenBalance;
uint tokenAllowance;
}
function cTokenBalances(CToken cToken, address payable account) public returns (CTokenBalances memory) {
uint balanceOf = cToken.balanceOf(account);
uint borrowBalanceCurrent = cToken.borrowBalanceCurrent(account);
uint balanceOfUnderlying = cToken.balanceOfUnderlying(account);
uint tokenBalance;
uint tokenAllowance;
if (compareStrings(cToken.symbol(), "cETH")) {
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) {
uint cTokenCount = cTokens.length;
CTokenBalances[] memory res = new CTokenBalances[](cTokenCount);
for (uint i = 0; i < cTokenCount; i++) {
res[i] = cTokenBalances(cTokens[i], account);
}
return res;
}
struct CTokenUnderlyingPrice {
address cToken;
uint 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) {
uint cTokenCount = cTokens.length;
CTokenUnderlyingPrice[] memory res = new CTokenUnderlyingPrice[](cTokenCount);
for (uint i = 0; i < cTokenCount; i++) {
res[i] = cTokenUnderlyingPrice(cTokens[i]);
}
return res;
}
struct AccountLimits {
CToken[] markets;
uint liquidity;
uint shortfall;
}
function getAccountLimits(ComptrollerLensInterface comptroller, address account) public returns (AccountLimits memory) {
(uint errorCode, uint liquidity, uint 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 {
uint balance;
uint votes;
address delegate;
}
function getCompBalanceMetadata(Comp comp, address account) external view returns (CompBalanceMetadata memory) {
return CompBalanceMetadata({
balance: comp.balanceOf(account),
votes: uint256(comp.getCurrentVotes(account)),
delegate: comp.delegates(account)
});
}
struct CompBalanceMetadataExt {
uint balance;
uint votes;
address delegate;
uint allocated;
}
function getCompBalanceMetadataExt(Comp comp, ComptrollerLensInterface comptroller, address account) external returns (CompBalanceMetadataExt memory) {
uint balance = comp.balanceOf(account);
comptroller.claimComp(account);
uint newBalance = comp.balanceOf(account);
uint accrued = comptroller.compAccrued(account);
uint total = add(accrued, newBalance, "sum comp total");
uint allocated = sub(total, balance, "sub allocated");
return CompBalanceMetadataExt({
balance: balance,
votes: uint256(comp.getCurrentVotes(account)),
delegate: comp.delegates(account),
allocated: allocated
});
}
struct CompVotes {
uint blockNumber;
uint votes;
}
function getCompVotes(Comp comp, address account, uint32[] calldata blockNumbers) external view returns (CompVotes[] memory) {
CompVotes[] memory res = new CompVotes[](blockNumbers.length);
for (uint i = 0; i < blockNumbers.length; i++) {
res[i] = CompVotes({
blockNumber: uint256(blockNumbers[i]),
votes: uint256(comp.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(uint a, uint b, string memory errorMessage) internal pure returns (uint) {
uint c = a + b;
require(c >= a, errorMessage);
return c;
}
function sub(uint a, uint b, string memory errorMessage) internal pure returns (uint) {
require(b <= a, errorMessage);
uint c = a - b;
return c;
}
}