Skip to content

Instantly share code, notes, and snippets.

@cgewecke
Created October 21, 2021 13:42
Show Gist options
  • Save cgewecke/d9216d5e41a10bb7a0455ac352b1e4dc to your computer and use it in GitHub Desktop.
Save cgewecke/d9216d5e41a10bb7a0455ac352b1e4dc to your computer and use it in GitHub Desktop.

Simple PERP Simulations

Code for Leverage / Oracle Scenarios:

Chainlink Price Oracle Impact

Scenario: Chainlink oracle price effects in isolation

  • Taker deposits 1 USD into protocol
  • Initial BaseToken Chainlink Price = 10 USD / 1 BaseToken
  • Final BaseToken Chainlink Price = 20USD / 1 BaseToken

chainlink impact

Levering Up

Scenario: 9X leverage from 1 USD

This case represents opening a position for 9 USDC worth of BaseToken with a vault deposit of 1 USDC. (9X seems to be an upper bound, wasn’t able to open for 9.1) ** **

lever 9x

Scenario: Adding to positions with leverage

This case represents opening a series of 1 USDC positions given an initial 1 USDC vault deposit. It highlights the way the 1% fee is applied to account values as positions are opened.

add position lev

Scenario: Closing position at a profit with 2X leverage

  • Deposit: 1 USDC
  • Open position for 2 USDC
  • vAMM Price moves up ~20% (10 -> 12.08)

Nets a 36.7% gain using 2X leverage on a 20.07% increase in the vAMM market price. ** **

close profix 2X

AMM Virtual Markets and Settlement

Scenario 1: Taker long 100 USDC, AMM market price rises 10%, taker closes position

This case shows how positions exit net of fees, as well as the way in which many account values remain static even as other traders open positions that move the AMM price.

taker long

Scenario 2: Taker long 100 USDC, Oracle price rises 100%, AMM market price rises 10%

This case highlights the way in which many account values are tied to oracle price but the realizable value of a position depends on the AMM market price. The outcome here despite a large spot price increase is the same as in the above where spot price is static.

Note: this example excludes funding flows which capture the difference between AMM and Oracle prices over time.

oracle 100

Scenario 3: Taker short 10 BaseToken (100 USDC), AMM price drops 13%

This case highlights the way in which short positions in the money are behaviorally symmetric to longs.

amm drop 13

Scenario 4: Reducing positions

  • Taker long 100 USDC
  • AMM market price rises 10%
  • Taker reduces position by half

This scenario is the same as scenario 1 but models account state after a partial reduction of a long position.

amm up 10

Liquidations / Closing levered positions

Scenario 1: Taker long 3 USDC, leveraged 3X, Oracle price drops from $10 to $7.15

In this case, AMM and Oracle prices have dislocated. Position is above the liquidation threshold if oracle price is $7.20. Moving the AMM market to similarly low price levels does not expose account to liquidation.

liquid 1

Scenario 2: Close leveraged position at loss. Long 3 USDC, (3X), AMM price to $7.12.

In this case, Oracle and AMM market prices are dislocated, AMM price is below the liquidation threshold but can’t be closed by 3rd party. Outcome is interesting to compare with liquidation (above) since exiting position at these price levels incurs substantially greater loss.

liquid 2

Funding

Gist of scenario case code

Funding payments between makers and takers are based on the difference between the AMM market price and the oracle price and accrue over time following the formula below.

positionSize * (AMM market price - Oracle price) *  ( time since position opened / day )

When taker opens a long position and the oracle price is below AMM market, taker pays funding fees. When oracle price is above AMM market price, taker receives funding fees.

Scenario 0: Baseline: Taker opens long 100 USD for .648 Base and immediately closes.

This case provides a comparison basis for how funding fees net out in the subsequent scenarios.

funding 0

Scenario 1: Paying funding fees as a taker

  • long 100 USD
  • AMM price $3 > Oracle price for 1 day
  • Close position at a loss with funding debited
  • No other traders (only liquidity providers)

funding 1

Scenario 2: Collecting funding fees as a taker

  • long 100 USD
  • Oracle price $3 > AMM price for 1 day
  • close position with funding credited

funding 2

Scenario 3: Collecting funding fees as a leveraged taker

  • long 200 USD (2X)
  • Oracle price moves above AMM price for 1 day
  • close position with funding credited

Baseline value after closing this position (without funding) is 96020049

funding 3

Scenario 4: Collecting funding fees as a taker when AMM prices drops below Oracle

  • long 100 USD
  • other participant short positions move AMM price $3 < Oracle price for 1 day
  • close position at a loss with funding credited

The baseline outcome for closing this position at a loss without funding payments is 94544705

funding 4

Scenario 5: Effect of time on funding settlement

  • long 100 USD
  • Oracle price moves above AMM price for 1 day
  • Oracle price moves back in line with AMM
  • close position

In slack, Brian noted that funding settlement for accounts only occurs when positions are opened / closed.

“...so could be in your favor for the whole time then flips for one day, you exit, and you end up paying”

(This is accurate, compare with Funding scenario #2)

funding 5

Scenario 6: Funding payments when position is reduced (rather than closed).

The complete amount of pending payments is settled to owedRealizedPNL when any increase or reduction in the position occurs.

funding 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment