Welcome to the D5 course for Sky Protocol. Sky is a decentralized protocol for their stable coins called USDS and D. In this course, you'll learn the fundamental mechanics of a stable coin, such as how to mint USDs and die, how the price of the stable coin stays relatively close to $1 with extremely low volatility, and what you can do with USDS and die within the Sky Protocol. the prerequisites for this course, our basic knowledge of solidity and some experience using Foundry. You can learn all of these inside the courses of Siphon Updraft. Next, I'm going to explain about the course repository that you can download from GitHub. I'll highlight some resources that you can use for this course and how to do the exercises. The first resource that I will highlight is where to find the code for Sky Protocol. If you're curious about how the code for Sky protocol is written, you can check the folder called notes/code. And inside here, there are some code that are relevant to the Sky protocol. For example, clicking on die. soul. This is the code for the die token that is used by the Sky protocol. So throughout the course, if you want to know how to do the exercises or maybe you're curious how something works, then this is the place that you should check out. Now when you're looking at the code you notice that the names of the variable might confuse you. For example, here we have a str called IK and year. As another example, we have a contract called dog and we also flopper. This is a common pattern in the contracts of the sky protocol. The namings might confuse you at first. So to understand these terms, I'll highlight two resources that you can use. The first one is a markdown file called terms. md inside notes. Here you'll find the common terms that are used and their descriptions. Another resource that might help you understand the contracts of the sky protocol is a repository called Bay Stablecoin. Most of the code that you'll find here are direct translations of the Sky Protocols contract into English that you might have easier time understanding. The final resource that I will highlight are example transactions. At the bottom of the readme file of the course repo, you'll find a section called example transactions. Here you'll find transactions where either I or some other user interacted with the Sky protocol. For example, clicking on die join. join. This will take you to the Ether scan page where a user interacted with the die join contract. For example, here you can see that this much amount of D was sent from this account over to the zero address. And finally, if you want to look at the details of this transaction, you can copy this transaction hash, paste the transaction hash in the transaction visualizer. The link to this tool will be in the course repository. The visualizer will graph out which contracts were called and what functions with what parameters were called. And finally, for the course intro, I'll explain about how to do the exercises. All of the exercises can be navigated from the readme file of the course repository. Inside the course menu, there's a section called exercises which will link you to the foundry exercises. The actual exercises are found inside a folder called foundry. Inside this folder, there's another readme file which documents commands to execute the exercise or the solutions. Inside the source folder, there are two folders called exercises and solutions. Solutions will contain the answer to the exercises. So when you're doing the exercises, you'll write your code inside a folder called exercises. And to make sure that you've done the exercises correctly, there are tests. These tests are executed by executing one of these commands. USDS is a stable coin that is issued by the Sky Protocol. In this video, I'll explain how it is created. Basically, USDS is created when a user takes out a over collateralized loan from the Sky Protocol. Let me show you what this looks like. Let's say that we have a user and the Sky protocol. To get a over collateralized loan, the user will first have to lock some kind of collateral into the Sky protocol. One of the tokens that can be used as a collateral is EH. So let's say that a user locks ETH as collateral and then they'll be able to mint USDs. So why is this called the over collateralized loan? Well, it's because the amount of USDS that the user can mint must be less than the value of the collateral that is locked in the Sky protocol. Let me be more explicit and answer the question, how much USDS can the user mint if they lock a collateral? So, given that a user locks some collateral into the Sky protocol, how much USDS can they mint? The answer is the amount of USDS that they borrowed plus the interest on borrow. I'll label this as debt. The debt must be less than the discounted collateral price multiplied by the collateral amount. Let's go through an example. A user locks one ETH as collateral when one price of ETH is at 2,000 USD. Let's say that the protocol evaluates the discounted collateral
Segment 2 (05:00 - 10:00)
price to be 1,200 USD. Take the market price 2,000 USD and then apply a discount. In this case, this 2,000 USD is applied a 40% discount. So it will be 60% of the market price which is 1,200 USD. Let's draw out the discounted value of the collateral that is locked by a user. So how much USDS can this user borrow? Well, let's use this equation. The debt must be smaller than the discounted collateral price multiplied by the collateral amount. In this example, we're locking one collateral. So collateral amount will be one. And what is the discounted collateral price? Well, it is 1,200 USD. So the amount of USD that the user can borrow must be less than 1,200 USD. For example, let's say that this user borrowed 900 USDs. And as you can see, currently the debt is less than the amount of collateral that is locked in the Sky protocol. Now, since there's an interest that occurs on the debt, this debt will slowly increase over time. And also note that the price of beef is volatile, it's not always 2,000 USD. Some days it goes up by 10% and other days it goes down by 10%. So now imagine the case that this discounted collateral price goes up. We're still safe. So we can borrow more. Or in the other case if it goes down then now notice that the debt is greater than the discounted value of the collateral. In this case what happens? What happens if the debt is under collateralized? In this case, the protocol will trigger a liquidation, which means it's going to sell the collateral and then force repay the debt. Again, let's walk through an example. The price of beef drops from 2,000 USD to 1,400 USD. The discounted collateral price will be 60% of the market price. So, the protocol will evaluate the value of the collateral locked by this user at 840 USD. 60% of the market price, which is 1,400 USD. What is the value of the collateral locked by this user? Well, currently it's 840 USD. One E locked as collateral at the market price of 1,400. Apply the discount and it is equal to 840 USD. Okay. How about the debt? How much did this user borrow? Well, the user borrowed 900 USD. Now, remember there's an ongoing fee for the debt that they borrowed. This fee is called a stability fee. But for simplicity, let's assume that the fee is zero. So they currently owe 900 USDS to the Sky protocol. As you can see, the current debt is greater than the value of the collateral locked in this protocol. So now the protocol will trigger a liquidation, sell the collateral, and force repay the debt. Let's see how this goes. The first step is to sell the collateral. Let's start by calculating how much collateral that we need to sell to cover the debt. The amount of collateral to sell is calculated by taking debt and then dividing by the value of the collateral at market price. So we have market price multiply the amount of collateral that is locked. For example, the debt is 900 USDS. The market price of EH is 1,400. Amount of collateral locked is 1. So this turns out to be approximately 0. 6428. The protocol is going to sell 0. 6428 6428 E that this user locked as collateral. Okay, the next step is to repay the debt. The Sky Protocol is going to recover this bad debt by selling this much amount of the user's collateral. We worked out this number in the first step. Sell this much amount of ETH at the market price 1,400 USD per E. The protocol will receive 900 USD as back. This is the amount that the user took out as loan. And to finish off the example, let's draw out how much debt the user has and how much collateral the user has in the Sky protocol. Let's start with debt. Their debt was forcefully repaid. So now they have zero debt in the Sky protocol. How about the amount of E that is locked in the protocol. To calculate this, we can do this simple math of multiplying the amount of E remaining as collateral multiply by the discount E price. The amount of E that is remaining can be calculated by this expression. 1 is the amount of collateral that we started with minus the amount that was sold and then multiply by the discount price 840. And this turns out to be 299 USD. Let's do a recap. In this video, I explain how a user can mint USDs. To do this, they first lock up collateral and then borrow USDs as a over collateralized loan. If the amount of USDS that they owe to the system becomes greater than the amount of collateral that is locked in the system, then their collateral will be liquidated, which means that the protocol will forcefully sell your collateral and try to get USDS from other users. The final token of the Sky protocol is the Sky token. Again, I'm going to answer the three question. What is it? How to acquire? And what can you do with it? Starting with the first question. What is it? It's a governance token of Sky. It's used to cover unbacked debt which means whatever debt
Segment 3 (10:00 - 15:00)
that was created without a collateral or maybe under liquidation the collateral was not enough to back the debt. This is unbacked debt. The Sky protocol uses the governance token to recover from unbacked debt. Specifically, what they will do is sell the Sky governance token and try to collect USDS from users. The last utility of the Sky token is protocol buyback. When the Sky protocol is making profits, it's going to buy back the Sky token from AMMs and this will have a effect of price appreciation of the Sky token. Let's move on to the next question. How to acquire it? Currently, there's only one way to acquire it and this is through the MKR Sky converter. It's a contract that will allow you to swap your MKR for Sky without any fees. And finally, what can you do with it? The first thing that you can do with the Sky token is boat or you can also delegate your boat. And finally, you can lock Sky as collateral and also stake it to earn rewards and also use the Sky collateral to borrow USDs. Let me show you this in a diagram and then I'll also show you it on the app. Imagine that a user has Sky tokens. They can use this token to lock and stake. Specifically, the user will interact with a contract called log stake engine. The protocol will take the user's Sky token and then stake it into a staking contract. By doing so, this user is going to be earning Sky tokens as a reward. Since they locked the Sky token as a collateral, they can also borrow USDs. Over time, the staking reward is going to accumulate and the user can claim this as a reward. So, that is how lock and stake work. It sounds like a better deal than locking ETH and then borrowing USDS. When you lock ETH, there's no option to stake ETH into another contract to earn more ETH. So that's the difference between using Sky as a collateral and then interacting with the lock stake engine contract versus locking other collaterals such as EH into the Sky protocol. However, there's a catch for lock staking Sky tokens. There's a exit fee when you unstake and then try to withdraw your Sky collateral. Currently, the exit fee is set to zero. However, this might not be true in the future. So this is some examples of what you can do with the sky token. Now finally let's look at the app. How do you actually lock stake sky tokens? Inside the app, navigate to a tab called stake and borrow. In here what you can do is stake your sky tokens. And once you stake enough Sky tokens, you'll be able to borrow USDS. Now there's a minimum amount of USDS that you need to borrow. Let's see what that minimum is. I'll click on open a new position and you will put the amount of Sky token that you're going to lock as collateral inside here. But notice here that it says that minimum borrow amount is 30k USDS. So this means currently the price of Sky is around 5. So to borrow 30K USDs, you're going to need at least 20 times more Sky tokens than 30K. There are three types of auction in the Sky protocol. Collateral auctions, debt auctions, and surplus auctions. When you look at the code for the Sky Protocol, you'll see terms such as clipper, flopper, and flapper. When you see these terms, remember that they refer to auctions. Collateral, debt, or surplus auction. And now I will explain what each of these auctions are. Let's start with collateral auction. Collateral auctions are auctions to liquidate a collateral. When a user's debt is under collateralized, their collateral will be forcefully liquidated. This is done by triggering a collateral auction. Specifically, the collateral auction is triggered by calling a function called bark on the dog contract. When this function is called, it's eventually going to call the clipper contract. The clipper contract refers to the collateral auction. So, this is the first type of auction, the Clipper contract that deals with liquidation. The other two types of auctions are debt and surplus auctions. The purpose of a debt auction is to recover from a debt that is not backed by any collateral. For example, this debt can come into existence when the collateral that was sold couldn't fully repay the debt. This introduces a unbacked debt in the system. When this debt becomes too large, the protocol will initiate a debt auction. And this is done by selling the Sky Protocol token in exchange for USDS. And the last type of auction is the surplus auction. The surplus auction is initiated when there's a substantial profit in the system. When the system has a surplus of USDS that it collected from interests, it will initiate a surplus auction. What it's going to do is initiate a auction to buy back the sky governance token. Both the debt and the surplus auction is initiated by the vow contract. Inside the vow contract, there are two state variables called flapper and flopper. Flapper refers to the surplus auction contract and flopper refers to the debt auction contract. Both of these auctions are initiated by the vow contract and we can see this by looking at the right functions. There are two functions called flap and flop. Calling the
Segment 4 (15:00 - 20:00)
function flap will initiate the surplus auction and calling the function flop will initiate the debt auction. For the final part of this video, I want to explain a little bit more about the surplus auction. Remember a surplus auction is a auction for the protocol to buy back the sky token. Let me show you a little bit more in details how this works. So from the bow contract, let's go back to read contract and look for the address of flapper. Flapper refers to the surplus auction contract. Let's navigate here. Let's scroll down to see the functions. I'm looking for a specific state variable inside this contract. Looking for a variable called flapper. So earlier I said that a surplus auction is a auction, but the current implementation of the surplus auction is not quite a auction. Currently, what it's going to do is immediately try to buy sky tokens from a unis swap b2 pool. And we can do this by clicking on this flapper contract. This redirects you to another contract and scrolling down under code. You can see that the name of the code is called flapper uni B2 swap only. And as the name suggest, this is a flapper contract that will swap with unis swap b2s. So let's look at the transactions or maybe better let's look at token transfers and let's examine what tokens come in and what tokens go out through this contract. Here are some of the latest transactions. Let's look at the first one. So this here the from address is the address of the flapper unis swap B2 swap only contract from this contract. It's calling into the unis swap b2 sky USDS pool contract. It's sending USDS. So it's sending USDS. But how about the sky token? Where is the sky token going to? Well, let's click on the transaction and then from the transaction detail, let's scroll down. So, here is the detail of the token transfer. You can see here that from the flapper uni B2 swap only contract, it's sending USDS to the UNIS swap B2 Sky USDS pool contract. And then the last token transfer from this pool, it's transferring the Sky token to this another contract called Sky MCD pods proxy. So to summarize what we saw the flapper contract inside the bow contract when it is called it's going to redirect to another contract which is the flapper UNB2 swap only contract and this contract will try to buy back the sky token. This is done by swapping in USDS and then buying back sky governance token. So that is how currently the surplus auction works and how the sky governance token buyback works. And finally, the reason why the protocol buy backs the token is for price appreciation. When the protocol buybacks the Sky token, Sky token holders will see that their Sky tokens appreciate in price. In the previous video, we saw how a user can mint USDs. Now, you might have a question, why would they want to borrow USDs? Once a user has USDS, they can do many things. For example, they can use a USDS to provide liquidity to a AMM or they can supply it to a lending protocol. Another common usage of borrowing USDS is for leverage. Leverage basically means to buy something with borrowed money. And in this case, borrowing USDC will allow a user to profit when the price of the collateral goes up. In this video, I'll show you example of how leverage can amplify the profit when the price of the collateral goes up. Let's say that a user locks ETH as collateral and then borrows 1,000 USDS. They will use this borrow money to buy ETH. Let's say that one E is at 2,000 USD and assuming that 1 USDS is equal to 1 USD, they'll be able to buy 0. 5 E. So now there's two scenarios that can play out. One scenario is the price of beef going up and the other down. So, we'll consider the case when one ETH is at 4,000 USD and 1,000 USD. Let's start with the case when the price of E goes up. The user can make a profit by selling this ETH and then repaying this loan. The way it works is they will first swap their ETH back into USDS. Currently, they have 0. 5 E at 4,000 USD per E. So, the amount of USDS that they will get back is 2,000 USDs. The next step is to repay this loan. Let's say that the borrowing fee is zero. So they will pay 1,000 USDs. Borrow 1,000 USDs, repay 1,000 USDS. And finally, let's calculate the profit. After selling the EE, the user had 2,000 USDs, and they had to pay 1,000 USDs. So the profit is the remaining USDS, which is 2,000 minus 1,000, which is equal to 1,000 USDS. So this is the case when the user borrowed USDs and the price of beef went up. Let's now consider the case when the price of beef goes down. down to 1,000 USD. The value of beef that this user holds is 0. 5 beef at 1,000 USD, which is 500 USD. Let's say that the user decides to stop their loss. So they sell the ETH and they get
Segment 5 (20:00 - 25:00)
back 500 USDs. The next step is to repay the loan. The month of loan that they owe is 1,000 USDS. And now the loss will be after the swap they had 500 USDS. But to repay the loan, they have to pay 1,000 USDs. Somewhere from their other wallet, they'll have to pay the additional 500 USDS to repay the full 1,000 USDS loan. This is a loss of 500 USDs. So this is an example of how a user can make profit or make a loss using leverage. to understand how stable coins are used by different types of users. Here I'm going to keep it simple and classify them into two types. The first type of user in a stable coin system is the borrowers. These are users who took out the stable coin loan. And the other type of user is the holders. This is everyone else. Any user who has USDS but did not take out a over collateralized loan to mint USDs. The difference is borrowers lock their collateral and they'll have to repay interest on the USDS that they borrowed. And for the rest of the users, they didn't lock any collaterals and they don't have to repay any interest on the USDS that they hold. We saw how borrows can lock their collateral and mint USDs. But at some point, the USDS that they hold will be transferred over to other users. And here's an example of how USDS transfers over from borrowers to holders. So let's say that on the left we have a user that borrowed USDS. They locked collateral and minted USDS from the Sky protocol just holding on to USDS. It acrews interest. So they better put it to good use and try to make some profit. For example, this user might try to do a leverage use the USDS and then buy ETH. So now the USDS will transfer over from this user over to the DEX. There might be other users, the holders who want to sell their ETH. Again, the holders will interact with the DEX to swap their ETH for USDS. So, this is an example of how USDS can transfer ownership from the users who took out the USDS over collateralized loan to other users who do not have to pay any interest for the loan. The price of USDS has remained stable around $1 since 2025. So, how does USDS remain relatively close to $1? What is the mechanics used by Sky Protocol to keep the price stable? There's two components that keep the price of USDS stable at $1. These are interest rates and PSM. There's two types of interest rates. Interest rates that the borrowers pay to the protocol and the interest rates that are paid to the USDS holders who deposit their USDS into the Sky protocol. The first one, the interest rate on borrowers are called stability fees. And the interest rates that go to users who deposit USDS into the Sky protocol are called savings rate. So these are the two types of interest rates. The other component is called PSM standing for PEG stability module. In this video, I'll explain how interest rates help keep the price of USDS around $1. And in the next video, I'll explain the mechanics of PEG stability module. Starting with interest rates, we'll consider the two cases when the price of USDS is less than $1 and above $1. How does a Sky Protocol tweak interest rates so that the price of USDS converges to $1. So let's start with the first case when the price of USDS is less than $1. What does the protocol do? Actually, let's take a step back and ask the question. If a price of some stable coin is less than $1, then what needs to happen for that stable coin to be at $1 for the price to increase? How about buying more? There will be more users buying the token and less users selling the token. More users buying, so this will increase the price, and less users are selling, so this will help reduce the price falling down. Okay, how would the protocol incentivize this? Buy more and sell less. So, here's what the protocol is going to do. It's going to increase the stability fee and increase the savings rate. Let's see how each of them play out and help the price of USDS increase to $1. Starting with increase stability fee. What happens when the stability fee is increased? Remember, stability fee is the interest for borrowing USDs. So, what happens when that interest goes up? Well, this means that the cost to borrow USDs becomes higher. it becomes more expensive. When USDS becomes more expensive to borrow, two things happen. New users are less likely to borrow. It becomes expensive to borrow and current borrowers will more likely want to repay their loan because otherwise the fee that they'll have to pay rapidly increases. Less borrowers means that there's going to be less USDs in the market. the USDS will be sold less and more repays means that these borrowers that who borrowed USDS
Segment 6 (25:00 - 30:00)
will now want to repay their loan. So they'll have to buy back the USDS if they sell it for some other asset. Okay, moving on. Let's see how increasing the savings rate will have the same effect. When you increase the savings rate, this means that users that deposited their USDS into the Sky protocol are going to be earning higher yield on the USDS that they deposited. As an example, think about a bank. You put some money into the bank and if it's going to give you a higher yield, then this will likely attract more money coming into the bank. Higher yield means that there's going to be more demand for USDs. It also means USDS holders are less likely to sell their USDS. For example, if the savings rate is 10% and the market average, let's say, is 5%, then users who don't have USDS and might want to swap to USDS and deposit to earn that 10%. So this is how increasing the stability fee and increasing the savings rate will have the effect of increasing the demand. So now the next case is what happens when the price of USDS is above $1. What should the protocol do? Well, let's think about it. When the price of some asset is above $1, how can we bring the price down to $1? Well, the answer is you should incentivize users to sell more and buy less. This would put a selling pressure for the price to come down. How does the Sky protocol incentivize users to sell more USDS and buy less? Well, it turns out that it's the opposite of what we saw earlier. To increase the price of USDS, the protocol will increase the stability fee and the savings rate. When the USDS is above $1, it's going to do the opposite. Decrease stability fee and decrease savings rate. Let's see how both of these play out. When you decrease the stability fee, it's going to lower the cost to borrow. This incentivizes users to borrow more and repay less. For example, let's say that you borrow USDs and the interest on borrowing is 0. 00001%. In that case, you're not in a hurry to repay the loan. So this means that users are incentivized to repay less. And since the loan is cheap, you might also want to borrow more. More USDS will be created from borrowing and then it's going to be circulating in the market. So there's more chance of USDS being sold and the borrowers are not in a hurry to repay their loan. So this means that there's no incentive for them to buy USDs if they swapped for another asset. This decreases the buying demand. Okay. And finally, let's look at what happens when the savings rate is decreased. It's pretty simple. It means that it's lower yield for users who deposited USDS into Sky Protocol, which makes holding on to USDS less attractive. So, they might be incentive to swap to another token, which increases sell demand. And since holding on to USDS is not attractive, less user will want to buy it. Okay? So, these are how interest rates affect the price of USDS. When the price of USDS is less than $1, the Sky protocol increases stability fee and the savings rate. On the other side, when USDS is greater than $1, the protocol decreases stability fee and savings rate. The other big component that keeps the price of USDS relatively close to $1 is the PEG stability module. More specifically, the PEG stability module allows users to swap USDS to USDC or from USDC to USDS at an exchange rate of one for one. Currently, the swap fee is set to zero. The PEG stability module will help users arbitrage between USDS and USDC when there's an arbitrage opportunity between these two tokens. And by doing so, it will help the price of USDS be stable around $1. So in the previous video we saw how interest rates are used to influence the price of USDS. In this video I'll explain why interest rate alone is not enough for the price of USDS to be close to $1. And this also explains the need for PEG stability module. So let's start by asking the question why is the PEG stability module needed? Well let's start with a review. When the price of USDS is less than $1, the protocol increases stability fee. When the price of USDS is above $1, the protocol is going to decrease stability fee. But the stability fee has a asymmetric cap, it's bounded by zero and plus infinity. Also note that the savings rate should be less than or equal to stability fee since the yield that is paid to the savers come from the interest that are paid from the borrowers. Therefore, savings rate should be less than or equal to stability fee. Okay. So what's important here is that the stability fee is kept below by zero. So let's see why this is important. Let's start with the case when stability fee keeps increasing to positive infinity. The protocol has no cap on the stability fee. So it can keep on increasing while the price of USDS is less than $1. Okay, let's look at the
Segment 7 (30:00 - 35:00)
other side. When does the protocol decrease the stability fee? Well, it's going to decrease the stability fee when the price of USDS is above $1. But note that this stability fee is kept at zero. And let's say that in an extreme case, the protocol sets the stability fee to zero. Now, let's imagine the case that stability fee is equal to zero for a while, but the price of USDS remains above $1. Then, what can the protocol do to bring the price of USDS down to $1? Well, since the protocol already set the stability to zero, it can no longer use this incentive to bring the price of USDS down to $1. So now you can see that it needs another way of bringing the price of USDS down to $1. And this is where the PSM comes in. PSM can help bring the price of USDS when the price of it is above $1. Let's see how the PSM can bring the price of USDS down to $1. You see that the price of USDS is above $1. The first step is for you to acquire USDC. Let's say that the price of USDC is exactly $1. The next step is to use PSM. Use PSM to swap your USDC for USDS. You acquired USDC with $1. The PSM will allow you to swap USDC for USDC at one. So if you put in one USDC, now you have one USDS. But now the market price of USDS is let's say $110. To finish the arbitrage, you would take this USDS and then sell it for USDC on a AMM. In the market, USDS is currently selling for $1. 1. Sell it on AMM and you will get 1. 1 USDC. So you started out with one USDC and ended up with 1. 1 USDC. So this is example of how you will use PSM to bring the price of USDS down to $1. There are currently four tokens in the Sky protocol. The tokens are DUSDS, MKR, and Sky. Over the next few videos, I'm going to answer three questions about each of the tokens. The questions that I'm going to answer are what is it, how to acquire it, and here I'm going to focus on how to acquire it using the Sky protocol. Sure, you can use a centralized exchange or a decentralized exchange to acquire these tokens. However, in this video, I'm going to focus on how to acquire it using the Sky protocol. And the last question that I'm going to answer is what can you do with it? And again, of course, once you have these tokens, you can use many D5 protocols. However, in this video, I'm going to focus on Sky Protocol. Once you have these tokens, what can you do with it on the Sky protocol? When I answer this question, I'm also going to focus on two types of users. the borrowers and the holders. Okay, let's start with the D token. What is it? How to acquire it? And what can you do with it? Starting with the first question. What is it? D is a stable coin before the Maker D rebranded to Skype. Originally, the Maker D protocol created a stable coin called D. In 2024, they rebranded to a protocol called Sky. The stable coin for Sky, which you'll see later, is called USDS. Okay, the next question is how to acquire it. There's several ways to acquire it. The first way is to lock collateral and then mint D. The core contract of the Sky protocol is the same contract that was used in Maker D. So you can still lock collaterals and mint D. Another way to acquire D is to use PSM. Swap between D and USDC. If you have USDC, then you can acquire D using the PSM. And the last way is to use the DUSDS converter. The DUSDS converter is like PSM except that the tokens are D and USDS. If you have USDS, you can convert it to D at one:1 and currently without any fees. So these are some ways to acquire D. The last question is what can you do with it? Again, we'll consider two types of users, borrowers and holders. Let's start with borrowers. One common use case of borrowing D is to use leverage or you might do yield arbitrage. Now I've never seen this in practice. In theory, what you can do is when the savings rate is greater than the interest rate on borrowing, then you can borrow and then put it into a savings account. So that's yield arbitrage. And one final example, peg arbitrage. Maybe the price of D is significantly different from USDC. Then you can borrow D, use the PSM, and then use AMM to make a profit. Okay. And moving on to holders. If you're simply holding on to die, what can you do with it? I can imagine two use cases. The first use case is to deposit into DSR, die savings rate. And the other use case might be peg arbitrage. If you have D and you see a price difference between stable coins, then you might use that die to do an arbitrage. So these are some use cases of D. Okay, moving on to USDS. What is it? USDS is a stable coin of the sky protocol. How do you acquire it? Well
Segment 8 (35:00 - 40:00)
there's two ways. You can lock collateral and mint USDs. You can use the old maker dow contract to lock collateral such as EH and then mint USDS or you can lock the sky token into a new contract called lock stake engine and then mint USDS. Okay. And the other way to acquire USDS is to use the D USDS converter. If you have D, swap it to USDS one to one and currently without any swap fees. And finally, what can you do with it? Okay, let's start with borrowers. Well, it's the same as D leverage and arbitrage. Okay. And then moving on, what can a holder of USDS do? The first option is to deposit their USDS into USDS savings rate. When you deposit your USDS, the contract will mint another token called SUSDS. Let me show you this on their app. So, inside their app, there's a tab called savings. Once you click on it, you can see here that the Sky savings rate is currently at 4%. What you can do is deposit your USDS and then it will mint you SUSDS. By doing so, you're going to be yielding 4% on the USDS that you deposited. Okay, going back. What else can you do with USDS? Well, similar to D, you can also do peg arbitrage. And there's other stuff that you can do with USDS that you couldn't do with D, such as stake USDS. Again, I'm going to show you this on the app. Inside the app, click on a tab called expert. And here you can see the token called STUSDS. When you deposit USDS into this contract, it's going to mint a token called STUSDS. The USDS that you deposited will be earning 7% which is higher than the USDS savings rate at 4%. Both of the contracts you deposit USDS. However, with staking, you have the potential to earn a higher yield. But there's a catch. you might not be able to withdraw your USDS when you stake into this contract. Here's the exact condition for you to be able to withdraw your USDS. The condition is the total USDS that is inside the staked USDS contract must be greater than or equal to the USDS debt that is created from Sky collateral. What does this mean? You can use the Sky token as a collateral to borrow USDs. The total amount of USDS that is borrowed using Sky as a collateral must be less than or equal to the USDS that is staked in this contract. I'll explain how staked USDS work in detail further in the course. And moving on, what else can you do with USDS? The last example I'll show you here is you can stake your USDS to earn other rewards. Let's go back to the app. Okay. And inside the app, click on rewards tab. And here you can see all of the other rewards that you can earn by staking your USDS. For example, you can earn a token called SPK or you can earn other tokens. Okay, and that's it. These are some examples of what you can do with USDS. Okay, let's move on to MKR. What is it, how to acquire, and what can you do with it? Here, I'm not going to answer the question, how to acquire, and what can you do with it? Since it is a token that is no longer going to be used, I'm going to only answer the question, what is it? MKR is a governance token of maker DA before they rebrand it to the Sky protocol. A lot of the functions of the MKR token transfer over to the new governance token called Sky which I will be explaining in the next video. There are two core contracts that we will focus in this course. The VAT contract and the lock stake engine contract. The VAT contract is a core contract of the Sky protocol. It's a contract that keeps track of all of the collaterals and the debt of users. The lock stake engine contract is a contract that you'll need to interact with if you want to use the sky governance token as collateral and also stake it into the system. Both of them will be inside the GitHub repo under the folder called notes and then under code. So feel free to take a look at them. Okay, let's go back to the back contract again. This is the core contract of the Sky protocol. You can think about this contract as being like a database that stores all the collateral locked by a user and all of the debt that the user owes to the system. Now, if you take a look at this back contract, you'll come across terms that don't make much sense. For example, here you'll see a str and here you see a str. So, for the rest of the video, I'm going to explain some of the terms that are used inside the sky protocol. And of course, it's difficult to remember all of these terms in one go. So I've created a handy note called bat terminologies. Inside here you'll find some of the terminologies that are used inside the sky protocol and what they are used for. This markdown file is again located under the folder notes. There's a markdown file called terms. md. Okay. So let's go back to the back contract again. The two most important data types to understand the back contract is and
Segment 9 (40:00 - 45:00)
refers to collateral types. So inside the strct it's going to store the total amount of normalized debt that is taken out against this type of collateral. Some other data types that are stored in the strct are the accumulated stability fee rates. So this term normalized debt and accumulated rates are data that is used to calculate the current debt for this type of collateral including the fees accumulated from the interests. And then next we have spot which will hold the current price of the collateral with some safety margins. And then line will be debt ceiling and dust will be the minimum amount of debt that a user has to take out. What's an example of collateral type? For example, we might have ETH with interest rate at let's say 1%. Or another collateral type we might have ETH again with let's say interest rate at 8%. Or we might have a different collateral. For example, let's say we have sky with interest rate say 2%. So all of these will be stored in a different ILK. Let me show you this in the app. Okay. Going to info. s sky. money. Here you'll find informations about the sky ecosystem. Specifically looking at the collaterals and clicking on core. This will show you all of the collateral types except for the collateral that is managed by block stake engine. For example, here we have ETH C with stability fee 7. 75% and we have ETH A with 8% and so on. Going back and then navigating to staking engine. Okay. And then scrolling down, this is the current parameter for Sky as collateral managed by the lock staking engine. Here you can see that the stability fee is 8. 46%. And here we see the debt ceiling at 166 million and the dust which is the minimum amount of debt that a user has to borrow at 30k USDs. All of these data are stored in this struck called and you can see this inside the mapping. The mapping ks maps from collateral type ID to this. Okay, so that's okay. And the other important data type that is stored in the back contract is the year. UR is basically a vault. Previously, it was also called CDP, collateralized debt position. In other words, this is the part that stores the information about a user's overcolateralized loan. Inc refers to the amount of collateral that is blocked by a user and art refers to the normalized debt. Again, this term normalized debt, I haven't explained it yet. I will explain this in another video when I talk about how stability fees are calculated. So for now just think about this normalized debt as being somehow related to debt. And this year is stored in a mapping called years. It's a mapping from the K ID to the owner of this year which will be the address. And finally you'll have the strruct. Okay. So that's the strruct. It stores the data for a over collateralized loan. If you deposit your USDS into the Sky savings rate, it will earn a yield of 4% per year. On the other hand, if you were to stake your USDS, then you will earn a yield of 6. 38% per year, assuming that this rate remains constant for a year. So, if you have USDS and want to earn some yield using the Sky protocol, you can either deposit into the Sky savings rate or stake your USDS into the staking contract. Since the staking contract gives you a higher yield, you might be tempted to stake your USDS into this contract. However, there's a catch for staking your USDS. When you deposit your USDS into the savings contract, you'll earn a lower yield. However, there's no restriction on withdrawal. You can withdraw your USDS and claim the yield at any time. However, when it comes to staking USDS, there is a condition that must be met when you withdraw. So, in this video, I'll explain what this condition is and what it means for staking USDS to have a higher risk than depositing your USDS into the savings contract. And finally, I'll explain where this higher yield comes from. When you stake your USDS into the STUSDS contract, you'll get another token called SDUSDS. Your USDS will be earning yield inside this contract. And when you want to withdraw your USDS, you will get back this STUSDS to the contract. When you withdraw your USDS, there's a condition that must be met. The condition is that the total sky debt generated from the lock stake engine must be less than or equal to the USDS that is inside this contract. In other words, all of the debt that was created from the lock stake engine must be fully backed by the USDS that is inside this contract. The logic for this condition can be found inside the burn function. Inside the function burn for the ST USDS contract, there's a check that checks that the total debt generated from the lock stake engine including the debt that is inside the collateral auction plus the amount of USDS that you're going to withdraw must be less than or equal to the total amount of USDS that is locked inside this contract including
Segment 10 (45:00 - 50:00)
the yield that it can claim. So in other words, the total amount of USDS including the yield minus the amount that you can claim must be greater than or equal to the total debt created from the lock stake engine. This is a condition that must be met when you unstake your USDS. Another risk with staking your USDS inside the STUSDS contract can be found inside a function called cut. Only authorized accounts will be able to call this function. This function will take in a specified amount of USDS as input and then send this USDS over to the bow contract. Effectively, this contract takes out USDS that can be claimed by this contract and then hands it over to the bow contract. If you are USDS staker, then the amount that you stake and including the yields might be snatched away when this function is called. So, we saw two risks of staking your USDS into the STUSDS contract. The first risk is liquidity. There's a restriction on when you can withdraw your USDS. And the other risk is that the governance of Sky Protocol can snatch your USDS that is locked inside this contract. The final question that I will answer in this video is where does the higher yield come from? Remember, when you deposit your USDS into the savings, you earn a 4% yield, whereas if you were to stake it, you earn a 6. 38% yield. So, where does this higher yield come from? The answer is higher yield comes from raising the debt ceiling of the lock stake engine. Let me explain. When a user stakes their USDS into the STUSDS contract, it will increase the debt ceiling of the lock stake engine. Inside the code, this function is called set line. The function set line calls into the bat contract calling the function file to adjust the debt ceiling line. This function is called when a new STUSDS is minted and also when the circulating STUSDS is burnt when the debt ceiling is increased for the lock stake engine. On the borrowing side, users who deposit their Skyas collateral into the lock stake engine will be able to borrow more USDS. More borrowing of USDS means that the protocol will receive more instability fee. A portion of that stability fee can be allocated to the ST USDS contract. Therefore, this will increase the yield for STUSDS contract. So to summarize, when a user stakes their USDS into the STUSDS contract, this will increase the debt ceiling for the debt generated by the lock stake engine which allows user to borrow more USDS locking sky as collateral. More borrow means that more stability fee will be collected by the protocol and a portion of that stability fee can be redirected to the SDUSDS contract to pay out the yield to these users. There's three other state variables inside the back contract that I want to explain and it's going to help you quickly understand how the back contract works. These state variables are gem, die, and sin. Let's start with gem. Gem refers to the amount of collateral that is locked into the Sky protocol by a user. This is identified by a mapping from the collateral type to the owner of the collateral and how much they locked in. Again, this is called gem. Next, we have the state variable called die. This will be the amount of die or the stable coin. So, you can think about this as both being die or USDS from the address the owner of the stable coin to how much they own. And finally, unbacked debt is a mapping from address the address that owns the unbacked debt to the amount they own. This state variable is called sin. So these are three state variables that will help you quickly understand what the back contract is. And finally, I'm going to explain how users can deposit collaterals into the sky protocol to update the gem state variable. And likewise, how users can deposit or withdraw stable coin into the system to update this state variable day. So let's start with gem. What's the flow for how a user will update this gem state variable? The entry point is called a gem join contract. Inside here, there are two functions called join and exit. Join will transfer the collateral from the user to this gem join contract and then update the back contract. It does this by calling a function called slip. Okay, here it is. When this function slip is called, it updates the mapping called gem. Okay, so that's the function join. Deposit a collateral into the sky protocol. To withdraw the collateral from the Sky protocol, the user will have to call a function called exit. And as you can see, it calls the function slip again on the back contract and then it transfers the amount to the user. How the mapping die is updated works similar to gem join. There's a contract called die join. There's also a contract called USDS join which handles deposit and withdrawals of USDS. But in this video, I'll focus on die join. a contract that manages depositing and withdrawing the stable coin D. Inside here again you'll find two functions join and exit. Join
Segment 11 (50:00 - 55:00)
means to join the die into the protocol. Therefore, it's going to allocate die to the user by calling the function bat domoop and then burning the die. Let's take a look at the function move. Okay, here's the function move. You can see that the state variable die is being updated. And finally, if you want to withdraw your die from the sky protocol, you will call the function exit. Again, this move on the back contract and then mint die to the user. Contracts inside the Sky protocol have their own naming for numerical units. These numerical units are called WAD, ray, and red. Here, I'll quickly explain what they are. Inside the code, when you see the term WAD, this refers to 10^ the 18. When you see the term ray, this refers to 10^ the 27. And when you see red, this refers to 10 to the 45. A easy way to remember all of these three is remember that WAD is 10^ the 18 and also ray is 10 the 27. Now to remember what rad is all you have to do is multiply wad by ray and you'll get red. 10^ the 18 * 10 27 is 10 45. Usually WAD will be used as a unit of token amounts and then ray will be used as prices and rates. When you multiply these two, you'll get a high precision internal unit which usually represents internal balance such as die and sin. There's a fee for borrowing the stable coin from the sky protocol. This both applies to ESDs and die. This fee is called a stability fee. The stability fee compounds every second and the stability fee rate may change every second. So for example, let's say that you mint D amount of USDs at time K and then you want to know what is the debt after J seconds. Let's denote RT to be the stability fee rate at time t. So what is the debt after J seconds? Let's start by asking a simple question. If you borrow D amount of USDS, then what will be the debt after 0? So at time K, what will be the debt that we have to repay? Since 0C has elapsed, the stability fee does not apply. So the amount that we'll have to pay is exactly the amount. How about after 1 second? After 1 second, the stability fee rate at time K will have to apply for exactly 1 second. So to the D amount that we borrowed, we apply the interest or the stability fee of 1 + RK. Okay, how about next second? Well, the debt is now going to apply to this much amount that we owe. So let's say that the rate from time k to k + 1 will be r k + 1. Then the debt that we'll have to repay after 2 seconds will be whatever we owed in the previous second multiply by the rate of the last second. We keep on multiplying the rates until the last second. And this is how you will calculate the debt after j seconds given that the stability fee rate applies every second and it may change at any second. Okay, you might be curious how this is actually implemented inside the code. Inside the code, you'll be surprised to see that there's no for loop that will calculate the compound interest given a debt. What you're going to see is a multiplication of two numbers called art, which represents normalized debt, which I will explain in this video and multiply by what's called a rate accumulator. Multiplying these two numbers somehow is able to calculate the debt of a user. How does this work? Let me show you how. A debt of any user can be calculated simply by multiplying two numbers. The first concept that I will introduce is called a rate accumulator. The rate accumulator is basically the multiplication of all of the rates starting from time zero up to some time t. So given some time t, it's going to multiply all of the rates starting from time z all the way up to time t. Okay. So how is this useful for calculating the debt of any user? So let's say that we have a user that borrowed some debt and after J second we calculated that this is the debt that they owe. What you're going to see is that we can rewrite this using the rate accumulator. Okay, so here's how it works. We first take this debt and then multiply by the rate from time equal to zero all the way up to time equal to k minus one. Since we multiply by some extra terms to make these two equations equal, we need to divide by the same rates. So we divide by the rates from time go to zero all the way up to k minus one. The first k terms will all cancel out and now what you're left with is exactly these terms. But the way that we express the debt using this form can be rewritten using the rate accumulator. Let's start with the top part. How do we write this using the rate accumulator? Well, all we have to do is plug this t in. And what is this t? This t is going to be k + j minus one. Okay, so here's the rate accumulator for the top part and we'll do the same for the bottom part. For the bottom part, this t will be k minus one. Okay, here it is. And now we managed to write the debt of this user with one multiplication and one division. Okay, the final part to
Segment 12 (55:00 - 60:00)
understand how multiplying two numbers can calculate the amount of debt that a user owes to the sky protocol is to actually go through an example. So let's say that a user mints the amount of USDs at time K. Over time this debt is going to compound and at every second the rate that it applies might be changing. And now let's say at some time later K + J they meant some other amount of debt. They increase their debt by delta D amount. And then over time this debt is going to grow again and at time K + N. We want to calculate the amount of total debt that this user owes to the protocol. Let's walk through an example and you'll get a feeling of how the debt is calculated in the Sky protocol. Let's start by calculating the debt at time K + J. So they borrow D amount and over time the debt that they owe increased and at K + J they borrow the additional delta D amount. To calculate the amount of debt at K plus J, we'll break this into two parts. What is the debt at exactly here before borrowing this amount? And then what is the debt after borrowing this much at time equal to K? This is the amount of debt that they took out. Okay. So what is the debt at this time? We calculated earlier that we can use the rate accumulator to calculate this debt. This debt at time K + J will be the rate accumulator at K + J minus 1 divided by the rate accumulator at K minus one. So that's the debt at K + J before minting delta D amount. How about the debt after minting delta D amount? was simply adding delta D. So this is the total debt at K plus J. Okay. And how about at K plus N? Well, at K plus J, this is the debt that they owe. And at K + N to this initial debt, we again use the rate accumulator function to calculate the debt. Apply the rate accumulator at K + N minus one. Note here that this time is K + N. So simply subject one k + n minus one and then divide by the rate accumulator at k + j minus one. We borrow that k + j. So simply minus one and calculate the rate accumulator. So this is the debt at k + n. Now this equation can be simplified. Notice that here we have rate accumulator at k + j minus one and here we're dividing by the same amount. When we simplify this equation, what we're going to get is this expression over here. initial amount divided by the rate accumulator at K minus one plus the additional amount that we borrowed divided by the rate accumulator at K + J minus one and then multiply by the current rate accumulator at time K + N minus one inside the code this is what you see multiplication of two numbers the part in parenthesis represents normalized debt which basically means whatever amount of debt that you borrowed or repaid you divide by the rate accumulator at that time. So that is what's called normalized debt. And then to this you multiply the current rate accumulator. So this is how Sky protocol calculates the debt of any user simply by multiplying two numbers. Inside the back contract there's a str called IK. Inside this it has a field called spot which the comment says it stores the price with safety margin. So this represents the price of the collateral but doesn't store the market price. It actually stores the discounted price of the collateral. In this video, I'll show you how this spot is calculated. But before we get to that, let's first see inside the back contract how this value is updated. The value spot is updated by calling the function file. This function can only be called by authorized account. When they call this and then specify the data to be updated is spot, then they can update it. Okay. So this is inside the back contract how the spot is updated. So the next question is who calls into this function and how is the actual data for spot calculated? The answer is inside a spotter contract. Inside the spotter contract there's a function called poke. It's a public function that anyone can call. The way it works is the input is the collateral type. From the collateral type it will get the price oracle specified by the collateral type and then here on this line it calculates the spot. Once this is calculated, this spot is fed into that contract by calling bat. file which we saw earlier. Okay. So going back, let's see in detail how this spot is calculated. What you can see here is how the spot is calculated. Now to make this a little bit easier to understand, I put a comment here on how it is calculated. The spot is calculated by taking the value from the price oracle and then normalizing it. You don't have to worry about this. It's just dealing with decimals and par. You can think about this as being equal to one. And what's important is that you take the price given from the price oracle and then divide by a variable called matt which is specified over here. This matt is specified over here inside a spotter contract. It also
Segment 13 (60:00 - 64:00)
defines a strate collateral type. Here you can see that it specifies the address for the price oracle and a variable called met. The comment says it is a liquidation ratio. Okay. So given these two informations, you can see that the spot is calculated by taking maybe the actual price of the collateral. Maybe it has some safety margins. So let's say this number given by the price oracle is relatively close to the current price in the market and then it divides by some number. So that is how the discounted price of the collateral is set inside the back contract. Let me give you a real world example. To show you this, I'm going to open a file called constants. soul. And inside here, you'll find all of the addresses for the Sky protocol contracts that I use in this course. So, we need to go to the contract called spot. So, I'm going to copy this and then paste it into ethercan. This is the spotter contract. Let's scroll down. And our goal here is to get the value of the liquidation ratio for some collateral type. The liquidation ratio is stored in a mapping called IKS. So here we need to put in the ID for the collateral type. But which type should we put in? Going back to our code editor, I have some example collateral types. Let's for example put in EC. Okay. And then paste it here. Okay. Here's the result. Matt is this number. Going back to my code editor. I'm going to open my terminal and then open Python and then paste the number. The liquidation ratio Matt is in units of ray. Ray is 1 to 7. So it turns out to be 1. 7. This number is going to be divided roughly by the current price of beef. beef according to Coin Gecko is roughly 1,970. So let's divide 1,970 by 1. 7. Okay. So this is the number that we get. Now going back to the code of spotter contract, when the function poke is called, it's going to call into bat and then call the function file which will set the spot price for this. Okay. Let's check the number that we calculated inside our terminal with the actual value that is stored inside the back contract. To do this, I'm going to copy the address of the back contract. Head over to Ether Scan and navigate to the back contract. Scroll down and then look for a mapping called OKS and then paste the K for EC. Click on query. Okay. And inside spot, this is the number. Let's compare this number with the one that we calculated. Okay. So, this is the number. The price is represented in ray. So let's again divide this by 1 27. This is the current number that is stored in the back contract. You can see that there's a little bit of difference. 1,347 that is stored in the back contract versus 1,158 that we just calculated. This was an example of how the price of a collateral is calculated and then stored into the back contract. There's three ways to interact with the Sky protocol to manage over collateralized loans. Furthermore, it depends if your collateral is the Sky governance token and if you want to stake or not. The way it worked before Maker Dow rebranded to Sky was either through using a proxy contract or directly interacting with the VAT contract. To interact with the proxy contract, you would deploy a DS proxy contract and then whenever you want to manage your over collateralized loan, you will call the function execute. This will execute a delegate call to another contract called DS proxy actions. This contract is like a script that will execute certain functions to manage your over collateralized loan. For example, here you can see that the function lock gem will deposit collateral into the sky protocol contract. So that's one way of interacting with the sky protocol proxy with proxy actions. Another way is to directly write a smart contract that interacts with the back contract. And if you want to take this approach, you probably still want to look at the DSS proxy actions contract to see the sequence of functions that you must call to correctly manage the loans. And the final way to manage over collateralized loans is by the lock stake engine and it only applies if your collateral is a sky token. Sure, you can use the Sky collateral token to directly interact with the back contract. However, the advantage of using the lock stake engine is that this contract will manage your overcolateralized loan and also stake your Sky token into some other contract so that you can earn Sky rewards.