Perpetual swap is a contract between two parties to bet on the price of an asset. One party goes long and the other goes short.
The Market Contract is connected to the 3party oracle that provides current asset price denominated in USD.
There are two main components of the system:
- The Market contract that manages the funds.
- Two token contracts (Long and Short) that represents the long and short position.
When user deposits funds, all goes into the Market. User has to choose if he wants to go long or short. The user receives the corresponding token that represents the ownership of deposited funds. The Market keeps track of the total amount of the asset for the long and short positions.
When the price changes the Market changes the amount of the asset for the long and short positions.
The Market contract is initialized with:
- Long Token address,
- Short Token address,
The Market contract has the following functions:
- deposit_long(&self, amount: U256, price_data: OracleProof)
- deposit_short(&self, amount: U256, price_data: OracleProof)
- withdraw_long(&self, amount: U256, price_data: OracleProof)
- withdraw_short(&self, amount: U256, price_data: OracleProof)
- get_long_balance(&self, addr: Address) -> U256
- get_short_balance(&self, addr: Address) -> U256
- get_total_deposit(&self) -> U256
- get_partial_deposits(&self) -> (U256, U256)
- get_collected_fee(&self) -> U256
- withdraw_fee(&self, amount: U256)
The system is defined as:
where:
-
$P$ is the current price of the asset, -
$L_{Long}$ is the total amount of the asset for the long position, -
$L_{Short}$ is the total amount of the asset for the short position, -
$T_{Long}$ is the total supply for the long token, -
$T_{Short}$ is the total supply for the short token.
In addition:
-
$D$ - liquidity deposit for the long or short position, -
$W$ - tokens amount to be exchanges for the asset. -
$L$ - the amount of the asset for the long or short position. -
$S$ - the total supply of the long or short token.
Whenever new price
If the new price is lower then the previous price, first the short position is reduced and then the long position is increased with the same amount. And vice versa for the price increase.
Then the system updates the total supply of the long or short token.
Short position is losing liquidity and it is transferred to the long position.
The formula is intended to cap the adjustment to the total available liquidity,
using the min
function.
Long position is losing liquidity and it is transferred to the short position.
When user deposits funds
When user withdraws
Given:
Then:
Given:
Then:
Given:
Then:
Given:
Then:
Given:
Then: