Skip to content

Commit

Permalink
[EASY] Move out fee calculation from "into_solution" (#2662)
Browse files Browse the repository at this point in the history
# Description
Baseline solver. Moves out fee calculation from the `into_solution`
function.

This is done as a preparation for a follow up PR, where I will not
calculate the sell token price, but instead build `native_price_request`
to buy weth in an exact amount needed to cover the gas costs (instead of
using 1ETH as a default buy value). The output sell amount of
`native_price_request` will directly give us the amount of sell token
needed to cover the gas costs - fee.

Related to #2624, where
the `native_price_request` uses 1ETH as buy amount and fails. I don't
see any good reason to use 1ETH here as buy amount, as it is not needed.
It is perfectly fine to use, for example, 0.01ETH amount that actually
covers the gas cost and it's more accurate.
  • Loading branch information
sunce86 authored Apr 26, 2024
1 parent 22fe18e commit 4e06d4d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/solvers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ axum = { workspace = true }
bigdecimal = { version = "0.3", features = ["serde"] }
chrono = { workspace = true, features = ["serde"], default-features = false }
clap = { workspace = true, features = ["derive", "env"] }
derive_more = { workspace = true }
ethereum-types = { workspace = true }
ethrpc = { path = "../ethrpc" }
futures = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/solvers/src/boundary/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ fn to_domain_solution(
match order.solver_determines_fee() {
true => execution
.exec_fee_amount
.map(solution::Fee::Surplus)
.map(|fee| solution::Fee::Surplus(fee.into()))
.context("no surplus fee")?,
false => solution::Fee::Protocol,
},
Expand Down
7 changes: 5 additions & 2 deletions crates/solvers/src/domain/eth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use {crate::util::bytes::Bytes, web3::types::AccessList};

use {crate::util::bytes::Bytes, derive_more::From, web3::types::AccessList};
mod chain;

pub use {
Expand Down Expand Up @@ -45,6 +44,10 @@ impl From<U256> for Ether {
}
}

/// A token amount in wei, always representing the sell token of an order.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, From)]
pub struct SellTokenAmount(pub U256);

/// Like [`Gas`] but can be negative to encode a gas discount.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct SignedGas(i64);
Expand Down
12 changes: 4 additions & 8 deletions crates/solvers/src/domain/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ pub struct Single {
impl Single {
/// Creates a full solution for a single order solution given gas and sell
/// token prices.
pub fn into_solution(
self,
gas_price: auction::GasPrice,
sell_token: auction::Price,
) -> Option<Solution> {
pub fn into_solution(self, fee: eth::SellTokenAmount) -> Option<Solution> {
let Self {
order,
input,
Expand All @@ -144,7 +140,7 @@ impl Single {
}

let fee = if order.solver_determines_fee() {
Fee::Surplus(sell_token.ether_value(eth::Ether(gas.0.checked_mul(gas_price.0 .0)?))?)
Fee::Surplus(fee)
} else {
Fee::Protocol
};
Expand Down Expand Up @@ -299,15 +295,15 @@ pub enum Fee {
Protocol,

/// An additional surplus fee that is charged by the solver.
Surplus(U256),
Surplus(eth::SellTokenAmount),
}

impl Fee {
/// Returns the dynamic component for the fee.
pub fn surplus(&self) -> Option<U256> {
match self {
Fee::Protocol => None,
Fee::Surplus(fee) => Some(*fee),
Fee::Surplus(fee) => Some(fee.0),
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/solvers/src/domain/solver/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ impl Inner {
}

let gas = route.gas() + self.solution_gas_offset;
let fee = sell_token_price
.ether_value(eth::Ether(gas.0.checked_mul(auction.gas_price.0 .0)?))?
.into();

Some(
solution::Single {
Expand All @@ -198,7 +201,7 @@ impl Inner {
interactions,
gas,
}
.into_solution(auction.gas_price, sell_token_price)?
.into_solution(fee)?
.with_id(solution::Id(i as u64))
.with_buffers_internalizations(&auction.tokens),
)
Expand Down

0 comments on commit 4e06d4d

Please sign in to comment.