Skip to content

Commit

Permalink
Merge branch 'main' into bumpGasFeeOnResubmit
Browse files Browse the repository at this point in the history
  • Loading branch information
3shv authored Mar 7, 2025
2 parents 2c34707 + 30d4c68 commit 6537b95
Show file tree
Hide file tree
Showing 42 changed files with 1,095 additions and 331 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/app-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde_with = { workspace = true }
primitive-types = { workspace = true }
hex = { workspace = true }
hex-literal = { workspace = true }
number = { path = "../number" }

[dev-dependencies]
ethcontract = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions crates/app-data/src/app_data.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use {
crate::{AppDataHash, Hooks, app_data_hash::hash_full_app_data},
anyhow::{Context, Result, anyhow},
number::serialization::HexOrDecimalU256,
primitive_types::{H160, U256},
serde::{Deserialize, Deserializer, Serialize, Serializer, de},
serde_with::serde_as,
std::{fmt, fmt::Display},
};

Expand Down Expand Up @@ -32,6 +34,7 @@ pub struct ProtocolAppData {
/// 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.
#[serde_as]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
#[cfg_attr(any(test, feature = "test_helpers"), derive(Serialize))]
pub struct Flashloan {
Expand All @@ -43,6 +46,7 @@ pub struct Flashloan {
/// Which token to flashloan.
pub token: H160,
/// How much of the token to flashloan.
#[serde_as(as = "HexOrDecimalU256")]
pub amount: U256,
}

Expand Down
1 change: 1 addition & 0 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ pub async fn run(args: Arguments) {
domain::ProtocolFees::new(&args.fee_policies, args.fee_policy_max_partner_fee),
cow_amm_registry.clone(),
args.run_loop_native_price_timeout,
eth.contracts().settlement().address(),
);

let liveness = Arc::new(Liveness::new(args.max_auction_age));
Expand Down
38 changes: 34 additions & 4 deletions crates/autopilot/src/solvable_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub struct SolvableOrdersCache {
protocol_fees: domain::ProtocolFees,
cow_amm_registry: cow_amm::Registry,
native_price_timeout: Duration,
settlement_contract: H160,
}

type Balances = HashMap<Query, U256>;
Expand All @@ -123,6 +124,7 @@ impl SolvableOrdersCache {
protocol_fees: domain::ProtocolFees,
cow_amm_registry: cow_amm::Registry,
native_price_timeout: Duration,
settlement_contract: H160,
) -> Arc<Self> {
let self_ = Arc::new(Self {
min_order_validity_period,
Expand All @@ -139,6 +141,7 @@ impl SolvableOrdersCache {
protocol_fees,
cow_amm_registry,
native_price_timeout,
settlement_contract,
});
self_
}
Expand Down Expand Up @@ -181,7 +184,7 @@ impl SolvableOrdersCache {
)
};

let orders = orders_with_balance(orders, &balances);
let orders = orders_with_balance(orders, &balances, self.settlement_contract);
let removed = counter.checkpoint("insufficient_balance", &orders);
invalid_order_uids.extend(removed);

Expand Down Expand Up @@ -507,10 +510,23 @@ async fn find_invalid_signature_orders(

/// Removes orders that can't possibly be settled because there isn't enough
/// balance.
fn orders_with_balance(mut orders: Vec<Order>, balances: &Balances) -> Vec<Order> {
fn orders_with_balance(
mut orders: Vec<Order>,
balances: &Balances,
settlement_contract: H160,
) -> Vec<Order> {
// Prefer newer orders over older ones.
orders.sort_by_key(|order| std::cmp::Reverse(order.metadata.creation_date));
orders.retain(|order| {
if order.data.receiver.as_ref() == Some(&settlement_contract) {
// TODO: replace with proper detection logic
// for now we assume that all orders with the settlement contract
// as the receiver are flashloan orders which unlock the necessary
// funds via a pre-interaction that can't succeed in our balance
// fetching simulation logic.
return true;
}

let balance = match balances.get(&Query::from_order(order)) {
None => return false,
Some(balance) => *balance,
Expand Down Expand Up @@ -1351,6 +1367,7 @@ mod tests {

#[test]
fn orders_with_balance_() {
let settlement_contract = H160([1; 20]);
let orders = vec![
// enough balance for sell and fee
Order {
Expand Down Expand Up @@ -1396,18 +1413,31 @@ mod tests {
},
..Default::default()
},
// considered flashloan order because of special receiver
Order {
data: OrderData {
sell_token: H160::from_low_u64_be(6),
sell_amount: 200.into(),
fee_amount: 0.into(),
partially_fillable: true,
receiver: Some(settlement_contract),
..Default::default()
},
..Default::default()
},
];
let balances = [
(Query::from_order(&orders[0]), 2.into()),
(Query::from_order(&orders[1]), 1.into()),
(Query::from_order(&orders[2]), 1.into()),
(Query::from_order(&orders[3]), 0.into()),
(Query::from_order(&orders[4]), 0.into()),
]
.into_iter()
.collect();
let expected = &[0, 2];
let expected = &[0, 2, 4];

let filtered = orders_with_balance(orders.clone(), &balances);
let filtered = orders_with_balance(orders.clone(), &balances, settlement_contract);
assert_eq!(filtered.len(), expected.len());
for index in expected {
let found = filtered.iter().any(|o| o.data == orders[*index].data);
Expand Down
3 changes: 2 additions & 1 deletion crates/contracts/artifacts/AaveFlashLoanSolverWrapper.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions crates/contracts/artifacts/FlashLoanRouter.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion crates/contracts/artifacts/IAavePool.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/contracts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ fn main() {
generate_contract("ERC20");
generate_contract("ERC20Mintable");
generate_contract("ERC3156FlashLoanSolverWrapper");
generate_contract("FlashLoanRouter");
generate_contract_with_config("GPv2AllowListAuthentication", |builder| {
builder
.contract_mod_override("gpv2_allow_list_authentication")
Expand Down
1 change: 1 addition & 0 deletions crates/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ include_contracts! {
ERC20;
ERC20Mintable;
ERC3156FlashLoanSolverWrapper;
FlashLoanRouter;
GPv2AllowListAuthentication;
GPv2Settlement;
GnosisSafe;
Expand Down
Loading

0 comments on commit 6537b95

Please sign in to comment.