Skip to content

Commit

Permalink
Rework hxro settlement process and update programs
Browse files Browse the repository at this point in the history
  • Loading branch information
EquilateralDelta committed Dec 4, 2023
1 parent 9a111ab commit 4ae6aaf
Show file tree
Hide file tree
Showing 31 changed files with 5,262 additions and 368 deletions.
82 changes: 63 additions & 19 deletions hxro-print-trade-provider/dex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ pub mod dex {
) -> ProgramResult {
Ok(())
}

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

fn log_errors(e: DomainOrProgramError) -> ProgramError {
Expand Down Expand Up @@ -718,43 +724,79 @@ pub struct InitializePrintTradeParams {
pub struct InitializePrintTrade<'info> {
#[account(mut)]
pub user: Signer<'info>,
pub creator: AccountLoader<'info, TraderRiskGroup>, // user owns creator trg
pub counterparty: AccountLoader<'info, TraderRiskGroup>,
pub operator: AccountLoader<'info, TraderRiskGroup>,
pub creator: AccountInfo<'info>,
pub counterparty: AccountInfo<'info>,
pub operator: AccountInfo<'info>,
#[account(mut)]
pub market_product_group: AccountLoader<'info, MarketProductGroup>,
#[account(init, owner = crate::ID, payer = user, space = 8 + PrintTrade::SIZE, seeds = [b"print_trade", creator.key().as_ref(), counterparty.key().as_ref()], bump)]
pub print_trade: AccountLoader<'info, PrintTrade>,
pub system_program: Program<'info, System>,
pub market_product_group: AccountInfo<'info>,
#[account(mut)]
pub print_trade: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
pub operator_owner: Signer<'info>,
pub seed: AccountInfo<'info>,
}

#[repr(C)]
#[derive(AnchorSerialize, AnchorDeserialize, Debug, Clone)]
pub struct SignPrintTradeParams {
pub num_products: usize,
pub products: PrintTradeProductIndexes,
pub price: Fractional, // quantity of quote (e.g., USDC) per base
pub side: Side, // side that counter party is taking
pub operator_creator_fee_proportion: Fractional, // force counterparty to pass in operator fees to avoid rugging via operator fees
pub operator_counterparty_fee_proportion: Fractional, // force counterparty to pass in operator fees to avoid rugging via operator fees
pub price: Fractional,
pub side: Side,
pub operator_creator_fee_proportion: Fractional,
pub operator_counterparty_fee_proportion: Fractional,
pub use_locked_collateral: bool,
}

#[derive(Accounts)]
pub struct SignPrintTrade<'info> {
#[account(mut)]
pub user: Signer<'info>, // user owns counterparty trg
pub user: Signer<'info>,
#[account(mut)]
pub creator: AccountLoader<'info, TraderRiskGroup>,
pub creator: AccountInfo<'info>,
#[account(mut)]
pub counterparty: AccountLoader<'info, TraderRiskGroup>,
pub counterparty: AccountInfo<'info>,
#[account(mut)]
pub operator: AccountLoader<'info, TraderRiskGroup>,
pub operator: AccountInfo<'info>,
#[account(mut)]
pub market_product_group: AccountLoader<'info, MarketProductGroup>,
#[account(mut, close=creator)]
pub print_trade: AccountLoader<'info, PrintTrade>,
pub market_product_group: AccountInfo<'info>,
#[account(mut)]
pub print_trade: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
pub fee_model_program: AccountInfo<'info>,
pub fee_model_configuration_acct: AccountInfo<'info>,
#[account(mut)]
pub fee_output_register: AccountInfo<'info>,
pub risk_engine_program: AccountInfo<'info>,
pub risk_model_configuration_acct: AccountInfo<'info>,
#[account(mut)]
pub risk_output_register: AccountInfo<'info>,
pub risk_and_fee_signer: AccountInfo<'info>,
#[account(mut)]
pub creator_trader_fee_state_acct: AccountInfo<'info>,
#[account(mut)]
pub creator_trader_risk_state_acct: AccountInfo<'info>,
#[account(mut)]
pub counterparty_trader_fee_state_acct: AccountInfo<'info>,
#[account(mut)]
pub counterparty_trader_risk_state_acct: AccountInfo<'info>,
pub seed: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct ExecutePrintTrade<'info> {
#[account(mut)]
pub op: Signer<'info>,
#[account(mut)]
pub creator: AccountInfo<'info>,
#[account(mut)]
pub counterparty: AccountInfo<'info>,
#[account(mut)]
pub operator: AccountInfo<'info>,
#[account(mut)]
pub market_product_group: AccountInfo<'info>,
#[account(mut)]
pub print_trade: AccountInfo<'info>,
pub system_program: Program<'info, System>,
#[account(executable)]
fee_model_program: AccountInfo<'info>,
Expand All @@ -775,5 +817,7 @@ pub struct SignPrintTrade<'info> {
counterparty_trader_fee_state_acct: AccountInfo<'info>,
#[account(mut)]
counterparty_trader_risk_state_acct: AccountInfo<'info>,
pub operator_owner: Signer<'info>,
pub seed: AccountInfo<'info>,
#[account(mut)]
pub execution_output: AccountInfo<'info>,
}
37 changes: 32 additions & 5 deletions hxro-print-trade-provider/dex/src/state/print_trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use crate::{
},
};

