Skip to content

Commit

Permalink
Add closing hxro print trade accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
EquilateralDelta committed Dec 5, 2023
1 parent b2fc8e2 commit 6f1cbfc
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 105 deletions.
22 changes: 22 additions & 0 deletions hxro-print-trade-provider/dex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ pub mod dex {
) -> Result<()> {
Ok(())
}

pub fn close_print_trade<'info>(
_ctx: Context<'_, '_, '_, 'info, ClosePrintTrade<'info>>,
) -> Result<()> {
Ok(())
}
}

fn log_errors(e: DomainOrProgramError) -> ProgramError {
Expand Down Expand Up @@ -821,3 +827,19 @@ pub struct ExecutePrintTrade<'info> {
#[account(mut)]
pub execution_output: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct ClosePrintTrade<'info> {
#[account(mut)]
pub op: Signer<'info>,
pub creator: AccountLoader<'info, TraderRiskGroup>, // user owns creator trg
pub counterparty: AccountLoader<'info, TraderRiskGroup>,
pub operator: AccountLoader<'info, TraderRiskGroup>,
pub market_product_group: AccountLoader<'info, MarketProductGroup>,
#[account(mut)]
pub print_trade: AccountLoader<'info, PrintTrade>,
pub system_program: Program<'info, System>,
pub seed: AccountInfo<'info>,
#[account(mut)]
pub creator_wallet: AccountInfo<'info>,
}
56 changes: 56 additions & 0 deletions hxro-print-trade-provider/program/src/helpers/close_print_trade.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use anchor_lang::prelude::*;
use dex::cpi::accounts::ClosePrintTrade;
use dex::cpi::close_print_trade as close_print_trade_cpi;
use rfq::state::AuthoritySide;

use crate::constants::OPERATOR_SEED;
use crate::CleanUpPrintTradeAccounts;

pub fn close_print_trade<'info>(
ctx: &Context<'_, '_, '_, 'info, CleanUpPrintTradeAccounts<'info>>,
) -> Result<()> {
let CleanUpPrintTradeAccounts {
response,
operator,
dex,
market_product_group,
taker_trg,
maker_trg,
operator_trg,
print_trade,
creator_wallet,
system_program,
..
} = &ctx.accounts;

let (creator_trg, counterparty_trg) =
if response.print_trade_initialized_by.unwrap() == AuthoritySide::Taker {
(taker_trg, maker_trg)
} else {
(maker_trg, taker_trg)
};

let accounts = ClosePrintTrade {
op: operator.to_account_info(),
creator: creator_trg.to_account_info(),
counterparty: counterparty_trg.to_account_info(),
operator: operator_trg.to_account_info(),
market_product_group: market_product_group.to_account_info(),
print_trade: print_trade.to_account_info(),
system_program: system_program.to_account_info(),
seed: response.to_account_info(),
creator_wallet: creator_wallet.to_account_info(),
};

let bump: u8 = *ctx.bumps.get("operator").unwrap();
let context = CpiContext {
accounts,
remaining_accounts: vec![],
program: dex.to_account_info(),
signer_seeds: &[&[OPERATOR_SEED.as_bytes(), &[bump]]],
};

close_print_trade_cpi(context)?;

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub fn initialize_print_trade<'info>(
dex,
market_product_group,
user,
user_trg,
counterparty_trg,
taker_trg,
maker_trg,
operator,
operator_trg,
print_trade,
Expand Down Expand Up @@ -59,9 +59,15 @@ pub fn initialize_print_trade<'info>(
operator_counterparty_fee_proportion: OPERATOR_COUNTERPARTY_FEE_PROPORTION,
};

let (creator_trg, counterparty_trg) = if authority_side == AuthoritySide::Taker {
(taker_trg, maker_trg)
} else {
(maker_trg, taker_trg)
};

