Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR changes the computation of network fees for solver payments. This change is required due to a change to how the
fee
entry in thesettlement_observations
table is computed, see cowprotocol/services#2956.Network fee computation
With this PR, network fees are computed using data of executed trades. The
surplus_fee
computed inorder_executions
is always such thatwith uniform clearing prices.
If it were not for protocol fees and network fees, the order would trade at uniform clearing price.
Combining it with the formula for surplus_fee gives
Traded amounts and surplus_fee are stored in the database. Protocol fees can be computed independently. Thus we can rearange the formula for
network_fee
,If the protocol fee is charged only in the sell token (i.e. for buy orders), this gives
If the protocol fee is charged in the buy token (i.e. for sell orders), this gives
These formulas are implemented now.
Additional changes
There were other minor changes required to make this work.
I realized that there are edge cases where a jit order is supposed to charge a fee but the price of the sell token is not available. In that case, with this PR and the old code, the network fee of such a trad would have been set to zero.
Test plan
I adapted unit tests. Network fee amounts changed a bit since before the computation depended on the settlement_observations table and not it depends on order_executions. Those tables are not consistent in that the fee entry does correspond to what the autopilot would compute when observing data stored in order_executions. The order of magnitude of the new results is easy to check as the difference of the sum of surplus fees (converted to ETH) and the sum of protocol fees. So the new tests seem correct.
I ran the query on the one-day period 2024-09-16--2024-09-17 and compared old and new network fees. They were identical up to rounding errors.
I ran the query on the one-day period 2024-09-17--2024-09-18 and compared old and new network fees. They were different, as is to be expected.
I looked into transaction with hash
0xcca1897297913e58fa9fae51c9b8f93c4f6c4aebce0b7a6a5fbceaff309ef37e
in particular since that trade had negative network fees with the old query. The new query gives numbers consistent with our new Dune queries.I also ran the full payment script for the accounting period 2024-09-10--2024-09-17 (commenting out slippage, which seems to have a bug). The only difference in rewards, protocol fees, and partner fees is that the actual payment reported a protocol fee of 27.8759 ETH while the new run gives 27.8662 ETH. There should be no difference. So there might be some small bug in the code somewhere, potentially ignoring some trade or converting using a wrong price.