From 2c5db06127c9b8649e901022afeb4d11b7fcb441 Mon Sep 17 00:00:00 2001 From: poliwop Date: Wed, 18 Oct 2023 16:21:56 -0500 Subject: [PATCH] Fixed confusion in CentralizedMarket about bids/asks --- hydradx/model/amm/centralized_market.py | 44 ++++++++++++------------ hydradx/tests/test_centralized_market.py | 32 ++++++++--------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/hydradx/model/amm/centralized_market.py b/hydradx/model/amm/centralized_market.py index bbc1da48..2c08f70e 100644 --- a/hydradx/model/amm/centralized_market.py +++ b/hydradx/model/amm/centralized_market.py @@ -75,32 +75,32 @@ def swap( sell_tkns_remaining = sell_quantity tkns_bought = 0 - if tkn_sell == quote: + if tkn_sell == base: for bid in self.order_book[(base, quote)].bids: if sell_tkns_remaining <= 0: break - if bid[0] * bid[1] >= sell_tkns_remaining: + if bid[1] >= sell_tkns_remaining: # this bid can fill the entire remaining order - tkns_bought += sell_tkns_remaining / bid[0] - bid[1] -= sell_tkns_remaining / bid[0] + tkns_bought += bid[0] * sell_tkns_remaining + bid[1] -= sell_tkns_remaining sell_tkns_remaining = 0 else: # this bid can partially fill the order - tkns_bought += bid[1] - sell_tkns_remaining -= bid[0] * bid[1] + tkns_bought += bid[1] * bid[0] + sell_tkns_remaining -= bid[1] bid[1] = 0 else: for ask in self.order_book[(base, quote)].asks: if sell_tkns_remaining <= 0: break - if ask[1] >= sell_tkns_remaining: - tkns_bought += ask[0] * sell_tkns_remaining - ask[1] -= sell_tkns_remaining + if ask[0] * ask[1] >= sell_tkns_remaining: + tkns_bought += sell_tkns_remaining / ask[0] + ask[1] -= sell_tkns_remaining / ask[0] sell_tkns_remaining = 0 else: - tkns_bought += ask[0] * ask[1] - sell_tkns_remaining -= ask[1] + tkns_bought += ask[1] + sell_tkns_remaining -= ask[1] * ask[0] ask[1] = 0 agent.holdings[tkn_sell] -= sell_quantity - sell_tkns_remaining @@ -111,30 +111,30 @@ def swap( buy_tkns_remaining = buy_quantity tkns_sold = 0 - if tkn_buy == quote: + if tkn_buy == base: for ask in self.order_book[(base, quote)].asks: if buy_tkns_remaining <= 0: break - if ask[0] * ask[1] >= buy_tkns_remaining: - tkns_sold += buy_tkns_remaining / ask[0] - ask[1] -= buy_tkns_remaining / ask[0] + if ask[1] >= buy_tkns_remaining: + tkns_sold += buy_tkns_remaining * ask[0] + ask[1] -= buy_tkns_remaining buy_tkns_remaining = 0 else: - tkns_sold += ask[1] - buy_tkns_remaining -= ask[0] * ask[1] + tkns_sold += ask[0] * ask[1] + buy_tkns_remaining -= ask[1] ask[1] = 0 else: for bid in self.order_book[(base, quote)].bids: if buy_tkns_remaining <= 0: break - if bid[1] >= buy_tkns_remaining: - tkns_sold += bid[0] * buy_tkns_remaining - bid[1] -= buy_tkns_remaining + if bid[0] * bid[1] >= buy_tkns_remaining: + tkns_sold += buy_tkns_remaining / bid[0] + bid[1] -= buy_tkns_remaining / bid[0] buy_tkns_remaining = 0 else: - tkns_sold += bid[0] * bid[1] - buy_tkns_remaining -= bid[1] + tkns_sold += bid[1] + buy_tkns_remaining -= bid[0] * bid[1] bid[1] = 0 agent.holdings[tkn_buy] += buy_quantity - buy_tkns_remaining diff --git a/hydradx/tests/test_centralized_market.py b/hydradx/tests/test_centralized_market.py index 56bc788f..fb946172 100644 --- a/hydradx/tests/test_centralized_market.py +++ b/hydradx/tests/test_centralized_market.py @@ -52,9 +52,9 @@ def test_sell_quote(quantity: float, trade_fee: float, order_book: OrderBook): ) value_bought = sum( - [quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids] + [quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks] ) - sum( - [quantity * price for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids] + [quantity * price for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks] ) quantity_sold = initial_state.agents['agent'].holdings[tkn_sell] - sell_state.agents['agent'].holdings[tkn_sell] @@ -65,9 +65,9 @@ def test_sell_quote(quantity: float, trade_fee: float, order_book: OrderBook): quantity_bought = sell_state.agents['agent'].holdings[tkn_buy] - initial_state.agents['agent'].holdings[tkn_buy] value_sold = sum( - [quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids] + [quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks] ) - sum( - [quantity for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids] + [quantity for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks] ) if value_sold * (1 - initial_state.pools['Kraken'].trade_fee) != pytest.approx(quantity_bought): @@ -104,18 +104,18 @@ def test_sell_base(quantity: float, trade_fee: float, order_book: OrderBook): quantity_sold = initial_state.agents['agent'].holdings[tkn_sell] - buy_state.agents['agent'].holdings[tkn_sell] value_bought = sum( - [quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks] + [quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids] ) - sum( - [quantity for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks] + [quantity for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids] ) if value_bought != pytest.approx(quantity_sold): raise AssertionError('Central market sell trade failed to execute correctly.') value_sold = sum( - [quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks] + [quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids] ) - sum( - [quantity * price for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks] + [quantity * price for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids] ) quantity_bought = buy_state.agents['agent'].holdings[tkn_buy] - initial_state.agents['agent'].holdings[tkn_buy] @@ -152,9 +152,9 @@ def test_buy_quote(quantity: float, trade_fee: float, order_book: OrderBook): ) value_sold = sum( - [quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks] + [quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids] ) - sum( - [quantity * price for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks] + [quantity * price for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids] ) quantity_bought = buy_state.agents['agent'].holdings[tkn_buy] - initial_state.agents['agent'].holdings[tkn_buy] @@ -165,9 +165,9 @@ def test_buy_quote(quantity: float, trade_fee: float, order_book: OrderBook): quantity_sold = initial_state.agents['agent'].holdings[tkn_sell] - buy_state.agents['agent'].holdings[tkn_sell] value_bought = sum( - [quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks] + [quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids] ) - sum( - [quantity for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks] + [quantity for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids] ) if value_bought / (1 - initial_state.pools['Kraken'].trade_fee) != pytest.approx(quantity_sold): @@ -203,18 +203,18 @@ def test_buy_base(quantity: float, order_book: OrderBook): quantity_bought = sell_state.agents['agent'].holdings[tkn_buy] - initial_state.agents['agent'].holdings[tkn_buy] value_sold = sum( - [quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids] + [quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks] ) - sum( - [quantity for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids] + [quantity for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks] ) if value_sold != pytest.approx(quantity_bought): raise AssertionError('Central market buy trade failed to execute correctly.') value_bought = sum( - [quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids] + [quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks] ) - sum( - [quantity * price for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids] + [quantity * price for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks] ) quantity_sold = initial_state.agents['agent'].holdings[tkn_sell] - sell_state.agents['agent'].holdings[tkn_sell]