Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return options as a struct #92

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
45 changes: 40 additions & 5 deletions contracts/amm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ from contracts.option_pricing_helpers import (
convert_amount_to_option_currency_from_base,
convert_amount_to_option_currency_from_base_uint256,
)
from helpers import intToUint256, toUint256_balance, get_underlying_from_option_data
from helpers import intToUint256, toUint256_balance, get_underlying_from_option_data, check_deadline



Expand Down Expand Up @@ -88,7 +88,8 @@ func do_trade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
option_size: Int,
quote_token_address: Address,
base_token_address: Address,
lptoken_address: Address
lptoken_address: Address,
limit_total_premia: Math64x61_, // Should be total premia including fees
) -> (premia: Math64x61_) {
// options_size is always denominated in the lowest possible unit of base tokens (ETH in case of ETH/USDC), e.g. wei in case of ETH.
// Option size of 1 ETH would be 10**18 since 1 ETH = 10**18 wei.
Expand Down Expand Up @@ -196,6 +197,15 @@ func do_trade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
underlying_price=underlying_price,
);

// 10) Validate slippage
with_attr error_message("Current premia with fees is out of slippage bounds."){
if (side == TRADE_SIDE_LONG) {
assert_le(total_premia, limit_total_premia);
} else {
assert_le(limit_total_premia, total_premia);
}
}

return (premia=premia);
}

Expand All @@ -208,7 +218,8 @@ func close_position{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_chec
option_size : Int, // in base token
quote_token_address: Address,
base_token_address: Address,
lptoken_address: Address
lptoken_address: Address,
limit_total_premia: Math64x61_, // Should be total premia including fees - w.r.t. to opposite side
) -> (premia : Math64x61_) {
// All of the unlocking of capital happens inside of the burn function below.
// Volatility is not updated since closing position is considered as
Expand Down Expand Up @@ -309,6 +320,15 @@ func close_position{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_chec
);
}

// 10) Validate slippage
with_attr error_message("Current premia with fees is out of slippage bounds."){
if (opposite_side == TRADE_SIDE_LONG) {
assert_le(total_premia, limit_total_premia);
} else {
assert_le(limit_total_premia, total_premia);
}
}

return (premia=premia);
}

Expand Down Expand Up @@ -401,6 +421,8 @@ func trade_open{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_pt
// underlying_asset
quote_token_address: Address,
base_token_address: Address,
limit_total_premia: Math64x61_, // The limit price that user wants
tx_deadline: Int,
) -> (premia : Math64x61_) {
// User wants to open a position

Expand Down Expand Up @@ -435,9 +457,14 @@ func trade_open{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_pt
option_size,
quote_token_address,
base_token_address,
lptoken_address
lptoken_address,
limit_total_premia,
);
}

// Validate deadline
check_deadline(tx_deadline);

return (premia=premia);
}

