Skip to content

Commit

Permalink
Merge branch 'main' into api/quote-metadata-in-orders
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrug committed Jan 16, 2025
2 parents e6206ca + 395d605 commit c7efc2d
Show file tree
Hide file tree
Showing 27 changed files with 381 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .github/nitpicks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Reminder: Please consider backward compatibility when modifying the API specification.
If breaking changes are unavoidable, ensure:
- You explicitly pointed out breaking changes.
- You communicate the changes to affected teams.
- You communicate the changes to affected teams (at least Frontend team and SAFE team).
- You provide proper versioning and migration mechanisms.
pathFilter:
- "**/openapi.yml"
2 changes: 1 addition & 1 deletion .github/workflows/hotfix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Check out
uses: actions/checkout@v4
with:
token: "${{ secrets.HOTFIX_ACTION_JOB }}"
token: "${{ secrets.HOTFIX_ACTION_TOKEN }}"
fetch-depth: 0

- name: Configure git
Expand Down
21 changes: 20 additions & 1 deletion crates/app-data/src/app_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{app_data_hash::hash_full_app_data, AppDataHash, Hooks},
anyhow::{anyhow, Context, Result},
primitive_types::H160,
primitive_types::{H160, U256},
serde::{de, Deserialize, Deserializer, Serialize, Serializer},
std::{fmt, fmt::Display},
};
Expand All @@ -24,6 +24,24 @@ pub struct ProtocolAppData {
pub signer: Option<H160>,
pub replaced_order: Option<ReplacedOrder>,
pub partner_fee: Option<PartnerFee>,
pub flashloan: Option<Flashloan>,
}

