Manage protection programmatically
Learn how to view and manage positions and accounts programmatically on the Cozy protocol.

This guide illustrates how you can view and manage your protection positions programmatically using a script written in TypeScript. The tasks in this section assume that you are a participant in a protection market and have supplied collateral to purchase protection as described in Buy protection programmatically. If you haven't already done so, you should read the Buy protection programmatically guide or review the buy-protection.ts script before continuing with this guide.
All of the code snippets in this guide are from the manage-protection.ts script in the Cozy developer guides repository. See that repository for more context, definitions of helper methods used, and related information.
Prepare to manage positions
The manage-protection.ts script includes code to set up a your environment and buy protection so that you have an account with positions to manage. Because those steps are covered in other guides, they aren't included in this guide. For managing positions, however, you need to specify how many decimals Cozy tokens have. You can add this information to the Environment Setup section of the code with a line like this:
After the Environment Setup instructions, the manage-protection.ts script includes a condensed version of the code for buying protection, which is covered in more detail in the buy-protection.ts script. You can view the condensed version of the code here.
View positions
As the last step of buying protection, you borrowed funds from a protection market. In this case, you have borrowed protected USDC from a Yearn protection market. Now, let's get an array of Cozy token (cToken) addresses for each market you've entered.
In practice, this distinction means that the getAssetsIn method only returns:
Assets that you have borrowed.
Assets that you have supplied and have used to enter markets.
If you have supplied assets but have not explicitly entered a market with them, those assets are not included in the results because there's no position to manage and the Comptroller does not need to be aware of those assets.
To determine whether you have borrowed from a market or supplied to the markets, you can loop through each asset and learn its current status. The comments in this code snippet explain what's happening at each step.
Check account liquidity
The amount of collateral you have is computed by multiplying the supplied USD balance in a market by that market's collateral factor, and summing that across all markets. Total borrow balances are subtracted from that, resulting in an account liquidity value. Users who have negative account liquidity, i.e. a shortfall, are subject to liquidation, and are prevented from withdrawing or borrowing assets.
To avoid liquidation, you must ensure that account liquidity is always greater than zero. You can check this for an account as follows:
Manage liquidity
After checking your account liquidity, you might find that you are at risk of having your account liquidated. If you find your account liquidity is too close to the minimum required—for example, you have a shortfall or only a small amount of excess liquidity—you can adjust your position by doing the following:
Supply additional collateral.
Repay debt.
Supply additional collateral
If you want to adjust your account liquidity (another way of measuring what's commonly known as your collateralization ratio) to reduce the risk of liquidation, you can supply more collateral.
To supply more collateral programmatically, follow the detailed steps in buy-protection.ts script (or reference the abbreviated version of the code for supplying collateral in the manage-protection.ts script).
Repay debt
An alternate way to reduce your chance of liquidation is to pay back some, or all, of your borrowed debt.
There are two options for repaying debts:
Repay debt you, as the transaction caller, borrowed.
Repay debt on behalf of another borrower.
repayBorrow
You can repay your own borrows using the repayBorrow() method. As the following code snippet illustrates, you first approve the contract to spend DAI, then execute the repay.
This example, repays a debt of 25 DAI for the account that called the repayBorrow method.
If you wanted to repay the full amount instead of just 25 DAI, the best way to do this is to set the repayAmount to MaxUint256. The Cozy contracts recognize this as a magic number that will repay all your token debt. If you try to repay all token debt without using MaxUint256 as the amount, you'll be left with a very tiny borrow balance, known as dust. This happens because interest accrues at the beginning of the repay transaction, and it's extremely difficult to predict how much will accrue and send the exact amount of tokens required. Using MaxUint256 tells the contracts to repay the full debt after interest accrues.
repayBorrowBehalf
Alternatively, you could have executed the same logic in the code snippet above using the repayBorrowBehalf method. For example, you could have used repayBorrowBehalf(borrower,repayAmount) to repay repayAmount on behalf of borrower.
The repayBorrowBehalf() method is particularly useful for repaying ETH debt. Because ETH is required for gas, calculating how much ETH you need to repay ETH debt can be difficult. For example, if you wanted to repay a full ETH balance, you would need to calculate the gas required for the transaction as well as the interest accrued. Instead of trying to predict the exact gas usage plus interest accrual, you can repay full ETH debts by using a special contract called the Maximillion contract. The Maximillion contract lets you send extra ETH along with your transaction. The contract repays your debt using repayBorrowBehalf(), then refunds you the excess after paying back all debt.
The following code snippet illustrates using the Maximillion contract to repay ETH debt.
Last updated
Was this helpful?