bigzPubkey
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Drift Accounting: Pools & Settlement Within Drift Protocol, all token deposits are held in a global collateral vault. This is required for seamless cross-margin and borrow-lend. The only exception to this is the insurance fund vault residing outside. Ensuring proper accounting across users requires a robust settlement mechanism. The protocol uses intermediate Pool Balances to facilitate transfers and ensure that claimed gains are required to come from settled offsetting losses. ![https://archbee.imgix.net/ps_9Ff-LBbQB7IaXI3f3F/7o9CDj7ho4pmKHiTI8_aD_img9824.png](https://archbee.imgix.net/ps_9Ff-LBbQB7IaXI3f3F/7o9CDj7ho4pmKHiTI8_aD_img9824.png) **Perpetual Market** An individual perpetual market has two pools: A. P&L Pool: to accumulate funds from users with losses for settlement to users with profits B. Fee Pool: to accumulate a fraction of exchange fees for the Quote Spot Market's Revenue Pool The P&L Pool receives the highest priority on claimed funds, in order to give user's the best possible experience. The default fraction of exchange fees for the Fee Pool is total_exchange_fee / 2 and this fraction is determined by: SHARE_OF_FEES_ALLOCATED_TO_CLEARING_HOUSE. The Fee Pool will only get partially filled up by up to 1% of intermediate P&L settled from a user's losses and aggressively drawn down for the benefit of the P&L Pool otherwise. **Spot Market** An individual spot market has two pools: A. Revenue Pool: to accumulate revenue across the protocol, denominated in this token B. Fee Pool: to pay fillers and settle portions to the revenue pool The Revenue Pool can collect fees from: - Borrow interest - Liquidations - Perpetual Markets and can pay out to - Insurance Fund Stakers - Perpetual Markets (see details of these rules in Revenue Pool) The Fee Pool collects exchange fees from swaps and uses them to pay out the Keeper Network Keepers & Decentralised Orderbook **Future Work** Currently, a Perpetuals Market can only pull from the Spot Market Revenue Pool and Insurance Fund for its quote currency. - In the future, it may be possible for a distressed associative perp market (BTC-PERP) to be able to pull funds from the associated spot market (BTC) revenue/insurance pool and immediately swap for USDC to top off its fee/P&L pool. ## pnl pool mechanics Any account can call the settlePNL instruction, which will trigger negative P&L accounts to be settled, adding to each per-market P&L pool. Negative P&L being settled increases the amount available to be settled, whilst positive P&L being settled decreases the amount available for settlement. ![https://archbee.imgix.net/ps_9Ff-LBbQB7IaXI3f3F/8bAYsBqUpelsCx7xgTblP_drift-pandl-settlement-mechanism-banner-v2-1.png](https://archbee.imgix.net/ps_9Ff-LBbQB7IaXI3f3F/8bAYsBqUpelsCx7xgTblP_drift-pandl-settlement-mechanism-banner-v2-1.png) ***Note***: calling settlePNL does not affect open positions. The function only settles the funds available in the PNL Pool for withdrawal. It's important to recognise the difference between **settling P&L** and **realising P&L** (read more here: P&L. **Calling settlePNL** Any account can call settlePNL instruction. Once called, all unrealised negative P&L will be settled and added to the market's P&L Pool to be made available for withdrawal. Users with open positions that have negative unrealised P&L will have their unrealised P&L settled and sent to the P&L Pool; **however, their position will be unaffected**. As users are settled against, the Cost Basis for their position will be adjusted so that their position remains unchanged even though a portion of their unrealised negative P&L has been realised and sent to the P&L Pool. The P&L settled as a result of the settlePNL instruction will be reflected in theUnrealised P&L tab, specifically within theRealised P&L column. The adjusted cost basis for the position is reflected in the Cost Basis column. ![https://archbee.imgix.net/ps_9Ff-LBbQB7IaXI3f3F/L1J0T17SoiEsYYH8FO8MJ_image.png](https://archbee.imgix.net/ps_9Ff-LBbQB7IaXI3f3F/L1J0T17SoiEsYYH8FO8MJ_image.png) ## fee pool mechanics Both Perpetual and Spot Markets have Fee Pools. Fee pools are set up to accumulate a fraction of exchange fees to eventually be sent to the associated Revenue Pool. Perpetual Market Fee Pool can be utilized for bankruptcy resolution after its insurance fund threshold is reached. ## revenue pool mechanics A portion of the fees collected within the protocol (denominated in a particular token: USDC, SOL, etc) go into that spot market's revenue pool. The revenue pool increases from various portions of the protocol: 1. borrow fees 2. spot market exchange fees 3. perpetual market exchange fees 4. liquidations and ultimately goes to fund: 1. insurance vault 2. perpetual market amm (conditionally) **Insurance Fund** Every hour (on the hour), a portion of the revenue pool can be settled to the insurance fund using the permissionless settle_revenue_to_insurance_fund instruction: If the insurance fund has users staked, each individual hourly settlement is capped to what would amount to 1000% APR - thus an astronomically large inflow into the revenue pool (relatively to user insurance staked amounts) would result in revenue that slowly reaches the insurance over a longer period of time rather than immediately - this encourages more insurance fund stakers (who require a medium horizon of insurance offering) to join during the high annualised cap inflow Insurance Fund Stakers must adhere to the cooldown period for withdrawals (see Insurance Fund Staking). **Spot Markets** Spot Markets allow for swaps between tokens and interest payments between depositors and borrowers. These token swaps and flow of interest are parameterised to allow fee collection for the revenue pool and thus ultimately insurance. Within the program, its parameterised by the following **in bold**: | Field | Description | | --- | --- | | total_if_factor | percentage of the borrow interest reserved for revenue pool | | user_if_factor | this proportion of total_if_factor is reserved for staked users (the other piece is reserved for the protocol itself) | | liquidation_if_factor | the proportion of liability transfer in liquidate_borrow that is sent to the revenue pool | Thus the following must be true: user_if_factor <= total_if_factor. For example, if the total_if_factor is 100%, depositors would receive no interest from borrows. The following instructions interact w/ the insurance fund: - resolve_borrow_bankruptcy **Perpetual Markets** Perpetual Markets are bootstrapped by the Drift AMM which depending on market-making performance conditions can add and remove funds from the revenue pool. Within the program, its parameterized by the following **in bold**: | Field | Description | | --- | --- | | max_revenue_withdraw_per_period | the amm's max revenue pool draw per period (note this doesn't include bankruptcy resolution) | | revenue_withdraw_since_last_settle | revenue pool draws on behalf of user pnl since the last settle (note this doesn't include bankruptcy resolution) | | last_revenue_withdraw_ts | the last timestamp of a revenue withdraw (track in order to reset the period) | A perpetual market's amm may draw up to max_revenue_withdraw_per_period from the revenue pool every period. Additionally, for direct draws from the insurance fund, it parameterized by the following **in bold**: | Field | Description | | --- | --- | | quote_settled_insurance | settled funds from the insurance fund since inception | | quote_max_insurance | max funds it can settle from insurance fund since inception | | unrealized_max_imbalance | max amount of pnl the net users can be owed within a market before: 1. draws from insurance are allowed 2. initial asset weights for this pnl gets discounted | Unlike spot markets, perp markets are capped by the max draw from insurance via quote_max_insurance quote_settled_insurance tracks the insurance fund draw amount since inception. Once this threshold is reached or the insurance fund is depleted, the market will then resort to the AMM Fee Pool. For any remaining losses not covered, the market will perform socialized losses in bankruptcy events. The following instructions interact w/ the insurance fund: - resolve_perp_pnl_deficit - resolve_perp_bankruptcy notes: resolve_perp_pnl_deficit can only be resolved by insurance fund deposits (within the market's constraints), not by social loss with other users ## INSURANCE FUND mechanics **Staking** **Reward** For providing liquidity to the Insurance Fund, Insurance Fund Stakers are rewarded with their proportionate share of the Revenue Pool every hour. The Revenue Pool is funded by various aspects of the protocol: 1. borrow fees; 2. spot market exchange fees; 3. perpetual market exchange fees; and 4. liquidation fees. An Insurance Fund Staker's proportionate share is calculated by: Total Staked Amount / Total Insurance Fund Each revenue settlement is split between Insurance Fund Stakers and a protocol-owned portion of the insurance fund. **Example** - *The Insurance Fund is at $5000 USDC. You decide to stake $10,000 USDC, bringing the total to $15,000 USDC.* Your proportionate share of the Revenue Pool paid every hour for stakers would be 10000/15000 = 66.6%. Each hour, as the revenue pool settles fees earned to the Insurance Fund, let's say $30. Half of this is reserved for the protocol, while the other half is designated for stakers, making the staker payout $15. You will receive 66.6% of the payout ($10) and the remaining Insurance Fund stakers would receive 33.3% ($5). **Unstaking** (Cooldown Period) There is a cooldown period of ~14 days for unstaking any collateral from the Insurance Fund. A user first requests to unstake a specific amount (denominated in shares). During the cooldown period, the user still receives rewards and is liable for user deficit resolution. After the elapsed period, the user can then unstake. Upon unstake, a net winning during the cooldown period is forgone and split amongst the current set of stakers and while a net loss is incurred by the unstaker. This means the unstake process downside only for the unstaker, and upside only for the stakers who remain. Additionally, during the cooldown period, if a user wishes to cancel the unstake request, they forgo any gains made during the unstake request period and their share is adjusted according. Those forgone gains are also split amongst the current stakers upon cancellation.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully