Exchange update cont. (#5272)
* Trade -> Order for keyedAcct indices * rename deserialize_trade -> deserialize_order * rename do_order_cancel params * rename vars *_trade -> *_order
This commit is contained in:
@ -39,7 +39,7 @@ impl ExchangeProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_trade(data: &[u8]) -> Result<OrderInfo, InstructionError> {
|
fn deserialize_order(data: &[u8]) -> Result<OrderInfo, InstructionError> {
|
||||||
let state: ExchangeState = bincode::deserialize(data).map_err(Self::map_to_invalid_arg)?;
|
let state: ExchangeState = bincode::deserialize(data).map_err(Self::map_to_invalid_arg)?;
|
||||||
if let ExchangeState::Trade(info) = state {
|
if let ExchangeState::Trade(info) = state {
|
||||||
Ok(info)
|
Ok(info)
|
||||||
@ -259,12 +259,12 @@ impl ExchangeProcessor {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_trade_request(
|
fn do_order_request(
|
||||||
keyed_accounts: &mut [KeyedAccount],
|
keyed_accounts: &mut [KeyedAccount],
|
||||||
info: &OrderRequestInfo,
|
info: &OrderRequestInfo,
|
||||||
) -> Result<(), InstructionError> {
|
) -> Result<(), InstructionError> {
|
||||||
const OWNER_INDEX: usize = 0;
|
const OWNER_INDEX: usize = 0;
|
||||||
const TRADE_INDEX: usize = 1;
|
const ORDER_INDEX: usize = 1;
|
||||||
const ACCOUNT_INDEX: usize = 2;
|
const ACCOUNT_INDEX: usize = 2;
|
||||||
|
|
||||||
if keyed_accounts.len() < 3 {
|
if keyed_accounts.len() < 3 {
|
||||||
@ -272,7 +272,7 @@ impl ExchangeProcessor {
|
|||||||
Err(InstructionError::InvalidArgument)?
|
Err(InstructionError::InvalidArgument)?
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::is_account_unallocated(&keyed_accounts[TRADE_INDEX].account.data)?;
|
Self::is_account_unallocated(&keyed_accounts[ORDER_INDEX].account.data)?;
|
||||||
|
|
||||||
let mut account = Self::deserialize_account(&keyed_accounts[ACCOUNT_INDEX].account.data)?;
|
let mut account = Self::deserialize_account(&keyed_accounts[ACCOUNT_INDEX].account.data)?;
|
||||||
|
|
||||||
@ -307,7 +307,7 @@ impl ExchangeProcessor {
|
|||||||
price: info.price,
|
price: info.price,
|
||||||
tokens_settled: 0,
|
tokens_settled: 0,
|
||||||
}),
|
}),
|
||||||
&mut keyed_accounts[TRADE_INDEX].account.data,
|
&mut keyed_accounts[ORDER_INDEX].account.data,
|
||||||
)?;
|
)?;
|
||||||
Self::serialize(
|
Self::serialize(
|
||||||
&ExchangeState::Account(account),
|
&ExchangeState::Account(account),
|
||||||
@ -317,39 +317,39 @@ impl ExchangeProcessor {
|
|||||||
|
|
||||||
fn do_order_cancellation(keyed_accounts: &mut [KeyedAccount]) -> Result<(), InstructionError> {
|
fn do_order_cancellation(keyed_accounts: &mut [KeyedAccount]) -> Result<(), InstructionError> {
|
||||||
const OWNER_INDEX: usize = 0;
|
const OWNER_INDEX: usize = 0;
|
||||||
const TRADE_INDEX: usize = 1;
|
const ORDER_INDEX: usize = 1;
|
||||||
|
|
||||||
if keyed_accounts.len() < 2 {
|
if keyed_accounts.len() < 2 {
|
||||||
error!("Not enough accounts");
|
error!("Not enough accounts");
|
||||||
Err(InstructionError::InvalidArgument)?
|
Err(InstructionError::InvalidArgument)?
|
||||||
}
|
}
|
||||||
|
|
||||||
let trade = Self::deserialize_trade(&keyed_accounts[TRADE_INDEX].account.data)?;
|
let order = Self::deserialize_order(&keyed_accounts[ORDER_INDEX].account.data)?;
|
||||||
|
|
||||||
if &trade.owner != keyed_accounts[OWNER_INDEX].unsigned_key() {
|
if &order.owner != keyed_accounts[OWNER_INDEX].unsigned_key() {
|
||||||
error!("Signer does not own trade");
|
error!("Signer does not own trade");
|
||||||
Err(InstructionError::GenericError)?
|
Err(InstructionError::GenericError)?
|
||||||
}
|
}
|
||||||
|
|
||||||
let token = match trade.direction {
|
let token = match order.direction {
|
||||||
Direction::To => trade.pair.primary(),
|
Direction::To => order.pair.primary(),
|
||||||
Direction::From => trade.pair.secondary(),
|
Direction::From => order.pair.secondary(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut account = TokenAccountInfo::default().owner(&trade.owner);
|
let mut account = TokenAccountInfo::default().owner(&order.owner);
|
||||||
account.tokens[token] = trade.tokens;
|
account.tokens[token] = order.tokens;
|
||||||
account.tokens[token] += trade.tokens_settled;
|
account.tokens[token] += order.tokens_settled;
|
||||||
|
|
||||||
// Turn trade order into a token account
|
// Turn trade order into a token account
|
||||||
Self::serialize(
|
Self::serialize(
|
||||||
&ExchangeState::Account(account),
|
&ExchangeState::Account(account),
|
||||||
&mut keyed_accounts[TRADE_INDEX].account.data,
|
&mut keyed_accounts[ORDER_INDEX].account.data,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_swap_request(keyed_accounts: &mut [KeyedAccount]) -> Result<(), InstructionError> {
|
fn do_swap_request(keyed_accounts: &mut [KeyedAccount]) -> Result<(), InstructionError> {
|
||||||
const TO_TRADE_INDEX: usize = 1;
|
const TO_ORDER_INDEX: usize = 1;
|
||||||
const FROM_TRADE_INDEX: usize = 2;
|
const FROM_ORDER_INDEX: usize = 2;
|
||||||
const PROFIT_ACCOUNT_INDEX: usize = 3;
|
const PROFIT_ACCOUNT_INDEX: usize = 3;
|
||||||
|
|
||||||
if keyed_accounts.len() < 4 {
|
if keyed_accounts.len() < 4 {
|
||||||
@ -357,64 +357,64 @@ impl ExchangeProcessor {
|
|||||||
Err(InstructionError::InvalidArgument)?
|
Err(InstructionError::InvalidArgument)?
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut to_trade = Self::deserialize_trade(&keyed_accounts[TO_TRADE_INDEX].account.data)?;
|
let mut to_order = Self::deserialize_order(&keyed_accounts[TO_ORDER_INDEX].account.data)?;
|
||||||
let mut from_trade =
|
let mut from_order =
|
||||||
Self::deserialize_trade(&keyed_accounts[FROM_TRADE_INDEX].account.data)?;
|
Self::deserialize_order(&keyed_accounts[FROM_ORDER_INDEX].account.data)?;
|
||||||
let mut profit_account =
|
let mut profit_account =
|
||||||
Self::deserialize_account(&keyed_accounts[PROFIT_ACCOUNT_INDEX].account.data)?;
|
Self::deserialize_account(&keyed_accounts[PROFIT_ACCOUNT_INDEX].account.data)?;
|
||||||
|
|
||||||
if to_trade.direction != Direction::To {
|
if to_order.direction != Direction::To {
|
||||||
error!("To trade is not a To");
|
error!("To trade is not a To");
|
||||||
Err(InstructionError::InvalidArgument)?
|
Err(InstructionError::InvalidArgument)?
|
||||||
}
|
}
|
||||||
if from_trade.direction != Direction::From {
|
if from_order.direction != Direction::From {
|
||||||
error!("From trade is not a From");
|
error!("From trade is not a From");
|
||||||
Err(InstructionError::InvalidArgument)?
|
Err(InstructionError::InvalidArgument)?
|
||||||
}
|
}
|
||||||
if to_trade.pair != from_trade.pair {
|
if to_order.pair != from_order.pair {
|
||||||
error!("Mismatched token pairs");
|
error!("Mismatched token pairs");
|
||||||
Err(InstructionError::InvalidArgument)?
|
Err(InstructionError::InvalidArgument)?
|
||||||
}
|
}
|
||||||
if to_trade.direction == from_trade.direction {
|
if to_order.direction == from_order.direction {
|
||||||
error!("Matching trade directions");
|
error!("Matching trade directions");
|
||||||
Err(InstructionError::InvalidArgument)?
|
Err(InstructionError::InvalidArgument)?
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) =
|
if let Err(e) =
|
||||||
Self::calculate_swap(SCALER, &mut to_trade, &mut from_trade, &mut profit_account)
|
Self::calculate_swap(SCALER, &mut to_order, &mut from_order, &mut profit_account)
|
||||||
{
|
{
|
||||||
error!(
|
error!(
|
||||||
"Swap calculation failed from {} for {} to {} for {}",
|
"Swap calculation failed from {} for {} to {} for {}",
|
||||||
from_trade.tokens, from_trade.price, to_trade.tokens, to_trade.price,
|
from_order.tokens, from_order.price, to_order.tokens, to_order.price,
|
||||||
);
|
);
|
||||||
Err(e)?
|
Err(e)?
|
||||||
}
|
}
|
||||||
|
|
||||||
inc_new_counter_info!("exchange_processor-swaps", 1, 1000, 1000);
|
inc_new_counter_info!("exchange_processor-swaps", 1, 1000, 1000);
|
||||||
|
|
||||||
if to_trade.tokens == 0 {
|
if to_order.tokens == 0 {
|
||||||
// Turn into token account
|
// Turn into token account
|
||||||
Self::serialize(
|
Self::serialize(
|
||||||
&ExchangeState::Account(Self::trade_to_token_account(&from_trade)),
|
&ExchangeState::Account(Self::trade_to_token_account(&from_order)),
|
||||||
&mut keyed_accounts[TO_TRADE_INDEX].account.data,
|
&mut keyed_accounts[TO_ORDER_INDEX].account.data,
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
Self::serialize(
|
Self::serialize(
|
||||||
&ExchangeState::Trade(to_trade),
|
&ExchangeState::Trade(to_order),
|
||||||
&mut keyed_accounts[TO_TRADE_INDEX].account.data,
|
&mut keyed_accounts[TO_ORDER_INDEX].account.data,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if from_trade.tokens == 0 {
|
if from_order.tokens == 0 {
|
||||||
// Turn into token account
|
// Turn into token account
|
||||||
Self::serialize(
|
Self::serialize(
|
||||||
&ExchangeState::Account(Self::trade_to_token_account(&from_trade)),
|
&ExchangeState::Account(Self::trade_to_token_account(&from_order)),
|
||||||
&mut keyed_accounts[FROM_TRADE_INDEX].account.data,
|
&mut keyed_accounts[FROM_ORDER_INDEX].account.data,
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
Self::serialize(
|
Self::serialize(
|
||||||
&ExchangeState::Trade(from_trade),
|
&ExchangeState::Trade(from_order),
|
||||||
&mut keyed_accounts[FROM_TRADE_INDEX].account.data,
|
&mut keyed_accounts[FROM_ORDER_INDEX].account.data,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -447,7 +447,7 @@ pub fn process_instruction(
|
|||||||
ExchangeProcessor::do_transfer_request(keyed_accounts, token, tokens)
|
ExchangeProcessor::do_transfer_request(keyed_accounts, token, tokens)
|
||||||
}
|
}
|
||||||
ExchangeInstruction::OrderRequest(info) => {
|
ExchangeInstruction::OrderRequest(info) => {
|
||||||
ExchangeProcessor::do_trade_request(keyed_accounts, &info)
|
ExchangeProcessor::do_order_request(keyed_accounts, &info)
|
||||||
}
|
}
|
||||||
ExchangeInstruction::OrderCancellation => {
|
ExchangeInstruction::OrderCancellation => {
|
||||||
ExchangeProcessor::do_order_cancellation(keyed_accounts)
|
ExchangeProcessor::do_order_cancellation(keyed_accounts)
|
||||||
@ -722,7 +722,7 @@ mod test {
|
|||||||
price: 1000,
|
price: 1000,
|
||||||
tokens_settled: 0
|
tokens_settled: 0
|
||||||
},
|
},
|
||||||
ExchangeProcessor::deserialize_trade(&trade_account_data).unwrap()
|
ExchangeProcessor::deserialize_order(&trade_account_data).unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
TokenAccountInfo::default()
|
TokenAccountInfo::default()
|
||||||
@ -781,7 +781,7 @@ mod test {
|
|||||||
price: 2000,
|
price: 2000,
|
||||||
tokens_settled: 2,
|
tokens_settled: 2,
|
||||||
},
|
},
|
||||||
ExchangeProcessor::deserialize_trade(&to_trade_account_data).unwrap()
|
ExchangeProcessor::deserialize_order(&to_trade_account_data).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
Reference in New Issue
Block a user