Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for order quotes #331

Merged
merged 9 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions populate_cip_20.sql → tests/queries/batch_rewards_test_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ CREATE TABLE IF NOT EXISTS auction_participants

CREATE TABLE IF NOT EXISTS settlement_scores
(
auction_id bigint PRIMARY KEY,
block_deadline bigint NOT NULL,
winner bytea NOT NULL,
winning_score numeric(78, 0) NOT NULL,
reference_score numeric(78, 0) NOT NULL
auction_id bigint PRIMARY KEY,
winner bytea NOT NULL,
winning_score numeric(78, 0) NOT NULL,
reference_score numeric(78, 0) NOT NULL,
block_deadline bigint NOT NULL,
simulation_block bigint NOT NULL
);

-- Populated after block finalization via transactionReceipt.
Expand All @@ -61,14 +62,15 @@ TRUNCATE auction_participants;
TRUNCATE settlement_scores;
TRUNCATE settlement_observations;


INSERT INTO settlements (block_number, log_index, solver, tx_hash, tx_from, tx_nonce)
VALUES (1, 0, '\x5111111111111111111111111111111111111111'::bytea, '\x7111'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 1),
(2, 0, '\x5222222222222222222222222222222222222222'::bytea, '\x7222'::bytea, '\x5222222222222222222222222222222222222222'::bytea, 1),
(5, 0, '\x5111111111111111111111111111111111111111'::bytea, '\x7333'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 2),
VALUES (1, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7111'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 1),
(2, 10, '\x5222222222222222222222222222222222222222'::bytea, '\x7222'::bytea, '\x5222222222222222222222222222222222222222'::bytea, 1),
(5, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7333'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 2),
-- would the following entry be in the data base? (submitted too late) -- YES
(20, 0, '\x5111111111111111111111111111111111111111'::bytea, '\x7444'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 3),
(25, 0, '\x5111111111111111111111111111111111111111'::bytea, '\x7555'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 4),
(26, 0, '\x5111111111111111111111111111111111111111'::bytea, '\x7666'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 6);
(20, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7444'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 3),
(25, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7555'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 4),
(26, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7666'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 6);

INSERT INTO auction_transaction (auction_id, tx_from, tx_nonce)
VALUES (1, '\x5111111111111111111111111111111111111111'::bytea, 1),
Expand Down Expand Up @@ -104,23 +106,23 @@ VALUES (1, '\x5222222222222222222222222222222222222222'::bytea),
(10, '\x5444444444444444444444444444444444444444'::bytea),
(10, '\x5333333333333333333333333333333333333333'::bytea);

INSERT INTO settlement_scores (auction_id, block_deadline, winning_score, reference_score, winner)
VALUES (1, 10, 5000000000000000000, 4000000000000000000, '\x5111111111111111111111111111111111111111'::bytea),
(2, 11, 6000000000000000000, 3000000000000000000, '\x5222222222222222222222222222222222222222'::bytea),
(3, 12, 21000000000000000000, 3000000000000000000, '\x5111111111111111111111111111111111111111'::bytea),
(5, 14, 5000000000000000000, 3000000000000000000, '\x5111111111111111111111111111111111111111'::bytea), -- jump in auction id
(6, 15, 10000000000000000000, 9000000000000000000, '\x5111111111111111111111111111111111111111'::bytea), -- settled too late
(7, 30, 5000000000000000000, 0, '\x5111111111111111111111111111111111111111'::bytea), -- no competition
(8, 35, 5000000000000000000, 0, '\x5111111111111111111111111111111111111111'::bytea), -- no competition, failed transaction
(9, 36, 5000000000000000000, 1000000000000000000, '\x5111111111111111111111111111111111111111'::bytea), -- score larger than quality
(10, 37, 5000000000000000000, 4000000000000000000, '\x5333333333333333333333333333333333333333'::bytea); -- participant with net negative payment
INSERT INTO settlement_scores (auction_id, winning_score, reference_score, winner, block_deadline, simulation_block)
VALUES (1, 5000000000000000000, 4000000000000000000, '\x5111111111111111111111111111111111111111'::bytea, 10, 0),
(2, 6000000000000000000, 3000000000000000000, '\x5222222222222222222222222222222222222222'::bytea, 11, 1),
(3, 21000000000000000000, 3000000000000000000, '\x5111111111111111111111111111111111111111'::bytea, 12, 2),
(5, 5000000000000000000, 3000000000000000000, '\x5111111111111111111111111111111111111111'::bytea, 14, 4), -- jump in auction id
(6, 10000000000000000000, 9000000000000000000, '\x5111111111111111111111111111111111111111'::bytea, 15, 5), -- settled too late
(7, 5000000000000000000, 0, '\x5111111111111111111111111111111111111111'::bytea, 30, 6), -- no competition
(8, 5000000000000000000, 0, '\x5111111111111111111111111111111111111111'::bytea, 35, 7), -- no competition, failed transaction
(9, 5000000000000000000, 1000000000000000000, '\x5111111111111111111111111111111111111111'::bytea, 36, 8), -- score larger than quality
(10, 5000000000000000000, 4000000000000000000, '\x5333333333333333333333333333333333333333'::bytea, 37, 9); -- participant with net negative payment

INSERT INTO settlement_observations (block_number, log_index, gas_used, effective_gas_price, surplus, fee)
VALUES (1, 0, 100000, 2000000000, 6000000000000000000, 200000000000000),
(2, 0, 150000, 3000000000, 12000000000000000000, 400000000000000),
(5, 0, 100000, 2000000000, 5000000000000000000, 250000000000000),
VALUES (1, 10, 100000, 2000000000, 6000000000000000000, 200000000000000),
(2, 10, 150000, 3000000000, 12000000000000000000, 400000000000000),
(5, 10, 100000, 2000000000, 5000000000000000000, 250000000000000),
-- Depends on backend (setting surplus and fee to zero).
-- I would prefer to use the real numbers. What is backend gonna do.
(20, 0, 100000, 2000000000, 0, 0), -- would that entry be in the data base? (submitted too late)
(25, 0, 100000, 2000000000, 6000000000000000000, 200000000000000),
(26, 0, 100000, 2000000000, 0, 400000000000000);
(20, 10, 100000, 2000000000, 0, 0), -- would that entry be in the data base? (submitted too late)
(25, 10, 100000, 2000000000, 6000000000000000000, 200000000000000),
(26, 10, 100000, 2000000000, 0, 400000000000000);
67 changes: 67 additions & 0 deletions tests/queries/quote_rewards_test_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
DROP TABLE IF EXISTS orders;
DROP TYPE IF EXISTS OrderKind;
DROP TABLE IF EXISTS order_quotes;
DROP TABLE IF EXISTS trades;


-- orders table
CREATE TYPE OrderKind AS ENUM ('buy', 'sell');

CREATE TABLE IF NOT EXISTS orders
(
uid bytea PRIMARY KEY,
sell_amount numeric(78,0) NOT NULL,
buy_amount numeric(78,0) NOT NULL,
fee_amount numeric(78,0) NOT NULL,
kind OrderKind NOT NULL,
partially_fillable boolean NOT NULL
);

CREATE TABLE IF NOT EXISTS order_quotes
(
order_uid bytea PRIMARY KEY,
sell_amount numeric(78, 0) NOT NULL,
buy_amount numeric(78, 0) NOT NULL,
solver bytea NOT NULL
);

CREATE TABLE IF NOT EXISTS trades
(
block_number bigint NOT NULL,
log_index bigint NOT NULL,
order_uid bytea NOT NULL,
sell_amount numeric(78, 0) NOT NULL,
buy_amount numeric(78, 0) NOT NULL,

PRIMARY KEY (block_number, log_index)
);


TRUNCATE orders;
TRUNCATE order_quotes;
TRUNCATE trades;


INSERT INTO orders (uid, sell_amount, buy_amount, fee_amount, kind, partially_fillable)
VALUES ('\x01'::bytea, 95000000, 94000000000000000000, 5000000, 'sell', 'f'), -- normal sell market order
('\x02'::bytea, 101000000, 100000000000000000000, 5000000, 'buy', 'f'), -- normal buy market order
('\x03'::bytea, 100000000, 100000000000000000000, 0, 'sell', 't'), -- partially fillable sell limit order
('\x04'::bytea, 100000000, 100000000000000000000, 0, 'buy', 't'), -- partially fillable buy limit order
('\x05'::bytea, 100000000, 94000000000000000000, 0, 'sell', 'f'), -- in market sell limit order
('\x06'::bytea, 106000000, 100000000000000000000, 0, 'buy', 'f'); -- in market buy limit order

INSERT INTO order_quotes (order_uid, sell_amount, buy_amount, solver)
Copy link
Contributor

@harisang harisang Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order_quotes table does have a fee entry but i am fine with assuming there is none here and instead treat sell_amount as the total amount users need to send to the contract, according to the quote.

Copy link
Collaborator Author

@fhenneke fhenneke Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, the order_quotes table does not have a fee entry. It does have gas_amount, gas_price, and sellt_token_price but due to hooks this is not enough to recover fees.

For market orders we can use the amount in the orders or trades table. For limit orders I am not sure what to do. Is there a way to recover the estimated fee amount or does quoting maybe work differently for such orders?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you are right, it is implicit.

VALUES ('\x01'::bytea, 95000000, 95000000000000000000, '\x01'::bytea),
('\x02'::bytea, 101000000, 100000000000000000000, '\x02'::bytea),
('\x03'::bytea, 100000000, 95000000000000000000, '\x03'::bytea),
('\x04'::bytea, 105000000, 100000000000000000000, '\x03'::bytea),
('\x05'::bytea, 100000000, 95000000000000000000, '\x03'::bytea),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's look at order \x05. I think that we should have order_quotes.sell_amount < orders.sell_amount, as the quote takes into account the fee.

Copy link
Collaborator Author

@fhenneke fhenneke Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked our database and it seems that for limit orders, the sell amount is not reduced by some fee amount. E.g.
select * from orders where uid = '\xD3C1CE97725452DFF58BDC755DDE48E0A7420046075E092BCBC2F397484A287DBDBCE09CF017D8BD51C88B988297B594BD2D3F1065A5E83A' and select * from order_quotes where order_uid = '\xD3C1CE97725452DFF58BDC755DDE48E0A7420046075E092BCBC2F397484A287DBDBCE09CF017D8BD51C88B988297B594BD2D3F1065A5E83A' both return the same sell amount for some recent order.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you are probably right, although I find this a bit confusing and want to look into how the order_quotes table is created. Somewhat irrelevant to this PR though

('\x06'::bytea, 105000000, 100000000000000000000, '\x03'::bytea);

INSERT INTO trades (block_number, log_index, order_uid, sell_amount, buy_amount)
VALUES (1, 0, '\x01'::bytea, 100000000, 95000000000000000000),
(2, 0, '\x02'::bytea, 106000000, 100000000000000000000),
(3, 0, '\x03'::bytea, 100000000, 101000000000000000000),
(4, 0, '\x04'::bytea, 99000000, 100000000000000000000),
(5, 0, '\x05'::bytea, 100000000, 95000000000000000000),
(6, 0, '\x06'::bytea, 105000000, 100000000000000000000);
4 changes: 3 additions & 1 deletion tests/queries/test_batch_rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class TestBatchRewards(unittest.TestCase):
def setUp(self) -> None:
db_url = "postgres:postgres@localhost:5432/postgres"
self.fetcher = MultiInstanceDBFetcher([db_url])
with open("./populate_cip_20.sql", "r", encoding="utf-8") as file:
with open(
"./tests/queries/batch_rewards_test_db.sql", "r", encoding="utf-8"
) as file:
self.fetcher.connections[0].execute(file.read())

def test_get_batch_rewards(self):
Expand Down
39 changes: 39 additions & 0 deletions tests/queries/test_quote_rewards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest

import pandas.testing
from pandas import DataFrame

from src.pg_client import MultiInstanceDBFetcher


class TestQuoteRewards(unittest.TestCase):
def setUp(self) -> None:
db_url = "postgres:postgres@localhost:5432/postgres"
self.fetcher = MultiInstanceDBFetcher([db_url])
with open(
"./tests/queries/quote_rewards_test_db.sql", "r", encoding="utf-8"
) as file:
self.fetcher.connections[0].execute(file.read())

def test_get_quote_rewards(self):
start_block, end_block = "0", "100"
quote_rewards = self.fetcher.get_quote_rewards(start_block, end_block)
expected = DataFrame(
{
"solver": [
"0x01",
"0x02",
"0x03",
],
"num_quotes": [
1,
1,
2,
],
}
)
self.assertIsNone(pandas.testing.assert_frame_equal(expected, quote_rewards))


if __name__ == "__main__":
unittest.main()
Loading