Pods Options
app.pods.financeGithubBlogDiscord
  • Getting Started
  • Understand Options
    • What are options?
    • How do options work?
    • Pricing Options
  • The Protocol
    • Overview
    • Safety Measures
    • Ecosystem Participants
    • Use Cases
  • Options
    • Overview
    • Options Instrument
      • Variables
      • Functions
        • Mint
        • Unmint
        • Withdraw
        • Exercise
    • Smart Contracts
      • OptionFactory
      • PodPut
      • WPodPut
      • PodCall
      • WPodCall
    • Applied Use Cases
    • Understanding Returns
  • Options AMM
    • Overview
    • Options AMM
      • Variables
      • Components
      • Functions
        • Add Liquidity
        • Re-add Liquidity
        • Trade
        • Remove Liquidity
      • Pricing
      • Find The Next IV
      • Fees
      • Scenarios
        • LP Simulations
    • Smart Contracts
      • OptionAMMPool
      • OptionAMMFactory
      • OptionPoolRegistry
    • Applied Math
  • Developers
    • System Overview
    • Deployed Contracts
    • Dev Environment
  • Interfacing with Pods
    • Brand Assets
  • Code Integration Guides
    • Integrating with Pods (video)
    • How To Create Your Own Option
    • How To Create Your Own Pool
    • How To Trade (Buy/Sell)
    • How To Exercise
    • How To Remove Liquidity
  • User Guides
    • Videos
  • Security
    • Audits
  • APPENDIX
    • FAQ
    • Glossary of Terms
  • Additional Resources
  • app.pods.finance
  • Github
  • Blog
  • Discord
  • Twitter
  • Pods v0 Docs
Powered by GitBook
On this page
  • Examples
  • APR
  • Adding liquidity
  • Remove liquidity
  • ATR
  • Adding liquidity
  • Trade
  • Remove liquidity
  • ATPR
  • Adding liquidity
  • Trade
  • Adding Liquidity (2nd time)
  • Remove liquidity

Was this helpful?

  1. Options AMM

Applied Math

There could be countless combinations of events. Let's see their impact in the pool with a few examples.

PreviousOptionPoolRegistryNextSystem Overview

Last updated 3 years ago

Was this helpful?

Examples

The examples below aim to highlight a few properties that the AMM pools currently hold and their effects on the pool and liquidity providers.

For simplicity, consider the following information about the pool and the option:

input name

Description Pool

asset pair

ETH:DAI

option type

Put

exercise type

European

strike price

$400

spot price (Chainlink)

$500

expiration

31 Dec 2020

current day

21 Nov 2020

1

Please consider that the function "add" in this context stands for adding liquidity for the first time in a pool or when the pool has no imbalance, meaning, in both cases Fvi=1Fv_i=1Fvi​=1 . Check for further details.

APR

Add, Price Moves, Remove

This example explores the scenario where a user adds liquidity for the first time. The option price was updated mostly based on external factors (time passed and time to maturity changed or spot price of the underlying asset changed). There are no trades during the period the user kept liquidity in the pool, and the user removes liquidity without losing value on its initial deposit.

Example Information

  • Owner = John

Adding liquidity

1) Calculate Factors

1.2) Calculate pool's value factor

2) Updates

2.1) Update User Balance

2.2) Update Pool Factor in the moment of the user's deposit

2.3) Update total balances of the pool

2.4) Update deamortized balance of the pool for each factor

Add liquidity ✅

Remove liquidity

Assuming that there were no trades, the price moved, and the user wanted to remove liquidity.

1) Calculate new price

2) Calculate the Pool's opening factor

2.2) Calculate multipliers

2.3) Calculate withdraw amount of each token

The following will happen for a 100% withdrawal in both tokens:

Therefore, the users in this scenario will withdraw the same amount of tokens in the same proportion they provided initially.

There are further steps on the remove liquidity function, but after calculating the withdrawal amount for each token, it is possible to see that the user won't lose value if there is a price change with no trades.

Price moves without trade won't impact liquidity providers' withdrawal value amount either the proportion of assets.

ATR

Add, Trade, Remove

This example explores the scenario where a user adds liquidity for the first time, and the option price was updated. There are trades followed by immediate withdraw of funds.

Example Information

  • Owner = John

Adding liquidity

1) Calculate Factors

1.2) Calculate pool's value factor

2) Updates

2.1) Update User Balance

2.2) Update Pool Factor in the moment of the user's deposit

2.3) Update total balances of the pool

2.4) Update deamortized balance of the pool for each factor

Add liquidity ✅

Example Information

  • Trade direction = user wants to receive exact amount of token A (trade function will follow the exactAOutput setup)

  • Max price slippage 20%

  • Owner = Gui

Trade

1) Calculate Factors

1.2) Calculate total price based on transaction amount

a) Calculate pool amounts for each token:

b) Calculate product constant

