// SPDX-License-Identifier: GPL-3.0-or-later// Deployed with donations via Gitcoin GR9pragmasolidity 0.7.5;// a library for performing various math operationslibraryMath{
functionmin(uint256 x, uint256 y) internalpurereturns (uint256 z) {
z = x < y ? x : y;
}
functionmax(uint256 x, uint256 y) internalpurereturns (uint256 z) {
z = x > y ? x : y;
}
// babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)functionsqrt(uint256 y) internalpurereturns (uint256 z) {
if (y >3) {
z = y;
uint256 x = y /2+1;
while (x < z) {
z = x;
x = (y / x + x) /2;
}
} elseif (y !=0) {
z =1;
}
}
}
Contract Source Code
File 5 of 7: SafeMath.sol
// SPDX-License-Identifier: GPL-3.0-or-later// Deployed with donations via Gitcoin GR9pragmasolidity 0.7.5;// a library for performing overflow-safe math, courtesy of DappHub (https://github.com/dapphub/ds-math)librarySafeMath{
functionadd(uint256 x, uint256 y) internalpurereturns (uint256 z) {
require((z = x + y) >= x, 'SM_ADD_OVERFLOW');
}
functionsub(uint256 x, uint256 y) internalpurereturns (uint256 z) {
z = sub(x, y, 'SM_SUB_UNDERFLOW');
}
functionsub(uint256 x,
uint256 y,
stringmemory message
) internalpurereturns (uint256 z) {
require((z = x - y) <= x, message);
}
functionmul(uint256 x, uint256 y) internalpurereturns (uint256 z) {
require(y ==0|| (z = x * y) / y == x, 'SM_MUL_OVERFLOW');
}
functiondiv(uint256 a, uint256 b) internalpurereturns (uint256) {
require(b >0, 'SM_DIV_BY_ZERO');
uint256 c = a / b;
return c;
}
functionceil_div(uint256 a, uint256 b) internalpurereturns (uint256 c) {
c = div(a, b);
if (c == mul(a, b)) {
return c;
} else {
return add(c, 1);
}
}
functiontoUint32(uint256 n) internalpurereturns (uint32) {
require(n <2**32, 'IS_EXCEEDS_32_BITS');
returnuint32(n);
}
functiontoUint96(uint256 n) internalpurereturns (uint96) {
require(n <2**96, 'IT_EXCEEDS_96_BITS');
returnuint96(n);
}
functionadd96(uint96 a, uint96 b) internalpurereturns (uint96 c) {
c = a + b;
require(c >= a, 'SM_ADD_OVERFLOW');
}
functionsub96(uint96 a, uint96 b) internalpurereturns (uint96) {
require(b <= a, 'SM_SUB_UNDERFLOW');
return a - b;
}
}
Contract Source Code
File 6 of 7: TransferHelper.sol
// SPDX-License-Identifier: GPL-3.0-or-later// Deployed with donations via Gitcoin GR9pragmasolidity 0.7.5;// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/falselibraryTransferHelper{
functionsafeApprove(address token,
address to,
uint256 value
) internal{
// bytes4(keccak256(bytes('approve(address,uint256)')));
(bool success, bytesmemory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
require(success && (data.length==0||abi.decode(data, (bool))), 'TH_APPROVE_FAILED');
}
functionsafeTransfer(address token,
address to,
uint256 value
) internal{
// bytes4(keccak256(bytes('transfer(address,uint256)')));
(bool success, bytesmemory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(success && (data.length==0||abi.decode(data, (bool))), 'TH_TRANSFER_FAILED');
}
functionsafeTransferFrom(address token,
addressfrom,
address to,
uint256 value
) internal{
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(bool success, bytesmemory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(success && (data.length==0||abi.decode(data, (bool))), 'TH_TRANSFER_FROM_FAILED');
}
functionsafeTransferETH(address to, uint256 value) internal{
(bool success, ) = to.call{ value: value }(newbytes(0));
require(success, 'TH_ETH_TRANSFER_FAILED');
}
}
Contract Source Code
File 7 of 7: Votes.sol
// SPDX-License-Identifier: GPL-3.0-or-later// CODE COPIED FROM COMPOUND PROTOCOL (https://github.com/compound-finance/compound-protocol/tree/b9b14038612d846b83f8a009a82c38974ff2dcfe)// Copyright 2020 Compound Labs, Inc.// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.// CODE WAS SLIGHTLY MODIFIED// Deployed with donations via Gitcoin GR9pragmasolidity 0.7.5;import'SafeMath.sol';
contractVotes{
usingSafeMathforuint96;
structCheckpoint {
uint32 fromBlock;
uint96 votes;
}
mapping(address=>mapping(uint32=> Checkpoint)) public checkpoints;
mapping(address=>uint32) public checkpointsLength;
eventDelegateVotesChanged(addressindexed account, uint96 oldVotes, uint96 newVotes);
functiongetCurrentVotes(address account) externalviewreturns (uint96) {
// out of bounds access is safe and returns 0 votesreturn checkpoints[account][checkpointsLength[account] -1].votes;
}
function_getPriorVotes(address account, uint256 blockNumber) internalviewreturns (uint96) {
require(blockNumber <block.number, 'VO_NOT_YET_DETERMINED');
uint32 n = checkpointsLength[account];
if (n ==0) {
return0;
}
if (checkpoints[account][n -1].fromBlock <= blockNumber) {
return checkpoints[account][n -1].votes;
}
if (checkpoints[account][0].fromBlock > blockNumber) {
return0;
}
uint32 lower =0;
uint32 upper = n -1;
while (upper > lower) {
uint32 center = upper - (upper - lower) /2;
Checkpoint memory checkpoint = checkpoints[account][center];
if (checkpoint.fromBlock == blockNumber) {
return checkpoint.votes;
} elseif (checkpoint.fromBlock < blockNumber) {
lower = center;
} else {
upper = center -1;
}
}
return checkpoints[account][lower].votes;
}
function_updateVotes(address giver,
address receiver,
uint96 votes
) internal{
if (giver == receiver || votes ==0) {
return;
}
if (giver !=address(0)) {
uint32 n = checkpointsLength[giver];
require(n >0, 'VO_INSUFFICIENT_VOTES');
// out of bounds access is safe and returns 0 votesuint96 oldVotes = checkpoints[giver][n -1].votes;
uint96 newVotes = oldVotes.sub96(votes);
_writeCheckpoint(giver, n, newVotes);
}
if (receiver !=address(0)) {
uint32 n = checkpointsLength[receiver];
// out of bounds access is safe and returns 0 votesuint96 oldVotes = checkpoints[receiver][n -1].votes;
uint96 newVotes = oldVotes.add96(votes);
_writeCheckpoint(receiver, n, newVotes);
}
}
function_writeCheckpoint(address account,
uint32 n,
uint96 votes
) internal{
uint32 blockNumber = safe32(block.number);
// out of bounds access is safe and returns 0 votesuint96 oldVotes = checkpoints[account][n -1].votes;
if (n >0&& checkpoints[account][n -1].fromBlock == blockNumber) {
checkpoints[account][n -1].votes = votes;
} else {
checkpoints[account][n] = Checkpoint(blockNumber, votes);
checkpointsLength[account] = n +1;
}
emit DelegateVotesChanged(account, oldVotes, votes);
}
functionsafe32(uint256 n) internalpurereturns (uint32) {
require(n <2**32, 'VO_EXCEEDS_32_BITS');
returnuint32(n);
}
}