Skip to content

Commit

Permalink
Merge pull request #171 from compound-finance/torrey/comp-speed-split…
Browse files Browse the repository at this point in the history
…-bandaid

Comp speed split
  • Loading branch information
arr00 authored Oct 22, 2021
2 parents fcf067f + 8547f5c commit 6548ec3
Show file tree
Hide file tree
Showing 10 changed files with 2,212 additions and 1,884 deletions.
70 changes: 69 additions & 1 deletion contracts/Lens/CompoundLens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ interface ComptrollerLensInterface {
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 {
Expand Down Expand Up @@ -56,6 +60,53 @@ contract CompoundLens {
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) {
Expand All @@ -74,6 +125,20 @@ contract CompoundLens {
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,
Expand All @@ -88,7 +153,10 @@ contract CompoundLens {
collateralFactorMantissa: collateralFactorMantissa,
underlyingAssetAddress: underlyingAssetAddress,
cTokenDecimals: cToken.decimals(),
underlyingDecimals: underlyingDecimals
underlyingDecimals: underlyingDecimals,
compSupplySpeed: compSupplySpeed,
compBorrowSpeed: compBorrowSpeed,
borrowCap: borrowCap
});
}

Expand Down
Loading

0 comments on commit 6548ec3

Please sign in to comment.