Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Add quote information for trades #83

Merged
merged 16 commits into from
Feb 12, 2024
4 changes: 4 additions & 0 deletions src/models/order_rewards_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def from_pdf_to_dune_records(cls, rewards_df: DataFrame) -> list[dict[str, Any]]
"protocol_fee_native_price": float(
row["protocol_fee_native_price"]
),
"quote_sell_amount": str(row["quote_sell_amount"]),
"quote_buy_amount": str(row["quote_buy_amount"]),
"quote_gas_cost": float(row["quote_gas_cost"]),
"quote_sell_token_price": float(row["quote_sell_token_price"]),
},
}
for row in rewards_df.to_dict(orient="records")
Expand Down
11 changes: 8 additions & 3 deletions src/sql/orderbook/order_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ winning_quotes as (
select
trade_hashes.block_number as block_number,
concat('0x', encode(trade_hashes.order_uid, 'hex')) as order_uid,
concat('0x', encode(solver, 'hex')) as solver,
concat('0x', encode(trade_hashes.solver, 'hex')) as solver,
quote_solver,
concat('0x', encode(tx_hash, 'hex')) as tx_hash,
coalesce(surplus_fee, 0) :: text as surplus_fee,
Expand All @@ -156,11 +156,16 @@ select
CASE
WHEN protocol_fee_token is not NULL THEN concat('0x', encode(protocol_fee_token, 'hex'))
END as protocol_fee_token,
coalesce(protocol_fee_native_price, 0.0) as protocol_fee_native_price
coalesce(protocol_fee_native_price, 0.0) as protocol_fee_native_price,
cast(oq.sell_amount as numeric(78, 0)) :: text as quote_sell_amount,
cast(oq.buy_amount as numeric(78, 0)) :: text as quote_buy_amount,
oq.gas_amount * oq.gas_price as quote_gas_cost,
oq.sell_token_price as quote_sell_token_price
from
trade_hashes
left outer join order_execution o on trade_hashes.order_uid = o.order_uid
and trade_hashes.auction_id = o.auction_id
left outer join winning_quotes wq on trade_hashes.order_uid = wq.order_uid
left outer join order_protocol_fee_prices opfp on trade_hashes.order_uid = opfp.order_uid
and trade_hashes.auction_id = opfp.auction_id;
and trade_hashes.auction_id = opfp.auction_id
left outer join order_quotes oq on trade_hashes.order_uid = oq.order_uid
36 changes: 36 additions & 0 deletions tests/integration/test_fetch_orderbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,42 @@ def test_get_order_rewards(self):
0.0,
0.0,
],
"quote_sell_amount": [
"4429378850",
"184573231326057476",
"7500000000",
"192465488420922607",
"1954467549",
"856446990599561516",
"71274765128486370400",
],
"quote_buy_amount": [
"12009545997505395163",
"143919269475547068",
"1920099071497969074176",
"110185117837140289",
"427611951239582233762",
"1465179606977149402",
"69200861384935994787",
],
"quote_gas_cost": [
28980049962721130.0,
15426768673942524.0,
18282786590639504.0,
18696459349125884.0,
18629380517476576.0,
23553009400438484.0,
25234871513629598.0,
],
"quote_sell_token_price": [
410359364.5808475,
1.0,
408619711.8704729,
0.538163462286757,
409145130.1194029,
1.0,
1.0,
],
}
)

Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_order_rewards_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def test_order_rewards_transformation(self):
"protocol_fee": [1000000000000000, 123123123123123, 0],
"protocol_fee_token": ["0x91", "0x92", None],
"protocol_fee_native_price": [1.0, 0.1, 0.0],
"quote_sell_amount": [10000000000000000, 2000000000000000, 35000],
"quote_buy_amount": [1000, 2000, 10],
"quote_gas_cost": [
5000000000000000.15,
6000000000000000,
12000000000000000,
],
"quote_sell_token_price": [1.0, 250000000, 100000000000000.0],
}
)

Expand All @@ -36,6 +44,10 @@ def test_order_rewards_transformation(self):
"protocol_fee": "1000000000000000",
"protocol_fee_token": "0x91",
"protocol_fee_native_price": 1.0,
"quote_sell_amount": "10000000000000000",
"quote_buy_amount": "1000",
"quote_gas_cost": 5000000000000000.15,
"quote_sell_token_price": 1.0,
},
},
{
Expand All @@ -50,6 +62,10 @@ def test_order_rewards_transformation(self):
"protocol_fee": "123123123123123",
"protocol_fee_token": "0x92",
"protocol_fee_native_price": 0.1,
"quote_sell_amount": "2000000000000000",
"quote_buy_amount": "2000",
"quote_gas_cost": 6000000000000000,
"quote_sell_token_price": 250000000,
},
},
{
Expand All @@ -64,6 +80,10 @@ def test_order_rewards_transformation(self):
"protocol_fee": "0",
"protocol_fee_token": None,
"protocol_fee_native_price": 0.0,
"quote_sell_amount": "35000",
"quote_buy_amount": "10",
"quote_gas_cost": 12000000000000000,
"quote_sell_token_price": 100000000000000.0,
},
},
],
Expand Down
Loading