How To Trade (Buy/Sell)
There are few ways to trade options from our pool. In this tutorial, we will focus on interacting directly with the OptionAMMPool. In order to do that, you will need to perform a few steps.
    1.
    Find the OptionAMMPool address given a certain option using the OptionAMMFactory
    2.
    Get the trade details given a certain options amount you want to trade using the OptionAMMPool with the function getOptionTradeDetails
    3.
    Allow the tokensB (stable tokens) or the tokensA (option tokens) to be spent by the OptionAMMPool
    4.
    Perform the trade

1. Find OptionAMMPool

You will need for this step:
You can also check this step directly on getPool function.
Solidity
1
pragma solidity >0.6.0;
2
// 1) Import IOptionAMMFactory interface
3
import "../interfaces/IOptionAMMFactory.sol";
4
// 2) Instantiate OptionAMMFactory.
5
IOptionAMMFactory optionAMMFactory = IOptionAMMFactory("/factoryAddress*/");
6
7
// 3) Get the option address you will want to buy.
8
address optionAddress = '0xe3...";
9
10
// 4) Call the getPool function and receive the pool address in return.
11
address optionAMMPoolAddress = optionAMMFactory.getPool(optionAddress);
12
Copied!

2. Get the Trade Details

Now that you have the pool address from the previous step, you can call one of the following view functions:
Function name
Description
getOptionTradeDetailsExactAOutput
You should pass as input the exact number of options you will want to buy.
getOptionTradeDetailsExactAInput
You should pass as input the exact number of options you will want to sell.
getOptionTradeDetailsExactBOutput
You should pass as input the exact number of stable tokens (premium) you will want to receive when selling.
getOptionTradeDetailsExactBInput
You should pass as input the exact number of stable tokens (premium) you will want to pay when buying.
In return, you will receive the amount of tokensB (stable assets) or tokensA (option tokens) and newIV. newIV will be necessary later to perform the trade.
Solidity
1
pragma solidity >0.6.0;
2
// 1) Import IOptionAMMFactory interface
3
import "../interfaces/IOptionAMMPool.sol";
4
5
// 2) Instantiate optionAMMPool using the address you
6
// got from the previous step
7
IOptionAMMPool optionAMMPool = IOptionAMMPool(optionAMMPoolAddress);
8
9
10
/*
11
In this example we will perform a buyExactAOuput action.
12
13
3) Call the getOptionTradeDetailsExactAOutput with
14
the option amount you will want to buy as an input
15
16
That function returns:
17
uint256 amountBIn - The amount of tokenB in order to buy amountOfOptions
18
uint256 newIV - The new pool Implied volatility, also known as sigma
19
*/
20
21
uint256 amountOfOptions = 100000; // The option decimals can be found calling option.decimals()
22
(uint256 amountBIn, uint256 newIV, , ,) = optionAMMPool.getOptionTradeDetailsExactAOutput(amountOfOptions);
23
Copied!
TokenA in the optionAMMPool will always be the option token. TokenB will always be the stable token.

3. Allow the tokensB (stable tokens) or tokensA (option tokens) to be spent by the OptionAMMPool

If you are already familiar with Ethereum development, you can jump to step 4. We will only approve the tokenA/tokenB to be spent by the pool. If you are selling, you will need to approvetokenA, but if you are buying, you will need to approve tokenB.
Solidity
1
// 1) Get tokenB address from the pool
2
address tokenBAddress = optionAMMPool.tokenB();
3
4
// 2) Instantiate tokenB of the pool using ERC20 interface.
5
IERC20 tokenB = IERC20(tokenBAddress);
6
7
// 3) Approve optionAMMPool to spend amountToApprove
8
// tokenB in your behalf
9
10
// You can use amountBIn from the previous step with
11
// an error.
12
// For example: (amountBIn + amountBIn / 10) // 10% tolerance
13
uint256 amountToApprove = 10000;
14
15
tokenB.approve(optionAMMPoolAddress, amountToApprove);
16
Copied!

4. Perform the trade

Now, finally, you are ready to perform the trade. You can check the full function specification here.
Solidity
1
/*
2
Using the newIV from previous step, you can use it as
3
the last parameter of the tradeExactAOutput function.
4
- Function inputs:
5
uint256 exactAmountAOut - Amount of options to buy
6
uint256 maxAmountBIn - Slippage control
7
address owner - Amount of options to buy
8
uint256 sigmaInitialGuess - mewIV found in previous step
9
*/
10
// Examples below;
11
optionAMMPool.tradeExactAOutput(
12
amountOfOptions,
13
maxAmountBIn,
14
owner,
15
newIV
16
);
17
Copied!

Last modified 3mo ago