let accounts = InitializePrintTrade {
user: user.to_account_info(),
creator: user_trg.to_account_info(),
creator: creator_trg.to_account_info(),
counterparty: counterparty_trg.to_account_info(),
operator: operator_trg.to_account_info(),
market_product_group: market_product_group.to_account_info(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub fn execute_print_trade<'info>(
dex,
operator,
market_product_group,
creator_trg,
counterparty_trg,
taker_trg,
maker_trg,
operator_trg,
print_trade,
execution_output,
Expand All @@ -36,6 +36,13 @@ pub fn execute_print_trade<'info>(
..
} = &ctx.accounts;

let (creator_trg, counterparty_trg) =
if response.print_trade_initialized_by.unwrap() == AuthoritySide::Taker {
(taker_trg, maker_trg)
} else {
(maker_trg, taker_trg)
};

let accounts = ExecutePrintTrade {
op: operator.to_account_info(),
creator: creator_trg.to_account_info(),
Expand Down
2 changes: 2 additions & 0 deletions hxro-print-trade-provider/program/src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod close_print_trade;
pub mod common;
mod conversions;
mod create_print_trade;
mod execute_print_trade;
mod initialize_trader_risk_group;
mod validation;

pub use close_print_trade::*;
pub use create_print_trade::*;
pub use execute_print_trade::*;
pub use initialize_trader_risk_group::*;
Expand Down
69 changes: 68 additions & 1 deletion hxro-print-trade-provider/program/src/helpers/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use dex::state::market_product_group::MarketProductGroup;
use dex::state::products::Product;
use dex::state::trader_risk_group::TraderRiskGroup;
use dex::utils::numeric::{Fractional, ZERO_FRAC};
use dex::ID as DexID;
use instruments::state::derivative_metadata::DerivativeMetadata;
use instruments::state::enums::{InstrumentType as HxroInstrumentType, OracleType};
use rfq::state::{BaseAssetInfo, Leg, Response, Rfq};
use rfq::state::{AuthoritySide, BaseAssetInfo, Leg, Response, Rfq};
use risk_engine::state::{InstrumentType, OptionType};

use crate::constants::EXPECTED_DECIMALS;
Expand Down Expand Up @@ -270,3 +271,69 @@ fn validate_underlying_amount_per_contract(risk_engine_data: &ParsedRiskEngineDa

Ok(())
}

pub struct ValidationInput<'a, 'info: 'a> {
pub first_to_prepare: AuthoritySide,

pub rfq: &'a Box<Account<'info, Rfq>>,
pub response: &'a Box<Account<'info, Response>>,

pub operator: &'a UncheckedAccount<'info>,
pub taker_trg: &'a AccountLoader<'info, TraderRiskGroup>,
pub maker_trg: &'a AccountLoader<'info, TraderRiskGroup>,
pub operator_trg: &'a AccountLoader<'info, TraderRiskGroup>,
pub print_trade_key: Pubkey,
}

pub fn validate_print_trade_accounts(input: ValidationInput) -> Result<()> {
let ValidationInput {
first_to_prepare,
rfq,
response,
operator,
taker_trg,
maker_trg,
operator_trg,
print_trade_key,
} = input;

require_keys_eq!(
taker_trg.key(),
parse_taker_trg(&rfq)?,
HxroPrintTradeProviderError::UnexpectedTRG
);
require_keys_eq!(
maker_trg.key(),
parse_maker_trg(&response)?,
HxroPrintTradeProviderError::UnexpectedTRG
);

let operator_trg_owner = operator_trg.load()?.owner;
require_keys_eq!(
operator.key(),
operator_trg_owner,
HxroPrintTradeProviderError::InvalidOperatorTRG
);

let (creator, counterparty) = if first_to_prepare == AuthoritySide::Taker {
(taker_trg, maker_trg)
} else {
(maker_trg, taker_trg)
};
let (expected_print_trade_address, _) = Pubkey::find_program_address(
&[
b"print_trade",
creator.key().as_ref(),
counterparty.key().as_ref(),
response.key().as_ref(),
],
&DexID,
);
require_keys_eq!(
print_trade_key,
expected_print_trade_address,
HxroPrintTradeProviderError::InvalidPrintTradeAddress
);

Ok(())
}
Loading

0 comments on commit 6f1cbfc

Please sign in to comment.