Skip to content

Commit

Permalink
Merge pull request #166 from convergence-rfq/pro-180-refactor-psyopti…
Browse files Browse the repository at this point in the history
…ons-american-logic-to-handle-call-and-put

Pro 180 refactor psyoptions american logic to handle call and put
  • Loading branch information
pindaroso authored Oct 4, 2023
2 parents af2cd06 + 22382d5 commit d8ddba5
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 184 deletions.
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ANCHOR_PROVIDER_URL=https://api.devnet.solana.com
ANCHOR_WALLET=/Users/pindaroso/.config/solana/id.json
ANCHOR_WALLET=~/.config/solana/id.json
6 changes: 2 additions & 4 deletions psyoptions-american-instrument/program/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ pub struct ValidateData<'info> {

/// user provided
pub american_meta: Account<'info, OptionMarket>,
#[account(constraint = american_meta.underlying_asset_mint == mint_info.mint_address @ PsyoptionsAmericanError::PassedMintDoesNotMatch)]
pub mint_info: Account<'info, MintInfo>,
#[account(constraint = american_meta.quote_asset_mint == quote_mint.mint_address @ PsyoptionsAmericanError::PassedMintDoesNotMatch)]
pub quote_mint: Account<'info, MintInfo>,
pub underlying_asset_mint: Account<'info, MintInfo>,
pub stable_asset_mint: Account<'info, MintInfo>,
}

#[derive(Accounts)]
Expand Down
57 changes: 46 additions & 11 deletions psyoptions-american-instrument/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use errors::PsyoptionsAmericanError;
use instructions::*;
use rfq::state::MintType;
use rfq::state::{AssetIdentifier, AuthoritySide};
use risk_engine::state::OptionType;
use state::{AssetIdentifierDuplicate, ParsedLegData};
use state::{AuthoritySideDuplicate, TOKEN_DECIMALS};

Expand All @@ -30,8 +31,8 @@ pub mod psyoptions_american_instrument {
) -> Result<()> {
let ValidateData {
american_meta,
mint_info,
quote_mint,
underlying_asset_mint,
stable_asset_mint,
..
} = &ctx.accounts;

Expand All @@ -49,30 +50,64 @@ pub mod psyoptions_american_instrument {
american_meta_address == american_meta.key(),
PsyoptionsAmericanError::PassedAmericanMetaDoesNotMatch
);

let option_type = option_common_data.option_type;
let underlying_amount_per_contract: u64;
let strike_price: u64;
let underlying_mint: Pubkey;
let quote_mint: Pubkey;
let expected_mint = american_meta.option_mint;

match option_type {
OptionType::Call => {
underlying_amount_per_contract = 10_u64.pow(underlying_asset_mint.decimals as u32);
strike_price = option_common_data.strike_price;
quote_mint = stable_asset_mint.mint_address;
underlying_mint = underlying_asset_mint.mint_address;
}
OptionType::Put => {
underlying_amount_per_contract = option_common_data.strike_price;
strike_price = 10_u64.pow(underlying_asset_mint.decimals as u32);
quote_mint = underlying_asset_mint.mint_address;
underlying_mint = stable_asset_mint.mint_address;
}
}

require!(
mint_address == expected_mint,
PsyoptionsAmericanError::PassedMintDoesNotMatch
);

require!(
option_common_data.underlying_amount_per_contract
== american_meta.underlying_amount_per_contract,
PsyoptionsAmericanError::PassedUnderlyingAmountPerContractDoesNotMatch
underlying_mint == american_meta.underlying_asset_mint,
PsyoptionsAmericanError::PassedMintDoesNotMatch
);
require_eq!(
option_common_data.underlying_amound_per_contract_decimals,
mint_info.decimals,

require!(
quote_mint == american_meta.quote_asset_mint,
PsyoptionsAmericanError::PassedMintDoesNotMatch
);

require!(
underlying_amount_per_contract == american_meta.underlying_amount_per_contract,
PsyoptionsAmericanError::PassedUnderlyingAmountPerContractDoesNotMatch
);

require!(
option_common_data.strike_price == american_meta.quote_amount_per_contract,
strike_price == american_meta.quote_amount_per_contract,
PsyoptionsAmericanError::PassedStrikePriceDoesNotMatch
);
require_eq!(
option_common_data.strike_price_decimals,
quote_mint.decimals,
stable_asset_mint.decimals,
PsyoptionsAmericanError::PassedStrikePriceDecimalsDoesNotMatch
);
require_eq!(
option_common_data.underlying_amound_per_contract_decimals,
underlying_asset_mint.decimals,
PsyoptionsAmericanError::PassedUnderlyingAmountPerContractDecimalsDoesNotMatch
);

require!(
option_common_data.expiration_timestamp == american_meta.expiration_unix_timestamp,
PsyoptionsAmericanError::PassedExpirationTimestampDoesNotMatch
Expand All @@ -84,7 +119,7 @@ pub mod psyoptions_american_instrument {
);

if let (Some(passed_base_asset_index), MintType::AssetWithRisk { base_asset_index }) =
(base_asset_index, mint_info.mint_type)
(base_asset_index, underlying_asset_mint.mint_type)
{
require!(
passed_base_asset_index == u16::from(base_asset_index),
Expand Down
8 changes: 4 additions & 4 deletions psyoptions-european-instrument/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub mod psyoptions_european_instrument {
) -> Result<()> {
let ValidateData {
euro_meta,
mint_info,
underlying_asset_mint,
..
} = &ctx.accounts;

Expand Down Expand Up @@ -89,7 +89,7 @@ pub mod psyoptions_european_instrument {
);

if let (Some(passed_base_asset_index), MintType::AssetWithRisk { base_asset_index }) =
(base_asset_index, mint_info.mint_type)
(base_asset_index, underlying_asset_mint.mint_type)
{
require!(
passed_base_asset_index == u16::from(base_asset_index),
Expand Down Expand Up @@ -331,8 +331,8 @@ pub struct ValidateData<'info> {

/// user provided
pub euro_meta: Account<'info, EuroMeta>,
#[account(constraint = euro_meta.underlying_mint == mint_info.mint_address @ PsyoptionsEuropeanError::PassedMintDoesNotMatch)]
pub mint_info: Account<'info, MintInfo>,
#[account(constraint = euro_meta.underlying_mint == underlying_asset_mint.mint_address @ PsyoptionsEuropeanError::PassedMintDoesNotMatch)]
pub underlying_asset_mint: Account<'info, MintInfo>,
}

#[derive(Accounts)]
Expand Down
Loading

0 comments on commit d8ddba5

Please sign in to comment.