Skip to content

Commit

Permalink
Add Comp and Gov to Compund Lens
Browse files Browse the repository at this point in the history
This patch adds to the `CompoundLens` and begins to build functions to read data for Comp and Gov. We add scenarios and tests for the Lens as well to verify its view of the data. The new Lens has been deployed to all test-nets.
  • Loading branch information
hayesgm committed Apr 24, 2020
1 parent eee83bc commit 303b73d
Show file tree
Hide file tree
Showing 20 changed files with 3,905 additions and 497 deletions.
122 changes: 122 additions & 0 deletions contracts/Lens/CompoundLens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "../CErc20.sol";
import "../CToken.sol";
import "../Comptroller.sol";
import "../EIP20Interface.sol";
import "../Governance/GovernorAlpha.sol";
import "../Governance/Comp.sol";

contract CompoundLens {
struct CTokenMetadata {
Expand Down Expand Up @@ -153,6 +155,126 @@ contract CompoundLens {
});
}

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 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 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 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))));
}
Expand Down
10 changes: 9 additions & 1 deletion contracts/SimplePriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ contract SimplePriceOracle is PriceOracle {


function getUnderlyingPrice(CToken cToken) public view returns (uint) {
return prices[address(CErc20(address(cToken)).underlying())];
if (compareStrings(cToken.symbol(), "cETH")) {
return 1e18;
} else {
return prices[address(CErc20(address(cToken)).underlying())];
}
}

function setUnderlyingPrice(CToken cToken, uint underlyingPriceMantissa) public {
Expand All @@ -28,4 +32,8 @@ contract SimplePriceOracle is PriceOracle {
function assetPrices(address asset) external view returns (uint) {
return prices[asset];
}

function compareStrings(string memory a, string memory b) internal pure returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}
}
Loading

0 comments on commit 303b73d

Please sign in to comment.