Skip to content

Commit

Permalink
Merge pull request #92 from cowprotocol/price_improvement
Browse files Browse the repository at this point in the history
[Mirror] Add price improvement protocol fee
  • Loading branch information
harisang authored Apr 1, 2024
2 parents cab1325 + c345a01 commit e52493e
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 44 deletions.
42 changes: 37 additions & 5 deletions src/sql/orderbook/barn_batch_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ order_surplus AS (
WHEN o.kind = 'sell' THEN t.buy_amount - t.sell_amount * o.buy_amount / (o.sell_amount + o.fee_amount)
WHEN o.kind = 'buy' THEN t.buy_amount * (o.sell_amount + o.fee_amount) / o.buy_amount - t.sell_amount
END AS surplus,
CASE
WHEN o.kind = 'sell' THEN t.buy_amount - t.sell_amount * (oq.buy_amount - oq.buy_amount / oq.sell_amount * oq.gas_amount * oq.gas_price / oq.sell_token_price) / oq.sell_amount
WHEN o.kind = 'buy' THEN t.buy_amount * (oq.sell_amount + oq.gas_amount * oq.gas_price / oq.sell_token_price) / oq.buy_amount - t.sell_amount
END AS price_improvement,
CASE
WHEN o.kind = 'sell' THEN o.buy_token
WHEN o.kind = 'buy' THEN o.sell_token
Expand All @@ -67,6 +71,8 @@ order_surplus AS (
JOIN order_execution oe -- contains surplus fee
ON t.order_uid = oe.order_uid
AND s.auction_id = oe.auction_id
LEFT OUTER JOIN order_quotes oq -- contains quote amounts
ON o.uid = oq.order_uid
WHERE
ss.block_deadline > {{start_block}}
AND ss.block_deadline <= {{end_block}}
Expand All @@ -76,6 +82,7 @@ order_protocol_fee AS (
os.auction_id,
os.solver,
os.tx_hash,
os.order_uid,
os.sell_amount,
os.buy_amount,
os.sell_token,
Expand All @@ -100,6 +107,28 @@ order_protocol_fee AS (
fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus
)
END
WHEN fp.kind = 'priceimprovement' THEN CASE
WHEN os.kind = 'sell' THEN
LEAST(
-- at most charge a fraction of volume
fp.price_improvement_max_volume_factor / (1 - fp.price_improvement_max_volume_factor) * os.buy_amount,
-- charge a fraction of price improvement, at most 0
GREATEST(
fp.price_improvement_factor / (1 - fp.price_improvement_factor) * price_improvement
,
0
)
)
WHEN os.kind = 'buy' THEN LEAST(
-- at most charge a fraction of volume
fp.price_improvement_max_volume_factor / (1 + fp.price_improvement_max_volume_factor) * os.sell_amount,
-- charge a fraction of price improvement
GREATEST(
fp.price_improvement_factor / (1 - fp.price_improvement_factor) * price_improvement,
0
)
)
END
WHEN fp.kind = 'volume' THEN CASE
WHEN os.kind = 'sell' THEN
fp.volume_factor / (1 - fp.volume_factor) * os.buy_amount
Expand All @@ -116,18 +145,21 @@ order_protocol_fee AS (
),
order_protocol_fee_prices AS (
SELECT
opf.auction_id,
opf.solver,
opf.tx_hash,
opf.order_uid,
opf.surplus,
opf.protocol_fee,
opf.protocol_fee_token,
CASE
WHEN opf.sell_token != opf.protocol_fee_token THEN (opf.sell_amount - opf.observed_fee) / opf.buy_amount * opf.protocol_fee
ELSE opf.protocol_fee
END AS network_fee_correction,
opf.sell_token as network_fee_token,
ap_surplus.price / pow(10, 18) as surplus_token_price,
ap_protocol.price / pow(10, 18) as protocol_fee_token_price,
ap_sell.price / pow(10, 18) as network_fee_token_price
ap_surplus.price / pow(10, 18) as surplus_token_native_price,
ap_protocol.price / pow(10, 18) as protocol_fee_token_native_price,
ap_sell.price / pow(10, 18) as network_fee_token_native_price
FROM
order_protocol_fee opf
JOIN auction_prices ap_sell -- contains price: sell token
Expand All @@ -145,8 +177,8 @@ batch_protocol_fees AS (
solver,
tx_hash,
-- sum(surplus * surplus_token_price) as surplus,
sum(protocol_fee * protocol_fee_token_price) as protocol_fee,
sum(network_fee_correction * network_fee_token_price) as network_fee_correction
sum(protocol_fee * protocol_fee_token_native_price) as protocol_fee,
sum(network_fee_correction * network_fee_token_native_price) as network_fee_correction
FROM
order_protocol_fee_prices
group by
Expand Down
86 changes: 69 additions & 17 deletions src/sql/orderbook/barn_order_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ with trade_hashes as (
),
order_surplus AS (
SELECT
ss.winner as solver,
s.auction_id,
s.tx_hash,
t.order_uid,
o.sell_token,
o.buy_token,
Expand All @@ -47,12 +49,16 @@ order_surplus AS (
WHEN o.kind = 'sell' THEN t.buy_amount - t.sell_amount * o.buy_amount / (o.sell_amount + o.fee_amount)
WHEN o.kind = 'buy' THEN t.buy_amount * (o.sell_amount + o.fee_amount) / o.buy_amount - t.sell_amount
END AS surplus,
CASE
WHEN o.kind = 'sell' THEN t.buy_amount - t.sell_amount * (oq.buy_amount - oq.buy_amount / oq.sell_amount * oq.gas_amount * oq.gas_price / oq.sell_token_price) / oq.sell_amount
WHEN o.kind = 'buy' THEN t.buy_amount * (oq.sell_amount + oq.gas_amount * oq.gas_price / oq.sell_token_price) / oq.buy_amount - t.sell_amount
END AS price_improvement,
CASE
WHEN o.kind = 'sell' THEN o.buy_token
WHEN o.kind = 'buy' THEN o.sell_token
END AS surplus_token
FROM
settlements s -- links block_number and log_index to tx_from and tx_nonce
settlements s
JOIN settlement_scores ss -- contains block_deadline
ON s.auction_id = ss.auction_id
JOIN trades t -- contains traded amounts
Expand All @@ -62,13 +68,17 @@ order_surplus AS (
JOIN order_execution oe -- contains surplus fee
ON t.order_uid = oe.order_uid
AND s.auction_id = oe.auction_id
LEFT OUTER JOIN order_quotes oq -- contains quote amounts
ON o.uid = oq.order_uid
WHERE
s.block_number > {{start_block}}
AND s.block_number <= {{end_block}}
ss.block_deadline >= {{start_block}}
AND ss.block_deadline <= {{end_block}}
),
order_protocol_fee AS (
SELECT
os.auction_id,
os.solver,
os.tx_hash,
os.order_uid,
os.sell_amount,
os.buy_amount,
Expand All @@ -84,20 +94,46 @@ order_protocol_fee AS (
-- impossible anyways. This query will return a division by
-- zero error in that case.
LEAST(
fp.surplus_max_volume_factor * os.sell_amount * os.buy_amount / (os.sell_amount - os.observed_fee), -- at most charge a fraction of volume
fp.surplus_max_volume_factor / (1 - fp.surplus_max_volume_factor) * os.buy_amount,
-- at most charge a fraction of volume
fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus
)
WHEN os.kind = 'buy' THEN LEAST(
fp.surplus_max_volume_factor / (1 + fp.surplus_max_volume_factor) * os.sell_amount, -- at most charge a fraction of volume
fp.surplus_max_volume_factor / (1 + fp.surplus_max_volume_factor) * os.sell_amount,
-- at most charge a fraction of volume
fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus
)
END
WHEN fp.kind = 'volume' THEN fp.volume_factor / (1 - fp.volume_factor) * os.sell_amount
WHEN fp.kind = 'priceimprovement' THEN CASE
WHEN os.kind = 'sell' THEN
LEAST(
-- at most charge a fraction of volume
fp.price_improvement_max_volume_factor / (1 - fp.price_improvement_max_volume_factor) * os.buy_amount,
-- charge a fraction of price improvement, at most 0
GREATEST(
fp.price_improvement_factor / (1 - fp.price_improvement_factor) * price_improvement
,
0
)
)
WHEN os.kind = 'buy' THEN LEAST(
-- at most charge a fraction of volume
fp.price_improvement_max_volume_factor / (1 + fp.price_improvement_max_volume_factor) * os.sell_amount,
-- charge a fraction of price improvement
GREATEST(
fp.price_improvement_factor / (1 - fp.price_improvement_factor) * price_improvement,
0
)
)
END
WHEN fp.kind = 'volume' THEN CASE
WHEN os.kind = 'sell' THEN
fp.volume_factor / (1 - fp.volume_factor) * os.buy_amount
WHEN os.kind = 'buy' THEN
fp.volume_factor / (1 + fp.volume_factor) * os.sell_amount
END
END AS protocol_fee,
CASE
WHEN fp.kind = 'surplus' THEN os.surplus_token
WHEN fp.kind = 'volume' THEN os.sell_token
END AS protocol_fee_token
os.surplus_token AS protocol_fee_token
FROM
order_surplus os
JOIN fee_policies fp -- contains protocol fee policy
Expand All @@ -106,16 +142,32 @@ order_protocol_fee AS (
),
order_protocol_fee_prices AS (
SELECT
opf.order_uid,
opf.auction_id,
opf.solver,
opf.tx_hash,
opf.order_uid,
opf.surplus,
opf.protocol_fee,
opf.protocol_fee_token,
ap.price / pow(10, 18) as protocol_fee_native_price
CASE
WHEN opf.sell_token != opf.protocol_fee_token THEN (opf.sell_amount - opf.observed_fee) / opf.buy_amount * opf.protocol_fee
ELSE opf.protocol_fee
END AS network_fee_correction,
opf.sell_token as network_fee_token,
ap_surplus.price / pow(10, 18) as surplus_token_native_price,
ap_protocol.price / pow(10, 18) as protocol_fee_token_native_price,
ap_sell.price / pow(10, 18) as network_fee_token_native_price
FROM
order_protocol_fee opf
JOIN auction_prices ap -- contains price: protocol fee token
ON opf.auction_id = ap.auction_id
AND opf.protocol_fee_token = ap.token
JOIN auction_prices ap_sell -- contains price: sell token
ON opf.auction_id = ap_sell.auction_id
AND opf.sell_token = ap_sell.token
JOIN auction_prices ap_surplus -- contains price: surplus token
ON opf.auction_id = ap_surplus.auction_id
AND opf.surplus_token = ap_surplus.token
JOIN auction_prices ap_protocol -- contains price: protocol fee token
ON opf.auction_id = ap_protocol.auction_id
AND opf.protocol_fee_token = ap_protocol.token
),
winning_quotes as (
SELECT
Expand Down Expand Up @@ -156,7 +208,7 @@ 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_token_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,
Expand All @@ -168,4 +220,4 @@ from
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
left outer join order_quotes oq on trade_hashes.order_uid = oq.order_uid
left outer join order_quotes oq on trade_hashes.order_uid = oq.order_uid
42 changes: 37 additions & 5 deletions src/sql/orderbook/prod_batch_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ order_surplus AS (
WHEN o.kind = 'sell' THEN t.buy_amount - t.sell_amount * o.buy_amount / (o.sell_amount + o.fee_amount)
WHEN o.kind = 'buy' THEN t.buy_amount * (o.sell_amount + o.fee_amount) / o.buy_amount - t.sell_amount
END AS surplus,
CASE
WHEN o.kind = 'sell' THEN t.buy_amount - t.sell_amount * (oq.buy_amount - oq.buy_amount / oq.sell_amount * oq.gas_amount * oq.gas_price / oq.sell_token_price) / oq.sell_amount
WHEN o.kind = 'buy' THEN t.buy_amount * (oq.sell_amount + oq.gas_amount * oq.gas_price / oq.sell_token_price) / oq.buy_amount - t.sell_amount
END AS price_improvement,
CASE
WHEN o.kind = 'sell' THEN o.buy_token
WHEN o.kind = 'buy' THEN o.sell_token
Expand All @@ -67,6 +71,8 @@ order_surplus AS (
JOIN order_execution oe -- contains surplus fee
ON t.order_uid = oe.order_uid
AND s.auction_id = oe.auction_id
LEFT OUTER JOIN order_quotes oq -- contains quote amounts
ON o.uid = oq.order_uid
WHERE
ss.block_deadline > {{start_block}}
AND ss.block_deadline <= {{end_block}}
Expand All @@ -76,6 +82,7 @@ order_protocol_fee AS (
os.auction_id,
os.solver,
os.tx_hash,
os.order_uid,
os.sell_amount,
os.buy_amount,
os.sell_token,
Expand All @@ -100,6 +107,28 @@ order_protocol_fee AS (
fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus
)
END
WHEN fp.kind = 'priceimprovement' THEN CASE
WHEN os.kind = 'sell' THEN
LEAST(
-- at most charge a fraction of volume
fp.price_improvement_max_volume_factor / (1 - fp.price_improvement_max_volume_factor) * os.buy_amount,
-- charge a fraction of price improvement, at most 0
GREATEST(
fp.price_improvement_factor / (1 - fp.price_improvement_factor) * price_improvement
,
0
)
)
WHEN os.kind = 'buy' THEN LEAST(
-- at most charge a fraction of volume
fp.price_improvement_max_volume_factor / (1 + fp.price_improvement_max_volume_factor) * os.sell_amount,
-- charge a fraction of price improvement
GREATEST(
fp.price_improvement_factor / (1 - fp.price_improvement_factor) * price_improvement,
0
)
)
END
WHEN fp.kind = 'volume' THEN CASE
WHEN os.kind = 'sell' THEN
fp.volume_factor / (1 - fp.volume_factor) * os.buy_amount
Expand All @@ -116,18 +145,21 @@ order_protocol_fee AS (
),
order_protocol_fee_prices AS (
SELECT
opf.auction_id,
opf.solver,
opf.tx_hash,
opf.order_uid,
opf.surplus,
opf.protocol_fee,
opf.protocol_fee_token,
CASE
WHEN opf.sell_token != opf.protocol_fee_token THEN (opf.sell_amount - opf.observed_fee) / opf.buy_amount * opf.protocol_fee
ELSE opf.protocol_fee
END AS network_fee_correction,
opf.sell_token as network_fee_token,
ap_surplus.price / pow(10, 18) as surplus_token_price,
ap_protocol.price / pow(10, 18) as protocol_fee_token_price,
ap_sell.price / pow(10, 18) as network_fee_token_price
ap_surplus.price / pow(10, 18) as surplus_token_native_price,
ap_protocol.price / pow(10, 18) as protocol_fee_token_native_price,
ap_sell.price / pow(10, 18) as network_fee_token_native_price
FROM
order_protocol_fee opf
JOIN auction_prices ap_sell -- contains price: sell token
Expand All @@ -145,8 +177,8 @@ batch_protocol_fees AS (
solver,
tx_hash,
-- sum(surplus * surplus_token_price) as surplus,
sum(protocol_fee * protocol_fee_token_price) as protocol_fee,
sum(network_fee_correction * network_fee_token_price) as network_fee_correction
sum(protocol_fee * protocol_fee_token_native_price) as protocol_fee,
sum(network_fee_correction * network_fee_token_native_price) as network_fee_correction
FROM
order_protocol_fee_prices
group by
Expand Down
Loading

0 comments on commit e52493e

Please sign in to comment.