From 0b0b6791209caf95f04af7b7bfac00ee2097482f Mon Sep 17 00:00:00 2001 From: Patrick Amato <51000544+pathoam@users.noreply.github.com> Date: Wed, 31 Jul 2019 11:19:09 -0600 Subject: [PATCH] exchange update: replace direction (#5362) * replace direction with OrderSide * bench exchange: update direction uses to OrderSide --- bench-exchange/src/bench.rs | 16 +++--- bench-exchange/src/order_book.rs | 8 +-- .../exchange_api/src/exchange_instruction.rs | 10 ++-- .../exchange_api/src/exchange_processor.rs | 56 +++++++++---------- programs/exchange_api/src/exchange_state.rs | 38 ++++++------- 5 files changed, 64 insertions(+), 64 deletions(-) diff --git a/bench-exchange/src/bench.rs b/bench-exchange/src/bench.rs index 358d06e91..5d7390ae9 100644 --- a/bench-exchange/src/bench.rs +++ b/bench-exchange/src/bench.rs @@ -527,21 +527,21 @@ fn trader( let mut trade_infos = vec![]; let start = account_group * batch_size as usize; let end = account_group * batch_size as usize + batch_size as usize; - let mut direction = Direction::To; + let mut side = OrderSide::Ask; for (signer, trade, src) in izip!( signers[start..end].iter(), trade_keys, srcs[start..end].iter(), ) { - direction = if direction == Direction::To { - Direction::From + side = if side == OrderSide::Ask { + OrderSide::Bid } else { - Direction::To + OrderSide::Ask }; let order_info = OrderInfo { /// Owner of the trade order owner: Pubkey::default(), // don't care - direction, + side, pair, tokens, price, @@ -551,7 +551,7 @@ fn trader( trade_account: trade.pubkey(), order_info, }); - trades.push((signer, trade.pubkey(), direction, src)); + trades.push((signer, trade.pubkey(), side, src)); } account_group = (account_group + 1) % account_groups as usize; @@ -562,7 +562,7 @@ fn trader( trades.chunks(chunk_size).for_each(|chunk| { let trades_txs: Vec<_> = chunk .par_iter() - .map(|(signer, trade, direction, src)| { + .map(|(signer, trade, side, src)| { let s: &Keypair = &signer; let owner = &signer.pubkey(); let space = mem::size_of::() as u64; @@ -571,7 +571,7 @@ fn trader( vec![ system_instruction::create_account(owner, trade, 1, space, &id()), exchange_instruction::trade_request( - owner, trade, *direction, pair, tokens, price, src, + owner, trade, *side, pair, tokens, price, src, ), ], blockhash, diff --git a/bench-exchange/src/order_book.rs b/bench-exchange/src/order_book.rs index 87a9aefa5..11082de9d 100644 --- a/bench-exchange/src/order_book.rs +++ b/bench-exchange/src/order_book.rs @@ -96,12 +96,12 @@ impl OrderBook { // Ok(()) // } pub fn push(&mut self, pubkey: Pubkey, info: OrderInfo) -> Result<(), Box> { - check_trade(info.direction, info.tokens, info.price)?; - match info.direction { - Direction::To => { + check_trade(info.side, info.tokens, info.price)?; + match info.side { + OrderSide::Ask => { self.to_ab.push(ToOrder { pubkey, info }); } - Direction::From => { + OrderSide::Bid => { self.from_ab.push(FromOrder { pubkey, info }); } } diff --git a/programs/exchange_api/src/exchange_instruction.rs b/programs/exchange_api/src/exchange_instruction.rs index 3b4b4290a..444fbbb02 100644 --- a/programs/exchange_api/src/exchange_instruction.rs +++ b/programs/exchange_api/src/exchange_instruction.rs @@ -8,13 +8,13 @@ use solana_sdk::pubkey::Pubkey; #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub struct OrderRequestInfo { - /// Direction of trade - pub direction: Direction, + /// Side of market of the order (bid/ask) + pub side: OrderSide, /// Token pair to trade pub pair: AssetPair, - /// Number of tokens to exchange; refers to the primary or the secondary depending on the direction + /// Number of tokens to exchange; refers to the primary or the secondary depending on the order side pub tokens: u64, /// The price ratio the primary price over the secondary price. The primary price is fixed @@ -84,7 +84,7 @@ pub fn transfer_request( pub fn trade_request( owner: &Pubkey, trade: &Pubkey, - direction: Direction, + side: OrderSide, pair: AssetPair, tokens: u64, price: u64, @@ -98,7 +98,7 @@ pub fn trade_request( Instruction::new( id(), &ExchangeInstruction::OrderRequest(OrderRequestInfo { - direction, + side, pair, tokens, price, diff --git a/programs/exchange_api/src/exchange_processor.rs b/programs/exchange_api/src/exchange_processor.rs index 41495d6e1..e0acdb663 100644 --- a/programs/exchange_api/src/exchange_processor.rs +++ b/programs/exchange_api/src/exchange_processor.rs @@ -63,9 +63,9 @@ impl ExchangeProcessor { fn trade_to_token_account(trade: &OrderInfo) -> TokenAccountInfo { // Turn trade order into token account - let token = match trade.direction { - Direction::To => trade.pair.Quote, - Direction::From => trade.pair.Base, + let token = match trade.side { + OrderSide::Ask => trade.pair.Quote, + OrderSide::Bid => trade.pair.Base, }; let mut account = TokenAccountInfo::default().owner(&trade.owner); @@ -224,9 +224,9 @@ impl ExchangeProcessor { Err(InstructionError::GenericError)? } - let from_token = match from_trade.direction { - Direction::To => from_trade.pair.Quote, - Direction::From => from_trade.pair.Base, + let from_token = match from_trade.side { + OrderSide::Ask => from_trade.pair.Quote, + OrderSide::Bid => from_trade.pair.Base, }; if token != from_token { error!("Trade to transfer from does not hold correct token"); @@ -280,16 +280,16 @@ impl ExchangeProcessor { error!("Signer does not own account"); Err(InstructionError::GenericError)? } - let from_token = match info.direction { - Direction::To => info.pair.Base, - Direction::From => info.pair.Quote, + let from_token = match info.side { + OrderSide::Ask => info.pair.Base, + OrderSide::Bid => info.pair.Quote, }; if account.tokens[from_token] < info.tokens { error!("From token balance is too low"); Err(InstructionError::GenericError)? } - if let Err(e) = check_trade(info.direction, info.tokens, info.price) { + if let Err(e) = check_trade(info.side, info.tokens, info.price) { bincode::serialize(&e).unwrap(); } @@ -301,7 +301,7 @@ impl ExchangeProcessor { Self::serialize( &ExchangeState::Trade(OrderInfo { owner: *keyed_accounts[OWNER_INDEX].unsigned_key(), - direction: info.direction, + side: info.side, pair: info.pair, tokens: info.tokens, price: info.price, @@ -331,9 +331,9 @@ impl ExchangeProcessor { Err(InstructionError::GenericError)? } - let token = match order.direction { - Direction::To => order.pair.Base, - Direction::From => order.pair.Quote, + let token = match order.side { + OrderSide::Ask => order.pair.Base, + OrderSide::Bid => order.pair.Quote, }; let mut account = TokenAccountInfo::default().owner(&order.owner); @@ -363,11 +363,11 @@ impl ExchangeProcessor { let mut profit_account = Self::deserialize_account(&keyed_accounts[PROFIT_ACCOUNT_INDEX].account.data)?; - if to_order.direction != Direction::To { + if to_order.side != OrderSide::Ask { error!("To trade is not a To"); Err(InstructionError::InvalidArgument)? } - if from_order.direction != Direction::From { + if from_order.side != OrderSide::Bid { error!("From trade is not a From"); Err(InstructionError::InvalidArgument)? } @@ -375,8 +375,8 @@ impl ExchangeProcessor { error!("Mismatched token pairs"); Err(InstructionError::InvalidArgument)? } - if to_order.direction == from_order.direction { - error!("Matching trade directions"); + if to_order.side == from_order.side { + error!("Matching trade sides"); Err(InstructionError::InvalidArgument)? } @@ -488,7 +488,7 @@ mod test { secondary_price, ); let mut to_trade = OrderInfo::default(); - let mut from_trade = OrderInfo::default().direction(Direction::From); + let mut from_trade = OrderInfo::default().side(OrderSide::Bid); let mut profit_account = TokenAccountInfo::default(); to_trade.tokens = primary_tokens; @@ -602,7 +602,7 @@ mod test { fn trade( client: &BankClient, owner: &Keypair, - direction: Direction, + side: OrderSide, pair: AssetPair, from_token: Token, src_tokens: u64, @@ -616,7 +616,7 @@ mod test { let instruction = exchange_instruction::trade_request( &owner.pubkey(), &trade, - direction, + side, pair, trade_tokens, price, @@ -700,7 +700,7 @@ mod test { let (trade, src) = trade( &client, &owner, - Direction::To, + OrderSide::Ask, AssetPair::default(), Token::A, 42, @@ -716,7 +716,7 @@ mod test { assert_eq!( OrderInfo { owner: owner.pubkey(), - direction: Direction::To, + side: OrderSide::Ask, pair: AssetPair::default(), tokens: 2, price: 1000, @@ -742,7 +742,7 @@ mod test { let (to_trade, _) = trade( &client, &owner, - Direction::To, + OrderSide::Ask, AssetPair::default(), Token::A, 2, @@ -752,7 +752,7 @@ mod test { let (from_trade, _) = trade( &client, &owner, - Direction::From, + OrderSide::Bid, AssetPair::default(), Token::B, 3, @@ -775,7 +775,7 @@ mod test { assert_eq!( OrderInfo { owner: owner.pubkey(), - direction: Direction::To, + side: OrderSide::Ask, pair: AssetPair::default(), tokens: 1, price: 2000, @@ -809,7 +809,7 @@ mod test { let (to_trade, _) = trade( &client, &owner, - Direction::To, + OrderSide::Ask, AssetPair::default(), Token::A, 3, @@ -819,7 +819,7 @@ mod test { let (from_trade, _) = trade( &client, &owner, - Direction::From, + OrderSide::Bid, AssetPair::default(), Token::B, 3, diff --git a/programs/exchange_api/src/exchange_state.rs b/programs/exchange_api/src/exchange_state.rs index 6721abce6..09a4a8d84 100644 --- a/programs/exchange_api/src/exchange_state.rs +++ b/programs/exchange_api/src/exchange_state.rs @@ -118,19 +118,19 @@ impl TokenAccountInfo { } } -/// Direction of the exchange between two tokens in a pair +/// side of the exchange between two tokens in a pair #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] -pub enum Direction { - /// Trade first token type (primary) in the pair 'To' the second - To, - /// Trade first token type in the pair 'From' the second (secondary) - From, +pub enum OrderSide { + /// Offer the Base asset and Accept the Quote asset + Ask, // to + /// Offer the Quote asset and Accept the Base asset + Bid, // from } -impl fmt::Display for Direction { +impl fmt::Display for OrderSide { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Direction::To => write!(f, "T")?, - Direction::From => write!(f, "F")?, + OrderSide::Ask => write!(f, "A")?, + OrderSide::Bid => write!(f, "B")?, } Ok(()) } @@ -141,11 +141,11 @@ impl fmt::Display for Direction { pub struct OrderInfo { /// Owner of the trade order pub owner: Pubkey, - /// Direction of the exchange - pub direction: Direction, + /// side of the order in the market (bid/ask) + pub side: OrderSide, /// Token pair indicating two tokens to exchange, first is primary pub pair: AssetPair, - /// Number of tokens to exchange; primary or secondary depending on direction. Once + /// Number of tokens to exchange; primary or secondary depending on side. Once /// this number goes to zero this trade order will be converted into a regular token account pub tokens: u64, /// Scaled price of the secondary token given the primary is equal to the scale value @@ -160,7 +160,7 @@ impl Default for OrderInfo { Self { owner: Pubkey::default(), pair: AssetPair::default(), - direction: Direction::To, + side: OrderSide::Ask, tokens: 0, price: 0, tokens_settled: 0, @@ -172,8 +172,8 @@ impl OrderInfo { self.pair = pair; self } - pub fn direction(mut self, direction: Direction) -> Self { - self.direction = direction; + pub fn side(mut self, side: OrderSide) -> Self { + self.side = side; self } pub fn tokens(mut self, tokens: u64) -> Self { @@ -186,9 +186,9 @@ impl OrderInfo { } } -pub fn check_trade(direction: Direction, tokens: u64, price: u64) -> Result<(), ExchangeError> { - match direction { - Direction::To => { +pub fn check_trade(side: OrderSide, tokens: u64, price: u64) -> Result<(), ExchangeError> { + match side { + OrderSide::Ask => { if tokens * price / SCALER == 0 { Err(ExchangeError::InvalidTrade(format!( "To trade of {} for {}/{} results in 0 tradeable tokens", @@ -196,7 +196,7 @@ pub fn check_trade(direction: Direction, tokens: u64, price: u64) -> Result<(), )))? } } - Direction::From => { + OrderSide::Bid => { if tokens * SCALER / price == 0 { Err(ExchangeError::InvalidTrade(format!( "From trade of {} for {}?{} results in 0 tradeable tokens",