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
  • 1. Calculate factors
  • 1.1 Calculate Option Unit Theoretical Price
  • 1.2 Calculate total price based on the transaction amount
  • 3. Find new IV correspondent to the new unit price
  • 4. Updates
  • 4.1 Update Total Balances

Was this helpful?

  1. Options AMM
  2. Options AMM
  3. Functions

Trade

Trade function step-by-step

PreviousRe-add LiquidityNextRemove Liquidity

Last updated 3 years ago

Was this helpful?

When, in a given instant iii, a trade is initiated and the contract will check the following parameters to proceed:

  • Trade direction (from token A to token B or the opposite).

  • The number of tokens the user wants to buy or sell ( exactAInput orexactBoutput ). The amounts considered in the trade will be called AiA_iAi​ and BiB_iBi​ , and they will be represented with the sign of the trade from the pool's point of view. If purchases options tokens, AiA_iAi​ will be negative (options tokens are leaving the pool), and BiB_iBi​will be positive (stablecoins are entering the pool).

  • The total price slippage the user allows.

  • Owner.

Once the contract gathered this information, it will start performing the following trade functions:

1. Calculate factors

1.1 Calculate Option Unit Theoretical Price

The contract will supply our BS implementation with the following data:

  • the current spot price of the underlying asset

  • time to maturity

  • strike price

  • risk-free rate

  • last sigma (IVi−1IV_{i-1}IVi−1​)

And will get in return the updated option price (Pi)P_i)Pi​) based on the current market scenario.

1.2 Calculate total price based on the transaction amount

a) Calculate pool amounts for each token:

b) Calculate product constant

c) Calculate total transaction price, in terms of B

d) Calculate new option unit price (aka target price)

The target price will be the input for the SigmaGuesser contract.

Note that on the contract level, for each of our 4 trade functions (exactAInput / exactAOutput / exactBInput / exactBOutput) the functions above are slightly different.

3. Find new IV correspondent to the new unit price

At this point, the pool's inventory changed, and it represents a new "virtual price." The IV corresponding to this new virtual price reflects market information of supply and demand for this option.

The IV found will be stored in the contracts to later feed the Weighted Average with 25% weight.

4. Updates

4.1 Update Total Balances

This step will update the pool's new total balances, considering the trade that just happened.

Trade ✅

poolAmountA=min⁡{TBA;TBBPi}\displaystyle poolAmountA = \min\left\{TB_{A};\frac{TB_{B}}{P_i}\right\}poolAmountA=min{TBA​;Pi​TBB​​}

poolAmountB=min⁡{TBB;TBA⋅Pi}\displaystyle poolAmountB = \min\left\{TB_{B};{TB_{A}}\cdot {P_i}\right\}poolAmountB=min{TBB​;TBA​⋅Pi​}

k=poolAmountA∗poolAmountBk=poolAmountA*poolAmountBk=poolAmountA∗poolAmountB

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

TargetPricei=poolAmountB−BipoolAmountA+Ai\displaystyle TargetPrice_i=\frac{poolAmountB-B_i}{poolAmountA+A_i}TargetPricei​=poolAmountA+Ai​poolAmountB−Bi​​

So the AMM will input the new target price in the Black Scholes formula and find the new correspondent IV for this price. It uses a specific numerical method applied to Black Scholes. If you want to deep dive into how it works, explore

TBAi=TBAi−1+AiTB_{A_{i}}=TB_{A_{i-1}} +A_iTBAi​​=TBAi−1​​+Ai​

TBBi=TBBi−1+BiTB_{B_{i}}=TB_{B_{i-1}} +B_iTBBi​​=TBBi−1​​+Bi​

At the contract level, AiA_{i}Ai​ are usually exactAmountIIn or exactAmountAOut and BiB_{i}Bi​ is amountBIn / amountBOut since we do not handle negatives numbers on solidity.

Note that the trade function won't update the DBDBDB nor the FvFvFv factor.

Find next IV.
inside _calculateNewABPrice function on optionAMMPool
_getPoolAmounts at OptionAMMPool.sol
newIV variable on any of the trade functions at OptionAMMPool