c) Calculate total transaction price, in terms of token B

2) Updates

2.1) Update Total Balances

Trade ✅

Remove liquidity

Assuming that John decides to remove the total liquidity immediately after the trade, considering that there were no changes in the price after the trade and removing liquidity events.

1) Calculate new price

2) Calculate the Pool's opening factor

Since there was a trade, the factors TB(A) and TB(B) are no longer equal to DB(a) and DB(B). Therefore, the pool's opening factor will be different from 1.

2.2) Calculate multipliers

2.3) Calculate withdrawal amount of each token

The following will happen considering a withdrawal of 100% of the initial liquidity provided on both tokens:

Therefore, in this scenario, the user had an impermanent gain expressed in the amount of token B in the withdrawal.

There are further steps on the remove liquidity function, but after calculating the withdrawal amount for each token, it is possible to verify the property exposed.

Trade followed by withdrawal with no price move from the trade moment doesn't incur different value to be withdrawn but different proportions between assets, reflecting the trade.

ATPR

Add, Trade, Price moves, Removes

This example explores the scenario where a user adds liquidity for the first time, the option price was updated, there are trades in the pool, e price moved again, and the user withdraws funds.

In this scenario, we'll see that the user will withdraw a different amount of assets from what it had deposited originally. They may reflect an impermanent loss or gain, the trade position, and the latest price movements.

Example Information

  • Owner = John

Adding liquidity

1) Calculate Factors

1.2) Calculate pool's value factor

2) Updates

2.1) Update User Balance

2.2) Update Pool Factor in the moment of the user's deposit

2.3) Update total balances of the pool

2.4) Update deamortized balance of the pool for each factor

John Add liquidity ✅

Example Information

  • Trade direction = user wants to receive exact amount of token A (trade function will follow the exactAOutput setup)

  • Max price slippage 20%

  • Owner = Gui

Trade

1) Calculate Factors

1.2) Calculate total price based on transaction amount

a) Calculate pool amounts for each token:

b) Calculate product constant

c) Calculate total transaction price in terms of token B

2) Updates

2.1) Update Total Balances

Gui bought options Trade ✅

Adding Liquidity (2nd time)

Consider that a second user, Bob, wants to add liquidity to the pool at this moment.

Example Information

  • Owner = John

1) Calculate Factors

1.2) Calculate pool's value factor

2) Updates

2.1) Update User Balance

2.2) Update Pool Factor in the moment of the user's deposit

2.3) Update total balances of the pool

2.4) Update deamortized balance of the pool for each factor

Bob Add liquidity ✅

Remove liquidity

Assuming that John decides to remove liquidity and that the price after changed again.

1) Calculate new price

2) Calculate the Pool's opening factor

Since there was a trade, the factors TB(A) and TB(B) are no longer equal to DB(a) and DB(B). Therefore, the pool's opening factor will be different from 1.

2.2) Calculate multipliers

2.3) Calculate the withdrawal amount of each token

The following will happen considering a 100% withdrawal of the liquidity provided initially on both tokens:

Trade followed by price moves can cause a change in the pool's value and proportion. The impermanent loss or gain is likely in this scenario, and it is fairly distributed among liquidity providers.

In this scenario, we'll see that the user will withdraw the same amount of assets it deposited initially, even if the price changed from Pi−1P_{i-1}Pi−1​ to PiP_iPi​ .

The event adding liquidity will happen in the instant i=0i= 0i=0 with the following information:

Adu=100A_{du}=100Adu​=100 options

Bdu=205B_{du}= 205Bdu​=205 DAI

1.1) Calculate new price (PiP_iPi​)

The price will be calculated by the BS contract and will return a unit price, PiP_iPi​. Consider that the unit price calculated was 2 DAI per option for this example.

Pi=2P_i=2Pi​=2

No inventory imbalance means Fvi=1Fv_i=1Fvi​=1 :

Fvi=(TB(A)i−1⋅Pi+TB(B)i−1)(DB(A)i−1⋅Pi+DB(B)i−1)\displaystyle Fv_i= \frac{(TB(A)_{i-1}\cdot P_i+TB(B)_{i-1})}{(DB(A)_{i-1} \cdot P_i+DB(B)_{i-1})}Fvi​=(DB(A)i−1​⋅Pi​+DB(B)i−1​)(TB(A)i−1​⋅Pi​+TB(B)i−1​)​

TB(A)i−1=DB(A)i−1TB(A)_{i-1}=DB(A)_{i-1} TB(A)i−1​=DB(A)i−1​

TB(B)i−1=DB(B)i−1TB(B)_{i-1}=DB(B)_{i-1}TB(B)i−1​=DB(B)i−1​

UB(A)u=AiUB(A)_u=A_iUB(A)u​=Ai​

100=Ai100 = A_i100=Ai​

UB(B)u=BiUB(B)_u=B_iUB(B)u​=Bi​

205=Bi205=B_i205=Bi​

UB(F)u=1UB(F)_u=1UB(F)u​=1

TB(A)i=TB(A)i−1+AduTB(A)_i=TB(A)_i-_1 +A_{du}TB(A)i​=TB(A)i​−1​+Adu​

TB(A)i=0+100=100TB(A)_i=0+100 = 100TB(A)i​=0+100=100

TB(B)i=TB(B)i−1+BduTB(B)_i=TB(B)_{i-1} +B_{du}TB(B)i​=TB(B)i−1​+Bdu​

TB(B)i=0+205=205TB(B)_i=0 +205=205TB(B)i​=0+205=205

DB(A)i=DB(A)i−1+AduFvi\displaystyle DB(A)_i=DB(A)_{i-1} + \frac{A_{du}}{Fv_i}DB(A)i​=DB(A)i−1​+Fvi​Adu​​

DB(A)i=0+1001=100\displaystyle DB(A)_i=0+\frac{100}{1}=100DB(A)i​=0+1100​=100

DB(B)i=DB(B)i−1+BduFvi\displaystyle DB(B)_i=DB(B)_{i-1} +\frac{B_{du}}{Fv_i}DB(B)i​=DB(B)i−1​+Fvi​Bdu​​

DB(B)i=0+2051=205\displaystyle DB(B)_i=0+\frac{205}{1}=205DB(B)i​=0+1205​=205

The price will be calculated by the BS contract and will return a unit price, PiP_iPi​. Consider that the unit price calculated was 3 DAI per option for this example.

Pi=3P_i=3Pi​=3

Since TB(A)i=DB(A)iTB(A)_i= DB(A)_iTB(A)i​=DB(A)i​ and TB(B)i=DB(B)iTB(B)_i= DB(B)_iTB(B)i​=DB(B)i​, Fvi+1=1Fv_{i+1}=1Fvi+1​=1

Fvi=(TB(A)i−1⋅Pi+TB(B)i−1)(DB(A)i−1⋅Pi+DB(B)i−1)=1\displaystyle Fv_i= \frac{(TB(A)_{i-1}\cdot P_i+TB(B)_{i-1})}{(DB(A)_{i-1} \cdot P_i+DB(B)_{i-1})}= 1Fvi​=(DB(A)i−1​⋅Pi​+DB(B)i−1​)(TB(A)i−1​⋅Pi​+TB(B)i−1​)​=1

mAAi=min(Fvi⋅DB(A)i−1;TB(A)i−1)DB(A)i−1=1\displaystyle mAA_i= \frac{min(Fv_i\cdot DB(A)_{i-1};TB(A)_{i-1})}{DB(A)_{i-1}}=1mAAi​=DB(A)i−1​min(Fvi​⋅DB(A)i−1​;TB(A)i−1​)​=1

mAAi=1mAA_i=1mAAi​=1

mBBi=min(Fvi⋅DB(B)i−1;TB(B)i−1)DB(B)i−1=1\displaystyle mBB_i= \frac{min(Fv_i\cdot DB(B)_{i-1};TB(B)_{i-1})}{DB(B)_{i-1}} =1mBBi​=DB(B)i−1​min(Fvi​⋅DB(B)i−1​;TB(B)i−1​)​=1

mBBi=1mBB_i=1mBBi​=1

Ai=−[mAAi⋅ra⋅UB(A)uFvdu+mBAi⋅rb⋅UB(B)uFvdu]\displaystyle A_i=-[mAA_i\cdot r_a\cdot \frac{UB(A)u}{Fv_{du}}+mBA_i\cdot r_b\cdot \frac {UB(B)u}{Fv_{du}}]Ai​=−[mAAi​⋅ra​⋅Fvdu​UB(A)u​+mBAi​⋅rb​⋅Fvdu​UB(B)u​]

−UB(A)u=−Ai-UB(A)_u =-A_i−UB(A)u​=−Ai​

−UB(A)u=−100-UB(A)_u=-100−UB(A)u​=−100

Bi=−[mBBi⋅rb⋅UB(B)uFvdu+mABi⋅ra⋅UB(A)uFvdu]\displaystyle B_i=-[mBB_i\cdot r_b\cdot \frac{UB(B)u}{Fv_{du}} + mAB_i \cdot r_a\cdot \frac {UB(A)_u}{Fv_{du}}] Bi​=−[mBBi​⋅rb​⋅Fvdu​UB(B)u​+mABi​⋅ra​⋅Fvdu​UB(A)u​​]

−UB(B)u=−Bi-UB(B)_u =-B_i−UB(B)u​=−Bi​

−UB(B)u=−205-UB(B)_u=-205−UB(B)u​=−205

In this scenario, we'll see that the user will withdraw a different amount of assets from what it had deposited originally. Still, its withdrawal value is not impacted since Pi=Pi−1P_i = P_{i-1}Pi​=Pi−1​ .

The event adding liquidity will happen in the instant i=0i= 0i=0 with the following information:

Adu=100A_{du}=100Adu​=100 options

Bdu=205B_{du}= 205Bdu​=205 DAI

1.1) Calculate new price (PiP_iPi​)

The price will be calculated by the BS contract and will return a unit price, PiP_iPi​. Consider that the unit price calculated was 2 DAI per option for this example.

Pi=2P_i=2Pi​=2

No inventory imbalance means Fvi=1Fv_i=1Fvi​=1 :

Fvi=(TB(A)i−1⋅Pi+TB(B)i−1)(DB(A)i−1⋅Pi+DB(B)i−1)=1\displaystyle Fv_i= \frac{(TB(A)_{i-1}\cdot P_i+TB(B)_{i-1})}{(DB(A)_{i-1} \cdot P_i+DB(B)_{i-1})}= 1Fvi​=(DB(A)i−1​⋅Pi​+DB(B)i−1​)(TB(A)i−1​⋅Pi​+TB(B)i−1​)​=1

TB(A)i−1=DB(A)i−1TB(A)_{i-1}=DB(A)_{i-1} TB(A)i−1​=DB(A)i−1​

TB(B)i−1=DB(B)i−1TB(B)_{i-1}=DB(B)_{i-1}TB(B)i−1​=DB(B)i−1​

UB(A)u=AiUB(A)_u=A_iUB(A)u​=Ai​

100=Ai100 = A_i100=Ai​

UB(B)u=BiUB(B)_u=B_iUB(B)u​=Bi​

205=Bi205=B_i205=Bi​

UB(F)u=1UB(F)_u=1UB(F)u​=1

TB(A)i=TB(A)i−1+AduTB(A)_i=TB(A)_{i-1} +A_{du}TB(A)i​=TB(A)i−1​+Adu​

TB(A)i=0+100=100TB(A)_i=0+100 = 100TB(A)i​=0+100=100

TB(B)i=TB(B)i−1+BduTB(B)_i=TB(B)_{i-1} +B_{du}TB(B)i​=TB(B)i−1​+Bdu​

TB(B)i=0+205=205TB(B)_i=0 +205=205TB(B)i​=0+205=205

DB(A)i=DB(A)i−1+AduFvi\displaystyle DB(A)_i=DB(A)_{i-1} +\frac{A_{du}}{Fv_i}DB(A)i​=DB(A)i−1​+Fvi​Adu​​

DB(A)i=0+1001=100\displaystyle DB(A)_i=0+\frac{100}{1}=100DB(A)i​=0+1100​=100

DB(B)i=DB(B)i−1+BduFvi\displaystyle DB(B)_i=DB(B)_{i-1} +\frac{B_{du}}{Fv_i}DB(B)i​=DB(B)i−1​+Fvi​Bdu​​

DB(B)i=0+2051=205\displaystyle DB(B)_i=0+\frac{205}{1}=205DB(B)i​=0+1205​=205

The event trade will happen in the instant i=1i= 1i=1 given the following information:

Adu=−2A_{du}=-2Adu​=−2 options (negative to show that the options will leave the contract)

1.1) Calculate new price (Pi+1P_{i+1}Pi+1​)

The price will be calculated by the BS contract and will return a unit price, PiP_iPi​. Consider that the unit price calculated was 4 DAI per option for this example.

Pi=4P_i=4Pi​=4

poolAmountA=min(TB(A);TB(B)Pi)\displaystyle poolAmountA = min(TB(A);\frac{TB(B)}{P_i})poolAmountA=min(TB(A);Pi​TB(B)​)

poolAmountA=min(100;2054)\displaystyle poolAmountA=min(100;\frac{205}{4})poolAmountA=min(100;4205​)

poolAmountA=51.25poolAmount A=51.25poolAmountA=51.25

poolAmountB=min(TB(B);TB(A)⋅Pi)poolAmountB=min(TB(B);TB(A)\cdot P_i)poolAmountB=min(TB(B);TB(A)⋅Pi​)

poolAmountB=min(205;100⋅4)poolAmountB=min(205;100\cdot 4)poolAmountB=min(205;100⋅4)

poolAmountB=205poolAmountB=205poolAmountB=205

k=poolAmountA⋅poolAmountBk=poolAmountA\cdot poolAmountBk=poolAmountA⋅poolAmountB

k=51.25⋅205=10,506.25k=51.25\cdot 205= 10,506.25k=51.25⋅205=10,506.25

Bi=k(poolAmountA−tradeAmountA)−poolAmountB\displaystyle B_i=\frac{k}{(poolAmountA-tradeAmountA)}-poolAmountBBi​=(poolAmountA−tradeAmountA)k​−poolAmountB

Bi=10,506.25(51.25−2)−205\displaystyle B_i= \frac{10,506.25}{(51.25-2)}-205 Bi​=(51.25−2)10,506.25​−205

Bi=213.32−205=8.32B_i=213.32-205= 8.32Bi​=213.32−205=8.32

TB(A)i=TB(A)i−1+AduTB(A)_i=TB(A)_i-_1 +A_{du}TB(A)i​=TB(A)i​−1​+Adu​

TB(A)i=100+(−2)=98TB(A)_i=100+ (-2)=98TB(A)i​=100+(−2)=98

TB(B)i=TB(B)i−1+BduTB(B)_i=TB(B)_i-_1 +B_{du}TB(B)i​=TB(B)i​−1​+Bdu​

TB(B)i=205+8.32=213.32TB(B)_i=205+8.32= 213.32TB(B)i​=205+8.32=213.32

ra=1r_a=1ra​=1 and rb=1r_b=1rb​=1

The price will be calculated by the BS contract and will return a unit price, PiP_iPi​. Consider that the unit price calculated was 4 DAI per option, equal to the previous period.

Pi=4P_i=4Pi​=4

Fvi=(TB(A)i−1⋅Pi+TB(B)i−1)(DB(A)i−1⋅Pi+DB(B)i−1)\displaystyle Fv_i= \frac{(TB(A)_{i-1}\cdot P_i+TB(B)_{i-1})}{(DB(A)_{i-1} \cdot P_i+DB(B)_{i-1})}Fvi​=(DB(A)i−1​⋅Pi​+DB(B)i−1​)(TB(A)i−1​⋅Pi​+TB(B)i−1​)​

Fvi=(98⋅4+213.32)(100⋅4+205)Fv_i=\frac{(98 \cdot 4+213.32)}{(100\cdot 4+205)}Fvi​=(100⋅4+205)(98⋅4+213.32)​

Fvi=605.32/605=1.00052893Fv_i= 605.32/605=1.00052893Fvi​=605.32/605=1.00052893

mAAi=min(Fvi⋅DB(A)i−1;TB(A)i−1)DB(A)i−1\displaystyle mAA_i= \frac{min(Fv_i\cdot DB(A)_{i-1};TB(A)_{i-1})}{DB(A)_{i-1}}mAAi​=DB(A)i−1​min(Fvi​⋅DB(A)i−1​;TB(A)i−1​)​

mAAi=min(100.0528;0.98)=0.98mAA_i=min(100.0528;0.98)=0.98mAAi​=min(100.0528;0.98)=0.98

mBBi=min(Fvi⋅DB(B)i−1;TB(B)i−1)DB(B)i−1\displaystyle mBB_i= \frac{min(Fv_i\cdot DB(B)_{i-1};TB(B)_{i-1})}{DB(B)_{i-1}}mBBi​=DB(B)i−1​min(Fvi​⋅DB(B)i−1​;TB(B)i−1​)​

mBBi=min(205.1082;1.0405)=1.0405mBB_i=min(205.1082;1.0405)=1.0405mBBi​=min(205.1082;1.0405)=1.0405

mABi=TB(B)i−1−mBBi⋅DB(B)i−1DB(A)i−1\displaystyle mAB_i= \frac{TB(B)_{i-1}-mBB_i\cdot DB(B)_{i-1}}{DB(A)_{i-1}}mABi​=DB(A)i−1​TB(B)i−1​−mBBi​⋅DB(B)i−1​​

mABi=213.32−(1.0405⋅205)100=0.000175\displaystyle mAB_i=\frac{213.32-(1.0405\cdot 205)}{100}=0.000175mABi​=100213.32−(1.0405⋅205)​=0.000175

mBAi=TB(A)i−1−mAAi⋅DB(A)i−1DB(B)i−1\displaystyle mBA_i= \frac{TB(A)_{i-1}-mAA_i\cdot DB(A)_{i-1}}{DB(B)_{i-1}}mBAi​=DB(B)i−1​TB(A)i−1​−mAAi​⋅DB(A)i−1​​

mBAi=98−(0.98∗100)205=0\displaystyle mBA_i=\frac{98-(0.98*100)}{205}=0mBAi​=20598−(0.98∗100)​=0

Ai=−[mAAi⋅ra⋅UB(A)uFvdu+mBAi⋅rb⋅UB(B)uFvdu]\displaystyle A_i=-[mAA_i\cdot r_a\cdot \frac{UB(A)u}{Fv_{du}}+mBA_i\cdot r_b\cdot \frac {UB(B)u}{Fv_{du}}]Ai​=−[mAAi​⋅ra​⋅Fvdu​UB(A)u​+mBAi​⋅rb​⋅Fvdu​UB(B)u​]

Ai=−[0.98⋅1⋅(1001)+0]=−98A_i=-[0.98\cdot 1\cdot (\frac{100}{1})+0]=-98Ai​=−[0.98⋅1⋅(1100​)+0]=−98

