Skip to content

Commit

Permalink
Merge branch 'update_tests' into protocol_fee
Browse files Browse the repository at this point in the history
  • Loading branch information
fhenneke committed Jan 15, 2024
2 parents da2c4c4 + 35b96ad commit 6a09ceb
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 103 deletions.
93 changes: 0 additions & 93 deletions populate_test_db.sql → tests/queries/batch_rewards_test_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ DROP TABLE IF EXISTS auction_transaction;
DROP TABLE IF EXISTS auction_participants;
DROP TABLE IF EXISTS settlement_scores;
DROP TABLE IF EXISTS settlement_observations;
DROP TABLE IF EXISTS orders;
DROP TYPE IF EXISTS OrderKind;
DROP TYPE IF EXISTS SigningScheme;
DROP TYPE IF EXISTS TokenBalance;
DROP TYPE IF EXISTS OrderClass;
DROP TABLE IF EXISTS order_quotes;
DROP TABLE IF EXISTS trades;

CREATE TABLE IF NOT EXISTS settlements
(
Expand Down Expand Up @@ -62,68 +55,12 @@ CREATE TABLE IF NOT EXISTS settlement_observations
PRIMARY KEY (block_number, log_index)
);

-- orders table
CREATE TYPE OrderKind AS ENUM ('buy', 'sell');
CREATE TYPE SigningScheme AS ENUM ('presign', 'eip712', 'ethsign');
CREATE TYPE TokenBalance AS ENUM ('erc20', 'external');
CREATE TYPE OrderClass AS ENUM ('market', 'limit');

CREATE TABLE orders (
uid bytea PRIMARY KEY,
owner bytea NOT NULL,
creation_timestamp timestamptz NOT NULL,
sell_token bytea NOT NULL,
buy_token bytea NOT NULL,
sell_amount numeric(78,0) NOT NULL,
buy_amount numeric(78,0) NOT NULL,
valid_to bigint NOT NULL,
fee_amount numeric(78,0) NOT NULL,
kind OrderKind NOT NULL,
partially_fillable boolean NOT NULL,
signature bytea NOT NULL,
cancellation_timestamp timestamptz,
receiver bytea NOT NULL,
app_data bytea NOT NULL,
signing_scheme SigningScheme NOT NULL,
settlement_contract bytea NOT NULL,
sell_token_balance TokenBalance NOT NULL,
buy_token_balance TokenBalance NOT NULL,
full_fee_amount numeric(78,0) NOT NULL,
class OrderClass NOT NULL
);

CREATE TABLE IF NOT EXISTS order_quotes
(
order_uid bytea PRIMARY KEY,
gas_amount double precision NOT NULL,
gas_price double precision NOT NULL,
sell_token_price double precision NOT NULL,
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,
fee_amount numeric(78, 0) NOT NULL,

PRIMARY KEY (block_number, log_index)
);


TRUNCATE settlements;
TRUNCATE auction_transaction;
TRUNCATE auction_participants;
TRUNCATE settlement_scores;
TRUNCATE settlement_observations;
TRUNCATE orders;
TRUNCATE order_quotes;
TRUNCATE trades;


INSERT INTO settlements (block_number, log_index, solver, tx_hash, tx_from, tx_nonce)
Expand Down Expand Up @@ -189,33 +126,3 @@ VALUES (1, 10, 100000, 2000000000, 6000000000000000000, 200000000000000),
(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);

