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

Check that option is settled before pricing pool #150

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions contracts/core_amm/liquidity_pool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ func _get_value_of_pool_position{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
return (res = value_of_rest_of_the_pool_);
}

let (current_block_time) = get_block_timestamp();
with_attr error_message("Option is not yet settled, please wait") {
assert_le_felt(current_block_time, option.maturity);
}

let (current_volatility) = get_pool_volatility_auto(lptoken_address, option.maturity, option.strike_price);

with_attr error_message("Failed getting value of position in _get_value_of_pool_position"){
Expand Down
103 changes: 103 additions & 0 deletions tests/itest_specs/deposit_liquidity.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -585,5 +585,108 @@ namespace DepositLiquidity {

return ();
}

func test_deposit_failing{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
// scenarios
// deposit to pool with no position
// deposit to pool with both long and short positon (different options)
// watchout for value of pool and that premia is correctly adjusted for fees
alloc_locals;
local lpt_call_addr;
local lpt_put_addr;
local amm_addr;
local myusd_addr;
local myeth_addr;
local admin_addr;
local expiry;
local opt_long_put_addr;
local opt_short_put_addr;
local opt_long_call_addr;
local opt_short_call_addr;

let strike_price = Math64x61.fromFelt(1500);

%{
ids.lpt_call_addr = context.lpt_call_addr
ids.lpt_put_addr = context.lpt_put_addr
ids.amm_addr = context.amm_addr
ids.myusd_addr = context.myusd_address
ids.myeth_addr = context.myeth_address
ids.admin_addr = context.admin_address
ids.expiry = context.expiry_0
ids.opt_long_put_addr = context.opt_long_put_addr_0
ids.opt_short_put_addr = context.opt_short_put_addr_0
ids.opt_long_call_addr = context.opt_long_call_addr_0
ids.opt_short_call_addr = context.opt_short_call_addr_0
%}
local tmp_address = EMPIRIC_ORACLE_ADDRESS;
%{
stop_prank_amm = start_prank(context.admin_address, context.amm_addr)
stop_warp_1 = warp(1000000000, target_contract_address=ids.amm_addr)
stop_mock_current_price = mock_call(
ids.tmp_address, "get_spot_median", [140000000000, 8, 1000000000, 0] # mock current ETH price at 1400
)
%}

// Conduct some trades
// let one_math = Math64x61.fromFelt(1);
let one_int = 1000000000000000000; // 1 * 10**18
let one_k_math = Math64x61.fromFelt(1000);

let x = IAMM.trade_open(
contract_address = amm_addr,
option_type = 0,
strike_price = strike_price,
maturity = expiry,
option_side = 0,
option_size = one_int,
quote_token_address = myusd_addr,
base_token_address = myeth_addr,
limit_total_premia=230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);
let x = IAMM.trade_open(
contract_address = amm_addr,
option_type = 1,
strike_price = strike_price,
maturity = expiry,
option_side = 0,
option_size = one_int,
quote_token_address = myusd_addr,
base_token_address = myeth_addr,
limit_total_premia=230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);
%{
stop_warp_1()
# Warp after maturity
stop_warp_2 = warp(1000000000 + 60 * 60 * 24 + 1, target_contract_address=ids.amm_addr)

# Expect fail
expect_revert(error_message = "Option is not yet settled, please wait")
%}

let one_eth = Uint256(low = 1000000000000000000, high = 0);
let one_thousand_usd = Uint256(low = 1000000000, high = 0);
IAMM.deposit_liquidity(
contract_address=amm_addr,
pooled_token_addr=myeth_addr,
quote_token_address=myusd_addr,
base_token_address=myeth_addr,
option_type=0,
amount=one_eth
);
IAMM.deposit_liquidity(
contract_address=amm_addr,
pooled_token_addr=myusd_addr,
quote_token_address=myusd_addr,
base_token_address=myeth_addr,
option_type=1,
amount=one_thousand_usd
);

return ();
}

}

105 changes: 105 additions & 0 deletions tests/itest_specs/withdraw_liquidity.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin

from interface_lptoken import ILPToken
from interface_amm import IAMM
Expand Down Expand Up @@ -449,4 +450,108 @@ namespace WithdrawLiquidity {

return ();
}