Bi=−[mBBi⋅rb⋅UB(B)uFvdu+mABi⋅ra⋅UB(A)uFvdu]\displaystyle B_i=-[mBB_i\cdot r_b\cdot \frac{UB(B)u}{Fv_{du}} + mAB_i \cdot r_a\cdot \frac {UB(A)_u}{Fv_{du}}] Bi​=−[mBBi​⋅rb​⋅Fvdu​UB(B)u​+mABi​⋅ra​⋅Fvdu​UB(A)u​​]

Bi=−[1.0405⋅1⋅(2051)+0.000175⋅1⋅(1001)]=−213.32B_i=-[1.0405\cdot1\cdot (\frac{205}{1})+0.000175\cdot 1\cdot (\frac{100}{1})]=-213.32Bi​=−[1.0405⋅1⋅(1205​)+0.000175⋅1⋅(1100​)]=−213.32

The event adding liquidity will happen in the instant i=0i= 0i=0 with the following information:

Adu=100A_{du}=100Adu​=100 options

Bdu=205B_{du}= 205Bdu​=205 DAI

1.1) Calculate new price (PiP_iPi​)

The price will be calculated by the BS contract and will return a unit price, PiP_iPi​. Consider that the unit price calculated was 2 DAI per option for this example.

Pi=2P_i=2Pi​=2

No inventory imbalance means Fvi=1Fv_i=1Fvi​=1 :

Fvi=(TB(A)i−1⋅Pi+TB(B)i−1)(DB(A)i−1⋅Pi+DB(B)i−1)=1\displaystyle Fv_i= \frac{(TB(A)_{i-1}\cdot P_i+TB(B)_{i-1})}{(DB(A)_{i-1} \cdot P_i+DB(B)_{i-1})}= 1Fvi​=(DB(A)i−1​⋅Pi​+DB(B)i−1​)(TB(A)i−1​⋅Pi​+TB(B)i−1​)​=1

TB(A)i−1=DB(A)i−1TB(A)_{i-1}=DB(A)_{i-1} TB(A)i−1​=DB(A)i−1​

TB(B)i−1=DB(B)i−1TB(B)_{i-1}=DB(B)_{i-1}TB(B)i−1​=DB(B)i−1​

UB(A)u=AiUB(A)_u=A_iUB(A)u​=Ai​

100=Ai100 = A_i100=Ai​

UB(B)u=BiUB(B)_u=B_iUB(B)u​=Bi​

205=Bi205=B_i205=Bi​

UB(F)u=1UB(F)_u=1UB(F)u​=1

TB(A)i=TB(A)i−1+AduTB(A)_i=TB(A)_{i-1} +A_{du}TB(A)i​=TB(A)i−1​+Adu​

TB(A)i=0+100=100TB(A)_i=0+100 = 100TB(A)i​=0+100=100

TB(B)i=TB(B)i−1+BduTB(B)_i=TB(B)_{i-1} +B_{du}TB(B)i​=TB(B)i−1​+Bdu​

TB(B)i=0+205=205TB(B)_i=0 +205=205TB(B)i​=0+205=205

DB(A)i=DB(A)i−1+AduFvi\displaystyle DB(A)_i=DB(A)_{i-1} +\frac{A_{du}}{Fv_i}DB(A)i​=DB(A)i−1​+Fvi​Adu​​

DB(A)i=0+1001=100DB(A)_i=0+\frac{100}{1}=100DB(A)i​=0+1100​=100

DB(B)i=DB(B)i−1+BduFvi\displaystyle DB(B)_i=DB(B)_{i-1} +\frac{B_{du}}{Fv_i}DB(B)i​=DB(B)i−1​+Fvi​Bdu​​

DB(B)i=0+2051=205DB(B)_i=0+\frac{205}{1}=205DB(B)i​=0+1205​=205

The event trade will happen in the instant i=1i= 1i=1 given the following information:

Adu=−2A_du=-2Ad​u=−2 options (negative to show that the options will leave the contract)

1.1) Calculate new price (Pi+1P_{i+1}Pi+1​)

The price will be calculated by the BS contract and will return a unit price, PiP_iPi​. Consider that the unit price calculated was 4 DAI per option for this example.

Pi=4P_i=4Pi​=4

poolAmountA=min(TB(A);TB(B)Pi)\displaystyle poolAmountA = min(TB(A);\frac{TB(B)}{P_i})poolAmountA=min(TB(A);Pi​TB(B)​)

poolAmountA=min(100;2054)poolAmountA=min(100;\frac{205}{4})poolAmountA=min(100;4205​)

poolAmountA=51.25poolAmount A=51.25poolAmountA=51.25

poolAmountB=min(TB(B);TB(A)⋅Pi)poolAmountB=min(TB(B);TB(A)\cdot P_i)poolAmountB=min(TB(B);TB(A)⋅Pi​)