INSERT INTO orders (uid, owner, creation_timestamp, sell_token, buy_token, sell_amount, buy_amount, valid_to, fee_amount, kind, partially_fillable, signature, cancellation_timestamp, receiver, app_data, signing_scheme, settlement_contract, sell_token_balance, buy_token_balance, full_fee_amount, class)
VALUES ('\x1111'::bytea, '\x1111111111'::bytea, '2024-01-01 00:00:00.000000+00
', '\x11'::bytea, '\x22'::bytea, 6000000000000000000, 4000000000000000000, 1655195621, 100000000000000000, 'sell', 'f', '\x987987987987'::bytea, NULL, '\x12341234'::bytea, '\x1234512345'::bytea, 'presign', '\x123456123456'::bytea, 'erc20', 'external', 200000000000000000, 'market'), -- normal sell market order
('\x2222'::bytea, '\x1111111111'::bytea, '2024-01-01 00:00:00.000000+00
', '\x11'::bytea, '\x22'::bytea, 6000000000000000000, 4000000000000000000, 1655195621, 100000000000000000, 'buy', 'f', '\x987987987987'::bytea, NULL, '\x12341234'::bytea, '\x1234512345'::bytea, 'presign', '\x123456123456'::bytea, 'erc20', 'external', 200000000000000000, 'market'), -- normal buy market order
('\x3333'::bytea, '\x1111111111'::bytea, '2024-01-01 00:00:00.000000+00
', '\x11'::bytea, '\x22'::bytea, 6000000000000000000, 4000000000000000000, 1655195621, 100000000000000000, 'sell', 't', '\x987987987987'::bytea, NULL, '\x12341234'::bytea, '\x1234512345'::bytea, 'presign', '\x123456123456'::bytea, 'erc20', 'external', 200000000000000000, 'limit'), -- partially fillable sell limit order
('\x4444'::bytea, '\x1111111111'::bytea, '2024-01-01 00:00:00.000000+00
', '\x11'::bytea, '\x22'::bytea, 6000000000000000000, 4000000000000000000, 1655195621, 0, 'buy', 't', '\x987987987987'::bytea, NULL, '\x12341234'::bytea, '\x1234512345'::bytea, 'presign', '\x123456123456'::bytea, 'erc20', 'external', 0, 'limit'), -- partially fillable buy limit order
('\x5555'::bytea, '\x1111111111'::bytea, '2024-01-01 00:00:00.000000+00
', '\x11'::bytea, '\x22'::bytea, 6000000000000000000, 4000000000000000000, 1655195621, 0, 'sell', 'f', '\x987987987987'::bytea, NULL, '\x12341234'::bytea, '\x1234512345'::bytea, 'presign', '\x123456123456'::bytea, 'erc20', 'external', 0, 'limit'), -- in market sell limit order
('\x6666'::bytea, '\x1111111111'::bytea, '2024-01-01 00:00:00.000000+00
', '\x11'::bytea, '\x22'::bytea, 6000000000000000000, 4000000000000000000, 1655195621, 0, 'buy', 'f', '\x987987987987'::bytea, NULL, '\x12341234'::bytea, '\x1234512345'::bytea, 'presign', '\x123456123456'::bytea, 'erc20', 'external', 0, 'limit'); -- in market buy limit order

INSERT INTO order_quotes (order_uid, gas_amount, gas_price, sell_token_price, sell_amount, buy_amount, solver)
VALUES ('\x1111'::bytea, 200000, 110000000000, 0.123, 6000000000000000000, 4500000000000000000, '\x5111111111111111111111111111111111111111'::bytea),
('\x2222'::bytea, 200000, 110000000000, 0.123, 5500000000000000000, 4000000000000000000, '\x5333333333333333333333333333333333333333'::bytea),
('\x3333'::bytea, 200000, 110000000000, 0.123, 6000000000000000000, 3000000000000000000, '\x5222222222222222222222222222222222222222'::bytea),
('\x4444'::bytea, 200000, 110000000000, 0.123, 7000000000000000000, 4000000000000000000, '\x5222222222222222222222222222222222222222'::bytea),
('\x5555'::bytea, 200000, 110000000000, 0.123, 6000000000000000000, 4500000000000000000, '\x5333333333333333333333333333333333333333'::bytea),
('\x6666'::bytea, 200000, 110000000000, 0.123, 5500000000000000000, 4000000000000000000, '\x5444444444444444444444444444444444444444'::bytea);

INSERT INTO trades (block_number, log_index, order_uid, sell_amount, buy_amount, fee_amount)
VALUES (1, 0, '\x1111'::bytea, 6200000000000000000, 4600000000000000000, 200000000000000000),
(2, 0, '\x2222'::bytea, 5600000000000000000, 4000000000000000000, 200000000000000000),
(2, 1, '\x3333'::bytea, 6200000000000000000, 4600000000000000000, 200000000000000000),
(5, 0, '\x4444'::bytea, 5600000000000000000, 4000000000000000000, 200000000000000000),
(5, 1, '\x5555'::bytea, 6200000000000000000, 4600000000000000000, 200000000000000000),
(20, 0, '\x6666'::bytea, 5600000000000000000, 4000000000000000000, 200000000000000000);
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)
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),
('\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_test_db.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
20 changes: 11 additions & 9 deletions tests/queries/test_quote_rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,33 @@
from src.pg_client import MultiInstanceDBFetcher


class TestBatchRewards(unittest.TestCase):
class TestQuoteRewards(unittest.TestCase):
def setUp(self) -> None:
db_url = "postgres:postgres@localhost:5432/postgres"
self.fetcher = MultiInstanceDBFetcher([db_url])
with open("./populate_test_db.sql", "r", encoding="utf-8") as file:
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_batch_rewards(self):
def test_get_quote_rewards(self):
start_block, end_block = "0", "100"
batch_rewards = self.fetcher.get_quote_rewards(start_block, end_block)
quote_rewards = self.fetcher.get_quote_rewards(start_block, end_block)
expected = DataFrame(
{
"solver": [
"0x5111111111111111111111111111111111111111",
"0x5333333333333333333333333333333333333333",
"0x5444444444444444444444444444444444444444",
"0x01",
"0x02",
"0x03",
],
"num_quotes": [
1,
2,
1,
2,
],
}
)
self.assertIsNone(pandas.testing.assert_frame_equal(expected, batch_rewards))
self.assertIsNone(pandas.testing.assert_frame_equal(expected, quote_rewards))


if __name__ == "__main__":
Expand Down

0 comments on commit 6a09ceb

Please sign in to comment.