func test_withdraw_failing{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
// scenarios
// deposit to pool with no position
// deposit to pool with both long and short positon (different options)
// watchout for value of pool and that premia is correctly adjusted for fees
alloc_locals;
local lpt_call_addr;
local lpt_put_addr;
local amm_addr;
local myusd_addr;
local myeth_addr;
local admin_addr;
local expiry;
local opt_long_put_addr;
local opt_short_put_addr;
local opt_long_call_addr;
local opt_short_call_addr;

let strike_price = Math64x61.fromFelt(1500);

%{
ids.lpt_call_addr = context.lpt_call_addr
ids.lpt_put_addr = context.lpt_put_addr
ids.amm_addr = context.amm_addr
ids.myusd_addr = context.myusd_address
ids.myeth_addr = context.myeth_address
ids.admin_addr = context.admin_address
ids.expiry = context.expiry_0
ids.opt_long_put_addr = context.opt_long_put_addr_0
ids.opt_short_put_addr = context.opt_short_put_addr_0
ids.opt_long_call_addr = context.opt_long_call_addr_0
ids.opt_short_call_addr = context.opt_short_call_addr_0
%}
local tmp_address = EMPIRIC_ORACLE_ADDRESS;
%{
stop_prank_amm = start_prank(context.admin_address, context.amm_addr)
stop_warp_1 = warp(1000000000, target_contract_address=ids.amm_addr)
stop_mock_current_price = mock_call(
ids.tmp_address, "get_spot_median", [140000000000, 8, 1000000000, 0] # mock current ETH price at 1400
)
%}

// Conduct some trades
// let one_math = Math64x61.fromFelt(1);
let one_int = 1000000000000000000; // 1 * 10**18
let one_k_math = Math64x61.fromFelt(1000);

let x = IAMM.trade_open(
contract_address = amm_addr,
option_type = 0,
strike_price = strike_price,
maturity = expiry,
option_side = 0,
option_size = one_int,
quote_token_address = myusd_addr,
base_token_address = myeth_addr,
limit_total_premia=230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);
let x = IAMM.trade_open(
contract_address = amm_addr,
option_type = 1,
strike_price = strike_price,
maturity = expiry,
option_side = 0,
option_size = one_int,
quote_token_address = myusd_addr,
base_token_address = myeth_addr,
limit_total_premia=230584300921369395200000, // 100_000
tx_deadline=99999999999, // Disable deadline
);
%{
stop_warp_1()
# Warp after maturity
stop_warp_2 = warp(1000000000 + 60 * 60 * 24 + 1, target_contract_address=ids.amm_addr)

# Expect fail
expect_revert(error_message = "Option is not yet settled, please wait")
%}

let two_and_half_eth = Uint256(low = 2500000000000000000, high = 0);
IAMM.withdraw_liquidity(
contract_address=amm_addr,
pooled_token_addr=myeth_addr,
quote_token_address=myusd_addr,
base_token_address=myeth_addr,
option_type=0,
lp_token_amount=two_and_half_eth
);

let two_and_half_thousand_usd = Uint256(low = 2500000000, high = 0);
IAMM.withdraw_liquidity(
contract_address=amm_addr,
pooled_token_addr=myusd_addr,
quote_token_address=myusd_addr,
base_token_address=myeth_addr,
option_type=1,
lp_token_amount=two_and_half_thousand_usd
);

return ();
}

}
14 changes: 14 additions & 0 deletions tests/test_run_integration_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ func test_withdraw_liquidity{syscall_ptr: felt*, range_check_ptr}() {
return ();
}

@external
func test_withdraw_liquidity_failing{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
// test withdraw half of the liquidity that was originally deposited (from both pools)
WithdrawLiquidity.test_withdraw_failing();
return ();
}

@external
func test_deposit_liquidity_failing{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
// test withdraw half of the liquidity that was originally deposited (from both pools)
DepositLiquidity.test_deposit_failing();
return ();
}

@external
func test_withdraw_liquidity_not_enough_unlocked{syscall_ptr: felt*, range_check_ptr}() {
// test what happens when more capital is withdrawn than there is unlocked
Expand Down