poolAmountB=min(205;100⋅4)poolAmountB=min(205;100\cdot 4)poolAmountB=min(205;100⋅4)

poolAmountB=205poolAmountB=205poolAmountB=205

k=poolAmountA⋅poolAmountBk=poolAmountA\cdot poolAmountBk=poolAmountA⋅poolAmountB

k=51.25⋅205=10,506.25k=51.25\cdot 205= 10,506.25k=51.25⋅205=10,506.25

Bi=k(poolAmountA−tradeAmountA)−poolAmountB\displaystyle B_i=\frac{k}{(poolAmountA-tradeAmountA)}-poolAmountBBi​=(poolAmountA−tradeAmountA)k​−poolAmountB

Bi=10,506.25(51.25−2)−205B_i= \frac{10,506.25}{(51.25-2)}-205 Bi​=(51.25−2)10,506.25​−205

Bi=213.32−205=8.32B_i=213.32-205= 8.32Bi​=213.32−205=8.32

TB(A)i=TB(A)i−1+AduTB(A)_i=TB(A)_{i-1} +A_{du}TB(A)i​=TB(A)i−1​+Adu​

TB(A)i=100+(−2)=98TB(A)_i=100+ (-2)=98TB(A)i​=100+(−2)=98

TB(B)i=TB(B)i−1+BduTB(B)_i=TB(B)_{i-1} +B_{du}TB(B)i​=TB(B)i−1​+Bdu​

TB(B)i=205+8.32=213.32TB(B)_i=205+8.32= 213.32TB(B)i​=205+8.32=213.32

The event adding liquidity will happen in the instant i=0i= 0i=0 with the following information:

Adu=50A_{du}=50Adu​=50 options

Bdu=30B_{du}= 30Bdu​=30 DAI

1.1) Calculate new price (PiP_iPi​)

The price will be calculated by the BS contract and will return a unit price, PiP_iPi​. Consider that the unit price calculated was 3 DAI per option for this example.

Pi=3P_i=3Pi​=3

There is an inventory imbalance, and FviFv_iFvi​ will be different from 1.

Fvi=(TB(A)i−1⋅Pi+TB(B)i−1)(DB(A)i−1⋅Pi+DB(B)i−1)\displaystyle Fv_i= \frac{(TB(A)_{i-1}\cdot P_i+TB(B)_{i-1})}{(DB(A)_{i-1} \cdot P_i+DB(B)_{i-1})}Fvi​=(DB(A)i−1​⋅Pi​+DB(B)i−1​)(TB(A)i−1​⋅Pi​+TB(B)i−1​)​

Fvi=(98⋅3+213.32)(100⋅3+205)=1.00459406Fv_i=\frac{(98\cdot3+213.32)}{(100\cdot 3+205)}= 1.00459406Fvi​=(100⋅3+205)(98⋅3+213.32)​=1.00459406

UB(A)u=AiUB(A)_u=A_iUB(A)u​=Ai​

50=Ai50 = A_i50=Ai​

UB(B)u=BiUB(B)_u=B_iUB(B)u​=Bi​

30=Bi30=B_i30=Bi​

UB(F)u=1.00459406UB(F)_u=1.00459406UB(F)u​=1.00459406

TB(A)i=TB(A)i−1+AduTB(A)_i=TB(A)_{i-1} +A_{du}TB(A)i​=TB(A)i−1​+Adu​

TB(A)i=98+50=148TB(A)_i=98+50 = 148TB(A)i​=98+50=148

TB(B)i=TB(B)i−1+BduTB(B)_i=TB(B)_{i-1} +B_{du}TB(B)i​=TB(B)i−1​+Bdu​

TB(B)i=213.32+30=243.32TB(B)_i=213.32+30=243.32TB(B)i​=213.32+30=243.32

DB(A)i=DB(A)i−1+AduFvi\displaystyle DB(A)_i=DB(A)_{i-1} +\frac{A_{du}}{Fv_i}DB(A)i​=DB(A)i−1​+Fvi​Adu​​

DB(A)i=100+501.00459406=149.7713DB(A)_i=100+\frac{50}{1.00459406}=149.7713DB(A)i​=100+1.0045940650​=149.7713

DB(B)i=DB(B)i−1+BduFvi\displaystyle DB(B)_i=DB(B)_{i-1} +\frac{B_{du}}{Fv_i}DB(B)i​=DB(B)i−1​+Fvi​Bdu​​

DB(B)i=205+301.00459406=234.8628DB(B)_i=205+\frac{30}{1.00459406}=234.8628DB(B)i​=205+1.0045940630​=234.8628

The price will be calculated by the BS contract and will return a unit price, PiP_iPi​. Consider that the unit price calculated was 2 DAI per option, equal to the previous period.

Pi=2P_i=2Pi​=2

