pragmasolidity ^0.5.16;import"./CTokenInterfaces.sol";
/**
* @title Compound's CEtherDelegator Contract
* @notice CTokens which wrap Ether and delegate to an implementation
* @author Compound
*/contractCEtherDelegatorisCDelegatorInterface, CTokenAdminStorage{
/**
* @notice Construct a new CEther money market
* @param comptroller_ The address of the Comptroller
* @param interestRateModel_ The address of the interest rate model
* @param initialExchangeRateMantissa_ The initial exchange rate, scaled by 1e18
* @param name_ ERC-20 name of this token
* @param symbol_ ERC-20 symbol of this token
* @param decimals_ ERC-20 decimal precision of this token
* @param admin_ Address of the administrator of this token
* @param implementation_ The address of the implementation the contract delegates to
* @param becomeImplementationData The encoded args for becomeImplementation
*/constructor(ComptrollerInterface comptroller_,
InterestRateModel interestRateModel_,
uint initialExchangeRateMantissa_,
stringmemory name_,
stringmemory symbol_,
uint8 decimals_,
addresspayable admin_,
address implementation_,
bytesmemory becomeImplementationData,
uint256 reserveFactorMantissa_,
uint256 adminFeeMantissa_) public{
// Creator of the contract is admin during initialization
admin =msg.sender;
// First delegate gets to initialize the delegator (i.e. storage contract)
delegateTo(implementation_, abi.encodeWithSignature("initialize(address,address,uint256,string,string,uint8,uint256,uint256)",
comptroller_,
interestRateModel_,
initialExchangeRateMantissa_,
name_,
symbol_,
decimals_,
reserveFactorMantissa_,
adminFeeMantissa_));
// New implementations always get set via the settor (post-initialize)
_setImplementation(implementation_, false, becomeImplementationData);
// Set the proper admin now that initialization is done
admin = admin_;
}
/**
* @notice Called by the admin to update the implementation of the delegator
* @param implementation_ The address of the new implementation for delegation
* @param allowResign Flag to indicate whether to call _resignImplementation on the old implementation
* @param becomeImplementationData The encoded bytes data to be passed to _becomeImplementation
*/function_setImplementation(address implementation_, bool allowResign, bytesmemory becomeImplementationData) public{
require(hasAdminRights(), "CErc20Delegator::_setImplementation: Caller must be admin");
if (allowResign) {
delegateToImplementation(abi.encodeWithSignature("_resignImplementation()"));
}
address oldImplementation = implementation;
implementation = implementation_;
delegateToImplementation(abi.encodeWithSignature("_becomeImplementation(bytes)", becomeImplementationData));
emit NewImplementation(oldImplementation, implementation);
}
/**
* @notice Internal method to delegate execution to another contract
* @dev It returns to the external caller whatever the implementation returns or forwards reverts
* @param callee The contract to delegatecall
* @param data The raw data to delegatecall
* @return The returned bytes from the delegatecall
*/functiondelegateTo(address callee, bytesmemory data) internalreturns (bytesmemory) {
(bool success, bytesmemory returnData) = callee.delegatecall(data);
assembly {
ifeq(success, 0) {
revert(add(returnData, 0x20), returndatasize)
}
}
return returnData;
}
/**
* @notice Delegates execution to the implementation contract
* @dev It returns to the external caller whatever the implementation returns or forwards reverts
* @param data The raw data to delegatecall
* @return The returned bytes from the delegatecall
*/functiondelegateToImplementation(bytesmemory data) publicreturns (bytesmemory) {
return delegateTo(implementation, data);
}
/**
* @notice Delegates execution to an implementation contract
* @dev It returns to the external caller whatever the implementation returns or forwards reverts
*/function () externalpayable{
// delegate all other functions to current implementation
(bool success, ) = implementation.delegatecall(msg.data);
assembly {
let free_mem_ptr :=mload(0x40)
returndatacopy(free_mem_ptr, 0, returndatasize)
switch success
case0 { revert(free_mem_ptr, returndatasize) }
default { return(free_mem_ptr, returndatasize) }
}
}
}
Contract Source Code
File 2 of 5: CTokenInterfaces.sol
pragmasolidity ^0.5.16;import"./IFuseFeeDistributor.sol";
import"./ComptrollerInterface.sol";
import"./InterestRateModel.sol";
contractCTokenAdminStorage{
/**
* @notice Administrator for Fuse
*/
IFuseFeeDistributor internalconstant fuseAdmin = IFuseFeeDistributor(0xa731585ab05fC9f83555cf9Bff8F58ee94e18F85);
/**
* @notice Administrator for this contract
*/addresspayablepublic admin;
/**
* @notice Whether or not the Fuse admin has admin rights
*/boolpublic fuseAdminHasRights =true;
/**
* @notice Whether or not the admin has admin rights
*/boolpublic adminHasRights =true;
/**
* @notice Returns a boolean indicating if the sender has admin rights
*/functionhasAdminRights() internalviewreturns (bool) {
return (msg.sender== admin && adminHasRights) || (msg.sender==address(fuseAdmin) && fuseAdminHasRights);
}
}
contractCTokenStorageisCTokenAdminStorage{
/**
* @dev Guard variable for re-entrancy checks
*/boolinternal _notEntered;
/**
* @notice EIP-20 token name for this token
*/stringpublic name;
/**
* @notice EIP-20 token symbol for this token
*/stringpublic symbol;
/**
* @notice EIP-20 token decimals for this token
*/uint8public decimals;
/**
* @notice Maximum borrow rate that can ever be applied (.0005% / block)
*/uintinternalconstant borrowRateMaxMantissa =0.0005e16;
/**
* @notice Maximum fraction of interest that can be set aside for reserves + fees
*/uintinternalconstant reserveFactorPlusFeesMaxMantissa =1e18;
/**
* @notice Pending administrator for this contract
*/addresspayablepublic pendingAdmin;
/**
* @notice Contract which oversees inter-cToken operations
*/
ComptrollerInterface public comptroller;
/**
* @notice Model which tells what the current interest rate should be
*/
InterestRateModel public interestRateModel;
/**
* @notice Initial exchange rate used when minting the first CTokens (used when totalSupply = 0)
*/uintinternal initialExchangeRateMantissa;
/**
* @notice Fraction of interest currently set aside for admin fees
*/uintpublic adminFeeMantissa;
/**
* @notice Fraction of interest currently set aside for Fuse fees
*/uintpublic fuseFeeMantissa;
/**
* @notice Fraction of interest currently set aside for reserves
*/uintpublic reserveFactorMantissa;
/**
* @notice Block number that interest was last accrued at
*/uintpublic accrualBlockNumber;
/**
* @notice Accumulator of the total earned interest rate since the opening of the market
*/uintpublic borrowIndex;
/**
* @notice Total amount of outstanding borrows of the underlying in this market
*/uintpublic totalBorrows;
/**
* @notice Total amount of reserves of the underlying held in this market
*/uintpublic totalReserves;
/**
* @notice Total amount of admin fees of the underlying held in this market
*/uintpublic totalAdminFees;
/**
* @notice Total amount of Fuse fees of the underlying held in this market
*/uintpublic totalFuseFees;
/**
* @notice Total number of tokens in circulation
*/uintpublic totalSupply;
/**
* @notice Official record of token balances for each account
*/mapping (address=>uint) internal accountTokens;
/**
* @notice Approved token transfer amounts on behalf of others
*/mapping (address=>mapping (address=>uint)) internal transferAllowances;
/**
* @notice Container for borrow balance information
* @member principal Total balance (with accrued interest), after applying the most recent balance-changing action
* @member interestIndex Global borrowIndex as of the most recent balance-changing action
*/structBorrowSnapshot {
uint principal;
uint interestIndex;
}
/**
* @notice Mapping of account addresses to outstanding borrow balances
*/mapping(address=> BorrowSnapshot) internal accountBorrows;
}
contractCTokenInterfaceisCTokenStorage{
/**
* @notice Indicator that this is a CToken contract (for inspection)
*/boolpublicconstant isCToken =true;
/**
* @notice Indicator that this is or is not a CEther contract (for inspection)
*/boolpublicconstant isCEther =false;
/*** Market Events ***//**
* @notice Event emitted when interest is accrued
*/eventAccrueInterest(uint cashPrior, uint interestAccumulated, uint borrowIndex, uint totalBorrows);
/**
* @notice Event emitted when tokens are minted
*/eventMint(address minter, uint mintAmount, uint mintTokens);
/**
* @notice Event emitted when tokens are redeemed
*/eventRedeem(address redeemer, uint redeemAmount, uint redeemTokens);
/**
* @notice Event emitted when underlying is borrowed
*/eventBorrow(address borrower, uint borrowAmount, uint accountBorrows, uint totalBorrows);
/**
* @notice Event emitted when a borrow is repaid
*/eventRepayBorrow(address payer, address borrower, uint repayAmount, uint accountBorrows, uint totalBorrows);
/**
* @notice Event emitted when a borrow is liquidated
*/eventLiquidateBorrow(address liquidator, address borrower, uint repayAmount, address cTokenCollateral, uint seizeTokens);
/*** Admin Events ***//**
* @notice Event emitted when the Fuse admin renounces their rights
*/eventFuseAdminRightsRenounced();
/**
* @notice Event emitted when the admin renounces their rights
*/eventAdminRightsRenounced();
/**
* @notice Event emitted when pendingAdmin is changed
*/eventNewPendingAdmin(address oldPendingAdmin, address newPendingAdmin);
/**
* @notice Event emitted when pendingAdmin is accepted, which means admin is updated
*/eventNewAdmin(address oldAdmin, address newAdmin);
/**
* @notice Event emitted when comptroller is changed
*/eventNewComptroller(ComptrollerInterface oldComptroller, ComptrollerInterface newComptroller);
/**
* @notice Event emitted when interestRateModel is changed
*/eventNewMarketInterestRateModel(InterestRateModel oldInterestRateModel, InterestRateModel newInterestRateModel);
/**
* @notice Event emitted when the reserve factor is changed
*/eventNewReserveFactor(uint oldReserveFactorMantissa, uint newReserveFactorMantissa);
/**
* @notice Event emitted when the reserves are added
*/eventReservesAdded(address benefactor, uint addAmount, uint newTotalReserves);
/**
* @notice Event emitted when the reserves are reduced
*/eventReservesReduced(address admin, uint reduceAmount, uint newTotalReserves);
/**
* @notice Event emitted when the admin fee is changed
*/eventNewAdminFee(uint oldAdminFeeMantissa, uint newAdminFeeMantissa);
/**
* @notice Event emitted when the Fuse fee is changed
*/eventNewFuseFee(uint oldFuseFeeMantissa, uint newFuseFeeMantissa);
/**
* @notice EIP20 Transfer event
*/eventTransfer(addressindexedfrom, addressindexed to, uint amount);
/**
* @notice EIP20 Approval event
*/eventApproval(addressindexed owner, addressindexed spender, uint amount);
/**
* @notice Failure event
*/eventFailure(uinterror, uint info, uint detail);
/*** User Interface ***/functiontransfer(address dst, uint amount) externalreturns (bool);
functiontransferFrom(address src, address dst, uint amount) externalreturns (bool);
functionapprove(address spender, uint amount) externalreturns (bool);
functionallowance(address owner, address spender) externalviewreturns (uint);
functionbalanceOf(address owner) externalviewreturns (uint);
functionbalanceOfUnderlying(address owner) externalreturns (uint);
functiongetAccountSnapshot(address account) externalviewreturns (uint, uint, uint, uint);
functionborrowRatePerBlock() externalviewreturns (uint);
functionsupplyRatePerBlock() externalviewreturns (uint);
functiontotalBorrowsCurrent() externalreturns (uint);
functionborrowBalanceCurrent(address account) externalreturns (uint);
functionborrowBalanceStored(address account) publicviewreturns (uint);
functionexchangeRateCurrent() publicreturns (uint);
functionexchangeRateStored() publicviewreturns (uint);
functiongetCash() externalviewreturns (uint);
functionaccrueInterest() publicreturns (uint);
functionseize(address liquidator, address borrower, uint seizeTokens) externalreturns (uint);
/*** Admin Functions ***/function_setPendingAdmin(addresspayable newPendingAdmin) externalreturns (uint);
function_acceptAdmin() externalreturns (uint);
function_setComptroller(ComptrollerInterface newComptroller) publicreturns (uint);
function_setReserveFactor(uint newReserveFactorMantissa) externalreturns (uint);
function_reduceReserves(uint reduceAmount) externalreturns (uint);
function_setInterestRateModel(InterestRateModel newInterestRateModel) publicreturns (uint);
}
contractCErc20Storage{
/**
* @notice Underlying asset for this CToken
*/addresspublic underlying;
}
contractCErc20InterfaceisCErc20Storage{
/*** User Interface ***/functionmint(uint mintAmount) externalreturns (uint);
functionredeem(uint redeemTokens) externalreturns (uint);
functionredeemUnderlying(uint redeemAmount) externalreturns (uint);
functionborrow(uint borrowAmount) externalreturns (uint);
functionrepayBorrow(uint repayAmount) externalreturns (uint);
functionrepayBorrowBehalf(address borrower, uint repayAmount) externalreturns (uint);
functionliquidateBorrow(address borrower, uint repayAmount, CTokenInterface cTokenCollateral) externalreturns (uint);
/*** Admin Functions ***/function_addReserves(uint addAmount) externalreturns (uint);
}
contractCEtherInterfaceisCErc20Storage{
/**
* @notice Indicator that this is a CEther contract (for inspection)
*/boolpublicconstant isCEther =true;
}
contractCDelegationStorage{
/**
* @notice Implementation address for this contract
*/addresspublic implementation;
}
contractCDelegatorInterfaceisCDelegationStorage{
/**
* @notice Emitted when implementation is changed
*/eventNewImplementation(address oldImplementation, address newImplementation);
/**
* @notice Called by the admin to update the implementation of the delegator
* @param implementation_ The address of the new implementation for delegation
* @param allowResign Flag to indicate whether to call _resignImplementation on the old implementation
* @param becomeImplementationData The encoded bytes data to be passed to _becomeImplementation
*/function_setImplementation(address implementation_, bool allowResign, bytesmemory becomeImplementationData) public;
}
contractCDelegateInterfaceisCDelegationStorage{
/**
* @notice Called by the delegator on a delegate to initialize it for duty
* @dev Should revert if any issues arise which make it unfit for delegation
* @param data The encoded bytes data for any initialization
*/function_becomeImplementation(bytesmemory data) public;
/**
* @notice Called by the delegator on a delegate to forfeit its responsibility
*/function_resignImplementation() public;
}
pragmasolidity ^0.5.16;/**
* @title Compound's InterestRateModel Interface
* @author Compound
*/contractInterestRateModel{
/// @notice Indicator that this is an InterestRateModel contract (for inspection)boolpublicconstant isInterestRateModel =true;
/**
* @notice Calculates the current borrow interest rate per block
* @param cash The total amount of cash the market has
* @param borrows The total amount of borrows the market has outstanding
* @param reserves The total amnount of reserves the market has
* @return The borrow rate per block (as a percentage, and scaled by 1e18)
*/functiongetBorrowRate(uint cash, uint borrows, uint reserves) externalviewreturns (uint);
/**
* @notice Calculates the current supply interest rate per block
* @param cash The total amount of cash the market has
* @param borrows The total amount of borrows the market has outstanding
* @param reserves The total amnount of reserves the market has
* @param reserveFactorMantissa The current reserve factor the market has
* @return The supply rate per block (as a percentage, and scaled by 1e18)
*/functiongetSupplyRate(uint cash, uint borrows, uint reserves, uint reserveFactorMantissa) externalviewreturns (uint);
}