Cozy tokens
API reference for Cozy tokens. Cozy tokens are a core contract in the Cozy protocol and each market is a unique instance of a Cozy token.
Core methods
This section contains the primary methods for interacting with the Cozy protocol. Most methods in this section return a uint256
value of zero (0) for a successful call, or return an error code on a unsuccessful call. The main exceptions to this general rule are the methods that are part of the standard ERC-20 specification, such as the transfer
and balanceOf
methods.
Mint
You use the mint
function to transfer assets from your wallet to any Cozy market. You can use the mint
function when you want to do the following:
supply funds to use as collateral for protected borrowing.
deposit funds to provide protection and earn interest.
Depending on whether you are minting tokens or ETH, use one of the following function signatures:
For tokens, the amount
is the number of tokens to supply, denominated in units of the underlying token. For ETH, instead of specifying an amount, you send ETH along with the transaction and msg.value
is used in place of amount
.
Before supplying a token, remember to approve
the Cozy market. A successful mint
call returns a value of zero and transfers Cozy tokens to the caller's wallet. The number of Cozy tokens the user receives is equal to the supplied amount divided by the exchange rate.
Redeem
You use the redeem
method to redeem Cozy tokens for the underlying supplying funds. The amount of underlying received is equal to the number of Cozy tokens redeemed multiplied by the exchange rate. There are two methods you can use to redeem tokens:
A successful redeem burns the caller's Cozy tokens, transfers a proportional amount of the underlying asset, and returns a value of zero.
Borrow
After supplying collateral with the mint
method, you can use the borrow
method to borrow funds from the Cozy protocol. Borrowed funds accrue interest at the current borrow rate. If a borrower has insufficient account liquidity, however, the borrowed funds can be liquidated.
The method for borrowing funds from a market is:
where borrowAmount
is specified in units of the token to borrow.
Repay debt
You can use either the repayBorrow
or repayBorrowBehalf
function to repay borrowed funds as follows:
To repay all token debt without leaving any dust, you can specify a repayAmount
ofMAX_UINT256 = 2^256 - 1
.
To repay all ETH debt without leaving any dust, it is recommended to use a special helper contract called Maximillion
and call the following method:
where borrower
is the address of the user to pay back debt for, and cEther_
is the address of the market to pay back debt in (for example, the Cozy Ether money market, or a protection market with ETH as the underlying). When calling this method, send along more ETH than the debt amount, and after paying off the full debt, the excess ETH will be refunded.
The Maximillion
contract is deployed on mainnet at 0xf859A1AD94BcF445A406B892eF0d3082f4174088.
Liquidate
You use the liquidateBorrow
function to liquidate a user's assets when their account liquidity is negative. This action brings the user back to a solvent state. During a liquidation, the liquidator repays some or all of the user's borrow and, in return, receives a portion of their collateral, as Cozy tokens, at a discount.
The maximum portion of the borrow that can be liquidated is specified by the close factor in the Comptroller. As with mints and repays, liquidating pulls tokens from the caller, so the liquidator must approve
the Cozy token contract before liquidating.
Liquidations can be executed with the following methods:
where borrower
is the account with a shortfall to liquidate and cTokenCollateral
is the token being used by the borrower as collateral that the liquidator will receive. For token-based markets, repayAmount
specifies how much of the borrowed asset to repay, and for ETH-based markets that amount is specified by msg.value
.
Transfer
All Cozy tokens are ERC-20 compliant and can be transferred with both the transfer
and transferFrom
functions. They also implement ERC-2612 permit
function to improve the approval flow.
Token transfers fail if the transfer would put the account into an under-collateralized position.
View methods
This section contains the primary methods for viewing information about Cozy markets.
Exchange rate
Every Cozy token has it's own exchange rate, which is a function of the market's cash, borrows, reserves, and supply. This value is initialized to 0.02 when a market is created, and increases as the accrues interest. The exchange rate for a given market is calculated as:
The exchange rate is represented as a mantissa (an 18 decimal number) with a precision of 18 + underlyingDecimals - cozyTokenDecimals
. For example, all Cozy tokens have 8 decimals. If the underlying token has 6 decimals, the initial exchange rate for that market would have 18 + 6 - 8 = 16 decimals. Querying the exchange rate of this market immediately after deployment would return 0.02e16 = 200000000000000
.
Cash
Cash is the amount of the underlying token held by the Cozy token. You can read the cash held by the contract using the following function:
For markets with tokens as the underlying, this function returns the value of underlyingToken.balanceOf(address(this));
. For markets with ETH as the underlying, the function returns address(this).balance - msg.value
.
Borrows
A market's total borrows is the amount of the underlying token actively borrowed. This amount is used when computing the accrued interest. It can be queried for a given market with the function below. When a market is triggered, this value is set to zero, so the below methods always returns zero after a trigger event.
The amount being borrowed by an individual user can be read with the method below. Both methods always returns a borrow balance of zero after a market is triggered.
Supply
A market's total supply is the number of Cozy tokens that have been minted and are currently in circulation. You can use the standard ERC-20 totalSupply
method to return the number of Cozy tokens in a market as follows:
Balances
To read a user's Cozy token balance, you can use the standard ERC-20 balanceOf
method as follows:
You can also read a user's balance with the balanceOfUnderlying
function, which returns the user's Cozy token balance multiplied by the exchange rate.
For example, if a user supplies 1000 USDC with an exchange rate of 0.02, she receives 1000 / 0.02 = 50,000
Cozy tokens. Calling balanceOf
will return 50,000e8, and calling balanceOfUnderlying
will return 1000e6, since Cozy tokens have 8 decimals and USDC has 6 decimals).
Interest rates
You can use the borrowRatePerBlock
function to read the current interest rate accrued by borrowers every block.
The supply-rate-per-block is a function of the borrow-rate-per-block, as well the market's reserve factor and the its utilization rate. The utilization rate is the ratio of borrowed funds to supplied funds, calculated as borrows / (cash + borrows - reserves)
. The supply rate is then calculated as utilizationRate * (borrowRatePerBlock * (1 - reserveFactor))
. You can query this value as follows:
Both the borrow rate and supply rate returned from these methods will always be zero after a market is triggered.
Reserves
A portion of interest accrued from borrowers is set aside as cash, and cash from this source is known as reserves. The amount of reserves set aside is determined by the reserve factor. You can use the reserveFactorMantissa
method to return the reserve factor. See the Interest Rates section for more information on how the reserve factor is used.
The total reserves in a given market can be read with:
Reserves can be withdrawn or transferred by the protocol administrator.
Events
This sections contains the core events emitted by Cozy token contracts, and is not a comprehensive list.
Error handling
This section contains information about error codes and failure messages that may be returned when calling methods on a Cozy token contract.
Error codes
The table below contains a list of error codes that may be returned when calling methods on a Cozy token contract.
Failure codes and logged messages
The table below contains a list of error codes that may be emitted in Failure
logs when calling methods on a Cozy token contract. These codes are descriptive so no additional description is provided.
Last updated