Fvi=(TB(A)i−1⋅Pi+TB(B)i−1)(DB(A)i−1⋅Pi+DB(B)i−1)\displaystyle Fv_i= \frac{(TB(A)_{i-1}\cdot P_i+TB(B)_{i-1})}{(DB(A)_{i-1} \cdot P_i+DB(B)_{i-1})}Fvi​=(DB(A)i−1​⋅Pi​+DB(B)i−1​)(TB(A)i−1​⋅Pi​+TB(B)i−1​)​

Fvi=(148⋅2+243.32)(149.7713⋅2+234.8628)=1.00919639Fv_i=\frac{(148\cdot 2+243.32)}{(149.7713\cdot2+234.8628)}=1.00919639Fvi​=(149.7713⋅2+234.8628)(148⋅2+243.32)​=1.00919639

mAAi=min(Fvi⋅DB(A)i−1;TB(A)i−1)DB(A)i−1\displaystyle mAA_i= \frac{min(Fv_i\cdot DB(A)_{i-1};TB(A)_{i-1})}{DB(A)_{i-1}}mAAi​=DB(A)i−1​min(Fvi​⋅DB(A)i−1​;TB(A)i−1​)​

mAAi=min(1.00919639∗149.7713;148)/149.7713=0.98817mAA_i=min(1.00919639*149.7713;148) /149.7713=0.98817mAAi​=min(1.00919639∗149.7713;148)/149.7713=0.98817

mBBi=min(Fvi⋅DB(B)i−1;TB(B)i−1)DB(B)i−1\displaystyle mBB_i= \frac{min(Fv_i\cdot DB(B)_{i-1};TB(B)_{i-1})}{DB(B)_{i-1}}mBBi​=DB(B)i−1​min(Fvi​⋅DB(B)i−1​;TB(B)i−1​)​

mBBi=min(1.00919639∗234.8628;243.32)/234.8628=1.00919639mBB_i=min(1.00919639*234.8628;243.32) /234.8628 =1.00919639mBBi​=min(1.00919639∗234.8628;243.32)/234.8628=1.00919639

mABi=TB(B)i−1−mBBi⋅DB(B)i−1DB(A)i−1\displaystyle mAB_i= \frac{TB(B)_{i-1}-mBB_i\cdot DB(B)_{i-1}}{DB(A)_{i-1}}mABi​=DB(A)i−1​TB(B)i−1​−mBBi​⋅DB(B)i−1​​

mABi=(243.32−(1.03600911∗234.8628))/149.7713=0mAB_i=(243.32-(1.03600911*234.8628))/149.7713=0mABi​=(243.32−(1.03600911∗234.8628))/149.7713=0

mBAi=TB(A)i−1−mAAi⋅DB(A)i−1DB(B)i−1\displaystyle mBA_i= \frac{TB(A)_{i-1}-mAA_i\cdot DB(A)_{i-1}}{DB(B)_{i-1}}mBAi​=DB(B)i−1​TB(A)i−1​−mAAi​⋅DB(A)i−1​​

mBAi=(148−(0.98817∗149.7713))/234.8628=0mBA_i=(148-(0.98817*149.7713))/234.8628=0mBAi​=(148−(0.98817∗149.7713))/234.8628=0

Ai=−[mAAi⋅ra⋅UB(A)uFvdu+mBAi⋅rb⋅UB(B)uFvdu]\displaystyle A_i=-[mAA_i\cdot r_a\cdot \frac{UB(A)u}{Fv_{du}}+mBA_i\cdot r_b\cdot \frac {UB(B)u}{Fv_{du}}]Ai​=−[mAAi​⋅ra​⋅Fvdu​UB(A)u​+mBAi​⋅rb​⋅Fvdu​UB(B)u​]

Ai=−[0.98817⋅1⋅(1001)+0⋅1⋅(2051)]=−98.17A_i=-[0.98817\cdot 1\cdot(\frac{100}{1})+0\cdot 1\cdot (\frac{205}{1})]=-98.17Ai​=−[0.98817⋅1⋅(1100​)+0⋅1⋅(1205​)]=−98.17

Bi=−[mBBi⋅rb⋅UB(B)uFvdu+mABi⋅ra⋅UB(A)uFvdu]\displaystyle B_i=-[mBB_i\cdot r_b\cdot \frac{UB(B)u}{Fv_{du}} + mAB_i \cdot r_a\cdot \frac {UB(A)_u}{Fv_{du}}] Bi​=−[mBBi​⋅rb​⋅Fvdu​UB(B)u​+mABi​⋅ra​⋅Fvdu​UB(A)u​​]

Bi=−[1.03600911⋅1⋅(2051)+0⋅1⋅(1001)]=−212.38B_i=-[1.03600911\cdot 1\cdot (\frac{205}{1})+0\cdot1\cdot (\frac{100}{1})]=-212.38Bi​=−[1.03600911⋅1⋅(1205​)+0⋅1⋅(1100​)]=−212.38

FviFv_iFvi​
add liquidity