Replace TokenPair in exchange (#5292)

* simplify token pair representation, rename to AssetPair for forward compat.

* update bench exchange TokenPair use
This commit is contained in:
Patrick Amato
2019-07-26 14:31:08 -06:00
committed by GitHub
parent aef7bae60d
commit 33de2cad6d
4 changed files with 37 additions and 49 deletions

View File

@ -509,7 +509,7 @@ fn trader<T>(
T: Client, T: Client,
{ {
// TODO Hard coded for now // TODO Hard coded for now
let pair = TokenPair::AB; let pair = AssetPair::default();
let tokens = 1; let tokens = 1;
let price = 1000; let price = 1000;
let mut account_group: usize = 0; let mut account_group: usize = 0;

View File

@ -12,7 +12,7 @@ pub struct OrderRequestInfo {
pub direction: Direction, pub direction: Direction,
/// Token pair to trade /// Token pair to trade
pub pair: TokenPair, 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 direction
pub tokens: u64, pub tokens: u64,
@ -85,7 +85,7 @@ pub fn trade_request(
owner: &Pubkey, owner: &Pubkey,
trade: &Pubkey, trade: &Pubkey,
direction: Direction, direction: Direction,
pair: TokenPair, pair: AssetPair,
tokens: u64, tokens: u64,
price: u64, price: u64,
src_account: &Pubkey, src_account: &Pubkey,

View File

@ -64,8 +64,8 @@ impl ExchangeProcessor {
// Turn trade order into token account // Turn trade order into token account
let token = match trade.direction { let token = match trade.direction {
Direction::To => trade.pair.secondary(), Direction::To => trade.pair.Quote,
Direction::From => trade.pair.primary(), Direction::From => trade.pair.Base,
}; };
let mut account = TokenAccountInfo::default().owner(&trade.owner); let mut account = TokenAccountInfo::default().owner(&trade.owner);
@ -131,8 +131,8 @@ impl ExchangeProcessor {
trace!("pp {} sp {}", primary_profit, secondary_profit); trace!("pp {} sp {}", primary_profit, secondary_profit);
let primary_token = to_trade.pair.primary(); let primary_token = to_trade.pair.Base;
let secondary_token = from_trade.pair.secondary(); let secondary_token = from_trade.pair.Quote;
// Update tokens // Update tokens
@ -225,8 +225,8 @@ impl ExchangeProcessor {
} }
let from_token = match from_trade.direction { let from_token = match from_trade.direction {
Direction::To => from_trade.pair.secondary(), Direction::To => from_trade.pair.Quote,
Direction::From => from_trade.pair.primary(), Direction::From => from_trade.pair.Base,
}; };
if token != from_token { if token != from_token {
error!("Trade to transfer from does not hold correct token"); error!("Trade to transfer from does not hold correct token");
@ -281,8 +281,8 @@ impl ExchangeProcessor {
Err(InstructionError::GenericError)? Err(InstructionError::GenericError)?
} }
let from_token = match info.direction { let from_token = match info.direction {
Direction::To => info.pair.primary(), Direction::To => info.pair.Base,
Direction::From => info.pair.secondary(), Direction::From => info.pair.Quote,
}; };
if account.tokens[from_token] < info.tokens { if account.tokens[from_token] < info.tokens {
error!("From token balance is too low"); error!("From token balance is too low");
@ -332,8 +332,8 @@ impl ExchangeProcessor {
} }
let token = match order.direction { let token = match order.direction {
Direction::To => order.pair.primary(), Direction::To => order.pair.Base,
Direction::From => order.pair.secondary(), Direction::From => order.pair.Quote,
}; };
let mut account = TokenAccountInfo::default().owner(&order.owner); let mut account = TokenAccountInfo::default().owner(&order.owner);
@ -603,7 +603,7 @@ mod test {
client: &BankClient, client: &BankClient,
owner: &Keypair, owner: &Keypair,
direction: Direction, direction: Direction,
pair: TokenPair, pair: AssetPair,
from_token: Token, from_token: Token,
src_tokens: u64, src_tokens: u64,
trade_tokens: u64, trade_tokens: u64,
@ -701,7 +701,7 @@ mod test {
&client, &client,
&owner, &owner,
Direction::To, Direction::To,
TokenPair::AB, AssetPair::default(),
Token::A, Token::A,
42, 42,
2, 2,
@ -717,7 +717,7 @@ mod test {
OrderInfo { OrderInfo {
owner: owner.pubkey(), owner: owner.pubkey(),
direction: Direction::To, direction: Direction::To,
pair: TokenPair::AB, pair: AssetPair::default(),
tokens: 2, tokens: 2,
price: 1000, price: 1000,
tokens_settled: 0 tokens_settled: 0
@ -743,7 +743,7 @@ mod test {
&client, &client,
&owner, &owner,
Direction::To, Direction::To,
TokenPair::AB, AssetPair::default(),
Token::A, Token::A,
2, 2,
2, 2,
@ -753,7 +753,7 @@ mod test {
&client, &client,
&owner, &owner,
Direction::From, Direction::From,
TokenPair::AB, AssetPair::default(),
Token::B, Token::B,
3, 3,
3, 3,
@ -776,7 +776,7 @@ mod test {
OrderInfo { OrderInfo {
owner: owner.pubkey(), owner: owner.pubkey(),
direction: Direction::To, direction: Direction::To,
pair: TokenPair::AB, pair: AssetPair::default(),
tokens: 1, tokens: 1,
price: 2000, price: 2000,
tokens_settled: 2, tokens_settled: 2,
@ -810,7 +810,7 @@ mod test {
&client, &client,
&owner, &owner,
Direction::To, Direction::To,
TokenPair::AB, AssetPair::default(),
Token::A, Token::A,
3, 3,
3, 3,
@ -820,7 +820,7 @@ mod test {
&client, &client,
&owner, &owner,
Direction::From, Direction::From,
TokenPair::AB, AssetPair::default(),
Token::B, Token::B,
3, 3,
3, 3,

View File

@ -76,32 +76,19 @@ impl std::ops::IndexMut<Token> for Tokens {
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub enum TokenPair { pub struct AssetPair {
AB, // represents a pair of two token enums that defines a market
AC, pub Base: Token,
AD, // "primary" token and numerator for pricing purposes
BC, pub Quote: Token,
BD, // "secondary" token and denominator for pricing purposes
CD,
} }
impl Default for TokenPair {
fn default() -> Self { impl Default for AssetPair {
TokenPair::AB fn default() -> AssetPair {
} AssetPair {
} Base: Token::A,
impl TokenPair { Quote: Token::B,
pub fn primary(self) -> Token {
match self {
TokenPair::AB | TokenPair::AC | TokenPair::AD => Token::A,
TokenPair::BC | TokenPair::BD => Token::B,
TokenPair::CD => Token::C,
}
}
pub fn secondary(self) -> Token {
match self {
TokenPair::AB => Token::B,
TokenPair::AC | TokenPair::BC => Token::C,
TokenPair::AD | TokenPair::BD | TokenPair::CD => Token::D,
} }
} }
} }
@ -114,6 +101,7 @@ pub struct TokenAccountInfo {
/// Current number of tokens this account holds /// Current number of tokens this account holds
pub tokens: Tokens, pub tokens: Tokens,
} }
impl TokenAccountInfo { impl TokenAccountInfo {
pub fn owner(mut self, owner: &Pubkey) -> Self { pub fn owner(mut self, owner: &Pubkey) -> Self {
self.owner = *owner; self.owner = *owner;
@ -156,7 +144,7 @@ pub struct OrderInfo {
/// Direction of the exchange /// Direction of the exchange
pub direction: Direction, pub direction: Direction,
/// Token pair indicating two tokens to exchange, first is primary /// Token pair indicating two tokens to exchange, first is primary
pub pair: TokenPair, 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 direction. Once
/// this number goes to zero this trade order will be converted into a regular token account /// this number goes to zero this trade order will be converted into a regular token account
pub tokens: u64, pub tokens: u64,
@ -171,7 +159,7 @@ impl Default for OrderInfo {
fn default() -> Self { fn default() -> Self {
Self { Self {
owner: Pubkey::default(), owner: Pubkey::default(),
pair: TokenPair::AB, pair: AssetPair::default(),
direction: Direction::To, direction: Direction::To,
tokens: 0, tokens: 0,
price: 0, price: 0,
@ -180,7 +168,7 @@ impl Default for OrderInfo {
} }
} }
impl OrderInfo { impl OrderInfo {
pub fn pair(mut self, pair: TokenPair) -> Self { pub fn pair(mut self, pair: AssetPair) -> Self {
self.pair = pair; self.pair = pair;
self self
} }