/// Contains information to hint at how a solver could make
/// use of flashloans to settle the associated order.
/// Since using flashloans introduces a bunch of complexities
/// all these hints are not binding for the solver.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
pub struct Flashloan {
/// Which contract to request the flashloan from.
lender: Option<H160>,
/// Who should receive the borrowed tokens. If this is not
/// set the order owner will get the tokens.
borrower: Option<H160>,
/// Which token to flashloan.
token: H160,
/// How much of the token to flashloan.
amount: U256,
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
Expand Down Expand Up @@ -217,6 +235,7 @@ impl From<BackendAppData> for ProtocolAppData {
signer: None,
replaced_order: None,
partner_fee: None,
flashloan: None,
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions crates/autopilot/src/database/onchain_order_events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ type GeneralOnchainOrderPlacementData = (
OnchainOrderPlacement,
Order,
);
async fn parse_general_onchain_order_placement_data<'a>(
quoter: &'a dyn OrderQuoting,
async fn parse_general_onchain_order_placement_data(
quoter: &'_ dyn OrderQuoting,
order_placement_events_and_quotes_zipped: Vec<(EthContractEvent<ContractEvent>, i64, i64)>,
domain_separator: DomainSeparator,
settlement_contract: H160,
Expand Down Expand Up @@ -616,7 +616,6 @@ fn convert_onchain_order_placement(
settlement_contract: ByteArray(settlement_contract.0),
sell_token_balance: sell_token_source_into(order_data.sell_token_balance),
buy_token_balance: buy_token_destination_into(order_data.buy_token_balance),
full_fee_amount: u256_to_big_decimal(&order_data.fee_amount),
cancellation_timestamp: None,
class: match order_data.fee_amount.is_zero() {
true => OrderClass::Limit,
Expand Down Expand Up @@ -925,7 +924,6 @@ mod test {
settlement_contract: ByteArray(settlement_contract.0),
sell_token_balance: sell_token_source_into(expected_order_data.sell_token_balance),
buy_token_balance: buy_token_destination_into(expected_order_data.buy_token_balance),
full_fee_amount: u256_to_big_decimal(&expected_order_data.fee_amount),
cancellation_timestamp: None,
};
assert_eq!(onchain_order_placement, expected_onchain_order_placement);
Expand Down Expand Up @@ -1036,7 +1034,6 @@ mod test {
settlement_contract: ByteArray(settlement_contract.0),
sell_token_balance: sell_token_source_into(expected_order_data.sell_token_balance),
buy_token_balance: buy_token_destination_into(expected_order_data.buy_token_balance),
full_fee_amount: u256_to_big_decimal(&U256::zero()),
cancellation_timestamp: None,
};
assert_eq!(onchain_order_placement, expected_onchain_order_placement);
Expand Down
2 changes: 1 addition & 1 deletion crates/database/src/jit_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use {

pub const SELECT: &str = r#"
o.uid, o.owner, o.creation_timestamp, o.sell_token, o.buy_token, o.sell_amount, o.buy_amount,
o.valid_to, o.app_data, o.fee_amount, o.fee_amount AS full_fee_amount, o.kind, o.partially_fillable, o.signature,
o.valid_to, o.app_data, o.fee_amount, o.kind, o.partially_fillable, o.signature,
o.receiver, o.signing_scheme, '\x9008d19f58aabd9ed0d60971565aa8510560ab41'::bytea AS settlement_contract, o.sell_token_balance, o.buy_token_balance,
'liquidity'::OrderClass AS class,
(SELECT COALESCE(SUM(t.buy_amount), 0) FROM trades t WHERE t.order_uid = o.uid) AS sum_buy,
Expand Down
13 changes: 2 additions & 11 deletions crates/database/src/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub struct Order {
pub settlement_contract: Address,
pub sell_token_balance: SellTokenSource,
pub buy_token_balance: BuyTokenDestination,
pub full_fee_amount: BigDecimal,
pub cancellation_timestamp: Option<DateTime<Utc>>,
pub class: OrderClass,
}
Expand Down Expand Up @@ -140,11 +139,10 @@ INSERT INTO orders (
settlement_contract,
sell_token_balance,
buy_token_balance,
full_fee_amount,
cancellation_timestamp,
class
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)
"#;

pub async fn insert_order_and_ignore_conflicts(
Expand Down Expand Up @@ -183,7 +181,6 @@ async fn insert_order_execute_sqlx(
.bind(order.settlement_contract)
.bind(order.sell_token_balance)
.bind(order.buy_token_balance)
.bind(&order.full_fee_amount)
.bind(order.cancellation_timestamp)
.bind(order.class)
.execute(ex)
Expand Down Expand Up @@ -473,7 +470,6 @@ pub struct FullOrder {
pub valid_to: i64,
pub app_data: AppId,
pub fee_amount: BigDecimal,
pub full_fee_amount: BigDecimal,
pub kind: OrderKind,
pub class: OrderClass,
pub partially_fillable: bool,
Expand Down Expand Up @@ -547,7 +543,7 @@ impl FullOrder {
// that with the current amount of data this wouldn't be better.
pub const SELECT: &str = r#"
o.uid, o.owner, o.creation_timestamp, o.sell_token, o.buy_token, o.sell_amount, o.buy_amount,
o.valid_to, o.app_data, o.fee_amount, o.full_fee_amount, o.kind, o.partially_fillable, o.signature,
o.valid_to, o.app_data, o.fee_amount, o.kind, o.partially_fillable, o.signature,
o.receiver, o.signing_scheme, o.settlement_contract, o.sell_token_balance, o.buy_token_balance,
o.class,
(SELECT COALESCE(SUM(t.buy_amount), 0) FROM trades t WHERE t.order_uid = o.uid) AS sum_buy,
Expand Down Expand Up @@ -880,7 +876,6 @@ mod tests {
assert_eq!(order.valid_to, full_order.valid_to);
assert_eq!(order.app_data, full_order.app_data);
assert_eq!(order.fee_amount, full_order.fee_amount);
assert_eq!(order.full_fee_amount, full_order.full_fee_amount);
assert_eq!(order.kind, full_order.kind);
assert_eq!(order.class, full_order.class);
assert_eq!(order.partially_fillable, full_order.partially_fillable);
Expand Down Expand Up @@ -1364,10 +1359,6 @@ mod tests {
order_with_quote.full_order.fee_amount,
full_order.fee_amount
);
assert_eq!(
order_with_quote.full_order.full_fee_amount,
full_order.full_fee_amount
);
assert_eq!(order_with_quote.full_order.kind, full_order.kind);
assert_eq!(order_with_quote.full_order.class, full_order.class);
assert_eq!(
Expand Down
20 changes: 18 additions & 2 deletions crates/driver/src/domain/competition/bad_tokens/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use {
super::Quality,
crate::domain::eth,
crate::{
domain::eth,
infra::{observe::metrics, solver},
},
dashmap::DashMap,
std::{
sync::Arc,
Expand All @@ -27,6 +30,7 @@ pub struct Detector {
counter: Arc<DashMap<eth::TokenAddress, TokenStatistics>>,
log_only: bool,
token_freeze_time: Duration,
solver: solver::Name,
}

impl Detector {
Expand All @@ -35,13 +39,15 @@ impl Detector {
required_measurements: u32,
log_only: bool,
token_freeze_time: Duration,
solver: solver::Name,
) -> Self {
Self {
failure_ratio,
required_measurements,
counter: Default::default(),
log_only,
token_freeze_time,
solver,
}
}

Expand Down Expand Up @@ -120,6 +126,10 @@ impl Detector {
tokens = ?new_unsupported_tokens,
"mark tokens as unsupported"
);
metrics::get()
.bad_tokens_detected
.with_label_values(&[&self.solver.0, "metrics"])
.inc_by(new_unsupported_tokens.len() as u64);
}
}
}
Expand All @@ -133,7 +143,13 @@ mod tests {
#[tokio::test]
async fn unfreeze_bad_tokens() {
const FREEZE_DURATION: Duration = Duration::from_millis(50);
let detector = Detector::new(0.5, 2, false, FREEZE_DURATION);
let detector = Detector::new(
0.5,
2,
false,
FREEZE_DURATION,
solver::Name("mysolver".to_string()),
);

let token_a = eth::TokenAddress(eth::ContractAddress(H160([1; 20])));
let token_b = eth::TokenAddress(eth::ContractAddress(H160([2; 20])));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
},
eth,
},
infra,
infra::{self, observe::metrics},
},
futures::FutureExt,
model::interaction::InteractionData,
Expand Down Expand Up @@ -100,6 +100,8 @@ impl Detector {
}
Ok(TokenQuality::Bad { reason }) => {
tracing::debug!(reason, token=?sell_token.0, "cache token as unsupported");
// All solvers share the same cache for the simulation detector, so there is no need to specify the solver name here.
metrics::get().bad_tokens_detected.with_label_values(&["any", "simulation"]).inc();
inner
.cache
.update_quality(sell_token, false, now);
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/infra/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl Api {
bad_token_config.metrics_strategy_required_measurements,
bad_token_config.metrics_strategy_log_only,
bad_token_config.metrics_strategy_token_freeze_time,
name.clone(),
));
}

Expand Down
7 changes: 2 additions & 5 deletions crates/driver/src/infra/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ impl Ethereum {
.transport()
.execute(
"eth_createAccessList",
vec![
serde_json::to_value(&tx).unwrap(),
serde_json::Value::String("latest".into()),
],
vec![serde_json::to_value(&tx).unwrap()],
)
.await?;
if let Some(err) = json.get("error") {
Expand All @@ -210,7 +207,7 @@ impl Ethereum {
gas_price: self.simulation_gas_price().await,
..Default::default()
},
Some(ethcontract::BlockNumber::Latest),
None,
)
.await
.map(Into::into)
Expand Down
3 changes: 3 additions & 0 deletions crates/driver/src/infra/observe/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub struct Metrics {
/// The results of the mempool submission.
#[metric(labels("mempool", "result"))]
pub mempool_submission: prometheus::IntCounterVec,
/// How many tokens detected by specific solver and strategy.
#[metric(labels("solver", "strategy"))]
pub bad_tokens_detected: prometheus::IntCounterVec,
}

/// Setup the metrics registry.
Expand Down
2 changes: 1 addition & 1 deletion crates/driver/src/infra/observe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use {
url::Url,
};

mod metrics;
pub mod metrics;

/// Setup the observability. The log argument configures the tokio tracing
/// framework.
Expand Down
Loading

0 comments on commit c7efc2d

Please sign in to comment.