Expand All @@ -452,6 +479,8 @@ func trade_close{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_p
// underlying_asset
quote_token_address: Address,
base_token_address: Address,
limit_total_premia: Math64x61_, // The limit price that user wants
tx_deadline: Int,
) -> (premia : Math64x61_) {
// User is closing a position before the option has expired
// -> side is what the user wants to close
Expand Down Expand Up @@ -489,6 +518,7 @@ func trade_close{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_p
with_attr error_message("Trading of given maturity has been stopped before expiration") {
assert_le(current_block_time, maturity - STOP_TRADING_BEFORE_MATURITY_SECONDS);
}

with_attr error_message("unable to close_position in trade_close"){
let (premia) = close_position(
option_type,
Expand All @@ -498,9 +528,14 @@ func trade_close{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_p
option_size,
quote_token_address,
base_token_address,
lptoken_address
lptoken_address,
limit_total_premia,
);
}

// Validate deadline
check_deadline(tx_deadline);

return (premia=premia);

}
Expand Down
11 changes: 11 additions & 0 deletions contracts/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ from openzeppelin.token.erc20.IERC20 import IERC20
from math64x61 import Math64x61
from lib.pow import pow10, pow5, pow2

func check_deadline{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(deadline: felt) {

let (current_block_time) = get_block_timestamp();

with_attr error_message("Transaction is too old") {
assert_le(current_block_time, deadline);
}

return ();
}



func max{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
Expand Down
4 changes: 4 additions & 0 deletions contracts/interface_amm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace IAMM {
option_size : Int,
quote_token_address: Address,
base_token_address: Address,
limit_total_premia: Math64x61_,
tx_deadline: Int,
) -> (premia : Math64x61_) {
}

Expand All @@ -25,6 +27,8 @@ namespace IAMM {
option_size : Int,
quote_token_address: Address,
base_token_address: Address,
limit_total_premia: Math64x61_,
tx_deadline: Int,
) -> (premia : Math64x61_) {
}

Expand Down
5 changes: 3 additions & 2 deletions contracts/interface_liquidity_pool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from starkware.cairo.common.uint256 import Uint256
from types import (
Address, OptionType, Math64x61_, OptionSide, Int, OptionWithPremia, Option, PoolInfo,
Address, OptionType, Math64x61_, OptionSide, Int, Option, PoolInfo, OptionWithPremia,
UserPoolInfo, Bool
)

Expand Down Expand Up @@ -71,7 +71,8 @@ namespace ILiquidityPool {


func get_all_non_expired_options_with_premia(lptoken_address: Address) -> (
array_len : felt, array : felt*
option_with_premia_len : felt,
option_with_premia : OptionWithPremia*
) {
}

Expand Down
13 changes: 7 additions & 6 deletions contracts/view.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ func save_option_to_array{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_
return save_option_to_array(lptoken_address, array_len_so_far + 1, array + Option.SIZE);
}


@view
func get_all_non_expired_options_with_premia{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
lptoken_address: Address
) -> (
array_len : felt,
array : felt*
option_with_premia_len : felt,
option_with_premia : OptionWithPremia*
) {
alloc_locals;
let (array : OptionWithPremia*) = alloc();
let array_len = save_all_non_expired_options_with_premia_to_array(lptoken_address, 0, array, 0);
let (option_with_premia : OptionWithPremia*) = alloc();
let option_with_premia_len = save_all_non_expired_options_with_premia_to_array(lptoken_address, 0, option_with_premia, 0);

return (array_len, array);
return (option_with_premia_len, option_with_premia);
}


Expand Down Expand Up @@ -93,7 +94,7 @@ func save_all_non_expired_options_with_premia_to_array{syscall_ptr: felt*, peder

return save_all_non_expired_options_with_premia_to_array(
lptoken_address,
array_len_so_far + OptionWithPremia.SIZE,
array_len_so_far + 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now the array length is not x*7, but x because of element in the array being OptionWithPremia

array + OptionWithPremia.SIZE,
option_index + 1
);
Expand Down
8 changes: 6 additions & 2 deletions tests/itest_specs/basic_round_trip/long_call.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ namespace LongCallRoundTrip {
option_side=0,
option_size=one_option_size,
quote_token_address=myusd_addr,
base_token_address=myeth_addr
base_token_address=myeth_addr,
limit_total_premia=230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);

let (volatility: Math64x61_) = ILiquidityPool.get_pool_volatility(
Expand Down Expand Up @@ -553,7 +555,9 @@ namespace LongCallRoundTrip {
option_side=0,
option_size=half,
quote_token_address=myusd_addr,
base_token_address=myeth_addr
base_token_address=myeth_addr,
limit_total_premia=-230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);

assert premia_2 = 11484227343495679; // approx 0.0049 ETH or 7.22 USD
Expand Down
8 changes: 6 additions & 2 deletions tests/itest_specs/basic_round_trip/long_put.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ namespace LongPutRoundTrip {
option_side=0,
option_size=one,
quote_token_address=myusd_addr,
base_token_address=myeth_addr
base_token_address=myeth_addr,
limit_total_premia=230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);
assert premia = 234655350073966452800; // approx 101.7655361342164 USD...
// notice difference in comparison to CALL premia... this is caused by different trade volatility
Expand Down Expand Up @@ -519,7 +521,9 @@ namespace LongPutRoundTrip {
option_side=0,
option_size=half,
quote_token_address=myusd_addr,
base_token_address=myeth_addr
base_token_address=myeth_addr,
limit_total_premia=-230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);

assert premia = 134675785494767915950; // approx 58.40631168584766 USD...
Expand Down
8 changes: 6 additions & 2 deletions tests/itest_specs/basic_round_trip/short_call.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ namespace ShortCallRoundTrip {
option_side=1,
option_size=one,
quote_token_address=myusd_addr,
base_token_address=myeth_addr
base_token_address=myeth_addr,
limit_total_premia=-230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);

assert premia = 627445539966218; // approx 0.00036 ETH which may sound like way too different from the long premia, but the trade_vol here is 91.6
Expand Down Expand Up @@ -534,7 +536,9 @@ namespace ShortCallRoundTrip {
option_side=1,
option_size=half,
quote_token_address=myusd_addr,
base_token_address=myeth_addr
base_token_address=myeth_addr,
limit_total_premia=230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);

assert premia_2 = 6665617208432872;
Expand Down
8 changes: 6 additions & 2 deletions tests/itest_specs/basic_round_trip/short_put.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ namespace ShortPutRoundTrip {
option_side=1,
option_size=one,
quote_token_address=myusd_addr,
base_token_address=myeth_addr
base_token_address=myeth_addr,
limit_total_premia=-230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);

assert premia = 231276759164374043900; // approx 100.45491895597071 USD...
Expand Down Expand Up @@ -521,7 +523,9 @@ namespace ShortPutRoundTrip {
option_side=1,
option_size=half,
quote_token_address=myusd_addr,
base_token_address=myeth_addr
base_token_address=myeth_addr,
limit_total_premia=230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);

assert premia = 124515225675871019200; // approx 53.9998712746395 USD...
Expand Down
Loading