exchange update: replace direction (#5362)

* replace direction with OrderSide

* bench exchange: update direction uses to OrderSide
This commit is contained in:
Patrick Amato 2019-07-31 11:19:09 -06:00 committed by GitHub
parent 3b752876ac
commit 0b0b679120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 64 deletions

View File

@ -527,21 +527,21 @@ fn trader<T>(
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<T>(
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<T>(
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::<ExchangeState>() as u64;
@ -571,7 +571,7 @@ fn trader<T>(
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,

View File

@ -96,12 +96,12 @@ impl OrderBook {
// Ok(())
// }
pub fn push(&mut self, pubkey: Pubkey, info: OrderInfo) -> Result<(), Box<dyn error::Error>> {
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 });
}
}

View File

@ -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,

View File

@ -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,

View File

@ -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",