Buy protection programmatically
Learn how to buy protection programmatically on the Cozy protocol.
Last updated
Was this helpful?
Learn how to buy protection programmatically on the Cozy protocol.
Last updated
Was this helpful?
Cozy provides a protected borrowing marketplace for investing in decentralized finance protocols that offer protection against a loss of funds. You pay a small premium to purchase protection for funds you deposit and can leverage your investment by borrowing against it. This guide illustrates how you can write a script using TypeScript that you can then use to buy protection from a programmatically,
All code snippets are from the buy-protection.ts
script in the repository. See that repository for more context, definitions of helper methods used, etc.
Before you can buy protection by borrowing protected funds from a Cozy market, you must supply collateral to borrow against.
Therefore, the first step toward protected borrowing is to determine the address of the Cozy money market you intend to use as collateral. To see how to do that, let's assume that you want to buy protection for an ETH investment and you have 2 ETH to use as collateral.
The following code snippet illustrates how to check the trigger
property to see if it returns the zero address and how to find the address for the Cozy ETH Money Market where you will deposit your collateral:
In this example, the ethAddress
used as input to the getCToken
method is the address 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
that Cozy uses to represent ETH.
After verifying that there's a valid Cozy money market available for depositing your collateral, you are ready to supply the collateral to that money market. In this example, you are supplying 2 ETH, and ETH has 18 decimal places, so your script need to account for that. The process is slightly different for ETH than for ERC-20 tokens. The following code and comments describe how supplying collateral using ETH differs from supplying collateral when using ERC-20 tokens.
In some cases, the mint transaction can appear to be successful without the mint operation actually being successful.
Supplying assets does not automatically mean you can use them as collateral. To use the supplied assets as collateral, you must explicitly call the enterMarkets
function on the Comptroller. The following code snippet illustrates how to add your collateral to the market:
Your account is now ready to borrow protected funds. Let's say there's a Yearn protection market for yUSDC, and you want to borrow protected USDC to invest in that protection market. The steps are very similar to the steps for supplying collateral, so let's jump straight to the code:
At this point, your account is now supplying 2 ETH as collateral to borrow 500 USDC. That USDC debt will not need to be paid back if the trigger event occurs, so you can safely deposit the 500 USDC into a Yearn vault.
All markets in Cozy—money markets and protection markets—have a trigger
state variable. If the value of the trigger
property is the , the money market does not have a trigger contract associated with it. A money market that does not have a trigger contract associated with it cannot be used for protected borrowing, but can be used for ordinary borrowing. If the value of the trigger
property is anything except the zero address, the value of the trigger
property represents the address of the trigger contract associated with that market. Only assets supplied to Cozy markets that have the zero address for the trigger
property, known as money markets, can be used as collateral. Similarly, only Cozy markets with a defined trigger contract, known as protection markets, can be used for protected borrowing.
Because of how Cozy does error handling, transactions can be successful—and be displayed as successful on Etherscan and other block explorers—but without doing what you expected. For example, if a transaction returns an error code and emits a Failure
event instead of reverting, it might appear as if the transaction was successful when it has actually failed. You can read more about error codes and failures in , and see information about error handling history .
You should manually ensure the mint transaction succeeded before continuing to the next step. You can use the findLog()
helper method to simplify the verification process. For more details about using the findLog()
method, see the file in the repository.