#[account(zero_copy)]
#[derive(AnchorSerialize, AnchorDeserialize, Default, Debug)]
#[account(zero_copy(unsafe))]
#[derive(Default, Debug)]
pub struct PrintTradeProduct {
pub product_key: Pubkey, // verify that the product at the given index is this one
pub size: Fractional, // quantity of base (e.g. BTCUSD contract)
Expand All @@ -49,25 +49,52 @@ pub type PrintTradeProducts = [PrintTradeProduct; PrintTrade::MAX_PRODUCTS_PER_T
pub type PrintTradeProductIndexes = [PrintTradeProductIndex; PrintTrade::MAX_PRODUCTS_PER_TRADE];

#[account(zero_copy(unsafe))]
#[derive(AnchorDeserialize)]
#[derive(Debug)]
pub struct PrintTrade {
pub is_initialized: bool,
pub creator: Pubkey,
pub counterparty: Pubkey,
pub seed: Pubkey,
pub market_product_group: Pubkey, // technically might not need to store this
pub strange_padding: [u8; 7], // for some reason, account parsing is misaligned without this padding
pub num_products: usize,
pub products: PrintTradeProducts,
pub price: Fractional, // quantity of quote (USD) per base
pub side: Side,
pub operator: Pubkey,
pub operator_creator_fee_proportion: Fractional,
pub operator_counterparty_fee_proportion: Fractional,
pub is_operator_signer: bool, // if false, only counterparty has to sign; if true, both operator and counterparty must sign
pub is_collateral_locked: bool,
pub strange_padding_2: [u8; 7], // for some reason, account parsing is misaligned without this padding
pub is_signed: bool,
pub is_cancelled: CancelStatus,
pub bump: u8,
}

impl PrintTrade {
pub const MAX_PRODUCTS_PER_TRADE: usize = 6;
pub const SIZE: usize = std::mem::size_of::<PrintTrade>();
}

#[derive(AnchorSerialize, AnchorDeserialize, Copy, Clone, Debug, PartialEq)]
#[repr(u8)]
pub enum CancelStatus {
Active,
CreatorCancelled,
CounterpartyCancelled,
}

#[account(zero_copy(unsafe))]
pub struct PrintTradeExecutionOutput {
pub result: PrintTradeExecutionResult,
}

#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u8)]
pub enum PrintTradeExecutionResult {
CounterpartyHasntSigned,
CreatorCancelled,
CounterpartyCancelled,
CreatorNotEnoughLockedCollateral,
CounterpartyNotEnoughLockedCollateral,
Success,
}
2 changes: 1 addition & 1 deletion hxro-print-trade-provider/dex/src/utils/cpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
find_fees_ix,
state::{
fee_model::TraderFeeParams,
risk_engine_register::{HealthResult, OrderInfo, RiskOutputRegister},
risk_engine_register::{HealthInfo, HealthResult, OrderInfo, RiskOutputRegister},
},
utils::{
loadable::Loadable,
Expand Down
6 changes: 6 additions & 0 deletions hxro-print-trade-provider/program/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ pub enum HxroPrintTradeProviderError {
InvalidTRGMarket,
#[msg("Another TRG is expected for this operation")]
UnexpectedTRG,
#[msg("Print trade account expected to be signed by counterparty")]
ExpectedSignedPrintTrade,
#[msg("Invalid print trade address")]
InvalidPrintTradeAddress,
#[msg("Invalid print trade parameters")]
InvalidPrintTradeParams,
}
8 changes: 6 additions & 2 deletions hxro-print-trade-provider/program/src/helpers/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ pub fn to_hxro_product(rfq: &Rfq, response: &Response, leg_index: u8) -> Result<
}

pub fn to_hxro_price(rfq: &Rfq, response: &Response) -> Fractional {
let full_amount = response.get_quote_amount_to_transfer(rfq) as i64;
let mut full_amount = response.get_quote_amount_to_transfer(rfq) as i64;

if response.get_quote_tokens_receiver() == AuthoritySide::Taker {
full_amount = -full_amount;
}

Fractional {
m: full_amount,
exp: rfq.quote_asset.decimals as u64,
} // missing division by the amount in leg
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub fn initialize_print_trade<'info>(
print_trade: print_trade.to_account_info(),
system_program: system_program.to_account_info(),
operator_owner: operator.to_account_info(),
seed: response.to_account_info(),
};

let bump: u8 = *ctx.bumps.get("operator").unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use anchor_lang::prelude::*;
use dex::cpi::accounts::ExecutePrintTrade;
use dex::cpi::execute_print_trade as execute_print_trade_cpi;

use crate::constants::OPERATOR_SEED;
use crate::SettlePrintTradeAccounts;

pub fn execute_print_trade<'info>(
ctx: &Context<'_, '_, '_, 'info, SettlePrintTradeAccounts<'info>>,
) -> Result<()> {
let SettlePrintTradeAccounts {
response,
dex,
operator,
market_product_group,
creator_trg,
counterparty_trg,
operator_trg,
print_trade,
execution_output,
fee_model_program,
fee_model_configuration_acct,
fee_output_register,
risk_engine_program,
risk_model_configuration_acct,
risk_output_register,
risk_and_fee_signer,
creator_fee_state_acct,
creator_risk_state_acct,
counterparty_fee_state_acct,
counterparty_risk_state_acct,
system_program,
..
} = &ctx.accounts;

let accounts = ExecutePrintTrade {
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(),
fee_model_program: fee_model_program.to_account_info(),
fee_model_configuration_acct: fee_model_configuration_acct.to_account_info(),
fee_output_register: fee_output_register.to_account_info(),
risk_engine_program: risk_engine_program.to_account_info(),
risk_model_configuration_acct: risk_model_configuration_acct.to_account_info(),
risk_output_register: risk_output_register.to_account_info(),
risk_and_fee_signer: risk_and_fee_signer.to_account_info(),
creator_trader_fee_state_acct: creator_fee_state_acct.to_account_info(),
creator_trader_risk_state_acct: creator_risk_state_acct.to_account_info(),
counterparty_trader_fee_state_acct: counterparty_fee_state_acct.to_account_info(),
counterparty_trader_risk_state_acct: counterparty_risk_state_acct.to_account_info(),
seed: response.to_account_info(),
execution_output: execution_output.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]]],
};

execute_print_trade_cpi(context)
}
83 changes: 0 additions & 83 deletions hxro-print-trade-provider/program/src/helpers/lock_collateral.rs

This file was deleted.

Loading

0 comments on commit 4ae6aaf

Please sign in to comment.