Request form
Cookbook may introduce a token in the future. Share and contribute to be eligible for future airdrops.
close icon
relevant tag icon
Compound CToken
copy icon
compound
• version 1.0.0
Compound
Token
ERC20
Finance
Audited

Compound CToken

Audited

The Compound CTokens are self-contained borrowing and lending contracts. CToken contains the core logic and CErc20 and CEther add public interfaces for Erc20 tokens and ether, respectively. Each CToken is assigned an interest rate and risk model (see InterestRateModel and Comptroller sections), and allows accounts to *mint* (supply capital), *redeem* (withdraw capital), *borrow* and *repay a borrow*. Each CToken is an ERC-20 compliant token where balances represent ownership of the market.

*Visit desktop site to download or deploy

Version

1.0.0

Creator

compound

Recent Use

🍞 0xa137 downloaded
🥐 0x8359 downloaded
🥖 0x8359 saved

Last Publish

10/26/2022
Any contract you deploy is yours.
Fully owned and controlled by your wallet.
Documentation
Source Code
initialize(string,string,uint8) :
This function initializes the market by setting the initial exchange rate, comptroller, interest rate model, name, symbol, and decimals. It also sets the accrual block number and borrow index to the current block number and mantissa one, respectively. Finally, it sets the _notEntered boolean to true. (autogenerated documentation)
transferTokens(address,address,address) :
This function is used to transfer tokens from one address to another. It first checks if the transfer is allowed by the comptroller, and if not, it reverts the transaction. It then checks if the source and destination addresses are the same, and if so, it reverts the transaction. It then calculates the new allowance for the spender, the new token balance for the source and destination addresses, and updates the transfer allowance and token balances accordingly. Finally, it emits a Transfer event and returns a success code. (autogenerated documentation)
transfer(address,uint256) :
This function is used to transfer tokens from the sender's address to a specified destination address. It uses the transferTokens function to do this, which checks that the sender has enough tokens to make the transfer and then updates the token balances of both the sender and the recipient. If the transfer is successful, the function returns true, otherwise it returns false. (autogenerated documentation)
transferFrom(address,address,uint256) :
This function is used to transfer tokens from one address (src) to another address (dst). The amount of tokens to be transferred is specified by the amount parameter. The function is executed by the msg.sender, and it calls the transferTokens function to perform the actual transfer. If the transferTokens function returns NO_ERROR, the transferFrom function returns true, indicating that the transfer was successful. (autogenerated documentation)
approve(address,uint256) :
This function allows a user (msg.sender) to approve a spender to transfer a certain amount of tokens from their account. It stores the amount of tokens that the spender is allowed to transfer in the transferAllowances mapping and emits an Approval event. It then returns true to indicate that the approval was successful. (autogenerated documentation)
allowance(address,address) :
This function allows users to check the amount of tokens that have been approved for transfer from one address (owner) to another address (spender). It does this by accessing the transferAllowances mapping, which stores the amount of tokens that have been approved for transfer from the owner address to the spender address. The function then returns the amount of tokens that have been approved for transfer. (autogenerated documentation)
balanceOf(address) :
This function returns the balance of a given address. It works by accessing the accountTokens mapping, which stores the amount of tokens associated with each address, and returning the value associated with the given address. (autogenerated documentation)
balanceOfUnderlying(address) :
This function returns the balance of the underlying asset (e.g. ETH) held by a given address. It does this by first retrieving the current exchange rate from the exchangeRateCurrent() function, and then multiplying this exchange rate by the number of tokens held by the given address (stored in the accountTokens mapping). The result is then truncated to a whole number. (autogenerated documentation)
getAccountSnapshot(address) :
This function is an external view function that returns a snapshot of an account's information. It takes in an address of an account and returns a tuple of four values: an error code, the amount of tokens held by the account, the amount of borrowed balance stored for the account, and the current exchange rate stored. This function is used to get a snapshot of an account's information without making any changes to the blockchain. (autogenerated documentation)
getBlockNumber() :
This function is a view function that returns the current block number of the Ethereum blockchain. It works by accessing the block object and returning the number property of the block. (autogenerated documentation)
borrowRatePerBlock() :
This function returns the borrow rate per block for the Compound protocol. It does this by using the interestRateModel to calculate the borrow rate based on the current cash prior, total borrows, and total reserves. (autogenerated documentation)
supplyRatePerBlock() :
This function returns the supply rate per block, which is the rate at which new tokens are created and added to the total supply. It does this by calling the getSupplyRate() function from the interestRateModel, which takes in the current cash prior, total borrows, total reserves, and reserve factor mantissa as parameters. The getSupplyRate() function then calculates the supply rate based on these parameters and returns it. (autogenerated documentation)
totalBorrowsCurrent() :
This function is an override of the totalBorrowsCurrent() function in the Aave protocol. It updates the totalBorrows value by calling the accrueInterest() function, which calculates the interest accrued on all outstanding borrows. The function then returns the updated totalBorrows value. (autogenerated documentation)
borrowBalanceCurrent(address) :
This function allows a user to check their current borrow balance. It does this by first calling the accrueInterest() function to update the interest rate, and then calling the borrowBalanceStored() function to retrieve the current borrow balance for the specified account. (autogenerated documentation)
borrowBalanceStored(address) :
This function allows users to view the amount of borrowed funds stored in their account. It works by retrieving the stored balance from the internal storage of the contract and returning it to the user. (autogenerated documentation)
borrowBalanceStoredInternal(address) :
This function calculates the stored borrow balance of an account. It takes in an address of an account and returns a uint. It does this by first retrieving the borrow snapshot of the account from the accountBorrows mapping. If the principal of the borrow snapshot is 0, it returns 0. Otherwise, it calculates the stored borrow balance by multiplying the principal by the borrow index and then dividing it by the interest index of the borrow snapshot. (autogenerated documentation)
exchangeRateCurrent() :
This function is used to retrieve the current exchange rate for a given currency. It does this by first calling the accrueInterest() function, which updates the exchange rate stored in the contract, and then returning the updated exchange rate using the exchangeRateStored() function. (autogenerated documentation)
exchangeRateStored() :
This function returns the stored exchange rate of a token. It works by accessing the internal exchange rate stored in the contract and returning it to the caller. (autogenerated documentation)
exchangeRateStoredInternal() :
This function calculates the exchange rate of the token. It does this by taking the total supply of the token, the total cash, total borrows, and total reserves, and then calculating the exchange rate as the total cash plus borrows minus reserves multiplied by the exponential scale divided by the total supply. (autogenerated documentation)
getCash() :
This function is an override of the external view function getCash() and it returns the amount of cash that is currently held in the contract. It works by calling the getCashPrior() function which returns the amount of cash held in the contract. (autogenerated documentation)
accrueInterest() :
This function is used to accrue interest on the borrows of a given contract. It takes the current block number, the cash prior, the borrows prior, the reserves prior, and the borrow index prior as inputs. It then calculates the borrow rate mantissa, the block delta, and the simple interest factor. It then uses these values to calculate the interest accumulated, the total borrows new, the total reserves new, and the borrow index new. Finally, it updates the accrual block number, borrow index, total borrows, and total reserves, and emits an AccrueInterest event with the cash prior, interest accumulated, borrow index new, and total borrows new. (autogenerated documentation)
mintInternal() :
This function is an internal function that is used to mint a specified amount of tokens to a specified address. It first calls the accrueInterest() function to update the interest rate, and then calls the mintFresh() function to mint the specified amount of tokens to the specified address. (autogenerated documentation)
mintFresh(address) :
This function is used to mint new tokens for a given address. It first checks that the minting is allowed by the Comptroller, and then checks that the minting is fresh (i.e. that the accrual block number is the same as the current block number). It then calculates the exchange rate between the given amount of tokens and the actual mint amount, and updates the total supply and the account tokens of the minter accordingly. Finally, it emits the Mint and Transfer events. (autogenerated documentation)
redeemInternal() :
This function allows a user to redeem tokens from the contract. It first calls the accrueInterest() function to update the interest rate, then calls the redeemFresh() function to transfer the specified amount of tokens from the contract to the user. The redeemFresh() function takes three parameters: the address of the user, the amount of tokens to be redeemed, and a zero value to indicate that the tokens are being redeemed from the contract. (autogenerated documentation)
redeemUnderlyingInternal() :
This function allows a user to redeem a certain amount of underlying tokens from the contract. It first calls the accrueInterest() function to update the interest rate, then calls the redeemFresh() function to transfer the underlying tokens to the user. The redeemFresh() function takes in the user's address, a zero value, and the amount of tokens to be redeemed as parameters. (autogenerated documentation)
redeemFresh(address) :
This function allows a user to redeem a certain amount of tokens from the contract. The user can specify either the amount of tokens to redeem, or the amount of cash to redeem. The function then calculates the exchange rate between the tokens and cash, and checks that the user is allowed to redeem the tokens according to the Comptroller. It then checks that the redemption is fresh, meaning that it is within the same block as the last accrual. It then checks that the contract has enough cash to cover the redemption, and if so, it deducts the tokens from the user's account and transfers the cash to the user. Finally, it verifies the redemption with the Comptroller. (autogenerated documentation)
borrowInternal() :
This function is an internal function that allows a user to borrow a specified amount of funds from the contract. It first calls the accrueInterest() function to update the interest rate, and then calls the borrowFresh() function to transfer the specified amount of funds from the contract to the user. (autogenerated documentation)
borrowFresh(address) :
This function allows a borrower to borrow a specified amount of funds from the contract. It first checks that the borrower is allowed to borrow the amount from the Comptroller, then checks that the borrow is fresh (i.e. not already used), and then checks that there is enough cash available to cover the borrow. If all of these checks pass, it updates the borrower's borrow balance, transfers the funds out to the borrower, and emits an event to signal the successful borrow. (autogenerated documentation)
repayBorrowInternal() :
This function is an internal, non-reentrant function that allows a user to repay a loan. It first calls the accrueInterest() function to update the interest rate, and then calls the repayBorrowFresh() function to repay the loan. The repayBorrowFresh() function takes three parameters: the borrower, the lender, and the amount to be repaid. (autogenerated documentation)
repayBorrowBehalfInternal(address) :
This function is an internal function that is used to repay a loan on behalf of a borrower. It first calls the accrueInterest() function to update the interest rate for the loan, and then calls the repayBorrowFresh() function to repay the loan with the specified amount. The msg.sender is used as the lender, and the borrower address is passed as an argument. (autogenerated documentation)
repayBorrowFresh(address,address) :
This function allows a payer to repay a borrower's debt. It first checks that the repayment is allowed by the Comptroller, then checks that the repayment is fresh (i.e. that it is not a duplicate repayment). It then calculates the final repayment amount, transfers the funds from the payer to the borrower, and updates the borrower's account balance and the total borrows. Finally, it emits an event to signal that the repayment has been successful. (autogenerated documentation)
liquidateBorrowInternal(address) :
This function liquidates a borrower's debt by repaying the amount owed using cToken collateral. It first calls the accrueInterest() function to update the interest rate, then calls the cTokenCollateral.accrueInterest() function to update the cToken collateral's interest rate. Finally, it calls the liquidateBorrowFresh() function to repay the debt using the cToken collateral. (autogenerated documentation)
liquidateBorrowFresh(address,address) :
This function is used to liquidate a borrower's debt. It takes in the address of the liquidator, the address of the borrower, the amount to be repaid, and the address of the cToken collateral. It first checks that the liquidation is allowed by the comptroller, then checks that the accrual block number is up to date, that the borrower and liquidator are not the same address, that the repay amount is not zero or the maximum uint value, and that the borrower has enough tokens to cover the seize amount. If all of these checks pass, it calls the repayBorrowFresh function to repay the debt, then calls the seizeInternal or cTokenCollateral.seize function to seize the tokens from (autogenerated documentation)
seize(address,address) :
This function allows a liquidator to seize a certain amount of tokens from a borrower. It takes three parameters: the address of the liquidator, the address of the borrower, and the amount of tokens to be seized. The function then calls the internal seizeInternal function, which handles the actual seizing of the tokens, and returns a status code indicating that the operation was successful. (autogenerated documentation)
seizeInternal(address,address,address) :
This function is used to seize tokens from a borrower in order to liquidate their debt. It takes in the address of the seizer token, the address of the liquidator, the address of the borrower, and the amount of tokens to seize. It then calculates the amount of tokens to be seized by the protocol and the amount to be seized by the liquidator. It then updates the total reserves and total supply accordingly, and emits the appropriate Transfer and ReservesAdded events. (autogenerated documentation)
_setPendingAdmin(address) :
This function sets a new pending admin for the contract. It first checks that the sender of the transaction is the current admin. If so, it sets the new pending admin and emits an event with the old and new pending admin. It then returns a status code of NO_ERROR. (autogenerated documentation)
_acceptAdmin() :
This function is used to accept a pending admin. It first checks to make sure that the sender of the message is the same as the pending admin, and if not, it reverts the transaction. It then sets the pending admin to the admin, and sets the pending admin to address(0). Finally, it emits two events, NewAdmin and NewPendingAdmin, to notify the other participants of the change. (autogenerated documentation)
_setComptroller() :
This function sets a new ComptrollerInterface contract as the comptroller for the current contract. It first checks that the caller is the admin, and then checks that the new ComptrollerInterface contract is a valid comptroller. If both of these checks pass, the new ComptrollerInterface contract is set as the comptroller and an event is emitted to notify of the change. The function returns a uint indicating whether or not the operation was successful. (autogenerated documentation)
_setReserveFactor() :
This function sets the reserve factor of the contract. It first calls the accrueInterest() function to update the interest rate, then calls the _setReserveFactorFresh() function to set the new reserve factor. The new reserve factor is passed in as an argument and is returned by the function. (autogenerated documentation)
_setReserveFactorFresh() :
This function sets a new reserve factor for the contract. It is only accessible to the admin and requires that the accrual block number is the same as the current block number. The new reserve factor must also be within the bounds of the reserve factor max mantissa. If all of these conditions are met, the new reserve factor is set and an event is emitted. (autogenerated documentation)
_addReservesInternal() :
This function adds reserves to the contract. It first calls the accrueInterest() function to update the interest rate, then calls the _addReservesFresh() function to add the specified amount of reserves to the contract. It then returns a status code of NO_ERROR. (autogenerated documentation)
_addReservesFresh() :
This function adds reserves to the contract. It first checks to make sure that the accrual block number is the same as the current block number. It then calls the doTransferIn function to transfer the addAmount to the contract. It then updates the totalReserves variable with the new amount. Finally, it emits an event to notify that the reserves have been added. (autogenerated documentation)
_reduceReserves() :
This function reduces the reserves of the contract by a specified amount. It first calls the accrueInterest() function to update the interest rate, and then calls the _reduceReservesFresh() function to reduce the reserves by the specified amount. (autogenerated documentation)
_reduceReservesFresh() :
This function is used to reduce the total reserves of a contract. It is called by the admin of the contract and requires a reduceAmount parameter. The function first checks that the admin is calling the function, that the accrualBlockNumber is up to date, that the reduceAmount is not greater than the totalReserves, and that the reduceAmount is not greater than the getCashPrior. If all of these checks pass, the totalReserves is reduced by the reduceAmount and the doTransferOut function is called to transfer the reduceAmount out of the contract. Finally, the ReservesReduced event is emitted. (autogenerated documentation)
_setInterestRateModel() :
This function sets a new InterestRateModel for the contract. It first calls the accrueInterest() function to ensure that any accrued interest is accounted for before the new InterestRateModel is set. Finally, it calls the _setInterestRateModelFresh() function to set the new InterestRateModel and returns a uint value. (autogenerated documentation)
_setInterestRateModelFresh() :
This function allows the admin to set a new interest rate model for the market. It first checks that the sender is the admin, and that the accrual block number is the same as the current block number. It then checks that the new interest rate model is valid, and sets it as the new interest rate model. Finally, it emits an event to notify of the change. (autogenerated documentation)
getCashPrior() :
This function is a modifier that prevents a function from being re-entered. It does this by setting a boolean variable, _notEntered, to false when the function is first called. This ensures that the function can only be called once. After the function is finished, the boolean variable is set back to true, allowing the function to be called again. (autogenerated documentation)
doTransferIn(address) :
This function is a modifier that prevents a function from being re-entered. It requires that the boolean variable _notEntered is true, and sets it to false before executing the function. After the function is executed, the variable is set back to true. This ensures that the function is not re-entered. (autogenerated documentation)
doTransferOut(address) :
This function is used to transfer a specified amount of funds from the contract to an external address. It uses a modifier called nonReentrant to ensure that the function is not re-entered. This is done by setting a boolean variable called _notEntered to false when the function is called, and then setting it back to true when the function is finished. This prevents the function from being called multiple times in a row. (autogenerated documentation)

Get Cookin'
share iconShare

copy iconDownload Source
copy iconnpx cookbookdev i ctoken
copy icon

Recent Use

🍞 0xa137 downloaded
🥐 0x8359 downloaded
🥖 0x8359 saved

Last Publish

10/26/2022

Version

1.0.0

Creator

compound

Cookbook is free.
Any contract you deploy is yours.
Your contract is owned and controlled by you.