Learn more about dYdX, what is the basic structure of a decentralized derivatives agreement?

Foreword: In the current DeFi ecosystem, lending applications are firmly in the dominant position, such as Maker and Compound, and derivatives trading applications are in the second echelon, and dYdX is one of them.

Previously, the translator has briefly introduced the concept and situation of the dYdX project.

In this article, we will delve into the basic components of the dYdX protocol.

The dYdX protocol (also known as "Solo") consists of smart contracts running on the Ethereum blockchain, which are supporting decentralized margin trading and lending operations.

Currently, trade.dydx.exchange is already using the Solo protocol.

Account based

Solo is account-based. Each asset account supported by Solo has a balance, and the balance can be positive (representing the net supply of assets) or negative (representing the net borrowing of assets). In addition, the account must maintain a certain level. The collateral will otherwise be liquidated.

example

0x6b5Bb4E60821eCE7363CaFf836Be1A4f9e3559B3 Address 0x6b5Bb4E60821eCE7363CaFf836Be1A4f9e3559B3 has the following balances in its account number 123456 :

  1. ETH: 1,000
  2. DAI: -10,000
  3. USDC: -5,000

This means that this account borrowed 10,000 DAI and 5,000 USDC. These borrowings are realized by pledgeing 1,000 ETHs. The account will receive interest on ETH and pay interest on DAI and USDC.

(Market) market

Solo provides a market for each ERC20 token asset it supports. Each market specifies a price oracle (Price Oracle) (used to determine its asset price) and an interest rate setting contract that determines the market interest rate.

Currently Solo only supports the following markets and is represented by a digital ID:

45

interest rate

The interest rates in the Solo system are dynamic and the settings for each market will be different. Each market rate is automatically set by algorithm based on the ratio of market (total borrowings) / (total supply). When the account balance is positive, it will continue to earn interest, and when the balance is negative, it will continue to pay interest.

Interest is earned/paid continuously, and rates in the agreement are expressed in annual percentages.

Wei & Par

There are two types of balances in Solo: Wei and Par.

1, Wei

Wei refers to the amount of the actual asset token held in the account. As the interest on the balance increases, the amount of Wei changes continuously. For example, if Bob deposits 10 DAI into a Solo account, its Wei balance will initially be 10, and as Bob begins to earn interest on the DAI, its balance will begin to grow per second.

Most of the time, you will most likely want to use the Wei balance to show your asset changes.

2, Par

Par is the interest adjusted amount, which is static and will not change in the agreement.

These are the balances actually stored in the protocol smart contract. The protocol uses the current market index (see below) to convert the Par value to the Wei value.

index

Each market has a global lending index and a supply index that are used to convert Par Wei values ​​(using the following formula):

Borrowing Wei balance = (borrowing Par balance)* (borrowing market index)

as well as

Supply Wei balance = (Supply Par balance)* (Supply Market Index)

After the market joins the agreement, the index starts at 1, and they increase based on the accumulated interest on the assets. For example, after joining the USDC, USDC's loan index and supply index are both 1. For example, in the next month, the borrower will receive 2% interest and the lender will receive 1% interest. After that, the supply index was 1.01, while the borrowing index was 1.02. These indices will increase as the cumulative interest increases.

example:

Alice deposited 10 DAIs into the agreement (then its Wei balance is 10 DAI) and the current DAI supply index is 2. Using the Supply Par Balance = (Supply Wei Balance) / (Supply Market Index) = 10/2 = 5, the agreement credits the 5 Par balance to Alice's account.

Later, the agreement accumulated DAI interest, and now the DAI's supply index is 3. Now, Alice takes her DAI and her DAI Par balance is still 5 (Par doesn't change over time). Now, the agreement calculates the supply Wei balance = (Supply Par balance) * (Supply Market Index) = 5 * 3 = 15, and sends 15 DAIs to Alice.

Action

All state changes to the account are made through Action. Action can modify the balance of one or more accounts. There is no so-called "borrowing" Action action in Solo. If the account balance is reduced, Action can automatically borrow funds. Solo supports the following Actions:

Deposit

Deposit funds into the account, and the funds are transferred from the sender or approved address to Solo, and the balance of the account is incremented.

Withdraw (withdrawal)

The funds are withdrawn from the account, funds are sent from Solo to the specified address, and the balance of the account is decremented.

Transfer

Transfer funds internally between two Solo accounts.

Buy

Use an asset to buy assets on a decentralized exchange. The exchange wrapper using dYdX interacts with different decentralized exchanges (DEX). The balance of the assets purchased increases and the balance of assets used for purchases decreases. Example: Use DAI to purchase 1 WETH on eth2dai.

Sell ​​(sold)

On the decentralized exchange, an asset is sold for another asset. Exchange wrappers using dYdX interact with different DEXs. As a result, the balance of assets sold decreased, while the balance of assets received increased. Example: Sell 1 WETH on eth2dai in exchange for DAI.

Trade

Asset trading with another account within the Solo system. In fact, no tokens will be moved, but the account balance will be updated. Using the AutoTrader interface, you are allowed to specify a smart contract that is called to determine the transaction price.

Call

The function specified by the ICallee interface is called through the context of the account, and the account balance is not modified. An example of how to use it is to set the expiration date of the Expiry contract.

Liquidate

Clearing accounts with insufficient mortgages. Operate on two accounts: clearing account and under-collateralized account. This operation does not transfer any tokens, but instead updates the account balance internally. Clearing at the price specified by the example:

Starting account balance:

Clearing account (L): +100 DAI Under-cold account (U): -1 ETH, +150 DAI ETH predictor (oracle) Price: $125 DAI predictor (oracle) Price: $1 Clearing spread: 5%

The clearing action causes 1 ETH to be transferred from L to U, 1 ETH * (($125/ETH) / ($1/DAI)) * 1.05 = 131.25 DAI transferred from U to L. The balance after liquidation is:

Clearing account (L): +231.25 DAI , -1 ETH Under-cold account (U): +18.75 DAI

Vaporize (vaporization)

The funds are withdrawn from the insurance fund, so that an underwater account (underwater account) with a negative balance is re-collateralized.

Operation

Every state change operation to the protocol occurs through Operation. Operation contains a series of actions, each of which is performed on an account.

In an Operation, multiple Actions can be concatenated together to achieve more complex interactions with the protocol. For example, performing a short ETH on Solo can be done with an Operation that includes the following Action:

Sell ​​ETH for DAI Deposit DAI

Importantly, the mortgage is only checked at the end of the operation, thus allowing temporary underpayment of the account within the scope of an Operation. This allows an Operation operation like Sell -> Trade, in which the asset is first sold and then the collateral is locked into the second Action in the Operation.

Amount (amount)

The amount in Solo is named after the following three items:

1, the value amount 2, reference AmountReference.Delta : refers to the amount relative to the existing balance AmountReference.Target : indicates the absolute amount of 3, denomination AmountDenomination.Wei : refers to the amount of the actual unit of the transferred token (see Wei) AmountDenomination.Par : Indicates that the amount is denominated in principal, Solo uses these types of amounts in its internal accounting, and they do not change over time (see Par)

It should be noted that the amount is always related to changes in the account balance, not to the number of Actions that occur. For example, using [pseudocode] withdraw(-10) , when you withdraw money, the account balance will be reduced.