This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
UnionLens.sol
126 lines (103 loc) · 4.58 KB
/
UnionLens.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
//SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
import {IUToken} from "./interfaces/IUToken.sol";
import {IUserManager} from "./interfaces/IUserManager.sol";
import {IMarketRegistry} from "./interfaces/IMarketRegistry.sol";
/**
* @author Union
* @title UnionLens is a view layer contract intended to be used by a UI
*/
contract UnionLens {
/* -------------------------------------------------------------------
Types
------------------------------------------------------------------- */
struct UserInfo {
bool isMember;
bool isOverdue;
uint256 memberFrozen;
uint256 stakedAmount;
uint256 locked;
uint256 voucherCount;
uint256 voucheeCount;
uint256 accountBorrow;
}
struct VouchInfo {
uint256 trust;
uint256 vouch;
uint256 locked;
uint256 lastUpdated;
}
struct RelatedInfo {
VouchInfo voucher;
VouchInfo vouchee;
}
/* -------------------------------------------------------------------
Storage
------------------------------------------------------------------- */
IMarketRegistry public marketRegistry;
/* -------------------------------------------------------------------
Constructor
------------------------------------------------------------------- */
constructor(IMarketRegistry _marketRegistry) {
marketRegistry = _marketRegistry;
}
/* -------------------------------------------------------------------
Core Functions
------------------------------------------------------------------- */
function getUserInfo(address underlying, address user) public view returns (UserInfo memory userInfo) {
IUserManager userManager = IUserManager(marketRegistry.userManagers(underlying));
IUToken uToken = IUToken(marketRegistry.uTokens(underlying));
(bool isMember, uint96 locked, uint96 stakedAmount) = userManager.stakers(user);
userInfo.isOverdue = uToken.checkIsOverdue(user);
userInfo.memberFrozen = userManager.memberFrozen(user);
userInfo.isMember = isMember;
userInfo.locked = uint256(locked);
userInfo.stakedAmount = uint256(stakedAmount);
userInfo.voucherCount = userManager.getVoucherCount(user);
userInfo.voucheeCount = userManager.getVoucheeCount(user);
userInfo.accountBorrow = uToken.getBorrowed(user);
}
function getRelatedInfo(
address underlying,
address staker,
address borrower
) public view returns (RelatedInfo memory related) {
IUserManager userManager = IUserManager(marketRegistry.userManagers(underlying));
(, , uint96 stakerStakedAmount) = userManager.stakers(staker);
(, , uint96 borrowerStakedAmount) = userManager.stakers(borrower);
bool isSet;
uint256 idx;
// Information about the relationship of this borrower to the
// staker such as how much trust has this staker given the borrower
// and how much of the vouch has this borrower locked
(isSet, idx) = userManager.voucherIndexes(borrower, staker);
if (isSet) {
(, uint96 trust, uint96 locked, uint64 lastUpdated) = userManager.vouchers(borrower, idx);
VouchInfo memory vouchInfo;
vouchInfo.trust = uint256(trust);
vouchInfo.vouch = trust > stakerStakedAmount ? stakerStakedAmount : trust;
vouchInfo.locked = uint256(locked);
vouchInfo.lastUpdated = uint256(lastUpdated);
related.voucher = vouchInfo;
}
// Information about the relationship of this staker to the borrower
// How much trust has this staker given the borrower, what is the vouch
(isSet, idx) = userManager.voucherIndexes(staker, borrower);
if (isSet) {
(, uint96 trust, uint96 locked, uint64 lastUpdated) = userManager.vouchers(staker, idx);
VouchInfo memory vouchInfo;
vouchInfo.trust = uint256(trust);
vouchInfo.vouch = trust > borrowerStakedAmount ? borrowerStakedAmount : trust;
vouchInfo.locked = uint256(locked);
vouchInfo.lastUpdated = uint256(lastUpdated);
related.vouchee = vouchInfo;
}
}
/* -------------------------------------------------------------------
Internal Functions
------------------------------------------------------------------- */
function _vouchee(bytes32 b) private pure returns (address addr, uint96 n) {
addr = address(bytes20(b));
n = uint96(bytes12(bytes20(uint160(2**160 - 1)) & (b << 160)));
}
}