Skip to content

Commit 71e9495

Browse files
committed
unchecked math in the lens, to reduce RPC credit usage
1 parent c1faa43 commit 71e9495

File tree

1 file changed

+84
-72
lines changed

1 file changed

+84
-72
lines changed

src/MaglevLens.sol

Lines changed: 84 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ contract MaglevLens {
1111
// Packed: underlying asset (address), decimals (uint8), symbol (variable)
1212

1313
function vaultsStatic(address[] calldata vaults) external view returns (bytes[] memory output) {
14-
output = new bytes[](vaults.length);
15-
for (uint256 i; i < vaults.length; ++i) {
16-
IEVault v = IEVault(vaults[i]);
17-
output[i] = abi.encodePacked(v.asset(), v.decimals(), v.symbol());
14+
unchecked {
15+
output = new bytes[](vaults.length);
16+
for (uint256 i; i < vaults.length; ++i) {
17+
IEVault v = IEVault(vaults[i]);
18+
output[i] = abi.encodePacked(v.asset(), v.decimals(), v.symbol());
19+
}
1820
}
1921
}
2022

@@ -24,19 +26,21 @@ contract MaglevLens {
2426
}
2527

2628
function vaultsGlobal(address[] calldata vaults) external view returns (VaultGlobal[] memory output) {
27-
output = new VaultGlobal[](vaults.length);
29+
unchecked {
30+
output = new VaultGlobal[](vaults.length);
2831

29-
for (uint256 i; i < vaults.length; ++i) {
30-
IEVault v = IEVault(vaults[i]);
32+
for (uint256 i; i < vaults.length; ++i) {
33+
IEVault v = IEVault(vaults[i]);
3134

32-
uint256 cash = v.cash();
33-
uint256 borrows = v.totalBorrows();
35+
uint256 cash = v.cash();
36+
uint256 borrows = v.totalBorrows();
3437

35-
(uint256 borrowAPY, uint256 supplyAPY) = _computeAPYs(v.interestRate(), cash, borrows, v.interestFee());
36-
(uint16 supplyCap, uint16 borrowCap) = v.caps();
38+
(uint256 borrowAPY, uint256 supplyAPY) = _computeAPYs(v.interestRate(), cash, borrows, v.interestFee());
39+
(uint16 supplyCap, uint16 borrowCap) = v.caps();
3740

38-
output[i].packed1 = (cash << (112 + 16 + 16)) | (borrows << (16 + 16)) | (supplyCap << 16) | borrowCap;
39-
output[i].packed2 = (v.totalSupply() << (48 + 48)) | (supplyAPY << 48) | borrowAPY;
41+
output[i].packed1 = (cash << (112 + 16 + 16)) | (borrows << (16 + 16)) | (supplyCap << 16) | borrowCap;
42+
output[i].packed2 = (v.totalSupply() << (48 + 48)) | (supplyAPY << 48) | borrowAPY;
43+
}
4044
}
4145
}
4246

@@ -63,29 +67,31 @@ contract MaglevLens {
6367
}
6468

6569
function vaultsDetailed(address[] calldata vaults) external view returns (VaultDetailed[] memory output) {
66-
output = new VaultDetailed[](vaults.length);
67-
68-
for (uint256 i; i < vaults.length; ++i) {
69-
IEVault v = IEVault(vaults[i]);
70-
VaultDetailed memory o = output[i];
71-
72-
o.governorAdmin = v.governorAdmin();
73-
o.feeReceiver = v.feeReceiver();
74-
o.interestFee = v.interestFee();
75-
o.interestRateModel = v.interestRateModel();
76-
o.protocolFeeShare = v.protocolFeeShare();
77-
o.protocolFeeReceiver = v.protocolFeeReceiver();
78-
o.maxLiquidationDiscount = v.maxLiquidationDiscount();
79-
o.liquidationCoolOffTime = v.liquidationCoolOffTime();
80-
(o.hookTarget, o.hookedOps) = v.hookConfig();
81-
o.configFlags = v.configFlags();
82-
o.unitOfAccount = v.unitOfAccount();
83-
o.oracle = v.oracle();
84-
85-
o.dToken = v.dToken();
86-
87-
o.accumulatedFees = v.accumulatedFees();
88-
o.creator = v.creator();
70+
unchecked {
71+
output = new VaultDetailed[](vaults.length);
72+
73+
for (uint256 i; i < vaults.length; ++i) {
74+
IEVault v = IEVault(vaults[i]);
75+
VaultDetailed memory o = output[i];
76+
77+
o.governorAdmin = v.governorAdmin();
78+
o.feeReceiver = v.feeReceiver();
79+
o.interestFee = v.interestFee();
80+
o.interestRateModel = v.interestRateModel();
81+
o.protocolFeeShare = v.protocolFeeShare();
82+
o.protocolFeeReceiver = v.protocolFeeReceiver();
83+
o.maxLiquidationDiscount = v.maxLiquidationDiscount();
84+
o.liquidationCoolOffTime = v.liquidationCoolOffTime();
85+
(o.hookTarget, o.hookedOps) = v.hookConfig();
86+
o.configFlags = v.configFlags();
87+
o.unitOfAccount = v.unitOfAccount();
88+
o.oracle = v.oracle();
89+
90+
o.dToken = v.dToken();
91+
92+
o.accumulatedFees = v.accumulatedFees();
93+
o.creator = v.creator();
94+
}
8995
}
9096
}
9197

@@ -98,29 +104,31 @@ contract MaglevLens {
98104
view
99105
returns (VaultPersonalState[] memory output)
100106
{
101-
uint256 numAccounts;
102-
for (uint256 b = subAccountBitmask; b != 0; b >>= 1) {
103-
if (b & 1 != 0) numAccounts++;
104-
}
107+
unchecked {
108+
uint256 numAccounts;
109+
for (uint256 b = subAccountBitmask; b != 0; b >>= 1) {
110+
if (b & 1 != 0) numAccounts++;
111+
}
105112

106-
output = new VaultPersonalState[](numAccounts * vaults.length);
113+
output = new VaultPersonalState[](numAccounts * vaults.length);
107114

108-
uint256 currAccount;
109-
for (uint256 i;; ++i) {
110-
if (subAccountBitmask & (1 << i) == 0) continue;
115+
uint256 currAccount;
116+
for (uint256 i;; ++i) {
117+
if (subAccountBitmask & (1 << i) == 0) continue;
111118

112-
address a = address(uint160(uint256(uint160(me)) ^ i));
119+
address a = address(uint160(uint256(uint160(me)) ^ i));
113120

114-
for (uint256 j; j < vaults.length; ++j) {
115-
IEVault v = IEVault(vaults[j]);
121+
for (uint256 j; j < vaults.length; ++j) {
122+
IEVault v = IEVault(vaults[j]);
116123

117-
uint256 index = (currAccount * vaults.length) + j;
118-
uint256 flags = (IEVC(evc).isCollateralEnabled(a, address(v)) ? 1 : 0)
119-
| (IEVC(evc).isControllerEnabled(a, address(v)) ? 2 : 0);
120-
output[index].packed = (flags << 224) | (v.balanceOf(a) << 112) | v.debtOf(a);
121-
}
124+
uint256 index = (currAccount * vaults.length) + j;
125+
uint256 flags = (IEVC(evc).isCollateralEnabled(a, address(v)) ? 1 : 0)
126+
| (IEVC(evc).isControllerEnabled(a, address(v)) ? 2 : 0);
127+
output[index].packed = (flags << 224) | (v.balanceOf(a) << 112) | v.debtOf(a);
128+
}
122129

123-
if (++currAccount >= numAccounts) break;
130+
if (++currAccount >= numAccounts) break;
131+
}
124132
}
125133
}
126134

@@ -140,36 +148,40 @@ contract MaglevLens {
140148
pure
141149
returns (uint256 borrowAPY, uint256 supplyAPY)
142150
{
143-
uint256 totalAssets = cash + borrows;
144-
bool overflow;
151+
unchecked {
152+
uint256 totalAssets = cash + borrows;
153+
bool overflow;
145154

146-
(borrowAPY, overflow) = RPow.rpow(borrowSPY + 1e27, SECONDS_PER_YEAR, 1e27);
155+
(borrowAPY, overflow) = RPow.rpow(borrowSPY + 1e27, SECONDS_PER_YEAR, 1e27);
147156

148-
if (overflow) return (0, 0);
157+
if (overflow) return (0, 0);
149158

150-
borrowAPY -= 1e27;
151-
supplyAPY = totalAssets == 0 ? 0 : borrowAPY * borrows * (1e4 - interestFee) / totalAssets / 1e4;
159+
borrowAPY -= 1e27;
160+
supplyAPY = totalAssets == 0 ? 0 : borrowAPY * borrows * (1e4 - interestFee) / totalAssets / 1e4;
152161

153-
borrowAPY /= 1e18;
154-
supplyAPY /= 1e18;
162+
borrowAPY /= 1e18;
163+
supplyAPY /= 1e18;
164+
}
155165
}
156166

157167
function getLTVMatrix(address[] calldata vaults, bool liquidationLtv)
158168
external
159169
view
160170
returns (uint16[] memory ltvs)
161171
{
162-
uint256 num = vaults.length;
163-
ltvs = new uint16[](num * num);
164-
165-
for (uint256 i = 0; i < num; ++i) {
166-
address collateralVault = vaults[i];
167-
168-
for (uint256 j = 0; j < num; ++j) {
169-
if (i == j) continue;
170-
IEVault debtVault = IEVault(vaults[j]);
171-
ltvs[(i * num) + j] =
172-
liquidationLtv ? debtVault.LTVLiquidation(collateralVault) : debtVault.LTVBorrow(collateralVault);
172+
unchecked {
173+
uint256 num = vaults.length;
174+
ltvs = new uint16[](num * num);
175+
176+
for (uint256 i = 0; i < num; ++i) {
177+
address collateralVault = vaults[i];
178+
179+
for (uint256 j = 0; j < num; ++j) {
180+
if (i == j) continue;
181+
IEVault debtVault = IEVault(vaults[j]);
182+
ltvs[(i * num) + j] =
183+
liquidationLtv ? debtVault.LTVLiquidation(collateralVault) : debtVault.LTVBorrow(collateralVault);
184+
}
173185
}
174186
}
175187
}

0 commit comments

Comments
 (0)